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;
65 int of_flush(const struct vol *vol)
72 for ( refnum = 0; refnum < nforks; refnum++ ) {
73 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
74 flushfork( oforks[ refnum ] ) < 0 ) {
75 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
81 int of_rename(const struct vol *vol,
83 struct dir *olddir, const char *oldpath _U_,
84 struct dir *newdir, const char *newpath)
86 struct ofork *of, *next;
92 next = ofork_table[hashfn(&s_of->key)];
94 next = next->next; /* so we can unhash and still be all right. */
97 && olddir->d_did == of->of_did
98 && s_of->key.dev == of->key.dev
99 && s_of->key.inode == of->key.inode ) {
102 if ((of_name(of) = strdup(newpath)) == NULL)
106 if (newdir != olddir)
107 of->of_did = newdir->d_did;
114 #define min(a,b) ((a)<(b)?(a):(b))
117 of_alloc(struct vol *vol,
126 uint16_t refnum, of_refnum;
131 nforks = getdtablesize() - 10;
132 /* protect against insane ulimit -n */
133 nforks = min(nforks, 0xffff);
134 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
139 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
140 /* cf AFP3.0.pdf, File fork page 40 */
143 if ( oforks[ refnum % nforks ] == NULL ) {
147 /* grr, Apple and their 'uniquely identifies'
148 the next line is a protection against
163 same if lastrefnum++ rather than ++lastrefnum.
167 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
171 of_refnum = refnum % nforks;
172 if (( oforks[ of_refnum ] =
173 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
174 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
177 of = oforks[of_refnum];
179 /* see if we need to allocate space for the adouble struct */
181 ad = malloc( sizeof( struct adouble ) );
183 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
185 oforks[ of_refnum ] = NULL;
189 /* initialize to zero. This is important to ensure that
190 ad_open really does reinitialize the structure. */
192 if ((ad->ad_name = strdup(path)) == NULL) {
193 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
196 oforks[ of_refnum ] = NULL;
200 /* Increase the refcount on this struct adouble. This is
201 decremented again in oforc_dealloc. */
207 of->of_did = dir->d_did;
210 of->of_refnum = refnum;
211 of->key.dev = st->st_dev;
212 of->key.inode = st->st_ino;
213 if (eid == ADEID_DFORK)
214 of->of_flags = AFPFORK_DATA;
216 of->of_flags = AFPFORK_RSRC;
222 struct ofork *of_find(const uint16_t ofrefnum )
224 if (!oforks || !nforks)
227 return( oforks[ ofrefnum % nforks ] );
230 /* -------------------------- */
231 int of_stat(const struct vol *vol, struct path *path)
238 if ((ret = ostat(path->u_name, &path->st, vol_syml_opt(vol))) < 0) {
239 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
240 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
241 path->st_errno = errno;
249 int of_fstatat(int dirfd, struct path *path)
256 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
257 path->st_errno = errno;
261 #endif /* HAVE_ATFUNCS */
263 /* --------------------------
264 stat the current directory.
265 stat(".") works even if "." is deleted thus
266 we have to stat ../name because we want to know if it's there
268 int of_statdir(struct vol *vol, struct path *path)
270 static char pathname[ MAXPATHLEN + 1] = "../";
277 return of_stat(vol, path);
281 /* FIXME, what about: we don't have r-x perm anymore ? */
282 len = blength(path->d_dir->d_u_name);
283 if (len > (MAXPATHLEN - 3))
284 len = MAXPATHLEN - 3;
285 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
287 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
289 if (!(ret = ostat(pathname, &path->st, vol_syml_opt(vol))))
292 path->st_errno = errno;
294 /* hmm, can't stat curdir anymore */
295 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
296 if (movecwd(vol, dir))
300 if ((ret = ostat(cfrombstr(path->d_dir->d_u_name), &path->st, vol_syml_opt(vol))) < 0)
301 path->st_errno = errno;
307 /* -------------------------- */
308 struct ofork *of_findname(const struct vol *vol, struct path *path)
313 if (!path->st_valid) {
320 key.dev = path->st.st_dev;
321 key.inode = path->st.st_ino;
323 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
324 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
333 * @brief Search for open fork by dirfd/name
335 * Function call of_fstatat with dirfd and path and uses dev and ino
336 * to search the open fork table.
338 * @param dirfd (r) directory fd
339 * @param path (rw) pointer to struct path
342 struct ofork *of_findnameat(int dirfd, struct path *path)
347 if ( ! path->st_valid) {
348 of_fstatat(dirfd, path);
354 key.dev = path->st.st_dev;
355 key.inode = path->st.st_ino;
357 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
358 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
367 void of_dealloc(struct ofork *of)
373 oforks[ of->of_refnum % nforks ] = NULL;
375 /* decrease refcount */
376 of->of_ad->ad_refcount--;
378 if ( of->of_ad->ad_refcount <= 0) {
379 free( of->of_ad->ad_name );
386 /* --------------------------- */
387 int of_closefork(const AFPObj *obj, struct ofork *ofork)
394 if (ofork->of_flags & AFPFORK_DATA)
395 adflags |= ADFLAGS_DF;
396 if (ofork->of_flags & AFPFORK_META)
397 adflags |= ADFLAGS_HF;
398 if (ofork->of_flags & AFPFORK_RSRC) {
399 adflags |= ADFLAGS_RF;
400 /* Only set the rfork's length if we're closing the rfork. */
401 ad_refresh(NULL, ofork->of_ad );
402 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
403 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
404 ad_flush( ofork->of_ad );
408 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
409 if (ofork->of_flags & AFPFORK_MODIFIED) {
410 struct dir *dir = dirlookup(ofork->of_vol, ofork->of_did);
411 bstring forkpath = bformat("%s/%s", bdata(dir->d_fullpath), of_name(ofork));
412 fce_register(FCE_FILE_MODIFY, bdata(forkpath), NULL, fce_file);
416 ad_unlock(ofork->of_ad, ofork->of_refnum, ofork->of_flags & AFPFORK_ERROR ? 0 : 1);
419 if (obj->options.flags & OPTION_SHARE_RESERV) {
421 shmd.f_id = ofork->of_refnum;
422 if (AD_DATA_OPEN(ofork->of_ad))
423 fcntl(ad_data_fileno(ofork->of_ad), F_UNSHARE, &shmd);
424 if (AD_RSRC_OPEN(ofork->of_ad))
425 fcntl(ad_reso_fileno(ofork->of_ad), F_UNSHARE, &shmd);
430 if ( ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD) < 0 ) {
439 /* ----------------------
442 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
447 if ((of = of_findname(vol, path))) {
456 /* ----------------------
457 close all forks for a volume
459 void of_closevol(const AFPObj *obj, const struct vol *vol)
466 for ( refnum = 0; refnum < nforks; refnum++ ) {
467 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
468 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
469 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
476 /* ----------------------
477 close all forks for a volume
479 void of_close_all_forks(const AFPObj *obj)
486 for ( refnum = 0; refnum < nforks; refnum++ ) {
487 if (oforks[ refnum ] != NULL) {
488 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
489 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );