new: add remove operation

This commit is contained in:
ngn
2024-07-08 05:47:13 +03:00
parent 93eb7bb8b4
commit 1d5880cfa6
11 changed files with 486 additions and 81 deletions

73
src/ctx/remove.c Normal file
View File

@ -0,0 +1,73 @@
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
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){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(!lm_ctx_database_is_installed(ctx, pkg, true)){
lm_error_set(LM_ERR_PkgNotInstalled);
return false;
}
if(!lm_ctx_database_init(ctx))
return false;
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);
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;
}
next:
if(!callback(ctx, pkg, path, ++current, total, data))
goto end;
}
if(!lm_database_package_del(ctx->db, pkg)){
char *suberr = lm_strerror_dup();
pdebug(__func__, "failed to delete %s from the database: %s", pkg->name, lm_strerror());
lm_error_set(LM_ERR_PkgDatabaseDelFail, suberr);
free(suberr);
goto end;
}
if(!lm_database_files_del(ctx->db, pkg)){
char *suberr = lm_strerror_dup();
pdebug(__func__, "failed to delete files of %s from the database: %s", pkg->name, suberr);
lm_error_set(LM_ERR_PkgFilesDelFail, suberr);
free(suberr);
goto end;
}
if(!lm_database_changes_del(ctx->db, pkg)){
char *suberr = lm_strerror_dup();
pdebug(__func__, "failed to delete changes file for %s: %s", pkg->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);
return ret;
}