2 * $Id: directory.c,v 1.48 2002-10-14 06:30:49 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
15 #include <atalk/logger.h>
16 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <netatalk/endian.h>
22 #include <atalk/adouble.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
26 #include <atalk/cnid.h>
34 #endif /* HAVE_FCNTL_H */
41 #else /* STDC_HEADERS */
45 #endif /* HAVE_STRCHR */
46 char *strchr (), *strrchr ();
48 #define memcpy(d,s,n) bcopy ((s), (d), (n))
49 #define memmove(d,s,n) bcopy ((s), (d), (n))
50 #endif /* ! HAVE_MEMCPY */
51 #endif /* STDC_HEADERS */
53 #include "directory.h"
64 #define SENTINEL (&sentinel)
65 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
68 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
69 NULL, NULL, NULL, NULL, NULL, 0, 0,
72 /* (from IM: Toolbox Essentials)
73 * dirFinderInfo (DInfo) fields:
75 * frRect 8 folder's window rectangle
77 * frLocation 4 folder's location in window
78 * frView 2 folder's view (default == closedView (256))
80 * extended dirFinderInfo (DXInfo) fields:
81 * frScroll 4 scroll position
82 * frOpenChain: 4 directory ID chain of open folders
83 * frScript: 1 script flag and code
84 * frXFlags: 1 reserved
85 * frComment: 2 comment ID
86 * frPutAway: 4 home directory ID
90 * redid did assignment for directories. now we use red-black trees.
95 const struct vol *vol;
101 /* check for 0 did */
105 if ( did == DIRDID_ROOT_PARENT ) {
107 rootpar.d_did = DIRDID_ROOT_PARENT;
108 rootpar.d_child = vol->v_dir;
113 while ( dir != SENTINEL ) {
114 if (dir->d_did == did)
115 return dir->d_m_name ? dir : NULL;
116 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
121 /* -----------------------------------------
122 * if did is not in the cache resolve it with cnid
126 dirlookup( vol, did )
127 const struct vol *vol;
134 static char path[MAXPATHLEN + 1];
138 static char buffer[12 + MAXPATHLEN + 1];
139 int buflen = 12 + MAXPATHLEN + 1;
142 ret = dirsearch(vol, did);
147 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
150 ptr = path + MAXPATHLEN;
151 mpath = utompath(vol, upath);
153 pathlen = len; /* no 0 in the last part */
155 strcpy(ptr - len, mpath);
158 ret = dirsearch(vol,id);
162 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL)
164 mpath = utompath(vol, upath);
165 len = strlen(mpath) + 1;
169 strcpy(ptr - len, mpath);
174 *ptr = (unsigned char)pathlen;
177 /* cname is not efficient */
178 if (cname( vol, ret, &ptr ) == NULL )
181 return dirsearch(vol, did);
184 /* --------------------------- */
185 /* rotate the tree to the left */
186 static void dir_leftrotate(vol, dir)
190 struct dir *right = dir->d_right;
192 /* whee. move the right's left tree into dir's right tree */
193 dir->d_right = right->d_left;
194 if (right->d_left != SENTINEL)
195 right->d_left->d_back = dir;
197 if (right != SENTINEL) {
198 right->d_back = dir->d_back;
202 if (!dir->d_back) /* no parent. move the right tree to the top. */
204 else if (dir == dir->d_back->d_left) /* we were on the left */
205 dir->d_back->d_left = right;
207 dir->d_back->d_right = right; /* we were on the right */
209 /* re-insert dir on the left tree */
216 /* rotate the tree to the right */
217 static void dir_rightrotate(vol, dir)
221 struct dir *left = dir->d_left;
223 /* whee. move the left's right tree into dir's left tree */
224 dir->d_left = left->d_right;
225 if (left->d_right != SENTINEL)
226 left->d_right->d_back = dir;
228 if (left != SENTINEL) {
229 left->d_back = dir->d_back;
233 if (!dir->d_back) /* no parent. move the left tree to the top. */
235 else if (dir == dir->d_back->d_right) /* we were on the right */
236 dir->d_back->d_right = left;
238 dir->d_back->d_left = left; /* we were on the left */
240 /* re-insert dir on the right tree */
246 /* recolor after a removal */
247 static struct dir *dir_rmrecolor(vol, dir)
253 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
254 /* are we on the left tree? */
255 if (dir == dir->d_back->d_left) {
256 leaf = dir->d_back->d_right; /* get right side */
257 if (leaf->d_color == DIRTREE_COLOR_RED) {
258 /* we're red. we need to change to black. */
259 leaf->d_color = DIRTREE_COLOR_BLACK;
260 dir->d_back->d_color = DIRTREE_COLOR_RED;
261 dir_leftrotate(vol, dir->d_back);
262 leaf = dir->d_back->d_right;
265 /* right leaf has black end nodes */
266 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
267 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
268 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
269 dir = dir->d_back; /* ascend */
271 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
272 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
273 leaf->d_color = DIRTREE_COLOR_RED;
274 dir_rightrotate(vol, leaf);
275 leaf = dir->d_back->d_right;
277 leaf->d_color = dir->d_back->d_color;
278 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
279 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
280 dir_leftrotate(vol, dir->d_back);
283 } else { /* right tree */
284 leaf = dir->d_back->d_left; /* left tree */
285 if (leaf->d_color == DIRTREE_COLOR_RED) {
286 leaf->d_color = DIRTREE_COLOR_BLACK;
287 dir->d_back->d_color = DIRTREE_COLOR_RED;
288 dir_rightrotate(vol, dir->d_back);
289 leaf = dir->d_back->d_left;
292 /* left leaf has black end nodes */
293 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
294 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
295 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
296 dir = dir->d_back; /* ascend */
298 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
299 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
300 leaf->d_color = DIRTREE_COLOR_RED;
301 dir_leftrotate(vol, leaf);
302 leaf = dir->d_back->d_left;
304 leaf->d_color = dir->d_back->d_color;
305 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
306 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
307 dir_rightrotate(vol, dir->d_back);
312 dir->d_color = DIRTREE_COLOR_BLACK;
319 /* remove the node from the tree. this is just like insertion, but
320 * different. actually, it has to worry about a bunch of things that
321 * insertion doesn't care about. */
322 static void dir_remove( vol, dir )
327 struct ofork *of, *last;
328 struct dir *node, *leaf;
329 #endif /* REMOVE_NODES */
331 if (!dir || (dir == SENTINEL))
334 /* i'm not sure if it really helps to delete stuff. */
336 if (dir->d_u_name != dir->d_m_name) {
340 dir->d_m_name = NULL;
341 dir->d_u_name = NULL;
342 #else /* ! REMOVE_NODES */
344 /* go searching for a node with at most one child */
345 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
349 while (node->d_left != SENTINEL)
354 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
357 leaf->d_back = node->d_back;
360 } else if (node == node->d_back->d_left) { /* left tree */
361 node->d_back->d_left = leaf;
363 node->d_back->d_right = leaf;
366 /* we want to free node, but we also want to free the data in dir.
367 * currently, that's d_name and the directory traversal bits.
368 * we just copy the necessary bits and then fix up all the
369 * various pointers to the directory. needless to say, there are
370 * a bunch of places that store the directory struct. */
372 struct dir save, *tmp;
374 memcpy(&save, dir, sizeof(save));
375 memcpy(dir, node, sizeof(struct dir));
377 /* restore the red-black bits */
378 dir->d_left = save.d_left;
379 dir->d_right = save.d_right;
380 dir->d_back = save.d_back;
381 dir->d_color = save.d_color;
383 if (node == vol->v_dir) {/* we may need to fix up this pointer */
385 rootpar.d_child = vol->v_dir;
387 /* if we aren't the root directory, we have parents and
388 * siblings to worry about */
389 if (dir->d_parent->d_child == node)
390 dir->d_parent->d_child = dir;
391 dir->d_next->d_prev = dir;
392 dir->d_prev->d_next = dir;
395 /* fix up children. */
399 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
402 if (node == curdir) /* another pointer to fixup */
405 /* we also need to fix up oforks. bleah */
406 if ((of = dir->d_ofork)) {
407 last = of->of_d_prev;
410 of = (last == of) ? NULL : of->of_d_next;
414 /* set the node's d_name */
415 node->d_m_name = save.d_m_name;
416 node->d_u_name = save.d_u_name;
419 if (node->d_color == DIRTREE_COLOR_BLACK)
420 dir_rmrecolor(vol, leaf);
422 if (node->d_u_name != node->d_m_name) {
423 free(node->d_u_name);
425 free(node->d_m_name);
427 #endif /* ! REMOVE_NODES */
430 /* ---------------------------------------
431 * remove the node and its childs from the tree
433 * FIXME what about opened forks with refs to it?
434 * it's an afp specs violation because you can't delete
435 * an opened forks. Now afpd doesn't care about forks opened by other
436 * process. It's fixable within afpd if fnctl_lock, doable with smb and
437 * next to impossible for nfs and local filesystem access.
440 static void dir_invalidate( vol, dir )
441 const struct vol *vol;
445 /* v_root can't be deleted */
446 if (movecwd(vol, vol->v_root) < 0)
447 printf("Yuup cant change dir to v_root\n");
450 dirchildremove(dir->d_parent, dir);
451 dir_remove( vol, dir );
454 /* ------------------------------------ */
455 static struct dir *dir_insert(vol, dir)
456 const struct vol *vol;
462 while (pdir->d_did != dir->d_did ) {
463 if ( pdir->d_did > dir->d_did ) {
464 if ( pdir->d_left == SENTINEL ) {
471 if ( pdir->d_right == SENTINEL ) {
476 pdir = pdir->d_right;
484 * attempt to extend the current dir. tree to include path
485 * as a side-effect, movecwd to that point and return the new dir
488 extenddir( vol, dir, path )
495 path->u_name = p = mtoupath(vol, path->m_name );
496 if ( of_stat( path ) != 0 ) {
500 if (!S_ISDIR(path->st.st_mode)) {
504 if (( dir = adddir( vol, dir, path)) == NULL ) {
508 if ( movecwd( vol, dir ) < 0 ) {
515 static int deletedir(char *dir)
517 char path[MAXPATHLEN + 1];
524 if ((len = strlen(dir)) > sizeof(path))
528 if ((dp = opendir(dir)) == NULL)
534 while ((de = readdir(dp))) {
535 /* skip this and previous directory */
536 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
539 strncpy(path + len, de->d_name, sizeof(path) - len);
540 if (stat(path, &st) == 0) {
541 if (S_ISDIR(st.st_mode)) {
542 if ((err = deletedir(path)) < 0) {
546 } else if (unlink(path) < 0) {
549 continue; /* somebody went and deleted it behind our backs. */
567 /* okay. the directory is empty. delete it. note: we already got rid
569 if (rmdir(dir) < 0) {
573 case ENOTEMPTY : /* should never happen */
574 return( AFPERR_DIRNEMPT );
577 return( AFPERR_ACCESS );
581 return( AFPERR_PARAM );
587 /* do a recursive copy. */
588 static int copydir(char *src, char *dst, int noadouble)
590 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
598 /* doesn't exist or the path is too long. */
599 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
600 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
601 ((dp = opendir(src)) == NULL))
604 /* try to create the destination directory */
605 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
609 return( AFPERR_NOOBJ );
611 return( AFPERR_VLOCK );
614 return( AFPERR_ACCESS );
616 return( AFPERR_EXIST );
619 return( AFPERR_DFULL );
621 return( AFPERR_PARAM );
625 /* set things up to copy */
633 while ((de = readdir(dp))) {
634 /* skip this and previous directory */
635 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
638 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
639 if (stat(spath, &st) == 0) {
640 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
642 if (S_ISDIR(st.st_mode)) {
643 if ((err = copydir(spath, dpath, noadouble)) < 0)
645 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
649 /* keep the same time stamp. */
650 ut.actime = ut.modtime = st.st_mtime;
656 /* keep the same time stamp. */
657 if (stat(src, &st) == 0) {
658 ut.actime = ut.modtime = st.st_mtime;
668 /* --- public functions follow --- */
670 /* NOTE: we start off with at least one node (the root directory). */
671 struct dir *dirinsert( vol, dir )
677 if ((node = dir_insert(vol, dir)))
680 /* recolor the tree. the current node is red. */
681 dir->d_color = DIRTREE_COLOR_RED;
683 /* parent of this node has to be black. if the parent node
684 * is red, then we have a grandparent. */
685 while ((dir != vol->v_root) &&
686 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
687 /* are we on the left tree? */
688 if (dir->d_back == dir->d_back->d_back->d_left) {
689 node = dir->d_back->d_back->d_right; /* get the right node */
690 if (node->d_color == DIRTREE_COLOR_RED) {
691 /* we're red. we need to change to black. */
692 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
693 node->d_color = DIRTREE_COLOR_BLACK;
694 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
695 dir = dir->d_back->d_back; /* finished. go up. */
697 if (dir == dir->d_back->d_right) {
699 dir_leftrotate(vol, dir);
701 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
702 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
703 dir_rightrotate(vol, dir->d_back->d_back);
706 node = dir->d_back->d_back->d_left;
707 if (node->d_color == DIRTREE_COLOR_RED) {
708 /* we're red. we need to change to black. */
709 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
710 node->d_color = DIRTREE_COLOR_BLACK;
711 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
712 dir = dir->d_back->d_back; /* finished. ascend */
714 if (dir == dir->d_back->d_left) {
716 dir_rightrotate(vol, dir);
718 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
719 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
720 dir_leftrotate(vol, dir->d_back->d_back);
725 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
729 /* free everything down. we don't bother to recolor as this is only
730 * called to free the entire tree */
734 if (!dir || (dir == SENTINEL))
737 if ( dir->d_left != SENTINEL ) {
738 dirfree( dir->d_left );
740 if ( dir->d_right != SENTINEL ) {
741 dirfree( dir->d_right );
744 if (dir != SENTINEL) {
745 if (dir->d_u_name != dir->d_m_name) {
753 /* --------------------------------------------
754 * most of the time mac name and unix name are the same
756 struct dir *dirnew(const char *m_name, const char *u_name)
760 dir = (struct dir *) calloc(1, sizeof( struct dir ));
764 if ((dir->d_m_name = strdup(m_name)) == NULL) {
769 if (m_name == u_name) {
770 dir->d_u_name = dir->d_m_name;
772 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
778 dir->d_left = dir->d_right = SENTINEL;
779 dir->d_next = dir->d_prev = dir;
783 /* -------------------------------------------------- */
784 /* XXX: this needs to be changed to handle path types
799 XXXX u_name can be an alias on m_name if the m_name is
804 cname( vol, dir, cpath )
805 const struct vol *vol;
810 static char path[ MAXPATHLEN + 1];
811 static struct path ret;
824 switch (*data) { /* path type */
827 len = (unsigned char) *data++;
832 if (afp_version >= 30) {
834 memcpy(&hint, data, sizeof(hint));
836 data += sizeof(hint);
838 memcpy(&len16, data, sizeof(len16));
845 /* else it's an error */
850 *cpath += len + size;
858 if ( !extend && movecwd( vol, dir ) < 0 ) {
860 movecwd failed so dir is not there anymore.
861 FIXME Is it true with other errors?
862 if path == '\0' ==> the cpath parameter is that dir,
863 and maybe we are trying to recreate it! So we can't
867 if ( dir->d_did == DIRDID_ROOT_PARENT)
869 cdir = dir->d_parent;
870 dir_invalidate(vol, dir);
871 if (*path != '\0' || u == NULL) {
872 /* FIXME: if path != '\0' then extend != 0 ?
873 * u == NUL ==> cpath is something like:
878 if (movecwd(vol, cdir) < 0) {
879 printf("can't change to parent\n");
880 return NULL; /* give up the whole tree is out of synch*/
882 /* restore the previous token */
883 strncpy(path, u, olen);
886 if (!ret.st_valid || *path == '\0') {
892 if (!*data || *data == sep ) {
898 while ( *data && *data == sep && len > 0 ) {
899 if ( dir->d_parent == NULL ) {
907 /* would this be faster with strlen + strncpy? */
913 while ( *data && *data != sep && len > 0 ) {
918 /* short cut bits by chopping off a trailing \0. this also
919 makes the traversal happy w/ filenames at the end of the
926 if ( p != path ) { /* we got something */
930 if ( strcasecmp( cdir->d_m_name, path ) == 0 ) {
933 cdir = (cdir == dir->d_child->d_prev) ? NULL :
936 if ( cdir == NULL ) {
938 /* if dir == curdir it always succeed,
939 even if curdir is deleted.
940 it's not a pb because it will fail in extenddir
942 if ( movecwd( vol, dir ) < 0 ) {
943 /* dir is not valid anymore
944 we delete dir from the cache and abort.
946 if ( dir->d_did != DIRDID_ROOT_PARENT)
947 dir_invalidate(vol, dir);
950 cdir = extenddir( vol, dir, &ret );
954 cdir = extenddir( vol, dir, &ret );
957 if ( cdir == NULL ) {
971 * Move curdir to dir, with a possible chdir()
973 int movecwd( vol, dir)
974 const struct vol *vol;
977 char path[MAXPATHLEN + 1];
982 if ( dir == curdir ) {
985 if ( dir->d_did == DIRDID_ROOT_PARENT) {
989 p = path + sizeof(path) - 1;
992 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1001 n = strlen( vol->v_path );
1003 strncpy( p, vol->v_path, n );
1005 if ( chdir( p ) < 0 ) {
1013 * We can't use unix file's perm to support Apple's inherited protection modes.
1014 * If we aren't the file's owner we can't change its perms when moving it and smb
1015 * nfs,... don't even try.
1017 #define AFP_CHECK_ACCESS
1019 int check_access(char *path, int mode)
1021 #ifdef AFP_CHECK_ACCESS
1029 accessmode(p, &ma, curdir, NULL);
1030 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1032 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1038 /* ------------------------------
1040 (name, dir) with curdir:name == dir, from afp_enumerate
1043 int getdirparams(const struct vol *vol,
1044 u_int16_t bitmap, struct path *s_path,
1046 char *buf, int *buflen )
1050 char *data, *nameoff = NULL;
1051 int bit = 0, isad = 0;
1056 struct stat *st = &s_path->st;
1057 char *upath = s_path->u_name;
1059 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1060 (1 << DIRPBIT_CDATE) |
1061 (1 << DIRPBIT_MDATE) |
1062 (1 << DIRPBIT_BDATE) |
1063 (1 << DIRPBIT_FINFO)))) {
1064 memset(&ad, 0, sizeof(ad));
1065 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1066 DIRBITS | 0777, &ad)) {
1072 while ( bitmap != 0 ) {
1073 while (( bitmap & 1 ) == 0 ) {
1081 ad_getattr(&ad, &ashort);
1082 } else if (*upath == '.' && strcmp(upath, ".") &&
1083 strcmp(upath, "..")) {
1084 ashort = htons(ATTRBIT_INVISIBLE);
1087 ashort |= htons(ATTRBIT_SHARED);
1088 memcpy( data, &ashort, sizeof( ashort ));
1089 data += sizeof( ashort );
1093 if ( dir->d_did == DIRDID_ROOT) {
1094 aint = DIRDID_ROOT_PARENT;
1095 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1098 aint = dir->d_parent->d_did;
1100 memcpy( data, &aint, sizeof( aint ));
1101 data += sizeof( aint );
1104 case DIRPBIT_CDATE :
1105 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1106 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1107 memcpy( data, &aint, sizeof( aint ));
1108 data += sizeof( aint );
1111 case DIRPBIT_MDATE :
1112 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1113 memcpy( data, &aint, sizeof( aint ));
1114 data += sizeof( aint );
1117 case DIRPBIT_BDATE :
1118 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1119 aint = AD_DATE_START;
1120 memcpy( data, &aint, sizeof( aint ));
1121 data += sizeof( aint );
1124 case DIRPBIT_FINFO :
1126 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1127 } else { /* no appledouble */
1128 memset( data, 0, 32 );
1129 /* set default view -- this also gets done in ad_open() */
1130 ashort = htons(FINDERINFO_CLOSEDVIEW);
1131 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1133 /* dot files are by default invisible */
1134 if (*upath == '.' && strcmp(upath, ".") &&
1135 strcmp(upath, "..")) {
1136 ashort = htons(FINDERINFO_INVISIBLE);
1137 memcpy(data + FINDERINFO_FRFLAGOFF,
1138 &ashort, sizeof(ashort));
1144 case DIRPBIT_LNAME :
1145 if (dir->d_m_name) /* root of parent can have a null name */
1148 memset(data, 0, sizeof(u_int16_t));
1149 data += sizeof( u_int16_t );
1152 case DIRPBIT_SNAME :
1153 memset(data, 0, sizeof(u_int16_t));
1154 data += sizeof( u_int16_t );
1158 memcpy( data, &dir->d_did, sizeof( aint ));
1159 data += sizeof( aint );
1162 case DIRPBIT_OFFCNT :
1164 /* this needs to handle current directory access rights */
1165 if (st->st_ctime == dir->ctime) {
1166 ashort = dir->offcnt;
1168 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1170 dir->offcnt = ashort;
1171 dir->ctime = st->st_ctime;
1173 ashort = htons( ashort );
1174 memcpy( data, &ashort, sizeof( ashort ));
1175 data += sizeof( ashort );
1179 aint = htonl(st->st_uid);
1180 memcpy( data, &aint, sizeof( aint ));
1181 data += sizeof( aint );
1185 aint = htonl(st->st_gid);
1186 memcpy( data, &aint, sizeof( aint ));
1187 data += sizeof( aint );
1190 case DIRPBIT_ACCESS :
1191 accessmode( upath, &ma, dir , st);
1193 *data++ = ma.ma_user;
1194 *data++ = ma.ma_world;
1195 *data++ = ma.ma_group;
1196 *data++ = ma.ma_owner;
1199 /* Client has requested the ProDOS information block.
1200 Just pass back the same basic block for all
1201 directories. <shirsch@ibm.net> */
1202 case DIRPBIT_PDINFO :
1203 if (afp_version >= 30) { /* UTF8 name */
1204 utf8 = kTextEncodingUTF8;
1205 if (dir->d_m_name) /* root of parent can have a null name */
1208 memset(data, 0, sizeof(u_int16_t));
1209 data += sizeof( u_int16_t );
1211 memcpy(data, &aint, sizeof( aint ));
1212 data += sizeof( aint );
1214 else { /* ProDOS Info Block */
1217 ashort = htons( 0x0200 );
1218 memcpy( data, &ashort, sizeof( ashort ));
1219 data += sizeof( ashort );
1220 memset( data, 0, sizeof( ashort ));
1221 data += sizeof( ashort );
1227 ad_close( &ad, ADFLAGS_HF );
1229 return( AFPERR_BITMAP );
1235 ashort = htons( data - buf );
1236 memcpy( nameoff, &ashort, sizeof( ashort ));
1237 data = set_name(data, dir->d_m_name, utf8);
1240 ad_close( &ad, ADFLAGS_HF );
1242 *buflen = data - buf;
1246 /* ----------------------------- */
1247 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1250 int ibuflen, *rbuflen;
1255 u_int16_t vid, bitmap;
1261 memcpy( &vid, ibuf, sizeof( vid ));
1262 ibuf += sizeof( vid );
1264 if (( vol = getvolbyvid( vid )) == NULL ) {
1265 return( AFPERR_PARAM );
1268 if (vol->v_flags & AFPVOL_RO)
1269 return AFPERR_VLOCK;
1271 memcpy( &did, ibuf, sizeof( did ));
1272 ibuf += sizeof( int );
1274 if (( dir = dirlookup( vol, did )) == NULL ) {
1275 return( AFPERR_NOOBJ );
1278 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1279 bitmap = ntohs( bitmap );
1280 ibuf += sizeof( bitmap );
1282 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1283 return( AFPERR_NOOBJ );
1286 if ( *path->m_name != '\0' ) {
1287 return( AFPERR_BADTYPE ); /* not a directory */
1291 * If ibuf is odd, make it even.
1293 if ((u_long)ibuf & 1 ) {
1297 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1298 setvoltime(obj, vol );
1304 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1306 * assume path == '\0' eg. it's a directory in canonical form
1309 struct path Cur_Path = {
1311 ".", /* unix name */
1312 0, /* stat is not set */
1316 int setdirparams(const struct vol *vol,
1317 struct path *path, u_int16_t bitmap, char *buf )
1325 int bit = 0, aint, isad = 1;
1326 u_int16_t ashort, bshort;
1328 int change_mdate = 0;
1329 int change_parent_mdate = 0;
1332 upath = path->u_name;
1333 memset(&ad, 0, sizeof(ad));
1335 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1336 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1338 * Check to see what we're trying to set. If it's anything
1339 * but ACCESS, UID, or GID, give an error. If it's any of those
1340 * three, we don't need the ad to be open, so just continue.
1342 * note: we also don't need to worry about mdate. also, be quiet
1343 * if we're using the noadouble option.
1345 if (!vol_noadouble(vol) && (bitmap &
1346 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1347 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1348 return AFPERR_ACCESS;
1354 * Check to see if a create was necessary. If it was, we'll want
1355 * to set our name, etc.
1357 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1358 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1359 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1360 ad_getentrylen( &ad, ADEID_NAME ));
1364 while ( bitmap != 0 ) {
1365 while (( bitmap & 1 ) == 0 ) {
1374 memcpy( &ashort, buf, sizeof( ashort ));
1375 ad_getattr(&ad, &bshort);
1376 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1377 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1381 ad_setattr(&ad, bshort);
1382 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1383 change_parent_mdate = 1;
1385 buf += sizeof( ashort );
1388 case DIRPBIT_CDATE :
1391 memcpy(&aint, buf, sizeof(aint));
1392 ad_setdate(&ad, AD_DATE_CREATE, aint);
1394 buf += sizeof( aint );
1397 case DIRPBIT_MDATE :
1398 memcpy(&newdate, buf, sizeof(newdate));
1399 buf += sizeof( newdate );
1402 case DIRPBIT_BDATE :
1405 memcpy(&aint, buf, sizeof(aint));
1406 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1408 buf += sizeof( aint );
1411 case DIRPBIT_FINFO :
1414 * Alright, we admit it, this is *really* sick!
1415 * The 4 bytes that we don't copy, when we're dealing
1416 * with the root of a volume, are the directory's
1417 * location information. This eliminates that annoying
1418 * behavior one sees when mounting above another mount
1422 if ( curdir->d_did == DIRDID_ROOT ) {
1423 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1424 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1426 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1432 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1433 change_parent_mdate = 1;
1434 memcpy( &aint, buf, sizeof(aint));
1435 buf += sizeof( aint );
1436 if ( (curdir->d_did == DIRDID_ROOT) &&
1437 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1441 err = AFPERR_ACCESS;
1442 goto setdirparam_done;
1446 goto setdirparam_done;
1449 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1453 goto setdirparam_done;
1458 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1462 err = AFPERR_ACCESS;
1463 goto setdirparam_done;
1467 goto setdirparam_done;
1470 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1477 change_parent_mdate = 1;
1478 memcpy( &aint, buf, sizeof( aint ));
1479 buf += sizeof( aint );
1480 if (curdir->d_did == DIRDID_ROOT)
1481 setdeskowner( -1, ntohl(aint) );
1483 #if 0 /* don't error if we can't set the desktop owner. */
1487 err = AFPERR_ACCESS;
1488 goto setdirparam_done;
1492 goto setdirparam_done;
1495 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1498 goto setdirparam_done;
1504 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1508 err = AFPERR_ACCESS;
1509 goto setdirparam_done;
1513 goto setdirparam_done;
1516 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1523 case DIRPBIT_ACCESS :
1525 change_parent_mdate = 1;
1526 ma.ma_user = *buf++;
1527 ma.ma_world = *buf++;
1528 ma.ma_group = *buf++;
1529 ma.ma_owner = *buf++;
1531 if (curdir->d_did == DIRDID_ROOT)
1532 setdeskmode(mtoumode( &ma ));
1533 #if 0 /* don't error if we can't set the desktop mode */
1537 err = AFPERR_ACCESS;
1538 goto setdirparam_done;
1541 goto setdirparam_done;
1543 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1547 goto setdirparam_done;
1551 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1552 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1556 err = AFPERR_ACCESS;
1557 goto setdirparam_done;
1560 goto setdirparam_done;
1562 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1565 goto setdirparam_done;
1570 /* Ignore what the client thinks we should do to the
1571 ProDOS information block. Skip over the data and
1572 report nothing amiss. <shirsch@ibm.net> */
1573 case DIRPBIT_PDINFO :
1574 if (afp_version < 30) {
1579 err = AFPERR_BITMAP;
1580 goto setdirparam_done;
1589 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1590 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1594 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1595 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1600 ad_flush( &ad, ADFLAGS_HF );
1601 ad_close( &ad, ADFLAGS_HF );
1604 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1605 && gettimeofday(&tv, NULL) == 0) {
1606 if (!movecwd(vol, curdir->d_parent)) {
1607 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1608 bitmap = 1<<DIRPBIT_MDATE;
1609 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1610 /* should we reset curdir ?*/
1617 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1620 int ibuflen, *rbuflen;
1626 struct path *s_path;
1633 memcpy( &vid, ibuf, sizeof( vid ));
1634 ibuf += sizeof( vid );
1635 if (( vol = getvolbyvid( vid )) == NULL ) {
1636 return( AFPERR_PARAM );
1639 if (vol->v_flags & AFPVOL_RO)
1640 return AFPERR_VLOCK;
1642 memcpy( &did, ibuf, sizeof( did ));
1643 ibuf += sizeof( did );
1644 if (( dir = dirlookup( vol, did )) == NULL ) {
1645 return( AFPERR_NOOBJ );
1648 if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
1651 return( AFPERR_ACCESS );
1652 case EEXIST: /* FIXME this one is impossible? */
1653 return( AFPERR_EXIST );
1655 return( AFPERR_NOOBJ );
1658 /* FIXME check done elswhere? cname was able to move curdir to it! */
1659 if (*s_path->m_name == '\0')
1660 return AFPERR_EXIST;
1662 upath = s_path->u_name;
1665 if (0 != (ret = check_name(vol, upath))) {
1670 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1673 return( AFPERR_NOOBJ );
1675 return( AFPERR_VLOCK );
1677 return( AFPERR_ACCESS );
1679 return( AFPERR_EXIST );
1682 return( AFPERR_DFULL );
1684 return( AFPERR_PARAM );
1688 if (of_stat(s_path) < 0) {
1691 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1695 if ( movecwd( vol, dir ) < 0 ) {
1696 return( AFPERR_PARAM );
1699 memset(&ad, 0, sizeof(ad));
1700 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1701 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1702 if (vol_noadouble(vol))
1703 goto createdir_done;
1704 return( AFPERR_ACCESS );
1707 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1708 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1709 ad_getentrylen( &ad, ADEID_NAME ));
1710 ad_flush( &ad, ADFLAGS_HF );
1711 ad_close( &ad, ADFLAGS_HF );
1714 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1715 *rbuflen = sizeof( u_int32_t );
1716 setvoltime(obj, vol );
1721 * dst new unix filename (not a pathname)
1722 * newname new mac name
1726 int renamedir(src, dst, dir, newparent, newname, noadouble)
1727 char *src, *dst, *newname;
1728 struct dir *dir, *newparent;
1729 const int noadouble;
1736 /* existence check moved to afp_moveandrename */
1737 if ( rename( src, dst ) < 0 ) {
1740 return( AFPERR_NOOBJ );
1742 return( AFPERR_ACCESS );
1744 return AFPERR_VLOCK;
1746 /* tried to move directory into a subdirectory of itself */
1747 return AFPERR_CANTMOVE;
1749 /* this needs to copy and delete. bleah. that means we have
1750 * to deal with entire directory hierarchies. */
1751 if ((err = copydir(src, dst, noadouble)) < 0) {
1755 if ((err = deletedir(src)) < 0)
1759 return( AFPERR_PARAM );
1763 memset(&ad, 0, sizeof(ad));
1764 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1768 len = strlen(newname);
1769 goto renamedir_done;
1771 return( AFPERR_NOOBJ );
1773 return( AFPERR_ACCESS );
1775 return( AFPERR_PARAM );
1778 len = strlen( newname );
1779 ad_setentrylen( &ad, ADEID_NAME, len );
1780 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1781 ad_flush( &ad, ADFLAGS_HF );
1782 ad_close( &ad, ADFLAGS_HF );
1785 if (dir->d_m_name == dir->d_u_name)
1786 dir->d_u_name = NULL;
1788 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
1789 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
1790 /* FIXME : fatal ? */
1793 dir->d_m_name = buf;
1794 strcpy( dir->d_m_name, newname );
1796 if (newname == dst) {
1797 free(dir->d_u_name);
1798 dir->d_u_name = dir->d_m_name;
1801 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
1802 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
1805 dir->d_u_name = buf;
1806 strcpy( dir->d_u_name, dst );
1809 if (( parent = dir->d_parent ) == NULL ) {
1812 if ( parent == newparent ) {
1816 /* detach from old parent and add to new one. */
1817 dirchildremove(parent, dir);
1818 dir->d_parent = newparent;
1819 dirchildadd(newparent, dir);
1823 #define DOT_APPLEDOUBLE_LEN 13
1824 /* delete an empty directory */
1825 int deletecurdir( vol, path, pathlen )
1826 const struct vol *vol;
1837 if ( curdir->d_parent == NULL ) {
1838 return( AFPERR_ACCESS );
1841 if ( curdir->d_child != NULL ) {
1842 return( AFPERR_DIRNEMPT );
1847 memset(&ad, 0, sizeof(ad));
1848 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1849 DIRBITS | 0777, &ad) == 0 ) {
1851 ad_getattr(&ad, &ashort);
1852 ad_close( &ad, ADFLAGS_HF );
1853 if ((ashort & htons(ATTRBIT_NODELETE))) {
1854 return AFPERR_OLOCK;
1858 /* delete stray .AppleDouble files. this happens to get .Parent files
1860 if ((dp = opendir(".AppleDouble"))) {
1861 strcpy(path, ".AppleDouble/");
1862 while ((de = readdir(dp))) {
1863 /* skip this and previous directory */
1864 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1867 /* bail if the file exists in the current directory.
1868 * note: this will not fail with dangling symlinks */
1869 if (stat(de->d_name, &st) == 0) {
1871 return AFPERR_DIRNEMPT;
1874 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1875 if (unlink(path) < 0) {
1880 return( AFPERR_ACCESS );
1882 return AFPERR_VLOCK;
1886 return( AFPERR_PARAM );
1893 if ( rmdir( ".AppleDouble" ) < 0 ) {
1898 return( AFPERR_DIRNEMPT );
1900 return AFPERR_VLOCK;
1903 return( AFPERR_ACCESS );
1905 return( AFPERR_PARAM );
1909 /* now get rid of dangling symlinks */
1910 if ((dp = opendir("."))) {
1911 while ((de = readdir(dp))) {
1912 /* skip this and previous directory */
1913 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1916 /* bail if it's not a symlink */
1917 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1918 return AFPERR_DIRNEMPT;
1921 if (unlink(de->d_name) < 0) {
1925 return( AFPERR_ACCESS );
1927 return AFPERR_VLOCK;
1931 return( AFPERR_PARAM );
1938 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1939 return( AFPERR_NOOBJ );
1942 if ( rmdir(fdir->d_u_name) < 0 ) {
1945 return( AFPERR_NOOBJ );
1947 return( AFPERR_DIRNEMPT );
1950 return( AFPERR_ACCESS );
1952 return AFPERR_VLOCK;
1954 return( AFPERR_PARAM );
1958 dirchildremove(curdir, fdir);
1960 cnid_delete(vol->v_db, fdir->d_did);
1961 #endif /* CNID_DB */
1962 dir_remove( vol, fdir );
1967 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1970 int ibuflen, *rbuflen;
1980 sfunc = (unsigned char) *ibuf++;
1981 memcpy( &id, ibuf, sizeof( id ));
1989 case 3 :/* unicode */
1990 if (( pw = getpwuid( id )) == NULL ) {
1991 return( AFPERR_NOITEM );
1997 case 4 : /* unicode */
1998 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1999 return( AFPERR_NOITEM );
2005 return( AFPERR_PARAM );
2010 if (afp_version < 30) {
2011 return( AFPERR_PARAM );
2014 /* map to unicode */
2017 len = strlen( name );
2024 u_int16_t tp = htons(len);
2025 memcpy(rbuf, &tp, sizeof(tp));
2034 memcpy( rbuf, name, len );
2040 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2043 int ibuflen, *rbuflen;
2052 sfunc = (unsigned char) *ibuf++;
2055 case 2 : /* unicode */
2056 memcpy(&ulen, ibuf, sizeof(ulen));
2062 len = (unsigned char) *ibuf++;
2066 return( AFPERR_PARAM );
2073 case 1 : /* unicode */
2075 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
2077 return( AFPERR_NOITEM );
2082 case 2 : /* unicode */
2084 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
2086 return( AFPERR_NOITEM );
2095 memcpy( rbuf, &id, sizeof( id ));
2096 *rbuflen = sizeof( id );
2100 /* ------------------------------------
2101 variable DID support
2103 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2106 int ibuflen, *rbuflen;
2117 /* do nothing as dids are static for the life of the process. */
2121 memcpy(&vid, ibuf, sizeof( vid ));
2122 ibuf += sizeof( vid );
2123 if (( vol = getvolbyvid( vid )) == NULL ) {
2124 return( AFPERR_PARAM );
2127 memcpy( &did, ibuf, sizeof( did ));
2128 ibuf += sizeof( did );
2129 if (( dir = dirlookup( vol, did )) == NULL ) {
2130 return( AFPERR_PARAM );
2133 /* dir_remove -- deletedid */
2139 /* did creation gets done automatically
2140 * there's a pb again with case but move it to cname
2142 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2145 int ibuflen, *rbuflen;
2148 struct dir *parentdir;
2156 memcpy(&vid, ibuf, sizeof(vid));
2157 ibuf += sizeof( vid );
2159 if (( vol = getvolbyvid( vid )) == NULL ) {
2160 return( AFPERR_PARAM );
2163 memcpy(&did, ibuf, sizeof(did));
2164 ibuf += sizeof(did);
2166 if (( parentdir = dirlookup( vol, did )) == NULL ) {
2167 return( AFPERR_NOOBJ );
2170 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2173 return( AFPERR_ACCESS );
2175 return( AFPERR_NOOBJ );
2179 if ( *path->m_name != '\0' ) {
2180 return( AFPERR_BADTYPE ); /* not a directory */
2183 if ( !path->st_valid && of_stat(path ) < 0 ) {
2184 return( AFPERR_NOOBJ );
2186 if ( path->st_errno ) {
2187 return( AFPERR_NOOBJ );
2190 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2191 *rbuflen = sizeof(curdir->d_did);