]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Merge branch-2-1
[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;
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
122             && olddir->d_did == of->of_did
123             && s_of->key.dev == of->key.dev
124             && s_of->key.inode == of->key.inode ) {
125             if (!done) {
126                 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
127                 done = 1;
128             }
129             if (newdir != olddir)
130                 of->of_did = newdir->d_did;
131         }
132     }
133
134     return AFP_OK;
135 }
136
137 #define min(a,b)    ((a)<(b)?(a):(b))
138
139 struct ofork *
140 of_alloc(struct vol *vol,
141          struct dir    *dir,
142          char      *path,
143          u_int16_t     *ofrefnum,
144          const int      eid,
145          struct adouble *ad,
146          struct stat    *st)
147 {
148     struct ofork        *of;
149     u_int16_t       refnum, of_refnum;
150
151     int         i;
152
153     if (!oforks) {
154         nforks = getdtablesize() - 10;
155         /* protect against insane ulimit -n */
156         nforks = min(nforks, 0xffff);
157         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
158         if (!oforks)
159             return NULL;
160     }
161
162     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
163         /* cf AFP3.0.pdf, File fork page 40 */
164         if (!refnum)
165             refnum++;
166         if ( oforks[ refnum % nforks ] == NULL ) {
167             break;
168         }
169     }
170     /* grr, Apple and their 'uniquely identifies'
171        the next line is a protection against
172        of_alloc()
173        refnum % nforks = 3
174        lastrefnum = 3
175        oforks[3] != NULL
176        refnum = 4
177        oforks[4] == NULL
178        return 4
179
180        close(oforks[4])
181
182        of_alloc()
183        refnum % nforks = 4
184        ...
185        return 4
186        same if lastrefnum++ rather than ++lastrefnum.
187     */
188     lastrefnum = refnum;
189     if ( i == nforks ) {
190         LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
191         return( NULL );
192     }
193
194     of_refnum = refnum % nforks;
195     if (( oforks[ of_refnum ] =
196           (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
197         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
198         return NULL;
199     }
200     of = oforks[of_refnum];
201
202     /* see if we need to allocate space for the adouble struct */
203     if (!ad) {
204         ad = malloc( sizeof( struct adouble ) );
205         if (!ad) {
206             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
207             free(of);
208             oforks[ of_refnum ] = NULL;
209             return NULL;
210         }
211
212         /* initialize to zero. This is important to ensure that
213            ad_open really does reinitialize the structure. */
214         ad_init(ad, vol->v_adouble, vol->v_ad_options);
215
216         ad->ad_m_namelen = 255 +1;
217         /* here's the deal: we allocate enough for the standard mac file length.
218          * in the future, we'll reallocate in fairly large jumps in case
219          * of long unicode names */
220         if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
221             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
222             free(ad);
223             free(of);
224             oforks[ of_refnum ] = NULL;
225             return NULL;
226         }
227         strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
228     } else {
229         /* Increase the refcount on this struct adouble. This is
230            decremented again in oforc_dealloc. */
231         ad->ad_refcount++;
232     }
233
234     of->of_ad = ad;
235     of->of_vol = vol;
236     of->of_did = dir->d_did;
237
238     *ofrefnum = refnum;
239     of->of_refnum = refnum;
240     of->key.dev = st->st_dev;
241     of->key.inode = st->st_ino;
242     if (eid == ADEID_DFORK)
243         of->of_flags = AFPFORK_DATA;
244     else
245         of->of_flags = AFPFORK_RSRC;
246
247     of_hash(of);
248     return( of );
249 }
250
251 struct ofork *of_find(const u_int16_t ofrefnum )
252 {
253     if (!oforks || !nforks)
254         return NULL;
255
256     return( oforks[ ofrefnum % nforks ] );
257 }
258
259 /* -------------------------- */
260 int of_stat(struct path *path)
261 {
262     int ret;
263
264     path->st_errno = 0;
265     path->st_valid = 1;
266
267     if ((ret = lstat(path->u_name, &path->st)) < 0) {
268         LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
269             cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
270         path->st_errno = errno;
271     }
272
273     return ret;
274 }
275
276
277 #ifdef HAVE_RENAMEAT
278 int of_fstatat(int dirfd, struct path *path)
279 {
280     int ret;
281
282     path->st_errno = 0;
283     path->st_valid = 1;
284
285     if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
286         path->st_errno = errno;
287
288    return ret;
289 }
290 #endif /* HAVE_RENAMEAT */
291
292 /* -------------------------- 
293    stat the current directory.
294    stat(".") works even if "." is deleted thus
295    we have to stat ../name because we want to know if it's there
296 */
297 int of_statdir(struct vol *vol, struct path *path)
298 {
299     static char pathname[ MAXPATHLEN + 1] = "../";
300     int ret;
301     size_t len;
302     struct dir *dir;
303
304     if (*path->m_name) {
305         /* not curdir */
306         return of_stat (path);
307     }
308     path->st_errno = 0;
309     path->st_valid = 1;
310     /* FIXME, what about: we don't have r-x perm anymore ? */
311     len = blength(path->d_dir->d_u_name);
312     if (len > (MAXPATHLEN - 3))
313         len = MAXPATHLEN - 3;
314     strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
315
316     LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
317
318     if (!(ret = lstat(pathname, &path->st)))
319         return 0;
320
321     path->st_errno = errno;
322
323     /* hmm, can't stat curdir anymore */
324     if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
325        if (movecwd(vol, dir)) 
326            return -1;
327        path->st_errno = 0;
328
329        if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0) 
330            path->st_errno = errno;
331     }
332
333     return ret;
334 }
335
336 /* -------------------------- */
337 struct ofork *of_findname(struct path *path)
338 {
339     struct ofork *of;
340     struct file_key key;
341
342     if (!path->st_valid) {
343         of_stat(path);
344     }
345
346     if (path->st_errno)
347         return NULL;
348
349     key.dev = path->st.st_dev;
350     key.inode = path->st.st_ino;
351
352     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
353         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
354             return of;
355         }
356     }
357
358     return NULL;
359 }
360
361 /*!
362  * @brief Search for open fork by dirfd/name
363  *
364  * Function call of_fstatat with dirfd and path and uses dev and ino
365  * to search the open fork table.
366  *
367  * @param dirfd     (r) directory fd
368  * @param path      (rw) pointer to struct path
369  */
370 #ifdef HAVE_RENAMEAT
371 struct ofork *of_findnameat(int dirfd, struct path *path)
372 {
373     struct ofork *of;
374     struct file_key key;
375     
376     if ( ! path->st_valid) {
377         of_fstatat(dirfd, path);
378     }
379         
380     if (path->st_errno)
381         return NULL;
382
383     key.dev = path->st.st_dev;
384     key.inode = path->st.st_ino;
385
386     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
387         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
388             return of;
389         }
390     }
391
392     return NULL;
393 }
394 #endif
395
396 void of_dealloc( struct ofork *of)
397 {
398     if (!oforks)
399         return;
400
401     of_unhash(of);
402     oforks[ of->of_refnum % nforks ] = NULL;
403
404     /* decrease refcount */
405     of->of_ad->ad_refcount--;
406
407     if ( of->of_ad->ad_refcount <= 0) {
408         free( of->of_ad->ad_m_name );
409         free( of->of_ad);
410     } else {/* someone's still using it. just free this user's locks */
411         ad_unlock(of->of_ad, of->of_refnum);
412     }
413
414     free( of );
415 }
416
417 /* --------------------------- */
418 int of_closefork(struct ofork *ofork)
419 {
420     struct timeval      tv;
421     int         adflags, doflush = 0;
422     int                 ret;
423
424     adflags = 0;
425     if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
426         adflags |= ADFLAGS_DF;
427     }
428     if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
429         adflags |= ADFLAGS_HF;
430         /*
431          * Only set the rfork's length if we're closing the rfork.
432          */
433         if ((ofork->of_flags & AFPFORK_RSRC)) {
434             ad_refresh( ofork->of_ad );
435             if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
436                 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
437                 doflush++;
438             }
439             if ( doflush ) {
440                 ad_flush( ofork->of_ad );
441             }
442         }
443     }
444     ret = 0;
445     if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
446         ret = -1;
447     }
448
449     of_dealloc( ofork );
450     return ret;
451 }
452
453 /* ----------------------
454
455  */
456 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
457 {
458     struct ofork        *of;
459     struct adouble      *adp;
460
461     if ((of = of_findname(path))) {
462         adp = of->of_ad;
463     } else {
464         ad_init(ad, vol->v_adouble, vol->v_ad_options);
465         adp = ad;
466     }
467     return adp;
468 }
469
470 /* ----------------------
471    close all forks for a volume
472 */
473 void of_closevol(const struct vol *vol)
474 {
475     int refnum;
476
477     if (!oforks)
478         return;
479
480     for ( refnum = 0; refnum < nforks; refnum++ ) {
481         if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
482             if (of_closefork( oforks[ refnum ]) < 0 ) {
483                 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
484             }
485         }
486     }
487     return;
488 }
489