From: alex Date: Thu, 9 Sep 2021 17:19:23 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=4802953af60d260349b26ba3fed82685e8477187;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index f95f2c2..5150b9e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,6 +20,7 @@ seederd_SOURCES = \ src/main.c \ src/opt/config.c \ src/opt/env.c \ + src/opt/filter.c \ src/opt/loglevel.c \ src/opt/piecel.c \ src/opt/set.c \ @@ -31,7 +32,8 @@ seederd_SOURCES = \ src/tree.c \ src/usage.c \ src/util/dir.c \ - src/util/file.c + src/util/file.c \ + src/util/filter.c seederd_SOURCES += \ inc/add.h \ diff --git a/inc/add.h b/inc/add.h index d5a4b43..ba80d5e 100644 --- a/inc/add.h +++ b/inc/add.h @@ -5,6 +5,7 @@ #include #include +#include #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n" diff --git a/inc/opt.h b/inc/opt.h index f185b16..436f678 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -9,10 +9,17 @@ #include #include +enum file_filters { + FILE_FILTER_IGNORE_DOTFILES, + FILE_FILTER_ALL, + FILE_FILTER_DEFAULT = FILE_FILTER_IGNORE_DOTFILES +}; + struct options { unsigned int worker_threads; unsigned long long piece_length; int verbose_flag; + enum file_filters file_filter; }; extern struct options global_options; @@ -20,6 +27,8 @@ 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_FILE_FILTER_SET "set file filter to %s\n" +#define OPT_MESSAGE_FILE_FILTER_UNKNOWN "unknown file filter: %s\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" @@ -30,12 +39,13 @@ extern struct options global_options; #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_add_watch(const char*); int opt_load_config_file(char*); int opt_load_from_env(); -int opt_set(char*,char*); +int opt_set(const char*,const char*); +int opt_set_file_filter(const char*); void opt_set_log_level(enum log_level); -int opt_set_piece_length(char*); -int opt_set_worker_threads(char*); +int opt_set_piece_length(const char*); +int opt_set_worker_threads(const char*); #endif diff --git a/inc/torrent.h b/inc/torrent.h index 7ca8768..7ba4a41 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -17,6 +17,6 @@ struct torrent { }; void torrent_free(struct torrent*); -int torrent_init(struct torrent**,char*,char*); +int torrent_init(struct torrent**,const char*,const char*); #endif diff --git a/inc/util.h b/inc/util.h index 55244cf..061ff67 100644 --- a/inc/util.h +++ b/inc/util.h @@ -3,11 +3,14 @@ #include #include +#include #include -int file_filter(char*); -void find(char*); -int is_directory(char*); -int is_file(char*); +#include + +int file_filter_all(const char*); +int file_filter_ignore_dotfiles(const char*); +int is_directory(const char*); +int is_file(const char*); #endif diff --git a/src/add.c b/src/add.c index fbf4a38..6a7a529 100644 --- a/src/add.c +++ b/src/add.c @@ -40,6 +40,15 @@ static int add_find_all(struct torrent *p) { } static int ftw_helper(const char *path, const struct stat *st, int typeflag) { + if(typeflag!=FTW_F) { return 0; } log_info("adding %s\n",path); + switch(global_options.file_filter) { + case FILE_FILTER_IGNORE_DOTFILES: + if(file_filter_ignore_dotfiles(path)<0) { return 0; } + break; + case FILE_FILTER_ALL: + if(file_filter_all(path)<0) { return 0; } + break; + } return 0; } diff --git a/src/default.c b/src/default.c index b3521ea..0498a15 100644 --- a/src/default.c +++ b/src/default.c @@ -25,6 +25,8 @@ int defaults() { if(opt_set_worker_threads(buf)<0) { return -1; } } + if(opt_set_file_filter("default")<0) { return -1; } + if(default_add_all_directories()<0) { return -1; } return 1; diff --git a/src/log.c b/src/log.c index 23c51c9..b2ffe78 100644 --- a/src/log.c +++ b/src/log.c @@ -144,9 +144,7 @@ void log_message(enum log_level level, FILE *out_stream, const char *format,...) void *log_poll(void *p) { log_info(LOG_THREAD_START_MESSAGE); while(1) { - pthread_mutex_lock(&logging_mutex); log_print(); - pthread_mutex_unlock(&logging_mutex); } return NULL; diff --git a/src/main.c b/src/main.c index d873c46..743f173 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ static struct option long_options[] = { {"config-file", required_argument, 0, 'c'}, {"daemon", no_argument, 0, 'd'}, + {"file_filter", required_argument, 0, 'f'}, {"help", no_argument, 0, 'h'}, {"quiet", no_argument, 0, 'q'}, {"verbose", no_argument, 0, 'v'}, @@ -22,7 +23,7 @@ int main(int argc, char **argv) { while(1) { int option_index = 0; - if((c = getopt_long(argc,argv,"c:dhqvw:",long_options,&option_index))==-1) { break; } + if((c = getopt_long(argc,argv,"c:df:hqvw:",long_options,&option_index))==-1) { break; } switch(c) { case 1: @@ -35,6 +36,9 @@ int main(int argc, char **argv) { log_err("not implemented\n"); return EXIT_FAILURE; break; + case 'f': + if(opt_set_file_filter(optarg)<0) { return EXIT_FAILURE; } + break; case 'h': usage(); return EXIT_FAILURE; diff --git a/src/opt/config.c b/src/opt/config.c index f572ea7..863ac56 100644 --- a/src/opt/config.c +++ b/src/opt/config.c @@ -1,11 +1,5 @@ #include -#define SET_OPT(x,f) { \ - if(strcmp(x,field)==0) { \ - if(f(buf)<0) { goto clean; } \ - } \ -} - int opt_load_config_file(char *path) { FILE *fp; char field[100]; diff --git a/src/opt/filter.c b/src/opt/filter.c new file mode 100644 index 0000000..c85e963 --- /dev/null +++ b/src/opt/filter.c @@ -0,0 +1,17 @@ +#include + +int opt_set_file_filter(const char *filter) { + if(strcmp(filter,"default")==0) { + global_options.file_filter = FILE_FILTER_DEFAULT; + } else if(strcmp(filter,"-dotfiles")==0) { + global_options.file_filter = FILE_FILTER_IGNORE_DOTFILES; + } else if(strcmp(filter,"all")==0) { + global_options.file_filter = FILE_FILTER_ALL; + } else { + log_err(OPT_MESSAGE_FILE_FILTER_UNKNOWN,filter); + return -1; + } + + log_info(OPT_MESSAGE_FILE_FILTER_SET,filter); + return 1; +} diff --git a/src/opt/piecel.c b/src/opt/piecel.c index c9fd8c7..3c6dac2 100644 --- a/src/opt/piecel.c +++ b/src/opt/piecel.c @@ -1,6 +1,6 @@ #include -int opt_set_piece_length(char *length) { +int opt_set_piece_length(const char *length) { char *end; unsigned long long i; diff --git a/src/opt/set.c b/src/opt/set.c index 14b1ef5..b1c42f9 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -2,17 +2,18 @@ struct option_lookup_table_entry { char *key; - int (*function)(char*); + int (*function)(const char*); }; struct option_lookup_table_entry option_lookup_table[] = { + {"file_filter",&opt_set_file_filter}, {"piece_length",&opt_set_piece_length}, {"watch",&opt_add_watch}, {"worker_threads",&opt_set_worker_threads}, {NULL,NULL} }; -int opt_set(char *key, char *value) { +int opt_set(const char *key, const char *value) { struct option_lookup_table_entry p; size_t i; diff --git a/src/opt/watch.c b/src/opt/watch.c index a84bae4..b057f65 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -1,14 +1,18 @@ #include -int opt_add_watch(char *directory) { +int opt_add_watch(const char *directory) { struct torrent *p; + char *name; if(!is_directory(directory)) { log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); return -1; } - if(torrent_init(&p,directory,basename(directory))<0) { return -1; } + name = strdup(directory); + name = basename(name); + + if(torrent_init(&p,directory,name)<0) { return -1; } if(session_torrent_add(p)<0) { return -1; } log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,p->root,p->name); diff --git a/src/opt/worker.c b/src/opt/worker.c index 4e3def8..d830d76 100644 --- a/src/opt/worker.c +++ b/src/opt/worker.c @@ -1,6 +1,6 @@ #include -int opt_set_worker_threads(char *str) { +int opt_set_worker_threads(const char *str) { char *end; global_options.worker_threads = strtoul(str,&end,10); diff --git a/src/torrent.c b/src/torrent.c index 000a652..da3c64a 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -9,7 +9,7 @@ void torrent_free(struct torrent *p) { free(p); } -int torrent_init(struct torrent **torrent_p, char *root, char *name) { +int torrent_init(struct torrent **torrent_p, const char *root, const char *name) { *(torrent_p) = malloc(sizeof(struct torrent)); if(NULL==(*torrent_p)) { perror("malloc"); @@ -20,21 +20,19 @@ int torrent_init(struct torrent **torrent_p, char *root, char *name) { (*torrent_p)->name = NULL; (*torrent_p)->file_tree = NULL; - (*torrent_p)->root = malloc(strlen(root)+1); + (*torrent_p)->root = strdup(root); if(NULL==(*torrent_p)->root) { - perror("malloc"); + perror("strdup"); torrent_free(*torrent_p); return -1; } - strcpy((*torrent_p)->root,root); - (*torrent_p)->name = malloc(strlen(name)+1); + (*torrent_p)->name = strdup(name); if(NULL==(*torrent_p)->name) { - perror("malloc"); + perror("strdup"); torrent_free(*torrent_p); return -1; } - strcpy((*torrent_p)->name,name); return 1; } diff --git a/src/usage.c b/src/usage.c index 5a194ed..8828607 100644 --- a/src/usage.c +++ b/src/usage.c @@ -7,6 +7,7 @@ void usage() { log_err("Options:\n"); log_err("\t--config-file=, -c \n"); log_err("\t--daemon, -d\n"); + log_err("\t--file-filter=, -f \n"); log_err("\t--help, -h\n"); log_err("\t--quiet, -q\n"); log_err("\t--verbose, -v\n"); diff --git a/src/util/dir.c b/src/util/dir.c index 07d324b..747ee34 100644 --- a/src/util/dir.c +++ b/src/util/dir.c @@ -1,6 +1,6 @@ #include -int is_directory(char *path) { +int is_directory(const char *path) { struct stat st; if(stat(path,&st)!=0) { diff --git a/src/util/file.c b/src/util/file.c index 4fcbe59..2ef1aba 100644 --- a/src/util/file.c +++ b/src/util/file.c @@ -1,6 +1,6 @@ #include -int is_file(char *path) { +int is_file(const char *path) { struct stat st; if(stat(path,&st)!=0) { diff --git a/src/util/filter.c b/src/util/filter.c new file mode 100644 index 0000000..5d0589c --- /dev/null +++ b/src/util/filter.c @@ -0,0 +1,19 @@ +#include + +int file_filter_all(const char *path) { + return 1; +} + +int file_filter_ignore_dotfiles(const char *path) { + const char *p; + if(NULL==path) { return -1; } + + p = path; + while((p = strchr(p, '/'))!=NULL) { + p++; + log_info("p: %s\n",p); + if(p[0]=='.') { return -1; } + } + + return 1; +} diff --git a/src/util/find.c b/src/util/find.c deleted file mode 100644 index 85f7860..0000000 --- a/src/util/find.c +++ /dev/null @@ -1,24 +0,0 @@ -#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) { - -}