From: Frank Lahm Date: Fri, 12 Aug 2011 12:25:42 +0000 (+0200) Subject: Some cleanup in FCE coalesce. Use bitflags instead of strings. X-Git-Url: https://arthur.barton.de/gitweb/?a=commitdiff_plain;h=e88619d48cc318ae02508037fbee51a38b74a787;p=netatalk.git Some cleanup in FCE coalesce. Use bitflags instead of strings. --- diff --git a/etc/afpd/fce_api_internal.h b/etc/afpd/fce_api_internal.h index d73b775a..a23cfdf8 100644 --- a/etc/afpd/fce_api_internal.h +++ b/etc/afpd/fce_api_internal.h @@ -14,6 +14,10 @@ #define FCE_HISTORY_LEN 10 /* This is used to coalesce events */ #define MAX_COALESCE_TIME_MS 1000 /* Events oldeer than this are not coalesced */ +#define FCE_COALESCE_CREATE (1 << 0) +#define FCE_COALESCE_DELETE (1 << 1) +#define FCE_COALESCE_ALL (FCE_COALESCE_CREATE | FCE_COALESCE_DELETE) + struct udp_entry { int sock; diff --git a/etc/afpd/fce_util.c b/etc/afpd/fce_util.c index 6959509d..f1855305 100644 --- a/etc/afpd/fce_util.c +++ b/etc/afpd/fce_util.c @@ -64,12 +64,9 @@ #define FCE_FALSE 0 /* We store our connection data here */ -static char coalesce[80] = {""}; +static uint32_t coalesce = 0; static struct fce_history fce_history_list[FCE_HISTORY_LEN]; - - - /**** * With coalesce we try to reduce the events over UDP, the eventlistener would throw these * events away anyway. @@ -81,30 +78,6 @@ static struct fce_history fce_history_list[FCE_HISTORY_LEN]; * event timeout. * ****/ -static int coalesce_none() -{ - return coalesce[0] == 0; -} -static int coalesce_all() -{ - return !strcmp( coalesce, "all" ); -} -static int coalesce_create() -{ - return !strcmp( coalesce, "create" ) || coalesce_all(); -} -static int coalesce_delete() -{ - return !strcmp( coalesce, "delete" ) || coalesce_all(); -} - -void fce_initialize_history() -{ - for (int i = 0; i < FCE_HISTORY_LEN; i++) - { - memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) ); - } -} static long get_ms_difftime ( struct timeval *tv1, struct timeval *tv2 ) { @@ -114,53 +87,51 @@ static long get_ms_difftime ( struct timeval *tv1, struct timeval *tv2 ) return s * 1000 + us/1000; } -int fce_handle_coalescation( char *path, int is_dir, int mode ) -{ - if (coalesce_none()) - return FALSE; - - +/****************************************************************************** + * Public functions follow + ******************************************************************************/ - // First one: - // After a file creation *ALWAYS* a file modification is produced - if (mode == FCE_FILE_CREATE) - { - if (coalesce_create()) - { - return TRUE; - } +void fce_initialize_history() +{ + for (int i = 0; i < FCE_HISTORY_LEN; i++) { + memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) ); } +} - /* get timestamp */ - struct timeval tv; - gettimeofday(&tv, 0); - - +int fce_handle_coalescation( char *path, int is_dir, int mode ) +{ /* These two are used to eval our next index in history */ /* the history is unsorted, speed should not be a problem, length is 10 */ unsigned long oldest_entry = (unsigned long )((long)-1); int oldest_entry_idx = -1; + struct timeval tv; + + if (coalesce == 0) + return FALSE; + + /* After a file creation *ALWAYS* a file modification is produced */ + if ((mode == FCE_FILE_CREATE) && (coalesce & FCE_COALESCE_CREATE)) + return TRUE; + + /* get timestamp */ + gettimeofday(&tv, 0); /* Now detect events in the very near history */ - for (int i = 0; i < FCE_HISTORY_LEN; i++) - { + for (int i = 0; i < FCE_HISTORY_LEN; i++) { struct fce_history *fh = &fce_history_list[i]; - //* Not inited ? */ - if (fh->tv.tv_sec == 0) - { + /* Not inited ? */ + if (fh->tv.tv_sec == 0) { /* we can use it for new elements */ oldest_entry = 0; oldest_entry_idx = i; continue; } - //* Too old ? */ - if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS) - { + /* Too old ? */ + if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS) { /* Invalidate entry */ fh->tv.tv_sec = 0; - oldest_entry = 0; oldest_entry_idx = i; continue; @@ -168,28 +139,23 @@ int fce_handle_coalescation( char *path, int is_dir, int mode ) /* If we find a parent dir wich was created we are done */ - if (coalesce_create() && fh->mode == FCE_DIR_CREATE) - { - //* Parent dir ? */ - if (!strncmp( fh->path, path, strlen( fh->path ) ) ) - { + if ((coalesce & FCE_COALESCE_CREATE) && (fh->mode == FCE_DIR_CREATE)) { + /* Parent dir ? */ + if (!strncmp(fh->path, path, strlen(fh->path))) return TRUE; - } } /* If we find a parent dir we should be DELETED we are done */ - if (coalesce_delete() && fh->is_dir && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE)) - { - //* Parent dir ? */ - if (!strncmp( fh->path, path, strlen( fh->path ) ) ) - { + if ((coalesce & FCE_COALESCE_DELETE) + && fh->is_dir + && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE)) { + /* Parent dir ? */ + if (!strncmp(fh->path, path, strlen(fh->path))) return TRUE; - } } - //* Detect oldest entry for next new entry */ - if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry) - { + /* Detect oldest entry for next new entry */ + if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry) { oldest_entry = fh->tv.tv_sec; oldest_entry_idx = i; } @@ -206,16 +172,31 @@ int fce_handle_coalescation( char *path, int is_dir, int mode ) } /* - * * Set event coalescation to reduce number of events sent over UDP * all|delete|create - * - * - * */ + */ -int fce_set_coalesce( char *coalesce_opt ) +int fce_set_coalesce(char *opt) { - strncpy( coalesce, coalesce_opt, sizeof(coalesce) - 1 ); + char *e; + char *p; + + if (opt == NULL) + return AFPERR_PARAM; + + e = strdup(opt); + + for (p = strtok(e, ","); p; p = strtok(NULL, ",")) { + if (strcmp(p, "all") == 0) { + coalesce = FCE_COALESCE_ALL; + } else if (strcmp(p, "delete") == 0) { + coalesce = FCE_COALESCE_DELETE; + } else if (strcmp(p, "create") == 0) { + coalesce = FCE_COALESCE_CREATE; + } + } + + free(e); }