From 9cd4eb990544ce308d30c59307d3a258f6dab100 Mon Sep 17 00:00:00 2001 From: ngn Date: Wed, 31 Jul 2024 21:16:19 +0300 Subject: [PATCH] update: refactoring and small fixes --- examples/client/check.c | 16 +-- examples/client/install.c | 8 +- examples/client/list.c | 13 ++- examples/client/remove.c | 20 ++-- examples/client/update.c | 7 +- include/all.h | 1 - include/ctx.h | 40 +++---- include/database.h | 94 ++++++++------- include/error.h | 4 +- include/mptp.h | 3 +- include/package.h | 52 ++++++--- include/pool.h | 23 +++- include/types.h | 51 -------- include/util.h | 4 +- locale/tr/LC_MESSAGES/libmp.po | 39 ++++--- src/ctx/check.c | 12 +- src/ctx/database.c | 26 ++--- src/ctx/download.c | 12 +- src/ctx/install.c | 85 +++++++++----- src/ctx/remove.c | 44 +++---- src/ctx/resolve.c | 12 +- src/ctx/serve.c | 8 +- src/ctx/update.c | 56 ++++----- src/database/changes.c | 22 ++-- src/database/database.c | 23 ++-- src/database/entry.c | 208 +++++++++++++++++++++++++++++++++ src/database/files.c | 44 +++---- src/database/package.c | 197 ------------------------------- src/error.c | 8 +- src/package/data.c | 40 +++---- src/package/depend.c | 81 +++++++------ src/package/keep.c | 49 ++++---- src/package/package.c | 47 ++------ src/package/path.c | 30 ++--- src/package/verify.c | 12 +- src/pool/info.c | 8 +- src/pool/list.c | 28 ++--- src/pool/package.c | 18 +-- src/pool/path.c | 45 ++++--- src/pool/pool.c | 4 + src/util.c | 37 +++++- 41 files changed, 779 insertions(+), 752 deletions(-) delete mode 100644 include/types.h create mode 100644 src/database/entry.c delete mode 100644 src/database/package.c diff --git a/examples/client/check.c b/examples/client/check.c index 9e866e7..8c18105 100644 --- a/examples/client/check.c +++ b/examples/client/check.c @@ -6,11 +6,11 @@ #include int main(int argc, char *argv[]) { - int ret = EXIT_FAILURE; - lm_pkg_t pkg; - lm_ctx_t ctx; + int ret = EXIT_FAILURE; + lm_entry_t entry; + lm_ctx_t ctx; - lm_package_init(&pkg); + lm_entry_init(&entry); if (argc != 2) { printf("usage: %s \n", argv[0]); @@ -34,14 +34,14 @@ int main(int argc, char *argv[]) { goto end; } - if (!lm_ctx_database_find(&ctx, &pkg, argv[1], NULL)) { + if (!lm_ctx_database_find(&ctx, &entry, argv[1], NULL)) { printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } - printf("checking %s (%s)...\n", pkg.name, pkg.version); + printf("checking %s (%s)...\n", entry.name, entry.version); - if (!lm_ctx_check(&ctx, &pkg, NULL, NULL)) { + if (!lm_ctx_check(&ctx, &entry, NULL, NULL)) { printf("failed to verify the package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) { ret = EXIT_SUCCESS; end: - lm_package_free(&pkg); + lm_entry_free(&entry); lm_ctx_free(&ctx); return ret; } diff --git a/examples/client/install.c b/examples/client/install.c index 2af92bc..df81d75 100644 --- a/examples/client/install.c +++ b/examples/client/install.c @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) { goto end; } - printf("resolving package: %s (%s)...\n", pkg->name, pkg->version); + printf("resolving package: %s (%s)...\n", pkg->data.name, pkg->data.version); if ((list = lm_ctx_resolve(&ctx, pkg, NULL)) == NULL) { printf("failed to resolve package: %s (%d)\n", lm_strerror(), lm_error()); @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { } while ((cur = lm_ctx_resolve_next(list)) != NULL) { - printf("downloading package: %s (%s)...\n", cur->name, cur->version); + printf("downloading package: %s (%s)...\n", cur->data.name, cur->data.version); if (!lm_ctx_download(&ctx, cur, NULL, NULL)) { printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error()); goto end; @@ -67,8 +67,8 @@ int main(int argc, char *argv[]) { } 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("installing package: %s (%s)...\n", cur->data.name, cur->data.version); + if (!lm_ctx_install(&ctx, cur, false, NULL, NULL)) { printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } diff --git a/examples/client/list.c b/examples/client/list.c index e2e916a..1f724ee 100644 --- a/examples/client/list.c +++ b/examples/client/list.c @@ -6,10 +6,11 @@ #include int main(int argc, char *argv[]) { - lm_ctx_t ctx; - lm_pkg_t pkg; - int ret = EXIT_FAILURE; + lm_ctx_t ctx; + lm_entry_t entry; + int ret = EXIT_FAILURE; + lm_entry_init(&entry); lm_ctx_init(&ctx); if (!lm_ctx_set_data(&ctx, DATA_DIR)) { @@ -19,10 +20,10 @@ int main(int argc, char *argv[]) { printf("listing installed packages:\n"); - while (lm_ctx_database_next(&ctx, &pkg)) - printf("%s (%s)\n", pkg.name, pkg.version); + while (lm_ctx_database_next(&ctx, &entry)) + printf("%s (%s)\n", entry.name, entry.version); end: - lm_ctx_database_next_free(&ctx, &pkg); + lm_ctx_database_next_free(&ctx, &entry); return ret; } diff --git a/examples/client/remove.c b/examples/client/remove.c index 94c3baa..b139ed2 100644 --- a/examples/client/remove.c +++ b/examples/client/remove.c @@ -6,11 +6,11 @@ #include int main(int argc, char *argv[]) { - int ret = EXIT_FAILURE; - lm_pkg_t pkg; - lm_ctx_t ctx; + int ret = EXIT_FAILURE; + lm_entry_t entry; + lm_ctx_t ctx; - lm_package_init(&pkg); + lm_entry_init(&entry); if (argc != 2) { printf("usage: %s \n", argv[0]); @@ -39,19 +39,19 @@ int main(int argc, char *argv[]) { goto end; } - if (!lm_ctx_database_find(&ctx, &pkg, argv[1], NULL)) { + if (!lm_ctx_database_find(&ctx, &entry, argv[1], NULL)) { printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } - if (!lm_ctx_removeable(&ctx, &pkg)) { - printf("cannot remove package: %s (%d)", lm_strerror(), lm_error()); + if (!lm_ctx_removeable(&ctx, &entry)) { + printf("cannot remove package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } - printf("removing package: %s (%s)...\n", pkg.name, pkg.version); + printf("removing package: %s (%s)...\n", entry.name, entry.version); - if (!lm_ctx_remove(&ctx, &pkg, NULL, NULL)) { + if (!lm_ctx_remove(&ctx, &entry, NULL, NULL)) { printf("failed to remove package: %s (%d)\n", lm_strerror(), lm_error()); goto end; } @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { ret = EXIT_SUCCESS; end: - lm_package_free(&pkg); + lm_entry_free(&entry); lm_ctx_free(&ctx); return ret; } diff --git a/examples/client/update.c b/examples/client/update.c index a7237d7..ba017c0 100644 --- a/examples/client/update.c +++ b/examples/client/update.c @@ -8,8 +8,9 @@ int main(int argc, char *argv[]) { lm_ctx_update_list_t *list = NULL; int ret = EXIT_FAILURE; - lm_pkg_t pkg, *old = NULL, *new = NULL; - lm_ctx_t ctx; + lm_pkg_t *new = NULL; + lm_entry_t *old = NULL; + lm_ctx_t ctx; if (argc != 3) { printf("usage: %s \n", argv[0]); @@ -74,7 +75,7 @@ int main(int argc, char *argv[]) { goto end; } - if (!lm_ctx_install(&ctx, new, NULL, NULL)) { + if (!lm_ctx_install(&ctx, new, false, NULL, NULL)) { printf("failed to install new package: %s", lm_strerror()); goto end; } diff --git a/include/all.h b/include/all.h index 6cf4314..1d2840e 100644 --- a/include/all.h +++ b/include/all.h @@ -27,5 +27,4 @@ #include "ctx.h" #include "error.h" #include "pool.h" -#include "types.h" #include "util.h" diff --git a/include/ctx.h b/include/ctx.h index 2a926fa..5f40f7c 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -1,7 +1,7 @@ #pragma once #include "database.h" #include "mptp.h" -#include "types.h" +#include "pool.h" #include @@ -18,13 +18,12 @@ typedef struct lm_ctx_resolve_list { lm_pkg_t *resolving; lm_pkg_t *packages; lm_pkg_t *cur; - size_t count; + ssize_t count; } lm_ctx_resolve_list_t; typedef struct lm_ctx_update_list { - lm_pkg_t *packages; - lm_pkg_t *cur; - size_t count; + lm_entry_t **entries; + ssize_t index, count; } lm_ctx_update_list_t; typedef enum lm_ctx_sync_state { @@ -48,8 +47,9 @@ typedef bool (*lm_ctx_sync_callback_t)( lm_ctx_t *ctx, lm_pool_t *pool, lm_ctx_sync_state_t state, size_t current, size_t total, void *data); typedef bool (*lm_ctx_serve_callback_t)(lm_pool_t *pool, lm_mptp_t *packet, struct sockaddr *addr, void *data); typedef bool (*lm_ctx_ping_callback_t)(lm_ctx_t *ctx, lm_pool_t *pool, bool status, void *data); -typedef lm_ctx_install_callback_t lm_ctx_remove_callback_t; -typedef lm_ctx_install_callback_t lm_ctx_check_callback_t; +typedef bool (*lm_ctx_remove_callback_t)( + lm_ctx_t *ctx, lm_entry_t *pkg, char *file, size_t current, size_t total, void *data); +typedef lm_ctx_remove_callback_t lm_ctx_check_callback_t; /* ############### ## ctx stuff ## @@ -68,21 +68,21 @@ lm_ctx_resolve_list_t *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_list(lm_ctx_t *ctx); // get a list of packages to update -lm_pkg_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list); // get the next package in the update list +lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx); // get a list of entries to update +lm_entry_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list); // get the next entry in the update list void lm_ctx_update_list_free(lm_ctx_update_list_t *list); // free the update list -lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg); // returns the updated version of a package +lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_entry_t *pkg); // returns the updated version of a entry -bool lm_ctx_removeable(lm_ctx_t *ctx, lm_pkg_t *pkg); // checks if a package is available for removal +bool lm_ctx_removeable(lm_ctx_t *ctx, lm_entry_t *entry); // checks if a package is available for removal bool lm_ctx_remove( - lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callback, void *data); // removes a single package + lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_remove_callback_t callback, void *data); // removes a single package bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t callback, void *data); // downloads a single package -bool lm_ctx_install( - lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t callback, void *data); // installs/updates a single package +bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, bool run_install, lm_ctx_install_callback_t callback, + void *data); // installs/updates a single package bool lm_ctx_check( - lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback, void *data); // checks a single package + lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data); // checks a single package size_t lm_ctx_sync(lm_ctx_t *ctx, bool do_update, lm_ctx_sync_callback_t callback, void *data); // syncs all the pools void lm_ctx_ping(lm_ctx_t *ctx, lm_ctx_ping_callback_t callback, void *data); // pings all the pools bool lm_ctx_serve( @@ -102,9 +102,9 @@ lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find p ## database fucntions ## ######################## */ bool lm_ctx_database_init(lm_ctx_t *ctx); // init ctx database (if not already present) -bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_version); // check if a package is installed -bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name, char *version); // find a package by name -bool lm_ctx_database_next(lm_ctx_t *ctx, lm_pkg_t *pkg); // load the next package into pkg pointer -bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_pkg_t *pkg); // free the used next pointers +bool lm_ctx_database_is_installed(lm_ctx_t *ctx, char *name, char *version); // check if a package is installed +bool lm_ctx_database_find(lm_ctx_t *ctx, lm_entry_t *entry, char *name, char *version); // find a package by name +bool lm_ctx_database_next(lm_ctx_t *ctx, lm_entry_t *entry); // load the next package into pkg pointer +bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_entry_t *entry); // free the used next pointers char *lm_ctx_database_changes( - lm_ctx_t *ctx, lm_pkg_t *pkg); // get changes file path for a package, FREE THE RETURNED POINTER + lm_ctx_t *ctx, lm_entry_t *entry); // get changes file path for a package, FREE THE RETURNED POINTER diff --git a/include/database.h b/include/database.h index 85ac649..3a557d0 100644 --- a/include/database.h +++ b/include/database.h @@ -1,78 +1,86 @@ #pragma once +#include "database.h" #include "package.h" + #include +#include +#include #define HASH_LEN 32 enum lm_query_index { - QUERY_CREATE_PACKAGE_TABLE = 0, - QUERY_INSERT_PACKAGE_SINGLE = 1, - QUERY_SELECT_PACKAGE_SINGLE_1 = 2, - QUERY_SELECT_PACKAGE_SINGLE_2 = 3, - QUERY_DELETE_PACKAGE_SINGLE = 4, - QUERY_SELECT_PACKAGE_ALL = 5, - QUERY_CREATE_FILE_TABLE = 6, - QUERY_INSERT_FILE_SINGLE = 7, - QUERY_DELETE_FILE_ALL = 8, - QUERY_SELECT_FILE_SINGLE = 9, - QUERY_SELECT_FILE_ALL = 10, - QUERY_UPDATE_FILE_1 = 11, - QUERY_UPDATE_FILE_2 = 12, + QUERY_CREATE_ENTRY_TABLE = 0, + QUERY_INSERT_ENTRY_SINGLE = 1, + QUERY_SELECT_ENTRY_SINGLE_1 = 2, + QUERY_SELECT_ENTRY_SINGLE_2 = 3, + QUERY_DELETE_ENTRY_SINGLE = 4, + QUERY_SELECT_ENTRY_ALL = 5, + QUERY_CREATE_FILE_TABLE = 6, + QUERY_INSERT_FILE_SINGLE = 7, + QUERY_DELETE_FILE_ALL = 8, + QUERY_SELECT_FILE_SINGLE = 9, + QUERY_SELECT_FILE_ALL = 10, + QUERY_UPDATE_FILE_1 = 11, + QUERY_UPDATE_FILE_2 = 12, }; enum lm_columns { - FILES_COLUMN_PATH = 1, - FILES_COLUMN_HASH = 2, - FILES_COLUMN_KEEP = 3, - FILES_COLUMN_PACKAGE = 4, + FILES_COLUMN_PATH = 1, + FILES_COLUMN_HASH = 2, + FILES_COLUMN_KEEP = 3, + FILES_COLUMN_ENTRY = 4, - PACKAGES_COLUMN_NAME = 1, - PACKAGES_COLUMN_VERSION = 2, - PACKAGES_COLUMN_DESC = 3, - PACKAGES_COLUMN_SIZE = 4, - PACKAGES_COLUMN_DEPENDS = 5, + ENTRIES_COLUMN_NAME = 1, + ENTRIES_COLUMN_VERSION = 2, + ENTRIES_COLUMN_DESC = 3, + ENTRIES_COLUMN_SIZE = 4, + ENTRIES_COLUMN_DEPENDS = 5, }; extern char *queries[]; +typedef lm_pkg_data_t lm_entry_t; + typedef struct lm_database { - sqlite3 *packages_db; - sqlite3_stmt *packages_st; + sqlite3 *entries_db; + sqlite3_stmt *entries_st; sqlite3 *files_db; sqlite3_stmt *files_st; - lm_pkg_t *pkg; - char *dir; + char *dir; } lm_database_t; +void lm_entry_init(lm_entry_t *entry); +void lm_entry_free(lm_entry_t *entry); + lm_database_t *lm_database_new(char *path); void lm_database_free(lm_database_t *db); bool lm_database_step_all(sqlite3_stmt *st); -bool lm_database_package_find( - lm_database_t *db, lm_pkg_t *pkg, char *name, char *version); // finds a package by its name, package stored in *pkg -bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg); // gets the next package in the database -bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg); // adds a package to the database -bool lm_database_package_del(lm_database_t *db, lm_pkg_t *pkg); // delete a package from the database -void lm_database_package_next_free( - lm_database_t *db, lm_pkg_t *pkg); // frees resources used for lm_database_package_next +bool lm_database_entry_find( + lm_database_t *db, lm_entry_t *out, char *name, char *version); // finds a entry by its name, entry stored in *pkg +bool lm_database_entry_next(lm_database_t *db, lm_entry_t *entry); // gets the next entry in the database +bool lm_database_entry_add(lm_database_t *db, lm_entry_t *entry); // adds a entry to the database +bool lm_database_entry_del(lm_database_t *db, lm_entry_t *entry); // delete a entry from the database +void lm_database_entry_next_free( + lm_database_t *db, lm_entry_t *entry); // frees resources used for lm_database_entry_next -size_t lm_database_files_count( - lm_database_t *db, lm_pkg_t *pkg); // returns the count of files associated with a package +ssize_t lm_database_files_count( + lm_database_t *db, lm_entry_t *entry); // returns the count of files associated with a entry bool lm_database_files_contains( - lm_database_t *db, lm_pkg_t *pkg, char *path); // check if a package contains the given file + lm_database_t *db, lm_entry_t *entry, char *path); // check if a entry contains the given file bool lm_database_files_matches( lm_database_t *db, char *path, char *hash); // checks if the given file matches with the given hash bool lm_database_files_iskeep(lm_database_t *db, char *path); // checks if the given file is marked as keep bool lm_database_files_next( - lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, bool *keep); // gets the next file of the package + lm_database_t *db, lm_entry_t *entry, char **path, char **hash, bool *keep); // gets the next file of the entry bool lm_database_files_add( - lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash); // adds a file to the files database -bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg); // dels all files of belonging to a package -void lm_database_files_next_free(lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, + lm_database_t *db, lm_entry_t *entry, char *path, char *hash); // adds a file to the files database +bool lm_database_files_del(lm_database_t *db, lm_entry_t *entry); // dels all files of belonging to a entry +void lm_database_files_next_free(lm_database_t *db, lm_entry_t *entry, char **path, char **hash, bool *keep); // frees resources used for lm_database_files_next -bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file); -char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg); -bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg); +bool lm_database_changes_update(lm_database_t *db, lm_entry_t *entry, char *file); +char *lm_database_changes_get(lm_database_t *db, lm_entry_t *entry); +bool lm_database_changes_del(lm_database_t *db, lm_entry_t *entry); diff --git a/include/error.h b/include/error.h index 3be5c57..b0df9e1 100644 --- a/include/error.h +++ b/include/error.h @@ -131,7 +131,7 @@ typedef enum lm_error { LM_ERR_InstallSpawnFail = 123, LM_ERR_InstallBackChdirFail = 124, LM_ERR_InstallStatusFail = 125, - LM_ERR_InstallScriptFail = 126, + LM_ERR_InstallRunFail = 126, LM_ERR_PkgBreaks = 127, LM_ERR_PkgUpToDate = 128, LM_ERR_HashOpenFail = 129, @@ -142,6 +142,8 @@ typedef enum lm_error { LM_ERR_NoPools = 134, LM_ERR_DbChangesNotExists = 135, LM_ERR_DbChangesChmodFail = 136, + LM_ERR_InstallDirFail = 137, + LM_ERR_InstallSaveFail = 138, } lm_error_t; typedef struct lm_error_desc { diff --git a/include/mptp.h b/include/mptp.h index e996b33..0813367 100644 --- a/include/mptp.h +++ b/include/mptp.h @@ -1,8 +1,9 @@ #pragma once -#include "types.h" #include +#include #include #include +#include // clang-format off diff --git a/include/package.h b/include/package.h index 0ddc1b1..e1b64b6 100644 --- a/include/package.h +++ b/include/package.h @@ -1,6 +1,6 @@ #pragma once -#include "types.h" #include +#include #define PKG_DATA_SIZE "size" #define PKG_DATA_DESC "desc" @@ -8,32 +8,46 @@ #define PKG_DATA_DEPENDS "depends" #define PKG_DATA_KEEPS "keeps" +typedef struct lm_pkg_data { + char *name; + char *desc; + char **depends; + char **keeps; + char *version; + ssize_t size; +} lm_pkg_data_t; + +typedef struct lm_pkg { + struct lm_pkg_data data; + struct lm_pool *pool; + struct lm_pkg *next; + char *signature; + char *archive; +} lm_pkg_t; + lm_pkg_t *lm_package_new(); void lm_package_free(lm_pkg_t *pkg); bool lm_package_verify(lm_pkg_t *pkg); void lm_package_init(lm_pkg_t *pkg); -bool lm_package_copy(lm_pkg_t *dst, lm_pkg_t *src); +bool lm_package_downloaded(lm_pkg_t *pkg); +bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two); -bool lm_package_data_load(lm_pkg_t *pkg, char *file); -void lm_package_data_free(lm_pkg_t *pkg); +bool lm_package_data_load(lm_pkg_data_t *data, char *file); +void lm_package_data_free(lm_pkg_data_t *data); -bool lm_package_downloaded(lm_pkg_t *pkg); -bool lm_package_depend_add(lm_pkg_t *pkg, char *depend); -size_t lm_package_depend_count(lm_pkg_t *pkg); -size_t lm_package_depend_strlen(lm_pkg_t *pkg); -char *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); +bool lm_package_data_depend_add(lm_pkg_data_t *data, char *depend); +ssize_t lm_package_data_depend_count(lm_pkg_data_t *data); +ssize_t lm_package_data_depend_strlen(lm_pkg_data_t *data); +char *lm_package_data_depend_tostr(lm_pkg_data_t *data, char *buffer); +bool lm_package_data_depend_fromstr(lm_pkg_data_t *data, char *buffer); +void lm_package_data_depend_free(lm_pkg_data_t *data); +bool lm_package_data_depends(lm_pkg_data_t *data, char *dep); -size_t lm_package_keep_count(lm_pkg_t *pkg); -bool lm_package_keep_add(lm_pkg_t *pkg, char *path); -bool lm_package_keep_contains(lm_pkg_t *pkg, char *path); -void lm_package_keep_free(lm_pkg_t *pkg); +size_t lm_package_data_keep_count(lm_pkg_data_t *data); +bool lm_package_data_keep_add(lm_pkg_data_t *data, char *path); +bool lm_package_data_keep_contains(lm_pkg_data_t *data, char *path); +void lm_package_data_keep_free(lm_pkg_data_t *data); bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path); bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path); bool lm_package_path_is_empty(lm_pkg_t *pkg); -void lm_package_path_free(lm_pkg_t *pkg); -bool lm_package_path_copy(lm_pkg_t *dst, lm_pkg_t *src); -bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two); diff --git a/include/pool.h b/include/pool.h index 087d53e..f929b40 100644 --- a/include/pool.h +++ b/include/pool.h @@ -1,10 +1,30 @@ #pragma once #include "mptp.h" -#include "types.h" +#include "package.h" +#include "url.h" #include #include +typedef struct lm_pool_info { + char *maintainer; + char *pubkey; + ssize_t size; +} lm_pool_info_t; + +typedef struct lm_pool { + struct lm_pool *next; + lm_pool_info_t info; + lm_url_t url; + lm_pkg_t *pkg; + char *name; + char *info_file; + char *list_file; + char *list_dir; + char *dir; + bool available, loaded; +} lm_pool_t; + #define POOL_INFO_SIZE "size" #define POOL_INFO_PUBKEY "pubkey" #define POOL_INFO_MAINTAINER "maintainer" @@ -18,7 +38,6 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg); bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir); bool lm_pool_path_is_empty(lm_pool_t *pool); -void lm_pool_path_free(lm_pool_t *pool); bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback, void *data); bool lm_pool_info_load(lm_pool_t *pool); diff --git a/include/types.h b/include/types.h deleted file mode 100644 index 156ebe0..0000000 --- a/include/types.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include "url.h" - -#include -#include -#include - -typedef struct lm_pkg_file { - char *path; - char *hash; -} lm_pkg_file_t; - -typedef struct lm_pkg_path { - char *archive; - char *signature; -} lm_pkg_path_t; - -typedef struct lm_pkg { - struct lm_pool *pool; - struct lm_pkg *next; - lm_pkg_path_t paths; - char *name; - char *desc; - char **depends; - char **keeps; - char *version; - size_t size; -} lm_pkg_t; - -typedef struct lm_pool_info { - char *maintainer; - char *pubkey; - ssize_t size; -} lm_pool_info_t; - -typedef struct lm_pool_path { - char *info_file; - char *list_file; - char *list_dir; - char *dir; -} lm_pool_path_t; - -typedef struct lm_pool { - struct lm_pool *next; - lm_pool_info_t info; - lm_pool_path_t paths; - lm_url_t url; - lm_pkg_t *pkg; - char *name; - bool available, loaded; -} lm_pool_t; diff --git a/include/util.h b/include/util.h index 60aee1b..752fb17 100644 --- a/include/util.h +++ b/include/util.h @@ -1,5 +1,6 @@ #pragma once -#include "types.h" +#include "pool.h" + #include #include #include @@ -23,6 +24,7 @@ bool can_read(char *path); bool is_file(char *path); bool is_dir(char *path); bool exists(char *path); +bool is_empty(char *p); bool package_parse(char *package, char *name, char *version); bool package_version_valid(char *name); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 0dbe71f..f3d1ea0 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-19 19:22+0300\n" +"POT-Creation-Date: 2024-07-31 21:14+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -249,11 +249,11 @@ msgid "specified root directory does not have write access" msgstr "" #: src/error.c:76 -msgid "specified data path does not exist" +msgid "specified data path is not a directory" msgstr "" #: src/error.c:77 -msgid "specified data path is not a directory" +msgid "specified data directory does not have write access" msgstr "" #: src/error.c:78 @@ -557,45 +557,54 @@ msgstr "" #: src/error.c:152 #, c-format -msgid "removing package breaks %s" +msgid "failed to save the package install script: %s" msgstr "" #: src/error.c:153 +#, c-format +msgid "removing package breaks %s" +msgstr "" + +#: src/error.c:154 #, fuzzy msgid "package is already up-to-date" msgstr "URL hostname is too large" -#: src/error.c:154 +#: src/error.c:155 msgid "failed to open file for hashing" msgstr "" -#: src/error.c:155 -msgid "failed create digest for hashing" -msgstr "" - #: src/error.c:156 -#, c-format -msgid "failed to get hash of %s: %s" +msgid "failed create digest for hashing" msgstr "" #: src/error.c:157 #, c-format -msgid "file hash does not match for %s" +msgid "failed to get hash of %s: %s" msgstr "" #: src/error.c:158 +#, c-format +msgid "file hash does not match for %s" +msgstr "" + +#: src/error.c:159 #, fuzzy msgid "pool info is not loaded" msgstr "URL hostname is too large" -#: src/error.c:159 +#: src/error.c:160 msgid "pool list is empty" msgstr "" -#: src/error.c:160 +#: src/error.c:161 msgid "package changes file not found in the database" msgstr "" -#: src/error.c:161 +#: src/error.c:162 msgid "failed to change mod of the changes file" msgstr "" + +#: src/error.c:163 +msgid "failed to create install script save directory" +msgstr "" diff --git a/src/ctx/check.c b/src/ctx/check.c index 04941b3..d4c8cd9 100644 --- a/src/ctx/check.c +++ b/src/ctx/check.c @@ -6,8 +6,8 @@ #include #include -bool lm_ctx_check(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback, void *data){ - if(NULL == ctx || NULL == pkg){ +bool lm_ctx_check(lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data){ + if(NULL == ctx || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -24,9 +24,9 @@ bool lm_ctx_check(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback char *path = NULL, *hash = NULL, *fhash = NULL; bool keep = false, ret = false; - total = lm_database_files_count(ctx->db, pkg); + total = lm_database_files_count(ctx->db, entry); - while(lm_database_files_next(ctx->db, pkg, &path, &hash, &keep)){ + while(lm_database_files_next(ctx->db, entry, &path, &hash, &keep)){ char fp[rootlen+strlen(path)+10]; join(fp, ctx->root, path); current++; @@ -34,7 +34,7 @@ bool lm_ctx_check(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback pdebug(__func__, "(%d/%d) checking %s", current, total, fp); if(NULL != callback) - if(!callback(ctx, pkg, fp, current, total, data)) + if(!callback(ctx, entry, fp, current, total, data)) goto end; if(!exists(fp)){ @@ -61,7 +61,7 @@ bool lm_ctx_check(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_check_callback_t callback ret = true; end: - lm_database_files_next_free(ctx->db, pkg, &path, &hash, &keep); + lm_database_files_next_free(ctx->db, entry, &path, &hash, &keep); free(fhash); return ret; } diff --git a/src/ctx/database.c b/src/ctx/database.c index dcf78dd..7edd1a6 100644 --- a/src/ctx/database.c +++ b/src/ctx/database.c @@ -21,17 +21,17 @@ bool lm_ctx_database_init(lm_ctx_t *ctx){ return ctx->db != NULL; // error set by function } -bool lm_ctx_database_is_installed(lm_ctx_t *ctx, lm_pkg_t *pkg, bool check_version){ +bool lm_ctx_database_is_installed(lm_ctx_t *ctx, char *name, char *version){ if(!lm_ctx_database_init(ctx)) return false; // error set by function if(NULL == ctx->db) return false; - return lm_database_package_find(ctx->db, NULL, pkg->name, check_version ? pkg->version : NULL); + return lm_database_entry_find(ctx->db, NULL, name, version); } -bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name, char *version){ +bool lm_ctx_database_find(lm_ctx_t *ctx, lm_entry_t *entry, char *name, char *version){ if(NULL == ctx || NULL == name){ lm_error_set(LM_ERR_ArgNULL); return false; @@ -41,11 +41,11 @@ bool lm_ctx_database_find(lm_ctx_t *ctx, lm_pkg_t *pkg, char *name, char *versio return false; // error set by function lm_error_clear(); - return lm_database_package_find(ctx->db, pkg, name, version); + return lm_database_entry_find(ctx->db, entry, name, version); } -bool lm_ctx_database_next(lm_ctx_t *ctx, lm_pkg_t *pkg){ - if(NULL == ctx || NULL == pkg){ +bool lm_ctx_database_next(lm_ctx_t *ctx, lm_entry_t *entry){ + if(NULL == ctx || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -53,11 +53,11 @@ bool lm_ctx_database_next(lm_ctx_t *ctx, lm_pkg_t *pkg){ if(!lm_ctx_database_init(ctx)) return false; // error set by function - return lm_database_package_next(ctx->db, pkg); + return lm_database_entry_next(ctx->db, entry); } -bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_pkg_t *pkg){ - if(NULL == ctx || NULL == pkg){ +bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_entry_t *entry){ + if(NULL == ctx || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -65,12 +65,12 @@ bool lm_ctx_database_next_free(lm_ctx_t *ctx, lm_pkg_t *pkg){ if(!lm_ctx_database_init(ctx)) return false; // error set by function - lm_database_package_next_free(ctx->db, pkg); + lm_database_entry_next_free(ctx->db, entry); return true; } -char *lm_ctx_database_changes(lm_ctx_t *ctx, lm_pkg_t *pkg){ - if(NULL == ctx || NULL == pkg){ +char *lm_ctx_database_changes(lm_ctx_t *ctx, lm_entry_t *entry){ + if(NULL == ctx || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return NULL; } @@ -78,5 +78,5 @@ char *lm_ctx_database_changes(lm_ctx_t *ctx, lm_pkg_t *pkg){ if(!lm_ctx_database_init(ctx)) return NULL; // error set by function - return lm_database_changes_get(ctx->db, pkg); + return lm_database_changes_get(ctx->db, entry); } diff --git a/src/ctx/download.c b/src/ctx/download.c index d1fa2bc..6b57823 100644 --- a/src/ctx/download.c +++ b/src/ctx/download.c @@ -21,7 +21,7 @@ bool __lm_ctx_download_callback(char *path, size_t current, size_t total, void * if(NULL == cbdata->callback) return true; - if(eq(cbdata->pkg->paths.archive, path)) + if(eq(cbdata->pkg->archive, path)) return cbdata->callback(cbdata->ctx, cbdata->pkg, true, current, total, cbdata->data); else return cbdata->callback(cbdata->ctx, cbdata->pkg, false, current, total, cbdata->data); @@ -45,8 +45,8 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca return false; } - char path[strlen(pool->url.path)+strlen(pkg->name)+strlen(pkg->version)+20]; - char name[strlen(pkg->name)+strlen(pkg->version)+10]; + char path[strlen(pool->url.path)+strlen(pkg->data.name)+strlen(pkg->data.version)+20]; + char name[strlen(pkg->data.name)+strlen(pkg->data.version)+10]; struct __lm_ctx_download_cb_data cbdata = { .ctx = ctx, .pkg = pkg, @@ -57,7 +57,7 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca bool ret = false; int sock = -1; - snprintf(name, sizeof(name), "%s_%s", pkg->name, pkg->version); + snprintf(name, sizeof(name), "%s_%s", pkg->data.name, pkg->data.version); join(path, pool->url.path, name); pdebug(__func__, "connecting to %s:%d for download", pool->url.host, pool->url.port); @@ -72,10 +72,10 @@ bool lm_ctx_download(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_download_callback_t ca if(!lm_mptp_client_send(sock, &packet)) goto end; - if(!lm_mptp_recvfile(sock, pkg->paths.archive, __lm_ctx_download_callback, &cbdata)) + if(!lm_mptp_recvfile(sock, pkg->archive, __lm_ctx_download_callback, &cbdata)) goto end; - if(!lm_mptp_recvfile(sock, pkg->paths.signature, __lm_ctx_download_callback, &cbdata)) + if(!lm_mptp_recvfile(sock, pkg->signature, __lm_ctx_download_callback, &cbdata)) goto end; if(!lm_package_verify(pkg)) diff --git a/src/ctx/install.c b/src/ctx/install.c index 33401aa..1f3da4f 100644 --- a/src/ctx/install.c +++ b/src/ctx/install.c @@ -14,13 +14,36 @@ #include #include -bool __lm_ctx_run_install(char *root, char *install_path) { - struct stat st; - bzero(&st, sizeof(st)); - stat(install_path, &st); +bool __lm_ctx_save_install(lm_ctx_t *ctx, lm_pkg_t *pkg, char *install_path){ + if(NULL == ctx->data){ + lm_error_set(LM_ERR_CtxDataNULL); + return false; + } + // no need to save empty install script :) + if(is_empty(install_path)) + return true; + + char script_name[strlen(pkg->data.name)+15]; + char script_dir[strlen(ctx->data)+15]; + + sprintf(script_name, "install_%s", pkg->data.name); + join(script_dir, ctx->data, "scripts"); + + if(!mkdir_ifnot(script_dir)){ + lm_error_set(LM_ERR_InstallDirFail); + return false; + } + + char dst[sizeof(script_name)+sizeof(script_dir)+10]; + join(dst, script_dir, script_name); + + return copy_file(dst, install_path); +} + +bool __lm_ctx_run_install(char *root, char *install_path) { // no need to run empty install script :) - if(st.st_size <= 0) + if(is_empty(install_path)) return true; char *args[] = {"/bin/bash", install_path, NULL}, *oldpwd = NULL; @@ -113,8 +136,8 @@ bool __lm_ctx_extract_files(lm_ctx_t *ctx, lm_pkg_t *pkg, char *files, lm_ctx_in current += archive_entry_size(entry); type = archive_entry_filetype(entry); - if(type == AE_IFREG && !lm_database_files_contains(ctx->db, pkg, entry_path)){ - pdebug(__func__, "archive file not included in the database: %s (%s)", entry_path, pkg->name); + if(type == AE_IFREG && !lm_database_files_contains(ctx->db, &pkg->data, entry_path)){ + pdebug(__func__, "archive file not included in the database: %s (%s)", entry_path, pkg->data.name); continue; } @@ -164,7 +187,7 @@ end: return ret; } -bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t callback, void *data) { +bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, bool run_install, lm_ctx_install_callback_t callback, void *data) { if(NULL == ctx->temp || NULL == pkg){ lm_error_set(LM_ERR_CtxTempNULL); return false; @@ -183,7 +206,7 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call if(!lm_ctx_database_init(ctx)) return false; // error set by function - if(lm_ctx_database_is_installed(ctx, pkg, false)){ + if(lm_ctx_database_is_installed(ctx, pkg->data.name, NULL)){ lm_error_set(LM_ERR_PkgAlreadyInstalled); return false; } @@ -193,7 +216,7 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call return false; } - if(!extract_archive(ctx->temp, pkg->paths.archive)) + if(!extract_archive(ctx->temp, pkg->archive)) return false; // error set by function size_t bufsize = strlen(ctx->temp) + 25; @@ -218,18 +241,18 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call return false; } - if(!lm_package_data_load(&temp, data_file)) + if(!lm_package_data_load(&temp.data, data_file)) return false; // error set by function if(!lm_package_is_same(&temp, pkg)){ - pdebug(__func__, "DATA file does not match with stored %s data", pkg->name); + pdebug(__func__, "DATA file does not match with stored %s data", pkg->data.name); lm_error_set(LM_ERR_PkgDataNotMatch); return false; } - if(!lm_database_changes_update(ctx->db, pkg, changes_file)){ + if(!lm_database_changes_update(ctx->db, &pkg->data, changes_file)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to update changes file for %s: %s", pkg->name, suberr); + pdebug(__func__, "failed to update changes file for %s: %s", pkg->data.name, suberr); lm_error_set(LM_ERR_PkgChangesUpdateFail, suberr); return false; } @@ -240,7 +263,7 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call bool ret = false; if((hashes = fopen(hashes_file, "r")) == NULL){ - pdebug(__func__, "failed to open hash file for %s", pkg->name); + pdebug(__func__, "failed to open hash file for %s", pkg->data.name); lm_error_set(LM_ERR_PkgHashesOpenFail); return false; } @@ -267,9 +290,9 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call pdebug(__func__, "(%lu) %s => %s", line_len, file, hash); - if(!lm_database_files_add(ctx->db, pkg, file, hash)){ + if(!lm_database_files_add(ctx->db, &pkg->data, file, hash)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to add file to the database for %s: %s", pkg->name, suberr); + pdebug(__func__, "failed to add file to the database for %s: %s", pkg->data.name, suberr); lm_error_set(LM_ERR_PkgFilesAddFail, file, suberr); free(suberr); goto end; @@ -283,24 +306,32 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call if(!__lm_ctx_extract_files(ctx, pkg, files_archive, callback, data)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to extract the files archive for %s: %s", pkg->name, suberr); + pdebug(__func__, "failed to extract the files archive for %s: %s", pkg->data.name, suberr); lm_error_set(LM_ERR_PkgExtractFilesFail, suberr); free(suberr); goto end; } - if(!lm_database_package_add(ctx->db, pkg)){ + if(!lm_database_entry_add(ctx->db, &pkg->data)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to add %s to the database: %s", pkg->name, lm_strerror()); + pdebug(__func__, "failed to add %s to the database: %s", pkg->data.name, lm_strerror()); lm_error_set(LM_ERR_PkgDatabaseAddFail, suberr); free(suberr); goto end; } - if(!__lm_ctx_run_install(ctx->root, install_file)){ + if(run_install && !__lm_ctx_run_install(ctx->root, install_file)){ char *suberr = lm_strerror_dup(); pdebug(__func__, "failed to run install script: %s", lm_strerror()); - lm_error_set(LM_ERR_InstallScriptFail, suberr); + lm_error_set(LM_ERR_InstallRunFail, suberr); + free(suberr); + goto end; + } + + if(!run_install && !__lm_ctx_save_install(ctx, pkg, install_file)){ + char *suberr = lm_strerror_dup(); + pdebug(__func__, "failed to save install script: %s", lm_strerror()); + lm_error_set(LM_ERR_InstallSaveFail, suberr); free(suberr); goto end; } @@ -313,12 +344,12 @@ end: fclose(hashes); if(!ret){ - lm_database_package_del(ctx->db, pkg); - lm_database_changes_del(ctx->db, pkg); - lm_database_files_del(ctx->db, pkg); + lm_database_entry_del(ctx->db, &pkg->data); + lm_database_changes_del(ctx->db, &pkg->data); + lm_database_files_del(ctx->db, &pkg->data); } - unlink(pkg->paths.archive); - unlink(pkg->paths.signature); + unlink(pkg->archive); + unlink(pkg->signature); return ret; } diff --git a/src/ctx/remove.c b/src/ctx/remove.c index ad4692b..f8d6b1d 100644 --- a/src/ctx/remove.c +++ b/src/ctx/remove.c @@ -8,13 +8,13 @@ #include #include -bool lm_ctx_removeable(lm_ctx_t *ctx, lm_pkg_t *pkg){ - if(NULL == ctx && NULL == pkg){ +bool lm_ctx_removeable(lm_ctx_t *ctx, lm_entry_t *entry){ + if(NULL == ctx && NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } - if(!lm_ctx_database_is_installed(ctx, pkg, true)){ + if(!lm_ctx_database_is_installed(ctx, entry->name, entry->version)){ lm_error_set(LM_ERR_PkgNotInstalled); return false; } @@ -22,22 +22,22 @@ bool lm_ctx_removeable(lm_ctx_t *ctx, lm_pkg_t *pkg){ if(!lm_ctx_database_init(ctx)) return false; - lm_pkg_t cur; + lm_entry_t cur; - while (lm_database_package_next(ctx->db, &cur)) { - if(lm_package_depends(&cur, pkg->name)){ + while (lm_database_entry_next(ctx->db, &cur)) { + if(lm_package_data_depends(&cur, entry->name)){ lm_error_set(LM_ERR_PkgBreaks, cur.name); - lm_database_package_next_free(ctx->db, &cur); + lm_database_entry_next_free(ctx->db, &cur); return false; } } - lm_database_package_next_free(ctx->db, &cur); + lm_database_entry_next_free(ctx->db, &cur); return true; } -bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callback, void *data){ - if(NULL == ctx && NULL == pkg){ +bool lm_ctx_remove(lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_remove_callback_t callback, void *data){ + if(NULL == ctx && NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -54,14 +54,14 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba bool in_keep = false, ret = false; size_t total = 0, current = 0; - total = lm_database_files_count(ctx->db, pkg); + total = lm_database_files_count(ctx->db, entry); - while(lm_database_files_next(ctx->db, pkg, &path, &hash, &in_keep)){ + while(lm_database_files_next(ctx->db, entry, &path, &hash, &in_keep)){ if(in_keep) goto next; fpath = join_alloc(ctx->root, path); - pdebug(__func__, "removing file %s (%s)", fpath, pkg->name); + pdebug(__func__, "removing file %s (%s)", fpath, entry->name); if(!exists(fpath)){ pdebug(__func__, "found file in database, but its not on the file system: %s", fpath); @@ -69,7 +69,7 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba } if(unlink(fpath) < 0){ - pdebug(__func__, "failed to delete file for removing %s: %s", pkg->name, strerror(errno)); + pdebug(__func__, "failed to delete file for removing %s: %s", entry->name, strerror(errno)); lm_error_set(LM_ERR_PkgFileUnlinkFail, path, strerror(errno)); goto next; } @@ -78,36 +78,36 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba free(fpath); fpath = NULL; - if(NULL != callback && !callback(ctx, pkg, fpath, ++current, total, data)) + if(NULL != callback && !callback(ctx, entry, fpath, ++current, total, data)) goto end; } - if(!lm_database_package_del(ctx->db, pkg)){ + if(!lm_database_entry_del(ctx->db, entry)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to delete %s from the database: %s", pkg->name, lm_strerror()); + pdebug(__func__, "failed to delete %s from the database: %s", entry->name, lm_strerror()); lm_error_set(LM_ERR_PkgDatabaseDelFail, suberr); free(suberr); goto end; } - if(!lm_database_files_del(ctx->db, pkg)){ + if(!lm_database_files_del(ctx->db, entry)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to delete files of %s from the database: %s", pkg->name, suberr); + pdebug(__func__, "failed to delete files of %s from the database: %s", entry->name, suberr); lm_error_set(LM_ERR_PkgFilesDelFail, suberr); free(suberr); goto end; } - if(!lm_database_changes_del(ctx->db, pkg)){ + if(!lm_database_changes_del(ctx->db, entry)){ char *suberr = lm_strerror_dup(); - pdebug(__func__, "failed to delete changes file for %s: %s", pkg->name, suberr); + pdebug(__func__, "failed to delete changes file for %s: %s", entry->name, suberr); lm_error_set(LM_ERR_PkgChangesDelFail, suberr); goto end; } ret = true; end: - lm_database_files_next_free(ctx->db, pkg, &path, &hash, &in_keep); + lm_database_files_next_free(ctx->db, entry, &path, &hash, &in_keep); return ret; } diff --git a/src/ctx/resolve.c b/src/ctx/resolve.c index 5f70d7d..3b5ba24 100644 --- a/src/ctx/resolve.c +++ b/src/ctx/resolve.c @@ -11,26 +11,26 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg) if(pkglist_contains(list->packages, pkg)) return true; - if(NULL == pkg->depends) + if(NULL == pkg->data.depends) goto end; list->resolving = pkglist_add_top(list->resolving, pkg); lm_pkg_t *depend = NULL; - for(int i = 0; pkg->depends[i] != NULL; i++){ + for(int i = 0; pkg->data.depends[i] != NULL; i++){ // if found then its already installed - if(lm_ctx_database_find(ctx, NULL, pkg->depends[i], NULL)) + if(lm_ctx_database_find(ctx, NULL, pkg->data.depends[i], NULL)) continue; - depend = lm_ctx_pool_find(ctx, pkg->depends[i], NULL); + depend = lm_ctx_pool_find(ctx, pkg->data.depends[i], NULL); if(NULL == depend){ - lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name); + lm_error_set(LM_ERR_DependNotFound, pkg->data.depends[i], pkg->data.name); return false; } if(pkglist_contains(list->resolving, depend)){ - lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name); + lm_error_set(LM_ERR_DependNotFound, pkg->data.depends[i], pkg->data.name); return false; } diff --git a/src/ctx/serve.c b/src/ctx/serve.c index 35a8124..e20d768 100644 --- a/src/ctx/serve.c +++ b/src/ctx/serve.c @@ -33,13 +33,13 @@ void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockadd // when INFO file is requested, send the file case MPTP_C2S_INFO: pdebug(__func__, "(%s) INFO: attempting to send info", pool->name); - lm_mptp_sendfile(sock, addr, pool->paths.info_file, NULL, NULL); + lm_mptp_sendfile(sock, addr, pool->info_file, NULL, NULL); break; // when LIST file is requested, send the file case MPTP_C2S_LIST: pdebug(__func__, "(%s) LIST: attempting to send list", pool->name); - lm_mptp_sendfile(sock, addr, pool->paths.list_file, NULL, NULL); + lm_mptp_sendfile(sock, addr, pool->list_file, NULL, NULL); break; // when the request code is PULL, send the requested package archive and @@ -88,8 +88,8 @@ void __lm_ctx_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockadd } // send package archive and the signature file - lm_mptp_sendfile(sock, addr, pkg->paths.archive, NULL, NULL); - lm_mptp_sendfile(sock, addr, pkg->paths.signature, NULL, NULL); + lm_mptp_sendfile(sock, addr, pkg->archive, NULL, NULL); + lm_mptp_sendfile(sock, addr, pkg->signature, NULL, NULL); break; } } diff --git a/src/ctx/update.c b/src/ctx/update.c index 285999b..5e56395 100644 --- a/src/ctx/update.c +++ b/src/ctx/update.c @@ -15,53 +15,45 @@ lm_ctx_update_list_t *lm_ctx_update_list(lm_ctx_t *ctx){ } lm_ctx_update_list_t *list = malloc(sizeof(lm_ctx_update_list_t)); - lm_pkg_t cur, *new = NULL; + lm_entry_t *cur = malloc(sizeof(lm_entry_t)); + lm_pkg_t *new = NULL; bzero(list, sizeof(lm_ctx_update_list_t)); - lm_package_init(&cur); + lm_entry_init(cur); - while(lm_ctx_database_next(ctx, &cur)){ - if((new = lm_ctx_pool_find(ctx, cur.name, NULL)) == NULL) + while(lm_ctx_database_next(ctx, cur)){ + if((new = lm_ctx_pool_find(ctx, cur->name, NULL)) == NULL) continue; - if(eq(new->version, cur.version)) + if(eq(new->data.version, cur->version)) continue; - if(NULL == list->packages) - list->packages = malloc(sizeof(lm_pkg_t)); + if(NULL == list->entries) + list->entries = malloc(sizeof(lm_entry_t*)*(++list->count)); else - list->packages = realloc(list->packages, (list->count+1)*sizeof(lm_pkg_t)); - - lm_package_copy(&list->packages[list->count++], &cur); + list->entries = realloc(list->entries, sizeof(lm_entry_t*)*(++list->count)); + list->entries[list->count-1] = cur; } - lm_ctx_database_next_free(ctx, &cur); + lm_ctx_database_next_free(ctx, NULL); return list; } -lm_pkg_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list){ +lm_entry_t *lm_ctx_update_list_next(lm_ctx_update_list_t *list){ if(NULL == list){ lm_error_set(LM_ERR_ArgNULL); return NULL; } - if(NULL == list->packages) + if(NULL == list->entries) return NULL; - if(NULL == list->cur){ - list->cur = &list->packages[0]; - return list->cur; + if(list->index >= list->count){ + list->index = 0; + return NULL; } - for(int i = 0; i < list->count-1; i++){ - if(list->cur != &list->packages[i]) - continue; - list->cur = &list->packages[i+1]; - return list->cur; - } - - list->cur = NULL; - return list->cur; + return list->entries[list->index++]; } void lm_ctx_update_list_free(lm_ctx_update_list_t *list){ @@ -70,29 +62,29 @@ void lm_ctx_update_list_free(lm_ctx_update_list_t *list){ return; } - if(NULL != list->packages){ + if(NULL != list->entries){ for(int i = 0; i < list->count; i++) - lm_package_free(&list->packages[i]); - free(list->packages); + lm_entry_free(list->entries[i]); + free(list->entries); } free(list); } -lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_pkg_t *pkg){ - if(NULL == ctx || NULL == pkg){ +lm_pkg_t *lm_ctx_update(lm_ctx_t *ctx, lm_entry_t *entry){ + if(NULL == ctx || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return NULL; } lm_pkg_t *new = NULL; - if((new = lm_ctx_pool_find(ctx, pkg->name, NULL)) == NULL){ + if((new = lm_ctx_pool_find(ctx, entry->name, NULL)) == NULL){ lm_error_set(LM_ERR_PkgNotFound); return NULL; } - if(eq(new->version, pkg->version)){ + if(eq(new->data.version, entry->version)){ lm_error_set(LM_ERR_PkgUpToDate); return NULL; } diff --git a/src/database/changes.c b/src/database/changes.c index 286e0f6..e5194b0 100644 --- a/src/database/changes.c +++ b/src/database/changes.c @@ -8,14 +8,14 @@ #include #include -bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){ - if(NULL == db || NULL == pkg || NULL == file){ +bool lm_database_changes_update(lm_database_t *db, lm_entry_t *entry, char *file){ + if(NULL == db || NULL == entry || NULL == file){ lm_error_set(LM_ERR_ArgNULL); return false; } - char changes_file[strlen(pkg->name)+20]; - sprintf(changes_file, "%s_changes", pkg->name); + char changes_file[strlen(entry->name)+20]; + sprintf(changes_file, "%s_changes", entry->name); char changes_path[strlen(db->dir)+sizeof(changes_file)]; join(changes_path, db->dir, changes_file); @@ -31,14 +31,14 @@ bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){ return true; } -bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ +bool lm_database_changes_del(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } - char changes_file[strlen(pkg->name)+20]; - sprintf(changes_file, "%s_changes", pkg->name); + char changes_file[strlen(entry->name)+20]; + sprintf(changes_file, "%s_changes", entry->name); char changes_path[strlen(db->dir)+sizeof(changes_file)]; join(changes_path, db->dir, changes_file); @@ -51,9 +51,9 @@ bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){ return true; } -char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg){ - char changes_file[strlen(pkg->name)+20]; - sprintf(changes_file, "%s_changes", pkg->name); +char *lm_database_changes_get(lm_database_t *db, lm_entry_t *entry){ + char changes_file[strlen(entry->name)+20]; + sprintf(changes_file, "%s_changes", entry->name); char *changes_path = malloc(strlen(db->dir)+sizeof(changes_file)); join(changes_path, db->dir, changes_file); diff --git a/src/database/database.c b/src/database/database.c index df958e9..91be89d 100644 --- a/src/database/database.c +++ b/src/database/database.c @@ -79,8 +79,8 @@ lm_database_t *lm_database_new(char *path){ join(packagesdb, path, "packages.db"); join(filesdb, path, "files.db"); - if(sqlite3_open(packagesdb, &db->packages_db)){ - pdebug(__func__, "(%s) failed to open databse: %s", packagesdb, sqlite3_errmsg(db->packages_db)); + if(sqlite3_open(packagesdb, &db->entries_db)){ + pdebug(__func__, "(%s) failed to open databse: %s", packagesdb, sqlite3_errmsg(db->entries_db)); lm_error_set(LM_ERR_DbSqlOpenFail); return NULL; } @@ -91,11 +91,11 @@ lm_database_t *lm_database_new(char *path){ return NULL; } - if(sqlite3_exec(db->packages_db, queries[QUERY_CREATE_PACKAGE_TABLE], NULL, 0, &err) != SQLITE_OK){ + if(sqlite3_exec(db->entries_db, queries[QUERY_CREATE_ENTRY_TABLE], NULL, 0, &err) != SQLITE_OK){ pdebug(__func__, "(%s) failed to create packages table: %s", packagesdb, err); lm_error_set(LM_ERR_DbSqlCreateFail); sqlite3_free(err); - db->packages_db = NULL; + db->entries_db = NULL; } if(sqlite3_exec(db->files_db, queries[QUERY_CREATE_FILE_TABLE], NULL, 0, &err) != SQLITE_OK){ @@ -110,23 +110,16 @@ lm_database_t *lm_database_new(char *path){ } void lm_database_free(lm_database_t *db){ - if(NULL != db->packages_st) - sqlite3_finalize(db->packages_st); + if(NULL != db->entries_st) + sqlite3_finalize(db->entries_st); if(NULL != db->files_st) sqlite3_finalize(db->files_st); - sqlite3_close(db->packages_db); + sqlite3_close(db->entries_db); sqlite3_close(db->files_db); + free(db->dir); - - lm_pkg_t *cur = db->pkg, *prev = NULL; - while(NULL != cur){ - prev = cur; - cur = cur->next; - lm_package_free(prev); - } - free(db); } diff --git a/src/database/entry.c b/src/database/entry.c new file mode 100644 index 0000000..3e1060e --- /dev/null +++ b/src/database/entry.c @@ -0,0 +1,208 @@ +#include "../../include/database.h" +#include "../../include/package.h" +#include "../../include/error.h" +#include "../../include/util.h" + +#include +#include +#include +#include + +void lm_entry_init(lm_entry_t *entry){ + bzero(entry, sizeof(lm_entry_t)); +} + +void lm_entry_free(lm_entry_t *entry){ + if(NULL == entry) + return; + lm_package_data_free(entry); + lm_entry_init(entry); +} + +bool lm_database_entry_add(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ + lm_error_set(LM_ERR_ArgNULL); + return false; + } + + char depends[lm_package_data_depend_strlen(entry)]; + bool ret = false; + + if(sqlite3_prepare(db->entries_db, queries[QUERY_INSERT_ENTRY_SINGLE], strlen(queries[QUERY_INSERT_ENTRY_SINGLE]), &db->entries_st, NULL) != SQLITE_OK){ + pdebug(__func__, "failed to prepare statement for inserting %s: %s", entry->name, sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlPrepareFail); + goto end; + } + + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_NAME, entry->name, strlen(entry->name), SQLITE_STATIC); + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_DESC, entry->desc, strlen(entry->desc), SQLITE_STATIC); + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_VERSION, entry->version, strlen(entry->version), SQLITE_STATIC); + sqlite3_bind_int64(db->entries_st, ENTRIES_COLUMN_SIZE, entry->size); + + if(!lm_package_data_depend_tostr(entry, depends)){ + pdebug(__func__, "failed to convert depends to string for inserting %s: %s", entry->name, lm_strerror()); + goto end; + } + pdebug(__func__, "depend list for %s: %s", entry->name, depends); + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_DEPENDS, depends, strlen(depends), SQLITE_STATIC); + + if(!lm_database_step_all(db->entries_st)){ + pdebug(__func__, "failed to execute insert statement for inserting %s: %s", entry->name, sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlInsertFail); + goto end; + } + + ret = true; +end: + if(NULL != db->entries_st){ + sqlite3_finalize(db->entries_st); + db->entries_st = NULL; + } + return ret; +} + +bool lm_database_entry_find(lm_database_t *db, lm_entry_t *entry, char *name, char *version){ + if(NULL == db || NULL == name){ + lm_error_set(LM_ERR_ArgNULL); + return false; + } + + bool ret = false; + char *query = NULL; + + if(NULL == version) + query = queries[QUERY_SELECT_ENTRY_SINGLE_1]; + else + query = queries[QUERY_SELECT_ENTRY_SINGLE_2]; + + // package pointer should already be free'd + // we are initing it just in case the caller didn't + if(NULL != entry) + lm_entry_free(entry); + + if(sqlite3_prepare(db->entries_db, query, strlen(query), &db->entries_st, NULL) != SQLITE_OK){ + pdebug(__func__, "failed to prepare statement for finding %s: %s", name, sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlPrepareFail); + goto end; + } + + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_NAME, name, strlen(name), SQLITE_STATIC); + if(NULL != version) + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_VERSION, version, strlen(version), SQLITE_STATIC); + + if(sqlite3_step(db->entries_st) != SQLITE_ROW){ + pdebug(__func__, "got no rows for %s", name); + lm_error_set(LM_ERR_DbSqlNotFound); + goto end; + } + + if(NULL == entry){ + ret = true; + goto end; + } + + entry->name = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_NAME-1)); + entry->desc = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_DESC-1)); + entry->version = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_VERSION-1)); + entry->size = sqlite3_column_int64(db->entries_st, ENTRIES_COLUMN_SIZE-1); + + char *depends = (char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_DEPENDS-1); + if(!lm_package_data_depend_fromstr(entry, depends)){ + pdebug(__func__, "failed to load depends for finding %s: %s", entry->name, lm_strerror()); + // error is set by the function + goto end; + } + + ret = true; +end: + if(NULL != db->entries_st){ + sqlite3_finalize(db->entries_st); + db->entries_st = NULL; + } + return ret; +} + +bool lm_database_entry_del(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ + lm_error_set(LM_ERR_ArgNULL); + return false; + } + + bool ret = false; + + if(sqlite3_prepare(db->entries_db, queries[QUERY_DELETE_ENTRY_SINGLE], strlen(queries[QUERY_DELETE_ENTRY_SINGLE]), &db->entries_st, NULL) != SQLITE_OK){ + pdebug(__func__, "failed to prepare statement for deleting %s: %s", entry->name, sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlPrepareFail); + goto end; + } + + sqlite3_bind_text(db->entries_st, ENTRIES_COLUMN_NAME, entry->name, strlen(entry->name), SQLITE_STATIC); + + if(sqlite3_step(db->entries_st) != SQLITE_DONE){ + pdebug(__func__, "failed to execute delete statement for deleting %s: %s", entry->name, sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlDeleteFail); + goto end; + } + + ret = true; +end: + if(NULL != db->entries_st){ + sqlite3_finalize(db->entries_st); + db->entries_st = NULL; + } + return ret; +} + +bool lm_database_entry_next(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ + lm_error_set(LM_ERR_ArgNULL); + return false; + } + + if(NULL != db->entries_st) + lm_entry_free(entry); + + else if(NULL == db->entries_st && + sqlite3_prepare(db->entries_db, queries[QUERY_SELECT_ENTRY_ALL], strlen(queries[QUERY_SELECT_ENTRY_ALL]), &db->entries_st, NULL) != SQLITE_OK){ + pdebug(__func__, "failed to prepare statement for selecting all: %s", sqlite3_errmsg(db->entries_db)); + lm_error_set(LM_ERR_DbSqlPrepareFail); + return false; + } + + lm_entry_init(entry); + + if(sqlite3_step(db->entries_st) != SQLITE_ROW){ + sqlite3_finalize(db->entries_st); + db->entries_st = NULL; + return false; + } + + entry->name = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_NAME-1)); + entry->desc = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_DESC-1)); + entry->version = strdup((char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_VERSION-1)); + entry->size = sqlite3_column_int64(db->entries_st, ENTRIES_COLUMN_SIZE-1); + + char *depends = (char*)sqlite3_column_text(db->entries_st, ENTRIES_COLUMN_DEPENDS-1); + if(!lm_package_data_depend_fromstr(entry, depends)){ + pdebug(__func__, "failed to load depends for finding %s: %s", entry->name, lm_strerror()); + sqlite3_finalize(db->entries_st); + return false; + } + + return true; +} + +void lm_database_entry_next_free(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ + lm_error_set(LM_ERR_ArgNULL); + return; + } + + if(NULL != db->entries_st){ + sqlite3_finalize(db->entries_st); + db->entries_st = NULL; + } + + if(NULL != entry) + lm_entry_free(entry); +} diff --git a/src/database/files.c b/src/database/files.c index c62f6ec..bbb09de 100644 --- a/src/database/files.c +++ b/src/database/files.c @@ -11,10 +11,10 @@ #include #include -size_t lm_database_files_count(lm_database_t *db, lm_pkg_t *pkg){ - size_t count = 0; +ssize_t lm_database_files_count(lm_database_t *db, lm_entry_t *entry){ + ssize_t count = 0; - if(NULL == db || NULL == pkg){ + if(NULL == db || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); goto end; } @@ -25,7 +25,7 @@ size_t lm_database_files_count(lm_database_t *db, lm_pkg_t *pkg){ return false; } - sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, entry->name, strlen(entry->name), SQLITE_STATIC); while(sqlite3_step(db->files_st) == SQLITE_ROW) count++; @@ -37,7 +37,7 @@ end: return count; } -bool lm_database_files_contains(lm_database_t *db, lm_pkg_t *pkg, char *path){ +bool lm_database_files_contains(lm_database_t *db, lm_entry_t *entry, char *path){ char *package = NULL; bool ret = false; @@ -60,8 +60,8 @@ bool lm_database_files_contains(lm_database_t *db, lm_pkg_t *pkg, char *path){ goto end; } - package = (char*)sqlite3_column_text(db->files_st, FILES_COLUMN_PACKAGE-1); - ret = eq(package, pkg->name); + package = (char*)sqlite3_column_text(db->files_st, FILES_COLUMN_ENTRY-1); + ret = eq(package, entry->name); end: if(NULL != db->files_st){ sqlite3_finalize(db->files_st); @@ -139,10 +139,10 @@ end: } -bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){ +bool lm_database_files_add(lm_database_t *db, lm_entry_t *entry, char *path, char *hash){ bool ret = false; - if(NULL == db || NULL == pkg || NULL == path || NULL == hash){ + if(NULL == db || NULL == entry || NULL == path || NULL == hash){ lm_error_set(LM_ERR_ArgNULL); goto end; } @@ -155,14 +155,14 @@ bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *h sqlite3_bind_text(db->files_st, FILES_COLUMN_PATH, path, strlen(path), SQLITE_STATIC); sqlite3_bind_text(db->files_st, FILES_COLUMN_HASH, hash, strlen(hash), SQLITE_STATIC); - if(lm_package_keep_contains(pkg, path)) + if(lm_package_data_keep_contains(entry, path)) sqlite3_bind_int(db->files_st, FILES_COLUMN_KEEP, 1); else sqlite3_bind_int(db->files_st, FILES_COLUMN_KEEP, 0); - sqlite3_bind_text(db->files_st, FILES_COLUMN_PACKAGE, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, FILES_COLUMN_ENTRY, entry->name, strlen(entry->name), SQLITE_STATIC); if(!lm_database_step_all(db->files_st)){ - pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->files_db)); + pdebug(__func__, "failed to execute insert statement for inserting %s: %s", entry->name, sqlite3_errmsg(db->files_db)); lm_error_set(LM_ERR_DbSqlInsertFail); goto end; } @@ -176,8 +176,8 @@ end: return ret; } -bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ +bool lm_database_files_del(lm_database_t *db, lm_entry_t *entry){ + if(NULL == db || NULL == entry){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -185,15 +185,15 @@ bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg){ bool ret = false; if(sqlite3_prepare(db->files_db, queries[QUERY_DELETE_FILE_ALL], strlen(queries[QUERY_DELETE_FILE_ALL]), &db->files_st, NULL) != SQLITE_OK){ - pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->files_db)); + pdebug(__func__, "failed to prepare statement for deleting %s: %s", entry->name, sqlite3_errmsg(db->files_db)); lm_error_set(LM_ERR_DbSqlPrepareFail); goto end; } - sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, entry->name, strlen(entry->name), SQLITE_STATIC); if(!lm_database_step_all(db->files_st)){ - pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->files_db)); + pdebug(__func__, "failed to execute delete statement for deleting %s: %s", entry->name, sqlite3_errmsg(db->files_db)); lm_error_set(LM_ERR_DbSqlDeleteFail); goto end; } @@ -207,8 +207,8 @@ end: return ret; } -bool lm_database_files_next(lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, bool *keep){ - if(NULL == db || NULL == pkg || NULL == path || NULL == hash || NULL == keep){ +bool lm_database_files_next(lm_database_t *db, lm_entry_t *entry, char **path, char **hash, bool *keep){ + if(NULL == db || NULL == entry || NULL == path || NULL == hash || NULL == keep){ lm_error_set(LM_ERR_ArgNULL); return false; } @@ -219,7 +219,7 @@ bool lm_database_files_next(lm_database_t *db, lm_pkg_t *pkg, char **path, char lm_error_set(LM_ERR_DbSqlPrepareFail); return false; } - sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, entry->name, strlen(entry->name), SQLITE_STATIC); } else if(NULL != db->files_st){ @@ -242,8 +242,8 @@ bool lm_database_files_next(lm_database_t *db, lm_pkg_t *pkg, char **path, char return true; } -void lm_database_files_next_free(lm_database_t *db, lm_pkg_t *pkg, char **path, char **hash, bool *keep){ - if(NULL == db || NULL == pkg || NULL == path || NULL == hash || NULL == keep){ +void lm_database_files_next_free(lm_database_t *db, lm_entry_t *entry, char **path, char **hash, bool *keep){ + if(NULL == db || NULL == entry || NULL == path || NULL == hash || NULL == keep){ lm_error_set(LM_ERR_ArgNULL); return; } diff --git a/src/database/package.c b/src/database/package.c deleted file mode 100644 index 1a6bf87..0000000 --- a/src/database/package.c +++ /dev/null @@ -1,197 +0,0 @@ -#include "../../include/database.h" -#include "../../include/package.h" -#include "../../include/error.h" -#include "../../include/util.h" - -#include -#include -#include -#include - -bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ - lm_error_set(LM_ERR_ArgNULL); - return false; - } - - char depends[lm_package_depend_strlen(pkg)]; - bool ret = false; - - if(sqlite3_prepare(db->packages_db, queries[QUERY_INSERT_PACKAGE_SINGLE], strlen(queries[QUERY_INSERT_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){ - pdebug(__func__, "failed to prepare statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db)); - lm_error_set(LM_ERR_DbSqlPrepareFail); - goto end; - } - - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, pkg->name, strlen(pkg->name), SQLITE_STATIC); - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DESC, pkg->desc, strlen(pkg->desc), SQLITE_STATIC); - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_VERSION, pkg->version, strlen(pkg->version), SQLITE_STATIC); - sqlite3_bind_int64(db->packages_st, PACKAGES_COLUMN_SIZE, pkg->size); - - if(!lm_package_depend_tostr(pkg, depends)){ - 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(!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; - } - - ret = true; -end: - if(NULL != db->packages_st){ - sqlite3_finalize(db->packages_st); - db->packages_st = NULL; - } - return ret; -} - -bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg, char *name, char *version){ - if(NULL == db || NULL == name){ - lm_error_set(LM_ERR_ArgNULL); - return false; - } - - bool ret = false; - char *query = NULL; - - if(NULL == version) - query = queries[QUERY_SELECT_PACKAGE_SINGLE_1]; - else - query = queries[QUERY_SELECT_PACKAGE_SINGLE_2]; - - // package pointer should already be free'd - // we are initing it just in case the caller didn't - if(NULL != pkg) - lm_package_init(pkg); - - if(sqlite3_prepare(db->packages_db, query, strlen(query), &db->packages_st, NULL) != SQLITE_OK){ - pdebug(__func__, "failed to prepare statement for finding %s: %s", name, sqlite3_errmsg(db->packages_db)); - lm_error_set(LM_ERR_DbSqlPrepareFail); - goto end; - } - - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, name, strlen(name), SQLITE_STATIC); - if(NULL != version) - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_VERSION, version, strlen(version), SQLITE_STATIC); - - if(sqlite3_step(db->packages_st) != SQLITE_ROW){ - pdebug(__func__, "got no rows for %s", name); - lm_error_set(LM_ERR_DbSqlNotFound); - goto end; - } - - if(NULL == pkg){ - ret = true; - goto end; - } - - pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_NAME-1)); - pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DESC-1)); - pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_VERSION-1)); - pkg->size = sqlite3_column_int64(db->packages_st, PACKAGES_COLUMN_SIZE-1); - - char *depends = (char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DEPENDS-1); - if(!lm_package_depend_fromstr(pkg, depends)){ - pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror()); - // error is set by the function - goto end; - } - - ret = true; -end: - if(NULL != db->packages_st){ - sqlite3_finalize(db->packages_st); - db->packages_st = NULL; - } - return ret; -} - -bool lm_database_package_del(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ - lm_error_set(LM_ERR_ArgNULL); - return false; - } - - bool ret = false; - - if(sqlite3_prepare(db->packages_db, queries[QUERY_DELETE_PACKAGE_SINGLE], strlen(queries[QUERY_DELETE_PACKAGE_SINGLE]), &db->packages_st, NULL) != SQLITE_OK){ - pdebug(__func__, "failed to prepare statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db)); - lm_error_set(LM_ERR_DbSqlPrepareFail); - goto end; - } - - sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_NAME, pkg->name, strlen(pkg->name), SQLITE_STATIC); - - if(sqlite3_step(db->packages_st) != SQLITE_DONE){ - pdebug(__func__, "failed to execute delete statement for deleting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db)); - lm_error_set(LM_ERR_DbSqlDeleteFail); - goto end; - } - - ret = true; -end: - if(NULL != db->packages_st){ - sqlite3_finalize(db->packages_st); - db->packages_st = NULL; - } - return ret; -} - -bool lm_database_package_next(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ - lm_error_set(LM_ERR_ArgNULL); - return false; - } - - 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; - } - - lm_package_init(pkg); - - if(sqlite3_step(db->packages_st) != SQLITE_ROW){ - sqlite3_finalize(db->packages_st); - db->packages_st = NULL; - return false; - } - - pkg->name = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_NAME-1)); - pkg->desc = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DESC-1)); - pkg->version = strdup((char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_VERSION-1)); - pkg->size = sqlite3_column_int64(db->packages_st, PACKAGES_COLUMN_SIZE-1); - - char *depends = (char*)sqlite3_column_text(db->packages_st, PACKAGES_COLUMN_DEPENDS-1); - if(!lm_package_depend_fromstr(pkg, depends)){ - pdebug(__func__, "failed to load depends for finding %s: %s", pkg->name, lm_strerror()); - sqlite3_finalize(db->packages_st); - return false; - } - - return true; -} - -void lm_database_package_next_free(lm_database_t *db, lm_pkg_t *pkg){ - if(NULL == db || NULL == pkg){ - lm_error_set(LM_ERR_ArgNULL); - return; - } - - 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 fabc990..1626735 100644 --- a/src/error.c +++ b/src/error.c @@ -73,8 +73,8 @@ void lm_error_set(lm_error_t code, ...) { {.code = LM_ERR_CtxRootFail, .desc = _("specified root path does not exist") }, {.code = LM_ERR_CtxRootNotDir, .desc = _("specified root path is not a directory") }, {.code = LM_ERR_CtxRootNoWrite, .desc = _("specified root directory does not have write access") }, - {.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") }, - {.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") }, + {.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path is not a directory") }, + {.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data directory does not have write access") }, {.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") }, {.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") }, {.code = LM_ERR_PkgPathsEmpty, .desc = _("package file and directory paths are empty") }, @@ -148,7 +148,8 @@ void lm_error_set(lm_error_t code, ...) { {.code = LM_ERR_InstallBackChdirFail, .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_InstallRunFail, .desc = _("failed to run the package install script: %s") }, + {.code = LM_ERR_InstallSaveFail, .desc = _("failed to save the package install script: %s") }, {.code = LM_ERR_PkgBreaks, .desc = _("removing package breaks %s") }, {.code = LM_ERR_PkgUpToDate, .desc = _("package is already up-to-date") }, {.code = LM_ERR_HashOpenFail, .desc = _("failed to open file for hashing") }, @@ -159,6 +160,7 @@ void lm_error_set(lm_error_t code, ...) { {.code = LM_ERR_NoPools, .desc = _("pool list is empty") }, {.code = LM_ERR_DbChangesNotExists, .desc = _("package changes file not found in the database") }, {.code = LM_ERR_DbChangesChmodFail, .desc = _("failed to change mod of the changes file") }, + {.code = LM_ERR_InstallDirFail, .desc = _("failed to create install script save directory") }, }; char *fmt = NULL; diff --git a/src/package/data.c b/src/package/data.c index 47c0b92..1f71e7b 100644 --- a/src/package/data.c +++ b/src/package/data.c @@ -7,48 +7,48 @@ #include #include -int lm_package_data_handler(void *data, const char *_section, const char *_key, const char *_value) { +int lm_package_data_handler(void *_data, const char *_section, const char *_key, const char *_value) { char *section = (char *)_section, *value = (char *)_value, *key = (char *)_key; - lm_pkg_t *pkg = data; + lm_pkg_data_t *data = _data; - if(NULL == pkg->name){ + if(NULL == data->name){ if(!package_name_valid(section)) return 0; - pkg->name = strdup(section); + data->name = strdup(section); } - else if(!eq(pkg->name, section)) + else if(!eq(data->name, section)) return 0; if(eq(key, PKG_DATA_DESC)) - pkg->desc = strdup(value); + data->desc = strdup(value); else if(eq(key, PKG_DATA_VERSION)){ if(!package_version_valid(value)) return 0; - pkg->version = strdup(value); + data->version = strdup(value); } else if(eq(key, PKG_DATA_SIZE)) - pkg->size = atol(value); + data->size = atol(value); else if(eq(key, PKG_DATA_DEPENDS)){ - if(!lm_package_depend_add(pkg, value)) + if(!lm_package_data_depend_add(data, value)) return 0; } else if(eq(key, PKG_DATA_KEEPS)){ - if(!lm_package_keep_add(pkg, value)) + if(!lm_package_data_keep_add(data, value)) return 0; } return 1; } -bool lm_package_data_load(lm_pkg_t *pkg, char *file){ - lm_package_data_free(pkg); +bool lm_package_data_load(lm_pkg_data_t *data, char *file){ + lm_package_data_free(data); - if (ini_parse(file, lm_package_data_handler, pkg) < 0) { + if (ini_parse(file, lm_package_data_handler, data) < 0) { lm_error_set(LM_ERR_PkgDataBad); return false; } @@ -56,11 +56,11 @@ bool lm_package_data_load(lm_pkg_t *pkg, char *file){ return true; } -void lm_package_data_free(lm_pkg_t *pkg){ - free(pkg->desc); - free(pkg->name); - free(pkg->version); - - lm_package_depend_free(pkg); - lm_package_keep_free(pkg); +void lm_package_data_free(lm_pkg_data_t *data){ + free(data->name); + free(data->desc); + free(data->version); + lm_package_data_depend_free(data); + lm_package_data_keep_free(data); + bzero(&data, sizeof(lm_pkg_data_t)); } diff --git a/src/package/depend.c b/src/package/depend.c index aeec8dc..cd58e0f 100644 --- a/src/package/depend.c +++ b/src/package/depend.c @@ -1,24 +1,23 @@ #include "../../include/package.h" #include "../../include/error.h" -#include "../../include/types.h" #include "../../include/util.h" #include #include -size_t lm_package_depend_count(lm_pkg_t *pkg){ +ssize_t lm_package_data_depend_count(lm_pkg_data_t *data){ size_t index = 0; - if(NULL == pkg->depends) + if(NULL == data->depends) return index; - while(pkg->depends[index] != NULL) + while(data->depends[index] != NULL) index++; return index; } -bool lm_package_depend_add(lm_pkg_t *pkg, char *depend){ +bool lm_package_data_depend_add(lm_pkg_data_t *data, char *depend){ if(NULL == depend){ lm_error_set(LM_ERR_ArgNULL); return false; @@ -29,76 +28,76 @@ bool lm_package_depend_add(lm_pkg_t *pkg, char *depend){ return false; } - if(NULL == pkg->depends){ - pkg->depends = malloc(sizeof(char*)*2); - pkg->depends[0] = strdup(depend); - pkg->depends[1] = NULL; + if(NULL == data->depends){ + data->depends = malloc(sizeof(char*)*2); + data->depends[0] = strdup(depend); + data->depends[1] = NULL; return true; } - size_t count = lm_package_depend_count(pkg); - pkg->depends = realloc(pkg->depends, sizeof(char*)*(count+2)); - pkg->depends[count++] = strdup(depend); - pkg->depends[count] = NULL; + size_t count = lm_package_data_depend_count(data); + data->depends = realloc(data->depends, sizeof(char*)*(count+2)); + data->depends[count++] = strdup(depend); + data->depends[count] = NULL; return true; } -void lm_package_depend_free(lm_pkg_t *pkg){ - if(NULL == pkg) +void lm_package_data_depend_free(lm_pkg_data_t *data){ + if(NULL == data) return; - if(NULL == pkg->depends) + if(NULL == data->depends) return; - for(int i = 0; pkg->depends[i] != NULL; i++) - free(pkg->depends[i]); - free(pkg->depends); + for(int i = 0; data->depends[i] != NULL; i++) + free(data->depends[i]); + free(data->depends); - pkg->depends = NULL; + data->depends = NULL; } -size_t lm_package_depend_strlen(lm_pkg_t *pkg){ +ssize_t lm_package_data_depend_strlen(lm_pkg_data_t *data){ size_t len = 1; - if(NULL == pkg->depends) + if(NULL == data->depends) return len; - for(int i = 0; NULL != pkg->depends[i]; i++) - len += strlen(pkg->depends[i])+1; + for(int i = 0; NULL != data->depends[i]; i++) + len += strlen(data->depends[i])+1; return len; } -char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ - if(NULL == pkg){ +char *lm_package_data_depend_tostr(lm_pkg_data_t *data, char *buffer){ + if(NULL == data){ lm_error_set(LM_ERR_ArgNULL); return NULL; } if(NULL == buffer){ - size_t buflen = lm_package_depend_strlen(pkg)+1; + size_t buflen = lm_package_data_depend_strlen(data)+1; buffer = malloc(buflen); bzero(buffer, buflen); } - if(NULL == pkg->depends){ + if(NULL == data->depends){ buffer[0] = 0; return buffer; } size_t bufsz = 0, depsz = 0; - for(int i = 0; NULL != pkg->depends[i]; i++){ - depsz = strlen(pkg->depends[i]); + for(int i = 0; NULL != data->depends[i]; i++){ + depsz = strlen(data->depends[i]); if(i == 0){ - memcpy(buffer, pkg->depends[i], depsz); + memcpy(buffer, data->depends[i], depsz); bufsz += depsz; continue; } buffer[bufsz++] = ','; - memcpy(buffer+bufsz, pkg->depends[i], depsz); + memcpy(buffer+bufsz, data->depends[i], depsz); bufsz += depsz; } @@ -106,13 +105,13 @@ char *lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){ return buffer; } -bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){ - if(NULL == pkg){ +bool lm_package_data_depend_fromstr(lm_pkg_data_t *data, char *buffer){ + if(NULL == data){ lm_error_set(LM_ERR_ArgNULL); return false; } - lm_package_depend_free(pkg); + lm_package_data_depend_free(data); if(NULL == buffer) return true; @@ -124,24 +123,24 @@ bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){ return true; do { - if(!lm_package_depend_add(pkg, dep)) + if(!lm_package_data_depend_add(data, dep)) return false; }while((dep = strtok_r(NULL, ",", &save)) != NULL); return true; } -bool lm_package_depends(lm_pkg_t *pkg, char *dep){ - if(NULL == pkg || NULL == dep){ +bool lm_package_data_depends(lm_pkg_data_t *data, char *dep){ + if(NULL == data || NULL == dep){ lm_error_set(LM_ERR_ArgNULL); return false; } - if(NULL == pkg->depends) + if(NULL == data->depends) return false; - for(int i = 0; pkg->depends[i] != NULL; i++) - if(eq(pkg->depends[i], dep)) + for(int i = 0; data->depends[i] != NULL; i++) + if(eq(data->depends[i], dep)) return true; return false; } diff --git a/src/package/keep.c b/src/package/keep.c index 009e356..440a463 100644 --- a/src/package/keep.c +++ b/src/package/keep.c @@ -1,69 +1,68 @@ #include "../../include/package.h" -#include "../../include/types.h" #include "../../include/error.h" #include "../../include/util.h" #include #include -size_t lm_package_keep_count(lm_pkg_t *pkg){ +size_t lm_package_data_keep_count(lm_pkg_data_t *data){ size_t index = 0; - if(NULL == pkg->keeps) + if(NULL == data->keeps) return index; - while(pkg->keeps[index] != NULL) + while(data->keeps[index] != NULL) index++; return index; } -bool lm_package_keep_add(lm_pkg_t *pkg, char *path){ +bool lm_package_data_keep_add(lm_pkg_data_t *data, char *path){ if(NULL == path){ lm_error_set(LM_ERR_ArgNULL); return false; } - if(NULL == pkg->keeps){ - pkg->keeps = malloc(sizeof(char*)*2); - pkg->keeps[0] = strdup(path); - pkg->keeps[1] = NULL; + if(NULL == data->keeps){ + data->keeps = malloc(sizeof(char*)*2); + data->keeps[0] = strdup(path); + data->keeps[1] = NULL; return true; } - size_t count = lm_package_depend_count(pkg); - pkg->keeps = realloc(pkg->depends, sizeof(char*)*(count+2)); - pkg->keeps[count++] = strdup(path); - pkg->keeps[count] = NULL; + size_t count = lm_package_data_depend_count(data); + data->keeps = realloc(data->depends, sizeof(char*)*(count+2)); + data->keeps[count++] = strdup(path); + data->keeps[count] = NULL; return true; } -bool lm_package_keep_contains(lm_pkg_t *pkg, char *path){ - if(NULL == pkg || NULL == path){ +bool lm_package_data_keep_contains(lm_pkg_data_t *data, char *path){ + if(NULL == data || NULL == path){ lm_error_set(LM_ERR_ArgNULL); return false; } - if(NULL == pkg->keeps) + if(NULL == data->keeps) return false; - for(int i = 0; NULL != pkg->keeps[i]; i++) - if(eq(pkg->keeps[i], path)) + for(int i = 0; NULL != data->keeps[i]; i++) + if(eq(data->keeps[i], path)) return true; return false; } -void lm_package_keep_free(lm_pkg_t *pkg){ - if(NULL == pkg) +void lm_package_data_keep_free(lm_pkg_data_t *data){ + if(NULL == data) return; - if(NULL == pkg->keeps) + if(NULL == data->keeps) return; - for(int i = 0; pkg->keeps[i] != NULL; i++) - free(pkg->keeps[i]); - free(pkg->keeps); + for(int i = 0; data->keeps[i] != NULL; i++) + free(data->keeps[i]); + free(data->keeps); - pkg->keeps = NULL; + data->keeps = NULL; } diff --git a/src/package/package.c b/src/package/package.c index 6614811..583be43 100644 --- a/src/package/package.c +++ b/src/package/package.c @@ -1,6 +1,5 @@ #include "../../include/package.h" #include "../../include/error.h" -#include "../../include/types.h" #include "../../include/mptp.h" #include "../../include/util.h" @@ -26,7 +25,7 @@ bool lm_package_downloaded(lm_pkg_t *pkg){ return false; } - if(!exists(pkg->paths.archive) || !exists(pkg->paths.signature)){ + if(!exists(pkg->archive) || !exists(pkg->signature)){ lm_error_set(LM_ERR_PkgNotDownloaded); return false; } @@ -35,12 +34,12 @@ bool lm_package_downloaded(lm_pkg_t *pkg){ } bool lm_package_remove_download(lm_pkg_t *pkg){ - if(unlink(pkg->paths.archive) < 0 && errno != ENOENT){ + if(unlink(pkg->archive) < 0 && errno != ENOENT){ lm_error_set(LM_ERR_PkgRemoveDownloadFail); return false; } - if(unlink(pkg->paths.signature) < 0 && errno != ENOENT){ + if(unlink(pkg->signature) < 0 && errno != ENOENT){ lm_error_set(LM_ERR_PkgRemoveDownloadFail); return false; } @@ -54,41 +53,15 @@ bool lm_package_is_same(lm_pkg_t *one, lm_pkg_t *two){ return false; } - return eq(one->version, two->version) && - eq(one->name, two->name) && - eq(one->desc, two->desc) && - one->size == two->size; -} - -bool lm_package_copy(lm_pkg_t *dst, lm_pkg_t *src){ - lm_package_init(dst); - - if(NULL != src->name) - dst->name = strdup(src->name); - - if(NULL != src->version) - dst->version = strdup(src->version); - - if(NULL != src->desc) - dst->desc = strdup(src->desc); - - dst->size = src->size; - dst->pool = src->pool; - - if(NULL != src->keeps){ - for(int i = 0; src->keeps[i] != NULL; i++) - lm_package_keep_add(dst, src->keeps[i]); - } - - if(NULL != src->depends){ - for(int i = 0; src->depends[i] != NULL; i++) - lm_package_depend_add(dst, src->depends[i]); - } - - return lm_package_path_copy(dst, src); + return eq(one->data.version, two->data.version) && + eq(one->data.name, two->data.name) && + eq(one->data.desc, two->data.desc) && + one->data.size == two->data.size; } void lm_package_free(lm_pkg_t *pkg){ - lm_package_data_free(pkg); + free(pkg->archive); + free(pkg->signature); + lm_package_data_free(&pkg->data); bzero(pkg, sizeof(lm_pkg_t)); } diff --git a/src/package/path.c b/src/package/path.c index 622aef7..e69b6b0 100644 --- a/src/package/path.c +++ b/src/package/path.c @@ -1,14 +1,13 @@ #include "../../include/package.h" #include "../../include/error.h" -#include "../../include/types.h" #include "../../include/util.h" #include #include bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path){ - free(pkg->paths.archive); - pkg->paths.archive = NULL; + free(pkg->archive); + pkg->archive = NULL; if(NULL == archive_path) return true; @@ -16,13 +15,13 @@ bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path){ if(is_dir(archive_path)) return false; - pkg->paths.archive = strdup(archive_path); + pkg->archive = strdup(archive_path); return true; } bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path){ - free(pkg->paths.signature); - pkg->paths.signature = NULL; + free(pkg->signature); + pkg->signature = NULL; if(NULL == signature_path) return true; @@ -30,13 +29,13 @@ bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path){ if(is_dir(signature_path)) return false; - pkg->paths.signature = strdup(signature_path); + pkg->signature = strdup(signature_path); return true; } bool lm_package_path_is_empty(lm_pkg_t *pkg){ - return NULL == pkg->paths.signature || - NULL == pkg->paths.archive; + return NULL == pkg->signature || + NULL == pkg->archive; } bool lm_package_path_copy(lm_pkg_t *dst, lm_pkg_t *src){ @@ -45,16 +44,11 @@ bool lm_package_path_copy(lm_pkg_t *dst, lm_pkg_t *src){ return false; } - if(NULL != src->paths.archive) - dst->paths.archive = strdup(src->paths.archive); + if(NULL != src->archive) + dst->archive = strdup(src->archive); - if(NULL != src->paths.signature) - dst->paths.signature = strdup(src->paths.signature); + if(NULL != src->signature) + dst->signature = strdup(src->signature); return true; } - -void lm_package_path_free(lm_pkg_t *pkg){ - free(pkg->paths.signature); - free(pkg->paths.archive); -} diff --git a/src/package/verify.c b/src/package/verify.c index 2a7613c..80b366c 100644 --- a/src/package/verify.c +++ b/src/package/verify.c @@ -26,27 +26,27 @@ bool lm_package_verify(lm_pkg_t *pkg){ err = gpgme_new(&ctx); if (err != GPG_ERR_NO_ERROR) { - pdebug(__func__, "(%s) failed to create gpgme: %s", pkg->name, gpgme_strerror(err)); + pdebug(__func__, "(%s) failed to create gpgme: %s", pkg->data.name, gpgme_strerror(err)); lm_error_set(LM_ERR_PkgGPGFail); return false; } gpgme_op_import(ctx, NULL); - if((err = gpgme_data_new_from_file(&sig, pkg->paths.signature, 1)) != GPG_ERR_NO_ERROR){ - pdebug(__func__, "(%s) failed to import package signature %s", pkg->name, gpgme_strerror(err)); + if((err = gpgme_data_new_from_file(&sig, pkg->signature, 1)) != GPG_ERR_NO_ERROR){ + pdebug(__func__, "(%s) failed to import package signature %s", pkg->data.name, gpgme_strerror(err)); lm_error_set(LM_ERR_PkgGPGSigFail); goto end; } - if ((err = gpgme_data_new_from_file(&text, pkg->paths.archive, 1)) != GPG_ERR_NO_ERROR) { - pdebug(__func__, "(%s) failed to import package archive %s", pkg->name, gpgme_strerror(err)); + if ((err = gpgme_data_new_from_file(&text, pkg->archive, 1)) != GPG_ERR_NO_ERROR) { + pdebug(__func__, "(%s) failed to import package archive %s", pkg->data.name, gpgme_strerror(err)); lm_error_set(LM_ERR_PkgGPGArchiveFail); goto end; } if ((err = gpgme_op_verify(ctx, sig, text, NULL)) != GPG_ERR_NO_ERROR) { - pdebug(__func__, "(%s) failed to verify package archive %s", pkg->name, gpgme_strerror(err)); + pdebug(__func__, "(%s) failed to verify package archive %s", pkg->data.name, gpgme_strerror(err)); lm_error_set(LM_ERR_PkgSigNoResult); goto end; } diff --git a/src/pool/info.c b/src/pool/info.c index 07d40e5..f5930c7 100644 --- a/src/pool/info.c +++ b/src/pool/info.c @@ -40,12 +40,12 @@ bool lm_pool_info_load(lm_pool_t *pool) { return false; } - if(!exists(pool->paths.info_file) || !can_read(pool->paths.info_file)){ + if(!exists(pool->info_file) || !can_read(pool->info_file)){ lm_error_set(LM_ERR_PoolInfoCantRead); return false; } - if (ini_parse(pool->paths.info_file, lm_pool_info_handler, pool) < 0) { + if (ini_parse(pool->info_file, lm_pool_info_handler, pool) < 0) { lm_error_set(LM_ERR_PoolInfoBad); return false; } @@ -76,7 +76,7 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback return false; } - if(!mkdir_ifnot(pool->paths.dir)){ + if(!mkdir_ifnot(pool->dir)){ lm_error_set(LM_ERR_PoolBadDir); return false; } @@ -100,7 +100,7 @@ bool lm_pool_info_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback if(!lm_mptp_client_send(sock, &packet)) goto end; - if(!lm_mptp_recvfile(sock, pool->paths.info_file, callback, data)) + if(!lm_mptp_recvfile(sock, pool->info_file, callback, data)) goto end; ret = true; diff --git a/src/pool/list.c b/src/pool/list.c index 1446728..33ffc9b 100644 --- a/src/pool/list.c +++ b/src/pool/list.c @@ -22,32 +22,32 @@ bool lm_pool_list_load(lm_pool_t *pool){ return false; } - if(!exists(pool->paths.list_file) || !can_read(pool->paths.list_file)){ + if(!exists(pool->list_file) || !can_read(pool->list_file)){ lm_error_set(LM_ERR_PoolListCantRead); return false; } - pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->paths.list_dir); + pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->list_dir); - if(!mkdir_ifnot(pool->paths.dir)){ + if(!mkdir_ifnot(pool->dir)){ lm_error_set(LM_ERR_PoolBadDir); return false; } - if(!mkdir_ifnot(pool->paths.list_dir)){ + if(!mkdir_ifnot(pool->list_dir)){ lm_error_set(LM_ERR_PoolBadPaths); return false; } - size_t ent_len = 0, list_dir_len = strlen(pool->paths.list_dir); + size_t ent_len = 0, list_dir_len = strlen(pool->list_dir); struct dirent *ent = NULL; DIR *dirfd = NULL; bool ret = false; - if(!extract_archive(pool->paths.list_dir, pool->paths.list_file)) + if(!extract_archive(pool->list_dir, pool->list_file)) goto end; - if((dirfd = opendir(pool->paths.list_dir)) == NULL){ + if((dirfd = opendir(pool->list_dir)) == NULL){ lm_error_set(LM_ERR_PoolListDirFail); goto end; } @@ -58,23 +58,23 @@ bool lm_pool_list_load(lm_pool_t *pool){ ent_len = strlen(ent->d_name); char datap[ent_len+list_dir_len+10]; - join_multiple(datap, pool->paths.list_dir, ent->d_name, "DATA"); + join_multiple(datap, pool->list_dir, ent->d_name, "DATA"); lm_pkg_t *pkg = lm_package_new(); - if(!lm_package_data_load(pkg, datap)){ + if(!lm_package_data_load(&pkg->data, datap)){ pdebug(__func__, "(%s) failed to load new package from %s", pool->name, datap); lm_package_free(pkg); continue; } if(!lm_pool_package_add(pool, pkg)){ - pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->name); + pdebug(__func__, "(%s) failed to add new package: %s", pool->name, pkg->data.name); lm_package_free(pkg); continue; } - pdebug(__func__, "(%s) added new package: %s", pool->name, pkg->name); + pdebug(__func__, "(%s) added new package: %s", pool->name, pkg->data.name); } ret = true; @@ -101,12 +101,12 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback return false; } - if(!mkdir_ifnot(pool->paths.dir)){ + if(!mkdir_ifnot(pool->dir)){ lm_error_set(LM_ERR_PoolBadDir); return false; } - if(!mkdir_ifnot(pool->paths.list_dir)){ + if(!mkdir_ifnot(pool->list_dir)){ lm_error_set(LM_ERR_PoolBadPaths); return false; } @@ -130,7 +130,7 @@ bool lm_pool_list_download(lm_pool_t *pool, lm_mptp_transfer_callback_t callback if(!lm_mptp_client_send(sock, &packet)) goto end; - if(!lm_mptp_recvfile(sock, pool->paths.list_file, callback, data)) + if(!lm_mptp_recvfile(sock, pool->list_file, callback, data)) goto end; ret = true; diff --git a/src/pool/package.c b/src/pool/package.c index 8bd1a15..aca6488 100644 --- a/src/pool/package.c +++ b/src/pool/package.c @@ -14,13 +14,13 @@ lm_pkg_t *lm_pool_package_find(lm_pool_t *pool, char *name, char *version){ lm_pkg_t *cur = pool->pkg; while(NULL != cur){ - if(NULL == version && eq(cur->name, name)) + if(NULL == version && eq(cur->data.name, name)) return cur; - else if(NULL == name && eq(cur->version, version)) + else if(NULL == name && eq(cur->data.version, version)) return cur; - else if(eq(cur->name, name) && eq(cur->version, version)) + else if(eq(cur->data.name, name) && eq(cur->data.version, version)) return cur; cur = cur->next; @@ -38,17 +38,17 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){ pkg->pool = pool; if(!lm_pool_path_is_empty(pool)){ - size_t path_size = strlen(pool->paths.dir)+strlen(pkg->name)+strlen(pkg->version); - size_t name_size = path_size - strlen(pool->paths.dir); + size_t path_size = strlen(pool->dir)+strlen(pkg->data.name)+strlen(pkg->data.version); + size_t name_size = path_size - strlen(pool->dir); char archive_path[path_size+10], sig_path[path_size+20]; char archive_name[name_size+10], sig_name[name_size+20]; - sprintf(archive_name, "%s_%s.mpf", pkg->name, pkg->version); - sprintf(sig_name, "%s_%s.mpf.sig", pkg->name, pkg->version); + sprintf(archive_name, "%s_%s.mpf", pkg->data.name, pkg->data.version); + sprintf(sig_name, "%s_%s.mpf.sig", pkg->data.name, pkg->data.version); - join(archive_path, pool->paths.dir, archive_name); - join(sig_path, pool->paths.dir, sig_name); + join(archive_path, pool->dir, archive_name); + join(sig_path, pool->dir, sig_name); lm_package_path_set_archive(pkg, archive_path); lm_package_path_set_signature(pkg, sig_path); diff --git a/src/pool/path.c b/src/pool/path.c index 78110c3..d8115e4 100644 --- a/src/pool/path.c +++ b/src/pool/path.c @@ -1,4 +1,3 @@ -#include "../../include/types.h" #include "../../include/error.h" #include "../../include/pool.h" #include "../../include/util.h" @@ -7,8 +6,15 @@ #include bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){ - free(pool->paths.dir); - pool->paths.dir = NULL; + free(pool->dir); + free(pool->list_dir); + free(pool->info_file); + free(pool->list_file); + + pool->dir = NULL; + pool->list_dir = NULL; + pool->info_file = NULL; + pool->list_file = NULL; if(NULL == dir) return true; @@ -18,25 +24,22 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){ return false; } - pool->paths.dir = strdup(dir); - pool->paths.info_file = join_alloc(dir, "INFO"); - pool->paths.list_file = join_alloc(dir, "LIST"); - pool->paths.list_dir = join_alloc(dir, "LIST_extracted"); + pool->dir = strdup(dir); + pool->info_file = join_alloc(dir, "INFO"); + pool->list_file = join_alloc(dir, "LIST"); + pool->list_dir = join_alloc(dir, "LIST_extracted"); - if(exists(pool->paths.info_file) && (!is_file(pool->paths.info_file) || !can_read(pool->paths.info_file) || !can_write(pool->paths.info_file))){ - lm_pool_path_free(pool); + if(exists(pool->info_file) && (!is_file(pool->info_file) || !can_read(pool->info_file) || !can_write(pool->info_file))){ lm_error_set(LM_ERR_PoolBadPaths); return false; } - if(exists(pool->paths.list_file) && (!is_file(pool->paths.list_file) || !can_read(pool->paths.list_file) || !can_write(pool->paths.list_file))){ - lm_pool_path_free(pool); + if(exists(pool->list_file) && (!is_file(pool->list_file) || !can_read(pool->list_file) || !can_write(pool->list_file))){ lm_error_set(LM_ERR_PoolBadPaths); return false; } - if(exists(pool->paths.list_dir) && (is_file(pool->paths.list_dir) || !can_read(pool->paths.list_dir) || !can_write(pool->paths.list_dir))){ - lm_pool_path_free(pool); + if(exists(pool->list_dir) && (is_file(pool->list_dir) || !can_read(pool->list_dir) || !can_write(pool->list_dir))){ lm_error_set(LM_ERR_PoolBadPaths); return false; } @@ -45,16 +48,8 @@ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){ } bool lm_pool_path_is_empty(lm_pool_t *pool){ - return NULL == pool->paths.info_file || - NULL == pool->paths.list_file || - NULL == pool->paths.list_dir || - NULL == pool->paths.dir; -} - -void lm_pool_path_free(lm_pool_t *pool){ - free(pool->paths.dir); - free(pool->paths.list_dir); - free(pool->paths.info_file); - free(pool->paths.list_file); - bzero(&pool->paths, sizeof(pool->paths)); + return NULL == pool->info_file || + NULL == pool->list_file || + NULL == pool->list_dir || + NULL == pool->dir; } diff --git a/src/pool/pool.c b/src/pool/pool.c index 0c65127..622c149 100644 --- a/src/pool/pool.c +++ b/src/pool/pool.c @@ -65,6 +65,10 @@ end: } void lm_pool_free(lm_pool_t *pool) { + free(pool->dir); + free(pool->list_dir); + free(pool->info_file); + free(pool->list_file); lm_url_free(&pool->url); lm_pool_info_free(pool); lm_pool_list_free(pool); diff --git a/src/util.c b/src/util.c index 591768f..bd478e0 100644 --- a/src/util.c +++ b/src/util.c @@ -1,16 +1,17 @@ #include "../include/util.h" #include "../include/error.h" -#include "../include/types.h" #include #include #include +#include #include #include #include #include #include #include +#include void pdebug(const char *func, const char *fmt, ...) { if (LM_DEBUG != 1) @@ -324,7 +325,7 @@ char *join_alloc(const char *base, const char *pth) { bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg) { lm_pkg_t *cur = list; while (cur) { - if (eq(pkg->name, cur->name)) + if (eq(pkg->data.name, cur->data.name)) return true; cur = cur->next; } @@ -340,7 +341,7 @@ lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg) { if (NULL == list) return list; - if (eq(list->name, pkg->name)) { + if (eq(list->data.name, pkg->data.name)) { list = NULL; return list; } @@ -349,7 +350,7 @@ lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg) { lm_pkg_t *found = NULL; while (NULL != cur->next) { - if (eq(cur->next->name, pkg->name)) { + if (eq(cur->next->data.name, pkg->data.name)) { found = cur->next; cur->next = cur->next->next; break; @@ -469,3 +470,31 @@ char *get_md5(char *path) { fclose(file); return sum; } + +bool is_empty(char *p) { + char buf[1]; + struct stat st; + + bzero(buf, sizeof(buf)); + bzero(&st, sizeof(st)); + stat(p, &st); + + if (st.st_size <= 0) + return true; + + if (st.st_size > 1) + return false; + + int fd = open(p, O_RDONLY); + bool ret = false; + + if (read(fd, buf, 1) != 1) + goto end; + + if (buf[0] == ' ' || buf[0] == '\n') + ret = true; + +end: + close(fd); + return ret; +}