update: better handling of the keep files
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
bool lm_ctx_check(lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t callback, void *data){
|
||||
if(NULL == ctx || NULL == entry){
|
||||
@ -22,24 +23,37 @@ bool lm_ctx_check(lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t call
|
||||
|
||||
size_t rootlen = strlen(ctx->root), current = 0, total = 0;
|
||||
char *path = NULL, *hash = NULL, *fhash = NULL;
|
||||
bool keep = false, ret = false;
|
||||
bool keep = false, ret = false, success = false;
|
||||
struct stat st;
|
||||
|
||||
total = lm_database_files_count(ctx->db, entry);
|
||||
|
||||
while(lm_database_files_next(ctx->db, entry, &path, &hash, &keep)){
|
||||
char fp[rootlen+strlen(path)+10];
|
||||
join(fp, ctx->root, path);
|
||||
bzero(&st, sizeof(st));
|
||||
|
||||
success = false;
|
||||
current++;
|
||||
|
||||
pdebug(__func__, "(%d/%d) checking %s", current, total, fp);
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(ctx, entry, fp, current, total, data))
|
||||
goto end;
|
||||
if(!exists(fp, &st)){
|
||||
pdebug(__func__, "%s does not exist, check will fail", fp);
|
||||
lm_error_set(LM_ERR_FileNotExist, fp);
|
||||
goto next;
|
||||
}
|
||||
|
||||
if(!exists(fp)){
|
||||
pdebug(__func__, "skipping %s because file does not exists", fp);
|
||||
continue;
|
||||
if(S_ISLNK(st.st_mode)){
|
||||
pdebug(__func__, "%s seems to be a link, no hash verification to do", fp);
|
||||
|
||||
if(hash[0] != '\0'){
|
||||
lm_error_set(LM_ERR_FileNotLink, fp);
|
||||
goto end;
|
||||
}
|
||||
|
||||
success = true;
|
||||
goto next;
|
||||
}
|
||||
|
||||
if((fhash = get_md5(fp)) == NULL){
|
||||
@ -47,16 +61,25 @@ bool lm_ctx_check(lm_ctx_t *ctx, lm_entry_t *entry, lm_ctx_check_callback_t call
|
||||
pdebug(__func__, "failed to get hash of %s: %s", fp, suberr);
|
||||
lm_error_set(LM_ERR_FileHashFail, fp, suberr);
|
||||
free(suberr);
|
||||
goto end;
|
||||
goto next;
|
||||
}
|
||||
|
||||
if(!eq(fhash, hash)){
|
||||
lm_error_set(LM_ERR_FileHashNoMatch, fp);
|
||||
goto end;
|
||||
goto next;
|
||||
}
|
||||
|
||||
success = true;
|
||||
next:
|
||||
free(fhash);
|
||||
fhash = NULL;
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(ctx, entry, fp, success, current, total, data))
|
||||
goto end;
|
||||
|
||||
if(!success)
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
|
Reference in New Issue
Block a user