21 #include "fileutils.h"
52 #define MAIN_ERR_PREFIX "DATA: "
55 #define DB_PERM (api_posix_mode_t) (API_POSIX_S_IRUSR | API_POSIX_S_IWUSR)
61 static api_posix_pthread_mutex_t db_mutex = API_POSIX_PTHREAD_MUTEX_INITIALIZER;
62 static int db_mutex_state = 0;
63 static const char* db_path = NULL;
64 static size_t db_path_len = 0;
78 static int db_mutex_lock(
void)
86 rv = api_posix_pthread_setcancelstate(API_POSIX_PTHREAD_CANCEL_DISABLE,
88 if(rv) {
PRINT_ERROR(
"Setting thread cancelability state failed"); }
91 rv = api_posix_pthread_mutex_lock(&db_mutex);
93 else { db_mutex_state = 1; res = 0; }
94 rv = api_posix_pthread_setcancelstate(cs, &cs);
97 PRINT_ERROR(
"Restoring thread cancelability state failed");
117 static int db_mutex_unlock(
void)
125 rv = api_posix_pthread_setcancelstate(API_POSIX_PTHREAD_CANCEL_DISABLE,
127 if(rv) {
PRINT_ERROR(
"Setting thread cancelability state failed"); }
130 rv = api_posix_pthread_mutex_unlock(&db_mutex);
132 else { db_mutex_state = 0; res = 0; }
133 rv = api_posix_pthread_setcancelstate(cs, &cs);
136 PRINT_ERROR(
"Restoring thread cancelability state failed");
152 static int db_compar_dummy(
const api_posix_struct_dirent** a,
153 const api_posix_struct_dirent** b)
175 static int db_compar_num(
const api_posix_struct_dirent** a,
176 const api_posix_struct_dirent** b)
179 const char* name_a = (*a)->d_name;
180 const char* name_b = (*b)->d_name;
187 if(name_b[i]) { res = -1; }
192 if(name_a[i]) { res = 1; }
197 if(name_a[i] != name_b[i])
199 if(name_a[i] < name_b[i]) { res = -1; }
else { res = 1; }
217 static int db_get_path(
const char** dbpath)
219 static const char dbdir[] =
"headers/";
234 res =
fu_create_path(*dbpath, (api_posix_mode_t) API_POSIX_S_IRWXU);
242 api_posix_free((
void*) *dbpath);
253 static int db_init_unlocked(
void)
260 res = db_get_path(&db_path);
261 if(!res) { db_path_len = strlen(db_path); }
277 static int db_exit_unlocked(
void)
281 api_posix_free((
void*) db_path);
303 rv = db_mutex_lock();
306 res = db_init_unlocked();
327 rv = db_mutex_lock();
330 res = db_exit_unlocked();
351 rv = db_mutex_lock();
354 if(NULL == db_path) {
PRINT_ERROR(
"Database not initialized"); }
385 api_posix_struct_dirent** content;
392 rv = db_mutex_lock();
395 if(NULL == db_path) {
PRINT_ERROR(
"Database not initialized"); }
399 num = api_posix_scandir(db_path, &content, NULL, db_compar_dummy);
402 for(i = 0; i < (size_t) num; ++i)
404 entry = content[i]->d_name;
406 if(!strcmp(
".", entry)) {
continue; }
407 if(!strcmp(
"..", entry)) {
continue; }
410 for(ii = 0; ii < groupcount; ++ii)
412 if(!strcmp(grouplist[ii], entry))
421 path = (
char*) api_posix_malloc(strlen(db_path)
431 strcpy(path, db_path);
434 api_posix_free((
void*) path);
440 while(num--) { api_posix_free((
void*) content[num]); }
441 api_posix_free((
void*) content);
465 const char* header,
size_t len)
467 static char tmpfile[] =
".tmp";
468 static size_t tmpfile_len =
sizeof(tmpfile) - (
size_t) 1;
472 char* tmppathname = NULL;
473 char* pathname = NULL;
484 if(NULL == group || !anum || NULL == header)
486 PRINT_ERROR(
"db_add() called with invalid parameters");
490 rv = db_mutex_lock();
498 tmppathname = (
char*) api_posix_malloc(db_path_len + strlen(group)
499 + tmpfile_len + (size_t) 2);
500 pathname = (
char*) api_posix_malloc(db_path_len + strlen(group)
501 + file_len + (size_t) 2);
502 if (NULL == tmppathname || NULL == pathname)
504 PRINT_ERROR(
"Cannot allocate memory for database pathname");
509 strcpy(tmppathname, db_path);
510 strcat(tmppathname, group);
511 strcat(tmppathname,
"/");
512 rv = api_posix_mkdir(tmppathname,
513 (api_posix_mode_t) API_POSIX_S_IRWXU);
514 if (!rv || (-1 == rv && API_POSIX_EEXIST == api_posix_errno))
516 strcat(tmppathname, tmpfile);
519 API_POSIX_O_WRONLY | API_POSIX_O_CREAT
534 strcpy(pathname, db_path);
535 strcat(pathname, group);
536 strcat(pathname,
"/");
537 strcat(pathname, file);
538 rv = api_posix_rename(tmppathname, pathname);
545 if(NULL != tmppathname)
555 else {
PRINT_ERROR(
"Cannot create group directory"); }
558 api_posix_free((
void*) pathname);
559 api_posix_free((
void*) tmppathname);
590 char* pathname = NULL;
600 if(NULL == group || !anum || NULL == header)
602 PRINT_ERROR(
"db_read() called with invalid parameters");
606 rv = db_mutex_lock();
614 pathname = (
char*) api_posix_malloc(db_path_len + strlen(group)
615 + file_len + (size_t) 2);
616 if (NULL == pathname)
618 PRINT_ERROR(
"Cannot allocate memory for database pathname");
622 strcpy(pathname, db_path);
623 strcat(pathname, group);
624 strcat(pathname,
"/");
625 strcat(pathname, file);
639 api_posix_free((
void*) pathname);
671 char* pathname = NULL;
674 api_posix_struct_dirent** content;
687 if(NULL == group || end < start)
689 PRINT_ERROR(
"db_delete() called with invalid parameters");
693 rv = db_mutex_lock();
698 pathname = (
char*) api_posix_malloc(db_path_len + strlen(group)
699 + (size_t) 17 + (
size_t) 2);
700 if (NULL == pathname)
702 PRINT_ERROR(
"Cannot allocate memory for database pathname");
706 strcpy(pathname, db_path);
707 strcat(pathname, group);
708 strcat(pathname,
"/");
709 path_len = strlen(pathname);
717 else if(!start || !end)
719 PRINT_ERROR(
"Invalid range specified for deletion");
724 num = api_posix_scandir(pathname, &content, NULL, db_compar_num);
728 for(ii = 0; ii < (size_t) num; ++ii)
730 entry = content[ii]->d_name;
732 if(!strcmp(
".", entry)) {
continue; }
733 if(!strcmp(
"..", entry)) {
continue; }
735 (
int) strlen(entry));
739 if(e > start) { i = e; }
749 pathname[path_len] = 0;
750 strncpy(&pathname[path_len], file, 17);
755 if(i == end) { res = 0; }
759 while(num--) { api_posix_free((
void*) content[num]); }
760 api_posix_free((
void*) content);
764 api_posix_free((
void*) pathname);