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>
29 #include "directory.h"
33 /* we need to have a hashed list of oforks (by dev inode) */
34 #define OFORK_HASHSIZE 64
35 static struct ofork *ofork_table[OFORK_HASHSIZE]; /* forks hashed by dev/inode */
36 static struct ofork **oforks = NULL; /* point to allocated table of open forks pointers */
37 static int nforks = 0;
38 static u_short lastrefnum = 0;
41 /* OR some of each character for the hash*/
42 static unsigned long hashfn(const struct file_key *key)
44 return key->inode & (OFORK_HASHSIZE - 1);
47 static void of_hash(struct ofork *of)
51 table = &ofork_table[hashfn(&of->key)];
52 if ((of->next = *table) != NULL)
53 (*table)->prevp = &of->next;
58 static void of_unhash(struct ofork *of)
62 of->next->prevp = of->prevp;
63 *(of->prevp) = of->next;
67 int of_flush(const struct vol *vol)
74 for ( refnum = 0; refnum < nforks; refnum++ ) {
75 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
76 flushfork( oforks[ refnum ] ) < 0 ) {
77 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
83 int of_rename(const struct vol *vol,
85 struct dir *olddir, const char *oldpath _U_,
86 struct dir *newdir, const char *newpath)
88 struct ofork *of, *next;
94 next = ofork_table[hashfn(&s_of->key)];
96 next = next->next; /* so we can unhash and still be all right. */
99 && olddir->d_did == of->of_did
100 && s_of->key.dev == of->key.dev
101 && s_of->key.inode == of->key.inode ) {
104 if ((of_name(of) = strdup(newpath)) == NULL)
108 if (newdir != olddir)
109 of->of_did = newdir->d_did;
116 #define min(a,b) ((a)<(b)?(a):(b))
119 of_alloc(struct vol *vol,
128 uint16_t refnum, of_refnum;
133 nforks = getdtablesize() - 10;
134 /* protect against insane ulimit -n */
135 nforks = min(nforks, 0xffff);
136 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
141 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
142 /* cf AFP3.0.pdf, File fork page 40 */
145 if ( oforks[ refnum % nforks ] == NULL ) {
149 /* grr, Apple and their 'uniquely identifies'
150 the next line is a protection against
165 same if lastrefnum++ rather than ++lastrefnum.
169 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
173 of_refnum = refnum % nforks;
174 if (( oforks[ of_refnum ] =
175 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
176 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
179 of = oforks[of_refnum];
181 /* see if we need to allocate space for the adouble struct */
183 ad = malloc( sizeof( struct adouble ) );
185 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
187 oforks[ of_refnum ] = NULL;
191 /* initialize to zero. This is important to ensure that
192 ad_open really does reinitialize the structure. */
194 if ((ad->ad_name = strdup(path)) == NULL) {
195 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
198 oforks[ of_refnum ] = NULL;
202 /* Increase the refcount on this struct adouble. This is
203 decremented again in oforc_dealloc. */
209 of->of_did = dir->d_did;
212 of->of_refnum = refnum;
213 of->key.dev = st->st_dev;
214 of->key.inode = st->st_ino;
215 if (eid == ADEID_DFORK)
216 of->of_flags = AFPFORK_DATA;
218 of->of_flags = AFPFORK_RSRC;
224 struct ofork *of_find(const uint16_t ofrefnum )
226 if (!oforks || !nforks)
229 return( oforks[ ofrefnum % nforks ] );
232 /* -------------------------- */
233 int of_stat(const struct vol *vol, struct path *path)
240 if ((ret = ostat(path->u_name, &path->st, vol_syml_opt(vol))) < 0) {
241 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
242 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
243 path->st_errno = errno;
251 int of_fstatat(int dirfd, struct path *path)
258 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
259 path->st_errno = errno;
263 #endif /* HAVE_ATFUNCS */
265 /* --------------------------
266 stat the current directory.
267 stat(".") works even if "." is deleted thus
268 we have to stat ../name because we want to know if it's there
270 int of_statdir(struct vol *vol, struct path *path)
272 static char pathname[ MAXPATHLEN + 1] = "../";
279 return of_stat(vol, path);
283 /* FIXME, what about: we don't have r-x perm anymore ? */
284 len = blength(path->d_dir->d_u_name);
285 if (len > (MAXPATHLEN - 3))
286 len = MAXPATHLEN - 3;
287 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
289 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
291 if (!(ret = ostat(pathname, &path->st, vol_syml_opt(vol))))
294 path->st_errno = errno;
296 /* hmm, can't stat curdir anymore */
297 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
298 if (movecwd(vol, dir))
302 if ((ret = ostat(cfrombstr(path->d_dir->d_u_name), &path->st, vol_syml_opt(vol))) < 0)
303 path->st_errno = errno;
309 /* -------------------------- */
310 struct ofork *of_findname(const struct vol *vol, struct path *path)
315 if (!path->st_valid) {
322 key.dev = path->st.st_dev;
323 key.inode = path->st.st_ino;
325 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
326 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
335 * @brief Search for open fork by dirfd/name
337 * Function call of_fstatat with dirfd and path and uses dev and ino
338 * to search the open fork table.
340 * @param dirfd (r) directory fd
341 * @param path (rw) pointer to struct path
344 struct ofork *of_findnameat(int dirfd, struct path *path)
349 if ( ! path->st_valid) {
350 of_fstatat(dirfd, path);
356 key.dev = path->st.st_dev;
357 key.inode = path->st.st_ino;
359 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
360 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
369 void of_dealloc(struct ofork *of)
375 oforks[ of->of_refnum % nforks ] = NULL;
377 /* decrease refcount */
378 of->of_ad->ad_refcount--;
380 if ( of->of_ad->ad_refcount <= 0) {
381 free( of->of_ad->ad_name );
388 /* --------------------------- */
389 int of_closefork(const AFPObj *obj, struct ofork *ofork)
395 bstring forkpath = NULL;
398 if (ofork->of_flags & AFPFORK_DATA)
399 adflags |= ADFLAGS_DF;
400 if (ofork->of_flags & AFPFORK_META)
401 adflags |= ADFLAGS_HF;
402 if (ofork->of_flags & AFPFORK_RSRC) {
403 adflags |= ADFLAGS_RF;
404 /* Only set the rfork's length if we're closing the rfork. */
405 ad_refresh(NULL, ofork->of_ad );
406 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
407 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
408 ad_flush( ofork->of_ad );
412 dir = dirlookup(ofork->of_vol, ofork->of_did);
414 LOG(log_debug, logtype_afpd, "dirlookup failed for %ju", (uintmax_t)ofork->of_did);
418 forkpath = bformat("%s/%s", bdata(dir->d_fullpath), of_name(ofork));
421 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
422 if ((ofork->of_flags & AFPFORK_MODIFIED) && (forkpath)) {
423 fce_register(obj, FCE_FILE_MODIFY, bdata(forkpath), NULL);
426 ad_unlock(ofork->of_ad, ofork->of_refnum, ofork->of_flags & AFPFORK_ERROR ? 0 : 1);
429 if (obj->options.flags & OPTION_SHARE_RESERV) {
431 shmd.f_id = ofork->of_refnum;
432 if (AD_DATA_OPEN(ofork->of_ad))
433 fcntl(ad_data_fileno(ofork->of_ad), F_UNSHARE, &shmd);
434 if (AD_RSRC_OPEN(ofork->of_ad))
435 fcntl(ad_reso_fileno(ofork->of_ad), F_UNSHARE, &shmd);
442 * Check for 0 byte size resource forks, delete them.
444 * (1) the size must be 0
445 * (2) the fork must refer to a resource fork
446 * (3) the refcount must be 1 which means this fork has the last
447 * reference to the adouble struct and the subsequent
448 * ad_close() will close the assoiciated fd.
449 * (4) nobody else has the resource fork open
451 * We only do this for ._ AppleDouble resource forks, not for
452 * xattr resource forks, because the test-suite then fails several
453 * tests on Solaris, the reason for that still needs to be
456 if ((ofork->of_ad->ad_rlen == 0)
457 && (ofork->of_flags & AFPFORK_RSRC)
458 && (ofork->of_ad->ad_rfp->adf_refcount == 1)
459 && (ad_openforks(ofork->of_ad, ATTRBIT_DOPEN) == 0)) {
462 (void)unlink(ofork->of_ad->ad_ops->ad_path(
463 mtoupath(ofork->of_vol,
471 if ( ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD) < 0 ) {
483 /* ----------------------
486 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
491 if ((of = of_findname(vol, path))) {
500 /* ----------------------
501 close all forks for a volume
503 void of_closevol(const AFPObj *obj, const struct vol *vol)
510 for ( refnum = 0; refnum < nforks; refnum++ ) {
511 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
512 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
513 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
520 /* ----------------------
521 close all forks for a volume
523 void of_close_all_forks(const AFPObj *obj)
530 for ( refnum = 0; refnum < nforks; refnum++ ) {
531 if (oforks[ refnum ] != NULL) {
532 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
533 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );