2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
5 * 19 jan 2000 implemented red-black trees for directory lookups
13 #include <sys/syslog.h>
14 #include <sys/types.h>
18 #include <sys/param.h>
19 #include <netatalk/endian.h>
20 #include <atalk/adouble.h>
21 #include <atalk/afp.h>
22 #include <atalk/util.h>
23 #include <atalk/cnid.h>
33 #include "directory.h"
43 #define SENTINEL (&sentinel)
44 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
45 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
46 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
47 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
49 /* (from IM: Toolbox Essentials)
50 * dirFinderInfo (DInfo) fields:
52 * frRect 8 folder's window rectangle
54 * frLocation 4 folder's location in window
55 * frView 2 folder's view (default == closedView (256))
57 * extended dirFinderInfo (DXInfo) fields:
58 * frScroll 4 scroll position
59 * frOpenChain: 4 directory ID chain of open folders
60 * frScript: 1 script flag and code
61 * frXFlags: 1 reserved
62 * frComment: 2 comment ID
63 * frPutAway: 4 home directory ID
67 * redid did assignment for directories. now we use red-black trees.
72 const struct vol *vol;
82 if ( did == DIRDID_ROOT_PARENT ) {
84 rootpar.d_did = DIRDID_ROOT_PARENT;
85 rootpar.d_child = vol->v_dir;
90 while ( dir != SENTINEL ) {
91 if (dir->d_did == did)
92 return dir->d_name ? dir : NULL;
93 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
99 /* rotate the tree to the left */
100 static void dir_leftrotate(vol, dir)
104 struct dir *right = dir->d_right;
106 /* whee. move the right's left tree into dir's right tree */
107 dir->d_right = right->d_left;
108 if (right->d_left != SENTINEL)
109 right->d_left->d_back = dir;
111 if (right != SENTINEL) {
112 right->d_back = dir->d_back;
116 if (!dir->d_back) /* no parent. move the right tree to the top. */
118 else if (dir == dir->d_back->d_left) /* we were on the left */
119 dir->d_back->d_left = right;
121 dir->d_back->d_right = right; /* we were on the right */
123 /* re-insert dir on the left tree */
130 /* rotate the tree to the right */
131 static void dir_rightrotate(vol, dir)
135 struct dir *left = dir->d_left;
137 /* whee. move the left's right tree into dir's left tree */
138 dir->d_left = left->d_right;
139 if (left->d_right != SENTINEL)
140 left->d_right->d_back = dir;
142 if (left != SENTINEL) {
143 left->d_back = dir->d_back;
147 if (!dir->d_back) /* no parent. move the left tree to the top. */
149 else if (dir == dir->d_back->d_right) /* we were on the right */
150 dir->d_back->d_right = left;
152 dir->d_back->d_left = left; /* we were on the left */
154 /* re-insert dir on the right tree */
159 /* recolor after a removal */
160 static struct dir *dir_rmrecolor(vol, dir)
166 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
167 /* are we on the left tree? */
168 if (dir == dir->d_back->d_left) {
169 leaf = dir->d_back->d_right; /* get right side */
170 if (leaf->d_color == DIRTREE_COLOR_RED) {
171 /* we're red. we need to change to black. */
172 leaf->d_color = DIRTREE_COLOR_BLACK;
173 dir->d_back->d_color = DIRTREE_COLOR_RED;
174 dir_leftrotate(vol, dir->d_back);
175 leaf = dir->d_back->d_right;
178 /* right leaf has black end nodes */
179 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
180 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
181 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
182 dir = dir->d_back; /* ascend */
184 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
185 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
186 leaf->d_color = DIRTREE_COLOR_RED;
187 dir_rightrotate(vol, leaf);
188 leaf = dir->d_back->d_right;
190 leaf->d_color = dir->d_back->d_color;
191 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
192 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
193 dir_leftrotate(vol, dir->d_back);
196 } else { /* right tree */
197 leaf = dir->d_back->d_left; /* left tree */
198 if (leaf->d_color == DIRTREE_COLOR_RED) {
199 leaf->d_color = DIRTREE_COLOR_BLACK;
200 dir->d_back->d_color = DIRTREE_COLOR_RED;
201 dir_rightrotate(vol, dir->d_back);
202 leaf = dir->d_back->d_left;
205 /* left leaf has black end nodes */
206 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
207 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
208 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
209 dir = dir->d_back; /* ascend */
211 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
212 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
213 leaf->d_color = DIRTREE_COLOR_RED;
214 dir_leftrotate(vol, leaf);
215 leaf = dir->d_back->d_left;
217 leaf->d_color = dir->d_back->d_color;
218 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
219 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
220 dir_rightrotate(vol, dir->d_back);
225 dir->d_color = DIRTREE_COLOR_BLACK;
229 /* remove the node from the tree. this is just like insertion, but
230 * different. actually, it has to worry about a bunch of things that
231 * insertion doesn't care about. */
232 static void dir_remove( vol, dir )
237 struct ofork *of, *last;
238 struct dir *node, *leaf;
241 if (!dir || (dir == SENTINEL))
244 /* i'm not sure if it really helps to delete stuff. */
250 /* go searching for a node with at most one child */
251 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
255 while (node->d_left != SENTINEL)
260 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
263 leaf->d_back = node->d_back;
266 } else if (node == node->d_back->d_left) { /* left tree */
267 node->d_back->d_left = leaf;
269 node->d_back->d_right = leaf;
272 /* we want to free node, but we also want to free the data in dir.
273 * currently, that's d_name and the directory traversal bits.
274 * we just copy the necessary bits and then fix up all the
275 * various pointers to the directory. needless to say, there are
276 * a bunch of places that store the directory struct. */
278 struct dir save, *tmp;
280 memcpy(&save, dir, sizeof(save));
281 memcpy(dir, node, sizeof(struct dir));
283 /* restore the red-black bits */
284 dir->d_left = save.d_left;
285 dir->d_right = save.d_right;
286 dir->d_back = save.d_back;
287 dir->d_color = save.d_color;
289 if (node == vol->v_dir) {/* we may need to fix up this pointer */
291 rootpar.d_child = vol->v_dir;
293 /* if we aren't the root directory, we have parents and
294 * siblings to worry about */
295 if (dir->d_parent->d_child == node)
296 dir->d_parent->d_child = dir;
297 dir->d_next->d_prev = dir;
298 dir->d_prev->d_next = dir;
301 /* fix up children. */
305 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
308 if (node == curdir) /* another pointer to fixup */
311 /* we also need to fix up oforks. bleah */
312 if ((of = dir->d_ofork)) {
313 last = of->of_d_prev;
316 of = (last == of) ? NULL : of->of_d_next;
320 /* set the node's d_name */
321 node->d_name = save.d_name;
324 if (node->d_color == DIRTREE_COLOR_BLACK)
325 dir_rmrecolor(vol, leaf);
332 static struct dir *dir_insert(vol, dir)
333 const struct vol *vol;
339 while (pdir->d_did != dir->d_did ) {
340 if ( pdir->d_did > dir->d_did ) {
341 if ( pdir->d_left == SENTINEL ) {
348 if ( pdir->d_right == SENTINEL ) {
353 pdir = pdir->d_right;
361 * attempt to extend the current dir. tree to include path
362 * as a side-effect, movecwd to that point and return the new dir
366 extenddir( vol, dir, path )
374 p = mtoupath(vol, path );
375 if ( stat( p, &st ) != 0 ) {
378 if (!S_ISDIR(st.st_mode)) {
382 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
387 if ( movecwd( vol, dir ) < 0 ) {
394 static int deletedir(char *dir)
396 char path[MAXPATHLEN + 1];
402 if ((len = strlen(dir)) > sizeof(path))
406 if ((dp = opendir(dir)) == NULL)
412 while ((de = readdir(dp))) {
413 /* skip this and previous directory */
414 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
417 strncpy(path + len, de->d_name, sizeof(path) - len);
418 if (stat(path, &st) == 0) {
419 if (S_ISDIR(st.st_mode)) {
420 if ((err = deletedir(path)) < 0) {
424 } else if (unlink(path) < 0) {
427 continue; /* somebody went and deleted it behind our backs. */
443 /* okay. the directory is empty. delete it. note: we already got rid
445 if (rmdir(dir) < 0) {
449 case ENOTEMPTY : /* should never happen */
450 return( AFPERR_DIRNEMPT );
453 return( AFPERR_ACCESS );
457 return( AFPERR_PARAM );
463 /* do a recursive copy. */
464 static int copydir(char *src, char *dst, int noadouble)
466 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
473 /* doesn't exist or the path is too long. */
474 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
475 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
476 ((dp = opendir(src)) == NULL))
479 /* try to create the destination directory */
480 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
484 return( AFPERR_NOOBJ );
486 return( AFPERR_VLOCK );
489 return( AFPERR_ACCESS );
491 return( AFPERR_EXIST );
494 return( AFPERR_DFULL );
496 return( AFPERR_PARAM );
500 /* set things up to copy */
508 while ((de = readdir(dp))) {
509 /* skip this and previous directory */
510 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
513 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
514 if (stat(spath, &st) == 0) {
515 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
517 if (S_ISDIR(st.st_mode)) {
518 if ((err = copydir(spath, dpath, noadouble)) < 0)
520 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
524 /* keep the same time stamp. */
525 ut.actime = ut.modtime = st.st_mtime;
531 /* keep the same time stamp. */
532 if (stat(src, &st) == 0) {
533 ut.actime = ut.modtime = st.st_mtime;
543 /* --- public functions follow --- */
545 /* NOTE: we start off with at least one node (the root directory). */
546 struct dir *dirinsert( vol, dir )
552 if ((node = dir_insert(vol, dir)))
555 /* recolor the tree. the current node is red. */
556 dir->d_color = DIRTREE_COLOR_RED;
558 /* parent of this node has to be black. if the parent node
559 * is red, then we have a grandparent. */
560 while ((dir != vol->v_root) &&
561 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
562 /* are we on the left tree? */
563 if (dir->d_back == dir->d_back->d_back->d_left) {
564 node = dir->d_back->d_back->d_right; /* get the right node */
565 if (node->d_color == DIRTREE_COLOR_RED) {
566 /* we're red. we need to change to black. */
567 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
568 node->d_color = DIRTREE_COLOR_BLACK;
569 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
570 dir = dir->d_back->d_back; /* finished. go up. */
572 if (dir == dir->d_back->d_right) {
574 dir_leftrotate(vol, dir);
576 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
577 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
578 dir_rightrotate(vol, dir->d_back->d_back);
581 node = dir->d_back->d_back->d_left;
582 if (node->d_color == DIRTREE_COLOR_RED) {
583 /* we're red. we need to change to black. */
584 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
585 node->d_color = DIRTREE_COLOR_BLACK;
586 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
587 dir = dir->d_back->d_back; /* finished. ascend */
589 if (dir == dir->d_back->d_left) {
591 dir_rightrotate(vol, dir);
593 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
594 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
595 dir_leftrotate(vol, dir->d_back->d_back);
600 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
604 /* free everything down. we don't bother to recolor as this is only
605 * called to free the entire tree */
609 if (!dir || (dir == SENTINEL))
612 if ( dir->d_left != SENTINEL ) {
613 dirfree( dir->d_left );
615 if ( dir->d_right != SENTINEL ) {
616 dirfree( dir->d_right );
619 if (dir != SENTINEL) {
626 struct dir *dirnew(const int len)
630 dir = (struct dir *) calloc(1, sizeof( struct dir ));
634 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
639 dir->d_left = dir->d_right = SENTINEL;
640 dir->d_next = dir->d_prev = dir;
645 /* XXX: this needs to be changed to handle path types */
647 cname( vol, dir, cpath )
648 const struct vol *vol;
653 static char path[ MAXPATHLEN + 1];
659 if ( *data++ != 2 ) { /* path type */
662 len = (unsigned char) *data++;
668 if ( !extend && movecwd( vol, dir ) < 0 ) {
674 if ( *data == '\0' ) {
679 while ( *data == '\0' && len > 0 ) {
680 if ( dir->d_parent == NULL ) {
688 /* would this be faster with strlen + strncpy? */
690 while ( *data != '\0' && len > 0 ) {
695 /* short cut bits by chopping off a trailing \0. this also
696 makes the traversal happy w/ filenames at the end of the
703 * Dung Nguyen <ntd@adb.fr>
705 * AFPD cannot handle paths with "::" if the "::" notation is
706 * not at the beginning of the path. The following path will not
707 * be interpreted correctly:
709 * :a:b:::c: (directory c at the same level as directory a) */
717 if ( p != path ) { /* we got something */
721 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
724 cdir = (cdir == dir->d_child->d_prev) ? NULL :
727 if ( cdir == NULL ) {
729 if ( movecwd( vol, dir ) < 0 ) {
732 cdir = extenddir( vol, dir, path );
736 cdir = extenddir( vol, dir, path );
739 if ( cdir == NULL ) {
753 * Move curdir to dir, with a possible chdir()
755 int movecwd( vol, dir)
756 const struct vol *vol;
759 char path[MAXPATHLEN + 1];
764 if ( dir == curdir ) {
767 if ( dir->d_did == DIRDID_ROOT_PARENT) {
771 p = path + sizeof(path) - 1;
774 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
776 u = mtoupath(vol, d->d_name );
783 n = strlen( vol->v_path );
785 strncpy( p, vol->v_path, n );
787 if ( chdir( p ) < 0 ) {
794 int getdirparams(vol, bitmap, upath, dir, st, buf, buflen )
795 const struct vol *vol;
805 char *data, *nameoff = NULL;
808 int bit = 0, isad = 1;
812 memset(&ad, 0, sizeof(ad));
813 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
814 DIRBITS | 0777, &ad) < 0 ) {
819 while ( bitmap != 0 ) {
820 while (( bitmap & 1 ) == 0 ) {
828 ad_getattr(&ad, &ashort);
829 } else if (*upath == '.' && strcmp(upath, ".") &&
830 strcmp(upath, "..")) {
831 ashort = htons(ATTRBIT_INVISIBLE);
834 memcpy( data, &ashort, sizeof( ashort ));
835 data += sizeof( ashort );
839 if ( dir->d_did == DIRDID_ROOT) {
840 aint = DIRDID_ROOT_PARENT;
841 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
844 aint = dir->d_parent->d_did;
846 memcpy( data, &aint, sizeof( aint ));
847 data += sizeof( aint );
851 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
852 aint = AD_DATE_FROM_UNIX(st->st_mtime);
853 memcpy( data, &aint, sizeof( aint ));
854 data += sizeof( aint );
858 aint = AD_DATE_FROM_UNIX(st->st_mtime);
859 memcpy( data, &aint, sizeof( aint ));
860 data += sizeof( aint );
864 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
865 aint = AD_DATE_START;
866 memcpy( data, &aint, sizeof( aint ));
867 data += sizeof( aint );
872 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
873 } else { /* no appledouble */
874 memset( data, 0, 32 );
875 /* set default view -- this also gets done in ad_open() */
876 ashort = htons(FINDERINFO_CLOSEDVIEW);
877 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
879 /* dot files are by default invisible */
880 if (*upath == '.' && strcmp(upath, ".") &&
881 strcmp(upath, "..")) {
882 ashort = htons(FINDERINFO_INVISIBLE);
883 memcpy(data + FINDERINFO_FRFLAGOFF,
884 &ashort, sizeof(ashort));
891 if (dir->d_name) /* root of parent can have a null name */
894 memset(data, 0, sizeof(u_int16_t));
895 data += sizeof( u_int16_t );
899 memset(data, 0, sizeof(u_int16_t));
900 data += sizeof( u_int16_t );
904 memcpy( data, &dir->d_did, sizeof( aint ));
905 data += sizeof( aint );
908 case DIRPBIT_OFFCNT :
910 /* this needs to handle current directory access rights */
911 if ((dp = opendir( upath ))) {
912 while (( de = readdir( dp )) != NULL ) {
913 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
916 if (!validupath(vol, de->d_name))
919 /* now check against too long a filename */
920 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
927 ashort = htons( ashort );
928 memcpy( data, &ashort, sizeof( ashort ));
929 data += sizeof( ashort );
934 memcpy( data, &aint, sizeof( aint ));
935 data += sizeof( aint );
940 memcpy( data, &aint, sizeof( aint ));
941 data += sizeof( aint );
944 case DIRPBIT_ACCESS :
947 afsmode( upath, &ma, dir );
949 *data++ = ma.ma_user;
950 *data++ = ma.ma_world;
951 *data++ = ma.ma_group;
952 *data++ = ma.ma_owner;
955 /* Client has requested the ProDOS information block.
956 Just pass back the same basic block for all
957 directories. <shirsch@ibm.net> */
958 case DIRPBIT_PDINFO : /* ProDOS Info Block */
961 ashort = htons( 0x0200 );
962 memcpy( data, &ashort, sizeof( ashort ));
963 data += sizeof( ashort );
964 memset( data, 0, sizeof( ashort ));
965 data += sizeof( ashort );
970 ad_close( &ad, ADFLAGS_HF );
972 return( AFPERR_BITMAP );
978 ashort = htons( data - buf );
979 memcpy( nameoff, &ashort, sizeof( ashort ));
981 if ((aint = strlen( dir->d_name )) > MACFILELEN)
985 memcpy( data, dir->d_name, aint );
989 ad_close( &ad, ADFLAGS_HF );
991 *buflen = data - buf;
995 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
998 int ibuflen, *rbuflen;
1003 u_int16_t vid, bitmap;
1009 memcpy( &vid, ibuf, sizeof( vid ));
1010 ibuf += sizeof( vid );
1012 if (( vol = getvolbyvid( vid )) == NULL ) {
1013 return( AFPERR_PARAM );
1016 if (vol->v_flags & AFPVOL_RO)
1017 return AFPERR_VLOCK;
1019 memcpy( &did, ibuf, sizeof( did ));
1020 ibuf += sizeof( int );
1022 if (( dir = dirsearch( vol, did )) == NULL ) {
1023 return( AFPERR_NOOBJ );
1026 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1027 bitmap = ntohs( bitmap );
1028 ibuf += sizeof( bitmap );
1030 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1031 return( AFPERR_NOOBJ );
1035 * If ibuf is odd, make it even.
1037 if ((u_long)ibuf & 1 ) {
1041 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1042 setvoltime(obj, vol );
1047 int setdirparams(vol, path, bitmap, buf )
1048 const struct vol *vol;
1056 int bit = 0, aint, isad = 1;
1057 u_int16_t ashort, bshort;
1060 upath = mtoupath(vol, path);
1061 memset(&ad, 0, sizeof(ad));
1062 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1063 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1065 * Check to see what we're trying to set. If it's anything
1066 * but ACCESS, UID, or GID, give an error. If it's any of those
1067 * three, we don't need the ad to be open, so just continue.
1069 * note: we also don't need to worry about mdate. also, be quiet
1070 * if we're using the noadouble option.
1072 if (!vol_noadouble(vol) && (bitmap &
1073 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1074 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO))))
1075 return AFPERR_ACCESS;
1080 * Check to see if a create was necessary. If it was, we'll want
1081 * to set our name, etc.
1083 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1084 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1085 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1086 ad_getentrylen( &ad, ADEID_NAME ));
1090 while ( bitmap != 0 ) {
1091 while (( bitmap & 1 ) == 0 ) {
1099 memcpy( &ashort, buf, sizeof( ashort ));
1100 ad_getattr(&ad, &bshort);
1101 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1102 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1106 ad_setattr(&ad, bshort);
1108 buf += sizeof( ashort );
1111 case DIRPBIT_CDATE :
1113 memcpy(&aint, buf, sizeof(aint));
1114 ad_setdate(&ad, AD_DATE_CREATE, aint);
1116 buf += sizeof( aint );
1119 case DIRPBIT_MDATE :
1120 memcpy(&aint, buf, sizeof(aint));
1122 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1123 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1125 buf += sizeof( aint );
1128 case DIRPBIT_BDATE :
1130 memcpy(&aint, buf, sizeof(aint));
1131 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1133 buf += sizeof( aint );
1136 case DIRPBIT_FINFO :
1138 * Alright, we admit it, this is *really* sick!
1139 * The 4 bytes that we don't copy, when we're dealing
1140 * with the root of a volume, are the directory's
1141 * location information. This eliminates that annoying
1142 * behavior one sees when mounting above another mount
1146 if ( curdir->d_did == DIRDID_ROOT ) {
1147 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1148 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1150 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1156 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1157 buf += sizeof( int );
1161 memcpy( &aint, buf, sizeof( aint ));
1162 buf += sizeof( aint );
1163 if (curdir->d_did == DIRDID_ROOT)
1164 setdeskowner( -1, aint );
1166 #if 0 /* don't error if we can't set the desktop owner. */
1170 err = AFPERR_ACCESS;
1171 goto setdirparam_done;
1175 goto setdirparam_done;
1178 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1181 goto setdirparam_done;
1187 if ( setdirowner( -1, aint, vol_noadouble(vol) ) < 0 ) {
1191 err = AFPERR_ACCESS;
1192 goto setdirparam_done;
1196 goto setdirparam_done;
1199 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1205 case DIRPBIT_ACCESS :
1206 ma.ma_user = *buf++;
1207 ma.ma_world = *buf++;
1208 ma.ma_group = *buf++;
1209 ma.ma_owner = *buf++;
1211 if (curdir->d_did == DIRDID_ROOT)
1212 setdeskmode(mtoumode( &ma ));
1213 #if 0 /* don't error if we can't set the desktop mode */
1217 err = AFPERR_ACCESS;
1218 goto setdirparam_done;
1221 goto setdirparam_done;
1223 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1226 goto setdirparam_done;
1231 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol)) < 0 ) {
1235 err = AFPERR_ACCESS;
1236 goto setdirparam_done;
1239 goto setdirparam_done;
1241 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1243 goto setdirparam_done;
1248 /* Ignore what the client thinks we should do to the
1249 ProDOS information block. Skip over the data and
1250 report nothing amiss. <shirsch@ibm.net> */
1251 case DIRPBIT_PDINFO :
1256 err = AFPERR_BITMAP;
1257 goto setdirparam_done;
1268 ad_flush( &ad, ADFLAGS_HF );
1269 ad_close( &ad, ADFLAGS_HF );
1275 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1278 int ibuflen, *rbuflen;
1291 memcpy( &vid, ibuf, sizeof( vid ));
1292 ibuf += sizeof( vid );
1293 if (( vol = getvolbyvid( vid )) == NULL ) {
1294 return( AFPERR_PARAM );
1297 if (vol->v_flags & AFPVOL_RO)
1298 return AFPERR_VLOCK;
1300 memcpy( &did, ibuf, sizeof( did ));
1301 ibuf += sizeof( did );
1302 if (( dir = dirsearch( vol, did )) == NULL ) {
1303 return( AFPERR_NOOBJ );
1306 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1307 return( AFPERR_NOOBJ );
1310 /* check for illegal bits */
1311 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1312 strpbrk(path, MSWINDOWS_BADCHARS))
1313 return AFPERR_PARAM;
1315 upath = mtoupath(vol, path);
1317 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1318 return AFPERR_PARAM;
1320 if (!validupath(vol, upath))
1321 return AFPERR_EXIST;
1323 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1326 return( AFPERR_NOOBJ );
1328 return( AFPERR_VLOCK );
1330 return( AFPERR_ACCESS );
1332 return( AFPERR_EXIST );
1335 return( AFPERR_DFULL );
1337 return( AFPERR_PARAM );
1341 if (stat(upath, &st) < 0)
1344 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1345 strlen(upath), &st)) == NULL)
1348 if ( movecwd( vol, dir ) < 0 ) {
1349 return( AFPERR_PARAM );
1352 memset(&ad, 0, sizeof(ad));
1353 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1354 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1355 if (vol_noadouble(vol))
1356 goto createdir_done;
1357 return( AFPERR_ACCESS );
1360 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1361 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1362 ad_getentrylen( &ad, ADEID_NAME ));
1363 ad_flush( &ad, ADFLAGS_HF );
1364 ad_close( &ad, ADFLAGS_HF );
1367 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1368 *rbuflen = sizeof( u_int32_t );
1369 setvoltime(obj, vol );
1374 int renamedir(src, dst, dir, newparent, newname, noadouble)
1375 char *src, *dst, *newname;
1376 struct dir *dir, *newparent;
1377 const int noadouble;
1384 /* existence check moved to afp_moveandrename */
1385 if ( rename( src, dst ) < 0 ) {
1388 return( AFPERR_NOOBJ );
1390 return( AFPERR_ACCESS );
1392 return AFPERR_VLOCK;
1394 /* tried to move directory into a subdirectory of itself */
1395 return AFPERR_CANTMOVE;
1397 /* this needs to copy and delete. bleah. that means we have
1398 * to deal with entire directory hierarchies. */
1399 if ((err = copydir(src, dst, noadouble)) < 0) {
1403 if ((err = deletedir(src)) < 0)
1407 return( AFPERR_PARAM );
1411 memset(&ad, 0, sizeof(ad));
1412 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1416 len = strlen(newname);
1417 goto renamedir_done;
1419 return( AFPERR_NOOBJ );
1421 return( AFPERR_ACCESS );
1423 return( AFPERR_PARAM );
1426 len = strlen( newname );
1427 ad_setentrylen( &ad, ADEID_NAME, len );
1428 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1429 ad_flush( &ad, ADFLAGS_HF );
1430 ad_close( &ad, ADFLAGS_HF );
1433 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1434 syslog( LOG_ERR, "renamedir: realloc: %m" );
1438 strcpy( dir->d_name, newname );
1440 if (( parent = dir->d_parent ) == NULL ) {
1443 if ( parent == newparent ) {
1447 /* detach from old parent and add to new one. */
1448 dirchildremove(parent, dir);
1449 dir->d_parent = newparent;
1450 dirchildadd(newparent, dir);
1454 #define DOT_APPLEDOUBLE_LEN 13
1455 /* delete an empty directory */
1456 int deletecurdir( vol, path, pathlen )
1457 const struct vol *vol;
1466 if ( curdir->d_parent == NULL ) {
1467 return( AFPERR_ACCESS );
1470 if ( curdir->d_child != NULL ) {
1471 return( AFPERR_DIRNEMPT );
1476 /* delete stray .AppleDouble files. this happens to get .Parent files
1478 if ((dp = opendir(".AppleDouble"))) {
1479 strcpy(path, ".AppleDouble/");
1480 while ((de = readdir(dp))) {
1481 /* skip this and previous directory */
1482 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1485 /* bail if the file exists in the current directory.
1486 * note: this will not fail with dangling symlinks */
1487 if (stat(de->d_name, &st) == 0) {
1489 return AFPERR_DIRNEMPT;
1492 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1493 if (unlink(path) < 0) {
1498 return( AFPERR_ACCESS );
1500 return AFPERR_VLOCK;
1504 return( AFPERR_PARAM );
1511 if ( rmdir( ".AppleDouble" ) < 0 ) {
1516 return( AFPERR_DIRNEMPT );
1518 return AFPERR_VLOCK;
1521 return( AFPERR_ACCESS );
1523 return( AFPERR_PARAM );
1527 /* now get rid of dangling symlinks */
1528 if ((dp = opendir("."))) {
1529 while ((de = readdir(dp))) {
1530 /* skip this and previous directory */
1531 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1534 /* bail if it's not a symlink */
1535 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode))
1536 return AFPERR_DIRNEMPT;
1538 if (unlink(de->d_name) < 0) {
1542 return( AFPERR_ACCESS );
1544 return AFPERR_VLOCK;
1548 return( AFPERR_PARAM );
1555 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1556 return( AFPERR_NOOBJ );
1559 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1562 return( AFPERR_NOOBJ );
1564 return( AFPERR_DIRNEMPT );
1567 return( AFPERR_ACCESS );
1569 return AFPERR_VLOCK;
1571 return( AFPERR_PARAM );
1575 dirchildremove(curdir, fdir);
1576 #if AD_VERSION > AD_VERSION1
1577 cnid_delete(vol->v_db, fdir->d_did);
1579 dir_remove( vol, fdir );
1584 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1587 int ibuflen, *rbuflen;
1596 sfunc = (unsigned char) *ibuf++;
1597 memcpy( &id, ibuf, sizeof( id ));
1602 if (( pw = getpwuid( id )) == NULL ) {
1604 return( AFPERR_NOITEM );
1610 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1612 return( AFPERR_NOITEM );
1619 return( AFPERR_PARAM );
1622 len = strlen( name );
1631 memcpy( rbuf, name, len );
1637 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1640 int ibuflen, *rbuflen;
1648 sfunc = (unsigned char) *ibuf++;
1649 len = (unsigned char) *ibuf++;
1655 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1657 return( AFPERR_NOITEM );
1663 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1665 return( AFPERR_NOITEM );
1671 return( AFPERR_PARAM );
1677 memcpy( rbuf, &id, sizeof( id ));
1678 *rbuflen = sizeof( id );
1682 /* variable DID support */
1683 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1686 int ibuflen, *rbuflen;
1697 /* do nothing as dids are static for the life of the process. */
1701 memcpy(&vid, ibuf, sizeof( vid ));
1702 ibuf += sizeof( vid );
1703 if (( vol = getvolbyvid( vid )) == NULL ) {
1704 return( AFPERR_PARAM );
1707 memcpy( &did, ibuf, sizeof( did ));
1708 ibuf += sizeof( did );
1709 if (( dir = dirsearch( vol, did )) == NULL ) {
1710 return( AFPERR_PARAM );
1713 /* dir_remove -- deletedid */
1719 /* did creation gets done automatically */
1720 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1723 int ibuflen, *rbuflen;
1726 struct dir *dir, *parentdir;
1735 memcpy(&vid, ibuf, sizeof(vid));
1736 ibuf += sizeof( vid );
1738 if (( vol = getvolbyvid( vid )) == NULL ) {
1739 return( AFPERR_PARAM );
1742 memcpy(&did, ibuf, sizeof(did));
1743 ibuf += sizeof(did);
1745 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1746 return( AFPERR_NOOBJ );
1749 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1750 return( AFPERR_NOOBJ );
1753 /* see if we already have the directory. */
1754 upath = mtoupath(vol, path);
1755 if ( stat( upath, &st ) < 0 ) {
1756 return( AFPERR_NOOBJ );
1759 dir = parentdir->d_child;
1761 if (strdiacasecmp(dir->d_name, path) == 0) {
1762 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1763 *rbuflen = sizeof(dir->d_did);
1766 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1769 /* we don't already have a did. add one in. */
1770 if ((dir = adddir(vol, parentdir, path, strlen(path),
1771 upath, strlen(upath), &st)) == NULL)
1774 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1775 *rbuflen = sizeof(dir->d_did);