diff options
| author | Jack Jamison <jackqjamison@gmail.com> | 2026-07-23 14:03:35 -0400 |
|---|---|---|
| committer | Jack Jamison <jackqjamison@gmail.com> | 2026-07-23 14:03:35 -0400 |
| commit | d9bc99629dd3f8624df98ead06f400c4ffb3880b (patch) | |
| tree | 8c1ab815ffa8aeb87fc30bfcb0b5f3a03d1e9252 | |
| parent | fa4344e5dedff14af2bce55d29a272495dd6bbb5 (diff) | |
asserts and cleanup
| -rw-r--r-- | snag/src/build_db.c | 2 | ||||
| -rw-r--r-- | snag/src/build_db.h | 2 | ||||
| -rw-r--r-- | snag/src/download_db.c | 33 | ||||
| -rw-r--r-- | snag/src/download_db.h | 2 | ||||
| -rw-r--r-- | snag/src/main.c | 4 | ||||
| -rw-r--r-- | snag/src/package.h | 2 | ||||
| -rw-r--r-- | snag/src/package_db.c | 2 | ||||
| -rw-r--r-- | snag/src/package_db.h | 2 | ||||
| -rw-r--r-- | snag/src/util.c | 13 | ||||
| -rw-r--r-- | snag/src/util.h | 13 | ||||
| -rw-r--r-- | todo.txt | 4 |
11 files changed, 42 insertions, 37 deletions
diff --git a/snag/src/build_db.c b/snag/src/build_db.c index f2f4e01..2d0500b 100644 --- a/snag/src/build_db.c +++ b/snag/src/build_db.c @@ -10,7 +10,7 @@ #define PKG_BUILD_CMD "./scripts/run_pkg_build.sh" -void package_run_install(const package_info_t* info) { +void package_run_build(const package_info_t* info) { // PIPE and FORK to run install script int pipefds[2] = {0}; diff --git a/snag/src/build_db.h b/snag/src/build_db.h index ed57c43..007f794 100644 --- a/snag/src/build_db.h +++ b/snag/src/build_db.h @@ -3,6 +3,6 @@ #include "package.h" -void package_run_install(const package_info_t* info); +void package_run_build(const package_info_t* info); #endif 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; } diff --git a/snag/src/download_db.h b/snag/src/download_db.h index 0f6e181..d45817f 100644 --- a/snag/src/download_db.h +++ b/snag/src/download_db.h @@ -1,8 +1,6 @@ #ifndef DOWNLOAD_DB_H #define DOWNLOAD_DB_H -#include <stdbool.h> - #include "package.h" typedef struct { diff --git a/snag/src/main.c b/snag/src/main.c index 921c31a..c5a0736 100644 --- a/snag/src/main.c +++ b/snag/src/main.c @@ -48,10 +48,10 @@ int cmd_install(char* cmd_name, ArgParser* parser) { // download or retrieve package download package_download_t download; - package_get_download(&info, &download); + if (!package_get_download(&info, &download)) continue; // run the package install - package_run_install(&info); + package_run_build(&info); } } diff --git a/snag/src/package.h b/snag/src/package.h index bdbf0cb..a96c5c1 100644 --- a/snag/src/package.h +++ b/snag/src/package.h @@ -1,6 +1,8 @@ #ifndef PACKAGE_H #define PACKAGE_H +#include <stdbool.h> + // this struct is the "handle" of a package to be passed into other package // functions typedef struct { diff --git a/snag/src/package_db.c b/snag/src/package_db.c index e0d7e8a..533b66a 100644 --- a/snag/src/package_db.c +++ b/snag/src/package_db.c @@ -110,7 +110,7 @@ bool load_package_info(char* pkgid, package_info_t* info) { case 'c': info->attrs.source_checksum = &info->attrs.buffer[i]; break; } - for(;info->attrs.buffer[i] && i < bytes_read; i++); + while(info->attrs.buffer[i] != '\0' && i < bytes_read) i++; i++; } diff --git a/snag/src/package_db.h b/snag/src/package_db.h index 52dad2b..98c3e6e 100644 --- a/snag/src/package_db.h +++ b/snag/src/package_db.h @@ -1,8 +1,6 @@ #ifndef PACKAGE_DB_H #define PACKAGE_DB_H -#include <stdbool.h> - #include "package.h" // loads and allocates package attributes for the info struct. does not do diff --git a/snag/src/util.c b/snag/src/util.c index df1a8fd..025e583 100644 --- a/snag/src/util.c +++ b/snag/src/util.c @@ -8,7 +8,7 @@ #if !DEBUG void die(const char* format, ...) { #else -void _die(int line, const char* file, const char* format, ...) { +void _die(const char* file, int line, const char* format, ...) { fprintf(stderr, "%s:%d: error: ", file, line); #endif va_list args; @@ -26,13 +26,20 @@ void* emalloc(size_t size, char* alloc_reason) { return d; } #else -void* _emalloc(int line, const char* file, size_t size, char* alloc_reason) { +void* _emalloc(const char* file, int line, size_t size, char* alloc_reason) { void* d = malloc(size); - if (d == NULL) _die(line, file, "failed to allocate '%s': %s", alloc_reason, strerror(errno)); + if (d == NULL) _die(file, line, "failed to allocate '%s': %s", alloc_reason, strerror(errno)); return d; } #endif +#if DEBUG +void _failassert(const char* file, int line, char* contents) { + fprintf(stderr, "%s:%d: assertion failed: '%s'", file, line, contents); + exit(1); +} +#endif + void printout(const char* format, ...) { va_list args; va_start(args, format); diff --git a/snag/src/util.h b/snag/src/util.h index 1300ace..0c684a4 100644 --- a/snag/src/util.h +++ b/snag/src/util.h @@ -3,18 +3,23 @@ #include <stdarg.h> #include <stddef.h> +#include <stdbool.h> // utility for errors #if !DEBUG void die(const char* format, ...) __attribute__((format(printf, 1, 2))); void* emalloc(size_t size, char* alloc_reason); + #define assert(condition) ((void)0) #else + #define die(fmt, ...) _die(__FILE__, __LINE__, fmt, ##__VA_ARGS__) + void _die(const char* file, int line, const char* format, ...) __attribute__((format(printf, 3, 4))); - #define die(fmt, ...) _die(__LINE__, __FILE__, fmt, ##__VA_ARGS__) - void _die(int line, const char* file, const char* format, ...) __attribute__((format(printf, 3, 4))); + #define emalloc(size, alloc_reason) _emalloc(__FILE__, __LINE__, size, alloc_reason) + void* _emalloc(const char* file, int line, size_t size, char* alloc_reason); + + #define assert(condition) do { if (!(condition)) _failassert(__FILE__, __LINE__, #condition); } while(0) + void _failassert(const char* file, int line, char* contents); - #define emalloc(size, alloc_reason) _emalloc(__LINE__, __FILE__, size, alloc_reason) - void* _emalloc(int line, const char* file, size_t size, char* alloc_reason); #endif // functions for outputting and displaying text, log is used for nonroutine bad things @@ -3,8 +3,8 @@ SNAG - [x] multiple subcommand handling and proper argument parsing - [x] add output and error functions for regular program output, make emalloc also have file info, also gen sh to configure - [x] download packages to download db and verify hash -- [ ] cache download and actually create build environment for packages in build script -- [ ] rename "download" db to source or archive db +- [ ] cache download and rename "download" db to source, archive, or release db +- [ ] make unpack source function and run build function actually work - [ ] snag api - [ ] proper download with snag api (supports git download) - [ ] proper installation |
