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