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