From cb193d0f1891551066df5f19da6a29684125fb4a Mon Sep 17 00:00:00 2001 From: ngn Date: Mon, 8 Jul 2024 07:24:39 +0300 Subject: [PATCH] update: new examples --- examples/Makefile | 10 +++-- examples/client/common.h | 4 ++ examples/client/{main.c => install.c} | 19 ++++----- examples/client/remove.c | 55 +++++++++++++++++++++++++++ include/ctx.h | 12 +++--- locale/tr/LC_MESSAGES/libmp.po | 2 +- src/ctx/find.c | 22 ----------- src/ctx/install.c | 5 +++ src/ctx/pool.c | 23 ++++++++++- src/ctx/resolve.c | 4 +- src/database/database.c | 12 +++--- src/database/package.c | 3 +- src/package/depend.c | 11 +++++- 13 files changed, 130 insertions(+), 52 deletions(-) create mode 100644 examples/client/common.h rename examples/client/{main.c => install.c} (75%) create mode 100644 examples/client/remove.c delete mode 100644 src/ctx/find.c diff --git a/examples/Makefile b/examples/Makefile index cf49f65..639ad42 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,11 +1,15 @@ CC = gcc -all: ../dist/example_client ../dist/example_server +all: ../dist/client_install ../dist/client_remove ../dist/server -../dist/example_client: client/*.c ../dist/libmp.so +../dist/client_install: client/install.c client/*.h ../dist/libmp.so mkdir -pv ../dist $(CC) -L../dist $< -lmp -o $@ -../dist/example_server: server/*.c ../dist/libmp.so +../dist/client_remove: client/remove.c client/*.h ../dist/libmp.so + mkdir -pv ../dist + $(CC) -L../dist $< -lmp -o $@ + +../dist/server: server/*.c ../dist/libmp.so mkdir -pv ../dist $(CC) -L../dist $< -lmp -o $@ diff --git a/examples/client/common.h b/examples/client/common.h new file mode 100644 index 0000000..6a5a48e --- /dev/null +++ b/examples/client/common.h @@ -0,0 +1,4 @@ +#pragma once +#define DATA_DIR "/tmp/data" +#define TEMP_DIR "/tmp/temp" +#define ROOT_DIR "/tmp/root" diff --git a/examples/client/main.c b/examples/client/install.c similarity index 75% rename from examples/client/main.c rename to examples/client/install.c index 07c5615..b8ac55e 100644 --- a/examples/client/main.c +++ b/examples/client/install.c @@ -1,25 +1,22 @@ #include "../../include/all.h" +#include "./common.h" #include #include #include -#define DATA_DIR "/tmp/data" -#define TEMP_DIR "/tmp/temp" -#define ROOT_DIR "/tmp/root" - int main(int argc, char *argv[]) { int ret = EXIT_FAILURE; lm_pkg_t *pkg = NULL; lm_ctx_t ctx; - if (argc != 2) { - printf("usage: %s \n", argv[0]); + if (argc != 4) { + printf("usage: %s \n", argv[0]); return ret; } if (!mkdir_ifnot(ROOT_DIR)) { - printf("failed to create root dir: %s", ROOT_DIR); + printf("failed to create root dir: %s\n", ROOT_DIR); return ret; } @@ -40,7 +37,7 @@ int main(int argc, char *argv[]) { goto end; } - if (lm_ctx_pool_add(&ctx, "base", argv[1]) == NULL) { + if (lm_ctx_pool_add(&ctx, argv[2], argv[3]) == NULL) { printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error()); goto end; } @@ -48,16 +45,20 @@ int main(int argc, char *argv[]) { lm_ctx_ping(&ctx, NULL, NULL); lm_ctx_sync(&ctx, true, NULL, NULL); - if ((pkg = lm_ctx_find(&ctx, "which", NULL)) == NULL) { + if ((pkg = lm_ctx_pool_find(&ctx, argv[1], NULL)) == NULL) { printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } + printf("downloading package: %s (%s)...\n", pkg->name, pkg->version); + if (!lm_ctx_download(&ctx, pkg, NULL, NULL)) { printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } + printf("installing package: %s (%s)...\n", pkg->name, pkg->version); + if (!lm_ctx_install(&ctx, pkg, NULL, NULL)) { printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error()); goto end; diff --git a/examples/client/remove.c b/examples/client/remove.c new file mode 100644 index 0000000..ae55f26 --- /dev/null +++ b/examples/client/remove.c @@ -0,0 +1,55 @@ +#include "../../include/all.h" +#include "./common.h" + +#include +#include +#include + +int main(int argc, char *argv[]) { + int ret = EXIT_FAILURE; + lm_pkg_t pkg; + lm_ctx_t ctx; + + if (argc != 2) { + printf("usage: %s \n", argv[0]); + return ret; + } + + if (!mkdir_ifnot(ROOT_DIR)) { + printf("failed to create root dir: %s\n", ROOT_DIR); + return ret; + } + + lm_ctx_init(&ctx); + + if (!lm_ctx_set_data(&ctx, DATA_DIR)) { + printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + + if (!lm_ctx_set_temp(&ctx, TEMP_DIR)) { + printf("failed to set temp dir: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + + if (!lm_ctx_set_root(&ctx, ROOT_DIR)) { + printf("failed to set root dir: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + + if (!lm_ctx_database_find(&ctx, &pkg, argv[1])) { + printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + + if (!lm_ctx_remove(&ctx, &pkg, NULL, NULL)) { + printf("failed to remove package: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + + ret = EXIT_SUCCESS; + +end: + lm_ctx_free(&ctx); + return ret; +} diff --git a/include/ctx.h b/include/ctx.h index 50ba5ce..2278c4c 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -61,8 +61,6 @@ void lm_ctx_free(lm_ctx_t *ctx); /* #################### ## main fucntions ## #################### */ -lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version); // find package by name (and version) - lm_ctx_resolve_list_t *lm_ctx_resolve( lm_ctx_t *ctx, lm_pkg_t *pkg); // resolves a package and returns a list of packages to install lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // returns the next package in the list @@ -88,10 +86,12 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads); /* #################### ## pool fucntions ## #################### */ -lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); // add a pool to the ctx pool list -bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); // remove a pool from the ctx pool list -void lm_ctx_pool_clear(lm_ctx_t *ctx); // clear all the pools in the ctx pool list -lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find pool by URL +lm_pkg_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name, char *version); // find a package in ctx pool list +lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); // add a pool to the ctx pool list +bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); // remove a pool from the ctx pool list +void lm_ctx_pool_clear(lm_ctx_t *ctx); // clear all the pools in the ctx pool list +lm_pool_t *lm_ctx_pool_by_name(lm_ctx_t *ctx, char *name); // find pool by name +lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find pool by URL /* ######################## ## database fucntions ## diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index ec22ffe..a65f913 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-08 05:46+0300\n" +"POT-Creation-Date: 2024-07-08 07:22+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/find.c b/src/ctx/find.c deleted file mode 100644 index f705931..0000000 --- a/src/ctx/find.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "../../include/error.h" -#include "../../include/pool.h" -#include "../../include/ctx.h" - -lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version){ - if(NULL == ctx || (NULL == name && NULL == version)){ - lm_error_set(LM_ERR_ArgNULL); - return NULL; - } - - lm_pool_t *cur = ctx->pools; - lm_pkg_t *found = NULL; - while(cur != NULL){ - if((found = lm_pool_package_find(cur, name, version)) != NULL) - break; - cur = cur->next; - } - - if(NULL == found) - lm_error_set(LM_ERR_PkgNotFound); - return found; -} diff --git a/src/ctx/install.c b/src/ctx/install.c index 763417b..497d10a 100644 --- a/src/ctx/install.c +++ b/src/ctx/install.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -242,6 +243,8 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call goto end; } + // TODO: run install script + ret = true; end: free(line); @@ -255,5 +258,7 @@ end: lm_database_files_del(ctx->db, pkg); } + unlink(pkg->paths.archive); + unlink(pkg->paths.signature); return ret; } diff --git a/src/ctx/pool.c b/src/ctx/pool.c index fac5d16..02fd448 100644 --- a/src/ctx/pool.c +++ b/src/ctx/pool.c @@ -7,6 +7,27 @@ #include #include +lm_pkg_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name, char *version){ + if(NULL == ctx || (NULL == name && NULL == version)){ + lm_error_set(LM_ERR_ArgNULL); + return NULL; + } + + lm_pool_t *cur = ctx->pools; + lm_pkg_t *found = NULL; + + while(cur != NULL){ + if((found = lm_pool_package_find(cur, name, version)) != NULL) + break; + cur = cur->next; + } + + if(NULL == found) + lm_error_set(LM_ERR_PkgNotFound); + + return found; +} + lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) { if(NULL == name){ lm_error_set(LM_ERR_ArgNULL); @@ -57,7 +78,7 @@ void lm_ctx_pool_clear(lm_ctx_t *ctx) { ctx->pools = NULL; } -lm_pool_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name) { +lm_pool_t *lm_ctx_pool_by_name(lm_ctx_t *ctx, char *name) { lm_pool_t *cur = ctx->pools; while (NULL != cur) { if (eq(cur->name, name)) diff --git a/src/ctx/resolve.c b/src/ctx/resolve.c index 0e497fa..9169869 100644 --- a/src/ctx/resolve.c +++ b/src/ctx/resolve.c @@ -17,7 +17,9 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg) list->resolving = pkglist_add(list->resolving, pkg); for(int i = 0; pkg->depends[i] != NULL; i++){ - lm_pkg_t *depend = lm_ctx_find(ctx, pkg->depends[i], NULL); + // TODO: check if exists in the db before checking pools + lm_pkg_t *depend = lm_ctx_pool_find(ctx, pkg->depends[i], NULL); + if(NULL == depend){ lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name); return false; diff --git a/src/database/database.c b/src/database/database.c index 5cc8b20..bc39c06 100644 --- a/src/database/database.c +++ b/src/database/database.c @@ -21,10 +21,10 @@ char *queries[] = { "INSERT INTO packages VALUES (?, ?, ?, ?, ?)", // QUERY_SELECT_PACKAGE_SINGLE - "SELECT * FROM packages WHERE name = '?'", + "SELECT * FROM packages WHERE name = ?", // QUERY_DELETE_PACKAGE_SINGLE - "DELETE FROM packages WHERE name = '?'", + "DELETE FROM packages WHERE name = ?", // QUERY_SELECT_PACKAGE_ALL "SELECT * FROM packages", @@ -40,19 +40,19 @@ char *queries[] = { "INSERT INTO files VALUES (?, ?, ?, ?)", // QUERY_DELETE_FILE_ALL - "DELETE FROM files WHERE package = '?'", + "DELETE FROM files WHERE package = ?", // QUERY_SELECT_FILE_SINGLE "SELECT * FROM files WHERE path = ?", // QUERY_SELECT_FILE_ALL - "SELECT * FROM files WHERE package = '?'", + "SELECT * FROM files WHERE package = ?", // QUERY_UPDATE_FILE_1 - "UPDATE files SET keep = 1 WHERE path = '?'", + "UPDATE files SET keep = 1 WHERE path = ?", // QUERY_UPDATE_FILE_2 - "UPDATE files SET keep = 0 WHERE path = '?'", + "UPDATE files SET keep = 0 WHERE path = ?", }; lm_database_t *lm_database_new(char *path){ diff --git a/src/database/package.c b/src/database/package.c index 43e1a0c..6702d62 100644 --- a/src/database/package.c +++ b/src/database/package.c @@ -32,9 +32,10 @@ bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg){ pdebug(__func__, "failed to convert depends to string for inserting %s: %s", pkg->name, lm_strerror()); goto end; } + pdebug(__func__, "depend list for %s: %s", pkg->name, depends); sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DEPENDS, depends, strlen(depends), SQLITE_STATIC); - if(sqlite3_step(db->packages_st) != SQLITE_DONE){ + if(!lm_database_step_all(db->packages_st)){ pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db)); lm_error_set(LM_ERR_DbSqlInsertFail); goto end; diff --git a/src/package/depend.c b/src/package/depend.c index 0b0a7d6..763c8c4 100644 --- a/src/package/depend.c +++ b/src/package/depend.c @@ -87,11 +87,12 @@ bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ if(i == 0){ memcpy(buffer, pkg->depends[i], depsz); + bufsz += depsz; continue; } buffer[bufsz++] = ','; - memcpy(buffer+bufsz, pkg->depends, depsz); + memcpy(buffer+bufsz, pkg->depends[i], depsz); bufsz += depsz; } @@ -111,9 +112,15 @@ bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){ return true; char *save = NULL, *dep = NULL; - while((dep = strtok_r(buffer, ",", &save)) != NULL) + dep = strtok_r(buffer, ",", &save); + + if(NULL == dep) + return true; + + do { if(!lm_package_depend_add(pkg, dep)) return false; + }while((dep = strtok_r(NULL, ",", &save)) != NULL); return true; }