fix: remove operation file check fixes

This commit is contained in:
ngn
2024-07-08 13:08:45 +03:00
parent 216ca5c9dd
commit d0dce209fa
5 changed files with 40 additions and 13 deletions

View File

@ -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;
}