From d0dce209fab755885e5e777c355539037ba51b29 Mon Sep 17 00:00:00 2001 From: ngn Date: Mon, 8 Jul 2024 13:08:45 +0300 Subject: [PATCH] fix: remove operation file check fixes --- examples/client/remove.c | 2 ++ locale/tr/LC_MESSAGES/libmp.po | 2 +- src/ctx/install.c | 8 ++++++++ src/ctx/remove.c | 31 ++++++++++++++++++++++++------- src/database/files.c | 10 +++++----- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/examples/client/remove.c b/examples/client/remove.c index 1994beb..c11d3e2 100644 --- a/examples/client/remove.c +++ b/examples/client/remove.c @@ -44,6 +44,8 @@ int main(int argc, char *argv[]) { goto end; } + printf("removing package: %s (%s)...\n", pkg.name, pkg.version); + if (!lm_ctx_remove(&ctx, &pkg, NULL, NULL)) { printf("failed to remove package: %s (%d)\n", lm_strerror(), lm_error()); goto end; diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index b660996..7869b6e 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-08 12:47+0300\n" +"POT-Creation-Date: 2024-07-08 13:05+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/install.c b/src/ctx/install.c index 0d49871..56d7f36 100644 --- a/src/ctx/install.c +++ b/src/ctx/install.c @@ -15,6 +15,14 @@ #include bool __lm_ctx_run_install(char *root, char *install_path) { + struct stat st; + bzero(&st, sizeof(st)); + stat(install_path, &st); + + // no need to run empty install script :) + if(st.st_size <= 0) + return true; + char *args[] = {"/bin/bash", install_path, NULL}, *oldpwd = NULL; bool ret = false; int status = 0; diff --git a/src/ctx/remove.c b/src/ctx/remove.c index fcc444a..bb80530 100644 --- a/src/ctx/remove.c +++ b/src/ctx/remove.c @@ -14,6 +14,11 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba return false; } + if(NULL == ctx->root){ + lm_error_set(LM_ERR_CtxRootNULL); + return false; + } + if(!lm_ctx_database_is_installed(ctx, pkg, true)){ lm_error_set(LM_ERR_PkgNotInstalled); return false; @@ -22,8 +27,8 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba if(!lm_ctx_database_init(ctx)) return false; + char *path = NULL, *hash = NULL, *fpath = NULL; size_t total = 0, current = 0; - char *path = NULL, *hash = NULL; bool in_keep = false, ret = false; total = lm_database_files_count(ctx->db, pkg); @@ -31,14 +36,26 @@ bool lm_ctx_remove(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_remove_callback_t callba while(lm_database_files_next(ctx->db, pkg, &path, &hash, &in_keep)){ if(in_keep) goto next; - - if(exists(path) && unlink(path) < 0){ - pdebug(__func__, "failed to delete file for removing %s: %s", pkg->name, errno); - lm_error_set(LM_ERR_PkgFileUnlinkFail, path, strerror(errno)); - goto end; + + fpath = join_alloc(ctx->root, path); + pdebug(__func__, "removing file %s (%s)", fpath, pkg->name); + + if(!exists(fpath)){ + pdebug(__func__, "found file in database, but its not on the file system: %s", fpath); + goto next; } + + if(unlink(fpath) < 0){ + pdebug(__func__, "failed to delete file for removing %s: %s", pkg->name, strerror(errno)); + lm_error_set(LM_ERR_PkgFileUnlinkFail, path, strerror(errno)); + goto next; + } + next: - if(!callback(ctx, pkg, path, ++current, total, data)) + free(fpath); + fpath = NULL; + + if(NULL != callback && !callback(ctx, pkg, fpath, ++current, total, data)) goto end; } diff --git a/src/database/files.c b/src/database/files.c index ea102ec..fb7498f 100644 --- a/src/database/files.c +++ b/src/database/files.c @@ -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, FILES_COLUMN_PACKAGE, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC); while(sqlite3_step(db->files_st) == SQLITE_ROW) count++; @@ -85,7 +85,7 @@ bool lm_database_files_matches(lm_database_t *db, char *path, char *hash){ goto end; } - sqlite3_bind_text(db->files_st, FILES_COLUMN_PATH, path, strlen(path), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, path, strlen(path), SQLITE_STATIC); if(sqlite3_step(db->files_st) != SQLITE_ROW){ pdebug(__func__, "failed to execute select statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db)); @@ -119,7 +119,7 @@ bool lm_database_files_iskeep(lm_database_t *db, char *path){ goto end; } - sqlite3_bind_text(db->files_st, FILES_COLUMN_PATH, path, strlen(path), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, path, strlen(path), SQLITE_STATIC); if(!lm_database_step_all(db->files_st)){ pdebug(__func__, "failed to execute select statement for selecting %s: %s", path, sqlite3_errmsg(db->files_db)); @@ -190,7 +190,7 @@ bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg){ goto end; } - sqlite3_bind_text(db->files_st, FILES_COLUMN_PACKAGE, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->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)); @@ -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, FILES_COLUMN_PACKAGE, pkg->name, strlen(pkg->name), SQLITE_STATIC); + sqlite3_bind_text(db->files_st, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC); } else if(NULL != db->files_st){