]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Macs have a maximum of 2^16 -1 open files... It's not the case for ulimit -n
[netatalk.git] / etc / afpd / ofork.c
1 /*
2  * $Id: ofork.c,v 1.16 2002-06-17 11:40:11 didg 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 #include <string.h>
15 #include <sys/stat.h> /* works around a bug */
16 #include <sys/param.h>
17 #include <atalk/logger.h>
18 #include <errno.h>
19
20 #include <atalk/adouble.h>
21
22 #include "globals.h"
23 #include "volume.h"
24 #include "directory.h"
25 #include "fork.h"
26
27 /* we need to have a hashed list of oforks (by name). just hash
28  * by first letter. */
29 #define OFORK_HASHSIZE  64
30 static struct ofork     *ofork_table[OFORK_HASHSIZE];
31
32 static struct ofork     **oforks = NULL;
33 static int              nforks = 0;
34 static u_short          lastrefnum = 0;
35
36
37 /* OR some of each character for the hash*/
38 static __inline__ unsigned long hashfn(const char *name)
39 {
40     unsigned long i = 0;
41
42     while (*name) {
43         i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
44     }
45     return i & (OFORK_HASHSIZE - 1);
46 }
47
48 static __inline__ void of_hash(struct ofork *of)
49 {
50     struct ofork **table;
51
52     table = &ofork_table[hashfn(of->of_name)];
53     if ((of->next = *table) != NULL)
54         (*table)->prevp = &of->next;
55     *table = of;
56     of->prevp = table;
57 }
58
59 static __inline__ void of_unhash(struct ofork *of)
60 {
61     if (of->prevp) {
62         if (of->next)
63             of->next->prevp = of->prevp;
64         *(of->prevp) = of->next;
65     }
66 }
67
68 void of_pforkdesc( f )
69 FILE    *f;
70 {
71     int ofrefnum;
72
73     if (!oforks)
74         return;
75
76     for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
77         if ( oforks[ ofrefnum ] != NULL ) {
78             fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
79         }
80     }
81 }
82
83 int of_flush(const struct vol *vol)
84 {
85     int refnum;
86
87     if (!oforks)
88         return 0;
89
90     for ( refnum = 0; refnum < nforks; refnum++ ) {
91         if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
92                 flushfork( oforks[ refnum ] ) < 0 ) {
93             LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
94         }
95     }
96     return( 0 );
97 }
98
99
100 int of_rename(vol, olddir, oldpath, newdir, newpath)
101 const struct vol *vol;
102 struct dir *olddir, *newdir;
103 const char *oldpath, *newpath;
104 {
105     struct ofork *of, *next, *d_ofork;
106
107     next = ofork_table[hashfn(oldpath)];
108     while ((of = next)) {
109         next = next->next; /* so we can unhash and still be all right. */
110
111         if ((vol == of->of_vol) && (olddir == of->of_dir) &&
112                 (strcmp(of->of_name, oldpath) == 0)) {
113             of_unhash(of);
114             strncpy( of->of_name, newpath, of->of_namelen);
115             of->of_d_prev->of_d_next = of->of_d_next;
116             of->of_d_next->of_d_prev = of->of_d_prev;
117             if (of->of_dir->d_ofork == of) {
118                 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
119             }       
120             of->of_dir = newdir;
121             if (!(d_ofork = newdir->d_ofork)) {
122                 newdir->d_ofork = of;
123                 of->of_d_next = of->of_d_prev = of;
124             } else {
125                 of->of_d_next = d_ofork;
126                 of->of_d_prev = d_ofork->of_d_prev;
127                 of->of_d_prev->of_d_next = of;
128                 d_ofork->of_d_prev = of;
129             }
130             of_hash(of);
131         }
132     }
133
134     return AFP_OK;
135 }
136
137 #define min(a,b)        ((a)<(b)?(a):(b))
138
139 struct ofork *
140             of_alloc(vol, dir, path, ofrefnum, eid, ad)
141             struct vol          *vol;
142 struct dir              *dir;
143 char            *path;
144 u_int16_t               *ofrefnum;
145 const int           eid;
146 struct adouble      *ad;
147 {
148     struct ofork        *of, *d_ofork;
149     u_int16_t           refnum, of_refnum;
150
151     int                 i;
152
153     if (!oforks) {
154         nforks = (getdtablesize() - 10) / 2;
155         /* protect against insane ulimit -n */
156         nforks = min(nforks, 0xffff);
157         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
158         if (!oforks)
159             return NULL;
160     }
161
162     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
163         /* cf AFP3.0.pdf, File fork page 40 */
164         if (!refnum)
165             refnum++;
166         if ( oforks[ refnum % nforks ] == NULL ) {
167             break;
168         }
169     }
170     /* grr, Apple and their 'uniquely identifies'
171           the next line is a protection against 
172           of_alloc()
173              refnum % nforks = 3 
174              lastrefnum = 3
175              oforks[3] != NULL 
176              refnum = 4
177              oforks[4] == NULL
178              return 4
179          
180           close(oforks[4])
181       
182           of_alloc()
183              refnum % nforks = 4
184              ...
185              return 4
186          same if lastrefnum++ rather than ++lastrefnum. 
187     */
188     lastrefnum = refnum;
189     if ( i == nforks ) {
190         LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
191         return( NULL );
192     }
193
194     of_refnum = refnum % nforks;
195     if (( oforks[ of_refnum ] =
196                 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
197         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
198         return NULL;
199     }
200     of = oforks[of_refnum];
201
202     /* see if we need to allocate space for the adouble struct */
203     if (!ad) {
204         ad = malloc( sizeof( struct adouble ) );
205         if (!ad) {
206             LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
207             return NULL;
208         }
209
210         /* initialize to zero. This is important to ensure that
211            ad_open really does reinitialize the structure. */
212         memset( ad, 0, sizeof( struct adouble ) );
213     } else {
214         /* Increase the refcount on this struct adouble. This is
215            decremented again in oforc_dealloc. */
216         ad->ad_refcount++;
217     }
218
219     of->of_ad = ad;
220
221     of->of_vol = vol;
222     of->of_dir = dir;
223
224     if (!(d_ofork = dir->d_ofork)) {
225         dir->d_ofork = of;
226         of->of_d_next = of->of_d_prev = of;
227     } else {
228         of->of_d_next = d_ofork;
229         of->of_d_prev = d_ofork->of_d_prev;
230         d_ofork->of_d_prev->of_d_next = of;
231         d_ofork->of_d_prev = of;
232     }
233
234     /* here's the deal: we allocate enough for the standard mac file length.
235      * in the future, we'll reallocate in fairly large jumps in case
236      * of long unicode names */
237     if (( of->of_name =(char *)malloc(255 + 1)) ==
238             NULL ) {
239         LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
240         if (!ad)
241             free(of->of_ad);
242         free(of);
243         oforks[ of_refnum ] = NULL;
244         return NULL;
245     }
246     strncpy( of->of_name, path, of->of_namelen = 255 + 1);
247     *ofrefnum = refnum;
248     of->of_refnum = refnum;
249     of_hash(of);
250
251     if (eid == ADEID_DFORK)
252         of->of_flags = AFPFORK_DATA;
253     else
254         of->of_flags = AFPFORK_RSRC;
255
256     return( of );
257 }
258
259 struct ofork *of_find(const u_int16_t ofrefnum )
260 {
261     if (!oforks || !nforks)
262         return NULL;
263
264     return( oforks[ ofrefnum % nforks ] );
265 }
266
267 /* --------------------------
268    FIXME it doesn't work :-(
269    mac1 open file "test" with simple text 
270    mac2 rename "test" ==> "test1"
271    
272    now of_findname return NULL 
273    
274    
275 */
276 struct ofork *
277             of_findname(const struct vol *vol, const struct dir *dir, const char *name)
278 {
279     struct ofork *of;
280
281     for (of = ofork_table[hashfn(name)]; of; of = of->next) {
282         if ((vol == of->of_vol) && (dir == of->of_dir) &&
283                 (strcmp(of->of_name, name) == 0))
284             return of;
285     }
286
287     return NULL;
288 }
289
290
291 void of_dealloc( of )
292 struct ofork    *of;
293 {
294     if (!oforks)
295         return;
296
297     of_unhash(of);
298
299     /* detach ofork */
300     of->of_d_prev->of_d_next = of->of_d_next;
301     of->of_d_next->of_d_prev = of->of_d_prev;
302     if (of->of_dir->d_ofork == of) {
303         of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
304     }
305
306     oforks[ of->of_refnum % nforks ] = NULL;
307     free( of->of_name );
308
309     /* decrease refcount */
310     of->of_ad->ad_refcount--;
311
312     if ( of->of_ad->ad_refcount <= 0) {
313         free( of->of_ad);
314     } else {/* someone's still using it. just free this user's locks */
315         ad_unlock(of->of_ad, of->of_refnum);
316     }
317
318     free( of );
319 }