Skip to content

Commit

Permalink
Replace with C (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
keith authored Nov 15, 2021
1 parent 9ef1323 commit 72953b8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 376 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BasedOnStyle: LLVM
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.cache
/build
/target
compile_commands.json
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
project(dyld-shared-cache-extractor LANGUAGES C)
cmake_minimum_required(VERSION 3.21)

set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

add_executable(dyld-shared-cache-extractor dyld-shared-cache-extractor.c)
target_compile_options(dyld-shared-cache-extractor PRIVATE -g -Weverything)
install(TARGETS dyld-shared-cache-extractor)
277 changes: 0 additions & 277 deletions Cargo.lock

This file was deleted.

10 changes: 0 additions & 10 deletions Cargo.toml

This file was deleted.

75 changes: 75 additions & 0 deletions dyld-shared-cache-extractor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define PATH_SIZE 200

static void (*extract)(const char *cache_path, const char *output_path,
void (^progress)(int, int));

static int get_library_path(char *output) {
FILE *pipe = popen("xcrun --sdk iphoneos --show-sdk-platform-path", "r");
if (!pipe)
return 1;

if (fgets(output, PATH_SIZE, pipe) == NULL)
return 1;

output[strlen(output) - 1] = '\0';
strcat(output, "/usr/lib/dsc_extractor.bundle");

return pclose(pipe);
}

__attribute__((noreturn))
__attribute__((__format__(__printf__, 1, 0))) static void
fail(const char *error, ...) {
va_list args;
va_start(args, error);
vfprintf(stderr, error, args);
va_end(args);
exit(EXIT_FAILURE);
}

static void extract_shared_cache(const char *library_path,
const char *cache_path,
const char *output_path) {
void *handle = dlopen(library_path, RTLD_LAZY);
if (!handle)
fail("error: failed to load bundle: %s\n", library_path);

*(void **)(&extract) =
dlsym(handle, "dyld_shared_cache_extract_dylibs_progress");

if (!extract)
fail("error: failed to load function from bundle: %s\n", library_path);

extract(cache_path, output_path, ^void(int completed, int total) {
printf("extracted %d/%d\n", completed, total);
});
}

int main(int argc, char *argv[]) {
if (argc != 3)
fail("Usage: %s <shared-cache-path> <output-path>\n", argv[0]);

const char *shared_cache = argv[1];
if (access(shared_cache, R_OK) != 0)
fail("error: shared cache path doesn't exist: %s\n", shared_cache);

char library_path[PATH_SIZE];
if (get_library_path(library_path) != 0)
fail("error: failed to fetch Xcode path\n");

if (access(library_path, R_OK) != 0)
fail("error: dsc_extractor.bundle wasn't found at expected path, Xcode "
"might have changed this location: %s\n",
library_path);

const char *output_path = argv[2];
extract_shared_cache(library_path, shared_cache, output_path);
return 0;
}
Loading

0 comments on commit 72953b8

Please sign in to comment.