From f253f5e3d606702688deecea2162f1c5289d277c Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Wed, 22 Jul 2026 17:27:04 -0400 Subject: actually download file to correct location --- snag/src/download_db.c | 20 +++++++++++--------- snag/src/util.c | 42 ++++++++++++++++++++++++++++++++++++++++++ snag/src/util.h | 4 ++++ 3 files changed, 57 insertions(+), 9 deletions(-) (limited to 'snag/src') diff --git a/snag/src/download_db.c b/snag/src/download_db.c index 3f51347..819474e 100644 --- a/snag/src/download_db.c +++ b/snag/src/download_db.c @@ -8,13 +8,16 @@ #include #include -// curl context -CURL *curl = NULL; +// note: fixed location of download database for now +const char download_db_path[] = "../downloads"; // libcrpyto context EVP_MD_CTX *mdctx = NULL; const EVP_MD *md = NULL; +// curl context +CURL *curl = NULL; + static size_t curl_write_cb(char* ptr, size_t size, size_t nmemb, void* usrdata) { // write the curl data to the file in usr data @@ -52,18 +55,17 @@ bool download_package(package_info_t* pkg) { // initialize up sha256 hash EVP_DigestInit_ex(mdctx, md, NULL); - // open file for writing and give it to the curl callback - // todo(jqj): get actual path with name - char* download_path = "download.tar.gz"; + // open file for writing + char* download_path = vastrcat(download_db_path, "/", pkg->attrs.id, "-", pkg->attrs.version, ".tar.gz"); FILE* download_file = fopen(download_path, "wb"); if (download_file == NULL) die("failed open download file for writing (%s): %s", download_path, strerror(errno)); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, download_file); - // make curl request + // make curl request with url and output file // callback writes to the file and feeds the hash curl_easy_setopt(curl, CURLOPT_URL, pkg->attrs.source_url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, download_file); CURLcode result = curl_easy_perform(curl); if(result != CURLE_OK) { log("failed to download package with curl: %s", curl_easy_strerror(result)); @@ -75,7 +77,7 @@ bool download_package(package_info_t* pkg) { unsigned int hash_len; EVP_DigestFinal_ex(mdctx, hash, &hash_len); - // get hexadecimal string of sha256 hash + // make hexadecimal string of sha256 hash char hash_hex[SHA256_DIGEST_LENGTH * 2 + 1]; for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(hash_hex + i*2,"%02x", hash[i]); diff --git a/snag/src/util.c b/snag/src/util.c index e79fd34..79c4b17 100644 --- a/snag/src/util.c +++ b/snag/src/util.c @@ -78,7 +78,49 @@ char* vastrcat_(int dummy, ...) { char* output = malloc(len + 1); if (!output) return NULL; char* p = output; + while ((arg = va_arg(args2, char*))) { + size_t alen = strlen(arg); + memcpy(p, arg, alen); + p += alen; + } + output[len] = '\0'; + va_end(args2); + + return output; +} + +char* join_path_(int dummy, ...) { + + va_list args; + va_list args2; + va_start(args, dummy); + va_copy(args2, args); + + // note(jqj): some path joining libraries reset if they encounter an + // absolute path, we could implement that easily, but I'm not + // sure about it + + // get len of output path + size_t len = 0; + char* arg = NULL; + int i = 0; while ((arg = va_arg(args, char*))) { + if (i > 0) len += 1; + len += strlen(arg) + 1; + ++i; + } + va_end(args); + + // allocate and build output + char* output = malloc(len + 1); + if (!output) return NULL; + char* p = output; + while ((arg = va_arg(args2, char*))) { + if (p != output) { + // add slash before every part except the first + *p = '/'; + ++p; + } size_t alen = strlen(arg); memcpy(p, arg, alen); p += alen; diff --git a/snag/src/util.h b/snag/src/util.h index 1a05ab0..081b570 100644 --- a/snag/src/util.h +++ b/snag/src/util.h @@ -28,4 +28,8 @@ void print(const char* format, ...) __attribute__((format(printf, 1, 2))); #define vastrcat(...) vastrcat_(0, __VA_ARGS__, NULL) char* vastrcat_(int dummy, ...); +// posix path functions +#define join_path(...) join_path_(0, __VA_ARGS__, NULL) +char* join_path_(int dummy, ...); + #endif -- cgit v1.2.3