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