From: alex Date: Fri, 14 Jan 2022 02:15:43 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=c93ecf9cd0e82d3615e751f6460139ce9ee7d688;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index b790ce6..6a28b3c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,6 +34,7 @@ seederd_SOURCES = \ src/meta.c \ src/opt/config.c \ src/opt/env.c \ + src/opt/feed.c \ src/opt/filter.c \ src/opt/loglevel.c \ src/opt/out.c \ diff --git a/inc/opt.h b/inc/opt.h index 96cc4ea..8c60900 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -16,10 +16,11 @@ enum file_filters { }; struct options { - unsigned int worker_threads; + char *feed_url; + enum file_filters file_filter; unsigned long long piece_length; int verbose_flag; - enum file_filters file_filter; + unsigned int worker_threads; }; extern struct options global_options; @@ -44,6 +45,7 @@ int opt_add_watch(const char*); int opt_load_config_file(char*); int opt_load_from_env(); int opt_set(const char*,const char*); +int opt_set_feed_url(const char*); int opt_set_file_filter(const char*); void opt_set_log_level(enum log_level); int opt_set_out_stream(const char*); diff --git a/inc/torrent.h b/inc/torrent.h index eb9d89a..69c3511 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -39,7 +39,7 @@ ssize_t torrent_file_info(struct torrent*,uint8_t**); char *torrent_file_path(unsigned char*,size_t); int torrent_file_piece_layers(FILE*,struct torrent*); void torrent_free(struct torrent*); -int torrent_init(struct torrent**,const char*,const char*,unsigned long long); +int torrent_init(struct torrent**,unsigned long long); char *torrent_magnet(struct torrent*); #endif diff --git a/src/default.c b/src/default.c index f40cc9d..cdcf59d 100644 --- a/src/default.c +++ b/src/default.c @@ -5,7 +5,9 @@ struct options global_options; static int default_add_all_directories(); int defaults() { - // logging needs to be setup first + // setting logging thread to the current thread so messages + // will be printed asap (also logging thread will not + // have been created yet). logging_thread = pthread_self(); log_info(DEFAULT_MESSAGE_SETTING_DEFAULTS); @@ -13,6 +15,8 @@ int defaults() { // suppress output temporarily opt_set_log_level(LOG_LEVEL_SILENT); + if(opt_set_feed_url("")<0) { return -1; } + if(opt_set_file_filter("default")<0) { return -1; } if(opt_set_piece_length("16384")<0) { return -1; } { @@ -21,7 +25,6 @@ int defaults() { if(opt_set_worker_threads(buf)<0) { return -1; } } - if(opt_set_file_filter("default")<0) { return -1; } // restore logging output #ifndef NDEBUG diff --git a/src/init.c b/src/init.c index ee405a3..4da9b66 100644 --- a/src/init.c +++ b/src/init.c @@ -6,6 +6,7 @@ struct option long_options[] = { {"config-file", required_argument, 0, 'c'}, {"daemon", no_argument, 0, 'd'}, + {"feed-url", required_argument, 0, 'u'}, {"file-filter", required_argument, 0, 'f'}, {"help", no_argument, 0, 'h'}, {"log-file", required_argument, 0, 'l'}, @@ -29,7 +30,7 @@ int init(int argc, char **argv) { while(1) { int option_index = 0; - if((c = getopt_long(argc,argv,"c:df:hl:qvw:",long_options,&option_index))==-1) { break; } + if((c = getopt_long(argc,argv,"c:df:hl:qu:vw:",long_options,&option_index))==-1) { break; } switch(c) { case 1: @@ -54,6 +55,9 @@ int init(int argc, char **argv) { case 'q': opt_set_log_level(LOG_LEVEL_SILENT); break; + case 'u': + if(opt_set_feed_url(optarg)<0) { return -1; } + break; case 'v': opt_set_log_level(LOG_LEVEL_VERBOSE); break; diff --git a/src/opt/feed.c b/src/opt/feed.c new file mode 100644 index 0000000..4d2e913 --- /dev/null +++ b/src/opt/feed.c @@ -0,0 +1,12 @@ +#include + +int opt_set_feed_url(const char *url) { + if(NULL==url) { return -1; } + + if(global_options.feed_url!=NULL) { free(global_options.feed_url); } + + global_options.feed_url = strdup(url); + if(NULL==global_options.feed_url) { return -1; } + + return 1; +} diff --git a/src/opt/set.c b/src/opt/set.c index 6059b44..25d7f02 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -8,6 +8,7 @@ struct option_lookup_table_entry { // NOTE: see src/init.c for list of CLI options struct option_lookup_table_entry option_lookup_table[] = { + {"feed_url",&opt_set_feed_url}, {"file_filter",&opt_set_file_filter}, {"piece_length",&opt_set_piece_length}, {"watch_directory",&opt_add_watch}, diff --git a/src/opt/watch.c b/src/opt/watch.c index e4842b1..2eb8644 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -9,13 +9,29 @@ int opt_add_watch(const char *directory) { return -1; } + name = strdup(directory); + if(NULL==name) { return -1; } name = basename(name); - if(torrent_init(&p,directory,name,global_options.piece_length)<0) { return -1; } - if(session_torrent_add(p)<0) { return -1; } + if(torrent_init(&p,global_options.piece_length)<0) { return -1; } + + p->root = strdup(directory); + if(NULL==p->root) { goto clean; } + + p->name = strdup(name); + if(NULL==p->name) { goto clean; } + free(name); + + p->feed_url = strdup(global_options.feed_url); + if(NULL==p->feed_url) { goto clean; } + + if(session_torrent_add(p)<0) { goto clean; } log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,p->root,p->name); return 1; +clean: + torrent_free(p); + return -1; } diff --git a/src/torrent/free.c b/src/torrent/free.c index 3905e66..abf6493 100644 --- a/src/torrent/free.c +++ b/src/torrent/free.c @@ -5,6 +5,7 @@ void torrent_free(struct torrent *p) { if(p->root!=NULL) { free(p->root); } if(p->name!=NULL) { free(p->name); } + if(p->feed_url!=NULL) { free(p->feed_url); } // clear first, tree_free will also free files hashmap_clear(p->files.paths); diff --git a/src/torrent/init.c b/src/torrent/init.c index 6b631f9..c15a110 100644 --- a/src/torrent/init.c +++ b/src/torrent/init.c @@ -2,10 +2,8 @@ #define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8 -int torrent_init(struct torrent **torrent_p, const char *root, const char *name, unsigned long long piece_length) { +int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) { if(NULL==torrent_p) { return -1; } - if(NULL==root) { return -1; } - if(NULL==name) { return -1; } if((piece_length<16384)||(!(piece_length&&!(piece_length&(piece_length-1))))) { return -1; } @@ -15,12 +13,9 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name, return -1; } - (*torrent_p)->root = strdup(root); - if(NULL==(*torrent_p)->root) { goto clean; } - - (*torrent_p)->name = strdup(name); - if(NULL==(*torrent_p)->name) { goto clean; } - + (*torrent_p)->root = NULL; + (*torrent_p)->name = NULL; + (*torrent_p)->feed_url = NULL; (*torrent_p)->piece_length = piece_length; if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }