]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/fce_util.c
Remove atalk/boolean.h, use stdbool.h instead
[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 <netatalk/at.h>
39
40 #include <atalk/adouble.h>
41 #include <atalk/vfs.h>
42 #include <atalk/logger.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/unix.h>
47 #include <atalk/fce_api.h>
48 #include <atalk/globals.h>
49
50 #include "fork.h"
51 #include "file.h"
52 #include "directory.h"
53 #include "desktop.h"
54 #include "volume.h"
55
56 // ONLY USED IN THIS FILE
57 #include "fce_api_internal.h"
58
59 #define FCE_TRUE 1
60 #define FCE_FALSE 0
61
62 /* We store our connection data here */
63 static uint32_t coalesce = 0;
64 static struct fce_history fce_history_list[FCE_HISTORY_LEN];
65
66 /****
67 * With coalesce we try to reduce the events over UDP, the eventlistener would throw these 
68 * events away anyway.
69 * This works only, if the connected listener uses the events on a "per directory" base
70 * It is a very simple aproach, but saves a lot of events sent to listeners.
71 * Every "child element" event is ignored as long as its parent event is not older 
72 * than MAX_COALESCE_TIME_MS ms. If large directory trees or large files are created or deleted, 
73 * this probably will not work recursive, because the time to copy data will exceed this 
74 * event timeout. 
75
76 ****/
77
78 static long get_ms_difftime (  struct timeval *tv1, struct timeval *tv2 )
79 {
80         unsigned long s = tv2->tv_sec - tv1->tv_sec;
81         long us = tv2->tv_usec - tv1->tv_usec;
82
83         return s * 1000 + us/1000;
84 }
85
86 /******************************************************************************
87  * Public functions follow
88  ******************************************************************************/
89
90 void fce_initialize_history()
91 {
92         for (int i = 0; i < FCE_HISTORY_LEN; i++) {
93                 memset( &fce_history_list[i], 0, sizeof(fce_history_list[i]) );
94         }
95 }
96
97 bool fce_handle_coalescation( char *path, int is_dir, int mode )
98 {
99         /* These two are used to eval our next index in history */
100         /* the history is unsorted, speed should not be a problem, length is 10 */
101         unsigned long oldest_entry = (unsigned long )((long)-1);
102         int oldest_entry_idx = -1;
103         struct timeval tv;
104
105         if (coalesce == 0)
106                 return false;
107
108         /* After a file creation *ALWAYS* a file modification is produced */
109         if ((mode == FCE_FILE_CREATE) && (coalesce & FCE_COALESCE_CREATE))
110         return true;
111
112         /* get timestamp */
113         gettimeofday(&tv, 0);
114
115         /* Now detect events in the very near history */
116         for (int i = 0; i < FCE_HISTORY_LEN; i++) {
117                 struct fce_history *fh = &fce_history_list[i];
118
119                 /* Not inited ? */
120                 if (fh->tv.tv_sec == 0) {
121                         /* we can use it for new elements */
122                         oldest_entry = 0;
123                         oldest_entry_idx = i;
124                         continue;
125                 }
126
127                 /* Too old ? */
128                 if (get_ms_difftime( &fh->tv, &tv ) > MAX_COALESCE_TIME_MS) {
129                         /* Invalidate entry */
130                         fh->tv.tv_sec = 0;
131                         oldest_entry = 0;
132                         oldest_entry_idx = i;                   
133                         continue;
134                 }
135
136
137                 /* If we find a parent dir wich was created we are done */
138                 if ((coalesce & FCE_COALESCE_CREATE) && (fh->mode == FCE_DIR_CREATE)) {
139                         /* Parent dir ? */
140                         if (!strncmp(fh->path, path, strlen(fh->path)))
141                                 return true;
142                 }
143
144                 /* If we find a parent dir we should be DELETED we are done */
145                 if ((coalesce & FCE_COALESCE_DELETE)
146             && fh->is_dir
147             && (mode == FCE_FILE_DELETE || mode == FCE_DIR_DELETE)) {
148                         /* Parent dir ? */
149                         if (!strncmp(fh->path, path, strlen(fh->path)))
150                                 return true;
151                 }
152
153                 /* Detect oldest entry for next new entry */
154                 if (oldest_entry_idx == -1 || fh->tv.tv_sec < oldest_entry) {
155                         oldest_entry = fh->tv.tv_sec;
156                         oldest_entry_idx = i;
157                 }
158         }
159
160         /* We have a new entry for the history, register it */
161         fce_history_list[oldest_entry_idx].tv = tv;
162         fce_history_list[oldest_entry_idx].mode = mode;
163         fce_history_list[oldest_entry_idx].is_dir = is_dir;
164         strncpy( fce_history_list[oldest_entry_idx].path, path, MAXPATHLEN);
165
166         /* we have to handle this event */
167         return false;
168 }
169
170 /*
171  * Set event coalescation to reduce number of events sent over UDP 
172  * all|delete|create
173  */
174
175 int fce_set_coalesce(char *opt)
176 {
177     char *e;
178     char *p;
179     
180     if (opt == NULL)
181         return AFPERR_PARAM;
182
183     e = strdup(opt);
184
185     for (p = strtok(e, ","); p; p = strtok(NULL, ",")) {
186         if (strcmp(p, "all") == 0) {
187             coalesce = FCE_COALESCE_ALL;
188         } else if (strcmp(p, "delete") == 0) {
189             coalesce = FCE_COALESCE_DELETE;
190         } else if (strcmp(p, "create") == 0) {
191             coalesce = FCE_COALESCE_CREATE;
192         }
193     }
194
195     free(e);
196 }
197
198
199
200