21 #include "fileutils.h"
52 #define MAIN_ERR_PREFIX "DATA: "
55 #define DB_PERM (posix_mode_t) (POSIX_S_IRUSR | POSIX_S_IWUSR)
61 static posix_pthread_mutex_t db_mutex = 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()
86 rv = posix_pthread_setcancelstate(POSIX_PTHREAD_CANCEL_DISABLE, &cs);
87 if(rv) {
PRINT_ERROR(
"Setting thread cancelability state failed"); }
90 rv = posix_pthread_mutex_lock(&db_mutex);
92 else { db_mutex_state = 1; res = 0; }
93 rv = posix_pthread_setcancelstate(cs, &cs);
96 PRINT_ERROR(
"Restoring thread cancelability state failed");
116 static int db_mutex_unlock()
124 rv = posix_pthread_setcancelstate(POSIX_PTHREAD_CANCEL_DISABLE, &cs);
125 if(rv) {
PRINT_ERROR(
"Setting thread cancelability state failed"); }
128 rv = posix_pthread_mutex_unlock(&db_mutex);
130 else { db_mutex_state = 0; res = 0; }
131 rv = posix_pthread_setcancelstate(cs, &cs);
134 PRINT_ERROR(
"Restoring thread cancelability state failed");
150 static int db_compar_dummy(
const struct_posix_dirent** a,
151 const struct_posix_dirent** b)
170 static int db_compar_num(
const struct_posix_dirent** a,
171 const struct_posix_dirent** b)
174 const char* name_a = (*a)->d_name;
175 const char* name_b = (*b)->d_name;
182 if(name_b[i]) { res = -1; }
187 if(name_a[i]) { res = 1; }
192 if(name_a[i] != name_b[i])
194 if(name_a[i] < name_b[i]) { res = -1; }
else { res = 1; }
212 static int db_get_path(
const char** dbpath)
214 static const char dbdir[] =
"headers/";
237 posix_free((
void*) *dbpath);
248 static int db_init_unlocked(
void)
255 res = db_get_path(&db_path);
256 if(!res) { db_path_len = strlen(db_path); }
272 static int db_exit_unlocked(
void)
276 posix_free((
void*) db_path);
298 rv = db_mutex_lock();
301 res = db_init_unlocked();
322 rv = db_mutex_lock();
325 res = db_exit_unlocked();
346 rv = db_mutex_lock();
349 if(NULL == db_path) {
PRINT_ERROR(
"Database not initialized"); }
380 struct_posix_dirent** content;
387 rv = db_mutex_lock();
390 if(NULL == db_path) {
PRINT_ERROR(
"Database not initialized"); }
394 num = posix_scandir(db_path, &content, NULL, db_compar_dummy);
397 for(i = 0; i < (size_t) num; ++i)
399 entry = content[i]->d_name;
401 if(!strcmp(
".", entry)) {
continue; }
402 if(!strcmp(
"..", entry)) {
continue; }
405 for(ii = 0; ii < groupcount; ++ii)
407 if(!strcmp(grouplist[ii], entry))
416 path = (
char*) posix_malloc(strlen(db_path) + strlen(entry)
425 strcpy(path, db_path);
428 posix_free((
void*) path);
434 while(num--) { posix_free((
void*) content[num]); }
435 posix_free((
void*) content);
459 const char* header,
size_t len)
461 static char tmpfile[] =
".tmp";
462 static size_t tmpfile_len =
sizeof(tmpfile) - (
size_t) 1;
466 char* tmppathname = NULL;
467 char* pathname = NULL;
478 if(NULL == group || !anum || NULL == header)
480 PRINT_ERROR(
"db_add() called with invalid parameters");
484 rv = db_mutex_lock();
492 tmppathname = (
char*) posix_malloc(db_path_len + strlen(group)
493 + tmpfile_len + (size_t) 2);
494 pathname = (
char*) posix_malloc(db_path_len + strlen(group)
495 + file_len + (size_t) 2);
496 if (NULL == tmppathname || NULL == pathname)
498 PRINT_ERROR(
"Cannot allocate memory for database pathname");
503 strcpy(tmppathname, db_path);
504 strcat(tmppathname, group);
505 strcat(tmppathname,
"/");
506 rv = posix_mkdir(tmppathname, (posix_mode_t) POSIX_S_IRWXU);
507 if (!rv || (-1 == rv && POSIX_EEXIST == posix_errno))
509 strcat(tmppathname, tmpfile);
512 POSIX_O_WRONLY | POSIX_O_CREAT | POSIX_O_TRUNC,
527 strcpy(pathname, db_path);
528 strcat(pathname, group);
529 strcat(pathname,
"/");
530 strcat(pathname, file);
531 rv = posix_rename(tmppathname, pathname);
538 if(NULL != tmppathname)
548 else {
PRINT_ERROR(
"Cannot create group directory"); }
551 posix_free((
void*) pathname);
552 posix_free((
void*) tmppathname);
583 char* pathname = NULL;
593 if(NULL == group || !anum || NULL == header)
595 PRINT_ERROR(
"db_read() called with invalid parameters");
599 rv = db_mutex_lock();
607 pathname = (
char*) posix_malloc(db_path_len + strlen(group)
608 + file_len + (size_t) 2);
609 if (NULL == pathname)
611 PRINT_ERROR(
"Cannot allocate memory for database pathname");
615 strcpy(pathname, db_path);
616 strcat(pathname, group);
617 strcat(pathname,
"/");
618 strcat(pathname, file);
632 posix_free((
void*) pathname);
664 char* pathname = NULL;
667 struct_posix_dirent** content;
680 if(NULL == group || end < start)
682 PRINT_ERROR(
"db_delete() called with invalid parameters");
686 rv = db_mutex_lock();
691 pathname = (
char*) posix_malloc(db_path_len + strlen(group)
692 + (size_t) 17 + (
size_t) 2);
693 if (NULL == pathname)
695 PRINT_ERROR(
"Cannot allocate memory for database pathname");
699 strcpy(pathname, db_path);
700 strcat(pathname, group);
701 strcat(pathname,
"/");
702 path_len = strlen(pathname);
710 else if(!start || !end)
712 PRINT_ERROR(
"Invalid range specified for deletion");
717 num = posix_scandir(pathname, &content, NULL, db_compar_num);
721 for(ii = 0; ii < (size_t) num; ++ii)
723 entry = content[ii]->d_name;
725 if(!strcmp(
".", entry)) {
continue; }
726 if(!strcmp(
"..", entry)) {
continue; }
728 (
int) strlen(entry));
732 if(e > start) { i = e; }
742 pathname[path_len] = 0;
743 strncpy(&pathname[path_len], file, 17);
748 if(i == end) { res = 0; }
752 while(num--) { posix_free((
void*) content[num]); }
753 posix_free((
void*) content);
757 posix_free((
void*) pathname);