]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 9 Sep 2021 17:19:23 +0000 (10:19 -0700)
committeralex <[email protected]>
Thu, 9 Sep 2021 17:19:23 +0000 (10:19 -0700)
21 files changed:
Makefile.am
inc/add.h
inc/opt.h
inc/torrent.h
inc/util.h
src/add.c
src/default.c
src/log.c
src/main.c
src/opt/config.c
src/opt/filter.c [new file with mode: 0644]
src/opt/piecel.c
src/opt/set.c
src/opt/watch.c
src/opt/worker.c
src/torrent.c
src/usage.c
src/util/dir.c
src/util/file.c
src/util/filter.c [new file with mode: 0644]
src/util/find.c [deleted file]

index f95f2c2c3fb4011d9264905099eb5098052c8ac7..5150b9ea0342b00c632c3a05dc364eafd525a955 100644 (file)
@@ -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 \
index d5a4b431bdb1d8d88c76ae8df5505d542f7e0495..ba80d5eddc691a9839812f8f2d31aff1ea87dffc 100644 (file)
--- a/inc/add.h
+++ b/inc/add.h
@@ -5,6 +5,7 @@
 
 #include<hashmap.h>
 #include<log.h>
+#include<util.h>
 
 #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n"
 
index f185b1671639800cac15dd863af978a81ea2a23f..436f678d04878af14c310bac81589e077b1b12fd 100644 (file)
--- a/inc/opt.h
+++ b/inc/opt.h
@@ -9,10 +9,17 @@
 #include<torrent.h>
 #include<util.h>
 
+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
index 7ca876838b8e9f87216e19f68e1acdffe619f5a8..7ba4a411ddf3d68562871186bfb10c814e7cddd5 100644 (file)
@@ -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
index 55244cfb8fb8a47925a79c6873b8cf839bc1c0ae..061ff6784474bb0a822c592bce9751094a6e8734 100644 (file)
@@ -3,11 +3,14 @@
 
 #include<dirent.h>
 #include<stdio.h>
+#include<string.h>
 #include<sys/stat.h>
 
-int file_filter(char*);
-void find(char*);
-int is_directory(char*);
-int is_file(char*);
+#include<log.h>
+
+int file_filter_all(const char*);
+int file_filter_ignore_dotfiles(const char*);
+int is_directory(const char*);
+int is_file(const char*);
 
 #endif
index fbf4a3830013daaa87221b8f8b11626b8201de95..6a7a529a82f8b066503888cbd0ef1ca82eb96256 100644 (file)
--- 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;
 }
index b3521ea37f92b0cf69b08a6b0b826437519d4f7b..0498a150c6d7df4b3d67981b8dfec0a5708fd6eb 100644 (file)
@@ -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;
index 23c51c9159cbadbdf88b6ab5ef3294628c7a26ad..b2ffe78dc24512f57387f4f6d2a42aad4af0f422 100644 (file)
--- 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;
index d873c4636df16f45885e41d3ae9867c828162bd0..743f173b5b6cd9826f1545a454cb6abd23a739cc 100644 (file)
@@ -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;
index f572ea74ad6af214fa8aa0013973c304404ac711..863ac561019cf9a06011fd53822d517325385d8f 100644 (file)
@@ -1,11 +1,5 @@
 #include<opt.h>
 
-#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 (file)
index 0000000..c85e963
--- /dev/null
@@ -0,0 +1,17 @@
+#include<opt.h>
+
+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;
+}
index c9fd8c7da1d1cda917ea0fe38cae76865dca225c..3c6dac252825bae2ed567efec447238dfdc66f86 100644 (file)
@@ -1,6 +1,6 @@
 #include<opt.h>
 
-int opt_set_piece_length(char *length) {
+int opt_set_piece_length(const char *length) {
        char *end;
        unsigned long long i;
 
index 14b1ef5279193233e19321b450e24a11301505ec..b1c42f904e7dc63244c954a2f7326b1f7e7cc135 100644 (file)
@@ -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;
        
index a84bae49c3ea56a39239fb165114f491bbe0fe9d..b057f6551897e0147eb6d006324bb4f900f146f3 100644 (file)
@@ -1,14 +1,18 @@
 #include<opt.h>
 
-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);
index 4e3def8b655b516998e0e2c4158fbe2c50c2e6a4..d830d76ddd72b21a66120fa5468e1ebc7881374e 100644 (file)
@@ -1,6 +1,6 @@
 #include<opt.h>
 
-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);
index 000a6529430831ff6b7afb2e453e7a67b51d6e18..da3c64a6a5bd2c6e7c8873a46c89cbca8174d860 100644 (file)
@@ -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;
 }
index 5a194edbd58afceef8bc03dff8450bde11c83de2..88286079d1fde165bc9c72f8b9a9303af4bbb5bf 100644 (file)
@@ -7,6 +7,7 @@ void usage() {
        log_err("Options:\n");
        log_err("\t--config-file=<path>, -c <path>\n");
        log_err("\t--daemon, -d\n");
+       log_err("\t--file-filter=<filter>, -f <filter>\n");
        log_err("\t--help, -h\n");
        log_err("\t--quiet, -q\n");
        log_err("\t--verbose, -v\n");
index 07d324bc8d0b9127a72d585b1b9dd42442816004..747ee345e98e3570c6820ef9af0759033e25b592 100644 (file)
@@ -1,6 +1,6 @@
 #include<util.h>
 
-int is_directory(char *path) {
+int is_directory(const char *path) {
        struct stat st;
        
        if(stat(path,&st)!=0) {
index 4fcbe59bb518796a6f9f6c374b583531ff1b2029..2ef1aba68211610d6fca9345a0220c1e9322de9c 100644 (file)
@@ -1,6 +1,6 @@
 #include<util.h>
 
-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 (file)
index 0000000..5d0589c
--- /dev/null
@@ -0,0 +1,19 @@
+#include<util.h>
+
+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 (file)
index 85f7860..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#include<util.h>
-
-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) {
-               
-}