update: refactoring and small fixes

This commit is contained in:
ngn
2024-07-31 21:16:19 +03:00
parent 3f794e1a90
commit 9cd4eb9905
41 changed files with 779 additions and 752 deletions

View File

@ -8,13 +8,13 @@
#include <stdlib.h>
#include <unistd.h>
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;
}