From a6af110834f2c45df01507dda209cdd1cc376fd3 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 5 Sep 2021 20:54:30 -0700 Subject: [PATCH] ... --- Makefile.am | 1 + inc/default.h | 2 ++ inc/opt.h | 11 +++++++++++ inc/torrent.h | 2 ++ inc/util.h | 3 +++ src/default.c | 10 +++++++++- src/main.c | 4 ++++ src/opt/env.c | 3 +++ src/opt/piecel.c | 4 +++- src/opt/set.c | 1 + src/opt/worker.c | 14 ++++++++++++++ src/torrent.c | 2 ++ src/usage.c | 1 + src/util/find.c | 24 ++++++++++++++++++++++++ 14 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/opt/worker.c create mode 100644 src/util/find.c diff --git a/Makefile.am b/Makefile.am index 2fb2ad5..dd4c753 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,7 @@ seederd_SOURCES = \ src/opt/piecel.c \ src/opt/set.c \ src/opt/watch.c \ + src/opt/worker.c \ src/setup.c \ src/torrent.c \ src/usage.c \ diff --git a/inc/default.h b/inc/default.h index 4b43a4c..3a91225 100644 --- a/inc/default.h +++ b/inc/default.h @@ -1,6 +1,8 @@ #ifndef __DEFAULT_H_ #define __DEFAULT_H_ +#include + #include #include diff --git a/inc/opt.h b/inc/opt.h index b3506d2..b1f5407 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -7,8 +7,16 @@ #include #include +struct options { + unsigned int worker_threads; + unsigned long long piece_length; +}; + +extern struct options global_options; + #define OPT_MESSAGE_CONFIG_FILE_FORMAT_ERROR "invalid format in config file %s:%d\n" #define OPT_MESSAGE_ENV_LOADING "loading settings from environment\n" +#define OPT_MESSAGE_ENV_LOADING_END "finished loading settings from environment\n" #define OPT_MESSAGE_LOADING_CONFIG_FILE "loading config file %s\n" #define OPT_MESSAGE_PIECE_LENGTH_INVALID "invalid piece length of %llu\npiece length must be >16384 and a power of 2\n" #define OPT_MESSAGE_PIECE_LENGTH_SET "piece length set to %llu\n" @@ -16,6 +24,8 @@ #define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n" #define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %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" int opt_add_watch(char*); int opt_load_config_file(char*); @@ -23,5 +33,6 @@ int opt_load_from_env(); int opt_set(char*,char*); void opt_set_log_level(enum log_level); int opt_set_piece_length(char*); +int opt_set_worker_threads(char*); #endif diff --git a/inc/torrent.h b/inc/torrent.h index 21ab327..0e53ec1 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -9,6 +9,8 @@ struct torrent { char *root; }; +extern struct torrent **torrents; + void torrent_free(struct torrent*); int torrent_init(struct torrent**,char*); diff --git a/inc/util.h b/inc/util.h index 4d5febb..55244cf 100644 --- a/inc/util.h +++ b/inc/util.h @@ -1,9 +1,12 @@ #ifndef __UTIL_H_ #define __UTIL_H_ +#include #include #include +int file_filter(char*); +void find(char*); int is_directory(char*); int is_file(char*); diff --git a/src/default.c b/src/default.c index cbc4a2d..c55fff9 100644 --- a/src/default.c +++ b/src/default.c @@ -1,5 +1,7 @@ #include +struct options global_options; + int defaults() { // logging needs to be setup first logging_thread = pthread_self(); @@ -13,7 +15,13 @@ int defaults() { log_info(DEFAULT_MESSAGE_SETTING_DEFAULTS); - opt_set_piece_length("16384"); + if(opt_set_piece_length("16384")<0) { return -1; } + + { + char buf[10]; + snprintf(buf,10,"%lu",sysconf(_SC_NPROCESSORS_ONLN)); + if(opt_set_worker_threads(buf)<0) { return -1; } + } return 1; } diff --git a/src/main.c b/src/main.c index c8e4b21..54c6c5d 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ static struct option long_options[] = { {"quiet", no_argument, &verbose_flag, LOG_LEVEL_SILENT}, {"verbose", no_argument, &verbose_flag, LOG_LEVEL_VERBOSE}, {"watch", required_argument, 0, 'w'}, + {"worker-threads", required_argument, 0, 1}, {0,0,0,0} }; @@ -22,6 +23,9 @@ int main(int argc, char **argv) { if((c = getopt_long(argc,argv,"c:dhqvw:",long_options,&option_index))==-1) { break; } switch(c) { + case 1: + if(opt_set_worker_threads(optarg)<0) { return EXIT_FAILURE; } + break; case 'c': if(opt_load_config_file(optarg)<0) { return EXIT_FAILURE; } break; diff --git a/src/opt/env.c b/src/opt/env.c index 4ec76db..31f2005 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -16,6 +16,9 @@ int opt_load_from_env() { CHECK_ENV("CONFIG",opt_load_config_file); CHECK_ENV("PIECE_LENGTH",opt_set_piece_length); + CHECK_ENV("WORKER_THREADS",opt_set_worker_threads); + + log_info(OPT_MESSAGE_ENV_LOADING_END); return 1; } diff --git a/src/opt/piecel.c b/src/opt/piecel.c index 1b5ba7d..c9fd8c7 100644 --- a/src/opt/piecel.c +++ b/src/opt/piecel.c @@ -11,6 +11,8 @@ int opt_set_piece_length(char *length) { return -1; } + global_options.piece_length = i; log_info(OPT_MESSAGE_PIECE_LENGTH_SET,i); - return -1; + + return 1; } diff --git a/src/opt/set.c b/src/opt/set.c index 18b7e87..14b1ef5 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -8,6 +8,7 @@ struct option_lookup_table_entry { struct option_lookup_table_entry option_lookup_table[] = { {"piece_length",&opt_set_piece_length}, {"watch",&opt_add_watch}, + {"worker_threads",&opt_set_worker_threads}, {NULL,NULL} }; diff --git a/src/opt/worker.c b/src/opt/worker.c new file mode 100644 index 0000000..4e3def8 --- /dev/null +++ b/src/opt/worker.c @@ -0,0 +1,14 @@ +#include + +int opt_set_worker_threads(char *str) { + char *end; + + global_options.worker_threads = strtoul(str,&end,10); + if(0==global_options.worker_threads) { + log_err(OPT_MESSAGE_WORKER_THREADS_INVALID,str); + return -1; + } + + log_info(OPT_MESSAGE_WORKER_THREADS_SET,global_options.worker_threads); + return 1; +} diff --git a/src/torrent.c b/src/torrent.c index 7f78130..a642272 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -1,5 +1,7 @@ #include +struct torrent **torrents; + void torrent_free(struct torrent *p) { free(p->root); free(p); diff --git a/src/usage.c b/src/usage.c index e189a94..5a194ed 100644 --- a/src/usage.c +++ b/src/usage.c @@ -11,4 +11,5 @@ void usage() { log_err("\t--quiet, -q\n"); log_err("\t--verbose, -v\n"); log_err("\t--watch=, -w \n"); + log_err("\t--worker-threads=\n"); } diff --git a/src/util/find.c b/src/util/find.c new file mode 100644 index 0000000..85f7860 --- /dev/null +++ b/src/util/find.c @@ -0,0 +1,24 @@ +#include + +int file_filter(char *path) { + /* scandir requires non-zero return values to include */ + char *p; + if(NULL==path) { return false; } + + p = strrchr(path, '/'); + + if(NULL==p) { + p = path; + } else { + p++; + } + + if(p[0]=='.') { return 0; } + + return 1; +} + +void find(char *path) { + if(is_file(path)<0) { + +} -- 2.30.2