2 * Copyright (c) 1996 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
13 #include <sys/stat.h> /* works around a bug */
14 #include <sys/param.h>
17 #include <atalk/adouble.h>
21 #include "directory.h"
24 /* we need to have a hashed list of oforks (by name). just hash
26 #define OFORK_HASHSIZE 64
27 static struct ofork *ofork_table[OFORK_HASHSIZE];
29 static struct ofork **oforks = NULL;
30 static int nforks = 0;
31 static u_short lastrefnum = 0;
34 /* OR some of each character for the hash*/
35 static __inline__ unsigned long hashfn(const char *name)
40 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
42 return i & (OFORK_HASHSIZE - 1);
45 static __inline__ void of_hash(struct ofork *of)
49 table = &ofork_table[hashfn(of->of_name)];
50 if ((of->next = *table) != NULL)
51 (*table)->prevp = &of->next;
56 static __inline__ void of_unhash(struct ofork *of)
60 of->next->prevp = of->prevp;
61 *(of->prevp) = of->next;
65 void of_pforkdesc( f )
73 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
74 if ( oforks[ ofrefnum ] != NULL ) {
75 fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
80 int of_flush(const struct vol *vol)
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" );
97 int of_rename(vol, olddir, oldpath, newdir, newpath)
98 const struct vol *vol;
99 struct dir *olddir, *newdir;
100 const char *oldpath, *newpath;
102 struct ofork *of, *next, *d_ofork;
104 next = ofork_table[hashfn(oldpath)];
105 while ((of = next)) {
106 next = next->next; /* so we can unhash and still be all right. */
108 if ((vol == of->of_vol) && (olddir == of->of_dir) &&
109 (strcmp(of->of_name, oldpath) == 0)) {
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;
115 if (!(d_ofork = newdir->d_ofork)) {
116 newdir->d_ofork = of;
117 of->of_d_next = of->of_d_prev = of;
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;
132 of_alloc(vol, dir, path, ofrefnum, eid, ad)
140 struct ofork *of, *d_ofork;
141 u_int16_t refnum, of_refnum;
146 nforks = (getdtablesize() - 10) / 2;
147 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
152 for ( refnum = lastrefnum++, i = 0; i < nforks; i++, refnum++ ) {
153 if ( oforks[ refnum % nforks ] == NULL ) {
158 syslog(LOG_ERR, "of_alloc: maximum number of forks exceeded.");
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" );
168 of = oforks[of_refnum];
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" );
180 if (!(d_ofork = dir->d_ofork)) {
182 of->of_d_next = of->of_d_prev = of;
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;
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)) ==
195 syslog( LOG_ERR, "of_alloc: malloc: %m" );
199 oforks[ of_refnum ] = NULL;
202 strncpy( of->of_name, path, of->of_namelen = MACFILELEN + 1);
204 of->of_refnum = of_refnum;
207 if (eid == ADEID_DFORK)
208 of->of_flags = AFPFORK_DATA;
210 of->of_flags = AFPFORK_RSRC;
215 struct ofork *of_find(const u_int16_t ofrefnum )
220 return( oforks[ ofrefnum % nforks ] );
224 of_findname(const struct vol *vol, const struct dir *dir, const char *name)
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))
238 void of_dealloc( of )
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;
253 oforks[ of->of_refnum ] = NULL;
256 if ((of->of_ad->ad_hf.adf_fd == -1) &&
257 (of->of_ad->ad_df.adf_fd == -1)) {
259 } else {/* someone's still using it. just free this user's locks */
260 ad_unlock(of->of_ad, of->of_refnum);