new: implement database keeps and files functions
This commit is contained in:
177
src/database/files.c
Normal file
177
src/database/files.c
Normal file
@ -0,0 +1,177 @@
|
||||
#include "../../include/database.h"
|
||||
#include "../../include/package.h"
|
||||
#include "../../include/error.h"
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_files_eachfunc_t func, void *data){
|
||||
if(NULL == db || NULL == pkg || NULL == func){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
|
||||
if(!exists(files_list)){
|
||||
lm_error_set(LM_ERR_DbFilesNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "r");
|
||||
char *line = NULL, hash[HASH_LEN+1];
|
||||
size_t line_len = 0;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, files) > 0){
|
||||
line_len = strlen(line);
|
||||
char path[line_len+1];
|
||||
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
memcpy(path, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
|
||||
if(!func(pkg, path, hash, data))
|
||||
goto end;
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != files)
|
||||
fclose(files);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){
|
||||
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char pkg_dir[strlen(db->dir)+strlen(pkg->name)+5];
|
||||
char files_list[sizeof(pkg_dir)+15];
|
||||
bool ret = false;
|
||||
|
||||
join(pkg_dir, db->dir, pkg->name);
|
||||
join(files_list, pkg_dir, "files");
|
||||
|
||||
if(!mkdir_ifnot(pkg_dir)){
|
||||
lm_error_set(LM_ERR_DbFilesDirFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "a");
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(fprintf(files, "%s:%s\n", hash, path) < 0){
|
||||
pdebug(__func__, "failed to append file to files list: %s", files_list);
|
||||
lm_error_set(LM_ERR_DbFilesWriteFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
if(NULL != files)
|
||||
fclose(files);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash){
|
||||
if(NULL == db || NULL == pkg || NULL == path || NULL == hash){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
// zero out the hash
|
||||
bzero(hash, HASH_LEN+1);
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+10];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
|
||||
if(!exists(files_list)){
|
||||
lm_error_set(LM_ERR_DbFilesNotFound);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "r");
|
||||
char *line = NULL;
|
||||
size_t line_len = 0;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == files){
|
||||
lm_error_set(LM_ERR_DbFilesOpenFail);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, files) > 0){
|
||||
line_len = strlen(line);
|
||||
char lpath[line_len+1];
|
||||
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
memcpy(lpath, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
|
||||
if(eq(lpath, path)){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
ret = true;
|
||||
goto end;
|
||||
}
|
||||
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
free(line);
|
||||
|
||||
if(NULL != files)
|
||||
fclose(files);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+10];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
|
||||
if(unlink(files_list) < 0 && errno != ENOENT){
|
||||
pdebug(__func__, "failed to delete file list for %s: %s", pkg->name, files_list);
|
||||
lm_error_set(LM_ERR_DbFilesUnlinkFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user