From d9bc99629dd3f8624df98ead06f400c4ffb3880b Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Thu, 23 Jul 2026 14:03:35 -0400 Subject: asserts and cleanup --- snag/src/download_db.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'snag/src/download_db.c') diff --git a/snag/src/download_db.c b/snag/src/download_db.c index c355566..e0aa85e 100644 --- a/snag/src/download_db.c +++ b/snag/src/download_db.c @@ -19,8 +19,7 @@ const EVP_MD *md = NULL; // curl context CURL *curl = NULL; -static size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void* usrdata) -{ +static size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void* usrdata) { // write the curl data to the file in usr data size_t written = fwrite(ptr, size, nmemb, (FILE *)usrdata); @@ -34,10 +33,14 @@ bool package_get_download(package_info_t* pkg, package_download_t* download) { memset(download, 0, sizeof(*download)); // construct path of download + // note(jqj): extension is fixed to tar gz until the switch out download + // attrs for the swenu api download command which will specify + // more information char* download_path = vastrcat(download_db_path, "/", pkg->attrs.id, "-", pkg->attrs.version, ".tar.gz"); // return download if it already exists in database if (access(download_path, F_OK) == 0) { + // todo(jqj): verify checksum anyway download->download_path = download_path; return true; } else { @@ -69,9 +72,6 @@ bool package_get_download(package_info_t* pkg, package_download_t* download) { EVP_DigestInit_ex(mdctx, md, NULL); // open file for writing - // note(jqj): extension is fixed to tar gz until the switch out download - // attrs for the swenu api download command which will specify - // more information FILE* download_file = fopen(download_path, "wb"); if (download_file == NULL) die("failed open download file for writing (%s): %s", download_path, strerror(errno)); @@ -82,6 +82,7 @@ bool package_get_download(package_info_t* pkg, package_download_t* download) { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, download_file); CURLcode result = curl_easy_perform(curl); + fclose(download_file); if(result != CURLE_OK) { // todo(jqj): handle better, maybe try again print("failed to download package with curl: %s", curl_easy_strerror(result)); @@ -100,25 +101,19 @@ bool package_get_download(package_info_t* pkg, package_download_t* download) { } hash_hex[SHA256_DIGEST_LENGTH * 2] = '\0'; - // todo(jqj): collapse codepath and delete download if it doesn't match checksum - - // confirm that hash matches checksum - if (pkg->attrs.source_checksum != NULL) { - if (strcmp(pkg->attrs.source_checksum, hash_hex) == 0) { - print("checksum verified for download: %s\n", hash_hex); - download->download_path = download_path; - return true; - } else { - print("FAIL: bad checksum for download: %s\n", hash_hex); - goto fail; - } + // confirm that checksum matches + assert(pkg->attrs.source_checksum != NULL); + if (strcmp(pkg->attrs.source_checksum, hash_hex) == 0) { + print("checksum verified for download: %s\n", hash_hex); + download->download_path = download_path; + return true; } else { - // todo(jqj): not sure if we will allow packages without hashes yet - print("WARNING: could not verify checksum for %s download because none was provided\n", pkg->attrs.id); + print("FAIL: bad checksum for download: %s\n", hash_hex); goto fail; } fail: + if (access(download_path, F_OK) == 0) remove(download_path); free(download_path); return false; } -- cgit v1.2.3