...
authoralex <[email protected]>
Sun, 24 Oct 2021 23:17:56 +0000 (16:17 -0700)
committeralex <[email protected]>
Sun, 24 Oct 2021 23:17:56 +0000 (16:17 -0700)
inc/torrent.h
inc/watch.h
src/add.c
src/main.c
src/watch.c

index 46cce890e81dde7b8f8f08c229820f355461ff66..f59016cafeedf7ae1de4054edb6002f221ada8cd 100644 (file)
@@ -14,6 +14,7 @@ struct torrent {
        char *name;
        struct tree *file_tree;
        struct hash_map *files;
+       int inotify_watch_fd;
 };
 
 int torrent_add(struct torrent*,struct file*);
index 31f0d55b5a6ef04be31b1a6a6c65349695cdf4e1..18b2aab09a943f4468672e0f5a39acb5cc99add0 100644 (file)
@@ -1,6 +1,16 @@
 #ifndef __WATCH_H_
 #define __WATCH_H_
 
+#include<pthread.h>
+#include<sys/inotify.h>
+#include<unistd.h>
+
+#include<log.h>
+
+#define WATCH_MESSAGE_FAILED "failed to start watching\n"
+#define WATCH_MESSAGE_START "watching started: %s\n"
+#define WATCH_MESSAGE_START_FAILED "failed to start watching %s\n"
+
 int watch();
 
 #endif
index 21035420b434033666fde918b354510b49002762..774806c29483436b9339155949070525b6f62dea 100644 (file)
--- a/src/add.c
+++ b/src/add.c
@@ -1,16 +1,6 @@
 #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;
index e761aba37e7ca3a40458876c05e2ca2d127c9be7..e58a4cd9a43794aeb454def9ba724e6030221e72 100644 (file)
@@ -9,6 +9,8 @@ int main(int argc, char **argv) {
        //if(feed()<0) { return EXIT_FAILURE; }
        if(watch()<0) { return EXIT_FAILURE; }
 
+       while(1) { }
+
        if(server_start()<0) { return EXIT_FAILURE; }
 
        return EXIT_SUCCESS;
index 397712745cfd0809f95cdd9084c5e24cc84589c6..09dcc7705a49836f5ffea2ab3e552e16a1cad233 100644 (file)
@@ -1,5 +1,71 @@
 #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);
+                       }
+               }
+       }
 }