From: alex Date: Tue, 7 Sep 2021 04:17:39 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=359fb0a67324d7a95b39d95196b0a2b661aa95b7;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 1b20790..0d16745 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,6 +23,7 @@ seederd_SOURCES = \ src/opt/set.c \ src/opt/watch.c \ src/opt/worker.c \ + src/session.c \ src/setup.c \ src/torrent.c \ src/tree.c \ @@ -37,6 +38,7 @@ seederd_SOURCES += \ inc/log.h \ inc/main.h \ inc/opt.h \ + inc/session.h \ inc/setup.h \ inc/torrent.h \ inc/tree.h \ diff --git a/inc/opt.h b/inc/opt.h index 4a43e3b..f185b16 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -1,9 +1,11 @@ #ifndef __OPT_H_ #define __OPT_H_ +#include #include #include +#include #include #include @@ -23,7 +25,7 @@ extern struct options global_options; #define OPT_MESSAGE_PIECE_LENGTH_SET "piece length set to %llu\n" #define OPT_MESSAGE_UNABLE_OPEN_FILE "unable to open file %s\n" #define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n" -#define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %s\n" +#define OPT_MESSAGE_WATCH_ADD_SUCCESS "adding all files in %s to torrent with name %s\n" #define OPT_MESSAGE_WATCH_INVALID_DIRECTORY "watching directory %s failed\n" #define OPT_MESSAGE_WORKER_THREADS_INVALID "invalid value to option worker_threads: %u\n" #define OPT_MESSAGE_WORKER_THREADS_SET "number of worker threads set to %u\n" diff --git a/inc/session.h b/inc/session.h new file mode 100644 index 0000000..73d5293 --- /dev/null +++ b/inc/session.h @@ -0,0 +1,22 @@ +#ifndef __SESSION_H_ +#define __SESSION_H_ + +#include +#include + +#define SESSION_MESSAGE_REALLOC_COUNT "(re)allocating memory for session torrents to %u\n" + +#define SESSION_REALLOC_COUNT 100 + +struct session { + struct torrent **torrents; + size_t torrent_count; +}; + +extern struct session session; + +void session_clean(); +int session_init(); +int session_torrent_add(struct torrent*); + +#endif diff --git a/inc/setup.h b/inc/setup.h index 8d5d526..a255aaf 100644 --- a/inc/setup.h +++ b/inc/setup.h @@ -1,11 +1,10 @@ #ifndef __SETUP_H_ #define __SETUP_H_ -#include #include +#include int setup(); -int setup_adding(); -int setup_logging(); +int setup_session(); #endif diff --git a/inc/torrent.h b/inc/torrent.h index 4e1e7c0..19963b0 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -17,6 +17,6 @@ struct torrent { extern struct torrent **torrents; void torrent_free(struct torrent*); -int torrent_init(struct torrent**,char*); +int torrent_init(struct torrent**,char*,char*); #endif diff --git a/inc/tree.h b/inc/tree.h index 6663628..3ca98d0 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -1,13 +1,18 @@ #ifndef __TREE_H_ #define __TREE_H_ +#include + +#include + struct tree { - struct file *files; + struct file **files; size_t file_count; - struct tree *directories; + struct tree **directories; size_t directory_count; }; void tree_free(struct tree*); +int tree_init(struct tree**); #endif diff --git a/src/add.c b/src/add.c index 84984b1..d65c8ab 100644 --- a/src/add.c +++ b/src/add.c @@ -1,5 +1,17 @@ #include +//pthread_t adding_thread; +//pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER; + +//static int setup_adding() { +// if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) { +// perror("pthread_create"); +// return -1; +// } + +// return 1; +//} + void *add(void *p) { return NULL; } diff --git a/src/main.c b/src/main.c index b4d5d77..204264a 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,8 @@ static struct option long_options[] = { int main(int argc, char **argv) { int c; + if(setup_session()<0) { return EXIT_FAILURE; } + if(defaults()<0) { return EXIT_FAILURE; } if(opt_load_from_env()<0) { return EXIT_FAILURE; } diff --git a/src/opt/watch.c b/src/opt/watch.c index c1a7736..a84bae4 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -8,11 +8,10 @@ int opt_add_watch(char *directory) { return -1; } - if(torrent_init(&p,directory)<0) { return -1; } - torrent_free(p); + if(torrent_init(&p,directory,basename(directory))<0) { return -1; } + if(session_torrent_add(p)<0) { return -1; } - log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,directory); + log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,p->root,p->name); - log_err("not implemented\n"); - return -1; + return 1; } diff --git a/src/session.c b/src/session.c new file mode 100644 index 0000000..6b91b83 --- /dev/null +++ b/src/session.c @@ -0,0 +1,46 @@ +#include + +struct session session; + +void session_clean() { + struct torrent *torrent_p; + if(session.torrents!=NULL) { + while(session.torrent_count>0) { + torrent_p = session.torrents[session.torrent_count]; + if(torrent_p!=NULL) { torrent_free(torrent_p); } + session.torrent_count--; + } + } + free(session.torrents); +} + +int session_init() { + session.torrents = NULL; + session.torrent_count = 0; + + return 1; +} + +int session_torrent_add(struct torrent *p) { + size_t new_count; + + if((NULL==session.torrents)||(session.torrent_count%SESSION_REALLOC_COUNT==0)) { + new_count = session.torrent_count+SESSION_REALLOC_COUNT; + log_info(SESSION_MESSAGE_REALLOC_COUNT,new_count); + + session.torrents = realloc(session.torrents,sizeof(struct torrent*)*new_count); + if(NULL==session.torrents) { + perror("realloc"); + return -1; + } + + for(size_t i=session.torrent_count;i +static int setup_logging(); + int setup() { + // logging must be setup first if(setup_logging()<0) { return -1; } - if(setup_adding()<0) { return -1; } - return 1; -} - -pthread_t adding_thread; -pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER; -int setup_adding() { - if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) { - perror("pthread_create"); - return -1; - } + if(setup_session()<0) { return -1; } return 1; } @@ -21,7 +14,7 @@ int setup_adding() { pthread_t logging_thread; pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER; -int setup_logging() { +static int setup_logging() { if(0!=atexit(&log_entries_clean)) { perror("atexit"); return -1; @@ -40,3 +33,14 @@ int setup_logging() { return 1; } + +int setup_session() { + if(session_init(&session)<0) { return -1; } + + if(0!=atexit(&session_clean)) { + perror("atexit"); + return -1; + } + + return 1; +} diff --git a/src/torrent.c b/src/torrent.c index 5611595..000a652 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -10,8 +10,6 @@ void torrent_free(struct torrent *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"); @@ -25,7 +23,7 @@ int torrent_init(struct torrent **torrent_p, char *root, char *name) { (*torrent_p)->root = malloc(strlen(root)+1); if(NULL==(*torrent_p)->root) { perror("malloc"); - torrent_free(torrent_p); + torrent_free(*torrent_p); return -1; } strcpy((*torrent_p)->root,root); @@ -33,7 +31,7 @@ int torrent_init(struct torrent **torrent_p, char *root, char *name) { (*torrent_p)->name = malloc(strlen(name)+1); if(NULL==(*torrent_p)->name) { perror("malloc"); - torrent_free(torrent_p); + torrent_free(*torrent_p); return -1; } strcpy((*torrent_p)->name,name); diff --git a/src/tree.c b/src/tree.c index 6ac3b7f..ac9f086 100644 --- a/src/tree.c +++ b/src/tree.c @@ -13,3 +13,7 @@ void tree_free(struct tree *p) { free(p); } + +int tree_init(struct tree **p) { + return -1; +}