2 * $Id: ofork.c,v 1.32 2010-03-12 15:16:49 franklahm Exp $
4 * Copyright (c) 1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #include <sys/stat.h> /* works around a bug */
19 #include <sys/param.h>
20 #include <atalk/logger.h>
23 #include <atalk/util.h>
27 #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, *d_ofork;
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 && olddir == of->of_dir &&
121 s_of->key.dev == of->key.dev &&
122 s_of->key.inode == of->key.inode ) {
124 strlcpy( of_name(of), newpath, of->of_ad->ad_m_namelen);
127 if (newdir != olddir) {
128 of->of_d_prev->of_d_next = of->of_d_next;
129 of->of_d_next->of_d_prev = of->of_d_prev;
130 if (of->of_dir->d_ofork == of) {
131 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
134 if (!(d_ofork = newdir->d_ofork)) {
135 newdir->d_ofork = of;
136 of->of_d_next = of->of_d_prev = of;
138 of->of_d_next = d_ofork;
139 of->of_d_prev = d_ofork->of_d_prev;
140 of->of_d_prev->of_d_next = of;
141 d_ofork->of_d_prev = of;
150 #define min(a,b) ((a)<(b)?(a):(b))
153 of_alloc(struct vol *vol,
161 struct ofork *of, *d_ofork;
162 u_int16_t refnum, of_refnum;
167 nforks = getdtablesize() - 10;
168 /* protect against insane ulimit -n */
169 nforks = min(nforks, 0xffff);
170 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
175 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
176 /* cf AFP3.0.pdf, File fork page 40 */
179 if ( oforks[ refnum % nforks ] == NULL ) {
183 /* grr, Apple and their 'uniquely identifies'
184 the next line is a protection against
199 same if lastrefnum++ rather than ++lastrefnum.
203 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
207 of_refnum = refnum % nforks;
208 if (( oforks[ of_refnum ] =
209 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
210 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
213 of = oforks[of_refnum];
215 /* see if we need to allocate space for the adouble struct */
217 ad = malloc( sizeof( struct adouble ) );
219 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
221 oforks[ of_refnum ] = NULL;
225 /* initialize to zero. This is important to ensure that
226 ad_open really does reinitialize the structure. */
227 ad_init(ad, vol->v_adouble, vol->v_ad_options);
229 ad->ad_m_namelen = 255 +1;
230 /* here's the deal: we allocate enough for the standard mac file length.
231 * in the future, we'll reallocate in fairly large jumps in case
232 * of long unicode names */
233 if (( ad->ad_m_name =(char *)malloc( ad->ad_m_namelen )) == NULL ) {
234 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
237 oforks[ of_refnum ] = NULL;
240 strlcpy( ad->ad_m_name, path, ad->ad_m_namelen);
242 /* Increase the refcount on this struct adouble. This is
243 decremented again in oforc_dealloc. */
251 if (!(d_ofork = dir->d_ofork)) {
253 of->of_d_next = of->of_d_prev = of;
255 of->of_d_next = d_ofork;
256 of->of_d_prev = d_ofork->of_d_prev;
257 d_ofork->of_d_prev->of_d_next = of;
258 d_ofork->of_d_prev = of;
262 of->of_refnum = refnum;
263 of->key.dev = st->st_dev;
264 of->key.inode = st->st_ino;
265 if (eid == ADEID_DFORK)
266 of->of_flags = AFPFORK_DATA;
268 of->of_flags = AFPFORK_RSRC;
274 struct ofork *of_find(const u_int16_t ofrefnum )
276 if (!oforks || !nforks)
279 return( oforks[ ofrefnum % nforks ] );
282 /* -------------------------- */
283 int of_stat (struct path *path)
288 if ((ret = lstat(path->u_name, &path->st)) < 0)
289 path->st_errno = errno;
294 int of_fstatat(int dirfd, struct path *path)
301 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
302 path->st_errno = errno;
306 #endif /* HAVE_RENAMEAT */
308 /* --------------------------
309 stat the current directory.
310 stat(".") works even if "." is deleted thus
311 we have to stat ../name because we want to know if it's there
313 int of_statdir (struct vol *vol, struct path *path)
315 static char pathname[ MAXPATHLEN + 1] = "../";
320 return of_stat (path);
324 /* FIXME, what about: we don't have r-x perm anymore ? */
325 strlcpy(pathname +3, path->d_dir->d_u_name, sizeof (pathname) -3);
327 if (!(ret = lstat(pathname, &path->st)))
330 path->st_errno = errno;
331 /* hmm, can't stat curdir anymore */
332 if (errno == EACCES && curdir->d_parent ) {
333 if (movecwd(vol, curdir->d_parent))
336 if ((ret = lstat(path->d_dir->d_u_name, &path->st)) < 0)
337 path->st_errno = errno;
342 /* -------------------------- */
343 struct ofork *of_findname(struct path *path)
348 if (!path->st_valid) {
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 * @brief Search for open fork by dirfd/name
370 * Function call of_fstatat with dirfd and path and uses dev and ino
371 * to search the open fork table.
373 * @param dirfd (r) directory fd
374 * @param path (rw) pointer to struct path
377 struct ofork *of_findnameat(int dirfd, struct path *path)
382 if ( ! path->st_valid) {
383 of_fstatat(dirfd, path);
389 key.dev = path->st.st_dev;
390 key.inode = path->st.st_ino;
392 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
393 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
402 void of_dealloc( struct ofork *of)
410 of->of_d_prev->of_d_next = of->of_d_next;
411 of->of_d_next->of_d_prev = of->of_d_prev;
412 if (of->of_dir->d_ofork == of) {
413 of->of_dir->d_ofork = (of == of->of_d_next) ? NULL : of->of_d_next;
416 oforks[ of->of_refnum % nforks ] = NULL;
418 /* decrease refcount */
419 of->of_ad->ad_refcount--;
421 if ( of->of_ad->ad_refcount <= 0) {
422 free( of->of_ad->ad_m_name );
424 } else {/* someone's still using it. just free this user's locks */
425 ad_unlock(of->of_ad, of->of_refnum);
431 /* --------------------------- */
432 int of_closefork(struct ofork *ofork)
435 int adflags, doflush = 0;
439 if ((ofork->of_flags & AFPFORK_DATA) && (ad_data_fileno( ofork->of_ad ) != -1)) {
440 adflags |= ADFLAGS_DF;
442 if ( (ofork->of_flags & AFPFORK_OPEN) && ad_reso_fileno( ofork->of_ad ) != -1 ) {
443 adflags |= ADFLAGS_HF;
445 * Only set the rfork's length if we're closing the rfork.
447 if ((ofork->of_flags & AFPFORK_RSRC)) {
448 ad_refresh( ofork->of_ad );
449 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
450 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
454 ad_flush( ofork->of_ad );
459 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
460 if (ofork->of_flags & AFPFORK_MODIFIED) {
461 fce_register_file_modification(ofork);
465 if ( ad_close( ofork->of_ad, adflags ) < 0 ) {
473 /* ----------------------
476 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
481 if ((of = of_findname(path))) {
484 ad_init(ad, vol->v_adouble, vol->v_ad_options);
490 /* ----------------------
491 close all forks for a volume
493 void of_closevol(const struct vol *vol)
500 for ( refnum = 0; refnum < nforks; refnum++ ) {
501 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
502 if (of_closefork( oforks[ refnum ]) < 0 ) {
503 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );