summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2009-11-20 14:03:35 (GMT)
committerChristian Pointner <equinox@helsinki.at>2009-11-20 14:03:35 (GMT)
commit16d3ccb1dd701665b1b138a48b4701108c141361 (patch)
treefa080ac44f2cba887e84f5343dc507082f60d75b
parente0985752f62c6bb3508cfd1b3d19eebbc195f99d (diff)
added event reading
-rw-r--r--datatypes.h2
-rw-r--r--rhdropbox.c53
2 files changed, 41 insertions, 14 deletions
diff --git a/datatypes.h b/datatypes.h
index 36d018b..1168ec6 100644
--- a/datatypes.h
+++ b/datatypes.h
@@ -42,7 +42,7 @@ typedef struct buffer_struct buffer_t;
struct read_buffer_struct {
u_int32_t offset;
- u_int8_t buf[100];
+ u_int8_t buf[1024];
};
typedef struct read_buffer_struct read_buffer_t;
diff --git a/rhdropbox.c b/rhdropbox.c
index bbf4509..2478616 100644
--- a/rhdropbox.c
+++ b/rhdropbox.c
@@ -35,6 +35,7 @@
#include "daemon.h"
#include "utils.h"
+#include "sysexec.h"
enum cmd_id_enum { ADD, REMOVE, STATUS, LOG, LISTEN };
typedef enum cmd_id_enum cmd_id_t;
@@ -58,16 +59,29 @@ int send_response(int fd, const char* response)
return ret;
}
-int process_watch(int inotify_fd)
+int process_watch(int inotify_fd, read_buffer_t* buffer, options_t* opt)
{
- log_printf(INFO, "something changed on watch descriptor '%d'", inotify_fd);
- u_int8_t buf[1000];
- int ret = read(inotify_fd, buf, 1000);
- if(ret < 0) {
- log_printf(ERROR, "read inotify_fd error: %s", strerror(errno));
- return ret;
- }
+ log_printf(DEBUG, "something changed on watch descriptor '%d'", inotify_fd);
+
+ int ret = 0;
+ for(;;) {
+ ret = read(inotify_fd, &buffer->buf[buffer->offset], sizeof(buffer->buf) - buffer->offset);
+ if(!ret) {
+ log_printf(ERROR, "read from inotfiy fd returned end of file!?");
+ return -1;
+ }
+ if(ret == -1 && errno == EAGAIN)
+ return 0;
+ else if(ret < 0)
+ return ret;
+ buffer->offset += ret;
+ for(;;) {
+ if(buffer->offset >= sizeof(struct inotify_event)) {
+ struct inotify_event* event;
+ event = (struct inotify_event*)buffer->buf;
+ u_int32_t len = sizeof(struct inotify_event) + event->len;
+ if(buffer->offset >= len) {
/* client_t* client; */
/* int listener_cnt = 0; */
/* for(client = client_lst; client; client = client->next) */
@@ -76,8 +90,21 @@ int process_watch(int inotify_fd)
/* listener_cnt++; */
/* } */
/* log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); */
-
- log_printf(DEBUG, "received %d bytes from watch", ret);
+ log_printf(DEBUG, "received event for %d (mask=0x%08X, cookie=0x%08X name[%u]='%s')",
+ event->wd, event->mask, event->cookie, event->len, event->len > 0 ? event->name : "");
+
+ if(buffer->offset > len) {
+ memmove(buffer->buf, &buffer->buf[len], buffer->offset - len);
+ buffer->offset -= len;
+ }
+ else
+ buffer->offset = 0;
+ }
+ }
+ else
+ break;
+ }
+ }
return 0;
}
@@ -254,8 +281,8 @@ int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt)
int max_fd = (cmd_listen_fd < inotify_fd) ? inotify_fd : cmd_listen_fd;
client_t* client_lst = NULL;
- read_buffer_t switch_buffer;
- switch_buffer.offset = 0;
+ read_buffer_t event_buffer;
+ event_buffer.offset = 0;
watch_list_t watch_lst;
watch_list_init(&watch_lst);
@@ -287,7 +314,7 @@ int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt)
}
if(FD_ISSET(inotify_fd, &tmpfds)) {
- return_value = process_watch(inotify_fd);
+ return_value = process_watch(inotify_fd, &event_buffer, opt);
if(return_value)
break;
}