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>
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 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 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 void of_unhash(struct ofork *of)
67 of->next->prevp = of->prevp;
68 *(of->prevp) = of->next;
72 int of_flush(const struct vol *vol)
79 for ( refnum = 0; refnum < nforks; refnum++ ) {
80 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
81 flushfork( oforks[ refnum ] ) < 0 ) {
82 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
88 int of_rename(const struct vol *vol,
90 struct dir *olddir, const char *oldpath _U_,
91 struct dir *newdir, const char *newpath)
93 struct ofork *of, *next;
99 next = ofork_table[hashfn(&s_of->key)];
100 while ((of = next)) {
101 next = next->next; /* so we can unhash and still be all right. */
103 if (vol == of->of_vol
104 && olddir->d_did == of->of_did
105 && s_of->key.dev == of->key.dev
106 && s_of->key.inode == of->key.inode ) {
108 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
111 if (newdir != olddir)
112 of->of_did = newdir->d_did;
119 #define min(a,b) ((a)<(b)?(a):(b))
122 of_alloc(struct vol *vol,
131 u_int16_t refnum, of_refnum;
136 nforks = getdtablesize() - 10;
137 /* protect against insane ulimit -n */
138 nforks = min(nforks, 0xffff);
139 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
144 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
145 /* cf AFP3.0.pdf, File fork page 40 */
148 if ( oforks[ refnum % nforks ] == NULL ) {
152 /* grr, Apple and their 'uniquely identifies'
153 the next line is a protection against
168 same if lastrefnum++ rather than ++lastrefnum.
172 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
176 of_refnum = refnum % nforks;
177 if (( oforks[ of_refnum ] =
178 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
179 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
182 of = oforks[of_refnum];
184 /* see if we need to allocate space for the adouble struct */
186 ad = malloc( sizeof( struct adouble ) );
188 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
190 oforks[ of_refnum ] = NULL;
194 /* initialize to zero. This is important to ensure that
195 ad_open really does reinitialize the structure. */
196 ad_init(ad, vol->v_adouble, vol->v_ad_options);
198 ad->ad_m_namelen = 255 +1;
199 /* here's the deal: we allocate enough for the standard mac file length.
200 * in the future, we'll reallocate in fairly large jumps in case
201 * of long unicode names */
202 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
203 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
206 oforks[ of_refnum ] = NULL;
209 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
215 of->of_did = dir->d_did;
218 of->of_refnum = refnum;
219 of->key.dev = st->st_dev;
220 of->key.inode = st->st_ino;
221 if (eid == ADEID_DFORK)
222 of->of_flags = AFPFORK_DATA;
224 of->of_flags = AFPFORK_RSRC;
230 struct ofork *of_find(const u_int16_t ofrefnum )
232 if (!oforks || !nforks)
235 return( oforks[ ofrefnum % nforks ] );
238 /* -------------------------- */
239 int of_stat(struct path *path)
246 if ((ret = lstat(path->u_name, &path->st)) < 0) {
247 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
248 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
249 path->st_errno = errno;
257 int of_fstatat(int dirfd, struct path *path)
264 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
265 path->st_errno = errno;
269 #endif /* HAVE_RENAMEAT */
271 /* --------------------------
272 stat the current directory.
273 stat(".") works even if "." is deleted thus
274 we have to stat ../name because we want to know if it's there
276 int of_statdir(struct vol *vol, struct path *path)
278 static char pathname[ MAXPATHLEN + 1] = "../";
285 return of_stat (path);
289 /* FIXME, what about: we don't have r-x perm anymore ? */
290 len = blength(path->d_dir->d_u_name);
291 if (len > (MAXPATHLEN - 3))
292 len = MAXPATHLEN - 3;
293 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
295 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
297 if (!(ret = lstat(pathname, &path->st)))
300 path->st_errno = errno;
302 /* hmm, can't stat curdir anymore */
303 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
304 if (movecwd(vol, dir))
308 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
309 path->st_errno = errno;
315 /* -------------------------- */
316 struct ofork *of_findname(struct path *path)
321 if (!path->st_valid) {
328 key.dev = path->st.st_dev;
329 key.inode = path->st.st_ino;
331 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
332 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
341 * @brief Search for open fork by dirfd/name
343 * Function call of_fstatat with dirfd and path and uses dev and ino
344 * to search the open fork table.
346 * @param dirfd (r) directory fd
347 * @param path (rw) pointer to struct path
350 struct ofork *of_findnameat(int dirfd, struct path *path)
355 if ( ! path->st_valid) {
356 of_fstatat(dirfd, path);
362 key.dev = path->st.st_dev;
363 key.inode = path->st.st_ino;
365 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
366 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
375 void of_dealloc( struct ofork *of)
381 oforks[ of->of_refnum % nforks ] = NULL;
385 /* --------------------------- */
386 int of_closefork(struct ofork *ofork)
392 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
394 * Only set the rfork's length if we're closing the rfork.
396 if ((ofork->of_flags & AFPFORK_RSRC)) {
397 ad_refresh( ofork->of_ad );
398 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
399 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
403 ad_flush( ofork->of_ad );
409 ad_unlock(ofork->of_ad, ofork->of_refnum);
411 if (ad_unref(ofork->of_ad) == 0) {
412 if ( ad_close( ofork->of_ad, 0 ) < 0 ) {
421 /* ----------------------
424 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
429 if ((of = of_findname(path))) {
432 ad_init(ad, vol->v_adouble, vol->v_ad_options);
438 /* ----------------------
439 close all forks for a volume
441 void of_closevol(const struct vol *vol)
448 for ( refnum = 0; refnum < nforks; refnum++ ) {
449 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
450 if (of_closefork( oforks[ refnum ]) < 0 ) {
451 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );