summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rhdropbox.c45
-rw-r--r--utils.c4
-rw-r--r--utils.h2
3 files changed, 40 insertions, 11 deletions
diff --git a/rhdropbox.c b/rhdropbox.c
index a7b3681..bc8eaf5 100644
--- a/rhdropbox.c
+++ b/rhdropbox.c
@@ -68,10 +68,6 @@ int process_watch(int inotify_fd)
return ret;
}
- log_printf(DEBUG, "received %d bytes from watch", ret);
- return 0;
-}
-
/* client_t* client; */
/* int listener_cnt = 0; */
/* for(client = client_lst; client; client = client->next) */
@@ -81,6 +77,34 @@ int process_watch(int inotify_fd)
/* } */
/* log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); */
+ log_printf(DEBUG, "received %d bytes from watch", ret);
+ return 0;
+}
+
+int process_cmd_add_remove(cmd_id_t cmd_id, const char* path, int fd, int inotify_fd)
+{
+ 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 {
+ log_printf(WARNING, "cmd_add_remove ignoring wrong cmd_id");
+ return 0;
+ }
+ if(wd < 0) {
+ log_printf(ERROR, "inotify %s '%s' failed: %s", cmd_id == ADD ? "adding" : "removing", path, strerror(errno));
+ send_response(fd, "Error: add/remove failed");
+ return 0;
+ }
+
+ // add/remove path to/from list
+
+ log_printf(ERROR, "inotify %s '%s'", cmd_id == ADD ? "added" : "removed", path);
+
+ return 0;
+}
+
void process_cmd_listen(const char* param, int fd, client_t* client_lst)
{
client_t* listener = client_find(client_lst, fd);
@@ -112,7 +136,7 @@ void process_cmd_listen(const char* param, int fd, client_t* client_lst)
}
}
-int process_cmd(const char* cmd, int fd, client_t* client_lst, options_t* opt)
+int process_cmd(const char* cmd, int fd, int inotify_fd, client_t* client_lst, options_t* opt)
{
log_printf(DEBUG, "processing command from %d", fd);
@@ -160,8 +184,13 @@ int process_cmd(const char* cmd, int fd, client_t* client_lst, options_t* opt)
}
switch(cmd_id) {
- case ADD: break;
- case REMOVE: break;
+ case ADD:
+ case REMOVE: {
+ int ret = process_cmd_add_remove(cmd_id, param, fd, inotify_fd);
+ if(ret)
+ return ret;
+ break;
+ }
case STATUS: break;
case LOG: {
if(param && param[0])
@@ -239,7 +268,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, client_lst, opt);
+ return_value = nonblock_recvline(&(lst->buffer), lst->fd, inotify_fd, client_lst, opt);
if(return_value == 2) {
log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd);
client_t* deletee = lst;
diff --git a/utils.c b/utils.c
index a057ca2..d25f7bc 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, client_t* client_lst, options_t* opt)
+int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, client_t* client_lst, options_t* opt)
{
int ret = 0;
for(;;) {
@@ -125,7 +125,7 @@ int nonblock_recvline(read_buffer_t* buffer, int fd, client_t* client_lst, optio
if(buffer->buf[buffer->offset] == '\n') {
buffer->buf[buffer->offset] = 0;
- ret = process_cmd(buffer->buf, fd, client_lst, opt);
+ ret = process_cmd(buffer->buf, fd, inotify_fd, client_lst, opt);
buffer->offset = 0;
break;
}
diff --git a/utils.h b/utils.h
index 500062d..c601062 100644
--- a/utils.h
+++ b/utils.h
@@ -29,7 +29,7 @@
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, client_t* client_lst, options_t* opt);
+int nonblock_recvline(read_buffer_t* buffer, int fd, int inotify_fd, client_t* client_lst, options_t* opt);
int create_inotify();
#endif