2 * $Id: ofork.c,v 1.20.6.6.2.7 2008-11-25 15:16:33 didg Exp $
4 * Copyright (c) 1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #include <sys/stat.h> /* works around a bug */
19 #include <sys/param.h>
20 #include <atalk/logger.h>
23 #include <atalk/util.h>
27 #include "directory.h"
30 /* we need to have a hashed list of oforks (by dev inode). just hash
32 #define OFORK_HASHSIZE 64
33 static struct ofork *ofork_table[OFORK_HASHSIZE];
35 static struct ofork **oforks = NULL;
36 static int nforks = 0;
37 static u_short lastrefnum = 0;
40 /* OR some of each character for the hash*/
41 static unsigned long hashfn(const struct file_key *key)
46 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
49 return key->inode & (OFORK_HASHSIZE - 1);
52 static void of_hash(struct ofork *of)
56 table = &ofork_table[hashfn(&of->key)];
57 if ((of->next = *table) != NULL)
58 (*table)->prevp = &of->next;
63 static void of_unhash(struct ofork *of)
67 of->next->prevp = of->prevp;
68 *(of->prevp) = of->next;
73 void of_pforkdesc( f )
81 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
82 if ( oforks[ ofrefnum ] != NULL ) {
83 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
89 int of_flush(const struct vol *vol)
96 for ( refnum = 0; refnum < nforks; refnum++ ) {
97 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
98 flushfork( oforks[ refnum ] ) < 0 ) {
99 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
105 int of_rename(vol, s_of, olddir, oldpath, newdir, newpath)
106 const struct vol *vol;
108 struct dir *olddir, *newdir;
109 const char *oldpath _U_, *newpath;
111 struct ofork *of, *next, *d_ofork;
117 next = ofork_table[hashfn(&s_of->key)];
118 while ((of = next)) {
119 next = next->next; /* so we can unhash and still be all right. */
121 if (vol == of->of_vol && olddir == of->of_dir &&
122 s_of->key.dev == of->key.dev &&
123 s_of->key.inode == of->key.inode ) {
125 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
128 if (newdir != olddir) {
129 of->of_d_prev->of_d_next = of->of_d_next;
130 of->of_d_next->of_d_prev = of->of_d_prev;
131 if (of->of_dir->d_ofork == of) {
132 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
135 if (!(d_ofork = newdir->d_ofork)) {
136 newdir->d_ofork = of;
137 of->of_d_next = of->of_d_prev = of;
139 of->of_d_next = d_ofork;
140 of->of_d_prev = d_ofork->of_d_prev;
141 of->of_d_prev->of_d_next = of;
142 d_ofork->of_d_prev = of;
151 #define min(a,b) ((a)<(b)?(a):(b))
154 of_alloc(vol, dir, path, ofrefnum, eid, ad, st)
163 struct ofork *of, *d_ofork;
164 u_int16_t refnum, of_refnum;
169 nforks = getdtablesize() - 10;
170 /* protect against insane ulimit -n */
171 nforks = min(nforks, 0xffff);
172 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
177 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
178 /* cf AFP3.0.pdf, File fork page 40 */
181 if ( oforks[ refnum % nforks ] == NULL ) {
185 /* grr, Apple and their 'uniquely identifies'
186 the next line is a protection against
201 same if lastrefnum++ rather than ++lastrefnum.
205 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
209 of_refnum = refnum % nforks;
210 if (( oforks[ of_refnum ] =
211 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
212 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
215 of = oforks[of_refnum];
217 /* see if we need to allocate space for the adouble struct */
219 ad = malloc( sizeof( struct adouble ) );
221 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
223 oforks[ of_refnum ] = NULL;
227 /* initialize to zero. This is important to ensure that
228 ad_open really does reinitialize the structure. */
229 ad_init(ad, vol->v_adouble, vol->v_ad_options);
231 ad->ad_m_namelen = 255 +1;
232 /* here's the deal: we allocate enough for the standard mac file length.
233 * in the future, we'll reallocate in fairly large jumps in case
234 * of long unicode names */
235 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
236 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
239 oforks[ of_refnum ] = NULL;
242 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
244 /* Increase the refcount on this struct adouble. This is
245 decremented again in oforc_dealloc. */
253 if (!(d_ofork = dir->d_ofork)) {
255 of->of_d_next = of->of_d_prev = of;
257 of->of_d_next = d_ofork;
258 of->of_d_prev = d_ofork->of_d_prev;
259 d_ofork->of_d_prev->of_d_next = of;
260 d_ofork->of_d_prev = of;
264 of->of_refnum = refnum;
265 of->key.dev = st->st_dev;
266 of->key.inode = st->st_ino;
267 if (eid == ADEID_DFORK)
268 of->of_flags = AFPFORK_DATA;
270 of->of_flags = AFPFORK_RSRC;
276 struct ofork *of_find(const u_int16_t ofrefnum )
278 if (!oforks || !nforks)
281 return( oforks[ ofrefnum % nforks ] );
284 /* -------------------------- */
285 int of_stat (struct path *path)
290 if ((ret = stat(path->u_name, &path->st)) < 0)
291 path->st_errno = errno;
295 /* -------------------------- */
296 int of_statdir (const struct vol *vol, struct path *path)
298 static char pathname[ MAXPATHLEN + 1];
303 return of_stat (path);
307 /* FIXME, what about: we don't have r-x perm anymore ? */
308 strcpy(pathname, "../");
309 strlcat(pathname, path->d_dir->d_u_name, MAXPATHLEN);
311 if (!(ret = stat(pathname, &path->st)))
314 path->st_errno = errno;
315 /* hmm, can't stat curdir anymore */
316 if (errno == EACCES && curdir->d_parent ) {
317 if (movecwd(vol, curdir->d_parent))
320 if ((ret = stat(path->d_dir->d_u_name, &path->st)) < 0)
321 path->st_errno = errno;
326 /* -------------------------- */
328 of_findname(struct path *path)
333 if (!path->st_valid) {
340 key.dev = path->st.st_dev;
341 key.inode = path->st.st_ino;
343 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
344 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
352 void of_dealloc( of )
361 of->of_d_prev->of_d_next = of->of_d_next;
362 of->of_d_next->of_d_prev = of->of_d_prev;
363 if (of->of_dir->d_ofork == of) {
364 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
367 oforks[ of->of_refnum % nforks ] = NULL;
369 /* decrease refcount */
370 of->of_ad->ad_refcount--;
372 if ( of->of_ad->ad_refcount <= 0) {
373 free( of->of_ad->ad_m_name );
375 } else {/* someone's still using it. just free this user's locks */
376 ad_unlock(of->of_ad, of->of_refnum);
382 /* ----------------------
385 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
390 if ((of = of_findname(path))) {
393 ad_init(ad, vol->v_adouble, vol->v_ad_options);