]> 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 #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 = 255 +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     }
227
228     of->of_ad = ad;
229     of->of_vol = vol;
230     of->of_did = dir->d_did;
231
232     *ofrefnum = refnum;
233     of->of_refnum = refnum;
234     of->key.dev = st->st_dev;
235     of->key.inode = st->st_ino;
236     if (eid == ADEID_DFORK)
237         of->of_flags = AFPFORK_DATA;
238     else
239         of->of_flags = AFPFORK_RSRC;
240
241     of_hash(of);
242     return( of );
243 }
244
245 struct ofork *of_find(const u_int16_t ofrefnum )
246 {
247     if (!oforks || !nforks)
248         return NULL;
249
250     return( oforks[ ofrefnum % nforks ] );
251 }
252
253 /* -------------------------- */
254 int of_stat(struct path *path)
255 {
256     int ret;
257
258     path->st_errno = 0;
259     path->st_valid = 1;
260
261     if ((ret = lstat(path->u_name, &path->st)) < 0) {
262         LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
263             cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
264         path->st_errno = errno;
265     }
266
267     return ret;
268 }
269
270
271 #ifdef HAVE_RENAMEAT
272 int of_fstatat(int dirfd, struct path *path)
273 {
274     int ret;
275
276     path->st_errno = 0;
277     path->st_valid = 1;
278
279     if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
280         path->st_errno = errno;
281
282    return ret;
283 }
284 #endif /* HAVE_RENAMEAT */
285
286 /* -------------------------- 
287    stat the current directory.
288    stat(".") works even if "." is deleted thus
289    we have to stat ../name because we want to know if it's there
290 */
291 int of_statdir(struct vol *vol, struct path *path)
292 {
293     static char pathname[ MAXPATHLEN + 1] = "../";
294     int ret;
295     size_t len;
296     struct dir *dir;
297
298     if (*path->m_name) {
299         /* not curdir */
300         return of_stat (path);
301     }
302     path->st_errno = 0;
303     path->st_valid = 1;
304     /* FIXME, what about: we don't have r-x perm anymore ? */
305     len = blength(path->d_dir->d_u_name);
306     if (len > (MAXPATHLEN - 3))
307         len = MAXPATHLEN - 3;
308     strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
309
310     LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
311
312     if (!(ret = lstat(pathname, &path->st)))
313         return 0;
314
315     path->st_errno = errno;
316
317     /* hmm, can't stat curdir anymore */
318     if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
319        if (movecwd(vol, dir)) 
320            return -1;
321        path->st_errno = 0;
322
323        if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0) 
324            path->st_errno = errno;
325     }
326
327     return ret;
328 }
329
330 /* -------------------------- */
331 struct ofork *of_findname(struct path *path)
332 {
333     struct ofork *of;
334     struct file_key key;
335
336     if (!path->st_valid) {
337         of_stat(path);
338     }
339
340     if (path->st_errno)
341         return NULL;
342
343     key.dev = path->st.st_dev;
344     key.inode = path->st.st_ino;
345
346     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
347         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
348             return of;
349         }
350     }
351
352     return NULL;
353 }
354
355 /*!
356  * @brief Search for open fork by dirfd/name
357  *
358  * Function call of_fstatat with dirfd and path and uses dev and ino
359  * to search the open fork table.
360  *
361  * @param dirfd     (r) directory fd
362  * @param path      (rw) pointer to struct path
363  */
364 #ifdef HAVE_RENAMEAT
365 struct ofork *of_findnameat(int dirfd, struct path *path)
366 {
367     struct ofork *of;
368     struct file_key key;
369     
370     if ( ! path->st_valid) {
371         of_fstatat(dirfd, path);
372     }
373         
374     if (path->st_errno)
375         return NULL;
376
377     key.dev = path->st.st_dev;
378     key.inode = path->st.st_ino;
379
380     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
381         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
382             return of;
383         }
384     }
385
386     return NULL;
387 }
388 #endif
389
390 void of_dealloc( struct ofork *of)
391 {
392     if (!oforks)
393         return;
394
395     of_unhash(of);
396     oforks[ of->of_refnum % nforks ] = NULL;
397     ad_unlock(of->of_ad, of->of_refnum);
398
399     free( of );
400 }
401
402 /* --------------------------- */
403 int of_closefork(struct ofork *ofork)
404 {
405     struct timeval      tv;
406     int         adflags, doflush = 0;
407     int                 ret;
408
409     adflags = 0;
410     if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
411         adflags |= ADFLAGS_DF;
412     }
413     if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
414         adflags |= ADFLAGS_HF;
415         /*
416          * Only set the rfork's length if we're closing the rfork.
417          */
418         if ((ofork->of_flags & AFPFORK_RSRC)) {
419             ad_refresh( ofork->of_ad );
420             if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
421                 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
422                 doflush++;
423             }
424             if ( doflush ) {
425                 ad_flush( ofork->of_ad );
426             }
427         }
428     }
429     ret = 0;
430     if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
431         ret = -1;
432     }
433
434     of_dealloc( ofork );
435     return ret;
436 }
437
438 /* ----------------------
439
440  */
441 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
442 {
443     struct ofork        *of;
444     struct adouble      *adp;
445
446     if ((of = of_findname(path))) {
447         adp = of->of_ad;
448     } else {
449         ad_init(ad, vol->v_adouble, vol->v_ad_options);
450         adp = ad;
451     }
452     return adp;
453 }
454
455 /* ----------------------
456    close all forks for a volume
457 */
458 void of_closevol(const struct vol *vol)
459 {
460     int refnum;
461
462     if (!oforks)
463         return;
464
465     for ( refnum = 0; refnum < nforks; refnum++ ) {
466         if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
467             if (of_closefork( oforks[ refnum ]) < 0 ) {
468                 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
469             }
470         }
471     }
472     return;
473 }
474