#include "hash.h" #include "package.h" #include "curl/curl.h" #include "stdio.h" #include "stdlib.h" #include "string.h" static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream) { size_t written = fwrite(ptr, size, nmemb, (FILE *)stream); return written; } int pow_download_file(char *path, char *url) { FILE *pagefile; CURL *curl = curl_easy_init(); if (!curl) { printf("failed to init curl\n"); return 1; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl, CURLOPT_USERAGENT, "pow! (codeberg.org/einfach/pow)"); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); pagefile = fopen(path, "wb"); if (!pagefile) { printf("failed to open file to download\n"); curl_easy_cleanup(curl); return 1; } curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile); CURLcode result = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(pagefile); return result; } int pow_download_source(pow_package_source *src, const char *prefix) { size_t len = strlen(prefix) + strlen(src->out) + 2; char *full_path = malloc(len); snprintf(full_path, len, "%s/%s", prefix, src->out); printf("downloading source file: %s", src->in); if (pow_download_file(full_path, src->in)) { printf("failed to download sources, aborting\n"); return 1; } if (pow_check_blake2b_hash(full_path, src->hash)) { printf("hash mismatch. something is going wrong with downloading the file " "or it may be tampered with.\n"); return 1; } printf("hash verification succeeded, going through."); return 0; }