80 lines
1.9 KiB
C
80 lines
1.9 KiB
C
#include "../../include/database.h"
|
|
#include "../../include/error.h"
|
|
#include "../../include/util.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
size_t __lm_database_changes_get(lm_database_t *db, lm_entry_t *entry, char *path){
|
|
size_t changes_size = strlen(entry->name)+15;
|
|
size_t full_size = changes_size + strlen(db->dir);
|
|
|
|
if(NULL != path){
|
|
char changes_file[changes_size];
|
|
snprintf(changes_file, changes_size, "%s_changes", entry->name);
|
|
join(path, db->dir, changes_file);
|
|
}
|
|
|
|
return full_size;
|
|
}
|
|
|
|
#define __lm_changes_size() __lm_database_changes_get(db, entry, NULL)
|
|
#define __lm_changes_path(p) __lm_database_changes_get(db, entry, p)
|
|
|
|
bool lm_database_changes_update(lm_database_t *db, lm_entry_t *entry, char *file){
|
|
if(NULL == db || NULL == entry || NULL == file){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char changes_path[__lm_changes_size()];
|
|
__lm_changes_path(changes_path);
|
|
|
|
if(!copy_file(changes_path, file))
|
|
return false;
|
|
|
|
if(chmod(changes_path, 0444) < 0){
|
|
lm_error_set(LM_ERR_DbChangesChmodFail);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool lm_database_changes_del(lm_database_t *db, lm_entry_t *entry){
|
|
if(NULL == db || NULL == entry){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char changes_path[__lm_changes_size()];
|
|
__lm_changes_path(changes_path);
|
|
|
|
if(unlink(changes_path) < 0 && errno != ENOENT){
|
|
lm_error_set(LM_ERR_DbChangesUnlinkFail);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
char *lm_database_changes_get(lm_database_t *db, lm_entry_t *entry){
|
|
if(NULL == db || NULL == entry){
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
return false;
|
|
}
|
|
|
|
char* changes_path = malloc(__lm_changes_size());
|
|
__lm_changes_path(changes_path);
|
|
|
|
if(!exists(changes_path, NULL)){
|
|
lm_error_set(LM_ERR_DbChangesNotExists);
|
|
return NULL;
|
|
}
|
|
|
|
return changes_path;
|
|
}
|