]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/ofork.c
Fixes from didier <dgautheron@magic.fr> to not use a freed pointer in
[netatalk.git] / etc / afpd / ofork.c
1 /*
2  * $Id: ofork.c,v 1.9 2002-01-17 16:13:34 jmarcus 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     u_short     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     u_int16_t   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_default, "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_prev;
116             of->of_d_next->of_d_prev = of->of_d_next;
117             of->of_dir = newdir;
118             if (!(d_ofork = newdir->d_ofork)) {
119                 newdir->d_ofork = of;
120                 of->of_d_next = of->of_d_prev = of;
121             } else {
122                 of->of_d_next = d_ofork;
123                 of->of_d_prev = d_ofork->of_d_prev;
124                 of->of_d_prev->of_d_next = of;
125                 d_ofork->of_d_prev = of;
126             }
127             of_hash(of);
128         }
129     }
130
131     return AFP_OK;
132 }
133
134 struct ofork *
135             of_alloc(vol, dir, path, ofrefnum, eid, ad)
136             struct vol          *vol;
137 struct dir              *dir;
138 char            *path;
139 u_int16_t               *ofrefnum;
140 const int           eid;
141 struct adouble      *ad;
142 {
143     struct ofork        *of, *d_ofork;
144     u_int16_t           refnum, of_refnum;
145
146     int                 i;
147
148     if (!oforks) {
149         nforks = (getdtablesize() - 10) / 2;
150         oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
151         if (!oforks)
152             return NULL;
153     }
154
155     for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
156         /* cf AFP3.0.pdf, File fork page 40 */
157         if (!refnum)
158            refnum++;
159         if ( oforks[ refnum % nforks ] == NULL ) {
160             break;
161         }
162     }
163     /* grr, Apple and their 'uniquely identifies' 
164           the next line is a protection against 
165           of_alloc()
166              refnum % nforks = 3 
167              lastrefnum = 3
168              oforks[3] != NULL 
169              refnum = 4
170              oforks[4] == NULL
171              return 4
172          
173           close(oforks[4])
174       
175           of_alloc()
176              refnum % nforks = 4
177              ...
178              return 4
179          same if lastrefnum++ rather than ++lastrefnum. 
180     */
181     lastrefnum = refnum;
182     if ( i == nforks ) {
183         LOG(log_error, logtype_default, "of_alloc: maximum number of forks exceeded.");
184         return( NULL );
185     }
186
187     of_refnum = refnum % nforks;
188     if (( oforks[ of_refnum ] =
189                 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
190         LOG(log_error, logtype_default, "of_alloc: malloc: %s", strerror(errno) );
191         return NULL;
192     }
193     of = oforks[of_refnum];
194
195     /* see if we need to allocate space for the adouble struct */
196     if (!ad) {
197         ad = malloc( sizeof( struct adouble ) );
198         if (!ad) {
199             LOG(log_error, logtype_default, "of_alloc: malloc: %s", strerror(errno) );
200             return NULL;
201         }
202
203         /* initialize to zero. This is important to ensure that
204            ad_open really does reinitialize the structure. */
205         memset( ad, 0, sizeof( struct adouble ) );
206     } else {
207         /* Increase the refcount on this struct adouble. This is
208            decremented again in oforc_dealloc. */
209         ad->ad_refcount++;
210     }
211
212     of->of_ad = ad;
213
214     of->of_vol = vol;
215     of->of_dir = dir;
216
217     if (!(d_ofork = dir->d_ofork)) {
218         dir->d_ofork = of;
219         of->of_d_next = of->of_d_prev = of;
220     } else {
221         of->of_d_next = d_ofork;
222         of->of_d_prev = d_ofork->of_d_prev;
223         d_ofork->of_d_prev->of_d_next = of;
224         d_ofork->of_d_prev = of;
225     }
226
227     /* here's the deal: we allocate enough for the standard mac file length.
228      * in the future, we'll reallocate in fairly large jumps in case
229      * of long unicode names */
230     if (( of->of_name =(char *)malloc(MACFILELEN + 1)) ==
231             NULL ) {
232         LOG(log_error, logtype_default, "of_alloc: malloc: %s", strerror(errno) );
233         if (!ad)
234             free(of->of_ad);
235         free(of);
236         oforks[ of_refnum ] = NULL;
237         return NULL;
238     }
239     strncpy( of->of_name, path, of->of_namelen = MACFILELEN + 1);
240     *ofrefnum = refnum;
241     of->of_refnum = of_refnum;
242     of_hash(of);
243
244     if (eid == ADEID_DFORK)
245         of->of_flags = AFPFORK_DATA;
246     else
247         of->of_flags = AFPFORK_RSRC;
248
249     return( of );
250 }
251
252 struct ofork *of_find(const u_int16_t ofrefnum )
253 {
254     if (!oforks || !nforks)
255         return NULL;
256
257     return( oforks[ ofrefnum % nforks ] );
258 }
259
260 struct ofork *
261             of_findname(const struct vol *vol, const struct dir *dir, const char *name)
262 {
263     struct ofork *of;
264
265     for (of = ofork_table[hashfn(name)]; of; of = of->next) {
266         if ((vol == of->of_vol) && (dir == of->of_dir) &&
267                 (strcmp(of->of_name, name) == 0))
268             return of;
269     }
270
271     return NULL;
272 }
273
274
275 void of_dealloc( of )
276 struct ofork    *of;
277 {
278     if (!oforks)
279         return;
280
281     of_unhash(of);
282
283     /* detach ofork */
284     of->of_d_prev->of_d_next = of->of_d_next;
285     of->of_d_next->of_d_prev = of->of_d_prev;
286     if (of->of_dir->d_ofork == of) {
287         of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
288     }
289
290     oforks[ of->of_refnum ] = NULL;
291     free( of->of_name );
292
293     /* decrease refcount */
294     of->of_ad->ad_refcount--;
295
296     if ( of->of_ad->ad_refcount <= 0) {
297         free( of->of_ad);
298     } else {/* someone's still using it. just free this user's locks */
299         ad_unlock(of->of_ad, of->of_refnum);
300     }
301
302     free( of );
303 }