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