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 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'snag/src/download_db.c') 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]); -- cgit v1.2.3