2 * Copyright (c) 1996 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/stat.h> /* works around a bug */
17 #include <sys/param.h>
20 #include <atalk/logger.h>
21 #include <atalk/util.h>
22 #include <atalk/bstrlib.h>
23 #include <atalk/bstradd.h>
24 #include <atalk/globals.h>
27 #include "directory.h"
30 /* we need to have a hashed list of oforks (by dev inode) */
31 #define OFORK_HASHSIZE 64
32 static struct ofork *ofork_table[OFORK_HASHSIZE]; /* forks hashed by dev/inode */
33 static struct ofork **oforks = NULL; /* point to allocated table of open forks pointers */
34 static int nforks = 0;
35 static u_short lastrefnum = 0;
38 /* OR some of each character for the hash*/
39 static unsigned long hashfn(const struct file_key *key)
41 return key->inode & (OFORK_HASHSIZE - 1);
44 static void of_hash(struct ofork *of)
48 table = &ofork_table[hashfn(&of->key)];
49 if ((of->next = *table) != NULL)
50 (*table)->prevp = &of->next;
55 static void of_unhash(struct ofork *of)
59 of->next->prevp = of->prevp;
60 *(of->prevp) = of->next;
65 void of_pforkdesc( FILE *f)
72 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
73 if ( oforks[ ofrefnum ] != NULL ) {
74 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
80 int of_flush(const struct vol *vol)
87 for ( refnum = 0; refnum < nforks; refnum++ ) {
88 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
89 flushfork( oforks[ refnum ] ) < 0 ) {
90 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
96 int of_rename(const struct vol *vol,
98 struct dir *olddir, const char *oldpath _U_,
99 struct dir *newdir, const char *newpath)
101 struct ofork *of, *next;
107 next = ofork_table[hashfn(&s_of->key)];
108 while ((of = next)) {
109 next = next->next; /* so we can unhash and still be all right. */
111 if (vol == of->of_vol
112 && olddir->d_did == of->of_did
113 && s_of->key.dev == of->key.dev
114 && s_of->key.inode == of->key.inode ) {
116 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
119 if (newdir != olddir)
120 of->of_did = newdir->d_did;
127 #define min(a,b) ((a)<(b)?(a):(b))
130 of_alloc(struct vol *vol,
139 u_int16_t refnum, of_refnum;
144 nforks = getdtablesize() - 10;
145 /* protect against insane ulimit -n */
146 nforks = min(nforks, 0xffff);
147 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
152 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
153 /* cf AFP3.0.pdf, File fork page 40 */
156 if ( oforks[ refnum % nforks ] == NULL ) {
160 /* grr, Apple and their 'uniquely identifies'
161 the next line is a protection against
176 same if lastrefnum++ rather than ++lastrefnum.
180 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
184 of_refnum = refnum % nforks;
185 if (( oforks[ of_refnum ] =
186 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
187 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
190 of = oforks[of_refnum];
192 /* see if we need to allocate space for the adouble struct */
194 ad = malloc( sizeof( struct adouble ) );
196 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
198 oforks[ of_refnum ] = NULL;
202 /* initialize to zero. This is important to ensure that
203 ad_open really does reinitialize the structure. */
204 ad_init(ad, vol->v_adouble, vol->v_ad_options);
206 ad->ad_m_namelen = UTF8FILELEN_EARLY +1;
207 /* here's the deal: we allocate enough for the standard mac file length.
208 * in the future, we'll reallocate in fairly large jumps in case
209 * of long unicode names */
210 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
211 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
214 oforks[ of_refnum ] = NULL;
217 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
219 /* Increase the refcount on this struct adouble. This is
220 decremented again in oforc_dealloc. */
226 of->of_did = dir->d_did;
229 of->of_refnum = refnum;
230 of->key.dev = st->st_dev;
231 of->key.inode = st->st_ino;
232 if (eid == ADEID_DFORK)
233 of->of_flags = AFPFORK_DATA;
235 of->of_flags = AFPFORK_RSRC;
241 struct ofork *of_find(const u_int16_t ofrefnum )
243 if (!oforks || !nforks)
246 return( oforks[ ofrefnum % nforks ] );
249 /* -------------------------- */
250 int of_stat(struct path *path)
257 if ((ret = lstat(path->u_name, &path->st)) < 0) {
258 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
259 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
260 path->st_errno = errno;
268 int of_fstatat(int dirfd, struct path *path)
275 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
276 path->st_errno = errno;
280 #endif /* HAVE_ATFUNCS */
282 /* --------------------------
283 stat the current directory.
284 stat(".") works even if "." is deleted thus
285 we have to stat ../name because we want to know if it's there
287 int of_statdir(struct vol *vol, struct path *path)
289 static char pathname[ MAXPATHLEN + 1] = "../";
296 return of_stat (path);
300 /* FIXME, what about: we don't have r-x perm anymore ? */
301 len = blength(path->d_dir->d_u_name);
302 if (len > (MAXPATHLEN - 3))
303 len = MAXPATHLEN - 3;
304 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
306 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
308 if (!(ret = lstat(pathname, &path->st)))
311 path->st_errno = errno;
313 /* hmm, can't stat curdir anymore */
314 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
315 if (movecwd(vol, dir))
319 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
320 path->st_errno = errno;
326 /* -------------------------- */
327 struct ofork *of_findname(struct path *path)
332 if (!path->st_valid) {
339 key.dev = path->st.st_dev;
340 key.inode = path->st.st_ino;
342 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
343 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
352 * @brief Search for open fork by dirfd/name
354 * Function call of_fstatat with dirfd and path and uses dev and ino
355 * to search the open fork table.
357 * @param dirfd (r) directory fd
358 * @param path (rw) pointer to struct path
361 struct ofork *of_findnameat(int dirfd, struct path *path)
366 if ( ! path->st_valid) {
367 of_fstatat(dirfd, path);
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 void of_dealloc( struct ofork *of)
392 oforks[ of->of_refnum % nforks ] = NULL;
394 /* decrease refcount */
395 of->of_ad->ad_refcount--;
397 if ( of->of_ad->ad_refcount <= 0) {
398 free( of->of_ad->ad_m_name );
400 } else {/* someone's still using it. just free this user's locks */
401 ad_unlock(of->of_ad, of->of_refnum);
407 /* --------------------------- */
408 int of_closefork(struct ofork *ofork)
411 int adflags, doflush = 0;
415 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
416 adflags |= ADFLAGS_DF;
418 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
419 adflags |= ADFLAGS_HF;
421 * Only set the rfork's length if we're closing the rfork.
423 if ((ofork->of_flags & AFPFORK_RSRC)) {
424 ad_refresh( ofork->of_ad );
425 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
426 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
430 ad_flush( ofork->of_ad );
435 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
443 /* ----------------------
446 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
451 if ((of = of_findname(path))) {
454 ad_init(ad, vol->v_adouble, vol->v_ad_options);
460 /* ----------------------
461 close all forks for a volume
463 void of_closevol(const struct vol *vol)
470 for ( refnum = 0; refnum < nforks; refnum++ ) {
471 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
472 if (of_closefork( oforks[ refnum ]) < 0 ) {
473 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
480 /* ----------------------
481 close all forks for a volume
483 void of_close_all_forks(void)
490 for ( refnum = 0; refnum < nforks; refnum++ ) {
491 if (oforks[ refnum ] != NULL) {
492 if (of_closefork( oforks[ refnum ]) < 0 ) {
493 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );