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