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
9 #include <sys/syslog.h>
10 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <netatalk/endian.h>
16 #include <atalk/adouble.h>
17 #include <atalk/afp.h>
18 #include <atalk/util.h>
19 #include <atalk/cnid.h>
29 #include "directory.h"
39 #define SENTINEL (&sentinel)
40 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
41 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
42 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
43 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
45 /* (from IM: Toolbox Essentials)
46 * dirFinderInfo (DInfo) fields:
48 * frRect 8 folder's window rectangle
50 * frLocation 4 folder's location in window
51 * frView 2 folder's view (default == closedView (256))
53 * extended dirFinderInfo (DXInfo) fields:
54 * frScroll 4 scroll position
55 * frOpenChain: 4 directory ID chain of open folders
56 * frScript: 1 script flag and code
57 * frXFlags: 1 reserved
58 * frComment: 2 comment ID
59 * frPutAway: 4 home directory ID
63 * redid did assignment for directories. now we use red-black trees.
68 const struct vol *vol;
78 if ( did == DIRDID_ROOT_PARENT ) {
80 rootpar.d_did = DIRDID_ROOT_PARENT;
81 rootpar.d_child = vol->v_dir;
86 while ( dir != SENTINEL ) {
87 if (dir->d_did == did)
88 return dir->d_name ? dir : NULL;
89 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
95 /* rotate the tree to the left */
96 static void dir_leftrotate(vol, dir)
100 struct dir *right = dir->d_right;
102 /* whee. move the right's left tree into dir's right tree */
103 dir->d_right = right->d_left;
104 if (right->d_left != SENTINEL)
105 right->d_left->d_back = dir;
107 if (right != SENTINEL) {
108 right->d_back = dir->d_back;
112 if (!dir->d_back) /* no parent. move the right tree to the top. */
114 else if (dir == dir->d_back->d_left) /* we were on the left */
115 dir->d_back->d_left = right;
117 dir->d_back->d_right = right; /* we were on the right */
119 /* re-insert dir on the left tree */
126 /* rotate the tree to the right */
127 static void dir_rightrotate(vol, dir)
131 struct dir *left = dir->d_left;
133 /* whee. move the left's right tree into dir's left tree */
134 dir->d_left = left->d_right;
135 if (left->d_right != SENTINEL)
136 left->d_right->d_back = dir;
138 if (left != SENTINEL) {
139 left->d_back = dir->d_back;
143 if (!dir->d_back) /* no parent. move the left tree to the top. */
145 else if (dir == dir->d_back->d_right) /* we were on the right */
146 dir->d_back->d_right = left;
148 dir->d_back->d_left = left; /* we were on the left */
150 /* re-insert dir on the right tree */
155 /* recolor after a removal */
156 static struct dir *dir_rmrecolor(vol, dir)
162 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
163 /* are we on the left tree? */
164 if (dir == dir->d_back->d_left) {
165 leaf = dir->d_back->d_right; /* get right side */
166 if (leaf->d_color == DIRTREE_COLOR_RED) {
167 /* we're red. we need to change to black. */
168 leaf->d_color = DIRTREE_COLOR_BLACK;
169 dir->d_back->d_color = DIRTREE_COLOR_RED;
170 dir_leftrotate(vol, dir->d_back);
171 leaf = dir->d_back->d_right;
174 /* right leaf has black end nodes */
175 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
176 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
177 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
178 dir = dir->d_back; /* ascend */
180 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
181 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
182 leaf->d_color = DIRTREE_COLOR_RED;
183 dir_rightrotate(vol, leaf);
184 leaf = dir->d_back->d_right;
186 leaf->d_color = dir->d_back->d_color;
187 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
188 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
189 dir_leftrotate(vol, dir->d_back);
192 } else { /* right tree */
193 leaf = dir->d_back->d_left; /* left tree */
194 if (leaf->d_color == DIRTREE_COLOR_RED) {
195 leaf->d_color = DIRTREE_COLOR_BLACK;
196 dir->d_back->d_color = DIRTREE_COLOR_RED;
197 dir_rightrotate(vol, dir->d_back);
198 leaf = dir->d_back->d_left;
201 /* left leaf has black end nodes */
202 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
203 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
204 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
205 dir = dir->d_back; /* ascend */
207 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
208 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
209 leaf->d_color = DIRTREE_COLOR_RED;
210 dir_leftrotate(vol, leaf);
211 leaf = dir->d_back->d_left;
213 leaf->d_color = dir->d_back->d_color;
214 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
215 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
216 dir_rightrotate(vol, dir->d_back);
221 dir->d_color = DIRTREE_COLOR_BLACK;
225 /* remove the node from the tree. this is just like insertion, but
226 * different. actually, it has to worry about a bunch of things that
227 * insertion doesn't care about. */
228 static void dir_remove( vol, dir )
233 struct ofork *of, *last;
234 struct dir *node, *leaf;
237 if (!dir || (dir == SENTINEL))
240 /* i'm not sure if it really helps to delete stuff. */
246 /* go searching for a node with at most one child */
247 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
251 while (node->d_left != SENTINEL)
256 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
259 leaf->d_back = node->d_back;
262 } else if (node == node->d_back->d_left) { /* left tree */
263 node->d_back->d_left = leaf;
265 node->d_back->d_right = leaf;
268 /* we want to free node, but we also want to free the data in dir.
269 * currently, that's d_name and the directory traversal bits.
270 * we just copy the necessary bits and then fix up all the
271 * various pointers to the directory. needless to say, there are
272 * a bunch of places that store the directory struct. */
274 struct dir save, *tmp;
276 memcpy(&save, dir, sizeof(save));
277 memcpy(dir, node, sizeof(struct dir));
279 /* restore the red-black bits */
280 dir->d_left = save.d_left;
281 dir->d_right = save.d_right;
282 dir->d_back = save.d_back;
283 dir->d_color = save.d_color;
285 if (node == vol->v_dir) {/* we may need to fix up this pointer */
287 rootpar.d_child = vol->v_dir;
289 /* if we aren't the root directory, we have parents and
290 * siblings to worry about */
291 if (dir->d_parent->d_child == node)
292 dir->d_parent->d_child = dir;
293 dir->d_next->d_prev = dir;
294 dir->d_prev->d_next = dir;
297 /* fix up children. */
301 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
304 if (node == curdir) /* another pointer to fixup */
307 /* we also need to fix up oforks. bleah */
308 if ((of = dir->d_ofork)) {
309 last = of->of_d_prev;
312 of = (last == of) ? NULL : of->of_d_next;
316 /* set the node's d_name */
317 node->d_name = save.d_name;
320 if (node->d_color == DIRTREE_COLOR_BLACK)
321 dir_rmrecolor(vol, leaf);
328 static struct dir *dir_insert(vol, dir)
329 const struct vol *vol;
335 while (pdir->d_did != dir->d_did ) {
336 if ( pdir->d_did > dir->d_did ) {
337 if ( pdir->d_left == SENTINEL ) {
344 if ( pdir->d_right == SENTINEL ) {
349 pdir = pdir->d_right;
357 * attempt to extend the current dir. tree to include path
358 * as a side-effect, movecwd to that point and return the new dir
362 extenddir( vol, dir, path )
370 p = mtoupath(vol, path );
371 if ( stat( p, &st ) != 0 ) {
374 if (!S_ISDIR(st.st_mode)) {
378 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
383 if ( movecwd( vol, dir ) < 0 ) {
390 static int deletedir(char *dir)
392 char path[MAXPATHLEN + 1];
398 if ((len = strlen(dir)) > sizeof(path))
402 if ((dp = opendir(dir)) == NULL)
408 while ((de = readdir(dp))) {
409 /* skip this and previous directory */
410 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
413 strncpy(path + len, de->d_name, sizeof(path) - len);
414 if (stat(path, &st) == 0) {
415 if (S_ISDIR(st.st_mode)) {
416 if ((err = deletedir(path)) < 0) {
420 } else if (unlink(path) < 0) {
423 continue; /* somebody went and deleted it behind our backs. */
439 /* okay. the directory is empty. delete it. note: we already got rid
441 if (rmdir(dir) < 0) {
445 case ENOTEMPTY : /* should never happen */
446 return( AFPERR_DIRNEMPT );
449 return( AFPERR_ACCESS );
453 return( AFPERR_PARAM );
459 /* do a recursive copy. */
460 static int copydir(char *src, char *dst, int noadouble)
462 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
469 /* doesn't exist or the path is too long. */
470 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
471 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
472 ((dp = opendir(src)) == NULL))
475 /* try to create the destination directory */
476 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
480 return( AFPERR_NOOBJ );
482 return( AFPERR_VLOCK );
485 return( AFPERR_ACCESS );
487 return( AFPERR_EXIST );
490 return( AFPERR_DFULL );
492 return( AFPERR_PARAM );
496 /* set things up to copy */
504 while ((de = readdir(dp))) {
505 /* skip this and previous directory */
506 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
509 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
510 if (stat(spath, &st) == 0) {
511 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
513 if (S_ISDIR(st.st_mode)) {
514 if ((err = copydir(spath, dpath, noadouble)) < 0)
516 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
520 /* keep the same time stamp. */
521 ut.actime = ut.modtime = st.st_mtime;
527 /* keep the same time stamp. */
528 if (stat(src, &st) == 0) {
529 ut.actime = ut.modtime = st.st_mtime;
539 /* --- public functions follow --- */
541 /* NOTE: we start off with at least one node (the root directory). */
542 struct dir *dirinsert( vol, dir )
548 if ((node = dir_insert(vol, dir)))
551 /* recolor the tree. the current node is red. */
552 dir->d_color = DIRTREE_COLOR_RED;
554 /* parent of this node has to be black. if the parent node
555 * is red, then we have a grandparent. */
556 while ((dir != vol->v_root) &&
557 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
558 /* are we on the left tree? */
559 if (dir->d_back == dir->d_back->d_back->d_left) {
560 node = dir->d_back->d_back->d_right; /* get the right node */
561 if (node->d_color == DIRTREE_COLOR_RED) {
562 /* we're red. we need to change to black. */
563 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
564 node->d_color = DIRTREE_COLOR_BLACK;
565 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
566 dir = dir->d_back->d_back; /* finished. go up. */
568 if (dir == dir->d_back->d_right) {
570 dir_leftrotate(vol, dir);
572 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
573 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
574 dir_rightrotate(vol, dir->d_back->d_back);
577 node = dir->d_back->d_back->d_left;
578 if (node->d_color == DIRTREE_COLOR_RED) {
579 /* we're red. we need to change to black. */
580 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
581 node->d_color = DIRTREE_COLOR_BLACK;
582 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
583 dir = dir->d_back->d_back; /* finished. ascend */
585 if (dir == dir->d_back->d_left) {
587 dir_rightrotate(vol, dir);
589 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
590 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
591 dir_leftrotate(vol, dir->d_back->d_back);
596 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
600 /* free everything down. we don't bother to recolor as this is only
601 * called to free the entire tree */
605 if (!dir || (dir == SENTINEL))
608 if ( dir->d_left != SENTINEL ) {
609 dirfree( dir->d_left );
611 if ( dir->d_right != SENTINEL ) {
612 dirfree( dir->d_right );
615 if (dir != SENTINEL) {
622 struct dir *dirnew(const int len)
626 dir = (struct dir *) calloc(1, sizeof( struct dir ));
630 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
635 dir->d_left = dir->d_right = SENTINEL;
636 dir->d_next = dir->d_prev = dir;
641 /* XXX: this needs to be changed to handle path types */
643 cname( vol, dir, cpath )
644 const struct vol *vol;
649 static char path[ MAXPATHLEN + 1];
655 if ( *data++ != 2 ) { /* path type */
658 len = (unsigned char) *data++;
664 if ( !extend && movecwd( vol, dir ) < 0 ) {
670 if ( *data == '\0' ) {
675 while ( *data == '\0' && len > 0 ) {
676 if ( dir->d_parent == NULL ) {
684 /* would this be faster with strlen + strncpy? */
686 while ( *data != '\0' && len > 0 ) {
691 /* short cut bits by chopping off a trailing \0. this also
692 makes the traversal happy w/ filenames at the end of the
699 * Dung Nguyen <ntd@adb.fr>
701 * AFPD cannot handle paths with "::" if the "::" notation is
702 * not at the beginning of the path. The following path will not
703 * be interpreted correctly:
705 * :a:b:::c: (directory c at the same level as directory a) */
713 if ( p != path ) { /* we got something */
717 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
720 cdir = (cdir == dir->d_child->d_prev) ? NULL :
723 if ( cdir == NULL ) {
725 if ( movecwd( vol, dir ) < 0 ) {
728 cdir = extenddir( vol, dir, path );
732 cdir = extenddir( vol, dir, path );
735 if ( cdir == NULL ) {
749 * Move curdir to dir, with a possible chdir()
751 int movecwd( vol, dir)
752 const struct vol *vol;
755 char path[MAXPATHLEN + 1];
760 if ( dir == curdir ) {
763 if ( dir->d_did == DIRDID_ROOT_PARENT) {
767 p = path + sizeof(path) - 1;
770 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
772 u = mtoupath(vol, d->d_name );
779 n = strlen( vol->v_path );
781 strncpy( p, vol->v_path, n );
783 if ( chdir( p ) < 0 ) {
790 int getdirparams(vol, bitmap, upath, dir, st, buf, buflen )
791 const struct vol *vol;
801 char *data, *nameoff = NULL;
804 int bit = 0, isad = 1;
808 memset(&ad, 0, sizeof(ad));
809 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
810 DIRBITS | 0777, &ad) < 0 ) {
815 while ( bitmap != 0 ) {
816 while (( bitmap & 1 ) == 0 ) {
824 ad_getattr(&ad, &ashort);
825 } else if (*upath == '.' && strcmp(upath, ".") &&
826 strcmp(upath, "..")) {
827 ashort = htons(ATTRBIT_INVISIBLE);
830 memcpy( data, &ashort, sizeof( ashort ));
831 data += sizeof( ashort );
835 if ( dir->d_did == DIRDID_ROOT) {
836 aint = DIRDID_ROOT_PARENT;
837 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
840 aint = dir->d_parent->d_did;
842 memcpy( data, &aint, sizeof( aint ));
843 data += sizeof( aint );
847 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
848 aint = AD_DATE_FROM_UNIX(st->st_mtime);
849 memcpy( data, &aint, sizeof( aint ));
850 data += sizeof( aint );
854 aint = AD_DATE_FROM_UNIX(st->st_mtime);
855 memcpy( data, &aint, sizeof( aint ));
856 data += sizeof( aint );
860 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
861 aint = AD_DATE_START;
862 memcpy( data, &aint, sizeof( aint ));
863 data += sizeof( aint );
868 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
869 } else { /* no appledouble */
870 memset( data, 0, 32 );
871 /* set default view -- this also gets done in ad_open() */
872 ashort = htons(FINDERINFO_CLOSEDVIEW);
873 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
875 /* dot files are by default invisible */
876 if (*upath == '.' && strcmp(upath, ".") &&
877 strcmp(upath, "..")) {
878 ashort = htons(FINDERINFO_INVISIBLE);
879 memcpy(data + FINDERINFO_FRFLAGOFF,
880 &ashort, sizeof(ashort));
887 if (dir->d_name) /* root of parent can have a null name */
890 memset(data, 0, sizeof(u_int16_t));
891 data += sizeof( u_int16_t );
895 memset(data, 0, sizeof(u_int16_t));
896 data += sizeof( u_int16_t );
900 memcpy( data, &dir->d_did, sizeof( aint ));
901 data += sizeof( aint );
904 case DIRPBIT_OFFCNT :
906 /* this needs to handle current directory access rights */
907 if ((dp = opendir( upath ))) {
908 while (( de = readdir( dp )) != NULL ) {
909 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
912 if (!validupath(vol, de->d_name))
915 /* now check against too long a filename */
916 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
923 ashort = htons( ashort );
924 memcpy( data, &ashort, sizeof( ashort ));
925 data += sizeof( ashort );
930 memcpy( data, &aint, sizeof( aint ));
931 data += sizeof( aint );
936 memcpy( data, &aint, sizeof( aint ));
937 data += sizeof( aint );
940 case DIRPBIT_ACCESS :
943 afsmode( upath, &ma, dir );
945 *data++ = ma.ma_user;
946 *data++ = ma.ma_world;
947 *data++ = ma.ma_group;
948 *data++ = ma.ma_owner;
951 /* Client has requested the ProDOS information block.
952 Just pass back the same basic block for all
953 directories. <shirsch@ibm.net> */
954 case DIRPBIT_PDINFO : /* ProDOS Info Block */
957 ashort = htons( 0x0200 );
958 memcpy( data, &ashort, sizeof( ashort ));
959 data += sizeof( ashort );
960 memset( data, 0, sizeof( ashort ));
961 data += sizeof( ashort );
966 ad_close( &ad, ADFLAGS_HF );
968 return( AFPERR_BITMAP );
974 ashort = htons( data - buf );
975 memcpy( nameoff, &ashort, sizeof( ashort ));
977 if ((aint = strlen( dir->d_name )) > MACFILELEN)
981 memcpy( data, dir->d_name, aint );
985 ad_close( &ad, ADFLAGS_HF );
987 *buflen = data - buf;
991 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
994 int ibuflen, *rbuflen;
999 u_int16_t vid, bitmap;
1005 memcpy( &vid, ibuf, sizeof( vid ));
1006 ibuf += sizeof( vid );
1008 if (( vol = getvolbyvid( vid )) == NULL ) {
1009 return( AFPERR_PARAM );
1012 if (vol->v_flags & AFPVOL_RO)
1013 return AFPERR_VLOCK;
1015 memcpy( &did, ibuf, sizeof( did ));
1016 ibuf += sizeof( int );
1018 if (( dir = dirsearch( vol, did )) == NULL ) {
1019 return( AFPERR_NOOBJ );
1022 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1023 bitmap = ntohs( bitmap );
1024 ibuf += sizeof( bitmap );
1026 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1027 return( AFPERR_NOOBJ );
1031 * If ibuf is odd, make it even.
1033 if ((u_long)ibuf & 1 ) {
1037 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1038 setvoltime(obj, vol );
1043 int setdirparams(vol, path, bitmap, buf )
1044 const struct vol *vol;
1052 int bit = 0, aint, isad = 1;
1053 u_int16_t ashort, bshort;
1056 upath = mtoupath(vol, path);
1057 memset(&ad, 0, sizeof(ad));
1058 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1059 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1061 * Check to see what we're trying to set. If it's anything
1062 * but ACCESS, UID, or GID, give an error. If it's any of those
1063 * three, we don't need the ad to be open, so just continue.
1065 * note: we also don't need to worry about mdate. also, be quiet
1066 * if we're using the noadouble option.
1068 if (!vol_noadouble(vol) && (bitmap &
1069 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1070 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO))))
1071 return AFPERR_ACCESS;
1076 * Check to see if a create was necessary. If it was, we'll want
1077 * to set our name, etc.
1079 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1080 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1081 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1082 ad_getentrylen( &ad, ADEID_NAME ));
1086 while ( bitmap != 0 ) {
1087 while (( bitmap & 1 ) == 0 ) {
1095 memcpy( &ashort, buf, sizeof( ashort ));
1096 ad_getattr(&ad, &bshort);
1097 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1098 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1102 ad_setattr(&ad, bshort);
1104 buf += sizeof( ashort );
1107 case DIRPBIT_CDATE :
1109 memcpy(&aint, buf, sizeof(aint));
1110 ad_setdate(&ad, AD_DATE_CREATE, aint);
1112 buf += sizeof( aint );
1115 case DIRPBIT_MDATE :
1116 memcpy(&aint, buf, sizeof(aint));
1118 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1119 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1121 buf += sizeof( aint );
1124 case DIRPBIT_BDATE :
1126 memcpy(&aint, buf, sizeof(aint));
1127 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1129 buf += sizeof( aint );
1132 case DIRPBIT_FINFO :
1134 * Alright, we admit it, this is *really* sick!
1135 * The 4 bytes that we don't copy, when we're dealing
1136 * with the root of a volume, are the directory's
1137 * location information. This eliminates that annoying
1138 * behavior one sees when mounting above another mount
1142 if ( curdir->d_did == DIRDID_ROOT ) {
1143 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1144 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1146 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1152 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1153 buf += sizeof( int );
1157 memcpy( &aint, buf, sizeof( aint ));
1158 buf += sizeof( aint );
1159 if (curdir->d_did == DIRDID_ROOT)
1160 setdeskowner( -1, aint );
1162 #if 0 /* don't error if we can't set the desktop owner. */
1166 err = AFPERR_ACCESS;
1167 goto setdirparam_done;
1171 goto setdirparam_done;
1174 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1177 goto setdirparam_done;
1183 if ( setdirowner( -1, aint, vol_noadouble(vol) ) < 0 ) {
1187 err = AFPERR_ACCESS;
1188 goto setdirparam_done;
1192 goto setdirparam_done;
1195 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1201 case DIRPBIT_ACCESS :
1202 ma.ma_user = *buf++;
1203 ma.ma_world = *buf++;
1204 ma.ma_group = *buf++;
1205 ma.ma_owner = *buf++;
1207 if (curdir->d_did == DIRDID_ROOT)
1208 setdeskmode(mtoumode( &ma ));
1209 #if 0 /* don't error if we can't set the desktop mode */
1213 err = AFPERR_ACCESS;
1214 goto setdirparam_done;
1217 goto setdirparam_done;
1219 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1222 goto setdirparam_done;
1227 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol)) < 0 ) {
1231 err = AFPERR_ACCESS;
1232 goto setdirparam_done;
1235 goto setdirparam_done;
1237 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1239 goto setdirparam_done;
1244 /* Ignore what the client thinks we should do to the
1245 ProDOS information block. Skip over the data and
1246 report nothing amiss. <shirsch@ibm.net> */
1247 case DIRPBIT_PDINFO :
1252 err = AFPERR_BITMAP;
1253 goto setdirparam_done;
1264 ad_flush( &ad, ADFLAGS_HF );
1265 ad_close( &ad, ADFLAGS_HF );
1271 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1274 int ibuflen, *rbuflen;
1287 memcpy( &vid, ibuf, sizeof( vid ));
1288 ibuf += sizeof( vid );
1289 if (( vol = getvolbyvid( vid )) == NULL ) {
1290 return( AFPERR_PARAM );
1293 if (vol->v_flags & AFPVOL_RO)
1294 return AFPERR_VLOCK;
1296 memcpy( &did, ibuf, sizeof( did ));
1297 ibuf += sizeof( did );
1298 if (( dir = dirsearch( vol, did )) == NULL ) {
1299 return( AFPERR_NOOBJ );
1302 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1303 return( AFPERR_NOOBJ );
1306 /* check for illegal bits */
1307 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1308 strpbrk(path, MSWINDOWS_BADCHARS))
1309 return AFPERR_PARAM;
1311 upath = mtoupath(vol, path);
1313 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1314 return AFPERR_PARAM;
1316 if (!validupath(vol, upath))
1317 return AFPERR_EXIST;
1319 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1322 return( AFPERR_NOOBJ );
1324 return( AFPERR_VLOCK );
1326 return( AFPERR_ACCESS );
1328 return( AFPERR_EXIST );
1331 return( AFPERR_DFULL );
1333 return( AFPERR_PARAM );
1337 if (stat(upath, &st) < 0)
1340 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1341 strlen(upath), &st)) == NULL)
1344 if ( movecwd( vol, dir ) < 0 ) {
1345 return( AFPERR_PARAM );
1348 memset(&ad, 0, sizeof(ad));
1349 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1350 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1351 if (vol_noadouble(vol))
1352 goto createdir_done;
1353 return( AFPERR_ACCESS );
1356 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1357 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1358 ad_getentrylen( &ad, ADEID_NAME ));
1359 ad_flush( &ad, ADFLAGS_HF );
1360 ad_close( &ad, ADFLAGS_HF );
1363 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1364 *rbuflen = sizeof( u_int32_t );
1365 setvoltime(obj, vol );
1370 int renamedir(src, dst, dir, newparent, newname, noadouble)
1371 char *src, *dst, *newname;
1372 struct dir *dir, *newparent;
1373 const int noadouble;
1380 /* existence check moved to afp_moveandrename */
1381 if ( rename( src, dst ) < 0 ) {
1384 return( AFPERR_NOOBJ );
1386 return( AFPERR_ACCESS );
1388 return AFPERR_VLOCK;
1390 /* tried to move directory into a subdirectory of itself */
1391 return AFPERR_CANTMOVE;
1393 /* this needs to copy and delete. bleah. that means we have
1394 * to deal with entire directory hierarchies. */
1395 if ((err = copydir(src, dst, noadouble)) < 0) {
1399 if ((err = deletedir(src)) < 0)
1403 return( AFPERR_PARAM );
1407 memset(&ad, 0, sizeof(ad));
1408 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1412 len = strlen(newname);
1413 goto renamedir_done;
1415 return( AFPERR_NOOBJ );
1417 return( AFPERR_ACCESS );
1419 return( AFPERR_PARAM );
1422 len = strlen( newname );
1423 ad_setentrylen( &ad, ADEID_NAME, len );
1424 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1425 ad_flush( &ad, ADFLAGS_HF );
1426 ad_close( &ad, ADFLAGS_HF );
1429 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1430 syslog( LOG_ERR, "renamedir: realloc: %m" );
1434 strcpy( dir->d_name, newname );
1436 if (( parent = dir->d_parent ) == NULL ) {
1439 if ( parent == newparent ) {
1443 /* detach from old parent and add to new one. */
1444 dirchildremove(parent, dir);
1445 dir->d_parent = newparent;
1446 dirchildadd(newparent, dir);
1450 #define DOT_APPLEDOUBLE_LEN 13
1451 /* delete an empty directory */
1452 int deletecurdir( vol, path, pathlen )
1453 const struct vol *vol;
1462 if ( curdir->d_parent == NULL ) {
1463 return( AFPERR_ACCESS );
1466 if ( curdir->d_child != NULL ) {
1467 return( AFPERR_DIRNEMPT );
1472 /* delete stray .AppleDouble files. this happens to get .Parent files
1474 if ((dp = opendir(".AppleDouble"))) {
1475 strcpy(path, ".AppleDouble/");
1476 while ((de = readdir(dp))) {
1477 /* skip this and previous directory */
1478 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1481 /* bail if the file exists in the current directory.
1482 * note: this will not fail with dangling symlinks */
1483 if (stat(de->d_name, &st) == 0) {
1485 return AFPERR_DIRNEMPT;
1488 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1489 if (unlink(path) < 0) {
1494 return( AFPERR_ACCESS );
1496 return AFPERR_VLOCK;
1500 return( AFPERR_PARAM );
1507 if ( rmdir( ".AppleDouble" ) < 0 ) {
1512 return( AFPERR_DIRNEMPT );
1514 return AFPERR_VLOCK;
1517 return( AFPERR_ACCESS );
1519 return( AFPERR_PARAM );
1523 /* now get rid of dangling symlinks */
1524 if ((dp = opendir("."))) {
1525 while ((de = readdir(dp))) {
1526 /* skip this and previous directory */
1527 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1530 /* bail if it's not a symlink */
1531 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode))
1532 return AFPERR_DIRNEMPT;
1534 if (unlink(de->d_name) < 0) {
1538 return( AFPERR_ACCESS );
1540 return AFPERR_VLOCK;
1544 return( AFPERR_PARAM );
1551 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1552 return( AFPERR_NOOBJ );
1555 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1558 return( AFPERR_NOOBJ );
1560 return( AFPERR_DIRNEMPT );
1563 return( AFPERR_ACCESS );
1565 return AFPERR_VLOCK;
1567 return( AFPERR_PARAM );
1571 dirchildremove(curdir, fdir);
1572 #if AD_VERSION > AD_VERSION1
1573 cnid_delete(vol->v_db, fdir->d_did);
1575 dir_remove( vol, fdir );
1580 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1583 int ibuflen, *rbuflen;
1592 sfunc = (unsigned char) *ibuf++;
1593 memcpy( &id, ibuf, sizeof( id ));
1598 if (( pw = getpwuid( id )) == NULL ) {
1600 return( AFPERR_NOITEM );
1606 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1608 return( AFPERR_NOITEM );
1615 return( AFPERR_PARAM );
1618 len = strlen( name );
1627 memcpy( rbuf, name, len );
1633 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1636 int ibuflen, *rbuflen;
1644 sfunc = (unsigned char) *ibuf++;
1645 len = (unsigned char) *ibuf++;
1651 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1653 return( AFPERR_NOITEM );
1659 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1661 return( AFPERR_NOITEM );
1667 return( AFPERR_PARAM );
1673 memcpy( rbuf, &id, sizeof( id ));
1674 *rbuflen = sizeof( id );
1678 /* variable DID support */
1679 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1682 int ibuflen, *rbuflen;
1693 /* do nothing as dids are static for the life of the process. */
1697 memcpy(&vid, ibuf, sizeof( vid ));
1698 ibuf += sizeof( vid );
1699 if (( vol = getvolbyvid( vid )) == NULL ) {
1700 return( AFPERR_PARAM );
1703 memcpy( &did, ibuf, sizeof( did ));
1704 ibuf += sizeof( did );
1705 if (( dir = dirsearch( vol, did )) == NULL ) {
1706 return( AFPERR_PARAM );
1709 /* dir_remove -- deletedid */
1715 /* did creation gets done automatically */
1716 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1719 int ibuflen, *rbuflen;
1722 struct dir *dir, *parentdir;
1731 memcpy(&vid, ibuf, sizeof(vid));
1732 ibuf += sizeof( vid );
1734 if (( vol = getvolbyvid( vid )) == NULL ) {
1735 return( AFPERR_PARAM );
1738 memcpy(&did, ibuf, sizeof(did));
1739 ibuf += sizeof(did);
1741 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1742 return( AFPERR_NOOBJ );
1745 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1746 return( AFPERR_NOOBJ );
1749 /* see if we already have the directory. */
1750 upath = mtoupath(vol, path);
1751 if ( stat( upath, &st ) < 0 ) {
1752 return( AFPERR_NOOBJ );
1755 dir = parentdir->d_child;
1757 if (strdiacasecmp(dir->d_name, path) == 0) {
1758 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1759 *rbuflen = sizeof(dir->d_did);
1762 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1765 /* we don't already have a did. add one in. */
1766 if ((dir = adddir(vol, parentdir, path, strlen(path),
1767 upath, strlen(upath), &st)) == NULL)
1770 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1771 *rbuflen = sizeof(dir->d_did);