aboutsummaryrefslogtreecommitdiff
path: root/snag/src/download_db.c
diff options
context:
space:
mode:
authorJack Jamison <jackqjamison@gmail.com>2026-07-22 17:27:04 -0400
committerJack Jamison <jackqjamison@gmail.com>2026-07-22 17:27:04 -0400
commitf253f5e3d606702688deecea2162f1c5289d277c (patch)
treecc4cdb6bb53a64d0127adbb174556d0e1a2a1cef /snag/src/download_db.c
parentdfff353cf081024f9e145629dd51ef08526bb54d (diff)
actually download file to correct location
Diffstat (limited to 'snag/src/download_db.c')
-rw-r--r--snag/src/download_db.c20
1 files changed, 11 insertions, 9 deletions
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 <string.h>
#include <errno.h>
-// 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]);