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