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