summaryrefslogtreecommitdiff
path: root/src/file_list.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2011-04-18 02:20:01 (GMT)
committerChristian Pointner <equinox@helsinki.at>2011-04-18 02:20:01 (GMT)
commit9a74b857f449d9a2ac8cca76872a0ad69aec40f4 (patch)
treeed2b9fe70764000542af036867f5fc3234968a63 /src/file_list.c
parentc83d06888770040c638f65bd4186dc79542ed7cf (diff)
moved file specific thins to file_list
Diffstat (limited to 'src/file_list.c')
-rw-r--r--src/file_list.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/file_list.c b/src/file_list.c
new file mode 100644
index 0000000..823c041
--- /dev/null
+++ b/src/file_list.c
@@ -0,0 +1,145 @@
+/*
+ * rharchive
+ *
+ * rharchive is a simple tcp connection proxy which combines the
+ * features of rinetd and 6tunnel. rharchive supports IPv4 and
+ * IPv6 and also supports connections from IPv6 to IPv4
+ * endpoints and vice versa.
+ *
+ *
+ * Copyright (C) 2010-2011 Christian Pointner <equinox@helsinki.at>
+ *
+ * This file is part of rharchive.
+ *
+ * rharchive is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * rharchive is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with rharchive. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "datatypes.h"
+#include "file_list.h"
+#include "slist.h"
+#include "log.h"
+
+static void delete_file(void* element)
+{
+ file_t* deletee = (file_t*)element;
+ 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);
+}
+
+void file_list_clear(file_list_t* list)
+{
+ slist_clear(list);
+}
+
+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)
+ return NULL;
+
+ file_t* tmp = malloc(sizeof(file_t));
+ if(!tmp)
+ return NULL;
+
+ log_printf(INFO, "%s time: %02d:%02d:%02d on %d.%d.%d", type, time->tm_hour, time->tm_min, time->tm_sec, time->tm_mday, time->tm_mon+1, time->tm_year+1900);
+
+ char name[256];
+ strftime(name, sizeof(name), format, time);
+ asprintf(&(tmp->path_), "%s/%s", dir, name);
+ if(!tmp->path_) {
+ free(tmp);
+ return NULL;
+ }
+
+ log_printf(INFO, "%s filename is: %s(.?)", type, tmp->path_);
+ tmp->fd_ = -1;
+ tmp->mode_ = mode;
+
+ if(slist_add(list, tmp) == NULL) {
+ free(tmp->path_);
+ free(tmp);
+ return NULL;
+ }
+
+ return tmp;
+}
+
+int file_list_remove(file_list_t* list, int fd)
+{
+ if(!list)
+ return -1;
+
+ slist_element_t* tmp = list->first_;
+ while(tmp) {
+ if(((file_t*)tmp->data_)->fd_ == fd) {
+ slist_remove(list, tmp);
+ break;
+ }
+ tmp = tmp->next_;
+ }
+}
+
+int open_file(file_t* file)
+{
+ if(file->fd_ > 0) // file already open!
+ return -1;
+
+ char* orig_path = file->path_;
+ int cnt = 0;
+ do {
+ file->fd_ = open(file->path_, O_WRONLY | O_CREAT | O_EXCL, file->mode_);
+ if(file->fd_ < 0) {
+ if(errno != EEXIST) {
+ // TODO: thread safe strerror
+ log_printf(ERROR, "can't open file '%s': %s", file->path_, strerror(errno));
+ if(orig_path != file->path_)
+ free(orig_path);
+ return -1;
+ }
+ cnt++;
+ char* tmp;
+ asprintf(&tmp, "%s.%d", orig_path, cnt);
+ if(!tmp) {
+ if(orig_path != file->path_)
+ free(orig_path);
+ return -2;
+ }
+
+ if(file->path_ != orig_path)
+ free(file->path_);
+ file->path_ = tmp;
+ }
+ fchmod(file->fd_, file->mode_);
+ }
+ while(file->fd_ < 0);
+
+ if(orig_path != file->path_)
+ free(orig_path);
+
+ return 0;
+}