diff options
| author | 0x221E <0x221E@0xinfinity.dev> | 2026-05-13 16:50:15 +0200 |
|---|---|---|
| committer | 0x221E <0x221E@0xinfinity.dev> | 2026-05-13 16:52:52 +0200 |
| commit | a532b3eb7e8610fb51cc703c9634532704e9b381 (patch) | |
| tree | 7ad12176952943f39dfb3e2d38a6243e2f3ea807 | |
| parent | 922d616c65573bdbbb52f1e2fa663d283d0fbd35 (diff) | |
| download | pkgman-dev.tar.gz | |
Added option to change mirror, tmp, staging directoryfrom the user
interface of the pkgman library. Added TRY and TRY_IFNOT macros for
better code readability.
| -rw-r--r-- | include/err.h | 17 | ||||
| -rw-r--r-- | include/net.h | 1 | ||||
| -rw-r--r-- | include/pkgman.h | 15 | ||||
| -rw-r--r-- | src/main.c | 36 | ||||
| -rw-r--r-- | src/net.c | 16 | ||||
| -rw-r--r-- | src/pkgman.c | 94 |
6 files changed, 119 insertions, 60 deletions
diff --git a/include/err.h b/include/err.h index e377889..cc556fa 100644 --- a/include/err.h +++ b/include/err.h @@ -1,6 +1,20 @@ #ifndef ERR_H #define ERR_H +#define TRY(cond, err) \ + do { \ + if (!(cond)) \ + return -err; \ + } while(0) + +#define TRY_IFNOT(cond, err, stmt) \ + do { \ + if (!(cond)) { \ + stmt \ + return -err; \ + } \ + } while(0) + #define SUCCESS 0 #define ERR 1 @@ -12,8 +26,9 @@ #define USAGE 7 // Usage screen displayed instead of cmd exec. #define URLINITERR 8 // URL Struct initialization failed #define URLPATHERR 9 // URL add path operation failed -#define INTEGRITYERR 10 +#define INTEGRITYERR 10 #define FORKERR 11 +#define PKGCONFERR 12 // Invalid package config error /** * Base errors. diff --git a/include/net.h b/include/net.h index b2b415f..7cf6b5e 100644 --- a/include/net.h +++ b/include/net.h @@ -22,5 +22,6 @@ int net_init(); void net_shutdown(); int net_send_request(char *url, int write_opts, void *userdata); +int net_download(const char *url, const char *dst); #endif diff --git a/include/pkgman.h b/include/pkgman.h index 0d17622..ad95528 100644 --- a/include/pkgman.h +++ b/include/pkgman.h @@ -1,8 +1,17 @@ #ifndef PKGMAN_H #define PKGMAN_H -int pkgman_upstream_check(const char *pkg); -int pkgman_install_pkg(const char *pkg); -int pkgman_upstream_integrity_download(const char *pkg); +typedef struct url url; + +struct pkgman_config { + char *dir_tmp; + char *dir_staging; + char *upstream; +}; + +int pkgman_upstream_check(struct pkgman_config *pc, const char *pkg); +int pkgman_install_pkg(struct pkgman_config *pc, const char *pkg); +int pkgman_upstream_integrity_download(struct pkgman_config *pc, + const char *pkg); #endif @@ -51,35 +51,40 @@ int cmd_install(int argc, char **argv) if (argc < 1) { install_usage(); return USAGE; - } + } + + struct pkgman_config pc; + pc.dir_tmp = "/tmp/pkgman"; + pc.dir_staging = "/var/pkgman"; + pc.upstream = "https://packages.0xinfinity.dev"; char *pkg = argv[0]; - int ret = pkgman_upstream_check(pkg); + int ret = pkgman_upstream_check(&pc, pkg); + TRY(ret == SUCCESS, ret); - if (ret != SUCCESS) - return -PKGNOTFND; + ret = pkgman_upstream_integrity_download(&pc, pkg); + TRY(ret == SUCCESS, ret); - ret = pkgman_upstream_integrity_download(pkg); - - if (ret != SUCCESS) - return ret; - - ret = pkgman_install_pkg(pkg); - - if (ret != SUCCESS) - return ret; + ret = pkgman_install_pkg(&pc, pkg); + TRY(ret == SUCCESS, ret); return SUCCESS; } int cmd_build(int argc, char** argv) { - // ZSTD_compress("test", 60, "aaa.pkg", 30, 3); - printf("Produced a tar file!"); + // ZSTD_compress("test", 60, "aaa.pkg", 30, 3); + printf("NOT IMPLEMENTED: Build command issued!\n"); return SUCCESS; } +int cmd_remove(int argc, char** argv) +{ + printf("NOT IMPLEMENTED: Remove command issued!\n"); + return SUCCESS; +} + typedef int (*cmd_fn)(int, char**); struct cmd_entry @@ -92,6 +97,7 @@ struct cmd_entry struct cmd_entry table[] = { { SV("build"), cmd_build }, { SV("install"), cmd_install }, + { SV("remove"), cmd_remove }, { SV(NULL), NULL }, }; @@ -93,3 +93,19 @@ int net_send_request(char *url, int write_opts, void* userdata) return SUCCESS; } + +int net_download(const char *url, const char *dst) +{ + struct net_file_write_data fwdata; + + fwdata.file = fopen(dst, "w"); // change to tmp dir + + if (!fwdata.file) { + return -PKGNOTFND; + } + + net_send_request(url, WRITE_OPT_FILE, (void*)&fwdata); + + fclose(fwdata.file); + return SUCCESS; +} diff --git a/src/pkgman.c b/src/pkgman.c index d22d15c..7f5912e 100644 --- a/src/pkgman.c +++ b/src/pkgman.c @@ -18,19 +18,32 @@ #include <gpgme.h> #include <archive.h> #include <archive_entry.h> +#include <errno.h> -#define UPSTREAM_URL "https://packages.0xinfinity.dev" +#define PKGMAN_CONFIG_CHECK(x) \ + do { \ + TRY(pc->dir_tmp != NULL, PKGCONFERR); \ + TRY(pc->dir_staging != NULL, PKGCONFERR); \ + TRY(pc->upstream != NULL, PKGCONFERR); \ + } while(0) -int pkgman_upstream_check(const char *pkg) +int pkgman_upstream_check(struct pkgman_config *pc, const char *pkg) { assert(pkg != NULL); + PKGMAN_CONFIG_CHECK(pc); - int ret = -ERR; + int ret = -ERR; + + struct url upstream; + url_init(&upstream, pc->upstream); + url_append_path(&upstream, "list"); struct net_write_data mem = { 0 }; - if ((ret = net_send_request(UPSTREAM_URL "/list", - WRITE_OPT_MEMORY, - (void*)&mem)) != SUCCESS) + ret = net_send_request(upstream.buffer, + WRITE_OPT_MEMORY, + (void*)&mem); + + if (ret != SUCCESS) goto cleanup; struct um_user_data userdata = { 0 }; @@ -39,7 +52,7 @@ int pkgman_upstream_check(const char *pkg) struct string_view parser_src = {0}; parser_src.buf = mem.buffer; parser_src.len = mem.size; - + parser_init(&parser, &parser_src, &backend, (void*)&userdata); parser_parse(&parser); @@ -57,7 +70,7 @@ int pkgman_upstream_check(const char *pkg) ret = SUCCESS; } } - + if (!found) { printf("Package '%s' not found!\n", pkg); ret = -PKGNOTFND; @@ -76,82 +89,80 @@ int pkgman_upstream_check(const char *pkg) cleanup: free(mem.buffer); + url_free(&upstream); return ret; } -int pkgman_download(const char *url, const char *dst) +int pkgman_install_pkg(struct pkgman_config *pc, const char *pkg) { - struct net_file_write_data fwdata; - - printf("url: %s, dst: %s\n", url, dst); - - fwdata.file = fopen(dst, "w"); // change to tmp dir - - if (!fwdata.file) { - return -PKGNOTFND; - } - - net_send_request(url, WRITE_OPT_FILE, (void*)&fwdata); + assert(pc != NULL); + PKGMAN_CONFIG_CHECK(pc); - fclose(fwdata.file); - return SUCCESS; -} - -int pkgman_install_pkg(const char *pkg) -{ int ret = -ERR; struct url path = {0}; - url_init(&path, "/tmp/pkgman"); + url_init(&path, pc->dir_tmp); url_append_path(&path, pkg); struct url dst_path = {0}; - url_init(&dst_path, "/tmp/pkgman"); + url_init(&dst_path, pc->dir_tmp); url_append_path(&dst_path, pkg); url_append(&dst_path, "-extract/"); - mkdir(dst_path.buffer, 0777); + if (mkdir(dst_path.buffer, 0777) != 0 && errno != EEXIST) { + ret = -ERR; + goto cleanup; + } pkg_extract(path.buffer, &dst_path); struct string_view recipe_out = {0}; sv_init(&recipe_out, ""); + + int cret = cookbook_recipe_run(&dst_path, "artifacts", &recipe_out); - if (int cret = cookbook_recipe_run(&dst_path, "test", &recipe_out) - != 0) { + if (cret != 0) { printf("Artifacts recipe failed with error code: %d", cret); ret = -ERR; goto cleanup; } - printf("Not displaying artifacts...\n"); + printf("Not displaying artifacts...\n"); sv_free(&recipe_out); sv_init(&recipe_out, ""); - if (int cret = cookbook_recipe_run(&dst_path, "install", &recipe_out) - != 0) { + cret = cookbook_recipe_run(&dst_path, "install", &recipe_out); + + if (cret != 0) { printf("Install recipe failed with error code: %d", cret); ret = -ERR; goto cleanup; } + printf("Build script response: %s\n", recipe_out.buf); + ret = SUCCESS; + cleanup: sv_free(&recipe_out); url_free(&dst_path); url_free(&path); - return SUCCESS; + return ret; } -int pkgman_upstream_integrity_download(const char *pkg) +int pkgman_upstream_integrity_download(struct pkgman_config *pc, + const char *pkg) { + assert(pc != NULL); + PKGMAN_CONFIG_CHECK(pc); + int ret = -ERR; struct url url_pkg = {0}; struct url url_sig = {0}; - url_init(&url_pkg, UPSTREAM_URL); + url_init(&url_pkg, pc->upstream); url_append_path(&url_pkg, pkg); url_append(&url_pkg, ".tar.zstd"); @@ -173,22 +184,23 @@ int pkgman_upstream_integrity_download(const char *pkg) //// - if(pkgman_download(url_pkg.buffer, path_pkg.buffer) != SUCCESS) { + if(net_download(url_pkg.buffer, path_pkg.buffer) != SUCCESS) { ret = -PKGNOTFND; goto cleanup; } - printf("'%s' package downloaded.\n", pkg); + printf("Package '%s' is downloaded from upstream.\n", pkg); // Download signature file - if(pkgman_download(url_sig.buffer, path_sig.buffer) != SUCCESS) { + if(net_download(url_sig.buffer, path_sig.buffer) != SUCCESS) { ret = -PKGNOTFND; goto cleanup; } printf("Signature for '%s' downloaded.\n", url_sig.buffer); - if(crypto_verify_integrity(path_sig.buffer, path_pkg.buffer) != SUCCESS) { + if(crypto_verify_integrity(path_sig.buffer, path_pkg.buffer) + != SUCCESS) { ret = -INTEGRITYERR; goto cleanup; } |
