2 * $Id: directory.c,v 1.11 2001-06-06 13:36:36 rufustfirefly 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
15 #include <sys/syslog.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>
25 #include <atalk/cnid.h>
35 #include "directory.h"
49 #define SENTINEL (&sentinel)
50 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
51 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
52 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
53 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
55 /* (from IM: Toolbox Essentials)
56 * dirFinderInfo (DInfo) fields:
58 * frRect 8 folder's window rectangle
60 * frLocation 4 folder's location in window
61 * frView 2 folder's view (default == closedView (256))
63 * extended dirFinderInfo (DXInfo) fields:
64 * frScroll 4 scroll position
65 * frOpenChain: 4 directory ID chain of open folders
66 * frScript: 1 script flag and code
67 * frXFlags: 1 reserved
68 * frComment: 2 comment ID
69 * frPutAway: 4 home directory ID
73 * redid did assignment for directories. now we use red-black trees.
78 const struct vol *vol;
88 if ( did == DIRDID_ROOT_PARENT ) {
90 rootpar.d_did = DIRDID_ROOT_PARENT;
91 rootpar.d_child = vol->v_dir;
96 while ( dir != SENTINEL ) {
97 if (dir->d_did == did)
98 return dir->d_name ? dir : NULL;
99 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
105 /* rotate the tree to the left */
106 static void dir_leftrotate(vol, dir)
110 struct dir *right = dir->d_right;
112 /* whee. move the right's left tree into dir's right tree */
113 dir->d_right = right->d_left;
114 if (right->d_left != SENTINEL)
115 right->d_left->d_back = dir;
117 if (right != SENTINEL) {
118 right->d_back = dir->d_back;
122 if (!dir->d_back) /* no parent. move the right tree to the top. */
124 else if (dir == dir->d_back->d_left) /* we were on the left */
125 dir->d_back->d_left = right;
127 dir->d_back->d_right = right; /* we were on the right */
129 /* re-insert dir on the left tree */
136 /* rotate the tree to the right */
137 static void dir_rightrotate(vol, dir)
141 struct dir *left = dir->d_left;
143 /* whee. move the left's right tree into dir's left tree */
144 dir->d_left = left->d_right;
145 if (left->d_right != SENTINEL)
146 left->d_right->d_back = dir;
148 if (left != SENTINEL) {
149 left->d_back = dir->d_back;
153 if (!dir->d_back) /* no parent. move the left tree to the top. */
155 else if (dir == dir->d_back->d_right) /* we were on the right */
156 dir->d_back->d_right = left;
158 dir->d_back->d_left = left; /* we were on the left */
160 /* re-insert dir on the right tree */
166 /* recolor after a removal */
167 static struct dir *dir_rmrecolor(vol, dir)
173 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
174 /* are we on the left tree? */
175 if (dir == dir->d_back->d_left) {
176 leaf = dir->d_back->d_right; /* get right side */
177 if (leaf->d_color == DIRTREE_COLOR_RED) {
178 /* we're red. we need to change to black. */
179 leaf->d_color = DIRTREE_COLOR_BLACK;
180 dir->d_back->d_color = DIRTREE_COLOR_RED;
181 dir_leftrotate(vol, dir->d_back);
182 leaf = dir->d_back->d_right;
185 /* right leaf has black end nodes */
186 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
187 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
188 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
189 dir = dir->d_back; /* ascend */
191 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
192 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
193 leaf->d_color = DIRTREE_COLOR_RED;
194 dir_rightrotate(vol, leaf);
195 leaf = dir->d_back->d_right;
197 leaf->d_color = dir->d_back->d_color;
198 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
199 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
200 dir_leftrotate(vol, dir->d_back);
203 } else { /* right tree */
204 leaf = dir->d_back->d_left; /* left tree */
205 if (leaf->d_color == DIRTREE_COLOR_RED) {
206 leaf->d_color = DIRTREE_COLOR_BLACK;
207 dir->d_back->d_color = DIRTREE_COLOR_RED;
208 dir_rightrotate(vol, dir->d_back);
209 leaf = dir->d_back->d_left;
212 /* left leaf has black end nodes */
213 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
214 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
215 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
216 dir = dir->d_back; /* ascend */
218 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
219 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
220 leaf->d_color = DIRTREE_COLOR_RED;
221 dir_leftrotate(vol, leaf);
222 leaf = dir->d_back->d_left;
224 leaf->d_color = dir->d_back->d_color;
225 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
226 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
227 dir_rightrotate(vol, dir->d_back);
232 dir->d_color = DIRTREE_COLOR_BLACK;
239 /* remove the node from the tree. this is just like insertion, but
240 * different. actually, it has to worry about a bunch of things that
241 * insertion doesn't care about. */
242 static void dir_remove( vol, dir )
247 struct ofork *of, *last;
248 struct dir *node, *leaf;
251 if (!dir || (dir == SENTINEL))
254 /* i'm not sure if it really helps to delete stuff. */
260 /* go searching for a node with at most one child */
261 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
265 while (node->d_left != SENTINEL)
270 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
273 leaf->d_back = node->d_back;
276 } else if (node == node->d_back->d_left) { /* left tree */
277 node->d_back->d_left = leaf;
279 node->d_back->d_right = leaf;
282 /* we want to free node, but we also want to free the data in dir.
283 * currently, that's d_name and the directory traversal bits.
284 * we just copy the necessary bits and then fix up all the
285 * various pointers to the directory. needless to say, there are
286 * a bunch of places that store the directory struct. */
288 struct dir save, *tmp;
290 memcpy(&save, dir, sizeof(save));
291 memcpy(dir, node, sizeof(struct dir));
293 /* restore the red-black bits */
294 dir->d_left = save.d_left;
295 dir->d_right = save.d_right;
296 dir->d_back = save.d_back;
297 dir->d_color = save.d_color;
299 if (node == vol->v_dir) {/* we may need to fix up this pointer */
301 rootpar.d_child = vol->v_dir;
303 /* if we aren't the root directory, we have parents and
304 * siblings to worry about */
305 if (dir->d_parent->d_child == node)
306 dir->d_parent->d_child = dir;
307 dir->d_next->d_prev = dir;
308 dir->d_prev->d_next = dir;
311 /* fix up children. */
315 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
318 if (node == curdir) /* another pointer to fixup */
321 /* we also need to fix up oforks. bleah */
322 if ((of = dir->d_ofork)) {
323 last = of->of_d_prev;
326 of = (last == of) ? NULL : of->of_d_next;
330 /* set the node's d_name */
331 node->d_name = save.d_name;
334 if (node->d_color == DIRTREE_COLOR_BLACK)
335 dir_rmrecolor(vol, leaf);
342 static struct dir *dir_insert(vol, dir)
343 const struct vol *vol;
349 while (pdir->d_did != dir->d_did ) {
350 if ( pdir->d_did > dir->d_did ) {
351 if ( pdir->d_left == SENTINEL ) {
358 if ( pdir->d_right == SENTINEL ) {
363 pdir = pdir->d_right;
371 * attempt to extend the current dir. tree to include path
372 * as a side-effect, movecwd to that point and return the new dir
376 extenddir( vol, dir, path )
384 p = mtoupath(vol, path );
385 if ( stat( p, &st ) != 0 ) {
388 if (!S_ISDIR(st.st_mode)) {
392 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
397 if ( movecwd( vol, dir ) < 0 ) {
404 static int deletedir(char *dir)
406 char path[MAXPATHLEN + 1];
412 if ((len = strlen(dir)) > sizeof(path))
416 if ((dp = opendir(dir)) == NULL)
422 while ((de = readdir(dp))) {
423 /* skip this and previous directory */
424 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
427 strncpy(path + len, de->d_name, sizeof(path) - len);
428 if (stat(path, &st) == 0) {
429 if (S_ISDIR(st.st_mode)) {
430 if ((err = deletedir(path)) < 0) {
434 } else if (unlink(path) < 0) {
437 continue; /* somebody went and deleted it behind our backs. */
453 /* okay. the directory is empty. delete it. note: we already got rid
455 if (rmdir(dir) < 0) {
459 case ENOTEMPTY : /* should never happen */
460 return( AFPERR_DIRNEMPT );
463 return( AFPERR_ACCESS );
467 return( AFPERR_PARAM );
473 /* do a recursive copy. */
474 static int copydir(char *src, char *dst, int noadouble)
476 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
483 /* doesn't exist or the path is too long. */
484 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
485 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
486 ((dp = opendir(src)) == NULL))
489 /* try to create the destination directory */
490 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
494 return( AFPERR_NOOBJ );
496 return( AFPERR_VLOCK );
499 return( AFPERR_ACCESS );
501 return( AFPERR_EXIST );
504 return( AFPERR_DFULL );
506 return( AFPERR_PARAM );
510 /* set things up to copy */
518 while ((de = readdir(dp))) {
519 /* skip this and previous directory */
520 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
523 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
524 if (stat(spath, &st) == 0) {
525 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
527 if (S_ISDIR(st.st_mode)) {
528 if ((err = copydir(spath, dpath, noadouble)) < 0)
530 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
534 /* keep the same time stamp. */
535 ut.actime = ut.modtime = st.st_mtime;
541 /* keep the same time stamp. */
542 if (stat(src, &st) == 0) {
543 ut.actime = ut.modtime = st.st_mtime;
553 /* --- public functions follow --- */
555 /* NOTE: we start off with at least one node (the root directory). */
556 struct dir *dirinsert( vol, dir )
562 if ((node = dir_insert(vol, dir)))
565 /* recolor the tree. the current node is red. */
566 dir->d_color = DIRTREE_COLOR_RED;
568 /* parent of this node has to be black. if the parent node
569 * is red, then we have a grandparent. */
570 while ((dir != vol->v_root) &&
571 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
572 /* are we on the left tree? */
573 if (dir->d_back == dir->d_back->d_back->d_left) {
574 node = dir->d_back->d_back->d_right; /* get the right node */
575 if (node->d_color == DIRTREE_COLOR_RED) {
576 /* we're red. we need to change to black. */
577 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
578 node->d_color = DIRTREE_COLOR_BLACK;
579 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
580 dir = dir->d_back->d_back; /* finished. go up. */
582 if (dir == dir->d_back->d_right) {
584 dir_leftrotate(vol, dir);
586 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
587 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
588 dir_rightrotate(vol, dir->d_back->d_back);
591 node = dir->d_back->d_back->d_left;
592 if (node->d_color == DIRTREE_COLOR_RED) {
593 /* we're red. we need to change to black. */
594 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
595 node->d_color = DIRTREE_COLOR_BLACK;
596 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
597 dir = dir->d_back->d_back; /* finished. ascend */
599 if (dir == dir->d_back->d_left) {
601 dir_rightrotate(vol, dir);
603 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
604 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
605 dir_leftrotate(vol, dir->d_back->d_back);
610 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
614 /* free everything down. we don't bother to recolor as this is only
615 * called to free the entire tree */
619 if (!dir || (dir == SENTINEL))
622 if ( dir->d_left != SENTINEL ) {
623 dirfree( dir->d_left );
625 if ( dir->d_right != SENTINEL ) {
626 dirfree( dir->d_right );
629 if (dir != SENTINEL) {
636 struct dir *dirnew(const int len)
640 dir = (struct dir *) calloc(1, sizeof( struct dir ));
644 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
649 dir->d_left = dir->d_right = SENTINEL;
650 dir->d_next = dir->d_prev = dir;
655 /* XXX: this needs to be changed to handle path types */
657 cname( vol, dir, cpath )
658 const struct vol *vol;
663 static char path[ MAXPATHLEN + 1];
669 if ( *data++ != 2 ) { /* path type */
672 len = (unsigned char) *data++;
678 if ( !extend && movecwd( vol, dir ) < 0 ) {
684 if ( *data == '\0' ) {
689 while ( *data == '\0' && len > 0 ) {
690 if ( dir->d_parent == NULL ) {
698 /* would this be faster with strlen + strncpy? */
700 while ( *data != '\0' && len > 0 ) {
705 /* short cut bits by chopping off a trailing \0. this also
706 makes the traversal happy w/ filenames at the end of the
713 * Dung Nguyen <ntd@adb.fr>
715 * AFPD cannot handle paths with "::" if the "::" notation is
716 * not at the beginning of the path. The following path will not
717 * be interpreted correctly:
719 * :a:b:::c: (directory c at the same level as directory a) */
727 if ( p != path ) { /* we got something */
731 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
734 cdir = (cdir == dir->d_child->d_prev) ? NULL :
737 if ( cdir == NULL ) {
739 if ( movecwd( vol, dir ) < 0 ) {
742 cdir = extenddir( vol, dir, path );
746 cdir = extenddir( vol, dir, path );
749 if ( cdir == NULL ) {
763 * Move curdir to dir, with a possible chdir()
765 int movecwd( vol, dir)
766 const struct vol *vol;
769 char path[MAXPATHLEN + 1];
774 if ( dir == curdir ) {
777 if ( dir->d_did == DIRDID_ROOT_PARENT) {
781 p = path + sizeof(path) - 1;
784 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
786 u = mtoupath(vol, d->d_name );
793 n = strlen( vol->v_path );
795 strncpy( p, vol->v_path, n );
797 if ( chdir( p ) < 0 ) {
804 int getdirparams(vol, bitmap, upath, dir, st, buf, buflen )
805 const struct vol *vol;
815 char *data, *nameoff = NULL;
818 int bit = 0, isad = 1;
825 memset(&ad, 0, sizeof(ad));
828 save_uidgid ( uidgid );
832 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
833 DIRBITS | 0777, &ad) < 0 ) {
838 while ( bitmap != 0 ) {
839 while (( bitmap & 1 ) == 0 ) {
847 ad_getattr(&ad, &ashort);
848 } else if (*upath == '.' && strcmp(upath, ".") &&
849 strcmp(upath, "..")) {
850 ashort = htons(ATTRBIT_INVISIBLE);
853 memcpy( data, &ashort, sizeof( ashort ));
854 data += sizeof( ashort );
858 if ( dir->d_did == DIRDID_ROOT) {
859 aint = DIRDID_ROOT_PARENT;
860 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
863 aint = dir->d_parent->d_did;
865 memcpy( data, &aint, sizeof( aint ));
866 data += sizeof( aint );
870 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
871 aint = AD_DATE_FROM_UNIX(st->st_mtime);
872 memcpy( data, &aint, sizeof( aint ));
873 data += sizeof( aint );
877 aint = AD_DATE_FROM_UNIX(st->st_mtime);
878 memcpy( data, &aint, sizeof( aint ));
879 data += sizeof( aint );
883 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
884 aint = AD_DATE_START;
885 memcpy( data, &aint, sizeof( aint ));
886 data += sizeof( aint );
891 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
892 } else { /* no appledouble */
893 memset( data, 0, 32 );
894 /* set default view -- this also gets done in ad_open() */
895 ashort = htons(FINDERINFO_CLOSEDVIEW);
896 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
898 /* dot files are by default invisible */
899 if (*upath == '.' && strcmp(upath, ".") &&
900 strcmp(upath, "..")) {
901 ashort = htons(FINDERINFO_INVISIBLE);
902 memcpy(data + FINDERINFO_FRFLAGOFF,
903 &ashort, sizeof(ashort));
910 if (dir->d_name) /* root of parent can have a null name */
913 memset(data, 0, sizeof(u_int16_t));
914 data += sizeof( u_int16_t );
918 memset(data, 0, sizeof(u_int16_t));
919 data += sizeof( u_int16_t );
923 memcpy( data, &dir->d_did, sizeof( aint ));
924 data += sizeof( aint );
927 case DIRPBIT_OFFCNT :
929 /* this needs to handle current directory access rights */
930 if ((dp = opendir( upath ))) {
931 while (( de = readdir( dp )) != NULL ) {
932 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
935 if (!validupath(vol, de->d_name))
938 /* now check against too long a filename */
939 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
946 ashort = htons( ashort );
947 memcpy( data, &ashort, sizeof( ashort ));
948 data += sizeof( ashort );
952 aint = htonl(st->st_uid);
953 memcpy( data, &aint, sizeof( aint ));
954 data += sizeof( aint );
958 aint = htonl(st->st_gid);
959 memcpy( data, &aint, sizeof( aint ));
960 data += sizeof( aint );
963 case DIRPBIT_ACCESS :
965 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
967 accessmode( upath, &ma, dir );
969 #endif SENDFILE_FLAVOR_LINUX
970 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
971 afsmode( upath, &ma, dir );
973 *data++ = ma.ma_user;
974 *data++ = ma.ma_world;
975 *data++ = ma.ma_group;
976 *data++ = ma.ma_owner;
979 /* Client has requested the ProDOS information block.
980 Just pass back the same basic block for all
981 directories. <shirsch@ibm.net> */
982 case DIRPBIT_PDINFO : /* ProDOS Info Block */
985 ashort = htons( 0x0200 );
986 memcpy( data, &ashort, sizeof( ashort ));
987 data += sizeof( ashort );
988 memset( data, 0, sizeof( ashort ));
989 data += sizeof( ashort );
994 ad_close( &ad, ADFLAGS_HF );
997 restore_uidgid ( uidgid );
999 return( AFPERR_BITMAP );
1005 ashort = htons( data - buf );
1006 memcpy( nameoff, &ashort, sizeof( ashort ));
1008 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1012 memcpy( data, dir->d_name, aint );
1016 ad_close( &ad, ADFLAGS_HF );
1018 *buflen = data - buf;
1022 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1025 int ibuflen, *rbuflen;
1030 u_int16_t vid, bitmap;
1036 memcpy( &vid, ibuf, sizeof( vid ));
1037 ibuf += sizeof( vid );
1039 if (( vol = getvolbyvid( vid )) == NULL ) {
1040 return( AFPERR_PARAM );
1043 if (vol->v_flags & AFPVOL_RO)
1044 return AFPERR_VLOCK;
1046 memcpy( &did, ibuf, sizeof( did ));
1047 ibuf += sizeof( int );
1049 if (( dir = dirsearch( vol, did )) == NULL ) {
1050 return( AFPERR_NOOBJ );
1053 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1054 bitmap = ntohs( bitmap );
1055 ibuf += sizeof( bitmap );
1057 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1058 return( AFPERR_NOOBJ );
1062 * If ibuf is odd, make it even.
1064 if ((u_long)ibuf & 1 ) {
1068 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1069 setvoltime(obj, vol );
1074 int setdirparams(vol, path, bitmap, buf )
1075 const struct vol *vol;
1083 int bit = 0, aint, isad = 1;
1084 u_int16_t ashort, bshort;
1090 upath = mtoupath(vol, path);
1091 memset(&ad, 0, sizeof(ad));
1093 save_uidgid ( uidgid );
1095 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1096 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1098 * Check to see what we're trying to set. If it's anything
1099 * but ACCESS, UID, or GID, give an error. If it's any of those
1100 * three, we don't need the ad to be open, so just continue.
1102 * note: we also don't need to worry about mdate. also, be quiet
1103 * if we're using the noadouble option.
1105 if (!vol_noadouble(vol) && (bitmap &
1106 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1107 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1109 restore_uidgid ( uidgid );
1111 return AFPERR_ACCESS;
1117 * Check to see if a create was necessary. If it was, we'll want
1118 * to set our name, etc.
1120 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1121 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1122 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1123 ad_getentrylen( &ad, ADEID_NAME ));
1127 while ( bitmap != 0 ) {
1128 while (( bitmap & 1 ) == 0 ) {
1136 memcpy( &ashort, buf, sizeof( ashort ));
1137 ad_getattr(&ad, &bshort);
1138 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1139 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1143 ad_setattr(&ad, bshort);
1145 buf += sizeof( ashort );
1148 case DIRPBIT_CDATE :
1150 memcpy(&aint, buf, sizeof(aint));
1151 ad_setdate(&ad, AD_DATE_CREATE, aint);
1153 buf += sizeof( aint );
1156 case DIRPBIT_MDATE :
1157 memcpy(&aint, buf, sizeof(aint));
1159 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1160 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1162 buf += sizeof( aint );
1165 case DIRPBIT_BDATE :
1167 memcpy(&aint, buf, sizeof(aint));
1168 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1170 buf += sizeof( aint );
1173 case DIRPBIT_FINFO :
1175 * Alright, we admit it, this is *really* sick!
1176 * The 4 bytes that we don't copy, when we're dealing
1177 * with the root of a volume, are the directory's
1178 * location information. This eliminates that annoying
1179 * behavior one sees when mounting above another mount
1183 if ( curdir->d_did == DIRDID_ROOT ) {
1184 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1185 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1187 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1193 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1194 memcpy( &aint, buf, sizeof(aint));
1195 buf += sizeof( aint );
1196 if ( (curdir->d_did == DIRDID_ROOT) &&
1197 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1201 err = AFPERR_ACCESS;
1202 goto setdirparam_done;
1206 goto setdirparam_done;
1209 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1212 goto setdirparam_done;
1217 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1221 err = AFPERR_ACCESS;
1222 goto setdirparam_done;
1226 goto setdirparam_done;
1229 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1235 memcpy( &aint, buf, sizeof( aint ));
1236 buf += sizeof( aint );
1237 if (curdir->d_did == DIRDID_ROOT)
1238 setdeskowner( -1, ntohl(aint) );
1240 #if 0 /* don't error if we can't set the desktop owner. */
1244 err = AFPERR_ACCESS;
1245 goto setdirparam_done;
1249 goto setdirparam_done;
1252 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1255 goto setdirparam_done;
1261 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1265 err = AFPERR_ACCESS;
1266 goto setdirparam_done;
1270 goto setdirparam_done;
1273 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1279 case DIRPBIT_ACCESS :
1280 ma.ma_user = *buf++;
1281 ma.ma_world = *buf++;
1282 ma.ma_group = *buf++;
1283 ma.ma_owner = *buf++;
1285 if (curdir->d_did == DIRDID_ROOT)
1286 setdeskmode(mtoumode( &ma ));
1287 #if 0 /* don't error if we can't set the desktop mode */
1291 err = AFPERR_ACCESS;
1292 goto setdirparam_done;
1295 goto setdirparam_done;
1297 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1300 goto setdirparam_done;
1305 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1306 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1310 err = AFPERR_ACCESS;
1311 goto setdirparam_done;
1314 goto setdirparam_done;
1316 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1318 goto setdirparam_done;
1323 /* Ignore what the client thinks we should do to the
1324 ProDOS information block. Skip over the data and
1325 report nothing amiss. <shirsch@ibm.net> */
1326 case DIRPBIT_PDINFO :
1331 err = AFPERR_BITMAP;
1332 goto setdirparam_done;
1343 ad_flush( &ad, ADFLAGS_HF );
1344 ad_close( &ad, ADFLAGS_HF );
1348 restore_uidgid ( uidgid );
1353 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1356 int ibuflen, *rbuflen;
1372 memcpy( &vid, ibuf, sizeof( vid ));
1373 ibuf += sizeof( vid );
1374 if (( vol = getvolbyvid( vid )) == NULL ) {
1375 return( AFPERR_PARAM );
1378 if (vol->v_flags & AFPVOL_RO)
1379 return AFPERR_VLOCK;
1381 memcpy( &did, ibuf, sizeof( did ));
1382 ibuf += sizeof( did );
1383 if (( dir = dirsearch( vol, did )) == NULL ) {
1384 return( AFPERR_NOOBJ );
1387 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1388 return( AFPERR_NOOBJ );
1391 /* check for illegal bits */
1392 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1393 strpbrk(path, MSWINDOWS_BADCHARS))
1394 return AFPERR_PARAM;
1396 upath = mtoupath(vol, path);
1398 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1399 return AFPERR_PARAM;
1401 if (!validupath(vol, upath))
1402 return AFPERR_EXIST;
1405 save_uidgid ( uidgid );
1409 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1411 restore_uidgid ( uidgid );
1415 return( AFPERR_NOOBJ );
1417 return( AFPERR_VLOCK );
1419 return( AFPERR_ACCESS );
1421 return( AFPERR_EXIST );
1424 return( AFPERR_DFULL );
1426 return( AFPERR_PARAM );
1430 if (stat(upath, &st) < 0) {
1432 restore_uidgid ( uidgid );
1437 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1438 strlen(upath), &st)) == NULL) {
1440 restore_uidgid ( uidgid );
1445 if ( movecwd( vol, dir ) < 0 ) {
1447 restore_uidgid ( uidgid );
1449 return( AFPERR_PARAM );
1452 memset(&ad, 0, sizeof(ad));
1453 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1454 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1455 if (vol_noadouble(vol))
1456 goto createdir_done;
1458 restore_uidgid ( uidgid );
1460 return( AFPERR_ACCESS );
1463 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1464 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1465 ad_getentrylen( &ad, ADEID_NAME ));
1466 ad_flush( &ad, ADFLAGS_HF );
1467 ad_close( &ad, ADFLAGS_HF );
1470 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1471 *rbuflen = sizeof( u_int32_t );
1472 setvoltime(obj, vol );
1474 restore_uidgid ( uidgid );
1480 int renamedir(src, dst, dir, newparent, newname, noadouble)
1481 char *src, *dst, *newname;
1482 struct dir *dir, *newparent;
1483 const int noadouble;
1490 /* existence check moved to afp_moveandrename */
1491 if ( rename( src, dst ) < 0 ) {
1494 return( AFPERR_NOOBJ );
1496 return( AFPERR_ACCESS );
1498 return AFPERR_VLOCK;
1500 /* tried to move directory into a subdirectory of itself */
1501 return AFPERR_CANTMOVE;
1503 /* this needs to copy and delete. bleah. that means we have
1504 * to deal with entire directory hierarchies. */
1505 if ((err = copydir(src, dst, noadouble)) < 0) {
1509 if ((err = deletedir(src)) < 0)
1513 return( AFPERR_PARAM );
1517 memset(&ad, 0, sizeof(ad));
1518 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1522 len = strlen(newname);
1523 goto renamedir_done;
1525 return( AFPERR_NOOBJ );
1527 return( AFPERR_ACCESS );
1529 return( AFPERR_PARAM );
1532 len = strlen( newname );
1533 ad_setentrylen( &ad, ADEID_NAME, len );
1534 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1535 ad_flush( &ad, ADFLAGS_HF );
1536 ad_close( &ad, ADFLAGS_HF );
1539 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1540 syslog( LOG_ERR, "renamedir: realloc: %m" );
1544 strcpy( dir->d_name, newname );
1546 if (( parent = dir->d_parent ) == NULL ) {
1549 if ( parent == newparent ) {
1553 /* detach from old parent and add to new one. */
1554 dirchildremove(parent, dir);
1555 dir->d_parent = newparent;
1556 dirchildadd(newparent, dir);
1560 #define DOT_APPLEDOUBLE_LEN 13
1561 /* delete an empty directory */
1562 int deletecurdir( vol, path, pathlen )
1563 const struct vol *vol;
1575 if ( curdir->d_parent == NULL ) {
1576 return( AFPERR_ACCESS );
1579 if ( curdir->d_child != NULL ) {
1580 return( AFPERR_DIRNEMPT );
1586 save_uidgid ( uidgid );
1590 /* delete stray .AppleDouble files. this happens to get .Parent files
1592 if ((dp = opendir(".AppleDouble"))) {
1593 strcpy(path, ".AppleDouble/");
1594 while ((de = readdir(dp))) {
1595 /* skip this and previous directory */
1596 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1599 /* bail if the file exists in the current directory.
1600 * note: this will not fail with dangling symlinks */
1601 if (stat(de->d_name, &st) == 0) {
1604 restore_uidgid ( uidgid );
1606 return AFPERR_DIRNEMPT;
1609 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1610 if (unlink(path) < 0) {
1616 restore_uidgid ( uidgid );
1618 return( AFPERR_ACCESS );
1621 restore_uidgid ( uidgid );
1623 return AFPERR_VLOCK;
1628 restore_uidgid ( uidgid );
1630 return( AFPERR_PARAM );
1637 if ( rmdir( ".AppleDouble" ) < 0 ) {
1643 restore_uidgid ( uidgid );
1645 return( AFPERR_DIRNEMPT );
1648 restore_uidgid ( uidgid );
1650 return AFPERR_VLOCK;
1654 restore_uidgid ( uidgid );
1656 return( AFPERR_ACCESS );
1659 restore_uidgid ( uidgid );
1661 return( AFPERR_PARAM );
1665 /* now get rid of dangling symlinks */
1666 if ((dp = opendir("."))) {
1667 while ((de = readdir(dp))) {
1668 /* skip this and previous directory */
1669 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1672 /* bail if it's not a symlink */
1673 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1675 restore_uidgid ( uidgid );
1677 return AFPERR_DIRNEMPT;
1680 if (unlink(de->d_name) < 0) {
1685 restore_uidgid ( uidgid );
1687 return( AFPERR_ACCESS );
1690 restore_uidgid ( uidgid );
1692 return AFPERR_VLOCK;
1697 restore_uidgid ( uidgid );
1699 return( AFPERR_PARAM );
1706 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1708 restore_uidgid ( uidgid );
1710 return( AFPERR_NOOBJ );
1713 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1717 restore_uidgid ( uidgid );
1719 return( AFPERR_NOOBJ );
1722 restore_uidgid ( uidgid );
1724 return( AFPERR_DIRNEMPT );
1728 restore_uidgid ( uidgid );
1730 return( AFPERR_ACCESS );
1733 restore_uidgid ( uidgid );
1735 return AFPERR_VLOCK;
1738 restore_uidgid ( uidgid );
1740 return( AFPERR_PARAM );
1744 dirchildremove(curdir, fdir);
1745 #if AD_VERSION > AD_VERSION1
1746 cnid_delete(vol->v_db, fdir->d_did);
1748 dir_remove( vol, fdir );
1751 restore_uidgid ( uidgid );
1756 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1759 int ibuflen, *rbuflen;
1768 sfunc = (unsigned char) *ibuf++;
1769 memcpy( &id, ibuf, sizeof( id ));
1774 if (( pw = getpwuid( id )) == NULL ) {
1776 return( AFPERR_NOITEM );
1782 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1784 return( AFPERR_NOITEM );
1791 return( AFPERR_PARAM );
1794 len = strlen( name );
1803 memcpy( rbuf, name, len );
1809 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1812 int ibuflen, *rbuflen;
1820 sfunc = (unsigned char) *ibuf++;
1821 len = (unsigned char) *ibuf++;
1827 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1829 return( AFPERR_NOITEM );
1835 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1837 return( AFPERR_NOITEM );
1843 return( AFPERR_PARAM );
1849 memcpy( rbuf, &id, sizeof( id ));
1850 *rbuflen = sizeof( id );
1854 /* variable DID support */
1855 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1858 int ibuflen, *rbuflen;
1869 /* do nothing as dids are static for the life of the process. */
1873 memcpy(&vid, ibuf, sizeof( vid ));
1874 ibuf += sizeof( vid );
1875 if (( vol = getvolbyvid( vid )) == NULL ) {
1876 return( AFPERR_PARAM );
1879 memcpy( &did, ibuf, sizeof( did ));
1880 ibuf += sizeof( did );
1881 if (( dir = dirsearch( vol, did )) == NULL ) {
1882 return( AFPERR_PARAM );
1885 /* dir_remove -- deletedid */
1891 /* did creation gets done automatically */
1892 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1895 int ibuflen, *rbuflen;
1898 struct dir *dir, *parentdir;
1905 #endif /* FORCE_UIDGID */
1910 memcpy(&vid, ibuf, sizeof(vid));
1911 ibuf += sizeof( vid );
1913 if (( vol = getvolbyvid( vid )) == NULL ) {
1914 return( AFPERR_PARAM );
1917 memcpy(&did, ibuf, sizeof(did));
1918 ibuf += sizeof(did);
1920 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1921 return( AFPERR_NOOBJ );
1924 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1925 return( AFPERR_NOOBJ );
1928 /* see if we already have the directory. */
1929 upath = mtoupath(vol, path);
1930 if ( stat( upath, &st ) < 0 ) {
1931 return( AFPERR_NOOBJ );
1934 dir = parentdir->d_child;
1936 if (strdiacasecmp(dir->d_name, path) == 0) {
1937 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1938 *rbuflen = sizeof(dir->d_did);
1941 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1945 save_uidgid ( uidgid );
1947 #endif /* FORCE_UIDGID */
1949 /* we don't already have a did. add one in. */
1950 if ((dir = adddir(vol, parentdir, path, strlen(path),
1951 upath, strlen(upath), &st)) == NULL) {
1953 restore_uidgid ( uidgid );
1954 #endif /* FORCE_UIDGID */
1958 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1959 *rbuflen = sizeof(dir->d_did);
1961 restore_uidgid ( uidgid );
1962 #endif /* FORCE_UIDGID */