summaryrefslogtreecommitdiff
path: root/src/file_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/file_list.c')
-rw-r--r--src/file_list.c34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/file_list.c b/src/file_list.c
index 823c041..c31ecef 100644
--- a/src/file_list.c
+++ b/src/file_list.c
@@ -35,6 +35,8 @@
#include <fcntl.h>
#include <errno.h>
+#include <glib.h>
+
#include "datatypes.h"
#include "file_list.h"
#include "slist.h"
@@ -43,23 +45,30 @@
static void delete_file(void* element)
{
file_t* deletee = (file_t*)element;
+ log_printf(INFO, "removing/closing file '%s'", deletee->path_);
if(deletee->path_) free(deletee->path_);
if(deletee->fd_ >= 0) close(deletee->fd_);
}
int file_list_init(file_list_t* list)
{
- return slist_init(list, &delete_file);
+ list->mutex_ = g_mutex_new();
+ if(!list->mutex_)
+ return -2;
+
+ return slist_init(&(list->list_), &delete_file);
}
void file_list_clear(file_list_t* list)
{
- slist_clear(list);
+ g_mutex_lock(list->mutex_);
+ slist_clear(&(list->list_));
+ g_mutex_unlock(list->mutex_);
}
file_t* file_list_add(file_list_t* list, struct tm* time, const char* type, const char* format, const char* dir, mode_t mode)
{
- if(!list)
+ if(!list || !(list->mutex_))
return NULL;
file_t* tmp = malloc(sizeof(file_t));
@@ -80,33 +89,40 @@ file_t* file_list_add(file_list_t* list, struct tm* time, const char* type, cons
tmp->fd_ = -1;
tmp->mode_ = mode;
- if(slist_add(list, tmp) == NULL) {
+ g_mutex_lock(list->mutex_);
+ if(slist_add(&(list->list_), tmp) == NULL) {
+ g_mutex_unlock(list->mutex_);
free(tmp->path_);
free(tmp);
return NULL;
}
+ g_mutex_unlock(list->mutex_);
return tmp;
}
int file_list_remove(file_list_t* list, int fd)
{
- if(!list)
+ if(!list || !(list->mutex_))
return -1;
- slist_element_t* tmp = list->first_;
+ g_mutex_lock(list->mutex_);
+ slist_element_t* tmp = list->list_.first_;
while(tmp) {
if(((file_t*)tmp->data_)->fd_ == fd) {
- slist_remove(list, tmp);
+ slist_remove(&(list->list_), tmp->data_);
break;
}
tmp = tmp->next_;
}
+ g_mutex_unlock(list->mutex_);
+
+ return 0;
}
int open_file(file_t* file)
{
- if(file->fd_ > 0) // file already open!
+ if(!file || file->fd_ > 0) // file already open!
return -1;
char* orig_path = file->path_;
@@ -141,5 +157,7 @@ int open_file(file_t* file)
if(orig_path != file->path_)
free(orig_path);
+ log_printf(INFO, "opened file '%s' -> %d", file->path_, file->fd_);
+
return 0;
}