update: redesigning ctx functions and structure
This commit is contained in:
52
src/database/changes.c
Normal file
52
src/database/changes.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include "../../include/database.h"
|
||||
#include "../../include/error.h"
|
||||
#include "../../include/util.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool lm_database_changes_update(lm_database_t *db, lm_pkg_t *pkg, char *file){
|
||||
if(NULL == db || NULL == pkg || NULL == file){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
|
||||
if(!copy_file(changes_file, file))
|
||||
return false; // error set by function
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lm_database_changes_del(lm_database_t *db, lm_pkg_t *pkg){
|
||||
if(NULL == db || NULL == pkg){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
char changes_file[strlen(db->dir)+strlen(pkg->name)+20];
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
|
||||
if(unlink(changes_file) < 0 && errno != ENOENT){
|
||||
lm_error_set(LM_ERR_DbChangesUnlinkFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char *lm_database_changes_get(lm_database_t *db, lm_pkg_t *pkg){
|
||||
char *changes_file = malloc(strlen(db->dir)+strlen(pkg->name)+20);
|
||||
join_multiple(changes_file, db->dir, pkg->name, "changes");
|
||||
|
||||
if(!exists(changes_file)){
|
||||
free(changes_file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return changes_file;
|
||||
}
|
@ -34,13 +34,11 @@ bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_fil
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, files) > 0){
|
||||
line_len = strlen(line);
|
||||
char path[line_len+1];
|
||||
|
||||
while((line_len = getline(&line, 0, files)) > 0){
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
char path[line_len+1];
|
||||
memcpy(path, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
@ -102,13 +100,13 @@ end:
|
||||
}
|
||||
|
||||
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){
|
||||
if(NULL == db || NULL == pkg || NULL == path){
|
||||
lm_error_set(LM_ERR_ArgNULL);
|
||||
return false;
|
||||
}
|
||||
|
||||
// zero out the hash
|
||||
bzero(hash, HASH_LEN+1);
|
||||
if(NULL != hash)
|
||||
bzero(hash, HASH_LEN+1); // zero out the hash
|
||||
|
||||
char files_list[strlen(db->dir)+strlen(pkg->name)+10];
|
||||
join_multiple(files_list, db->dir, pkg->name, "files");
|
||||
@ -119,8 +117,8 @@ bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *h
|
||||
}
|
||||
|
||||
FILE *files = fopen(files_list, "r");
|
||||
char *line = NULL;
|
||||
size_t line_len = 0;
|
||||
char *line = NULL;
|
||||
bool ret = false;
|
||||
|
||||
if(NULL == files){
|
||||
@ -128,18 +126,18 @@ bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *h
|
||||
goto end;
|
||||
}
|
||||
|
||||
while(getline(&line, 0, files) > 0){
|
||||
line_len = strlen(line);
|
||||
char lpath[line_len+1];
|
||||
|
||||
while((line_len = getline(&line, 0, files)) > 0){
|
||||
if(HASH_LEN >= line_len)
|
||||
continue;
|
||||
|
||||
char lpath[line_len+1];
|
||||
memcpy(lpath, line+HASH_LEN+1, line_len-HASH_LEN);
|
||||
|
||||
if(eq(lpath, path)){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
if(NULL != hash){
|
||||
memcpy(hash, line, HASH_LEN+1);
|
||||
hash[HASH_LEN] = 0;
|
||||
}
|
||||
ret = true;
|
||||
goto end;
|
||||
}
|
||||
|
Reference in New Issue
Block a user