2 * $Id: ofork.c,v 1.15 2002-05-29 18:02:59 jmarcus Exp $
4 * Copyright (c) 1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
15 #include <sys/stat.h> /* works around a bug */
16 #include <sys/param.h>
17 #include <atalk/logger.h>
20 #include <atalk/adouble.h>
24 #include "directory.h"
27 /* we need to have a hashed list of oforks (by name). just hash
29 #define OFORK_HASHSIZE 64
30 static struct ofork *ofork_table[OFORK_HASHSIZE];
32 static struct ofork **oforks = NULL;
33 static int nforks = 0;
34 static u_short lastrefnum = 0;
37 /* OR some of each character for the hash*/
38 static __inline__ unsigned long hashfn(const char *name)
43 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
45 return i & (OFORK_HASHSIZE - 1);
48 static __inline__ void of_hash(struct ofork *of)
52 table = &ofork_table[hashfn(of->of_name)];
53 if ((of->next = *table) != NULL)
54 (*table)->prevp = &of->next;
59 static __inline__ void of_unhash(struct ofork *of)
63 of->next->prevp = of->prevp;
64 *(of->prevp) = of->next;
68 void of_pforkdesc( f )
76 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
77 if ( oforks[ ofrefnum ] != NULL ) {
78 fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
83 int of_flush(const struct vol *vol)
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) );
100 int of_rename(vol, olddir, oldpath, newdir, newpath)
101 const struct vol *vol;
102 struct dir *olddir, *newdir;
103 const char *oldpath, *newpath;
105 struct ofork *of, *next, *d_ofork;
107 next = ofork_table[hashfn(oldpath)];
108 while ((of = next)) {
109 next = next->next; /* so we can unhash and still be all right. */
111 if ((vol == of->of_vol) && (olddir == of->of_dir) &&
112 (strcmp(of->of_name, oldpath) == 0)) {
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;
121 if (!(d_ofork = newdir->d_ofork)) {
122 newdir->d_ofork = of;
123 of->of_d_next = of->of_d_prev = of;
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;
138 of_alloc(vol, dir, path, ofrefnum, eid, ad)
146 struct ofork *of, *d_ofork;
147 u_int16_t refnum, of_refnum;
152 nforks = (getdtablesize() - 10) / 2;
153 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
158 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
159 /* cf AFP3.0.pdf, File fork page 40 */
162 if ( oforks[ refnum % nforks ] == NULL ) {
166 /* grr, Apple and their 'uniquely identifies'
167 the next line is a protection against
182 same if lastrefnum++ rather than ++lastrefnum.
186 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
190 of_refnum = refnum % nforks;
191 if (( oforks[ of_refnum ] =
192 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
193 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
196 of = oforks[of_refnum];
198 /* see if we need to allocate space for the adouble struct */
200 ad = malloc( sizeof( struct adouble ) );
202 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
206 /* initialize to zero. This is important to ensure that
207 ad_open really does reinitialize the structure. */
208 memset( ad, 0, sizeof( struct adouble ) );
210 /* Increase the refcount on this struct adouble. This is
211 decremented again in oforc_dealloc. */
220 if (!(d_ofork = dir->d_ofork)) {
222 of->of_d_next = of->of_d_prev = of;
224 of->of_d_next = d_ofork;
225 of->of_d_prev = d_ofork->of_d_prev;
226 d_ofork->of_d_prev->of_d_next = of;
227 d_ofork->of_d_prev = of;
230 /* here's the deal: we allocate enough for the standard mac file length.
231 * in the future, we'll reallocate in fairly large jumps in case
232 * of long unicode names */
233 if (( of->of_name =(char *)malloc(255 + 1)) ==
235 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
239 oforks[ of_refnum ] = NULL;
242 strncpy( of->of_name, path, of->of_namelen = 255 + 1);
244 of->of_refnum = refnum;
247 if (eid == ADEID_DFORK)
248 of->of_flags = AFPFORK_DATA;
250 of->of_flags = AFPFORK_RSRC;
255 struct ofork *of_find(const u_int16_t ofrefnum )
257 if (!oforks || !nforks)
260 return( oforks[ ofrefnum % nforks ] );
263 /* --------------------------
265 mac1 open file "test" with simple text
266 mac2 rename "test" ==> "test1"
268 now of_findname return NULL
273 of_findname(const struct vol *vol, const struct dir *dir, const char *name)
277 for (of = ofork_table[hashfn(name)]; of; of = of->next) {
278 if ((vol == of->of_vol) && (dir == of->of_dir) &&
279 (strcmp(of->of_name, name) == 0))
287 void of_dealloc( of )
296 of->of_d_prev->of_d_next = of->of_d_next;
297 of->of_d_next->of_d_prev = of->of_d_prev;
298 if (of->of_dir->d_ofork == of) {
299 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
302 oforks[ of->of_refnum % nforks ] = NULL;
305 /* decrease refcount */
306 of->of_ad->ad_refcount--;
308 if ( of->of_ad->ad_refcount <= 0) {
310 } else {/* someone's still using it. just free this user's locks */
311 ad_unlock(of->of_ad, of->of_refnum);