]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Symlink patch from Anton Starikov
[netatalk.git] / etc / afpd / ofork.c
1 /*
2  * $Id: ofork.c,v 1.30.2.1 2010-01-02 10:22:32 franklahm Exp $
3  *
4  * Copyright (c) 1996 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <sys/stat.h> /* works around a bug */
19 #include <sys/param.h>
20 #include <atalk/logger.h>
21 #include <errno.h>
22
23 #include <atalk/util.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(
105     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, *d_ofork;
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 && olddir == of->of_dir &&
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_d_prev->of_d_next = of->of_d_next;
129                 of->of_d_next->of_d_prev = of->of_d_prev;
130                 if (of->of_dir->d_ofork == of) {
131                     of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
132                 }           
133                 of->of_dir = newdir;
134                 if (!(d_ofork = newdir->d_ofork)) {
135                     newdir->d_ofork = of;
136                     of->of_d_next = of->of_d_prev = of;
137                 } else {
138                     of->of_d_next = d_ofork;
139                     of->of_d_prev = d_ofork->of_d_prev;
140                     of->of_d_prev->of_d_next = of;
141                     d_ofork->of_d_prev = of;
142                 }
143             }
144         }
145     }
146
147     return AFP_OK;
148 }
149
150 #define min(a,b)        ((a)<(b)?(a):(b))
151
152 struct ofork *
153 of_alloc(struct vol *vol,
154     struct dir     *dir,
155     char           *path,
156     u_int16_t      *ofrefnum,
157     const int      eid,
158     struct adouble *ad,
159     struct stat    *st)
160 {
161     struct ofork        *of, *d_ofork;
162     u_int16_t           refnum, of_refnum;
163
164     int                 i;
165
166     if (!oforks) {
167         nforks = getdtablesize() - 10;
168         /* protect against insane ulimit -n */
169         nforks = min(nforks, 0xffff);
170         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
171         if (!oforks)
172             return NULL;
173     }
174
175     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
176         /* cf AFP3.0.pdf, File fork page 40 */
177         if (!refnum)
178             refnum++;
179         if ( oforks[ refnum % nforks ] == NULL ) {
180             break;
181         }
182     }
183     /* grr, Apple and their 'uniquely identifies'
184           the next line is a protection against 
185           of_alloc()
186              refnum % nforks = 3 
187              lastrefnum = 3
188              oforks[3] != NULL 
189              refnum = 4
190              oforks[4] == NULL
191              return 4
192          
193           close(oforks[4])
194       
195           of_alloc()
196              refnum % nforks = 4
197              ...
198              return 4
199          same if lastrefnum++ rather than ++lastrefnum. 
200     */
201     lastrefnum = refnum;
202     if ( i == nforks ) {
203         LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
204         return( NULL );
205     }
206
207     of_refnum = refnum % nforks;
208     if (( oforks[ of_refnum ] =
209                 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
210         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
211         return NULL;
212     }
213     of = oforks[of_refnum];
214
215     /* see if we need to allocate space for the adouble struct */
216     if (!ad) {
217         ad = malloc( sizeof( struct adouble ) );
218         if (!ad) {
219             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
220             free(of);
221             oforks[ of_refnum ] = NULL;
222             return NULL;
223         }
224
225         /* initialize to zero. This is important to ensure that
226            ad_open really does reinitialize the structure. */
227         ad_init(ad, vol->v_adouble, vol->v_ad_options);
228
229         ad->ad_m_namelen = 255 +1;
230         /* here's the deal: we allocate enough for the standard mac file length.
231          * in the future, we'll reallocate in fairly large jumps in case
232          * of long unicode names */
233         if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
234             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
235             free(ad);
236             free(of);
237             oforks[ of_refnum ] = NULL;
238             return NULL;
239         }
240         strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
241     } else {
242         /* Increase the refcount on this struct adouble. This is
243            decremented again in oforc_dealloc. */
244         ad->ad_refcount++;
245     }
246
247     of->of_ad = ad;
248     of->of_vol = vol;
249     of->of_dir = dir;
250
251     if (!(d_ofork = dir->d_ofork)) {
252         dir->d_ofork = of;
253         of->of_d_next = of->of_d_prev = of;
254     } else {
255         of->of_d_next = d_ofork;
256         of->of_d_prev = d_ofork->of_d_prev;
257         d_ofork->of_d_prev->of_d_next = of;
258         d_ofork->of_d_prev = of;
259     }
260
261     *ofrefnum = refnum;
262     of->of_refnum = refnum;
263     of->key.dev = st->st_dev;
264     of->key.inode = st->st_ino;
265     if (eid == ADEID_DFORK)
266         of->of_flags = AFPFORK_DATA;
267     else
268         of->of_flags = AFPFORK_RSRC;
269
270     of_hash(of);
271     return( of );
272 }
273
274 struct ofork *of_find(const u_int16_t ofrefnum )
275 {
276     if (!oforks || !nforks)
277         return NULL;
278
279     return( oforks[ ofrefnum % nforks ] );
280 }
281
282 /* -------------------------- */
283 int of_stat  (struct path *path)
284 {
285 int ret;
286     path->st_errno = 0;
287     path->st_valid = 1;
288     if ((ret = lstat(path->u_name, &path->st)) < 0)
289         path->st_errno = errno;
290    return ret;
291 }
292
293 /* -------------------------- 
294    stat the current directory.
295    stat(".") works even if "." is deleted thus
296    we have to stat ../name because we want to know if it's there
297 */
298 int of_statdir  (struct vol *vol, struct path *path)
299 {
300 static char pathname[ MAXPATHLEN + 1] = "../";
301 int ret;
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     strlcpy(pathname +3, path->d_dir->d_u_name, sizeof (pathname) -3);
311
312     if (!(ret = lstat(pathname, &path->st)))
313         return 0;
314         
315     path->st_errno = errno;
316     /* hmm, can't stat curdir anymore */
317     if (errno == EACCES && curdir->d_parent ) {
318        if (movecwd(vol, curdir->d_parent)) 
319            return -1;
320        path->st_errno = 0;
321        if ((ret = lstat(path->d_dir->d_u_name, &path->st)) < 0) 
322            path->st_errno = errno;
323     }
324     return ret;
325 }
326
327 /* -------------------------- */
328 struct ofork *
329             of_findname(struct path *path)
330 {
331     struct ofork *of;
332     struct file_key key;
333     
334     if (!path->st_valid) {
335        of_stat(path);
336     }
337         
338     if (path->st_errno)
339         return NULL;
340
341     key.dev = path->st.st_dev;
342     key.inode = path->st.st_ino;
343
344     for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
345         if (key.dev == of->key.dev && key.inode == of->key.inode ) {
346             return of;
347         }
348     }
349
350     return NULL;
351 }
352
353 void of_dealloc( struct ofork *of)
354 {
355     if (!oforks)
356         return;
357
358     of_unhash(of);
359
360     /* detach ofork */
361     of->of_d_prev->of_d_next = of->of_d_next;
362     of->of_d_next->of_d_prev = of->of_d_prev;
363     if (of->of_dir->d_ofork == of) {
364         of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
365     }
366
367     oforks[ of->of_refnum % nforks ] = NULL;
368
369     /* decrease refcount */
370     of->of_ad->ad_refcount--;
371
372     if ( of->of_ad->ad_refcount <= 0) {
373         free( of->of_ad->ad_m_name );
374         free( of->of_ad);
375     } else {/* someone's still using it. just free this user's locks */
376         ad_unlock(of->of_ad, of->of_refnum);
377     }
378
379     free( of );
380 }
381
382 /* --------------------------- */
383 int of_closefork(struct ofork *ofork)
384 {
385     struct timeval      tv;
386     int                 adflags, doflush = 0;
387     int                 ret;
388
389     adflags = 0;
390     if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
391             adflags |= ADFLAGS_DF;
392     }
393     if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
394         adflags |= ADFLAGS_HF;
395         /*
396          * Only set the rfork's length if we're closing the rfork.
397          */
398         if ((ofork->of_flags & AFPFORK_RSRC)) {
399             ad_refresh( ofork->of_ad );
400             if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
401                 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
402                 doflush++;
403             }
404             if ( doflush ) {
405                  ad_flush( ofork->of_ad );
406             }
407         }
408     }
409     ret = 0;
410     if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
411         ret = -1;
412     }
413  
414     of_dealloc( ofork );
415     return ret;
416 }
417
418 /* ----------------------
419
420 */
421 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
422 {
423     struct ofork        *of;
424     struct adouble      *adp;
425
426     if ((of = of_findname(path))) {
427         adp = of->of_ad;
428     } else {
429         ad_init(ad, vol->v_adouble, vol->v_ad_options);
430         adp = ad;
431     }
432     return adp;
433 }
434
435 /* ---------------------- 
436    close all forks for a volume
437 */
438 void of_closevol(const struct vol *vol)
439 {
440     int refnum;
441
442     if (!oforks)
443         return;
444
445     for ( refnum = 0; refnum < nforks; refnum++ ) {
446         if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
447             if (of_closefork( oforks[ refnum ]) < 0 ) {
448                 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
449             }
450         }
451     }
452     return;
453 }
454