2 * $Id: ofork.c,v 1.22 2005-07-08 16:48:45 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 __inline__ 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 __inline__ 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 __inline__ 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, oforks[ ofrefnum ]->of_name);
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;
116 next = ofork_table[hashfn(&s_of->key)];
117 while ((of = next)) {
118 next = next->next; /* so we can unhash and still be all right. */
120 if (vol == of->of_vol && olddir == of->of_dir &&
121 s_of->key.dev == of->key.dev &&
122 s_of->key.inode == of->key.inode ) {
123 strlcpy( of->of_name, newpath, of->of_namelen);
124 if (newdir != olddir) {
125 of->of_d_prev->of_d_next = of->of_d_next;
126 of->of_d_next->of_d_prev = of->of_d_prev;
127 if (of->of_dir->d_ofork == of) {
128 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
131 if (!(d_ofork = newdir->d_ofork)) {
132 newdir->d_ofork = of;
133 of->of_d_next = of->of_d_prev = of;
135 of->of_d_next = d_ofork;
136 of->of_d_prev = d_ofork->of_d_prev;
137 of->of_d_prev->of_d_next = of;
138 d_ofork->of_d_prev = of;
147 #define min(a,b) ((a)<(b)?(a):(b))
150 of_alloc(vol, dir, path, ofrefnum, eid, ad, st)
159 struct ofork *of, *d_ofork;
160 u_int16_t refnum, of_refnum;
165 nforks = (getdtablesize() - 10) / 2;
166 /* protect against insane ulimit -n */
167 nforks = min(nforks, 0xffff);
168 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
173 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
174 /* cf AFP3.0.pdf, File fork page 40 */
177 if ( oforks[ refnum % nforks ] == NULL ) {
181 /* grr, Apple and their 'uniquely identifies'
182 the next line is a protection against
197 same if lastrefnum++ rather than ++lastrefnum.
201 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
205 of_refnum = refnum % nforks;
206 if (( oforks[ of_refnum ] =
207 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
208 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
211 of = oforks[of_refnum];
213 /* see if we need to allocate space for the adouble struct */
215 ad = malloc( sizeof( struct adouble ) );
217 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
221 /* initialize to zero. This is important to ensure that
222 ad_open really does reinitialize the structure. */
223 ad_init(ad, vol->v_adouble, vol->v_ad_options);
225 /* Increase the refcount on this struct adouble. This is
226 decremented again in oforc_dealloc. */
235 if (!(d_ofork = dir->d_ofork)) {
237 of->of_d_next = of->of_d_prev = of;
239 of->of_d_next = d_ofork;
240 of->of_d_prev = d_ofork->of_d_prev;
241 d_ofork->of_d_prev->of_d_next = of;
242 d_ofork->of_d_prev = of;
245 /* here's the deal: we allocate enough for the standard mac file length.
246 * in the future, we'll reallocate in fairly large jumps in case
247 * of long unicode names */
248 if (( of->of_name =(char *)malloc(255 + 1)) == NULL ) {
249 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
253 oforks[ of_refnum ] = NULL;
256 strlcpy( of->of_name, path, of->of_namelen = 255 + 1);
258 of->of_refnum = refnum;
259 of->key.dev = st->st_dev;
260 of->key.inode = st->st_ino;
261 if (eid == ADEID_DFORK)
262 of->of_flags = AFPFORK_DATA;
264 of->of_flags = AFPFORK_RSRC;
270 struct ofork *of_find(const u_int16_t ofrefnum )
272 if (!oforks || !nforks)
275 return( oforks[ ofrefnum % nforks ] );
278 /* -------------------------- */
279 int of_stat (struct path *path)
284 if ((ret = stat(path->u_name, &path->st)) < 0)
285 path->st_errno = errno;
289 /* -------------------------- */
290 int of_statdir (const struct vol *vol, struct path *path)
292 static char pathname[ MAXPATHLEN + 1];
297 return of_stat (path);
301 /* FIXME, what about: we don't have r-x perm anymore ? */
302 strcpy(pathname, "../");
303 strlcat(pathname, path->d_dir->d_u_name, MAXPATHLEN);
305 if (!(ret = stat(pathname, &path->st)))
308 path->st_errno = errno;
309 /* hmm, can't stat curdir anymore */
310 if (errno == EACCES && curdir->d_parent ) {
311 if (movecwd(vol, curdir->d_parent))
314 if ((ret = stat(path->d_dir->d_u_name, &path->st)) < 0)
315 path->st_errno = errno;
320 /* -------------------------- */
322 of_findname(struct path *path)
327 if (!path->st_valid) {
334 key.dev = path->st.st_dev;
335 key.inode = path->st.st_ino;
337 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
338 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
346 void of_dealloc( of )
355 of->of_d_prev->of_d_next = of->of_d_next;
356 of->of_d_next->of_d_prev = of->of_d_prev;
357 if (of->of_dir->d_ofork == of) {
358 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
361 oforks[ of->of_refnum % nforks ] = NULL;
364 /* decrease refcount */
365 of->of_ad->ad_refcount--;
367 if ( of->of_ad->ad_refcount <= 0) {
369 } else {/* someone's still using it. just free this user's locks */
370 ad_unlock(of->of_ad, of->of_refnum);
376 /* --------------------------- */
377 int of_closefork(struct ofork *ofork)
380 int adflags, doflush = 0;
384 if ((ofork->of_flags & AFPFORK_DATA) && (ad_dfileno( ofork->of_ad ) != -1)) {
385 adflags |= ADFLAGS_DF;
387 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_hfileno( ofork->of_ad ) != -1 ) {
388 adflags |= ADFLAGS_HF;
390 * Only set the rfork's length if we're closing the rfork.
392 if ((ofork->of_flags & AFPFORK_RSRC)) {
393 ad_refresh( ofork->of_ad );
394 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
395 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
399 ad_flush( ofork->of_ad, adflags );
404 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
412 /* ----------------------
415 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
420 if ((of = of_findname(path))) {
423 ad_init(ad, vol->v_adouble, vol->v_ad_options);
429 /* ----------------------
430 close all forks for a volume
432 void of_closevol(const struct vol *vol)
439 for ( refnum = 0; refnum < nforks; refnum++ ) {
440 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
441 if (of_closefork( oforks[ refnum ]) < 0 ) {
442 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );