From 17d0447a55b971f66e383e5f3b0c12213949dbc9 Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Fri, 24 Jul 2026 10:35:44 -0400 Subject: unpack source function unpacks tarballs (very hard) --- snag/scripts/extract_tar_contents.sh | 38 ++++++++++++++++++++++++++++++++++++ snag/src/source_db.c | 24 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 snag/scripts/extract_tar_contents.sh (limited to 'snag') diff --git a/snag/scripts/extract_tar_contents.sh b/snag/scripts/extract_tar_contents.sh new file mode 100755 index 0000000..a50a1a7 --- /dev/null +++ b/snag/scripts/extract_tar_contents.sh @@ -0,0 +1,38 @@ +#!/bin/sh +set -e + +# extracts a tarball's contents to the output directory. +# if the tarball contains just one directory, extract those contents +# +# we can rewrite this script in c eventually, if we want + +if [ -z "$1" -o -z "$2" ]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +tarball="$1" +target="$2" + +is_one_directory() { + if [ $(echo $1 | wc -l) -ne 1 ]; then + return 1 + fi + case "$1" in + */) return 0 ;; + *) return 1 ;; + esac +} + +# get top level paths in tarball +tlps=$(tar -tf "$tarball" | grep -E '^(./)?[^/]+/?$') + +if is_one_directory "$tlps"; then + # if only one top level directory, extract it and rename it + tar xf "$tarball" + mv "$tlps" "$target" +else + # if a bunch of files or directories, extract them in a directory + mkdir $target + tar xf "$tarball" -C "$target" +fi diff --git a/snag/src/source_db.c b/snag/src/source_db.c index ab7ec99..86bab8b 100644 --- a/snag/src/source_db.c +++ b/snag/src/source_db.c @@ -9,6 +9,8 @@ #include #include +#define EXTRACT_TAR_SCRIPT "./scripts/extract_tar_contents.sh" + // libcrpyto context EVP_MD_CTX *mdctx = NULL; const EVP_MD *md = NULL; @@ -101,6 +103,28 @@ static size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void return written; } +bool unpack_source(source_info_t* source, char* dest_path) { + + char* source_path = get_source_path(source); + char* command = vastrcat(EXTRACT_TAR_SCRIPT, " ", source_path, " ", dest_path); + + // just run the unpack script. we can rewrite in c in the future + int ret = system(command); + if (ret == -1) { + die("failed to execute unpack command '%s': %s", command, strerror(errno)); + } else if (!WIFEXITED(ret)) { + die("failed to execute unpack command '%s' (didn't exit)", command); + } else if (WEXITSTATUS(ret) != 0) { + die("unpack command '%s' failed: exit status %d", command, WEXITSTATUS(ret)); + } + + free(command); + free(source_path); + + return true; +} + + void ensure_libcrypto() { if (mdctx == NULL) { mdctx = EVP_MD_CTX_new(); -- cgit v1.2.3