154 lines
3.0 KiB
C
154 lines
3.0 KiB
C
#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_keeps_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_keeps_eachfunc_t func, void *data){
|
|
if(NULL == db || NULL == pkg || NULL == func){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
|
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
|
|
|
if(!exists(keeps_list)){
|
|
lm_error_set(LM_ERR_DbKeepsNotFound);
|
|
return false;
|
|
}
|
|
|
|
FILE *keeps = fopen(keeps_list, "r");
|
|
char *line = NULL;
|
|
bool ret = false;
|
|
|
|
if(NULL == keeps){
|
|
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
|
goto end;
|
|
}
|
|
|
|
while(getline(&line, 0, keeps) > 0){
|
|
if(line[0] == 0)
|
|
continue;
|
|
|
|
if(!func(pkg, line, data))
|
|
goto end;
|
|
|
|
free(line);
|
|
line = NULL;
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
free(line);
|
|
|
|
if(NULL != keeps)
|
|
fclose(keeps);
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool lm_database_keeps_load(lm_database_t *db, lm_pkg_t *pkg){
|
|
if(NULL == db || NULL == pkg){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
|
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
|
|
|
if(!exists(keeps_list)){
|
|
lm_error_set(LM_ERR_DbKeepsNotFound);
|
|
return false;
|
|
}
|
|
|
|
FILE *keeps = fopen(keeps_list, "r");
|
|
char *line = NULL;
|
|
bool ret = false;
|
|
|
|
if(NULL == keeps){
|
|
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
|
goto end;
|
|
}
|
|
|
|
while(getline(&line, 0, keeps) > 0){
|
|
if(line[0] == 0)
|
|
continue;
|
|
|
|
if(!lm_package_keep_add(pkg, line))
|
|
goto end;
|
|
|
|
free(line);
|
|
line = NULL;
|
|
}
|
|
|
|
ret = true;
|
|
end:
|
|
free(line);
|
|
|
|
if(NULL != keeps)
|
|
fclose(keeps);
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool lm_database_keeps_save(lm_database_t *db, lm_pkg_t *pkg){
|
|
if(NULL == db || NULL == pkg){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char pkg_dir[strlen(db->dir)+strlen(pkg->name)+5];
|
|
char keeps_list[sizeof(pkg_dir)+15];
|
|
bool ret = false;
|
|
|
|
join(pkg_dir, db->dir, pkg->name);
|
|
join(keeps_list, pkg_dir, "keeps");
|
|
|
|
if(!mkdir_ifnot(pkg_dir)){
|
|
lm_error_set(LM_ERR_DbKeepsDirFail);
|
|
return false;
|
|
}
|
|
|
|
FILE *keeps = fopen(keeps_list, "a");
|
|
|
|
if(NULL == keeps){
|
|
lm_error_set(LM_ERR_DbKeepsOpenFail);
|
|
goto end;
|
|
}
|
|
|
|
for(int i = 0; NULL != pkg->keeps[i]; i++)
|
|
fprintf(keeps, "%s\n", pkg->keeps[i]);
|
|
|
|
ret = true;
|
|
end:
|
|
if(NULL != keeps)
|
|
fclose(keeps);
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool lm_database_keeps_del(lm_database_t *db, lm_pkg_t *pkg){
|
|
if(NULL == db || NULL == pkg){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char keeps_list[strlen(db->dir)+strlen(pkg->name)+20];
|
|
join_multiple(keeps_list, db->dir, pkg->name, "keeps");
|
|
|
|
if(unlink(keeps_list) < 0 && errno != ENOENT){
|
|
pdebug(__func__, "failed to delete keep list for %s: %s", pkg->name, keeps_list);
|
|
lm_error_set(LM_ERR_DbKeepsUnlinkFail);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|