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