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 ) {
101 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
104 if (newdir != olddir)
105 of->of_did = newdir->d_did;
112 #define min(a,b) ((a)<(b)?(a):(b))
115 of_alloc(struct vol *vol,
124 uint16_t refnum, of_refnum;
129 nforks = getdtablesize() - 10;
130 /* protect against insane ulimit -n */
131 nforks = min(nforks, 0xffff);
132 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
137 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
138 /* cf AFP3.0.pdf, File fork page 40 */
141 if ( oforks[ refnum % nforks ] == NULL ) {
145 /* grr, Apple and their 'uniquely identifies'
146 the next line is a protection against
161 same if lastrefnum++ rather than ++lastrefnum.
165 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
169 of_refnum = refnum % nforks;
170 if (( oforks[ of_refnum ] =
171 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
172 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
175 of = oforks[of_refnum];
177 /* see if we need to allocate space for the adouble struct */
179 ad = malloc( sizeof( struct adouble ) );
181 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
183 oforks[ of_refnum ] = NULL;
187 /* initialize to zero. This is important to ensure that
188 ad_open really does reinitialize the structure. */
191 ad->ad_m_namelen = UTF8FILELEN_EARLY +1;
192 /* here's the deal: we allocate enough for the standard mac file length.
193 * in the future, we'll reallocate in fairly large jumps in case
194 * of long unicode names */
195 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
196 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
199 oforks[ of_refnum ] = NULL;
202 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
204 /* Increase the refcount on this struct adouble. This is
205 decremented again in oforc_dealloc. */
211 of->of_did = dir->d_did;
214 of->of_refnum = refnum;
215 of->key.dev = st->st_dev;
216 of->key.inode = st->st_ino;
217 if (eid == ADEID_DFORK)
218 of->of_flags = AFPFORK_DATA;
220 of->of_flags = AFPFORK_RSRC;
226 struct ofork *of_find(const uint16_t ofrefnum )
228 if (!oforks || !nforks)
231 return( oforks[ ofrefnum % nforks ] );
234 /* -------------------------- */
235 int of_stat(struct path *path)
242 if ((ret = lstat(path->u_name, &path->st)) < 0) {
243 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
244 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
245 path->st_errno = errno;
253 int of_fstatat(int dirfd, struct path *path)
260 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
261 path->st_errno = errno;
265 #endif /* HAVE_ATFUNCS */
267 /* --------------------------
268 stat the current directory.
269 stat(".") works even if "." is deleted thus
270 we have to stat ../name because we want to know if it's there
272 int of_statdir(struct vol *vol, struct path *path)
274 static char pathname[ MAXPATHLEN + 1] = "../";
281 return of_stat (path);
285 /* FIXME, what about: we don't have r-x perm anymore ? */
286 len = blength(path->d_dir->d_u_name);
287 if (len > (MAXPATHLEN - 3))
288 len = MAXPATHLEN - 3;
289 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
291 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
293 if (!(ret = lstat(pathname, &path->st)))
296 path->st_errno = errno;
298 /* hmm, can't stat curdir anymore */
299 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
300 if (movecwd(vol, dir))
304 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
305 path->st_errno = errno;
311 /* -------------------------- */
312 struct ofork *of_findname(struct path *path)
317 if (!path->st_valid) {
324 key.dev = path->st.st_dev;
325 key.inode = path->st.st_ino;
327 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
328 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
337 * @brief Search for open fork by dirfd/name
339 * Function call of_fstatat with dirfd and path and uses dev and ino
340 * to search the open fork table.
342 * @param dirfd (r) directory fd
343 * @param path (rw) pointer to struct path
346 struct ofork *of_findnameat(int dirfd, struct path *path)
351 if ( ! path->st_valid) {
352 of_fstatat(dirfd, path);
358 key.dev = path->st.st_dev;
359 key.inode = path->st.st_ino;
361 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
362 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
371 void of_dealloc( struct ofork *of)
377 oforks[ of->of_refnum % nforks ] = NULL;
379 /* decrease refcount */
380 of->of_ad->ad_refcount--;
382 if ( of->of_ad->ad_refcount <= 0) {
383 free( of->of_ad->ad_m_name );
385 } else {/* someone's still using it. just free this user's locks */
386 ad_unlock(of->of_ad, of->of_refnum);
392 /* --------------------------- */
393 int of_closefork(struct ofork *ofork)
396 int adflags, doflush = 0;
400 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
401 adflags |= ADFLAGS_DF;
403 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
404 adflags |= ADFLAGS_HF;
406 * Only set the rfork's length if we're closing the rfork.
408 if ((ofork->of_flags & AFPFORK_RSRC)) {
409 ad_refresh( ofork->of_ad );
410 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
411 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
415 ad_flush( ofork->of_ad );
420 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
421 if (ofork->of_flags & AFPFORK_MODIFIED) {
422 fce_register_file_modification(ofork);
426 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
434 /* ----------------------
437 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
442 if ((of = of_findname(path))) {
451 /* ----------------------
452 close all forks for a volume
454 void of_closevol(const struct vol *vol)
461 for ( refnum = 0; refnum < nforks; refnum++ ) {
462 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
463 if (of_closefork( oforks[ refnum ]) < 0 ) {
464 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
471 /* ----------------------
472 close all forks for a volume
474 void of_close_all_forks(void)
481 for ( refnum = 0; refnum < nforks; refnum++ ) {
482 if (oforks[ refnum ] != NULL) {
483 if (of_closefork( oforks[ refnum ]) < 0 ) {
484 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );