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;
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
122 && olddir->d_did == of->of_did
123 && s_of->key.dev == of->key.dev
124 && s_of->key.inode == of->key.inode ) {
126 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
129 if (newdir != olddir)
130 of->of_did = newdir->d_did;
137 #define min(a,b) ((a)<(b)?(a):(b))
140 of_alloc(struct vol *vol,
149 u_int16_t refnum, of_refnum;
154 nforks = getdtablesize() - 10;
155 /* protect against insane ulimit -n */
156 nforks = min(nforks, 0xffff);
157 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
162 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
163 /* cf AFP3.0.pdf, File fork page 40 */
166 if ( oforks[ refnum % nforks ] == NULL ) {
170 /* grr, Apple and their 'uniquely identifies'
171 the next line is a protection against
186 same if lastrefnum++ rather than ++lastrefnum.
190 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
194 of_refnum = refnum % nforks;
195 if (( oforks[ of_refnum ] =
196 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
197 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
200 of = oforks[of_refnum];
202 /* see if we need to allocate space for the adouble struct */
204 ad = malloc( sizeof( struct adouble ) );
206 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
208 oforks[ of_refnum ] = NULL;
212 /* initialize to zero. This is important to ensure that
213 ad_open really does reinitialize the structure. */
214 ad_init(ad, vol->v_adouble, vol->v_ad_options);
216 ad->ad_m_namelen = 255 +1;
217 /* here's the deal: we allocate enough for the standard mac file length.
218 * in the future, we'll reallocate in fairly large jumps in case
219 * of long unicode names */
220 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
221 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
224 oforks[ of_refnum ] = NULL;
227 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
229 /* Increase the refcount on this struct adouble. This is
230 decremented again in oforc_dealloc. */
236 of->of_did = dir->d_did;
239 of->of_refnum = refnum;
240 of->key.dev = st->st_dev;
241 of->key.inode = st->st_ino;
242 if (eid == ADEID_DFORK)
243 of->of_flags = AFPFORK_DATA;
245 of->of_flags = AFPFORK_RSRC;
251 struct ofork *of_find(const u_int16_t ofrefnum )
253 if (!oforks || !nforks)
256 return( oforks[ ofrefnum % nforks ] );
259 /* -------------------------- */
260 int of_stat(struct path *path)
267 if ((ret = lstat(path->u_name, &path->st)) < 0) {
268 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
269 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
270 path->st_errno = errno;
278 int of_fstatat(int dirfd, struct path *path)
285 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
286 path->st_errno = errno;
290 #endif /* HAVE_RENAMEAT */
292 /* --------------------------
293 stat the current directory.
294 stat(".") works even if "." is deleted thus
295 we have to stat ../name because we want to know if it's there
297 int of_statdir(struct vol *vol, struct path *path)
299 static char pathname[ MAXPATHLEN + 1] = "../";
306 return of_stat (path);
310 /* FIXME, what about: we don't have r-x perm anymore ? */
311 len = blength(path->d_dir->d_u_name);
312 if (len > (MAXPATHLEN - 3))
313 len = MAXPATHLEN - 3;
314 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
316 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
318 if (!(ret = lstat(pathname, &path->st)))
321 path->st_errno = errno;
323 /* hmm, can't stat curdir anymore */
324 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
325 if (movecwd(vol, dir))
329 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
330 path->st_errno = errno;
336 /* -------------------------- */
337 struct ofork *of_findname(struct path *path)
342 if (!path->st_valid) {
349 key.dev = path->st.st_dev;
350 key.inode = path->st.st_ino;
352 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
353 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
362 * @brief Search for open fork by dirfd/name
364 * Function call of_fstatat with dirfd and path and uses dev and ino
365 * to search the open fork table.
367 * @param dirfd (r) directory fd
368 * @param path (rw) pointer to struct path
371 struct ofork *of_findnameat(int dirfd, struct path *path)
376 if ( ! path->st_valid) {
377 of_fstatat(dirfd, path);
383 key.dev = path->st.st_dev;
384 key.inode = path->st.st_ino;
386 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
387 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
396 void of_dealloc( struct ofork *of)
402 oforks[ of->of_refnum % nforks ] = NULL;
404 /* decrease refcount */
405 of->of_ad->ad_refcount--;
407 if ( of->of_ad->ad_refcount <= 0) {
408 free( of->of_ad->ad_m_name );
410 } else {/* someone's still using it. just free this user's locks */
411 ad_unlock(of->of_ad, of->of_refnum);
417 /* --------------------------- */
418 int of_closefork(struct ofork *ofork)
421 int adflags, doflush = 0;
425 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
426 adflags |= ADFLAGS_DF;
428 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
429 adflags |= ADFLAGS_HF;
431 * Only set the rfork's length if we're closing the rfork.
433 if ((ofork->of_flags & AFPFORK_RSRC)) {
434 ad_refresh( ofork->of_ad );
435 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
436 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
440 ad_flush( ofork->of_ad );
445 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
453 /* ----------------------
456 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
461 if ((of = of_findname(path))) {
464 ad_init(ad, vol->v_adouble, vol->v_ad_options);
470 /* ----------------------
471 close all forks for a volume
473 void of_closevol(const struct vol *vol)
480 for ( refnum = 0; refnum < nforks; refnum++ ) {
481 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
482 if (of_closefork( oforks[ refnum ]) < 0 ) {
483 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );