]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Mon, 6 Sep 2021 23:35:08 +0000 (16:35 -0700)
committeralex <[email protected]>
Mon, 6 Sep 2021 23:35:08 +0000 (16:35 -0700)
12 files changed:
Makefile.am
inc/file.h [new file with mode: 0644]
inc/log.h
inc/torrent.h
inc/tree.h [new file with mode: 0644]
src/file.c [new file with mode: 0644]
src/log.c
src/main.c
src/opt/watch.c
src/setup.c
src/torrent.c
src/tree.c [new file with mode: 0644]

index dd4c753b5d802f3975472dab62174a8117f8e284..1b20790dc6690a0683d774bec50aab09133add76 100644 (file)
@@ -13,6 +13,7 @@ bin_PROGRAMS = seederd
 seederd_SOURCES = \
        src/add.c \
        src/default.c \
+       src/file.c \
        src/log.c \
        src/main.c \
        src/opt/config.c \
@@ -24,6 +25,7 @@ seederd_SOURCES = \
        src/opt/worker.c \
        src/setup.c \
        src/torrent.c \
+       src/tree.c \
        src/usage.c \
        src/util/dir.c \
        src/util/file.c
@@ -31,10 +33,12 @@ seederd_SOURCES = \
 seederd_SOURCES += \
        inc/add.h \
        inc/default.h \
+       inc/file.h \
        inc/log.h \
        inc/main.h \
        inc/opt.h \
        inc/setup.h \
        inc/torrent.h \
+       inc/tree.h \
        inc/usage.h \
        inc/util.h
