From c3fce94e4068af63a11d2cc5e44ba264d53594df Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 26 Oct 2021 17:06:04 -0700 Subject: [PATCH] ... --- Makefile.am | 1 + inc/torrent.h | 4 ++-- inc/tree.h | 1 + inc/util.h | 1 + src/torrent.c | 6 ++--- src/util/concat.c | 24 ++++++++++++++++++++ src/watch.c | 56 ++++++++++++++++++++++++++++++++++++----------- 7 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 src/util/concat.c diff --git a/Makefile.am b/Makefile.am index 4f480a2..0b37448 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/inc/torrent.h b/inc/torrent.h index f59016c..2befb2e 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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*); diff --git a/inc/tree.h b/inc/tree.h index 2bb9edd..005a491 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -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*); diff --git a/inc/util.h b/inc/util.h index 061ff67..cb8f070 100644 --- a/inc/util.h +++ b/inc/util.h @@ -8,6 +8,7 @@ #include +char *concat(const char*,const char*); int file_filter_all(const char*); int file_filter_ignore_dotfiles(const char*); int is_directory(const char*); diff --git a/src/torrent.c b/src/torrent.c index e1ff26a..a53de68 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -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 index 0000000..3ab863a --- /dev/null +++ b/src/util/concat.c @@ -0,0 +1,24 @@ +#include + +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; +} diff --git a/src/watch.c b/src/watch.c index 09dcc77..3d0a178 100644 --- a/src/watch.c +++ b/src/watch.c @@ -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;iinotify_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)))); -- 2.30.2