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;
73 void of_pforkdesc( FILE *f)
80 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
81 if ( oforks[ ofrefnum ] != NULL ) {
82 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
88 int of_flush(const struct vol *vol)
95 for ( refnum = 0; refnum < nforks; refnum++ ) {
96 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
97 flushfork( oforks[ refnum ] ) < 0 ) {
98 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
104 int of_rename(const struct vol *vol,
106 struct dir *olddir, const char *oldpath _U_,
107 struct dir *newdir, const char *newpath)
109 struct ofork *of, *next;
115 next = ofork_table[hashfn(&s_of->key)];
116 while ((of = next)) {
117 next = next->next; /* so we can unhash and still be all right. */
119 if (vol == of->of_vol
120 && olddir->d_did == of->of_did
121 && s_of->key.dev == of->key.dev
122 && s_of->key.inode == of->key.inode ) {
124 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
127 if (newdir != olddir)
128 of->of_did = newdir->d_did;
135 #define min(a,b) ((a)<(b)?(a):(b))
138 of_alloc(struct vol *vol,
147 u_int16_t refnum, of_refnum;
152 nforks = getdtablesize() - 10;
153 /* protect against insane ulimit -n */
154 nforks = min(nforks, 0xffff);
155 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
160 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
161 /* cf AFP3.0.pdf, File fork page 40 */
164 if ( oforks[ refnum % nforks ] == NULL ) {
168 /* grr, Apple and their 'uniquely identifies'
169 the next line is a protection against
184 same if lastrefnum++ rather than ++lastrefnum.
188 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
192 of_refnum = refnum % nforks;
193 if (( oforks[ of_refnum ] =
194 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
195 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
198 of = oforks[of_refnum];
200 /* see if we need to allocate space for the adouble struct */
202 ad = malloc( sizeof( struct adouble ) );
204 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
206 oforks[ of_refnum ] = NULL;
210 /* initialize to zero. This is important to ensure that
211 ad_open really does reinitialize the structure. */
212 ad_init(ad, vol->v_adouble, vol->v_ad_options);
214 ad->ad_m_namelen = 255 +1;
215 /* here's the deal: we allocate enough for the standard mac file length.
216 * in the future, we'll reallocate in fairly large jumps in case
217 * of long unicode names */
218 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
219 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
222 oforks[ of_refnum ] = NULL;
225 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
231 of->of_did = dir->d_did;
234 of->of_refnum = refnum;
235 of->key.dev = st->st_dev;
236 of->key.inode = st->st_ino;
237 if (eid == ADEID_DFORK)
238 of->of_flags = AFPFORK_DATA;
240 of->of_flags = AFPFORK_RSRC;
246 struct ofork *of_find(const u_int16_t ofrefnum )
248 if (!oforks || !nforks)
251 return( oforks[ ofrefnum % nforks ] );
254 /* -------------------------- */
255 int of_stat(struct path *path)
262 if ((ret = lstat(path->u_name, &path->st)) < 0) {
263 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
264 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
265 path->st_errno = errno;
273 int of_fstatat(int dirfd, struct path *path)
280 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
281 path->st_errno = errno;
285 #endif /* HAVE_RENAMEAT */
287 /* --------------------------
288 stat the current directory.
289 stat(".") works even if "." is deleted thus
290 we have to stat ../name because we want to know if it's there
292 int of_statdir(struct vol *vol, struct path *path)
294 static char pathname[ MAXPATHLEN + 1] = "../";
301 return of_stat (path);
305 /* FIXME, what about: we don't have r-x perm anymore ? */
306 len = blength(path->d_dir->d_u_name);
307 if (len > (MAXPATHLEN - 3))
308 len = MAXPATHLEN - 3;
309 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
311 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
313 if (!(ret = lstat(pathname, &path->st)))
316 path->st_errno = errno;
318 /* hmm, can't stat curdir anymore */
319 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
320 if (movecwd(vol, dir))
324 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
325 path->st_errno = errno;
331 /* -------------------------- */
332 struct ofork *of_findname(struct path *path)
337 if (!path->st_valid) {
344 key.dev = path->st.st_dev;
345 key.inode = path->st.st_ino;
347 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
348 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
357 * @brief Search for open fork by dirfd/name
359 * Function call of_fstatat with dirfd and path and uses dev and ino
360 * to search the open fork table.
362 * @param dirfd (r) directory fd
363 * @param path (rw) pointer to struct path
366 struct ofork *of_findnameat(int dirfd, struct path *path)
371 if ( ! path->st_valid) {
372 of_fstatat(dirfd, path);
378 key.dev = path->st.st_dev;
379 key.inode = path->st.st_ino;
381 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
382 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
391 void of_dealloc( struct ofork *of)
397 oforks[ of->of_refnum % nforks ] = NULL;
398 ad_unlock(of->of_ad, of->of_refnum);
403 /* --------------------------- */
404 int of_closefork(struct ofork *ofork)
407 int adflags, doflush = 0;
411 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
412 adflags |= ADFLAGS_DF;
414 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
415 adflags |= ADFLAGS_HF;
417 * Only set the rfork's length if we're closing the rfork.
419 if ((ofork->of_flags & AFPFORK_RSRC)) {
420 ad_refresh( ofork->of_ad );
421 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
422 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
426 ad_flush( ofork->of_ad );
433 if (ad_unref(ofork->of_ad) == 0) {
434 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) );