2 * $Id: ofork.c,v 1.4 2001-06-20 18:33:04 rufustfirefly 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>
19 #include <atalk/adouble.h>
23 #include "directory.h"
26 /* we need to have a hashed list of oforks (by name). just hash
28 #define OFORK_HASHSIZE 64
29 static struct ofork *ofork_table[OFORK_HASHSIZE];
31 static struct ofork **oforks = NULL;
32 static int nforks = 0;
33 static u_short lastrefnum = 0;
36 /* OR some of each character for the hash*/
37 static __inline__ unsigned long hashfn(const char *name)
42 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
44 return i & (OFORK_HASHSIZE - 1);
47 static __inline__ void of_hash(struct ofork *of)
51 table = &ofork_table[hashfn(of->of_name)];
52 if ((of->next = *table) != NULL)
53 (*table)->prevp = &of->next;
58 static __inline__ void of_unhash(struct ofork *of)
62 of->next->prevp = of->prevp;
63 *(of->prevp) = of->next;
67 void of_pforkdesc( f )
75 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
76 if ( oforks[ ofrefnum ] != NULL ) {
77 fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
82 int of_flush(const struct vol *vol)
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" );
99 int of_rename(vol, olddir, oldpath, newdir, newpath)
100 const struct vol *vol;
101 struct dir *olddir, *newdir;
102 const char *oldpath, *newpath;
104 struct ofork *of, *next, *d_ofork;
106 next = ofork_table[hashfn(oldpath)];
107 while ((of = next)) {
108 next = next->next; /* so we can unhash and still be all right. */
110 if ((vol == of->of_vol) && (olddir == of->of_dir) &&
111 (strcmp(of->of_name, oldpath) == 0)) {
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;
117 if (!(d_ofork = newdir->d_ofork)) {
118 newdir->d_ofork = of;
119 of->of_d_next = of->of_d_prev = of;
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;
134 of_alloc(vol, dir, path, ofrefnum, eid, ad)
142 struct ofork *of, *d_ofork;
143 u_int16_t refnum, of_refnum;
148 nforks = (getdtablesize() - 10) / 2;
149 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
154 for ( refnum = lastrefnum++, i = 0; i < nforks; i++, refnum++ ) {
155 if ( oforks[ refnum % nforks ] == NULL ) {
160 syslog(LOG_ERR, "of_alloc: maximum number of forks exceeded.");
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" );
170 of = oforks[of_refnum];
172 /* see if we need to allocate space for the adouble struct */
174 ad = malloc( sizeof( struct adouble ) );
176 syslog( LOG_ERR, "of_alloc: malloc: %m" );
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 ) );
184 /* Increase the refcount on this struct adouble. This is
185 decremented again in oforc_dealloc. */
194 if (!(d_ofork = dir->d_ofork)) {
196 of->of_d_next = of->of_d_prev = of;
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;
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)) ==
209 syslog( LOG_ERR, "of_alloc: malloc: %m" );
213 oforks[ of_refnum ] = NULL;
216 strncpy( of->of_name, path, of->of_namelen = MACFILELEN + 1);
218 of->of_refnum = of_refnum;
221 if (eid == ADEID_DFORK)
222 of->of_flags = AFPFORK_DATA;
224 of->of_flags = AFPFORK_RSRC;
229 struct ofork *of_find(const u_int16_t ofrefnum )
234 return( oforks[ ofrefnum % nforks ] );
238 of_findname(const struct vol *vol, const struct dir *dir, const char *name)
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))
252 void of_dealloc( of )
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;
267 oforks[ of->of_refnum ] = NULL;
270 /* decrease refcount */
271 of->of_ad->ad_refcount--;
273 if ( of->of_ad->ad_refcount <= 0) {
275 } else {/* someone's still using it. just free this user's locks */
276 ad_unlock(of->of_ad, of->of_refnum);