2 * $Id: ofork.c,v 1.32 2010/03/12 15:16:49 franklahm 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>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
24 #include <atalk/bstrlib.h>
25 #include <atalk/bstradd.h>
29 #include "directory.h"
32 /* we need to have a hashed list of oforks (by dev inode). just hash
34 #define OFORK_HASHSIZE 64
35 static struct ofork *ofork_table[OFORK_HASHSIZE];
37 static struct ofork **oforks = NULL;
38 static int nforks = 0;
39 static u_short lastrefnum = 0;
42 /* OR some of each character for the hash*/
43 static unsigned long hashfn(const struct file_key *key)
48 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
51 return key->inode & (OFORK_HASHSIZE - 1);
54 static void of_hash(struct ofork *of)
58 table = &ofork_table[hashfn(&of->key)];
59 if ((of->next = *table) != NULL)
60 (*table)->prevp = &of->next;
65 static void of_unhash(struct ofork *of)
69 of->next->prevp = of->prevp;
70 *(of->prevp) = of->next;
75 void of_pforkdesc( FILE *f)
82 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
83 if ( oforks[ ofrefnum ] != NULL ) {
84 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
90 int of_flush(const struct vol *vol)
97 for ( refnum = 0; refnum < nforks; refnum++ ) {
98 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
99 flushfork( oforks[ refnum ] ) < 0 ) {
100 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
106 int of_rename(const struct vol *vol,
108 struct dir *olddir, const char *oldpath _U_,
109 struct dir *newdir, const char *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(struct vol *vol,
162 struct ofork *of, *d_ofork;
163 u_int16_t refnum, of_refnum;
168 nforks = getdtablesize() - 10;
169 /* protect against insane ulimit -n */
170 nforks = min(nforks, 0xffff);
171 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
176 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
177 /* cf AFP3.0.pdf, File fork page 40 */
180 if ( oforks[ refnum % nforks ] == NULL ) {
184 /* grr, Apple and their 'uniquely identifies'
185 the next line is a protection against
200 same if lastrefnum++ rather than ++lastrefnum.
204 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
208 of_refnum = refnum % nforks;
209 if (( oforks[ of_refnum ] =
210 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
211 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
214 of = oforks[of_refnum];
216 /* see if we need to allocate space for the adouble struct */
218 ad = malloc( sizeof( struct adouble ) );
220 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
222 oforks[ of_refnum ] = NULL;
226 /* initialize to zero. This is important to ensure that
227 ad_open really does reinitialize the structure. */
228 ad_init(ad, vol->v_adouble, vol->v_ad_options);
230 ad->ad_m_namelen = 255 +1;
231 /* here's the deal: we allocate enough for the standard mac file length.
232 * in the future, we'll reallocate in fairly large jumps in case
233 * of long unicode names */
234 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
235 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
238 oforks[ of_refnum ] = NULL;
241 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
243 /* Increase the refcount on this struct adouble. This is
244 decremented again in oforc_dealloc. */
252 if (!(d_ofork = dir->d_ofork)) {
254 of->of_d_next = of->of_d_prev = of;
256 of->of_d_next = d_ofork;
257 of->of_d_prev = d_ofork->of_d_prev;
258 d_ofork->of_d_prev->of_d_next = of;
259 d_ofork->of_d_prev = of;
263 of->of_refnum = refnum;
264 of->key.dev = st->st_dev;
265 of->key.inode = st->st_ino;
266 if (eid == ADEID_DFORK)
267 of->of_flags = AFPFORK_DATA;
269 of->of_flags = AFPFORK_RSRC;
275 struct ofork *of_find(const u_int16_t ofrefnum )
277 if (!oforks || !nforks)
280 return( oforks[ ofrefnum % nforks ] );
283 /* -------------------------- */
284 int of_stat(struct path *path)
291 if ((ret = lstat(path->u_name, &path->st)) < 0) {
292 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
293 cfrombstring(curdir->d_fullpath), path->u_name, strerror(errno));
294 path->st_errno = errno;
302 int of_fstatat(int dirfd, struct path *path)
309 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
310 path->st_errno = errno;
314 #endif /* HAVE_RENAMEAT */
316 /* --------------------------
317 stat the current directory.
318 stat(".") works even if "." is deleted thus
319 we have to stat ../name because we want to know if it's there
321 int of_statdir(struct vol *vol, struct path *path)
323 static char pathname[ MAXPATHLEN + 1] = "../";
330 return of_stat (path);
334 /* FIXME, what about: we don't have r-x perm anymore ? */
335 len = blength(path->d_dir->d_u_name);
336 if (len > (MAXPATHLEN - 3))
337 len = MAXPATHLEN - 3;
338 strncpy(pathname + 3, cfrombstring(path->d_dir->d_u_name), len + 1);
340 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
342 if (!(ret = lstat(pathname, &path->st)))
345 path->st_errno = errno;
347 /* hmm, can't stat curdir anymore */
348 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
349 if (movecwd(vol, dir))
353 if ((ret = lstat(cfrombstring(path->d_dir->d_u_name), &path->st)) < 0)
354 path->st_errno = errno;
360 /* -------------------------- */
361 struct ofork *of_findname(struct path *path)
366 if (!path->st_valid) {
373 key.dev = path->st.st_dev;
374 key.inode = path->st.st_ino;
376 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
377 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
386 * @brief Search for open fork by dirfd/name
388 * Function call of_fstatat with dirfd and path and uses dev and ino
389 * to search the open fork table.
391 * @param dirfd (r) directory fd
392 * @param path (rw) pointer to struct path
395 struct ofork *of_findnameat(int dirfd, struct path *path)
400 if ( ! path->st_valid) {
401 of_fstatat(dirfd, path);
407 key.dev = path->st.st_dev;
408 key.inode = path->st.st_ino;
410 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
411 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
420 void of_dealloc( struct ofork *of)
428 of->of_d_prev->of_d_next = of->of_d_next;
429 of->of_d_next->of_d_prev = of->of_d_prev;
430 if (of->of_dir->d_ofork == of) {
431 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
434 oforks[ of->of_refnum % nforks ] = NULL;
436 /* decrease refcount */
437 of->of_ad->ad_refcount--;
439 if ( of->of_ad->ad_refcount <= 0) {
440 free( of->of_ad->ad_m_name );
442 } else {/* someone's still using it. just free this user's locks */
443 ad_unlock(of->of_ad, of->of_refnum);
449 /* --------------------------- */
450 int of_closefork(struct ofork *ofork)
453 int adflags, doflush = 0;
457 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
458 adflags |= ADFLAGS_DF;
460 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
461 adflags |= ADFLAGS_HF;
463 * Only set the rfork's length if we're closing the rfork.
465 if ((ofork->of_flags & AFPFORK_RSRC)) {
466 ad_refresh( ofork->of_ad );
467 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
468 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
472 ad_flush( ofork->of_ad );
477 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
485 /* ----------------------
488 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
493 if ((of = of_findname(path))) {
496 ad_init(ad, vol->v_adouble, vol->v_ad_options);
502 /* ----------------------
503 close all forks for a volume
505 void of_closevol(const struct vol *vol)
512 for ( refnum = 0; refnum < nforks; refnum++ ) {
513 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
514 if (of_closefork( oforks[ refnum ]) < 0 ) {
515 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );