]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/fce_api.c
Add a configurable hold time option to FCE file modification event generation, defaul...
[netatalk.git] / etc / afpd / fce_api.c
old mode 100755 (executable)
new mode 100644 (file)
index b2dead2..00a0424
@@ -80,6 +80,7 @@ static const char *skip_files[] =
        ".DS_Store",\r
        NULL\r
 };\r
+static struct fce_close_event last_close_event;\r
 \r
 /*\r
  *\r
@@ -230,13 +231,22 @@ static int pack_fce_packet(struct fce_packet *packet, unsigned char *buf)
  * */\r
 static void send_fce_event( char *path, int mode )\r
 {    \r
+    static int first_event = FCE_TRUE;\r
+\r
     struct fce_packet packet;\r
     void *data = &packet;\r
     static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */\r
+    time_t now = time(NULL);\r
 \r
     LOG(log_debug, logtype_afpd, "send_fce_event: start");\r
 \r
-    time_t now = time(NULL);\r
+    /* initialized ? */\r
+    if (first_event == FCE_TRUE) {\r
+        first_event = FCE_FALSE;\r
+        fce_init_udp();\r
+        /* Notify listeners the we start from the beginning */\r
+        send_fce_event( "", FCE_CONN_START );\r
+    }\r
 \r
     /* build our data packet */\r
     ssize_t data_len = build_fce_packet( &packet, path, mode, ++event_id );\r
@@ -327,6 +337,23 @@ static int add_udp_socket(const char *target_ip, const char *target_port )
     return AFP_OK;\r
 }\r
 \r
+static void save_close_event(const char *path)\r
+{\r
+    time_t now = time(NULL);\r
+\r
+    /* Check if it's a close for the same event as the last one */\r
+    if (last_close_event.time   /* is there any saved event ? */\r
+        && (strcmp(path, last_close_event.path) != 0)) {\r
+        /* no, so send the saved event out now */\r
+        send_fce_event(last_close_event.path, FCE_FILE_MODIFY);\r
+    }\r
+\r
+    LOG(log_debug, logtype_afpd, "save_close_event: %s", path);\r
+\r
+    last_close_event.time = now;\r
+    strncpy(last_close_event.path, path, MAXPATHLEN);\r
+}\r
+\r
 /*\r
  *\r
  * Dispatcher for all incoming file change events\r
@@ -334,6 +361,8 @@ static int add_udp_socket(const char *target_ip, const char *target_port )
  * */\r
 static int register_fce(const char *u_name, int is_dir, int mode)\r
 {\r
+    static int first_event = FCE_TRUE;\r
+\r
     if (udp_sockets == 0)\r
         /* No listeners configured */\r
         return AFP_OK;\r
@@ -341,11 +370,10 @@ static int register_fce(const char *u_name, int is_dir, int mode)
     if (u_name == NULL)\r
         return AFPERR_PARAM;\r
 \r
-    static int first_event = FCE_TRUE;\r
-\r
        /* do some initialization on the fly the first time */\r
        if (first_event) {\r
                fce_initialize_history();\r
+        first_event = FCE_FALSE;\r
        }\r
 \r
        /* handle files which should not cause events (.DS_Store atc. ) */\r
@@ -384,26 +412,29 @@ static int register_fce(const char *u_name, int is_dir, int mode)
 \r
        LOG(log_debug9, logtype_afpd, "Detected fc event <%d> for <%s>", mode, full_path_buffer );\r
 \r
-\r
-    /* we do initilization on the fly, no blocking calls in here \r
-     * (except when using FQDN in broken DNS environment)\r
-     */\r
-    if (first_event == FCE_TRUE)\r
-    {\r
-        fce_init_udp();\r
-        \r
-        /* Notify listeners the we start from the beginning */\r
-        send_fce_event( "", FCE_CONN_START );\r
-        \r
-        first_event = FCE_FALSE;\r
+    if (mode & FCE_FILE_MODIFY) {\r
+        save_close_event(full_path_buffer);\r
+        return AFP_OK;\r
     }\r
 \r
-       /* Handle UDP transport */\r
     send_fce_event( full_path_buffer, mode );\r
 \r
     return AFP_OK;\r
 }\r
 \r
+static void check_saved_close_events(int fmodwait)\r
+{\r
+    time_t now = time(NULL);\r
+\r
+    /* check if configured holdclose time has passed */\r
+    if (last_close_event.time && ((last_close_event.time + fmodwait) < now)) {\r
+        LOG(log_debug, logtype_afpd, "check_saved_close_events: sending event: %s", last_close_event.path);\r
+        /* yes, send event */\r
+        send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY);\r
+        last_close_event.path[0] = 0;\r
+        last_close_event.time = 0;\r
+    }\r
+}\r
 \r
 /******************** External calls start here **************************/\r
 \r
@@ -412,6 +443,12 @@ static int register_fce(const char *u_name, int is_dir, int mode)
  * */\r
 #ifndef FCE_TEST_MAIN\r
 \r
+void fce_pending_events(AFPObj *obj)\r
+{\r
+    vol_fce_tm_event();\r
+    check_saved_close_events(obj->options.fce_fmodwait);\r
+}\r
+\r
 int fce_register_delete_file( struct path *path )\r
 {\r
     int ret = AFP_OK;\r