summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c36
-rw-r--r--src/net.c16
-rw-r--r--src/pkgman.c94
3 files changed, 90 insertions, 56 deletions
diff --git a/src/main.c b/src/main.c
index c866c89..914279c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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 },
};
diff --git a/src/net.c b/src/net.c
index 6d566f2..bad3b1c 100644
--- a/src/net.c
+++ b/src/net.c
@@ -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;
}