diff --git a/inc/file.h b/inc/file.h
new file mode 100644 (file)
index 0000000..d85ce26
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef __FILE_H_
+#define __FILE_H_
+
+struct file {
+       const char *name;
+};
+
+void file_free(struct file*);
+
+#endif
index 7bed5e4d528751ad2929702dede7966256e9f105..274d862533ab21aaef9b03f19b762d16813440c9 100644 (file)
--- a/inc/log.h
+++ b/inc/log.h
@@ -25,9 +25,9 @@ enum log_level {
 #define LOG_MESSAGE_PREFIX "%s "
 #endif
 
-#define log_err(...) log_message_with_prefix(LOG_LEVEL_ERRORS,stderr,LOG_MESSAGE_PREFIX __VA_ARGS__)
-#define log_info(...) log_message_with_prefix(LOG_LEVEL_VERBOSE,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__)
-#define log_msg(...) log_message_with_prefix(LOG_LEVEL_DEFAULT,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__)
+#define log_err(...) log_message(LOG_LEVEL_ERRORS,stderr,__VA_ARGS__)
+#define log_info(...) log_message(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__)
+#define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__)
 
 #define LOG_FLUSH_MESSAGE "flushing log queue...\n"
 #define LOG_THREAD_START_MESSAGE "logging thread start\n"
@@ -49,12 +49,9 @@ struct log_helper {
        size_t next;
 };
 
-struct log_entry *log_dequeue();
-void log_enqueue(struct log_entry*);
 int log_entries_init();
 void log_entries_clean();
 void log_flush();
-void log_print();
 void log_message_with_prefix(enum log_level,FILE*,const char*,...);
 void log_message(enum log_level,FILE*,const char*,...);
 void *log_poll(void*);
index 0e53ec1218c0fce65d48767eefe93bab2d2564e1..4e1e7c0739e29aabb367465d772a3fb0f9bba129 100644 (file)
@@ -5,8 +5,13 @@
 #include<stdlib.h>
 #include<string.h>
 
+#include<file.h>
+#include<tree.h>
+
 struct torrent {
        char *root;
+       char *name;
+       struct tree *file_tree;
 };
 
 extern struct torrent **torrents;
diff --git a/inc/tree.h b/inc/tree.h
new file mode 100644 (file)
index 0000000..6663628
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef __TREE_H_
+#define __TREE_H_
+
+struct tree {
+       struct file *files;
+       size_t file_count;
+       struct tree *directories;
+       size_t directory_count;
+};
+
+void tree_free(struct tree*);
+
+#endif
diff --git a/src/file.c b/src/file.c
new file mode 100644 (file)
index 0000000..b332714
--- /dev/null
@@ -0,0 +1,5 @@
+#include<file.h>
+
+void file_free(struct file *p) {
+       return;
+}
index 4406e239a473136b9dfb2fc616e449d4e332aac6..23c51c9159cbadbdf88b6ab5ef3294628c7a26ad 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2,7 +2,13 @@
 
 static struct log_helper helper;
 
-struct log_entry *log_dequeue() {
+// static functions
+static struct log_entry *log_dequeue();
+static void log_enqueue(struct log_entry*);
+static void log_print();
+static void log_print_prefix(FILE*);
+
+static struct log_entry *log_dequeue() {
        struct log_entry *p;
 
        pthread_mutex_lock(&logging_mutex);
@@ -19,7 +25,7 @@ struct log_entry *log_dequeue() {
        return p;
 }
 
-void log_enqueue(struct log_entry *p) {
+static void log_enqueue(struct log_entry *p) {
        pthread_mutex_lock(&logging_mutex);
 
        if(NULL==helper.start) {
@@ -70,38 +76,34 @@ void log_flush() {
 }
 
 void log_print() {
-       struct log_entry *p = helper.start;
+       struct log_entry *p = log_dequeue();
        if(NULL!=p) {
+               log_print_prefix(p->out_stream);
                fputs(p->buf,p->out_stream);
                helper.start = p->next;
        }
 }
 
-void log_message_with_prefix(enum log_level level, FILE *out_stream, const char *format,...) {
+static void log_print_prefix(FILE *out_stream) {
+       char buf[TIMESTAMP_BUF_LENGTH];
        time_t t;
        
-       if(level>global_options.verbose_flag) { return; }
-
        t = time(NULL);
        struct tm now = *localtime(&t);
-       char buf[TIMESTAMP_BUF_LENGTH];
-       strftime(buf,TIMESTAMP_BUF_LENGTH,"%Y-%m-%d-%H:%M:%S",&now);
-
-       va_list args;
-       va_start(args,format);
+       strftime(buf,TIMESTAMP_BUF_LENGTH,"%Y-%m-%d_%H:%M:%S",&now);
 
 #ifndef NDEBUG
-       log_message(level,out_stream,format,pthread_self(),buf,args);
+       fprintf(out_stream,LOG_MESSAGE_PREFIX,pthread_self(),buf);
 #else
-       log_message(level,out_stream,format,buf,args);
+       fprintf(out_stream,LOG_MESSAGE_PREFIX,buf);
 #endif
-
-       va_end(args);
 }
 
-void log_message(enum log_level level, FILE *out_stream, const char *format, va_list args,...) {
-       //va_list args;
-       //va_start(args,format);
+void log_message(enum log_level level, FILE *out_stream, const char *format,...) {
+       if(level>global_options.verbose_flag) { return; }
+       
+       va_list args;
+       va_start(args,format);
        if(0==pthread_equal(pthread_self(),logging_thread)) {
                // not on logging_thread
 
@@ -132,6 +134,7 @@ void log_message(enum log_level level, FILE *out_stream, const char *format, va_
 
                log_enqueue(p);
        } else {
+               log_print_prefix(out_stream);
                vfprintf(out_stream,format,args);
        }
 
index 359bb3f152c13f09c16c5e7678b48668087d20e4..b4d5d779823608c5f60342690009e1576c5f8106 100644 (file)
@@ -53,7 +53,7 @@ int main(int argc, char **argv) {
 
        if(setup()<0) { return EXIT_FAILURE; }
 
-       log_err(LOG_MESSAGE_PREFIX "this is a test %d %s\n",10,"what?");
+       log_err("this is a test %d %s\n",10,"what?");
        while(1) { }
 
        return EXIT_FAILURE;
index 320f9371c0c2b24815c046979f0548a05c2c8449..c1a7736fc45e7c8e1d6b1b5285b9595bbb2caace 100644 (file)
@@ -2,6 +2,7 @@
 
 int opt_add_watch(char *directory) {
        struct torrent *p;
+
        if(!is_directory(directory)) {
                log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory);
                return -1;
index f7621e126421dd8fec7f4b6e1844b1e8c1d704aa..8c9f010227abda0298a3d8e0dba17d7914ee1855 100644 (file)
@@ -1,6 +1,5 @@
 #include<setup.h>
 
-
 int setup() {
        if(setup_logging()<0) { return -1; }
        if(setup_adding()<0) { return -1; }
@@ -8,6 +7,7 @@ int setup() {
 }
 
 pthread_t adding_thread;
+pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 int setup_adding() {
        if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) {
index a64227224f1208b31edd7a0e259f019060e1d961..5611595e88a5cc48c13b67d6c054440299373d1f 100644 (file)
@@ -3,24 +3,40 @@
 struct torrent **torrents;
 
 void torrent_free(struct torrent *p) {
-       free(p->root);
+       if(p->root!=NULL) { free(p->root); }
+       if(p->name!=NULL) { free(p->name); }
+       if(p->file_tree!=NULL) { tree_free(p->file_tree); }
        free(p);
 }
 
-int torrent_init(struct torrent **p, char *root) {
-       *p = malloc(sizeof(struct torrent));
-       if(NULL==(*p)) {
+int torrent_init(struct torrent **torrent_p, char *root, char *name) {
+       char *p;
+
+       *(torrent_p) = malloc(sizeof(struct torrent));
+       if(NULL==(*torrent_p)) {
+               perror("malloc");
+               return -1;
+       }
+
+       (*torrent_p)->root = NULL;
+       (*torrent_p)->name = NULL;
+       (*torrent_p)->file_tree = NULL;
+
+       (*torrent_p)->root = malloc(strlen(root)+1);
+       if(NULL==(*torrent_p)->root) {
                perror("malloc");
+               torrent_free(torrent_p);
                return -1;
        }
+       strcpy((*torrent_p)->root,root);
 
-       (*p)->root = malloc(strlen(root)+1);
-       if(NULL==(*p)->root) {
+       (*torrent_p)->name = malloc(strlen(name)+1);
+       if(NULL==(*torrent_p)->name) {
                perror("malloc");
-               free(*p);
+               torrent_free(torrent_p);
                return -1;
        }
-       strcpy((*p)->root,root);
+       strcpy((*torrent_p)->name,name);
 
        return 1;
 }
diff --git a/src/tree.c b/src/tree.c
new file mode 100644 (file)
index 0000000..6ac3b7f
--- /dev/null
@@ -0,0 +1,15 @@
+#include<tree.h>
+
+void tree_free(struct tree *p) {
+       while(p->file_count>0) {
+               file_free(p->files[p->file_count-1]);
+               p->file_count--;
+       }
+       
+       while(p->directory_count>0) {
+               tree_free(p->directories[p->directory_count-1]);
+               p->directory_count--;
+       }
+
+       free(p);
+}