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 accessmode( upath, &ma, dir );
949 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
950 afsmode( upath, &ma, dir );
952 *data++ = ma.ma_user;
953 *data++ = ma.ma_world;
954 *data++ = ma.ma_group;
955 *data++ = ma.ma_owner;
958 /* Client has requested the ProDOS information block.
959 Just pass back the same basic block for all
960 directories. <shirsch@ibm.net> */
961 case DIRPBIT_PDINFO : /* ProDOS Info Block */
964 ashort = htons( 0x0200 );
965 memcpy( data, &ashort, sizeof( ashort ));
966 data += sizeof( ashort );
967 memset( data, 0, sizeof( ashort ));
968 data += sizeof( ashort );
973 ad_close( &ad, ADFLAGS_HF );
975 return( AFPERR_BITMAP );
981 ashort = htons( data - buf );
982 memcpy( nameoff, &ashort, sizeof( ashort ));
984 if ((aint = strlen( dir->d_name )) > MACFILELEN)
988 memcpy( data, dir->d_name, aint );
992 ad_close( &ad, ADFLAGS_HF );
994 *buflen = data - buf;
998 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1001 int ibuflen, *rbuflen;
1006 u_int16_t vid, bitmap;
1012 memcpy( &vid, ibuf, sizeof( vid ));
1013 ibuf += sizeof( vid );
1015 if (( vol = getvolbyvid( vid )) == NULL ) {
1016 return( AFPERR_PARAM );
1019 if (vol->v_flags & AFPVOL_RO)
1020 return AFPERR_VLOCK;
1022 memcpy( &did, ibuf, sizeof( did ));
1023 ibuf += sizeof( int );
1025 if (( dir = dirsearch( vol, did )) == NULL ) {
1026 return( AFPERR_NOOBJ );
1029 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1030 bitmap = ntohs( bitmap );
1031 ibuf += sizeof( bitmap );
1033 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1034 return( AFPERR_NOOBJ );
1038 * If ibuf is odd, make it even.
1040 if ((u_long)ibuf & 1 ) {
1044 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1045 setvoltime(obj, vol );
1050 int setdirparams(vol, path, bitmap, buf )
1051 const struct vol *vol;
1059 int bit = 0, aint, isad = 1;
1060 u_int16_t ashort, bshort;
1063 upath = mtoupath(vol, path);
1064 memset(&ad, 0, sizeof(ad));
1065 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1066 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1068 * Check to see what we're trying to set. If it's anything
1069 * but ACCESS, UID, or GID, give an error. If it's any of those
1070 * three, we don't need the ad to be open, so just continue.
1072 * note: we also don't need to worry about mdate. also, be quiet
1073 * if we're using the noadouble option.
1075 if (!vol_noadouble(vol) && (bitmap &
1076 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1077 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO))))
1078 return AFPERR_ACCESS;
1083 * Check to see if a create was necessary. If it was, we'll want
1084 * to set our name, etc.
1086 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1087 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1088 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1089 ad_getentrylen( &ad, ADEID_NAME ));
1093 while ( bitmap != 0 ) {
1094 while (( bitmap & 1 ) == 0 ) {
1102 memcpy( &ashort, buf, sizeof( ashort ));
1103 ad_getattr(&ad, &bshort);
1104 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1105 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1109 ad_setattr(&ad, bshort);
1111 buf += sizeof( ashort );
1114 case DIRPBIT_CDATE :
1116 memcpy(&aint, buf, sizeof(aint));
1117 ad_setdate(&ad, AD_DATE_CREATE, aint);
1119 buf += sizeof( aint );
1122 case DIRPBIT_MDATE :
1123 memcpy(&aint, buf, sizeof(aint));
1125 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1126 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1128 buf += sizeof( aint );
1131 case DIRPBIT_BDATE :
1133 memcpy(&aint, buf, sizeof(aint));
1134 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1136 buf += sizeof( aint );
1139 case DIRPBIT_FINFO :
1141 * Alright, we admit it, this is *really* sick!
1142 * The 4 bytes that we don't copy, when we're dealing
1143 * with the root of a volume, are the directory's
1144 * location information. This eliminates that annoying
1145 * behavior one sees when mounting above another mount
1149 if ( curdir->d_did == DIRDID_ROOT ) {
1150 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1151 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1153 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1159 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1160 memcpy( &aint, buf, sizeof(aint));
1161 buf += sizeof( aint );
1162 if ( (curdir->d_did == DIRDID_ROOT) &&
1163 (setdeskowner( aint, -1 ) < 0)) {
1167 err = AFPERR_ACCESS;
1168 goto setdirparam_done;
1172 goto setdirparam_done;
1175 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1178 goto setdirparam_done;
1183 if ( setdirowner( aint, -1, vol_noadouble(vol) ) < 0 ) {
1187 err = AFPERR_ACCESS;
1188 goto setdirparam_done;
1192 goto setdirparam_done;
1195 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1202 memcpy( &aint, buf, sizeof( aint ));
1203 buf += sizeof( aint );
1204 if (curdir->d_did == DIRDID_ROOT)
1205 setdeskowner( -1, aint );
1207 #if 0 /* don't error if we can't set the desktop owner. */
1211 err = AFPERR_ACCESS;
1212 goto setdirparam_done;
1216 goto setdirparam_done;
1219 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1222 goto setdirparam_done;
1228 if ( setdirowner( -1, aint, vol_noadouble(vol) ) < 0 ) {
1232 err = AFPERR_ACCESS;
1233 goto setdirparam_done;
1237 goto setdirparam_done;
1240 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1246 case DIRPBIT_ACCESS :
1247 ma.ma_user = *buf++;
1248 ma.ma_world = *buf++;
1249 ma.ma_group = *buf++;
1250 ma.ma_owner = *buf++;
1252 if (curdir->d_did == DIRDID_ROOT)
1253 setdeskmode(mtoumode( &ma ));
1254 #if 0 /* don't error if we can't set the desktop mode */
1258 err = AFPERR_ACCESS;
1259 goto setdirparam_done;
1262 goto setdirparam_done;
1264 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1267 goto setdirparam_done;
1272 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol)) < 0 ) {
1276 err = AFPERR_ACCESS;
1277 goto setdirparam_done;
1280 goto setdirparam_done;
1282 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1284 goto setdirparam_done;
1289 /* Ignore what the client thinks we should do to the
1290 ProDOS information block. Skip over the data and
1291 report nothing amiss. <shirsch@ibm.net> */
1292 case DIRPBIT_PDINFO :
1297 err = AFPERR_BITMAP;
1298 goto setdirparam_done;
1309 ad_flush( &ad, ADFLAGS_HF );
1310 ad_close( &ad, ADFLAGS_HF );
1316 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1319 int ibuflen, *rbuflen;
1332 memcpy( &vid, ibuf, sizeof( vid ));
1333 ibuf += sizeof( vid );
1334 if (( vol = getvolbyvid( vid )) == NULL ) {
1335 return( AFPERR_PARAM );
1338 if (vol->v_flags & AFPVOL_RO)
1339 return AFPERR_VLOCK;
1341 memcpy( &did, ibuf, sizeof( did ));
1342 ibuf += sizeof( did );
1343 if (( dir = dirsearch( vol, did )) == NULL ) {
1344 return( AFPERR_NOOBJ );
1347 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1348 return( AFPERR_NOOBJ );
1351 /* check for illegal bits */
1352 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1353 strpbrk(path, MSWINDOWS_BADCHARS))
1354 return AFPERR_PARAM;
1356 upath = mtoupath(vol, path);
1358 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1359 return AFPERR_PARAM;
1361 if (!validupath(vol, upath))
1362 return AFPERR_EXIST;
1364 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1367 return( AFPERR_NOOBJ );
1369 return( AFPERR_VLOCK );
1371 return( AFPERR_ACCESS );
1373 return( AFPERR_EXIST );
1376 return( AFPERR_DFULL );
1378 return( AFPERR_PARAM );
1382 if (stat(upath, &st) < 0)
1385 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1386 strlen(upath), &st)) == NULL)
1389 if ( movecwd( vol, dir ) < 0 ) {
1390 return( AFPERR_PARAM );
1393 memset(&ad, 0, sizeof(ad));
1394 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1395 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1396 if (vol_noadouble(vol))
1397 goto createdir_done;
1398 return( AFPERR_ACCESS );
1401 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1402 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1403 ad_getentrylen( &ad, ADEID_NAME ));
1404 ad_flush( &ad, ADFLAGS_HF );
1405 ad_close( &ad, ADFLAGS_HF );
1408 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1409 *rbuflen = sizeof( u_int32_t );
1410 setvoltime(obj, vol );
1415 int renamedir(src, dst, dir, newparent, newname, noadouble)
1416 char *src, *dst, *newname;
1417 struct dir *dir, *newparent;
1418 const int noadouble;
1425 /* existence check moved to afp_moveandrename */
1426 if ( rename( src, dst ) < 0 ) {
1429 return( AFPERR_NOOBJ );
1431 return( AFPERR_ACCESS );
1433 return AFPERR_VLOCK;
1435 /* tried to move directory into a subdirectory of itself */
1436 return AFPERR_CANTMOVE;
1438 /* this needs to copy and delete. bleah. that means we have
1439 * to deal with entire directory hierarchies. */
1440 if ((err = copydir(src, dst, noadouble)) < 0) {
1444 if ((err = deletedir(src)) < 0)
1448 return( AFPERR_PARAM );
1452 memset(&ad, 0, sizeof(ad));
1453 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1457 len = strlen(newname);
1458 goto renamedir_done;
1460 return( AFPERR_NOOBJ );
1462 return( AFPERR_ACCESS );
1464 return( AFPERR_PARAM );
1467 len = strlen( newname );
1468 ad_setentrylen( &ad, ADEID_NAME, len );
1469 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1470 ad_flush( &ad, ADFLAGS_HF );
1471 ad_close( &ad, ADFLAGS_HF );
1474 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1475 syslog( LOG_ERR, "renamedir: realloc: %m" );
1479 strcpy( dir->d_name, newname );
1481 if (( parent = dir->d_parent ) == NULL ) {
1484 if ( parent == newparent ) {
1488 /* detach from old parent and add to new one. */
1489 dirchildremove(parent, dir);
1490 dir->d_parent = newparent;
1491 dirchildadd(newparent, dir);
1495 #define DOT_APPLEDOUBLE_LEN 13
1496 /* delete an empty directory */
1497 int deletecurdir( vol, path, pathlen )
1498 const struct vol *vol;
1507 if ( curdir->d_parent == NULL ) {
1508 return( AFPERR_ACCESS );
1511 if ( curdir->d_child != NULL ) {
1512 return( AFPERR_DIRNEMPT );
1517 /* delete stray .AppleDouble files. this happens to get .Parent files
1519 if ((dp = opendir(".AppleDouble"))) {
1520 strcpy(path, ".AppleDouble/");
1521 while ((de = readdir(dp))) {
1522 /* skip this and previous directory */
1523 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1526 /* bail if the file exists in the current directory.
1527 * note: this will not fail with dangling symlinks */
1528 if (stat(de->d_name, &st) == 0) {
1530 return AFPERR_DIRNEMPT;
1533 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1534 if (unlink(path) < 0) {
1539 return( AFPERR_ACCESS );
1541 return AFPERR_VLOCK;
1545 return( AFPERR_PARAM );
1552 if ( rmdir( ".AppleDouble" ) < 0 ) {
1557 return( AFPERR_DIRNEMPT );
1559 return AFPERR_VLOCK;
1562 return( AFPERR_ACCESS );
1564 return( AFPERR_PARAM );
1568 /* now get rid of dangling symlinks */
1569 if ((dp = opendir("."))) {
1570 while ((de = readdir(dp))) {
1571 /* skip this and previous directory */
1572 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1575 /* bail if it's not a symlink */
1576 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode))
1577 return AFPERR_DIRNEMPT;
1579 if (unlink(de->d_name) < 0) {
1583 return( AFPERR_ACCESS );
1585 return AFPERR_VLOCK;
1589 return( AFPERR_PARAM );
1596 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1597 return( AFPERR_NOOBJ );
1600 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1603 return( AFPERR_NOOBJ );
1605 return( AFPERR_DIRNEMPT );
1608 return( AFPERR_ACCESS );
1610 return AFPERR_VLOCK;
1612 return( AFPERR_PARAM );
1616 dirchildremove(curdir, fdir);
1617 #if AD_VERSION > AD_VERSION1
1618 cnid_delete(vol->v_db, fdir->d_did);
1620 dir_remove( vol, fdir );
1625 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1628 int ibuflen, *rbuflen;
1637 sfunc = (unsigned char) *ibuf++;
1638 memcpy( &id, ibuf, sizeof( id ));
1643 if (( pw = getpwuid( id )) == NULL ) {
1645 return( AFPERR_NOITEM );
1651 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1653 return( AFPERR_NOITEM );
1660 return( AFPERR_PARAM );
1663 len = strlen( name );
1672 memcpy( rbuf, name, len );
1678 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1681 int ibuflen, *rbuflen;
1689 sfunc = (unsigned char) *ibuf++;
1690 len = (unsigned char) *ibuf++;
1696 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1698 return( AFPERR_NOITEM );
1704 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1706 return( AFPERR_NOITEM );
1712 return( AFPERR_PARAM );
1718 memcpy( rbuf, &id, sizeof( id ));
1719 *rbuflen = sizeof( id );
1723 /* variable DID support */
1724 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1727 int ibuflen, *rbuflen;
1738 /* do nothing as dids are static for the life of the process. */
1742 memcpy(&vid, ibuf, sizeof( vid ));
1743 ibuf += sizeof( vid );
1744 if (( vol = getvolbyvid( vid )) == NULL ) {
1745 return( AFPERR_PARAM );
1748 memcpy( &did, ibuf, sizeof( did ));
1749 ibuf += sizeof( did );
1750 if (( dir = dirsearch( vol, did )) == NULL ) {
1751 return( AFPERR_PARAM );
1754 /* dir_remove -- deletedid */
1760 /* did creation gets done automatically */
1761 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1764 int ibuflen, *rbuflen;
1767 struct dir *dir, *parentdir;
1776 memcpy(&vid, ibuf, sizeof(vid));
1777 ibuf += sizeof( vid );
1779 if (( vol = getvolbyvid( vid )) == NULL ) {
1780 return( AFPERR_PARAM );
1783 memcpy(&did, ibuf, sizeof(did));
1784 ibuf += sizeof(did);
1786 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1787 return( AFPERR_NOOBJ );
1790 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1791 return( AFPERR_NOOBJ );
1794 /* see if we already have the directory. */
1795 upath = mtoupath(vol, path);
1796 if ( stat( upath, &st ) < 0 ) {
1797 return( AFPERR_NOOBJ );
1800 dir = parentdir->d_child;
1802 if (strdiacasecmp(dir->d_name, path) == 0) {
1803 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1804 *rbuflen = sizeof(dir->d_did);
1807 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1810 /* we don't already have a did. add one in. */
1811 if ((dir = adddir(vol, parentdir, path, strlen(path),
1812 upath, strlen(upath), &st)) == NULL)
1815 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1816 *rbuflen = sizeof(dir->d_did);