From 0f751a00bae7200471f5813f9aad39c47cc15b72 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 24 Oct 2021 16:17:56 -0700 Subject: [PATCH] ... --- inc/torrent.h | 1 + inc/watch.h | 10 ++++++++ src/add.c | 12 +-------- src/main.c | 2 ++ src/watch.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 81 insertions(+), 12 deletions(-) diff --git a/inc/torrent.h b/inc/torrent.h index 46cce89..f59016c 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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*); diff --git a/inc/watch.h b/inc/watch.h index 31f0d55..18b2aab 100644 --- a/inc/watch.h +++ b/inc/watch.h @@ -1,6 +1,16 @@ #ifndef __WATCH_H_ #define __WATCH_H_ +#include +#include +#include + +#include + +#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 diff --git a/src/add.c b/src/add.c index 2103542..774806c 100644 --- a/src/add.c +++ b/src/add.c @@ -1,16 +1,6 @@ #include -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; diff --git a/src/main.c b/src/main.c index e761aba..e58a4cd 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/watch.c b/src/watch.c index 3977127..09dcc77 100644 --- a/src/watch.c +++ b/src/watch.c @@ -1,5 +1,71 @@ #include +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;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); + 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); + } + } + } } -- 2.39.5