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;
hashmap_free(p->files);
}
- if(p->file_tree!=NULL) { tree_free(p->file_tree); }
+ if(p->tree!=NULL) { tree_free(p->tree); }
free(p);
}
(*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; }
--- /dev/null
+#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;
+}
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() {
}
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);
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))));