]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/fce_util.c
Merge branch-allea
[netatalk.git] / etc / afpd / fce_util.c
1 /*
2  * Copyright (c) 2010 Mark Williams
3  *
4  * File change event API for netatalk
5  *
6  * for every detected filesystem change a UDP packet is sent to an arbitrary list
7  * of listeners. Each packet contains unix path of modified filesystem element,
8  * event reason, and a consecutive event id (32 bit). Technically we are UDP client and are sending
9  * out packets synchronuosly as they are created by the afp functions. This should not affect
10  * performance measurably. The only delaying calls occur during initialization, if we have to
11  * resolve non-IP hostnames to IP. All numeric data inside the packet is network byte order, so use
12  * ntohs / ntohl to resolve length and event id. Ideally a listener receives every packet with
13  * no gaps in event ids, starting with event id 1 and mode FCE_CONN_START followed by
14  * data events from id 2 up to 0xFFFFFFFF, followed by 0 to 0xFFFFFFFF and so on.
15  *
16  * A gap or not starting with 1 mode FCE_CONN_START or receiving mode FCE_CONN_BROKEN means that
17  * the listener has lost at least one filesystem event
18  * 
19  * All Rights Reserved.  See COPYRIGHT.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif /* HAVE_CONFIG_H */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <time.h>
31 #include <stdbool.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37
38 #include <atalk/adouble.h>
39 #include <atalk/vfs.h>
40 #include <atalk/logger.h>
41 #include <atalk/afp.h>
42 #include <atalk/util.h>
43 #include <atalk/cnid.h>
44 #include <atalk/unix.h>
45 #include <atalk/fce_api.h>
46 #include <atalk/globals.h>
47
48 #include "fork.h"
49 #include "file.h"
50 #include "directory.h"
51 #include "desktop.h"
52 #include "volume.h"
53
54 // ONLY USED IN THIS FILE
55 #include "fce_api_internal.h"
56
57 #define FCE_TRUE 1
58 #define FCE_FALSE 0
59
60 /* We store our connection data here */
61 static uint32_t coalesce = 0;
62 static struct fce_history fce_history_list[FCE_HISTORY_LEN];
63
64 /****
65 * With coalesce we try to reduce the events over UDP, the eventlistener would throw these 
66 * events away anyway.
67 * This works only, if the connected listener uses the events on a "per directory" base
68 * It is a very simple aproach, but saves a lot of events sent to listeners.
69 * Every "child element" event is ignored as long as its parent event is not older 
70 * than MAX_COALESCE_TIME_MS ms. If large directory trees or large files are created or deleted, 
71 * this probably will not work recursive, because the time to copy data will exceed this 
72 * event timeout. 
73
74 ****/
75
76 static long get_ms_difftime (  struct timeval *tv1, struct timeval *tv2 )
77 {
78         unsigned long s = tv2->tv_sec - tv1->tv_sec;
79         long us = tv2->tv_usec - tv1->tv_usec;
80
81         return s * 1000 + us/1000;
82 }
83
84 /******************************************************************************
85  * Public functions follow
86  ******************************************************************************/
87
88 void fce_initialize_history()
89 {
90         for (int i = 0; i < FCE_HISTORY_LEN; i++) {
91                 memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) );
92         }
93 }
94
95 bool fce_handle_coalescation( char *path, int is_dir, int mode )
96 {
97         /* These two are used to eval our next index in history */
98         /* the history is unsorted, speed should not be a problem, length is 10 */
99         unsigned long oldest_entry = (unsigned long )((long)-1);
100         int oldest_entry_idx = -1;
101         struct timeval tv;
102
103         if (coalesce == 0)
104                 return false;
105
106         /* After a file creation *ALWAYS* a file modification is produced */
107         if ((mode == FCE_FILE_CREATE) && (coalesce & FCE_COALESCE_CREATE))
108         return true;
109
110         /* get timestamp */
111         gettimeofday(&tv, 0);
112
113         /* Now detect events in the very near history */
114         for (int i = 0; i < FCE_HISTORY_LEN; i++) {
115                 struct fce_history *fh = &fce_history_list[i];
116
117                 /* Not inited ? */
118                 if (fh->tv.tv_sec == 0) {
119                         /* we can use it for new elements */
120                         oldest_entry = 0;
121                         oldest_entry_idx = i;
122                         continue;
123                 }
124
125                 /* Too old ? */
126                 if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS) {
127                         /* Invalidate entry */
128                         fh->tv.tv_sec = 0;
129                         oldest_entry = 0;
130                         oldest_entry_idx = i;                   
131                         continue;
132                 }
133
134
135                 /* If we find a parent dir wich was created we are done */
136                 if ((coalesce & FCE_COALESCE_CREATE) && (fh->mode == FCE_DIR_CREATE)) {
137                         /* Parent dir ? */
138                         if (!strncmp(fh->path, path, strlen(fh->path)))
139                                 return true;
140                 }
141
142                 /* If we find a parent dir we should be DELETED we are done */
143                 if ((coalesce & FCE_COALESCE_DELETE)
144             && fh->is_dir
145             && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE)) {
146                         /* Parent dir ? */
147                         if (!strncmp(fh->path, path, strlen(fh->path)))
148                                 return true;
149                 }
150
151                 /* Detect oldest entry for next new entry */
152                 if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry) {
153                         oldest_entry = fh->tv.tv_sec;
154                         oldest_entry_idx = i;
155                 }
156         }
157
158         /* We have a new entry for the history, register it */
159         fce_history_list[oldest_entry_idx].tv = tv;
160         fce_history_list[oldest_entry_idx].mode = mode;
161         fce_history_list[oldest_entry_idx].is_dir = is_dir;
162         strncpy( fce_history_list[oldest_entry_idx].path, path, MAXPATHLEN);
163
164         /* we have to handle this event */
165         return false;
166 }
167
168 /*
169  * Set event coalescation to reduce number of events sent over UDP 
170  * all|delete|create
171  */
172
173 int fce_set_coalesce(const char *opt)
174 {
175     char *e;
176     char *p;
177     
178     if (opt == NULL)
179         return AFPERR_PARAM;
180
181     e = strdup(opt);
182
183     for (p = strtok(e, ","); p; p = strtok(NULL, ",")) {
184         if (strcmp(p, "all") == 0) {
185             coalesce = FCE_COALESCE_ALL;
186         } else if (strcmp(p, "delete") == 0) {
187             coalesce = FCE_COALESCE_DELETE;
188         } else if (strcmp(p, "create") == 0) {
189             coalesce = FCE_COALESCE_CREATE;
190         }
191     }
192
193     free(e);
194
195     return AFP_OK;
196 }
197
198
199
200