|
| 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