]> arthur.barton.de Git - netatalk.git/commitdiff
Some cleanup in FCE coalesce. Use bitflags instead of strings.
authorFrank Lahm <franklahm@googlemail.com>
Fri, 12 Aug 2011 12:25:42 +0000 (14:25 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Fri, 12 Aug 2011 12:25:42 +0000 (14:25 +0200)
etc/afpd/fce_api_internal.h
etc/afpd/fce_util.c

index d73b775a56e19619027df5d431948759ce33db52..a23cfdf8eae76390e4b8b8c633c0346e31d16d64 100644 (file)
 #define FCE_HISTORY_LEN 10  /* This is used to coalesce events */\r
 #define MAX_COALESCE_TIME_MS 1000  /* Events oldeer than this are not coalesced */\r
 \r
+#define FCE_COALESCE_CREATE (1 << 0)\r
+#define FCE_COALESCE_DELETE (1 << 1)\r
+#define FCE_COALESCE_ALL    (FCE_COALESCE_CREATE | FCE_COALESCE_DELETE)\r
+\r
 struct udp_entry\r
 {\r
     int sock;\r
index 6959509df027e3de6825faedb838c74bd7f0ce1c..f185530531d2c795ab633517cb7ff6b8f01e3b65 100644 (file)
 #define FCE_FALSE 0\r
 \r
 /* We store our connection data here */\r
-static char coalesce[80] = {""};\r
+static uint32_t coalesce = 0;\r
 static struct fce_history fce_history_list[FCE_HISTORY_LEN];\r
 \r
-\r
-\r
-\r
 /****\r
 * With coalesce we try to reduce the events over UDP, the eventlistener would throw these \r
 * events away anyway.\r
@@ -81,30 +78,6 @@ static struct fce_history fce_history_list[FCE_HISTORY_LEN];
 * event timeout. \r
 * \r
 ****/\r
-static int coalesce_none()\r
-{\r
-       return coalesce[0] == 0;\r
-}\r
-static int coalesce_all()\r
-{\r
-       return !strcmp( coalesce, "all" );\r
-}\r
-static int coalesce_create()\r
-{\r
-       return !strcmp( coalesce, "create" ) || coalesce_all();\r
-}\r
-static int coalesce_delete()\r
-{\r
-       return !strcmp( coalesce, "delete" ) || coalesce_all();\r
-}\r
-\r
-void fce_initialize_history()\r
-{\r
-       for (int i = 0; i < FCE_HISTORY_LEN; i++)\r
-       {\r
-               memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) );\r
-       }\r
-}\r
 \r
 static long get_ms_difftime (  struct timeval *tv1, struct timeval *tv2 )\r
 {\r
@@ -114,53 +87,51 @@ static long get_ms_difftime (  struct timeval *tv1, struct timeval *tv2 )
        return s * 1000 + us/1000;\r
 }\r
 \r
-int fce_handle_coalescation( char *path, int is_dir, int mode )\r
-{\r
-       if (coalesce_none())\r
-               return FALSE;\r
-\r
-               \r
+/******************************************************************************\r
+ * Public functions follow\r
+ ******************************************************************************/\r
 \r
-       // First one:\r
-       // After a file creation *ALWAYS* a file modification is produced\r
-       if (mode == FCE_FILE_CREATE)\r
-       {\r
-               if (coalesce_create())\r
-               {\r
-                       return TRUE;\r
-               }\r
+void fce_initialize_history()\r
+{\r
+       for (int i = 0; i < FCE_HISTORY_LEN; i++) {\r
+               memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) );\r
        }\r
+}\r
 \r
