...
authoralex <[email protected]>
Wed, 27 Oct 2021 00:06:04 +0000 (17:06 -0700)
committeralex <[email protected]>
Wed, 27 Oct 2021 00:06:04 +0000 (17:06 -0700)
Makefile.am
inc/torrent.h
inc/tree.h
inc/util.h
src/torrent.c
src/util/concat.c [new file with mode: 0644]
src/watch.c

index 4f480a2c72baa3e9367a7f35a3e6ce782943e525..0b37448019f13cd0f491cf433d5cecf233d5e62e 100644 (file)
@@ -36,6 +36,7 @@ seederd_SOURCES = \
        src/torrent.c \
        src/tree.c \
        src/usage.c \
+       src/util/concat.c \
        src/util/dir.c \
        src/util/file.c \
        src/util/filter.c \
index f59016cafeedf7ae1de4054edb6002f221ada8cd..2befb2eb988aa8cb5cf34f9ceb6142230ad66590 100644 (file)
@@ -12,9 +12,9 @@
 struct torrent {
        char *root;
        char *name;
-       struct tree *file_tree;
+       struct tree *tree;
        struct hash_map *files;
-       int inotify_watch_fd;
+       int watch_fd;
 };
 
 int torrent_add(struct torrent*,struct file*);
index 2bb9edd5434e637361ccda6cc0ef3243908e89f6..005a491b932d0bff012a73e4c39bcb7467e9f602 100644 (file)
@@ -15,6 +15,7 @@ struct tree {
        struct tree_entry *files;
        struct tree *directories;
        struct tree *next;
+       int watch_fd;
 };
 
 int tree_add(struct tree*,struct file*);
index 061ff6784474bb0a822c592bce9751094a6e8734..cb8f070d9677cb6d1ae193d1278f58b2063074ee 100644 (file)
@@ -8,6 +8,7 @@
 
 #include<log.h>
 
+char *concat(const char*,const char*);
 int file_filter_all(const char*);
 int file_filter_ignore_dotfiles(const char*);
 int is_directory(const char*);
index e1ff26a803bf09126fe9d2131c7fc27f995e50fe..a53de68fb0bef3077a04b095d1dcc4b85387bf11 100644 (file)
@@ -8,7 +8,7 @@ int torrent_add(struct torrent *p, struct file *f) {
        if(NULL==p) { return -1; }
        if(NULL==f) { return -1; }
 
-       if(tree_add(p->file_tree,f)<0) { return -1; }
+       if(tree_add(p->tree,f)<0) { return -1; }
        if(torrent_add_file(p,f)<0) { return -1; }
 
        return 1;
@@ -32,7 +32,7 @@ void torrent_free(struct torrent *p) {
                hashmap_free(p->files);
        }
 
-       if(p->file_tree!=NULL) { tree_free(p->file_tree); }
+       if(p->tree!=NULL) { tree_free(p->tree); }
        
        free(p);
 }
@@ -56,7 +56,7 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name)
        (*torrent_p)->name = strdup(name);
        if(NULL==(*torrent_p)->name) { goto clean; }
 
-       if(tree_init(&((*torrent_p)->file_tree),NULL)<0) { goto clean; }
+       if(tree_init(&((*torrent_p)->tree),NULL)<0) { goto clean; }
 
        if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 
diff --git a/src/util/concat.c b/src/util/concat.c
new file mode 100644 (file)
index 0000000..3ab863a
--- /dev/null
@@ -0,0 +1,24 @@
+#include<util.h>
+
+char *concat(const char *a, const char *b) {
+       int len_a, len_b;
+
+       if(NULL==a) { return NULL; }
+       if(NULL==b) { return NULL; }
+
+       len_a = strlen(a);
+       len_b = strlen(b);
+       if(len_a<=0) { return NULL; }
+       if(len_b<=0) { return NULL; }
+
+       char *p = malloc(len_a+len_b+2);
+       if(NULL==p) { return NULL; }
+
+       strcpy(p,a);
+       if(a[len_a-1]!='/') {
+               strcat(p,"/");
+       }
+       strcat(p,b);
+
+       return p;
+}
index 09dcc7705a49836f5ffea2ab3e552e16a1cad233..3d0a1786b3d74acd49a8ed776d84caab5d74371b 100644 (file)
@@ -3,6 +3,7 @@
 static pthread_t watching_thread;
 
 static void *watch_spawn(void*);
+static int watch_spawn_all(int,const char*,struct tree*);
 static void watch_poll(int);
 
 int watch() {
@@ -25,22 +26,10 @@ static void *watch_spawn(void *unused) {
        }
 
        for(size_t i=0;i<session.torrent_count;i++) {
-               struct torrent *p = session.torrents[i];
-
-               uint32_t watch_flags = 
-                       IN_CLOSE_WRITE || 
-                       IN_CREATE || 
-                       IN_DELETE || 
-                       IN_MODIFY || 
-                       IN_MOVED_TO;
-
-               p->inotify_watch_fd = inotify_add_watch(fd,p->root,watch_flags);
-               if(p->inotify_watch_fd<0) {
-                       log_err(WATCH_MESSAGE_START_FAILED ,session.torrents[i]->root);
+               if(watch_spawn_all(fd,session.torrents[i]->root,session.torrents[i]->tree)<0) {
                        return NULL;
                }
 
-               log_info(WATCH_MESSAGE_START,session.torrents[i]->root);
        }
 
        watch_poll(fd);
@@ -48,6 +37,47 @@ static void *watch_spawn(void *unused) {
        return NULL;
 }
 
+#define WATCH_FLAGS IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO
+
+static int watch_spawn_all(int fd, const char *root, struct tree *tree) {
+       struct tree *p;
+
+       if(fd<0) { return -1; }
+       if(NULL==root) { return -1; }
+       if(strlen(root)<=0) { return -1; }
+       if(NULL==tree) { return -1; }
+
+       tree->watch_fd = inotify_add_watch(fd,root,WATCH_FLAGS);
+       if(tree->watch_fd<0) {
+               perror("inotify_add_watch");
+               log_err(WATCH_MESSAGE_START_FAILED,root);
+               return -1;
+       }
+
+       log_info(WATCH_MESSAGE_START,root);
+
+       p = tree->next;
+       while(p!=NULL) {
+               if(watch_spawn_all(fd,root,p)<0) { return -1; }
+               p = p->next;
+       }
+
+       p = tree->directories;
+       while(p!=NULL) {
+               char *newroot = concat(root,p->name);
+
+               if(watch_spawn_all(fd,newroot,p)<0) {
+                       free(newroot);
+                       return -1;
+               }
+
+               free(newroot);
+               p = p->next;
+       }
+
+       return 1;
+}
+
 static void watch_poll(int fd) {
        /* see `man inotify` for explanation of alignment modifiers */
        char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));