2 * Copyright (c) 1996 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
9 #include <sys/stat.h> /* works around a bug */
10 #include <sys/param.h>
13 #include <atalk/adouble.h>
17 #include "directory.h"
20 /* we need to have a hashed list of oforks (by name). just hash
22 #define OFORK_HASHSIZE 64
23 static struct ofork *ofork_table[OFORK_HASHSIZE];
25 static struct ofork **oforks = NULL;
26 static int nforks = 0;
27 static u_short lastrefnum = 0;
30 /* OR some of each character for the hash*/
31 static __inline__ unsigned long hashfn(const char *name)
36 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
38 return i & (OFORK_HASHSIZE - 1);
41 static __inline__ void of_hash(struct ofork *of)
45 table = &ofork_table[hashfn(of->of_name)];
46 if ((of->next = *table) != NULL)
47 (*table)->prevp = &of->next;
52 static __inline__ void of_unhash(struct ofork *of)
56 of->next->prevp = of->prevp;
57 *(of->prevp) = of->next;
61 void of_pforkdesc( f )
69 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
70 if ( oforks[ ofrefnum ] != NULL ) {
71 fprintf( f, "%hu <%s>\n", ofrefnum, oforks[ ofrefnum ]->of_name);
76 int of_flush(const struct vol *vol)
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" );
93 int of_rename(vol, olddir, oldpath, newdir, newpath)
94 const struct vol *vol;
95 struct dir *olddir, *newdir;
96 const char *oldpath, *newpath;
98 struct ofork *of, *next, *d_ofork;
100 next = ofork_table[hashfn(oldpath)];
101 while ((of = next)) {
102 next = next->next; /* so we can unhash and still be all right. */
104 if ((vol == of->of_vol) && (olddir == of->of_dir) &&
105 (strcmp(of->of_name, oldpath) == 0)) {
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;
111 if (!(d_ofork = newdir->d_ofork)) {
112 newdir->d_ofork = of;
113 of->of_d_next = of->of_d_prev = of;
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;
128 of_alloc(vol, dir, path, ofrefnum, eid, ad)
136 struct ofork *of, *d_ofork;
137 u_int16_t refnum, of_refnum;
142 nforks = (getdtablesize() - 10) / 2;
143 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
148 for ( refnum = lastrefnum++, i = 0; i < nforks; i++, refnum++ ) {
149 if ( oforks[ refnum % nforks ] == NULL ) {
154 syslog(LOG_ERR, "of_alloc: maximum number of forks exceeded.");
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" );
164 of = oforks[of_refnum];
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" );
176 if (!(d_ofork = dir->d_ofork)) {
178 of->of_d_next = of->of_d_prev = of;
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;
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)) ==
191 syslog( LOG_ERR, "of_alloc: malloc: %m" );
195 oforks[ of_refnum ] = NULL;
198 strncpy( of->of_name, path, of->of_namelen = MACFILELEN + 1);
200 of->of_refnum = of_refnum;
203 if (eid == ADEID_DFORK)
204 of->of_flags = AFPFORK_DATA;
206 of->of_flags = AFPFORK_RSRC;
211 struct ofork *of_find(const u_int16_t ofrefnum )
216 return( oforks[ ofrefnum % nforks ] );
220 of_findname(const struct vol *vol, const struct dir *dir, const char *name)
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))
234 void of_dealloc( of )
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;
249 oforks[ of->of_refnum ] = NULL;
252 if ((of->of_ad->ad_hf.adf_fd == -1) &&
253 (of->of_ad->ad_df.adf_fd == -1)) {
255 } else {/* someone's still using it. just free this user's locks */
256 ad_unlock(of->of_ad, of->of_refnum);