#include<add.h>
-pthread_t *adding_threads;
-pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-//static int setup_adding() {
-// if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) {
-// perror("pthread_create");
-// return -1;
-// }
-
-// return 1;
-//}
+static pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct hash_map *add_queue;
static size_t add_queue_index;
#include<watch.h>
+static pthread_t watching_thread;
+
+static void *watch_spawn(void*);
+static void watch_poll(int);
+
int watch() {
- return -1;
+ if(pthread_create(&watching_thread,NULL,&watch_spawn,NULL)!=0) {
+ perror("pthread_create");
+ return -1;
+ }
+
+ return 1;
+}
+
+static void *watch_spawn(void *unused) {
+ int fd;
+
+ fd = inotify_init();
+ if(fd<0) {
+ log_err(WATCH_MESSAGE_FAILED);
+ perror("inotify_init");
+ return NULL;
+ }
+
+ 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);
+ return NULL;
+ }
+
+ log_info(WATCH_MESSAGE_START,session.torrents[i]->root);
+ }
+
+ watch_poll(fd);
+
+ return NULL;
+}
+
+static void watch_poll(int fd) {
+ /* see `man inotify` for explanation of alignment modifiers */
+ char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
+ const struct inotify_event *event;
+ ssize_t len;
+
+ for(;;) {
+ len = read(fd,buf,sizeof(buf));
+ if(len<=0) {
+ perror("read");
+ break;
+ }
+
+ for(char *p=buf;p<(buf+len);p+=(sizeof(struct inotify_event)+event->len)) {
+ event = (const struct inotify_event*) p;
+ if(event->len) {
+ log_info("%s modified?\n",event->name);
+ }
+ }
+ }
}