]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Merge master
[netatalk.git] / etc / afpd / ofork.c
1 /*
2  * Copyright (c) 1996 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <string.h>
16 #include <sys/stat.h> /* works around a bug */
17 #include <sys/param.h>
18 #include <errno.h>
19
20 #include <atalk/logger.h>
21 #include <atalk/util.h>
22 #include <atalk/bstrlib.h>
23 #include <atalk/bstradd.h>
24 #include <atalk/globals.h>
25 #include <atalk/fce_api.h>
26
27 #include "volume.h"
28 #include "directory.h"
29 #include "fork.h"
30
31 /* we need to have a hashed list of oforks (by dev inode) */
32 #define OFORK_HASHSIZE  64
33 static struct ofork *ofork_table[OFORK_HASHSIZE]; /* forks hashed by dev/inode */
34 static struct ofork **oforks = NULL;              /* point to allocated table of open forks pointers */
35 static int          nforks = 0;
36 static u_short      lastrefnum = 0;
37
38
39 /* OR some of each character for the hash*/
40 static unsigned long hashfn(const struct file_key *key)
41 {
42     return key->inode & (OFORK_HASHSIZE - 1);
43 }
44
45 static void of_hash(struct ofork *of)
46 {
47     struct ofork **table;
48
49     table = &ofork_table[hashfn(&of->key)];
50     if ((of->next = *table) != NULL)
51         (*table)->prevp = &of->next;
52     *table = of;
53     of->prevp = table;
54 }
55
56 static void of_unhash(struct ofork *of)
57 {
58     if (of->prevp) {
59         if (of->next)
60             of->next->prevp = of->prevp;
61         *(of->prevp) = of->next;
62     }
63 }
64
65 int of_flush(const struct vol *vol)
66 {
67     int refnum;
68
69     if (!oforks)
70         return 0;
71
72     for ( refnum = 0; refnum < nforks; refnum++ ) {
73         if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
74             flushfork( oforks[ refnum ] ) < 0 ) {
75             LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
76         }
77     }
78     return( 0 );
79 }
80
81 int of_rename(const struct vol *vol,
82               struct ofork *s_of,
83               struct dir *olddir, const char *oldpath _U_,
84               struct dir *newdir, const char *newpath)
85 {
86     struct ofork *of, *next;
87     int done = 0;
88
89     if (!s_of)
90         return AFP_OK;
91
92     next = ofork_table[hashfn(&s_of->key)];
93     while ((of = next)) {
94         next = next->next; /* so we can unhash and still be all right. */
95
96         if (vol == of->of_vol
97             && olddir->d_did == of->of_did
98             && s_of->key.dev == of->key.dev
99             && s_of->key.inode == of->key.inode ) {
100             if (!done) {
101                 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
102                 done = 1;
103             }
104             if (newdir != olddir)
105                 of->of_did = newdir->d_did;
106         }
107     }
108
109     return AFP_OK;
110 }
111
112 #define min(a,b)    ((a)<(b)?(a):(b))
113
114 struct ofork *
115 of_alloc(struct vol *vol,
116          struct dir    *dir,
117          char      *path,
118          uint16_t     *ofrefnum,
119          const int      eid,
120          struct adouble *ad,
121          struct stat    *st)
122 {
123     struct ofork        *of;
124     uint16_t       refnum, of_refnum;
125
126     int         i;
127
128     if (!oforks) {
129         nforks = getdtablesize() - 10;
130         /* protect against insane ulimit -n */
131         nforks = min(nforks, 0xffff);
132         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
133         if (!oforks)
134             return NULL;
135     }
136
137     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
138         /* cf AFP3.0.pdf, File fork page 40 */
139         if (!refnum)
140             refnum++;
141         if ( oforks[ refnum % nforks ] == NULL ) {
142             break;
143         }
144     }
145     /* grr, Apple and their 'uniquely identifies'
146        the next line is a protection against
147        of_alloc()
148        refnum % nforks = 3
149        lastrefnum = 3
150        oforks[3] != NULL
151        refnum = 4
152        oforks[4] == NULL
153        return 4
154
155        close(oforks[4])
156
157        of_alloc()
158        refnum % nforks = 4
159        ...
160        return 4
161        same if lastrefnum++ rather than ++lastrefnum.
162     */
163     lastrefnum = refnum;
164     if ( i == nforks ) {
165         LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
166         return( NULL );
167     }
168
169     of_refnum = refnum % nforks;
170     if (( oforks[ of_refnum ] =
171           (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
172         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
173         return NULL;
174     }
175     of = oforks[of_refnum];
176
177     /* see if we need to allocate space for the adouble struct */
178     if (!ad) {
179         ad = malloc( sizeof( struct adouble ) );
180         if (!ad) {
181             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
182             free(of);
183             oforks[ of_refnum ] = NULL;
184             return NULL;
185         }
186
187         /* initialize to zero. This is important to ensure that
188            ad_open really does reinitialize the structure. */
189         ad_init(ad, vol);
190
191         ad->ad_m_namelen = UTF8FILELEN_EARLY +1;
192         /* here's the deal: we allocate enough for the standard mac file length.
193          * in the future, we'll reallocate in fairly large jumps in case
194          * of long unicode names */
195         if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
196             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
197             free(ad);
198             free(of);
199             oforks[ of_refnum ] = NULL;
200             return NULL;
201         }
202         strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
203     } else {
204         /* Increase the refcount on this struct adouble. This is
205            decremented again in oforc_dealloc. */
206         ad_ref(ad);
207     }
208
209     of->of_ad = ad;
210     of->of_vol = vol;
211     of->of_did = dir->d_did;
212
213     *ofrefnum = refnum;
214     of->of_refnum = refnum;
215     of->key.dev = st->st_dev;
216     of->key.inode = st->st_ino;
217     if (eid == ADEID_DFORK)
218         of->of_flags = AFPFORK_DATA;
219     else
220         of->of_flags = AFPFORK_RSRC;
221
222     of_hash(of);
223     return( of );
224 }
225
226 struct ofork *of_find(const uint16_t ofrefnum )
227 {
228     if (!oforks || !nforks)
229         return NULL;
230
231     return( oforks[ ofrefnum % nforks ] );
232 }
233
234 /* -------------------------- */
235 int of_stat(struct path *path)
236 {
237     int ret;
238
239     path->st_errno = 0;
240     path->st_valid = 1;
241
242     if ((ret = lstat(path->u_name, &path->st)) < 0) {
243         LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
244             cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
245         path->st_errno = errno;
246     }
247
248     return ret;
249 }
250
251
252 #ifdef HAVE_ATFUNCS
253 int of_fstatat(int dirfd, struct path *path)
254 {
255     int ret;
256
257     path->st_errno = 0;
258     path->st_valid = 1;
259
260     if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
261         path->st_errno = errno;
262
263    return ret;
264 }
265 #endif /* HAVE_ATFUNCS */
266
267 /* -------------------------- 
268    stat the current directory.
269    stat(".") works even if "." is deleted thus
270    we have to stat ../name because we want to know if it's there
271 */
272 int of_statdir(struct vol *vol, struct path *path)
273 {
274     static char pathname[ MAXPATHLEN + 1] = "../";
275     int ret;
276     size_t len;
277     struct dir *dir;
278
279     if (*path->m_name) {
280         /* not curdir */
281         return of_stat (path);
282     }
283     path->st_errno = 0;
284     path->st_valid = 1;
285     /* FIXME, what about: we don't have r-x perm anymore ? */
286     len = blength(path->d_dir->d_u_name);
287     if (len > (MAXPATHLEN - 3))
288         len = MAXPATHLEN - 3;
289     strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
290
291     LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
292
293     if (!(ret = lstat(pathname, &path->st)))
294         return 0;
295
296     path->st_errno = errno;
297
298     /* hmm, can't stat curdir anymore */
299     if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
300        if (movecwd(vol, dir)) 
301            return -1;
302        path->st_errno = 0;
303
304        if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0) 
305            path->st_errno = errno;
306     }
307
308     return ret;
309 }
310
311 /* -------------------------- */
312 struct ofork *of_findname(struct path *path)
313 {
314     struct ofork *of;
315     struct file_key key;
316
317     if (!path->st_valid) {
318         of_stat(path);
319     }
320
321     if (path->st_errno)
322         return NULL;
323
324     key.dev = path->st.st_dev;
325     key.inode = path->st.st_ino;
326
327     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
328         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
329             return of;
330         }
331     }
332
333     return NULL;
334 }
335
336 /*!
337  * @brief Search for open fork by dirfd/name
338  *
339  * Function call of_fstatat with dirfd and path and uses dev and ino
340  * to search the open fork table.
341  *
342  * @param dirfd     (r) directory fd
343  * @param path      (rw) pointer to struct path
344  */
345 #ifdef HAVE_ATFUNCS
346 struct ofork *of_findnameat(int dirfd, struct path *path)
347 {
348     struct ofork *of;
349     struct file_key key;
350     
351     if ( ! path->st_valid) {
352         of_fstatat(dirfd, path);
353     }
354         
355     if (path->st_errno)
356         return NULL;
357
358     key.dev = path->st.st_dev;
359     key.inode = path->st.st_ino;
360
361     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
362         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
363             return of;
364         }
365     }
366
367     return NULL;
368 }
369 #endif
370
371 void of_dealloc( struct ofork *of)
372 {
373     if (!oforks)
374         return;
375
376     of_unhash(of);
377     oforks[ of->of_refnum % nforks ] = NULL;
378
379     /* decrease refcount */
380     of->of_ad->ad_refcount--;
381
382     if ( of->of_ad->ad_refcount <= 0) {
383         free( of->of_ad->ad_m_name );
384         free( of->of_ad);
385     } else {/* someone's still using it. just free this user's locks */
386         ad_unlock(of->of_ad, of->of_refnum);
387     }
388
389     free( of );
390 }
391
392 /* --------------------------- */
393 int of_closefork(struct ofork *ofork)
394 {
395     struct timeval      tv;
396     int         adflags, doflush = 0;
397     int                 ret;
398
399     adflags = 0;
400     if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
401         adflags |= ADFLAGS_DF;
402     }
403     if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
404         adflags |= ADFLAGS_HF;
405         /*
406          * Only set the rfork's length if we're closing the rfork.
407          */
408         if ((ofork->of_flags & AFPFORK_RSRC)) {
409             ad_refresh( ofork->of_ad );
410             if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
411                 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
412                 doflush++;
413             }
414             if ( doflush ) {
415                 ad_flush( ofork->of_ad );
416             }
417         }
418     }
419
420     /* Somone has used write_fork, we assume file was changed, register it to file change event api */
421     if (ofork->of_flags & AFPFORK_MODIFIED) {
422         fce_register_file_modification(ofork);
423     }
424
425     ret = 0;
426     if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
427         ret = -1;
428     }
429
430     of_dealloc( ofork );
431     return ret;
432 }
433
434 /* ----------------------
435
436  */
437 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
438 {
439     struct ofork        *of;
440     struct adouble      *adp;
441
442     if ((of = of_findname(path))) {
443         adp = of->of_ad;
444     } else {
445         ad_init(ad, vol);
446         adp = ad;
447     }
448     return adp;
449 }
450
451 /* ----------------------
452    close all forks for a volume
453 */
454 void of_closevol(const struct vol *vol)
455 {
456     int refnum;
457
458     if (!oforks)
459         return;
460
461     for ( refnum = 0; refnum < nforks; refnum++ ) {
462         if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
463             if (of_closefork( oforks[ refnum ]) < 0 ) {
464                 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
465             }
466         }
467     }
468     return;
469 }
470
471 /* ----------------------
472    close all forks for a volume
473 */
474 void of_close_all_forks(void)
475 {
476     int refnum;
477
478     if (!oforks)
479         return;
480
481     for ( refnum = 0; refnum < nforks; refnum++ ) {
482         if (oforks[ refnum ] != NULL) {
483             if (of_closefork( oforks[ refnum ]) < 0 ) {
484                 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );
485             }
486         }
487     }
488     return;
489 }
490