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