From: alex Date: Mon, 6 Sep 2021 23:35:08 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=1deb3ee9438ce7cf8a40fc1bd913f01598fa4e22;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index dd4c753..1b20790 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 0000000..d85ce26 --- /dev/null +++ b/inc/file.h @@ -0,0 +1,10 @@ +#ifndef __FILE_H_ +#define __FILE_H_ + +struct file { + const char *name; +}; + +void file_free(struct file*); + +#endif diff --git a/inc/log.h b/inc/log.h index 7bed5e4..274d862 100644 --- 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*); diff --git a/inc/torrent.h b/inc/torrent.h index 0e53ec1..4e1e7c0 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -5,8 +5,13 @@ #include #include +#include +#include + 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 index 0000000..6663628 --- /dev/null +++ b/inc/tree.h @@ -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 index 0000000..b332714 --- /dev/null +++ b/src/file.c @@ -0,0 +1,5 @@ +#include + +void file_free(struct file *p) { + return; +} diff --git a/src/log.c b/src/log.c index 4406e23..23c51c9 100644 --- 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); } diff --git a/src/main.c b/src/main.c index 359bb3f..b4d5d77 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/opt/watch.c b/src/opt/watch.c index 320f937..c1a7736 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -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; diff --git a/src/setup.c b/src/setup.c index f7621e1..8c9f010 100644 --- a/src/setup.c +++ b/src/setup.c @@ -1,6 +1,5 @@ #include - 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) { diff --git a/src/torrent.c b/src/torrent.c index a642272..5611595 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -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 index 0000000..6ac3b7f --- /dev/null +++ b/src/tree.c @@ -0,0 +1,15 @@ +#include + +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); +}