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>
25 #include <atalk/fce_api.h>
28 #include "directory.h"
31 /* we need to have a hashed list of oforks (by dev inode) */
32 #define OFORK_HASHSIZE 64
33 static struct ofork *ofork_table[OFORK_HASHSIZE]; /* forks hashed by dev/inode */
34 static struct ofork **oforks = NULL; /* point to allocated table of open forks pointers */
35 static int nforks = 0;
36 static u_short lastrefnum = 0;
39 /* OR some of each character for the hash*/
40 static unsigned long hashfn(const struct file_key *key)
42 return key->inode & (OFORK_HASHSIZE - 1);
45 static void of_hash(struct ofork *of)
49 table = &ofork_table[hashfn(&of->key)];
50 if ((of->next = *table) != NULL)
51 (*table)->prevp = &of->next;
56 static void of_unhash(struct ofork *of)
60 of->next->prevp = of->prevp;
61 *(of->prevp) = of->next;
66 void of_pforkdesc( FILE *f)
73 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
74 if ( oforks[ ofrefnum ] != NULL ) {
75 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
81 int of_flush(const struct vol *vol)
88 for ( refnum = 0; refnum < nforks; refnum++ ) {
89 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
90 flushfork( oforks[ refnum ] ) < 0 ) {
91 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
97 int of_rename(const struct vol *vol,
99 struct dir *olddir, const char *oldpath _U_,
100 struct dir *newdir, const char *newpath)
102 struct ofork *of, *next;
108 next = ofork_table[hashfn(&s_of->key)];
109 while ((of = next)) {
110 next = next->next; /* so we can unhash and still be all right. */
112 if (vol == of->of_vol
113 && olddir->d_did == of->of_did
114 && s_of->key.dev == of->key.dev
115 && s_of->key.inode == of->key.inode ) {
117 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
120 if (newdir != olddir)
121 of->of_did = newdir->d_did;
128 #define min(a,b) ((a)<(b)?(a):(b))
131 of_alloc(struct vol *vol,
140 u_int16_t refnum, of_refnum;
145 nforks = getdtablesize() - 10;
146 /* protect against insane ulimit -n */
147 nforks = min(nforks, 0xffff);
148 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
153 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
154 /* cf AFP3.0.pdf, File fork page 40 */
157 if ( oforks[ refnum % nforks ] == NULL ) {
161 /* grr, Apple and their 'uniquely identifies'
162 the next line is a protection against
177 same if lastrefnum++ rather than ++lastrefnum.
181 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
185 of_refnum = refnum % nforks;
186 if (( oforks[ of_refnum ] =
187 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
188 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
191 of = oforks[of_refnum];
193 /* see if we need to allocate space for the adouble struct */
195 ad = malloc( sizeof( struct adouble ) );
197 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
199 oforks[ of_refnum ] = NULL;
203 /* initialize to zero. This is important to ensure that
204 ad_open really does reinitialize the structure. */
205 ad_init(ad, vol->v_adouble, vol->v_ad_options);
207 ad->ad_m_namelen = UTF8FILELEN_EARLY +1;
208 /* here's the deal: we allocate enough for the standard mac file length.
209 * in the future, we'll reallocate in fairly large jumps in case
210 * of long unicode names */
211 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
212 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
215 oforks[ of_refnum ] = NULL;
218 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
220 /* Increase the refcount on this struct adouble. This is
221 decremented again in oforc_dealloc. */
227 of->of_did = dir->d_did;
230 of->of_refnum = refnum;
231 of->key.dev = st->st_dev;
232 of->key.inode = st->st_ino;
233 if (eid == ADEID_DFORK)
234 of->of_flags = AFPFORK_DATA;
236 of->of_flags = AFPFORK_RSRC;
242 struct ofork *of_find(const u_int16_t ofrefnum )
244 if (!oforks || !nforks)
247 return( oforks[ ofrefnum % nforks ] );
250 /* -------------------------- */
251 int of_stat(struct path *path)
258 if ((ret = lstat(path->u_name, &path->st)) < 0) {
259 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
260 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
261 path->st_errno = errno;
269 int of_fstatat(int dirfd, struct path *path)
276 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
277 path->st_errno = errno;
281 #endif /* HAVE_ATFUNCS */
283 /* --------------------------
284 stat the current directory.
285 stat(".") works even if "." is deleted thus
286 we have to stat ../name because we want to know if it's there
288 int of_statdir(struct vol *vol, struct path *path)
290 static char pathname[ MAXPATHLEN + 1] = "../";
297 return of_stat (path);
301 /* FIXME, what about: we don't have r-x perm anymore ? */
302 len = blength(path->d_dir->d_u_name);
303 if (len > (MAXPATHLEN - 3))
304 len = MAXPATHLEN - 3;
305 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
307 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
309 if (!(ret = lstat(pathname, &path->st)))
312 path->st_errno = errno;
314 /* hmm, can't stat curdir anymore */
315 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
316 if (movecwd(vol, dir))
320 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
321 path->st_errno = errno;
327 /* -------------------------- */
328 struct ofork *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 ) {
353 * @brief Search for open fork by dirfd/name
355 * Function call of_fstatat with dirfd and path and uses dev and ino
356 * to search the open fork table.
358 * @param dirfd (r) directory fd
359 * @param path (rw) pointer to struct path
362 struct ofork *of_findnameat(int dirfd, struct path *path)
367 if ( ! path->st_valid) {
368 of_fstatat(dirfd, path);
374 key.dev = path->st.st_dev;
375 key.inode = path->st.st_ino;
377 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
378 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
387 void of_dealloc( struct ofork *of)
393 oforks[ of->of_refnum % nforks ] = NULL;
395 /* decrease refcount */
396 of->of_ad->ad_refcount--;
398 if ( of->of_ad->ad_refcount <= 0) {
399 free( of->of_ad->ad_m_name );
401 } else {/* someone's still using it. just free this user's locks */
402 ad_unlock(of->of_ad, of->of_refnum);
408 /* --------------------------- */
409 int of_closefork(struct ofork *ofork)
412 int adflags, doflush = 0;
416 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
417 adflags |= ADFLAGS_DF;
419 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
420 adflags |= ADFLAGS_HF;
422 * Only set the rfork's length if we're closing the rfork.
424 if ((ofork->of_flags & AFPFORK_RSRC)) {
425 ad_refresh( ofork->of_ad );
426 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
427 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
431 ad_flush( ofork->of_ad );
436 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
437 if (ofork->of_flags & AFPFORK_MODIFIED) {
438 fce_register_file_modification(ofork);
442 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
450 /* ----------------------
453 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
458 if ((of = of_findname(path))) {
461 ad_init(ad, vol->v_adouble, vol->v_ad_options);
467 /* ----------------------
468 close all forks for a volume
470 void of_closevol(const struct vol *vol)
477 for ( refnum = 0; refnum < nforks; refnum++ ) {
478 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
479 if (of_closefork( oforks[ refnum ]) < 0 ) {
480 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
487 /* ----------------------
488 close all forks for a volume
490 void of_close_all_forks(void)
497 for ( refnum = 0; refnum < nforks; refnum++ ) {
498 if (oforks[ refnum ] != NULL) {
499 if (of_closefork( oforks[ refnum ]) < 0 ) {
500 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );