diff options
Diffstat (limited to 'snag/src/download_db.c')
| -rw-r--r-- | snag/src/download_db.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/snag/src/download_db.c b/snag/src/download_db.c index c1e2e1b..c355566 100644 --- a/snag/src/download_db.c +++ b/snag/src/download_db.c @@ -6,6 +6,7 @@ #include <openssl/evp.h> #include <openssl/sha.h> #include <string.h> +#include <unistd.h> #include <errno.h> // note: fixed location of download database for now @@ -29,10 +30,22 @@ static size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void return written; } -bool download_package(package_info_t* pkg) { +bool package_get_download(package_info_t* pkg, package_download_t* download) { + memset(download, 0, sizeof(*download)); + // construct path of download + 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) { + download->download_path = download_path; + return true; + } else { + print("Downloading %s\n", pkg->attrs.source_url); + } + + // initialize curl if it isn't already if (curl == NULL) { - // initialize curl if it isn't already CURLcode result = curl_global_init(CURL_GLOBAL_ALL); if (result != CURLE_OK) die("failed to initialize curl: %s", curl_easy_strerror(result)); curl = curl_easy_init(); // note(jqj): for now we reuse one easy curl object @@ -43,8 +56,8 @@ bool download_package(package_info_t* pkg) { /* curl_global_cleanup(); */ } + // initialize libcrypto context if it isn't already if (mdctx == NULL) { - // initialize libcrypto context if it isn't already mdctx = EVP_MD_CTX_new(); md = EVP_sha256(); @@ -59,7 +72,6 @@ bool download_package(package_info_t* pkg) { // 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"); FILE* download_file = fopen(download_path, "wb"); if (download_file == NULL) die("failed open download file for writing (%s): %s", download_path, strerror(errno)); @@ -73,7 +85,7 @@ bool download_package(package_info_t* pkg) { if(result != CURLE_OK) { // todo(jqj): handle better, maybe try again print("failed to download package with curl: %s", curl_easy_strerror(result)); - return false; + goto fail; } // get sha256 hash @@ -88,20 +100,25 @@ bool download_package(package_info_t* pkg) { } 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 %s: %s\n", pkg->attrs.id, hash_hex); + print("checksum verified for download: %s\n", hash_hex); + download->download_path = download_path; return true; } else { - print("FAIL: bad checksum for %s: %s\n", pkg->attrs.id, hash_hex); - return false; + print("FAIL: bad checksum for download: %s\n", hash_hex); + goto fail; } } 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); - return true; + goto fail; } - return true; + fail: + free(download_path); + return false; } |
