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