-       /* get timestamp */\r
-       struct timeval tv;\r
-       gettimeofday(&tv, 0);\r
-\r
-\r
+int fce_handle_coalescation( char *path, int is_dir, int mode )\r
+{\r
        /* These two are used to eval our next index in history */\r
        /* the history is unsorted, speed should not be a problem, length is 10 */\r
        unsigned long oldest_entry = (unsigned long )((long)-1);\r
        int oldest_entry_idx = -1;\r
+       struct timeval tv;\r
+\r
+       if (coalesce == 0)\r
+               return FALSE;\r
+\r
+       /* After a file creation *ALWAYS* a file modification is produced */\r
+       if ((mode == FCE_FILE_CREATE) && (coalesce & FCE_COALESCE_CREATE))\r
+        return TRUE;\r
+\r
+       /* get timestamp */\r
+       gettimeofday(&tv, 0);\r
 \r
        /* Now detect events in the very near history */\r
-       for (int i = 0; i < FCE_HISTORY_LEN; i++)\r
-       {\r
+       for (int i = 0; i < FCE_HISTORY_LEN; i++) {\r
                struct fce_history *fh = &fce_history_list[i];\r
 \r
-               //* Not inited ? */\r
-               if (fh->tv.tv_sec == 0)\r
-               {\r
+               /* Not inited ? */\r
+               if (fh->tv.tv_sec == 0) {\r
                        /* we can use it for new elements */\r
                        oldest_entry = 0;\r
                        oldest_entry_idx = i;\r
                        continue;\r
                }\r
 \r
-               //* Too old ? */\r
-               if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS)\r
-               {\r
+               /* Too old ? */\r
+               if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS) {\r
                        /* Invalidate entry */\r
                        fh->tv.tv_sec = 0;\r
-\r
                        oldest_entry = 0;\r
                        oldest_entry_idx = i;                   \r
                        continue;\r
@@ -168,28 +139,23 @@ int fce_handle_coalescation( char *path, int is_dir, int mode )
 \r
 \r
                /* If we find a parent dir wich was created we are done */\r
-               if (coalesce_create() && fh->mode == FCE_DIR_CREATE)\r
-               {\r
-                       //* Parent dir ? */\r
-                       if (!strncmp( fh->path, path, strlen( fh->path ) ) )\r
-                       {\r
+               if ((coalesce & FCE_COALESCE_CREATE) && (fh->mode == FCE_DIR_CREATE)) {\r
+                       /* Parent dir ? */\r
+                       if (!strncmp(fh->path, path, strlen(fh->path)))\r
                                return TRUE;\r
-                       }\r
                }\r
 \r
                /* If we find a parent dir we should be DELETED we are done */\r
-               if (coalesce_delete() && fh->is_dir && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE))\r
-               {\r
-                       //* Parent dir ? */\r
-                       if (!strncmp( fh->path, path, strlen( fh->path ) ) )\r
-                       {\r
+               if ((coalesce & FCE_COALESCE_DELETE)\r
+            && fh->is_dir\r
+            && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE)) {\r
+                       /* Parent dir ? */\r
+                       if (!strncmp(fh->path, path, strlen(fh->path)))\r
                                return TRUE;\r
-                       }\r
                }\r
 \r
-               //* Detect oldest entry for next new entry */\r
-               if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry)\r
-               {\r
+               /* Detect oldest entry for next new entry */\r
+               if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry) {\r
                        oldest_entry = fh->tv.tv_sec;\r
                        oldest_entry_idx = i;\r
                }\r
@@ -206,16 +172,31 @@ int fce_handle_coalescation( char *path, int is_dir, int mode )
 }\r
 \r
 /*\r
- *\r
  * Set event coalescation to reduce number of events sent over UDP \r
  * all|delete|create\r
- *\r
- *\r
- * */\r
+ */\r
 \r
-int fce_set_coalesce( char *coalesce_opt )\r
+int fce_set_coalesce(char *opt)\r
 {\r
-       strncpy( coalesce, coalesce_opt, sizeof(coalesce) - 1 ); \r
+    char *e;\r
+    char *p;\r
+    \r
+    if (opt == NULL)\r
+        return AFPERR_PARAM;\r
+\r
+    e = strdup(opt);\r
+\r
+    for (p = strtok(e, ","); p; p = strtok(NULL, ",")) {\r
+        if (strcmp(p, "all") == 0) {\r
+            coalesce = FCE_COALESCE_ALL;\r
+        } else if (strcmp(p, "delete") == 0) {\r
+            coalesce = FCE_COALESCE_DELETE;\r
+        } else if (strcmp(p, "create") == 0) {\r
+            coalesce = FCE_COALESCE_CREATE;\r
+        }\r
+    }\r
+\r
+    free(e);\r
 }\r
 \r
 \r