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 u_int16_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. */
189 ad_init(ad, vol->v_adouble, vol->v_ad_options);
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);
208 of->of_did = dir->d_did;
211 of->of_refnum = refnum;
212 of->key.dev = st->st_dev;
213 of->key.inode = st->st_ino;
214 if (eid == ADEID_DFORK)
215 of->of_flags = AFPFORK_DATA;
217 of->of_flags = AFPFORK_RSRC;
223 struct ofork *of_find(const u_int16_t ofrefnum )
225 if (!oforks || !nforks)
228 return( oforks[ ofrefnum % nforks ] );
231 /* -------------------------- */
232 int of_stat(struct path *path)
239 if ((ret = lstat(path->u_name, &path->st)) < 0) {
240 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
241 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
242 path->st_errno = errno;
250 int of_fstatat(int dirfd, struct path *path)
257 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
258 path->st_errno = errno;
262 #endif /* HAVE_ATFUNCS */
264 /* --------------------------
265 stat the current directory.
266 stat(".") works even if "." is deleted thus
267 we have to stat ../name because we want to know if it's there
269 int of_statdir(struct vol *vol, struct path *path)
271 static char pathname[ MAXPATHLEN + 1] = "../";
278 return of_stat (path);
282 /* FIXME, what about: we don't have r-x perm anymore ? */
283 len = blength(path->d_dir->d_u_name);
284 if (len > (MAXPATHLEN - 3))
285 len = MAXPATHLEN - 3;
286 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
288 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
290 if (!(ret = lstat(pathname, &path->st)))
293 path->st_errno = errno;
295 /* hmm, can't stat curdir anymore */
296 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
297 if (movecwd(vol, dir))
301 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
302 path->st_errno = errno;
308 /* -------------------------- */
309 struct ofork *of_findname(struct path *path)
314 if (!path->st_valid) {
321 key.dev = path->st.st_dev;
322 key.inode = path->st.st_ino;
324 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
325 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
334 * @brief Search for open fork by dirfd/name
336 * Function call of_fstatat with dirfd and path and uses dev and ino
337 * to search the open fork table.
339 * @param dirfd (r) directory fd
340 * @param path (rw) pointer to struct path
343 struct ofork *of_findnameat(int dirfd, struct path *path)
348 if ( ! path->st_valid) {
349 of_fstatat(dirfd, path);
355 key.dev = path->st.st_dev;
356 key.inode = path->st.st_ino;
358 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
359 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
368 void of_dealloc( struct ofork *of)
374 oforks[ of->of_refnum % nforks ] = NULL;
378 /* --------------------------- */
379 int of_closefork(struct ofork *ofork)
385 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
387 * Only set the rfork's length if we're closing the rfork.
389 if ((ofork->of_flags & AFPFORK_RSRC)) {
390 ad_refresh( ofork->of_ad );
391 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
392 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
396 ad_flush( ofork->of_ad );
401 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
402 if (ofork->of_flags & AFPFORK_MODIFIED) {
403 fce_register_file_modification(ofork);
408 ad_unlock(ofork->of_ad, ofork->of_refnum);
410 if (ad_unref(ofork->of_ad) == 0) {
411 if ( ad_close( ofork->of_ad, 0 ) < 0 ) {
420 /* ----------------------
423 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
428 if ((of = of_findname(path))) {
431 ad_init(ad, vol->v_adouble, vol->v_ad_options);
437 /* ----------------------
438 close all forks for a volume
440 void of_closevol(const struct vol *vol)
447 for ( refnum = 0; refnum < nforks; refnum++ ) {
448 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
449 if (of_closefork( oforks[ refnum ]) < 0 ) {
450 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
457 /* ----------------------
458 close all forks for a volume
460 void of_close_all_forks(void)
467 for ( refnum = 0; refnum < nforks; refnum++ ) {
468 if (oforks[ refnum ] != NULL) {
469 if (of_closefork( oforks[ refnum ]) < 0 ) {
470 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );