new: implement check and update functions

This commit is contained in:
ngn
2024-07-11 07:31:36 +03:00
parent f886bc08e4
commit 839bcb47bf
20 changed files with 456 additions and 52 deletions

65
src/ctx/check.c Normal file
View File

@ -0,0 +1,65 @@
#include "../../include/error.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == ctx->root){
lm_error_set(LM_ERR_CtxRootNULL);
return false;
}
if(!lm_ctx_database_init(ctx))
return false; // error set by function
size_t rootlen = strlen(ctx->root), current = 0, total = 0;
char *path = NULL, *hash = NULL, *fhash = NULL;
bool keep = false, ret = false;
total = lm_database_files_count(ctx->db, pkg);
while(lm_database_files_next(ctx->db, pkg, &path, &hash, &keep)){
char fp[rootlen+strlen(path)+10];
join(fp, ctx->root, path);
current++;
if(NULL != callback)
if(!callback(ctx, pkg, fp, current, total, data))
goto end;
if(!exists(fp)){
pdebug(__func__, "skipping %s because file does not exists", fp);
continue;
}
if((fhash = get_md5(fp)) == NULL){
char *suberr = lm_strerror_dup();
pdebug(__func__, "failed to get hash of %s: %s", fp, suberr);
lm_error_set(LM_ERR_FileHashFail, fp, suberr);
free(suberr);
goto end;
}
if(!eq(fhash, hash)){
lm_error_set(LM_ERR_FileHashNoMatch, fp);
goto end;
}
free(fhash);
fhash = NULL;
}
ret = true;
end:
lm_database_files_next_free(ctx->db, pkg, &path, &hash, &keep);
free(fhash);
return ret;
}