From 440f0502990098326b2d8e4935da35809a0532a8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 20 Nov 2009 02:11:28 +0000 Subject: add and remove should work now diff --git a/rhdropbox.c b/rhdropbox.c index bc8eaf5..1fbd56f 100644 --- a/rhdropbox.c +++ b/rhdropbox.c @@ -81,13 +81,21 @@ int process_watch(int inotify_fd) return 0; } -int process_cmd_add_remove(cmd_id_t cmd_id, const char* path, int fd, int inotify_fd) +int process_cmd_add_remove(cmd_id_t cmd_id, const char* path, int fd, int inotify_fd, watch_list_t* watch_lst) { int wd = 0; if(cmd_id == ADD) wd = inotify_add_watch(inotify_fd, path, IN_CLOSE_WRITE); - else if(cmd_id == REMOVE) - wd = inotify_rm_watch(inotify_fd, -1); // lookup wd from path list + else if(cmd_id == REMOVE) { + int fd = watch_list_find_fd(watch_lst, path); + if(fd >= 0) + wd = inotify_rm_watch(inotify_fd, fd); + else { + log_printf(ERROR, "cmd_add_remove path not found in watch list"); + send_response(fd, "Error: path not found in watch list"); + return 0; + } + } else { log_printf(WARNING, "cmd_add_remove ignoring wrong cmd_id"); return 0; @@ -98,11 +106,22 @@ int process_cmd_add_remove(cmd_id_t cmd_id, const char* path, int fd, int inotif return 0; } - // add/remove path to/from list + int ret = 0; + if(cmd_id == ADD) { + ret = watch_list_add(watch_lst, wd, path); + if(ret) { + log_printf(ERROR, "can't add path to watch list"); + send_response(fd, "Error: can't add path to wath list"); + } + } + else { + watch_list_rm(watch_lst, path); + } - log_printf(ERROR, "inotify %s '%s'", cmd_id == ADD ? "added" : "removed", path); + if(!ret) + log_printf(ERROR, "inotify %s '%s'", cmd_id == ADD ? "added" : "removed", path); - return 0; + return ret; } void process_cmd_listen(const char* param, int fd, client_t* client_lst) @@ -131,12 +150,13 @@ void process_cmd_listen(const char* param, int fd, client_t* client_lst) log_printf(DEBUG, "listener %d requests %s messages", fd, param ? param:"all"); } else { + log_printf(ERROR, "unable to add listener %d", fd); send_response(fd, "Error: listen: client not found in client list?!"); } } -int process_cmd(const char* cmd, int fd, int inotify_fd, client_t* client_lst, options_t* opt) +int process_cmd(const char* cmd, int fd, int inotify_fd, watch_list_t* watch_lst, client_t* client_lst, options_t* opt) { log_printf(DEBUG, "processing command from %d", fd); @@ -186,7 +206,7 @@ int process_cmd(const char* cmd, int fd, int inotify_fd, client_t* client_lst, o switch(cmd_id) { case ADD: case REMOVE: { - int ret = process_cmd_add_remove(cmd_id, param, fd, inotify_fd); + int ret = process_cmd_add_remove(cmd_id, param, fd, inotify_fd, watch_lst); if(ret) return ret; break; @@ -219,6 +239,9 @@ int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt) read_buffer_t switch_buffer; switch_buffer.offset = 0; + watch_list_t watch_lst; + watch_list_init(&watch_lst); + int sig_fd = signal_init(); if(sig_fd < 0) return -1; @@ -268,7 +291,7 @@ int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt) client_t* lst = client_lst; while(lst) { if(FD_ISSET(lst->fd, &tmpfds)) { - return_value = nonblock_recvline(&(lst->buffer), lst->fd, inotify_fd, client_lst, opt); + return_value = nonblock_recvline(&(lst->buffer), lst->fd, inotify_fd, &watch_lst, client_lst, opt); if(return_value == 2) { log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd); client_t* deletee = lst; @@ -286,6 +309,7 @@ int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt) } } + watch_list_clear(&watch_lst); client_clear(&client_lst); signal_stop(); return return_value; diff --git a/utils.c b/utils.c index d25f7bc..e78e8ae 100644 --- a/utils.c +++ b/utils.c @@ -111,7 +111,7 @@ int send_string(int fd, const char* string) return ret; } -int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, client_t* client_lst, options_t* opt) +int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, watch_list_t* watch_lst, client_t* client_lst, options_t* opt) { int ret = 0; for(;;) { @@ -125,7 +125,7 @@ int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, client_t* c if(buffer->buf[buffer->offset] == '\n') { buffer->buf[buffer->offset] = 0; - ret = process_cmd(buffer->buf, fd, inotify_fd, client_lst, opt); + ret = process_cmd(buffer->buf, fd, inotify_fd, watch_lst, client_lst, opt); buffer->offset = 0; break; } diff --git a/utils.h b/utils.h index c601062..b3cebc1 100644 --- a/utils.h +++ b/utils.h @@ -23,13 +23,14 @@ #define RHDROPBOX_utils_h_INCLUDED #include "client_list.h" +#include "watch_list.h" #include "options.h" #include int init_command_socket(const char* path); int connect_command_socket(const char* path); int send_string(int fd, const char* string); -int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, client_t* client_lst, options_t* opt); +int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, watch_list_t* watch_lst, client_t* client_lst, options_t* opt); int create_inotify(); #endif diff --git a/watch_list.c b/watch_list.c index 4339b85..28ca97d 100644 --- a/watch_list.c +++ b/watch_list.c @@ -88,6 +88,38 @@ int watch_list_add(watch_list_t* list, int watch_fd, const char* string) return 0; } +void watch_list_rm(watch_list_t* list, const char* path) +{ + if(!list) + return; + + watch_list_element_t* tmp = NULL; + if(list->first_->path_ && !strcmp(path, list->first_->path_)) { + tmp = list->first_; + list->first_ = list->first_->next_; + + if(tmp->path_) + free(tmp->path_); + free(tmp); + return; + } + + watch_list_element_t* prev = list->first_; + tmp = list->first_->next_; + while(tmp) { + if(tmp->path_ && !strcmp(path, tmp->path_)) { + prev->next_ = tmp->next_; + + if(tmp->path_) + free(tmp->path_); + free(tmp); + return; + } + prev = tmp; + tmp = tmp->next_; + } +} + char* watch_list_find_path(watch_list_t* list, int watch_fd) { if(!list) diff --git a/watch_list.h b/watch_list.h index 5a695e6..bd428ab 100644 --- a/watch_list.h +++ b/watch_list.h @@ -37,6 +37,7 @@ typedef struct watch_list_struct watch_list_t; void watch_list_init(watch_list_t* list); void watch_list_clear(watch_list_t* list); int watch_list_add(watch_list_t* list, int watch_fd, const char* path); +void watch_list_rm(watch_list_t* list, const char* path); char* watch_list_find_path(watch_list_t* list, int watch_fd); int watch_list_find_fd(watch_list_t* list, const char* path); -- cgit v0.10.2