2 * $Id: directory.c,v 1.41 2002-10-05 14:04:47 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, NULL };
67 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
68 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
70 /* (from IM: Toolbox Essentials)
71 * dirFinderInfo (DInfo) fields:
73 * frRect 8 folder's window rectangle
75 * frLocation 4 folder's location in window
76 * frView 2 folder's view (default == closedView (256))
78 * extended dirFinderInfo (DXInfo) fields:
79 * frScroll 4 scroll position
80 * frOpenChain: 4 directory ID chain of open folders
81 * frScript: 1 script flag and code
82 * frXFlags: 1 reserved
83 * frComment: 2 comment ID
84 * frPutAway: 4 home directory ID
88 * redid did assignment for directories. now we use red-black trees.
93 const struct vol *vol;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
111 while ( dir != SENTINEL ) {
112 if (dir->d_did == did)
113 return dir->d_name ? dir : NULL;
114 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
119 /* -----------------------------------------
120 * if did is not in the cache resolve it with cnid
124 dirlookup( vol, did )
125 const struct vol *vol;
132 static char path[MAXPATHLEN + 1];
136 static char buffer[12 + MAXPATHLEN + 1];
137 int buflen = 12 + MAXPATHLEN + 1;
140 ret = dirsearch(vol, did);
145 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
148 ptr = path + MAXPATHLEN;
149 mpath = utompath(vol, upath);
151 pathlen = len; /* no 0 in the last part */
153 strcpy(ptr - len, mpath);
156 ret = dirsearch(vol,id);
160 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL)
162 mpath = utompath(vol, upath);
163 len = strlen(mpath) + 1;
167 strcpy(ptr - len, mpath);
172 *ptr = (unsigned char)pathlen;
175 /* cname is not efficient */
176 if (cname( vol, ret, &ptr ) == NULL )
179 return dirsearch(vol, did);
182 /* --------------------------- */
183 /* rotate the tree to the left */
184 static void dir_leftrotate(vol, dir)
188 struct dir *right = dir->d_right;
190 /* whee. move the right's left tree into dir's right tree */
191 dir->d_right = right->d_left;
192 if (right->d_left != SENTINEL)
193 right->d_left->d_back = dir;
195 if (right != SENTINEL) {
196 right->d_back = dir->d_back;
200 if (!dir->d_back) /* no parent. move the right tree to the top. */
202 else if (dir == dir->d_back->d_left) /* we were on the left */
203 dir->d_back->d_left = right;
205 dir->d_back->d_right = right; /* we were on the right */
207 /* re-insert dir on the left tree */
214 /* rotate the tree to the right */
215 static void dir_rightrotate(vol, dir)
219 struct dir *left = dir->d_left;
221 /* whee. move the left's right tree into dir's left tree */
222 dir->d_left = left->d_right;
223 if (left->d_right != SENTINEL)
224 left->d_right->d_back = dir;
226 if (left != SENTINEL) {
227 left->d_back = dir->d_back;
231 if (!dir->d_back) /* no parent. move the left tree to the top. */
233 else if (dir == dir->d_back->d_right) /* we were on the right */
234 dir->d_back->d_right = left;
236 dir->d_back->d_left = left; /* we were on the left */
238 /* re-insert dir on the right tree */
244 /* recolor after a removal */
245 static struct dir *dir_rmrecolor(vol, dir)
251 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
252 /* are we on the left tree? */
253 if (dir == dir->d_back->d_left) {
254 leaf = dir->d_back->d_right; /* get right side */
255 if (leaf->d_color == DIRTREE_COLOR_RED) {
256 /* we're red. we need to change to black. */
257 leaf->d_color = DIRTREE_COLOR_BLACK;
258 dir->d_back->d_color = DIRTREE_COLOR_RED;
259 dir_leftrotate(vol, dir->d_back);
260 leaf = dir->d_back->d_right;
263 /* right leaf has black end nodes */
264 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
265 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
266 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
267 dir = dir->d_back; /* ascend */
269 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
270 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
271 leaf->d_color = DIRTREE_COLOR_RED;
272 dir_rightrotate(vol, leaf);
273 leaf = dir->d_back->d_right;
275 leaf->d_color = dir->d_back->d_color;
276 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
277 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
278 dir_leftrotate(vol, dir->d_back);
281 } else { /* right tree */
282 leaf = dir->d_back->d_left; /* left tree */
283 if (leaf->d_color == DIRTREE_COLOR_RED) {
284 leaf->d_color = DIRTREE_COLOR_BLACK;
285 dir->d_back->d_color = DIRTREE_COLOR_RED;
286 dir_rightrotate(vol, dir->d_back);
287 leaf = dir->d_back->d_left;
290 /* left leaf has black end nodes */
291 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
292 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
293 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
294 dir = dir->d_back; /* ascend */
296 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
297 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
298 leaf->d_color = DIRTREE_COLOR_RED;
299 dir_leftrotate(vol, leaf);
300 leaf = dir->d_back->d_left;
302 leaf->d_color = dir->d_back->d_color;
303 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
304 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
305 dir_rightrotate(vol, dir->d_back);
310 dir->d_color = DIRTREE_COLOR_BLACK;
317 /* remove the node from the tree. this is just like insertion, but
318 * different. actually, it has to worry about a bunch of things that
319 * insertion doesn't care about. */
320 static void dir_remove( vol, dir )
325 struct ofork *of, *last;
326 struct dir *node, *leaf;
327 #endif /* REMOVE_NODES */
329 if (!dir || (dir == SENTINEL))
332 /* i'm not sure if it really helps to delete stuff. */
336 #else /* ! REMOVE_NODES */
338 /* go searching for a node with at most one child */
339 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
343 while (node->d_left != SENTINEL)
348 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
351 leaf->d_back = node->d_back;
354 } else if (node == node->d_back->d_left) { /* left tree */
355 node->d_back->d_left = leaf;
357 node->d_back->d_right = leaf;
360 /* we want to free node, but we also want to free the data in dir.
361 * currently, that's d_name and the directory traversal bits.
362 * we just copy the necessary bits and then fix up all the
363 * various pointers to the directory. needless to say, there are
364 * a bunch of places that store the directory struct. */
366 struct dir save, *tmp;
368 memcpy(&save, dir, sizeof(save));
369 memcpy(dir, node, sizeof(struct dir));
371 /* restore the red-black bits */
372 dir->d_left = save.d_left;
373 dir->d_right = save.d_right;
374 dir->d_back = save.d_back;
375 dir->d_color = save.d_color;
377 if (node == vol->v_dir) {/* we may need to fix up this pointer */
379 rootpar.d_child = vol->v_dir;
381 /* if we aren't the root directory, we have parents and
382 * siblings to worry about */
383 if (dir->d_parent->d_child == node)
384 dir->d_parent->d_child = dir;
385 dir->d_next->d_prev = dir;
386 dir->d_prev->d_next = dir;
389 /* fix up children. */
393 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
396 if (node == curdir) /* another pointer to fixup */
399 /* we also need to fix up oforks. bleah */
400 if ((of = dir->d_ofork)) {
401 last = of->of_d_prev;
404 of = (last == of) ? NULL : of->of_d_next;
408 /* set the node's d_name */
409 node->d_name = save.d_name;
412 if (node->d_color == DIRTREE_COLOR_BLACK)
413 dir_rmrecolor(vol, leaf);
416 #endif /* ! REMOVE_NODES */
419 /* ---------------------------------------
420 * remove the node and its childs from the tree
422 * FIXME what about opened forks with refs to it?
423 * it's an afp specs violation because you can't delete
424 * an opened forks. Now afpd doesn't care about forks opened by other
425 * process. It's fixable within afpd if fnctl_lock, doable with smb and
426 * next to impossible for nfs and local filesystem access.
429 static void dir_invalidate( vol, dir )
430 const struct vol *vol;
434 /* v_root can't be deleted */
435 if (movecwd(vol, vol->v_root) < 0)
436 printf("Yuup cant change dir to v_root\n");
439 dirchildremove(dir->d_parent, dir);
440 dir_remove( vol, dir );
443 /* ------------------------------------ */
444 static struct dir *dir_insert(vol, dir)
445 const struct vol *vol;
451 while (pdir->d_did != dir->d_did ) {
452 if ( pdir->d_did > dir->d_did ) {
453 if ( pdir->d_left == SENTINEL ) {
460 if ( pdir->d_right == SENTINEL ) {
465 pdir = pdir->d_right;
473 * attempt to extend the current dir. tree to include path
474 * as a side-effect, movecwd to that point and return the new dir
478 extenddir( vol, dir, path )
486 p = mtoupath(vol, path );
487 if ( stat( p, &st ) != 0 ) {
490 if (!S_ISDIR(st.st_mode)) {
494 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
499 if ( movecwd( vol, dir ) < 0 ) {
506 static int deletedir(char *dir)
508 char path[MAXPATHLEN + 1];
514 if ((len = strlen(dir)) > sizeof(path))
518 if ((dp = opendir(dir)) == NULL)
524 while ((de = readdir(dp))) {
525 /* skip this and previous directory */
526 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
529 strncpy(path + len, de->d_name, sizeof(path) - len);
530 if (stat(path, &st) == 0) {
531 if (S_ISDIR(st.st_mode)) {
532 if ((err = deletedir(path)) < 0) {
536 } else if (unlink(path) < 0) {
539 continue; /* somebody went and deleted it behind our backs. */
557 /* okay. the directory is empty. delete it. note: we already got rid
559 if (rmdir(dir) < 0) {
563 case ENOTEMPTY : /* should never happen */
564 return( AFPERR_DIRNEMPT );
567 return( AFPERR_ACCESS );
571 return( AFPERR_PARAM );
577 /* do a recursive copy. */
578 static int copydir(char *src, char *dst, int noadouble)
580 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
587 /* doesn't exist or the path is too long. */
588 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
589 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
590 ((dp = opendir(src)) == NULL))
593 /* try to create the destination directory */
594 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
598 return( AFPERR_NOOBJ );
600 return( AFPERR_VLOCK );
603 return( AFPERR_ACCESS );
605 return( AFPERR_EXIST );
608 return( AFPERR_DFULL );
610 return( AFPERR_PARAM );
614 /* set things up to copy */
622 while ((de = readdir(dp))) {
623 /* skip this and previous directory */
624 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
627 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
628 if (stat(spath, &st) == 0) {
629 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
631 if (S_ISDIR(st.st_mode)) {
632 if ((err = copydir(spath, dpath, noadouble)) < 0)
634 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
638 /* keep the same time stamp. */
639 ut.actime = ut.modtime = st.st_mtime;
645 /* keep the same time stamp. */
646 if (stat(src, &st) == 0) {
647 ut.actime = ut.modtime = st.st_mtime;
657 /* --- public functions follow --- */
659 /* NOTE: we start off with at least one node (the root directory). */
660 struct dir *dirinsert( vol, dir )
666 if ((node = dir_insert(vol, dir)))
669 /* recolor the tree. the current node is red. */
670 dir->d_color = DIRTREE_COLOR_RED;
672 /* parent of this node has to be black. if the parent node
673 * is red, then we have a grandparent. */
674 while ((dir != vol->v_root) &&
675 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
676 /* are we on the left tree? */
677 if (dir->d_back == dir->d_back->d_back->d_left) {
678 node = dir->d_back->d_back->d_right; /* get the right node */
679 if (node->d_color == DIRTREE_COLOR_RED) {
680 /* we're red. we need to change to black. */
681 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
682 node->d_color = DIRTREE_COLOR_BLACK;
683 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
684 dir = dir->d_back->d_back; /* finished. go up. */
686 if (dir == dir->d_back->d_right) {
688 dir_leftrotate(vol, dir);
690 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
691 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
692 dir_rightrotate(vol, dir->d_back->d_back);
695 node = dir->d_back->d_back->d_left;
696 if (node->d_color == DIRTREE_COLOR_RED) {
697 /* we're red. we need to change to black. */
698 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
699 node->d_color = DIRTREE_COLOR_BLACK;
700 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
701 dir = dir->d_back->d_back; /* finished. ascend */
703 if (dir == dir->d_back->d_left) {
705 dir_rightrotate(vol, dir);
707 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
708 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
709 dir_leftrotate(vol, dir->d_back->d_back);
714 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
718 /* free everything down. we don't bother to recolor as this is only
719 * called to free the entire tree */
723 if (!dir || (dir == SENTINEL))
726 if ( dir->d_left != SENTINEL ) {
727 dirfree( dir->d_left );
729 if ( dir->d_right != SENTINEL ) {
730 dirfree( dir->d_right );
733 if (dir != SENTINEL) {
740 struct dir *dirnew(const int len)
744 dir = (struct dir *) calloc(1, sizeof( struct dir ));
748 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
753 dir->d_left = dir->d_right = SENTINEL;
754 dir->d_next = dir->d_prev = dir;
759 /* XXX: this needs to be changed to handle path types */
761 cname( vol, dir, cpath )
762 const struct vol *vol;
767 static char path[ MAXPATHLEN + 1];
775 if ( *data++ != 2 ) { /* path type */
778 len = (unsigned char) *data++;
785 if ( !extend && movecwd( vol, dir ) < 0 ) {
787 movecwd failed so dir is not there anymore.
788 FIXME Is it true with other errors?
789 if path == '\0' ==> the cpath parameter is that dir,
790 and maybe we are trying to recreate it! So we can't
794 if ( dir->d_did == DIRDID_ROOT_PARENT)
796 cdir = dir->d_parent;
797 dir_invalidate(vol, dir);
798 if (*path != '\0' || u == NULL) {
799 /* FIXME: if path != '\0' then extend != 0 ?
800 * u == NUL ==> cpath is something like:
805 if (movecwd(vol, cdir) < 0) {
806 printf("can't change to parent\n");
807 return NULL; /* give up the whole tree is out of synch*/
809 /* restore the previous token */
810 strncpy(path, u, olen);
816 if ( *data == '\0' ) {
822 while ( *data == '\0' && len > 0 ) {
823 if ( dir->d_parent == NULL ) {
831 /* would this be faster with strlen + strncpy? */
837 while ( *data != '\0' && len > 0 ) {
842 /* short cut bits by chopping off a trailing \0. this also
843 makes the traversal happy w/ filenames at the end of the
850 * Dung Nguyen <ntd@adb.fr>
852 * AFPD cannot handle paths with "::" if the "::" notation is
853 * not at the beginning of the path. The following path will not
854 * be interpreted correctly:
856 * :a:b:::c: (directory c at the same level as directory a) */
864 if ( p != path ) { /* we got something */
868 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
871 cdir = (cdir == dir->d_child->d_prev) ? NULL :
874 if ( cdir == NULL ) {
876 /* if dir == curdir it always succeed,
877 even if curdir is deleted.
878 it's not a pb because it will failed in extenddir
880 if ( movecwd( vol, dir ) < 0 ) {
881 /* dir is not valid anymore
882 we delete dir from the cache and abort.
884 dir_invalidate(vol, dir);
887 cdir = extenddir( vol, dir, path );
891 cdir = extenddir( vol, dir, path );
894 if ( cdir == NULL ) {
908 * Move curdir to dir, with a possible chdir()
910 int movecwd( vol, dir)
911 const struct vol *vol;
914 char path[MAXPATHLEN + 1];
919 if ( dir == curdir ) {
922 if ( dir->d_did == DIRDID_ROOT_PARENT) {
926 p = path + sizeof(path) - 1;
929 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
931 u = mtoupath(vol, d->d_name );
938 n = strlen( vol->v_path );
940 strncpy( p, vol->v_path, n );
942 if ( chdir( p ) < 0 ) {
949 int getdirparams(const struct vol *vol,
951 char *upath, struct dir *dir, struct stat *st,
952 char *buf, int *buflen )
956 char *data, *nameoff = NULL;
959 int bit = 0, isad = 1;
963 memset(&ad, 0, sizeof(ad));
965 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
966 DIRBITS | 0777, &ad) < 0 ) {
971 while ( bitmap != 0 ) {
972 while (( bitmap & 1 ) == 0 ) {
980 ad_getattr(&ad, &ashort);
981 } else if (*upath == '.' && strcmp(upath, ".") &&
982 strcmp(upath, "..")) {
983 ashort = htons(ATTRBIT_INVISIBLE);
986 ashort |= htons(ATTRBIT_SHARED);
987 memcpy( data, &ashort, sizeof( ashort ));
988 data += sizeof( ashort );
992 if ( dir->d_did == DIRDID_ROOT) {
993 aint = DIRDID_ROOT_PARENT;
994 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
997 aint = dir->d_parent->d_did;
999 memcpy( data, &aint, sizeof( aint ));
1000 data += sizeof( aint );
1003 case DIRPBIT_CDATE :
1004 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1005 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1006 memcpy( data, &aint, sizeof( aint ));
1007 data += sizeof( aint );
1010 case DIRPBIT_MDATE :
1011 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1012 memcpy( data, &aint, sizeof( aint ));
1013 data += sizeof( aint );
1016 case DIRPBIT_BDATE :
1017 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1018 aint = AD_DATE_START;
1019 memcpy( data, &aint, sizeof( aint ));
1020 data += sizeof( aint );
1023 case DIRPBIT_FINFO :
1025 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1026 } else { /* no appledouble */
1027 memset( data, 0, 32 );
1028 /* set default view -- this also gets done in ad_open() */
1029 ashort = htons(FINDERINFO_CLOSEDVIEW);
1030 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1032 /* dot files are by default invisible */
1033 if (*upath == '.' && strcmp(upath, ".") &&
1034 strcmp(upath, "..")) {
1035 ashort = htons(FINDERINFO_INVISIBLE);
1036 memcpy(data + FINDERINFO_FRFLAGOFF,
1037 &ashort, sizeof(ashort));
1043 case DIRPBIT_LNAME :
1044 if (dir->d_name) /* root of parent can have a null name */
1047 memset(data, 0, sizeof(u_int16_t));
1048 data += sizeof( u_int16_t );
1051 case DIRPBIT_SNAME :
1052 memset(data, 0, sizeof(u_int16_t));
1053 data += sizeof( u_int16_t );
1057 memcpy( data, &dir->d_did, sizeof( aint ));
1058 data += sizeof( aint );
1061 case DIRPBIT_OFFCNT :
1063 /* this needs to handle current directory access rights */
1064 if ((dp = opendir( upath ))) {
1065 while (( de = readdir( dp )) != NULL ) {
1066 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
1069 if (!validupath(vol, de->d_name))
1072 /* check for vetoed filenames */
1073 if (veto_file(vol->v_veto, de->d_name))
1076 /* now check against too long a filename */
1077 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
1084 ashort = htons( ashort );
1085 memcpy( data, &ashort, sizeof( ashort ));
1086 data += sizeof( ashort );
1090 aint = htonl(st->st_uid);
1091 memcpy( data, &aint, sizeof( aint ));
1092 data += sizeof( aint );
1096 aint = htonl(st->st_gid);
1097 memcpy( data, &aint, sizeof( aint ));
1098 data += sizeof( aint );
1101 case DIRPBIT_ACCESS :
1102 accessmode( upath, &ma, dir , st);
1104 *data++ = ma.ma_user;
1105 *data++ = ma.ma_world;
1106 *data++ = ma.ma_group;
1107 *data++ = ma.ma_owner;
1110 /* Client has requested the ProDOS information block.
1111 Just pass back the same basic block for all
1112 directories. <shirsch@ibm.net> */
1113 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1116 ashort = htons( 0x0200 );
1117 memcpy( data, &ashort, sizeof( ashort ));
1118 data += sizeof( ashort );
1119 memset( data, 0, sizeof( ashort ));
1120 data += sizeof( ashort );
1125 ad_close( &ad, ADFLAGS_HF );
1127 return( AFPERR_BITMAP );
1133 ashort = htons( data - buf );
1134 memcpy( nameoff, &ashort, sizeof( ashort ));
1136 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1140 memcpy( data, dir->d_name, aint );
1144 ad_close( &ad, ADFLAGS_HF );
1146 *buflen = data - buf;
1150 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1153 int ibuflen, *rbuflen;
1158 u_int16_t vid, bitmap;
1164 memcpy( &vid, ibuf, sizeof( vid ));
1165 ibuf += sizeof( vid );
1167 if (( vol = getvolbyvid( vid )) == NULL ) {
1168 return( AFPERR_PARAM );
1171 if (vol->v_flags & AFPVOL_RO)
1172 return AFPERR_VLOCK;
1174 memcpy( &did, ibuf, sizeof( did ));
1175 ibuf += sizeof( int );
1177 if (( dir = dirlookup( vol, did )) == NULL ) {
1178 return( AFPERR_NOOBJ );
1181 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1182 bitmap = ntohs( bitmap );
1183 ibuf += sizeof( bitmap );
1185 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1186 return( AFPERR_NOOBJ );
1189 if ( *path != '\0' ) {
1190 return( AFPERR_BADTYPE ); /* not a directory */
1194 * If ibuf is odd, make it even.
1196 if ((u_long)ibuf & 1 ) {
1200 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1201 setvoltime(obj, vol );
1207 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1209 * assume path == '\0' eg. it's a directory in canonical form
1211 int setdirparams(const struct vol *vol,
1212 char *path, u_int16_t bitmap, char *buf )
1220 int bit = 0, aint, isad = 1;
1221 u_int16_t ashort, bshort;
1223 int change_mdate = 0;
1224 int change_parent_mdate = 0;
1227 upath = mtoupath(vol, path);
1228 memset(&ad, 0, sizeof(ad));
1230 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1231 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1233 * Check to see what we're trying to set. If it's anything
1234 * but ACCESS, UID, or GID, give an error. If it's any of those
1235 * three, we don't need the ad to be open, so just continue.
1237 * note: we also don't need to worry about mdate. also, be quiet
1238 * if we're using the noadouble option.
1240 if (!vol_noadouble(vol) && (bitmap &
1241 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1242 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1243 return AFPERR_ACCESS;
1249 * Check to see if a create was necessary. If it was, we'll want
1250 * to set our name, etc.
1252 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1253 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1254 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1255 ad_getentrylen( &ad, ADEID_NAME ));
1259 while ( bitmap != 0 ) {
1260 while (( bitmap & 1 ) == 0 ) {
1269 memcpy( &ashort, buf, sizeof( ashort ));
1270 ad_getattr(&ad, &bshort);
1271 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1272 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1276 ad_setattr(&ad, bshort);
1277 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1278 change_parent_mdate = 1;
1280 buf += sizeof( ashort );
1283 case DIRPBIT_CDATE :
1286 memcpy(&aint, buf, sizeof(aint));
1287 ad_setdate(&ad, AD_DATE_CREATE, aint);
1289 buf += sizeof( aint );
1292 case DIRPBIT_MDATE :
1293 memcpy(&newdate, buf, sizeof(newdate));
1294 buf += sizeof( newdate );
1297 case DIRPBIT_BDATE :
1300 memcpy(&aint, buf, sizeof(aint));
1301 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1303 buf += sizeof( aint );
1306 case DIRPBIT_FINFO :
1309 * Alright, we admit it, this is *really* sick!
1310 * The 4 bytes that we don't copy, when we're dealing
1311 * with the root of a volume, are the directory's
1312 * location information. This eliminates that annoying
1313 * behavior one sees when mounting above another mount
1317 if ( curdir->d_did == DIRDID_ROOT ) {
1318 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1319 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1321 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1327 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1328 change_parent_mdate = 1;
1329 memcpy( &aint, buf, sizeof(aint));
1330 buf += sizeof( aint );
1331 if ( (curdir->d_did == DIRDID_ROOT) &&
1332 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1336 err = AFPERR_ACCESS;
1337 goto setdirparam_done;
1341 goto setdirparam_done;
1344 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1348 goto setdirparam_done;
1353 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1357 err = AFPERR_ACCESS;
1358 goto setdirparam_done;
1362 goto setdirparam_done;
1365 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1372 change_parent_mdate = 1;
1373 memcpy( &aint, buf, sizeof( aint ));
1374 buf += sizeof( aint );
1375 if (curdir->d_did == DIRDID_ROOT)
1376 setdeskowner( -1, ntohl(aint) );
1378 #if 0 /* don't error if we can't set the desktop owner. */
1382 err = AFPERR_ACCESS;
1383 goto setdirparam_done;
1387 goto setdirparam_done;
1390 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1393 goto setdirparam_done;
1399 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1403 err = AFPERR_ACCESS;
1404 goto setdirparam_done;
1408 goto setdirparam_done;
1411 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1418 case DIRPBIT_ACCESS :
1420 change_parent_mdate = 1;
1421 ma.ma_user = *buf++;
1422 ma.ma_world = *buf++;
1423 ma.ma_group = *buf++;
1424 ma.ma_owner = *buf++;
1426 if (curdir->d_did == DIRDID_ROOT)
1427 setdeskmode(mtoumode( &ma ));
1428 #if 0 /* don't error if we can't set the desktop mode */
1432 err = AFPERR_ACCESS;
1433 goto setdirparam_done;
1436 goto setdirparam_done;
1438 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1442 goto setdirparam_done;
1446 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1447 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1451 err = AFPERR_ACCESS;
1452 goto setdirparam_done;
1455 goto setdirparam_done;
1457 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1460 goto setdirparam_done;
1465 /* Ignore what the client thinks we should do to the
1466 ProDOS information block. Skip over the data and
1467 report nothing amiss. <shirsch@ibm.net> */
1468 case DIRPBIT_PDINFO :
1473 err = AFPERR_BITMAP;
1474 goto setdirparam_done;
1483 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1484 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1488 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1489 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1494 ad_flush( &ad, ADFLAGS_HF );
1495 ad_close( &ad, ADFLAGS_HF );
1498 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1499 && gettimeofday(&tv, NULL) == 0) {
1500 if (!movecwd(vol, curdir->d_parent)) {
1501 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1502 bitmap = 1<<DIRPBIT_MDATE;
1503 setdirparams(vol, "", bitmap, (char *)&newdate);
1504 /* should we reset curdir ?*/
1511 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1514 int ibuflen, *rbuflen;
1527 memcpy( &vid, ibuf, sizeof( vid ));
1528 ibuf += sizeof( vid );
1529 if (( vol = getvolbyvid( vid )) == NULL ) {
1530 return( AFPERR_PARAM );
1533 if (vol->v_flags & AFPVOL_RO)
1534 return AFPERR_VLOCK;
1536 memcpy( &did, ibuf, sizeof( did ));
1537 ibuf += sizeof( did );
1538 if (( dir = dirlookup( vol, did )) == NULL ) {
1539 return( AFPERR_NOOBJ );
1542 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1545 return( AFPERR_ACCESS );
1546 case EEXIST: /* FIXME this on is impossible? */
1547 return( AFPERR_EXIST );
1549 return( AFPERR_NOOBJ );
1552 /* FIXME check done elswhere? cname was able to move curdir to it! */
1554 return AFPERR_EXIST;
1555 upath = mtoupath(vol, path);
1558 if (0 != (ret = check_name(vol, upath))) {
1563 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1566 return( AFPERR_NOOBJ );
1568 return( AFPERR_VLOCK );
1570 return( AFPERR_ACCESS );
1572 return( AFPERR_EXIST );
1575 return( AFPERR_DFULL );
1577 return( AFPERR_PARAM );
1581 if (stat(upath, &st) < 0) {
1585 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1586 strlen(upath), &st)) == NULL) {
1590 if ( movecwd( vol, dir ) < 0 ) {
1591 return( AFPERR_PARAM );
1594 memset(&ad, 0, sizeof(ad));
1595 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1596 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1597 if (vol_noadouble(vol))
1598 goto createdir_done;
1599 return( AFPERR_ACCESS );
1602 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1603 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1604 ad_getentrylen( &ad, ADEID_NAME ));
1605 ad_flush( &ad, ADFLAGS_HF );
1606 ad_close( &ad, ADFLAGS_HF );
1609 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1610 *rbuflen = sizeof( u_int32_t );
1611 setvoltime(obj, vol );
1616 int renamedir(src, dst, dir, newparent, newname, noadouble)
1617 char *src, *dst, *newname;
1618 struct dir *dir, *newparent;
1619 const int noadouble;
1626 /* existence check moved to afp_moveandrename */
1627 if ( rename( src, dst ) < 0 ) {
1630 return( AFPERR_NOOBJ );
1632 return( AFPERR_ACCESS );
1634 return AFPERR_VLOCK;
1636 /* tried to move directory into a subdirectory of itself */
1637 return AFPERR_CANTMOVE;
1639 /* this needs to copy and delete. bleah. that means we have
1640 * to deal with entire directory hierarchies. */
1641 if ((err = copydir(src, dst, noadouble)) < 0) {
1645 if ((err = deletedir(src)) < 0)
1649 return( AFPERR_PARAM );
1653 memset(&ad, 0, sizeof(ad));
1654 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1658 len = strlen(newname);
1659 goto renamedir_done;
1661 return( AFPERR_NOOBJ );
1663 return( AFPERR_ACCESS );
1665 return( AFPERR_PARAM );
1668 len = strlen( newname );
1669 ad_setentrylen( &ad, ADEID_NAME, len );
1670 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1671 ad_flush( &ad, ADFLAGS_HF );
1672 ad_close( &ad, ADFLAGS_HF );
1675 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1676 LOG(log_error, logtype_afpd, "renamedir: realloc: %s", strerror(errno) );
1680 strcpy( dir->d_name, newname );
1682 if (( parent = dir->d_parent ) == NULL ) {
1685 if ( parent == newparent ) {
1689 /* detach from old parent and add to new one. */
1690 dirchildremove(parent, dir);
1691 dir->d_parent = newparent;
1692 dirchildadd(newparent, dir);
1696 #define DOT_APPLEDOUBLE_LEN 13
1697 /* delete an empty directory */
1698 int deletecurdir( vol, path, pathlen )
1699 const struct vol *vol;
1710 if ( curdir->d_parent == NULL ) {
1711 return( AFPERR_ACCESS );
1714 if ( curdir->d_child != NULL ) {
1715 return( AFPERR_DIRNEMPT );
1720 memset(&ad, 0, sizeof(ad));
1721 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1722 DIRBITS | 0777, &ad) == 0 ) {
1724 ad_getattr(&ad, &ashort);
1725 ad_close( &ad, ADFLAGS_HF );
1726 if ((ashort & htons(ATTRBIT_NODELETE))) {
1727 return AFPERR_OLOCK;
1731 /* delete stray .AppleDouble files. this happens to get .Parent files
1733 if ((dp = opendir(".AppleDouble"))) {
1734 strcpy(path, ".AppleDouble/");
1735 while ((de = readdir(dp))) {
1736 /* skip this and previous directory */
1737 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1740 /* bail if the file exists in the current directory.
1741 * note: this will not fail with dangling symlinks */
1742 if (stat(de->d_name, &st) == 0) {
1744 return AFPERR_DIRNEMPT;
1747 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1748 if (unlink(path) < 0) {
1753 return( AFPERR_ACCESS );
1755 return AFPERR_VLOCK;
1759 return( AFPERR_PARAM );
1766 if ( rmdir( ".AppleDouble" ) < 0 ) {
1771 return( AFPERR_DIRNEMPT );
1773 return AFPERR_VLOCK;
1776 return( AFPERR_ACCESS );
1778 return( AFPERR_PARAM );
1782 /* now get rid of dangling symlinks */
1783 if ((dp = opendir("."))) {
1784 while ((de = readdir(dp))) {
1785 /* skip this and previous directory */
1786 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1789 /* bail if it's not a symlink */
1790 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1791 return AFPERR_DIRNEMPT;
1794 if (unlink(de->d_name) < 0) {
1798 return( AFPERR_ACCESS );
1800 return AFPERR_VLOCK;
1804 return( AFPERR_PARAM );
1811 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1812 return( AFPERR_NOOBJ );
1815 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1818 return( AFPERR_NOOBJ );
1820 return( AFPERR_DIRNEMPT );
1823 return( AFPERR_ACCESS );
1825 return AFPERR_VLOCK;
1827 return( AFPERR_PARAM );
1831 dirchildremove(curdir, fdir);
1833 cnid_delete(vol->v_db, fdir->d_did);
1834 #endif /* CNID_DB */
1835 dir_remove( vol, fdir );
1840 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1843 int ibuflen, *rbuflen;
1852 sfunc = (unsigned char) *ibuf++;
1853 memcpy( &id, ibuf, sizeof( id ));
1860 if (( pw = getpwuid( id )) == NULL ) {
1862 return( AFPERR_NOITEM );
1868 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1870 return( AFPERR_NOITEM );
1877 return( AFPERR_PARAM );
1880 len = strlen( name );
1889 memcpy( rbuf, name, len );
1895 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1898 int ibuflen, *rbuflen;
1906 sfunc = (unsigned char) *ibuf++;
1907 len = (unsigned char) *ibuf++;
1913 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1915 return( AFPERR_NOITEM );
1921 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1923 return( AFPERR_NOITEM );
1929 return( AFPERR_PARAM );
1935 memcpy( rbuf, &id, sizeof( id ));
1936 *rbuflen = sizeof( id );
1940 /* variable DID support */
1941 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1944 int ibuflen, *rbuflen;
1955 /* do nothing as dids are static for the life of the process. */
1959 memcpy(&vid, ibuf, sizeof( vid ));
1960 ibuf += sizeof( vid );
1961 if (( vol = getvolbyvid( vid )) == NULL ) {
1962 return( AFPERR_PARAM );
1965 memcpy( &did, ibuf, sizeof( did ));
1966 ibuf += sizeof( did );
1967 if (( dir = dirlookup( vol, did )) == NULL ) {
1968 return( AFPERR_PARAM );
1971 /* dir_remove -- deletedid */
1977 /* did creation gets done automatically */
1978 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1981 int ibuflen, *rbuflen;
1984 struct dir *dir, *parentdir;
1993 memcpy(&vid, ibuf, sizeof(vid));
1994 ibuf += sizeof( vid );
1996 if (( vol = getvolbyvid( vid )) == NULL ) {
1997 return( AFPERR_PARAM );
2000 memcpy(&did, ibuf, sizeof(did));
2001 ibuf += sizeof(did);
2003 if (( parentdir = dirlookup( vol, did )) == NULL ) {
2004 return( AFPERR_NOOBJ );
2007 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2010 return( AFPERR_ACCESS );
2012 return( AFPERR_NOOBJ );
2016 /* see if we already have the directory. */
2017 upath = mtoupath(vol, path);
2018 if ( stat( upath, &st ) < 0 ) {
2019 return( AFPERR_NOOBJ );
2022 dir = parentdir->d_child;
2024 if (strdiacasecmp(dir->d_name, path) == 0) {
2025 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2026 *rbuflen = sizeof(dir->d_did);
2029 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
2032 /* we don't already have a did. add one in. */
2033 if ((dir = adddir(vol, parentdir, path, strlen(path),
2034 upath, strlen(upath), &st)) == NULL) {
2038 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2039 *rbuflen = sizeof(dir->d_did);