Skip to content

Commit 72953b8

Browse files
authored
Replace with C (#2)
1 parent 9ef1323 commit 72953b8

File tree

7 files changed

+87
-376
lines changed

7 files changed

+87
-376
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: LLVM

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
.cache
2+
/build
13
/target
4+
compile_commands.json

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project(dyld-shared-cache-extractor LANGUAGES C)
2+
cmake_minimum_required(VERSION 3.21)
3+
4+
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
5+
6+
add_executable(dyld-shared-cache-extractor dyld-shared-cache-extractor.c)
7+
target_compile_options(dyld-shared-cache-extractor PRIVATE -g -Weverything)
8+
install(TARGETS dyld-shared-cache-extractor)

Cargo.lock

Lines changed: 0 additions & 277 deletions
This file was deleted.

Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

dyld-shared-cache-extractor.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <dlfcn.h>
2+
#include <stdarg.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
8+
#define PATH_SIZE 200
9+
10+
static void (*extract)(const char *cache_path, const char *output_path,
11+
void (^progress)(int, int));
12+
13+
static int get_library_path(char *output) {
14+
FILE *pipe = popen("xcrun --sdk iphoneos --show-sdk-platform-path", "r");
15+
if (!pipe)
16+
return 1;
17+
18+
if (fgets(output, PATH_SIZE, pipe) == NULL)
19+
return 1;
20+
21+
output[strlen(output) - 1] = '\0';
22+
strcat(output, "/usr/lib/dsc_extractor.bundle");
23+
24+
return pclose(pipe);
25+
}
26+
27+
__attribute__((noreturn))
28+
__attribute__((__format__(__printf__, 1, 0))) static void
29+
fail(const char *error, ...) {
30+
va_list args;
31+
va_start(args, error);
32+
vfprintf(stderr, error, args);
33+
va_end(args);
34+
exit(EXIT_FAILURE);
35+
}
36+
37+
static void extract_shared_cache(const char *library_path,
38+
const char *cache_path,
39+
const char *output_path) {
40+
void *handle = dlopen(library_path, RTLD_LAZY);
41+
if (!handle)
42+
fail("error: failed to load bundle: %s\n", library_path);
43+
44+
*(void **)(&extract) =
45+
dlsym(handle, "dyld_shared_cache_extract_dylibs_progress");
46+
47+
if (!extract)
48+
fail("error: failed to load function from bundle: %s\n", library_path);
49+
50+
extract(cache_path, output_path, ^void(int completed, int total) {
51+
printf("extracted %d/%d\n", completed, total);
52+
});
53+
}
54+
55+
int main(int argc, char *argv[]) {
56+
if (argc != 3)
57+
fail("Usage: %s <shared-cache-path> <output-path>\n", argv[0]);
58+
59+
const char *shared_cache = argv[1];
60+
if (access(shared_cache, R_OK) != 0)
61+
fail("error: shared cache path doesn't exist: %s\n", shared_cache);
62+
63+
char library_path[PATH_SIZE];
64+
if (get_library_path(library_path) != 0)
65+
fail("error: failed to fetch Xcode path\n");
66+
67+
if (access(library_path, R_OK) != 0)
68+
fail("error: dsc_extractor.bundle wasn't found at expected path, Xcode "
69+
"might have changed this location: %s\n",
70+
library_path);
71+
72+
const char *output_path = argv[2];
73+
extract_shared_cache(library_path, shared_cache, output_path);
74+
return 0;
75+
}

0 commit comments

Comments
 (0)