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/fce_api.h>
28 #include "directory.h"
31 /* we need to have a hashed list of oforks (by dev inode). just hash
33 #define OFORK_HASHSIZE 64
34 static struct ofork *ofork_table[OFORK_HASHSIZE];
36 static struct ofork **oforks = NULL;
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)
47 i = ((i << 4) | (8*sizeof(i) - 4)) ^ *name++;
50 return key->inode & (OFORK_HASHSIZE - 1);
53 static void of_hash(struct ofork *of)
57 table = &ofork_table[hashfn(&of->key)];
58 if ((of->next = *table) != NULL)
59 (*table)->prevp = &of->next;
64 static void of_unhash(struct ofork *of)
68 of->next->prevp = of->prevp;
69 *(of->prevp) = of->next;
74 void of_pforkdesc( FILE *f)
81 for ( ofrefnum = 0; ofrefnum < nforks; ofrefnum++ ) {
82 if ( oforks[ ofrefnum ] != NULL ) {
83 fprintf( f, "%hu <%s>\n", ofrefnum, of_name(oforks[ ofrefnum ]));
89 int of_flush(const struct vol *vol)
96 for ( refnum = 0; refnum < nforks; refnum++ ) {
97 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
98 flushfork( oforks[ refnum ] ) < 0 ) {
99 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
105 int of_rename(const struct vol *vol,
107 struct dir *olddir, const char *oldpath _U_,
108 struct dir *newdir, const char *newpath)
110 struct ofork *of, *next;
116 next = ofork_table[hashfn(&s_of->key)];
117 while ((of = next)) {
118 next = next->next; /* so we can unhash and still be all right. */
120 if (vol == of->of_vol
121 && olddir->d_did == of->of_did
122 && s_of->key.dev == of->key.dev
123 && s_of->key.inode == of->key.inode ) {
125 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
128 if (newdir != olddir)
129 of->of_did = newdir->d_did;
136 #define min(a,b) ((a)<(b)?(a):(b))
139 of_alloc(struct vol *vol,
148 u_int16_t refnum, of_refnum;
153 nforks = getdtablesize() - 10;
154 /* protect against insane ulimit -n */
155 nforks = min(nforks, 0xffff);
156 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
161 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
162 /* cf AFP3.0.pdf, File fork page 40 */
165 if ( oforks[ refnum % nforks ] == NULL ) {
169 /* grr, Apple and their 'uniquely identifies'
170 the next line is a protection against
185 same if lastrefnum++ rather than ++lastrefnum.
189 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
193 of_refnum = refnum % nforks;
194 if (( oforks[ of_refnum ] =
195 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
196 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
199 of = oforks[of_refnum];
201 /* see if we need to allocate space for the adouble struct */
203 ad = malloc( sizeof( struct adouble ) );
205 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
207 oforks[ of_refnum ] = NULL;
211 /* initialize to zero. This is important to ensure that
212 ad_open really does reinitialize the structure. */
213 ad_init(ad, vol->v_adouble, vol->v_ad_options);
215 ad->ad_m_namelen = UTF8FILELEN_EARLY +1;
216 /* here's the deal: we allocate enough for the standard mac file length.
217 * in the future, we'll reallocate in fairly large jumps in case
218 * of long unicode names */
219 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
220 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
223 oforks[ of_refnum ] = NULL;
226 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
228 /* Increase the refcount on this struct adouble. This is
229 decremented again in oforc_dealloc. */
235 of->of_did = dir->d_did;
238 of->of_refnum = refnum;
239 of->key.dev = st->st_dev;
240 of->key.inode = st->st_ino;
241 if (eid == ADEID_DFORK)
242 of->of_flags = AFPFORK_DATA;
244 of->of_flags = AFPFORK_RSRC;
250 struct ofork *of_find(const u_int16_t ofrefnum )
252 if (!oforks || !nforks)
255 return( oforks[ ofrefnum % nforks ] );
258 /* -------------------------- */
259 int of_stat(struct path *path)
266 if ((ret = lstat(path->u_name, &path->st)) < 0) {
267 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
268 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
269 path->st_errno = errno;
277 int of_fstatat(int dirfd, struct path *path)
284 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
285 path->st_errno = errno;
289 #endif /* HAVE_ATFUNCS */
291 /* --------------------------
292 stat the current directory.
293 stat(".") works even if "." is deleted thus
294 we have to stat ../name because we want to know if it's there
296 int of_statdir(struct vol *vol, struct path *path)
298 static char pathname[ MAXPATHLEN + 1] = "../";
305 return of_stat (path);
309 /* FIXME, what about: we don't have r-x perm anymore ? */
310 len = blength(path->d_dir->d_u_name);
311 if (len > (MAXPATHLEN - 3))
312 len = MAXPATHLEN - 3;
313 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
315 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
317 if (!(ret = lstat(pathname, &path->st)))
320 path->st_errno = errno;
322 /* hmm, can't stat curdir anymore */
323 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
324 if (movecwd(vol, dir))
328 if ((ret = lstat(cfrombstr(path->d_dir->d_u_name), &path->st)) < 0)
329 path->st_errno = errno;
335 /* -------------------------- */
336 struct ofork *of_findname(struct path *path)
341 if (!path->st_valid) {
348 key.dev = path->st.st_dev;
349 key.inode = path->st.st_ino;
351 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
352 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
361 * @brief Search for open fork by dirfd/name
363 * Function call of_fstatat with dirfd and path and uses dev and ino
364 * to search the open fork table.
366 * @param dirfd (r) directory fd
367 * @param path (rw) pointer to struct path
370 struct ofork *of_findnameat(int dirfd, struct path *path)
375 if ( ! path->st_valid) {
376 of_fstatat(dirfd, path);
382 key.dev = path->st.st_dev;
383 key.inode = path->st.st_ino;
385 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
386 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
395 void of_dealloc( struct ofork *of)
401 oforks[ of->of_refnum % nforks ] = NULL;
403 /* decrease refcount */
404 of->of_ad->ad_refcount--;
406 if ( of->of_ad->ad_refcount <= 0) {
407 free( of->of_ad->ad_m_name );
409 } else {/* someone's still using it. just free this user's locks */
410 ad_unlock(of->of_ad, of->of_refnum);
416 /* --------------------------- */
417 int of_closefork(struct ofork *ofork)
420 int adflags, doflush = 0;
424 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
425 adflags |= ADFLAGS_DF;
427 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
428 adflags |= ADFLAGS_HF;
430 * Only set the rfork's length if we're closing the rfork.
432 if ((ofork->of_flags & AFPFORK_RSRC)) {
433 ad_refresh( ofork->of_ad );
434 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
435 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
439 ad_flush( ofork->of_ad );
444 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
445 if (ofork->of_flags & AFPFORK_MODIFIED) {
446 fce_register_file_modification(ofork);
450 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
458 /* ----------------------
461 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
466 if ((of = of_findname(path))) {
469 ad_init(ad, vol->v_adouble, vol->v_ad_options);
475 /* ----------------------
476 close all forks for a volume
478 void of_closevol(const struct vol *vol)
485 for ( refnum = 0; refnum < nforks; refnum++ ) {
486 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
487 if (of_closefork( oforks[ refnum ]) < 0 ) {
488 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
495 /* ----------------------
496 close all forks for a volume
498 void of_close_all_forks(void)
505 for ( refnum = 0; refnum < nforks; refnum++ ) {
506 if (oforks[ refnum ] != NULL) {
507 if (of_closefork( oforks[ refnum ]) < 0 ) {
508 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );