diff --git a/examples/client/install.c b/examples/client/install.c index b8ac55e..85f6cc5 100644 --- a/examples/client/install.c +++ b/examples/client/install.c @@ -6,9 +6,10 @@ #include int main(int argc, char *argv[]) { - int ret = EXIT_FAILURE; - lm_pkg_t *pkg = NULL; - lm_ctx_t ctx; + lm_ctx_resolve_list_t *list = NULL; + lm_pkg_t *pkg = NULL, *cur = NULL; + lm_ctx_t ctx; + int ret = EXIT_FAILURE; if (argc != 4) { printf("usage: %s \n", argv[0]); @@ -50,23 +51,33 @@ int main(int argc, char *argv[]) { goto end; } - printf("downloading package: %s (%s)...\n", pkg->name, pkg->version); + printf("resolving 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()); + if ((list = lm_ctx_resolve(&ctx, pkg)) == NULL) { + printf("failed to resolve package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } - printf("installing package: %s (%s)...\n", pkg->name, pkg->version); + while ((cur = lm_ctx_resolve_next(list)) != NULL) { + printf("downloading package: %s (%s)...\n", cur->name, cur->version); + if (!lm_ctx_download(&ctx, cur, NULL, NULL)) { + printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } + } - if (!lm_ctx_install(&ctx, pkg, NULL, NULL)) { - printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error()); - goto end; + while ((cur = lm_ctx_resolve_next(list)) != NULL) { + printf("installing package: %s (%s)...\n", cur->name, cur->version); + if (!lm_ctx_install(&ctx, cur, NULL, NULL)) { + printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error()); + goto end; + } } ret = EXIT_SUCCESS; end: + lm_ctx_resolve_free(list); lm_ctx_free(&ctx); return ret; } diff --git a/examples/pools/base/LIST b/examples/pools/base/LIST deleted file mode 100644 index 5a6839b..0000000 Binary files a/examples/pools/base/LIST and /dev/null differ diff --git a/examples/pools/base/grep_3.11.mpf b/examples/pools/base/grep_3.11.mpf deleted file mode 100644 index 1d6780d..0000000 Binary files a/examples/pools/base/grep_3.11.mpf and /dev/null differ diff --git a/examples/pools/base/grep_3.11.mpf.sig b/examples/pools/base/grep_3.11.mpf.sig deleted file mode 100644 index 978c456..0000000 Binary files a/examples/pools/base/grep_3.11.mpf.sig and /dev/null differ diff --git a/examples/pools/base/which_2.21.mpf b/examples/pools/base/which_2.21.mpf deleted file mode 100644 index 2f85825..0000000 Binary files a/examples/pools/base/which_2.21.mpf and /dev/null differ diff --git a/examples/pools/base/which_2.21.mpf.sig b/examples/pools/base/which_2.21.mpf.sig deleted file mode 100644 index 8b08b22..0000000 Binary files a/examples/pools/base/which_2.21.mpf.sig and /dev/null differ diff --git a/examples/pools/base/INFO b/examples/pools/test/INFO similarity index 76% rename from examples/pools/base/INFO rename to examples/pools/test/INFO index 1c41929..c8bb1bc 100644 --- a/examples/pools/base/INFO +++ b/examples/pools/test/INFO @@ -1,4 +1,4 @@ -[base] -size = 314121 +[test] +size = 3936 maintainer = ngn pubkey = F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D diff --git a/examples/pools/test/LIST b/examples/pools/test/LIST new file mode 100644 index 0000000..b792dee Binary files /dev/null and b/examples/pools/test/LIST differ diff --git a/examples/pools/test/bar_1.mpf b/examples/pools/test/bar_1.mpf new file mode 100644 index 0000000..e58efc5 Binary files /dev/null and b/examples/pools/test/bar_1.mpf differ diff --git a/examples/pools/test/bar_1.mpf.sig b/examples/pools/test/bar_1.mpf.sig new file mode 100644 index 0000000..05b581a Binary files /dev/null and b/examples/pools/test/bar_1.mpf.sig differ diff --git a/examples/pools/test/dummy_1.mpf b/examples/pools/test/dummy_1.mpf new file mode 100644 index 0000000..ea63f46 Binary files /dev/null and b/examples/pools/test/dummy_1.mpf differ diff --git a/examples/pools/test/dummy_1.mpf.sig b/examples/pools/test/dummy_1.mpf.sig new file mode 100644 index 0000000..f910a0e Binary files /dev/null and b/examples/pools/test/dummy_1.mpf.sig differ diff --git a/examples/pools/test/foo_1.mpf b/examples/pools/test/foo_1.mpf new file mode 100644 index 0000000..cb3059a Binary files /dev/null and b/examples/pools/test/foo_1.mpf differ diff --git a/examples/pools/test/foo_1.mpf.sig b/examples/pools/test/foo_1.mpf.sig new file mode 100644 index 0000000..b689295 Binary files /dev/null and b/examples/pools/test/foo_1.mpf.sig differ diff --git a/examples/pools/test/test_1.mpf b/examples/pools/test/test_1.mpf new file mode 100644 index 0000000..a32c670 Binary files /dev/null and b/examples/pools/test/test_1.mpf differ diff --git a/examples/pools/test/test_1.mpf.sig b/examples/pools/test/test_1.mpf.sig new file mode 100644 index 0000000..f4ed0d3 Binary files /dev/null and b/examples/pools/test/test_1.mpf.sig differ diff --git a/examples/server/main.c b/examples/server/main.c index 3c458a5..1fb19f3 100644 --- a/examples/server/main.c +++ b/examples/server/main.c @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) { goto end; } - if (lm_ctx_pool_add(&ctx, "base", "mptp://127.0.0.1:5858") == NULL) { + if (lm_ctx_pool_add(&ctx, "test", "mptp://127.0.0.1:5858") == NULL) { printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error()); goto end; } diff --git a/include/ctx.h b/include/ctx.h index fcca97c..a756ebf 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -63,13 +63,12 @@ void lm_ctx_free(lm_ctx_t *ctx); #################### */ 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 -void lm_ctx_resolve_free( - lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve +lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list); // returns the next package in the list +void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list); // frees the resolved list returned by lm_ctx_resolve -lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx); // returns a list of packages to update -lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // returns the next package in the list -void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list); // frees the update list returned by lm_ctx_update +lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx); // returns a list of packages to update +lm_pkg_t *lm_ctx_update_next(lm_ctx_update_list_t *list); // returns the next package in the list +void lm_ctx_update_free(lm_ctx_update_list_t *list); // frees the update list returned by lm_ctx_update bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback, void *data); // downloads a single package diff --git a/include/error.h b/include/error.h index 8b65354..7916b0f 100644 --- a/include/error.h +++ b/include/error.h @@ -132,6 +132,7 @@ typedef enum lm_error { LM_ERR_InstallBackChdirFail = 124, LM_ERR_InstallStatusFail = 125, LM_ERR_InstallScriptFail = 126, + LM_ERR_PkgBreaks = 127, } lm_error_t; typedef struct lm_error_desc { diff --git a/include/package.h b/include/package.h index c0addcd..fe5dbe8 100644 --- a/include/package.h +++ b/include/package.h @@ -23,6 +23,7 @@ size_t lm_package_depend_strlen(lm_pkg_t *pkg); bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer); bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer); void lm_package_depend_free(lm_pkg_t *pkg); +bool lm_package_depends(lm_pkg_t *pkg, char *dep); size_t lm_package_keep_count(lm_pkg_t *pkg); bool lm_package_keep_add(lm_pkg_t *pkg, char *path); diff --git a/include/util.h b/include/util.h index 307e949..de8a08c 100644 --- a/include/util.h +++ b/include/util.h @@ -40,5 +40,6 @@ char *join_alloc(const char *base, const char *pth); bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg); lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg); -lm_pkg_t *pkglist_add(lm_pkg_t *list, lm_pkg_t *pkg); +lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg); +lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg); void pkglist_free(lm_pkg_t *list); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 7869b6e..cf01fb2 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 13:05+0300\n" +"POT-Creation-Date: 2024-07-10 16:36+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -554,3 +554,8 @@ msgstr "" #, c-format msgid "failed to run the package install script: %s" msgstr "" + +#: src/error.c:152 +#, c-format +msgid "removing package breaks %s" +msgstr "" diff --git a/src/ctx/download.c b/src/ctx/download.c index d57eefd..3a43c82 100644 --- a/src/ctx/download.c +++ b/src/ctx/download.c @@ -60,6 +60,8 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version); join(path, pool->url.path, name); + pdebug(__func__, "connecting to %s:%d for download", pool->url.host, pool->url.port); + if((sock = lm_mptp_client_connect(pool->url.host, pool->url.port)) < 0) return false; diff --git a/src/ctx/remove.c b/src/ctx/remove.c index bb80530..8012d41 100644 --- a/src/ctx/remove.c +++ b/src/ctx/remove.c @@ -27,9 +27,19 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba if(!lm_ctx_database_init(ctx)) return false; + lm_pkg_t cur; + + while (lm_database_package_next(ctx->db, &cur)) { + if(lm_package_depends(&cur, pkg->name)){ + lm_error_set(LM_ERR_PkgBreaks, cur.name); + lm_database_package_next_free(ctx->db, &cur); + return false; + } + } + char *path = NULL, *hash = NULL, *fpath = NULL; - size_t total = 0, current = 0; bool in_keep = false, ret = false; + size_t total = 0, current = 0; total = lm_database_files_count(ctx->db, pkg); @@ -54,7 +64,7 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba next: free(fpath); fpath = NULL; - + if(NULL != callback && !callback(ctx, pkg, fpath, ++current, total, data)) goto end; } diff --git a/src/ctx/resolve.c b/src/ctx/resolve.c index 0ef730d..54357dc 100644 --- a/src/ctx/resolve.c +++ b/src/ctx/resolve.c @@ -14,7 +14,7 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg) if(NULL == pkg->depends) goto end; - list->resolving = pkglist_add(list->resolving, pkg); + list->resolving = pkglist_add_top(list->resolving, pkg); lm_pkg_t *depend = NULL; for(int i = 0; pkg->depends[i] != NULL; i++){ @@ -34,13 +34,13 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg) return false; } - if(!__lm_ctx_resolve(ctx, list, pkg)) + if(!__lm_ctx_resolve(ctx, list, depend)) return false; } list->resolving = pkglist_del(list->resolving, pkg); end: - list->packages = pkglist_add(list->packages, pkg); + list->packages = pkglist_add_end(list->packages, pkg); list->count++; return true; } @@ -52,10 +52,7 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg){ } lm_ctx_resolve_list_t *list = malloc(sizeof(lm_ctx_resolve_list_t)); - list->resolving = NULL; - list->packages = NULL; - list->cur = NULL; - list->count = 0; + bzero(list, sizeof(lm_ctx_resolve_list_t)); if(!__lm_ctx_resolve(ctx, list, pkg)){ pkglist_free(list->resolving); @@ -66,11 +63,12 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg){ pkglist_free(list->resolving); list->resolving = NULL; + return list; } -lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){ - if(NULL == ctx || NULL == list){ +lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list){ + if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return NULL; } @@ -84,8 +82,8 @@ lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){ return list->cur; } -void lm_ctx_resolve_free(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list){ - if(NULL == ctx || NULL == list){ +void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list){ + if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return; } diff --git a/src/ctx/update.c b/src/ctx/update.c index 3d63199..faa60dc 100644 --- a/src/ctx/update.c +++ b/src/ctx/update.c @@ -32,8 +32,8 @@ lm_ctx_update_list_t *lm_ctx_update(lm_ctx_t *ctx){ return list; } -lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list){ - if(NULL == ctx || NULL == list){ +lm_pkg_t *lm_ctx_update_next(lm_ctx_update_list_t *list){ + if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return NULL; } @@ -46,8 +46,8 @@ lm_pkg_t *lm_ctx_update_next(lm_ctx_t *ctx, lm_ctx_update_list_t *list){ return list->packages[list->index++]; } -void lm_ctx_update_free(lm_ctx_t *ctx, lm_ctx_update_list_t *list){ - if(NULL == ctx || NULL == list){ +void lm_ctx_update_free(lm_ctx_update_list_t *list){ + if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return; } diff --git a/src/database/package.c b/src/database/package.c index 34c5ffe..b011455 100644 --- a/src/database/package.c +++ b/src/database/package.c @@ -147,16 +147,16 @@ bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){ return false; } - if(NULL == db->packages_st && + if(NULL != db->packages_st) + lm_package_free(pkg); + + else if(NULL == db->packages_st && sqlite3_prepare(db->packages_db, queries[QUERY_SELECT_PACKAGE_ALL], strlen(queries[QUERY_SELECT_PACKAGE_ALL]), &db->packages_st, NULL) != SQLITE_OK){ pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->packages_db)); lm_error_set(LM_ERR_DbSqlPrepareFail); return false; } - else if(NULL != db->packages_st) - lm_package_free(pkg); - if(sqlite3_step(db->packages_st) != SQLITE_ROW){ sqlite3_finalize(db->packages_st); db->packages_st = NULL; @@ -186,8 +186,10 @@ void lm_database_package_next_free(lm_database_t *db, lm_pkg_t *pkg){ return; } - if(NULL != db->packages_st) + if(NULL != db->packages_st){ sqlite3_finalize(db->packages_st); + db->packages_st = NULL; + } if(NULL != pkg) lm_package_free(pkg); diff --git a/src/error.c b/src/error.c index 4088475..ba76d29 100644 --- a/src/error.c +++ b/src/error.c @@ -149,6 +149,7 @@ void lm_error_set(lm_error_t code, ...) { .desc = _("failed to change directory to old directory after running install") }, {.code = LM_ERR_InstallStatusFail, .desc = _("install script returned a bad status code") }, {.code = LM_ERR_InstallScriptFail, .desc = _("failed to run the package install script: %s") }, + {.code = LM_ERR_PkgBreaks, .desc = _("removing package breaks %s") }, }; char *fmt = NULL; diff --git a/src/package/depend.c b/src/package/depend.c index 763c8c4..d7ef925 100644 --- a/src/package/depend.c +++ b/src/package/depend.c @@ -124,3 +124,18 @@ bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){ return true; } + +bool lm_package_depends(lm_pkg_t *pkg, char *dep){ + if(NULL == pkg || NULL == dep){ + lm_error_set(LM_ERR_ArgNULL); + return false; + } + + if(NULL == pkg->depends) + return false; + + for(int i = 0; pkg->depends[i] != NULL; i++) + if(eq(pkg->depends[i], dep)) + return true; + return false; +} diff --git a/src/util.c b/src/util.c index 252ca7e..9ae3fda 100644 --- a/src/util.c +++ b/src/util.c @@ -337,55 +337,66 @@ bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg) { } lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg) { + if (NULL == pkg) { + lm_error_set(LM_ERR_ArgNULL); + return list; + } + if (NULL == list) return list; - lm_pkg_t *cur = list; - lm_pkg_t *target = NULL; - int indx = 0; + if (eq(list->name, pkg->name)) { + list = NULL; + return list; + } - while (cur) { - if (eq(cur->name, pkg->name)) { - target = cur; + lm_pkg_t *cur = list; + lm_pkg_t *found = NULL; + + while (NULL != cur->next) { + if (eq(cur->next->name, pkg->name)) { + found = cur->next; + cur->next = cur->next->next; break; } cur = cur->next; - indx++; } - if (NULL == target) - return list; - - cur = list; - for (int i = 0; i < indx; i++) { - if (cur->next != NULL) - cur = cur->next; - } - - if (NULL == cur->next) - list = NULL; - else - cur->next = cur->next->next; - - free(target); + free(found); return list; } -lm_pkg_t *pkglist_add(lm_pkg_t *list, lm_pkg_t *pkg) { +lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg) { lm_pkg_t *new = malloc(sizeof(lm_pkg_t)); memcpy(new, pkg, sizeof(lm_pkg_t)); - new->next = NULL; - new->next = list; list = new; return list; } +lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg) { + lm_pkg_t *new = malloc(sizeof(lm_pkg_t)), *cur = list; + memcpy(new, pkg, sizeof(lm_pkg_t)); + + new->next = NULL; + + if (NULL == cur) { + list = new; + return list; + } + + while (cur->next != NULL) + cur = cur->next; + cur->next = new; + + return list; +} + void pkglist_free(lm_pkg_t *list) { - lm_pkg_t *cur = list; - while (cur) { - lm_pkg_t *old = cur; - cur = cur->next; + lm_pkg_t *cur = list, *old = NULL; + while (cur != NULL) { + old = cur; + cur = cur->next; free(old); } }