2 * $Id: directory.c,v 1.13 2001-06-27 14:53:16 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
13 #endif /* HAVE_CONFIG_H */
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>
32 #endif /* HAVE_FCNTL_H */
37 #include "directory.h"
47 #endif /* FORCE_UIDGID */
51 #define SENTINEL (&sentinel)
52 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
53 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
54 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
55 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
57 /* (from IM: Toolbox Essentials)
58 * dirFinderInfo (DInfo) fields:
60 * frRect 8 folder's window rectangle
62 * frLocation 4 folder's location in window
63 * frView 2 folder's view (default == closedView (256))
65 * extended dirFinderInfo (DXInfo) fields:
66 * frScroll 4 scroll position
67 * frOpenChain: 4 directory ID chain of open folders
68 * frScript: 1 script flag and code
69 * frXFlags: 1 reserved
70 * frComment: 2 comment ID
71 * frPutAway: 4 home directory ID
75 * redid did assignment for directories. now we use red-black trees.
80 const struct vol *vol;
90 if ( did == DIRDID_ROOT_PARENT ) {
92 rootpar.d_did = DIRDID_ROOT_PARENT;
93 rootpar.d_child = vol->v_dir;
98 while ( dir != SENTINEL ) {
99 if (dir->d_did == did)
100 return dir->d_name ? dir : NULL;
101 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
107 /* rotate the tree to the left */
108 static void dir_leftrotate(vol, dir)
112 struct dir *right = dir->d_right;
114 /* whee. move the right's left tree into dir's right tree */
115 dir->d_right = right->d_left;
116 if (right->d_left != SENTINEL)
117 right->d_left->d_back = dir;
119 if (right != SENTINEL) {
120 right->d_back = dir->d_back;
124 if (!dir->d_back) /* no parent. move the right tree to the top. */
126 else if (dir == dir->d_back->d_left) /* we were on the left */
127 dir->d_back->d_left = right;
129 dir->d_back->d_right = right; /* we were on the right */
131 /* re-insert dir on the left tree */
138 /* rotate the tree to the right */
139 static void dir_rightrotate(vol, dir)
143 struct dir *left = dir->d_left;
145 /* whee. move the left's right tree into dir's left tree */
146 dir->d_left = left->d_right;
147 if (left->d_right != SENTINEL)
148 left->d_right->d_back = dir;
150 if (left != SENTINEL) {
151 left->d_back = dir->d_back;
155 if (!dir->d_back) /* no parent. move the left tree to the top. */
157 else if (dir == dir->d_back->d_right) /* we were on the right */
158 dir->d_back->d_right = left;
160 dir->d_back->d_left = left; /* we were on the left */
162 /* re-insert dir on the right tree */
168 /* recolor after a removal */
169 static struct dir *dir_rmrecolor(vol, dir)
175 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
176 /* are we on the left tree? */
177 if (dir == dir->d_back->d_left) {
178 leaf = dir->d_back->d_right; /* get right side */
179 if (leaf->d_color == DIRTREE_COLOR_RED) {
180 /* we're red. we need to change to black. */
181 leaf->d_color = DIRTREE_COLOR_BLACK;
182 dir->d_back->d_color = DIRTREE_COLOR_RED;
183 dir_leftrotate(vol, dir->d_back);
184 leaf = dir->d_back->d_right;
187 /* right leaf has black end nodes */
188 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
189 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
190 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
191 dir = dir->d_back; /* ascend */
193 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
194 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
195 leaf->d_color = DIRTREE_COLOR_RED;
196 dir_rightrotate(vol, leaf);
197 leaf = dir->d_back->d_right;
199 leaf->d_color = dir->d_back->d_color;
200 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
201 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
202 dir_leftrotate(vol, dir->d_back);
205 } else { /* right tree */
206 leaf = dir->d_back->d_left; /* left tree */
207 if (leaf->d_color == DIRTREE_COLOR_RED) {
208 leaf->d_color = DIRTREE_COLOR_BLACK;
209 dir->d_back->d_color = DIRTREE_COLOR_RED;
210 dir_rightrotate(vol, dir->d_back);
211 leaf = dir->d_back->d_left;
214 /* left leaf has black end nodes */
215 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
216 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
217 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
218 dir = dir->d_back; /* ascend */
220 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
221 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
222 leaf->d_color = DIRTREE_COLOR_RED;
223 dir_leftrotate(vol, leaf);
224 leaf = dir->d_back->d_left;
226 leaf->d_color = dir->d_back->d_color;
227 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
228 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
229 dir_rightrotate(vol, dir->d_back);
234 dir->d_color = DIRTREE_COLOR_BLACK;
241 /* remove the node from the tree. this is just like insertion, but
242 * different. actually, it has to worry about a bunch of things that
243 * insertion doesn't care about. */
244 static void dir_remove( vol, dir )
249 struct ofork *of, *last;
250 struct dir *node, *leaf;
251 #endif /* REMOVE_NODES */
253 if (!dir || (dir == SENTINEL))
256 /* i'm not sure if it really helps to delete stuff. */
260 #else /* ! REMOVE_NODES */
262 /* go searching for a node with at most one child */
263 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
267 while (node->d_left != SENTINEL)
272 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
275 leaf->d_back = node->d_back;
278 } else if (node == node->d_back->d_left) { /* left tree */
279 node->d_back->d_left = leaf;
281 node->d_back->d_right = leaf;
284 /* we want to free node, but we also want to free the data in dir.
285 * currently, that's d_name and the directory traversal bits.
286 * we just copy the necessary bits and then fix up all the
287 * various pointers to the directory. needless to say, there are
288 * a bunch of places that store the directory struct. */
290 struct dir save, *tmp;
292 memcpy(&save, dir, sizeof(save));
293 memcpy(dir, node, sizeof(struct dir));
295 /* restore the red-black bits */
296 dir->d_left = save.d_left;
297 dir->d_right = save.d_right;
298 dir->d_back = save.d_back;
299 dir->d_color = save.d_color;
301 if (node == vol->v_dir) {/* we may need to fix up this pointer */
303 rootpar.d_child = vol->v_dir;
305 /* if we aren't the root directory, we have parents and
306 * siblings to worry about */
307 if (dir->d_parent->d_child == node)
308 dir->d_parent->d_child = dir;
309 dir->d_next->d_prev = dir;
310 dir->d_prev->d_next = dir;
313 /* fix up children. */
317 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
320 if (node == curdir) /* another pointer to fixup */
323 /* we also need to fix up oforks. bleah */
324 if ((of = dir->d_ofork)) {
325 last = of->of_d_prev;
328 of = (last == of) ? NULL : of->of_d_next;
332 /* set the node's d_name */
333 node->d_name = save.d_name;
336 if (node->d_color == DIRTREE_COLOR_BLACK)
337 dir_rmrecolor(vol, leaf);
340 #endif /* ! REMOVE_NODES */
344 static struct dir *dir_insert(vol, dir)
345 const struct vol *vol;
351 while (pdir->d_did != dir->d_did ) {
352 if ( pdir->d_did > dir->d_did ) {
353 if ( pdir->d_left == SENTINEL ) {
360 if ( pdir->d_right == SENTINEL ) {
365 pdir = pdir->d_right;
373 * attempt to extend the current dir. tree to include path
374 * as a side-effect, movecwd to that point and return the new dir
378 extenddir( vol, dir, path )
386 p = mtoupath(vol, path );
387 if ( stat( p, &st ) != 0 ) {
390 if (!S_ISDIR(st.st_mode)) {
394 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
399 if ( movecwd( vol, dir ) < 0 ) {
406 static int deletedir(char *dir)
408 char path[MAXPATHLEN + 1];
414 if ((len = strlen(dir)) > sizeof(path))
418 if ((dp = opendir(dir)) == NULL)
424 while ((de = readdir(dp))) {
425 /* skip this and previous directory */
426 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
429 strncpy(path + len, de->d_name, sizeof(path) - len);
430 if (stat(path, &st) == 0) {
431 if (S_ISDIR(st.st_mode)) {
432 if ((err = deletedir(path)) < 0) {
436 } else if (unlink(path) < 0) {
439 continue; /* somebody went and deleted it behind our backs. */
455 /* okay. the directory is empty. delete it. note: we already got rid
457 if (rmdir(dir) < 0) {
461 case ENOTEMPTY : /* should never happen */
462 return( AFPERR_DIRNEMPT );
465 return( AFPERR_ACCESS );
469 return( AFPERR_PARAM );
475 /* do a recursive copy. */
476 static int copydir(char *src, char *dst, int noadouble)
478 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
485 /* doesn't exist or the path is too long. */
486 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
487 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
488 ((dp = opendir(src)) == NULL))
491 /* try to create the destination directory */
492 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
496 return( AFPERR_NOOBJ );
498 return( AFPERR_VLOCK );
501 return( AFPERR_ACCESS );
503 return( AFPERR_EXIST );
506 return( AFPERR_DFULL );
508 return( AFPERR_PARAM );
512 /* set things up to copy */
520 while ((de = readdir(dp))) {
521 /* skip this and previous directory */
522 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
525 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
526 if (stat(spath, &st) == 0) {
527 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
529 if (S_ISDIR(st.st_mode)) {
530 if ((err = copydir(spath, dpath, noadouble)) < 0)
532 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
536 /* keep the same time stamp. */
537 ut.actime = ut.modtime = st.st_mtime;
543 /* keep the same time stamp. */
544 if (stat(src, &st) == 0) {
545 ut.actime = ut.modtime = st.st_mtime;
555 /* --- public functions follow --- */
557 /* NOTE: we start off with at least one node (the root directory). */
558 struct dir *dirinsert( vol, dir )
564 if ((node = dir_insert(vol, dir)))
567 /* recolor the tree. the current node is red. */
568 dir->d_color = DIRTREE_COLOR_RED;
570 /* parent of this node has to be black. if the parent node
571 * is red, then we have a grandparent. */
572 while ((dir != vol->v_root) &&
573 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
574 /* are we on the left tree? */
575 if (dir->d_back == dir->d_back->d_back->d_left) {
576 node = dir->d_back->d_back->d_right; /* get the right node */
577 if (node->d_color == DIRTREE_COLOR_RED) {
578 /* we're red. we need to change to black. */
579 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
580 node->d_color = DIRTREE_COLOR_BLACK;
581 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
582 dir = dir->d_back->d_back; /* finished. go up. */
584 if (dir == dir->d_back->d_right) {
586 dir_leftrotate(vol, dir);
588 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
589 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
590 dir_rightrotate(vol, dir->d_back->d_back);
593 node = dir->d_back->d_back->d_left;
594 if (node->d_color == DIRTREE_COLOR_RED) {
595 /* we're red. we need to change to black. */
596 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
597 node->d_color = DIRTREE_COLOR_BLACK;
598 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
599 dir = dir->d_back->d_back; /* finished. ascend */
601 if (dir == dir->d_back->d_left) {
603 dir_rightrotate(vol, dir);
605 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
606 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
607 dir_leftrotate(vol, dir->d_back->d_back);
612 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
616 /* free everything down. we don't bother to recolor as this is only
617 * called to free the entire tree */
621 if (!dir || (dir == SENTINEL))
624 if ( dir->d_left != SENTINEL ) {
625 dirfree( dir->d_left );
627 if ( dir->d_right != SENTINEL ) {
628 dirfree( dir->d_right );
631 if (dir != SENTINEL) {
638 struct dir *dirnew(const int len)
642 dir = (struct dir *) calloc(1, sizeof( struct dir ));
646 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
651 dir->d_left = dir->d_right = SENTINEL;
652 dir->d_next = dir->d_prev = dir;
657 /* XXX: this needs to be changed to handle path types */
659 cname( vol, dir, cpath )
660 const struct vol *vol;
665 static char path[ MAXPATHLEN + 1];
671 if ( *data++ != 2 ) { /* path type */
674 len = (unsigned char) *data++;
680 if ( !extend && movecwd( vol, dir ) < 0 ) {
686 if ( *data == '\0' ) {
691 while ( *data == '\0' && len > 0 ) {
692 if ( dir->d_parent == NULL ) {
700 /* would this be faster with strlen + strncpy? */
702 while ( *data != '\0' && len > 0 ) {
707 /* short cut bits by chopping off a trailing \0. this also
708 makes the traversal happy w/ filenames at the end of the
715 * Dung Nguyen <ntd@adb.fr>
717 * AFPD cannot handle paths with "::" if the "::" notation is
718 * not at the beginning of the path. The following path will not
719 * be interpreted correctly:
721 * :a:b:::c: (directory c at the same level as directory a) */
729 if ( p != path ) { /* we got something */
733 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
736 cdir = (cdir == dir->d_child->d_prev) ? NULL :
739 if ( cdir == NULL ) {
741 if ( movecwd( vol, dir ) < 0 ) {
744 cdir = extenddir( vol, dir, path );
748 cdir = extenddir( vol, dir, path );
751 if ( cdir == NULL ) {
765 * Move curdir to dir, with a possible chdir()
767 int movecwd( vol, dir)
768 const struct vol *vol;
771 char path[MAXPATHLEN + 1];
776 if ( dir == curdir ) {
779 if ( dir->d_did == DIRDID_ROOT_PARENT) {
783 p = path + sizeof(path) - 1;
786 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
788 u = mtoupath(vol, d->d_name );
795 n = strlen( vol->v_path );
797 strncpy( p, vol->v_path, n );
799 if ( chdir( p ) < 0 ) {
806 int getdirparams(vol, bitmap, upath, dir, st, buf, buflen )
807 const struct vol *vol;
817 char *data, *nameoff = NULL;
820 int bit = 0, isad = 1;
825 memset(&uidgid, 0, sizeof(uidgid));
826 #endif /* FORCE_UIDGID */
828 memset(&ad, 0, sizeof(ad));
831 save_uidgid ( &uidgid );
833 #endif /* FORCE_UIDGID */
835 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
836 DIRBITS | 0777, &ad) < 0 ) {
841 while ( bitmap != 0 ) {
842 while (( bitmap & 1 ) == 0 ) {
850 ad_getattr(&ad, &ashort);
851 } else if (*upath == '.' && strcmp(upath, ".") &&
852 strcmp(upath, "..")) {
853 ashort = htons(ATTRBIT_INVISIBLE);
856 memcpy( data, &ashort, sizeof( ashort ));
857 data += sizeof( ashort );
861 if ( dir->d_did == DIRDID_ROOT) {
862 aint = DIRDID_ROOT_PARENT;
863 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
866 aint = dir->d_parent->d_did;
868 memcpy( data, &aint, sizeof( aint ));
869 data += sizeof( aint );
873 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
874 aint = AD_DATE_FROM_UNIX(st->st_mtime);
875 memcpy( data, &aint, sizeof( aint ));
876 data += sizeof( aint );
880 aint = AD_DATE_FROM_UNIX(st->st_mtime);
881 memcpy( data, &aint, sizeof( aint ));
882 data += sizeof( aint );
886 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
887 aint = AD_DATE_START;
888 memcpy( data, &aint, sizeof( aint ));
889 data += sizeof( aint );
894 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
895 } else { /* no appledouble */
896 memset( data, 0, 32 );
897 /* set default view -- this also gets done in ad_open() */
898 ashort = htons(FINDERINFO_CLOSEDVIEW);
899 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
901 /* dot files are by default invisible */
902 if (*upath == '.' && strcmp(upath, ".") &&
903 strcmp(upath, "..")) {
904 ashort = htons(FINDERINFO_INVISIBLE);
905 memcpy(data + FINDERINFO_FRFLAGOFF,
906 &ashort, sizeof(ashort));
913 if (dir->d_name) /* root of parent can have a null name */
916 memset(data, 0, sizeof(u_int16_t));
917 data += sizeof( u_int16_t );
921 memset(data, 0, sizeof(u_int16_t));
922 data += sizeof( u_int16_t );
926 memcpy( data, &dir->d_did, sizeof( aint ));
927 data += sizeof( aint );
930 case DIRPBIT_OFFCNT :
932 /* this needs to handle current directory access rights */
933 if ((dp = opendir( upath ))) {
934 while (( de = readdir( dp )) != NULL ) {
935 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
938 if (!validupath(vol, de->d_name))
941 /* now check against too long a filename */
942 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
949 ashort = htons( ashort );
950 memcpy( data, &ashort, sizeof( ashort ));
951 data += sizeof( ashort );
955 aint = htonl(st->st_uid);
956 memcpy( data, &aint, sizeof( aint ));
957 data += sizeof( aint );
961 aint = htonl(st->st_gid);
962 memcpy( data, &aint, sizeof( aint ));
963 data += sizeof( aint );
966 case DIRPBIT_ACCESS :
968 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
970 accessmode( upath, &ma, dir );
971 #endif /* HAVE_ACCESS */
972 #endif /* SENDFILE_FLAVOR_LINUX */
973 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
974 afsmode( upath, &ma, dir );
976 *data++ = ma.ma_user;
977 *data++ = ma.ma_world;
978 *data++ = ma.ma_group;
979 *data++ = ma.ma_owner;
982 /* Client has requested the ProDOS information block.
983 Just pass back the same basic block for all
984 directories. <shirsch@ibm.net> */
985 case DIRPBIT_PDINFO : /* ProDOS Info Block */
988 ashort = htons( 0x0200 );
989 memcpy( data, &ashort, sizeof( ashort ));
990 data += sizeof( ashort );
991 memset( data, 0, sizeof( ashort ));
992 data += sizeof( ashort );
997 ad_close( &ad, ADFLAGS_HF );
1000 restore_uidgid ( &uidgid );
1001 #endif /* FORCE_UIDGID */
1002 return( AFPERR_BITMAP );
1008 ashort = htons( data - buf );
1009 memcpy( nameoff, &ashort, sizeof( ashort ));
1011 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1015 memcpy( data, dir->d_name, aint );
1019 ad_close( &ad, ADFLAGS_HF );
1021 *buflen = data - buf;
1025 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1028 int ibuflen, *rbuflen;
1033 u_int16_t vid, bitmap;
1039 memcpy( &vid, ibuf, sizeof( vid ));
1040 ibuf += sizeof( vid );
1042 if (( vol = getvolbyvid( vid )) == NULL ) {
1043 return( AFPERR_PARAM );
1046 if (vol->v_flags & AFPVOL_RO)
1047 return AFPERR_VLOCK;
1049 memcpy( &did, ibuf, sizeof( did ));
1050 ibuf += sizeof( int );
1052 if (( dir = dirsearch( vol, did )) == NULL ) {
1053 return( AFPERR_NOOBJ );
1056 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1057 bitmap = ntohs( bitmap );
1058 ibuf += sizeof( bitmap );
1060 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1061 return( AFPERR_NOOBJ );
1065 * If ibuf is odd, make it even.
1067 if ((u_long)ibuf & 1 ) {
1071 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1072 setvoltime(obj, vol );
1077 int setdirparams(vol, path, bitmap, buf )
1078 const struct vol *vol;
1086 int bit = 0, aint, isad = 1;
1087 u_int16_t ashort, bshort;
1092 memset(&uidgid, 0, sizeof(uidgid));
1093 #endif /* FORCE_UIDGID */
1095 upath = mtoupath(vol, path);
1096 memset(&ad, 0, sizeof(ad));
1098 save_uidgid ( &uidgid );
1099 #endif /* FORCE_UIDGID */
1100 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1101 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1103 * Check to see what we're trying to set. If it's anything
1104 * but ACCESS, UID, or GID, give an error. If it's any of those
1105 * three, we don't need the ad to be open, so just continue.
1107 * note: we also don't need to worry about mdate. also, be quiet
1108 * if we're using the noadouble option.
1110 if (!vol_noadouble(vol) && (bitmap &
1111 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1112 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1114 restore_uidgid ( &uidgid );
1115 #endif /* FORCE_UIDGID */
1116 return AFPERR_ACCESS;
1122 * Check to see if a create was necessary. If it was, we'll want
1123 * to set our name, etc.
1125 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1126 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1127 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1128 ad_getentrylen( &ad, ADEID_NAME ));
1132 while ( bitmap != 0 ) {
1133 while (( bitmap & 1 ) == 0 ) {
1141 memcpy( &ashort, buf, sizeof( ashort ));
1142 ad_getattr(&ad, &bshort);
1143 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1144 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1148 ad_setattr(&ad, bshort);
1150 buf += sizeof( ashort );
1153 case DIRPBIT_CDATE :
1155 memcpy(&aint, buf, sizeof(aint));
1156 ad_setdate(&ad, AD_DATE_CREATE, aint);
1158 buf += sizeof( aint );
1161 case DIRPBIT_MDATE :
1162 memcpy(&aint, buf, sizeof(aint));
1164 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1165 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1167 buf += sizeof( aint );
1170 case DIRPBIT_BDATE :
1172 memcpy(&aint, buf, sizeof(aint));
1173 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1175 buf += sizeof( aint );
1178 case DIRPBIT_FINFO :
1180 * Alright, we admit it, this is *really* sick!
1181 * The 4 bytes that we don't copy, when we're dealing
1182 * with the root of a volume, are the directory's
1183 * location information. This eliminates that annoying
1184 * behavior one sees when mounting above another mount
1188 if ( curdir->d_did == DIRDID_ROOT ) {
1189 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1190 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1192 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1198 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1199 memcpy( &aint, buf, sizeof(aint));
1200 buf += sizeof( aint );
1201 if ( (curdir->d_did == DIRDID_ROOT) &&
1202 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1206 err = AFPERR_ACCESS;
1207 goto setdirparam_done;
1211 goto setdirparam_done;
1214 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1217 goto setdirparam_done;
1222 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1226 err = AFPERR_ACCESS;
1227 goto setdirparam_done;
1231 goto setdirparam_done;
1234 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1240 memcpy( &aint, buf, sizeof( aint ));
1241 buf += sizeof( aint );
1242 if (curdir->d_did == DIRDID_ROOT)
1243 setdeskowner( -1, ntohl(aint) );
1245 #if 0 /* don't error if we can't set the desktop owner. */
1249 err = AFPERR_ACCESS;
1250 goto setdirparam_done;
1254 goto setdirparam_done;
1257 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1260 goto setdirparam_done;
1266 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1270 err = AFPERR_ACCESS;
1271 goto setdirparam_done;
1275 goto setdirparam_done;
1278 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1284 case DIRPBIT_ACCESS :
1285 ma.ma_user = *buf++;
1286 ma.ma_world = *buf++;
1287 ma.ma_group = *buf++;
1288 ma.ma_owner = *buf++;
1290 if (curdir->d_did == DIRDID_ROOT)
1291 setdeskmode(mtoumode( &ma ));
1292 #if 0 /* don't error if we can't set the desktop mode */
1296 err = AFPERR_ACCESS;
1297 goto setdirparam_done;
1300 goto setdirparam_done;
1302 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1305 goto setdirparam_done;
1310 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1311 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1315 err = AFPERR_ACCESS;
1316 goto setdirparam_done;
1319 goto setdirparam_done;
1321 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1323 goto setdirparam_done;
1328 /* Ignore what the client thinks we should do to the
1329 ProDOS information block. Skip over the data and
1330 report nothing amiss. <shirsch@ibm.net> */
1331 case DIRPBIT_PDINFO :
1336 err = AFPERR_BITMAP;
1337 goto setdirparam_done;
1348 ad_flush( &ad, ADFLAGS_HF );
1349 ad_close( &ad, ADFLAGS_HF );
1353 restore_uidgid ( &uidgid );
1354 #endif /* FORCE_UIDGID */
1358 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1361 int ibuflen, *rbuflen;
1373 memset(&uidgid, 0, sizeof(uidgid));
1374 #endif /* FORCE_UIDGID */
1379 memcpy( &vid, ibuf, sizeof( vid ));
1380 ibuf += sizeof( vid );
1381 if (( vol = getvolbyvid( vid )) == NULL ) {
1382 return( AFPERR_PARAM );
1385 if (vol->v_flags & AFPVOL_RO)
1386 return AFPERR_VLOCK;
1388 memcpy( &did, ibuf, sizeof( did ));
1389 ibuf += sizeof( did );
1390 if (( dir = dirsearch( vol, did )) == NULL ) {
1391 return( AFPERR_NOOBJ );
1394 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1395 return( AFPERR_NOOBJ );
1398 /* check for illegal bits */
1399 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1400 strpbrk(path, MSWINDOWS_BADCHARS))
1401 return AFPERR_PARAM;
1403 upath = mtoupath(vol, path);
1405 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1406 return AFPERR_PARAM;
1408 if (!validupath(vol, upath))
1409 return AFPERR_EXIST;
1412 save_uidgid ( &uidgid );
1414 #endif /* FORCE_UIDGID */
1416 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1418 restore_uidgid ( &uidgid );
1419 #endif /* FORCE_UIDGID */
1422 return( AFPERR_NOOBJ );
1424 return( AFPERR_VLOCK );
1426 return( AFPERR_ACCESS );
1428 return( AFPERR_EXIST );
1431 return( AFPERR_DFULL );
1433 return( AFPERR_PARAM );
1437 if (stat(upath, &st) < 0) {
1439 restore_uidgid ( &uidgid );
1440 #endif /* FORCE_UIDGID */
1444 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1445 strlen(upath), &st)) == NULL) {
1447 restore_uidgid ( &uidgid );
1448 #endif /* FORCE_UIDGID */
1452 if ( movecwd( vol, dir ) < 0 ) {
1454 restore_uidgid ( &uidgid );
1455 #endif /* FORCE_UIDGID */
1456 return( AFPERR_PARAM );
1459 memset(&ad, 0, sizeof(ad));
1460 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1461 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1462 if (vol_noadouble(vol))
1463 goto createdir_done;
1465 restore_uidgid ( &uidgid );
1466 #endif /* FORCE_UIDGID */
1467 return( AFPERR_ACCESS );
1470 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1471 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1472 ad_getentrylen( &ad, ADEID_NAME ));
1473 ad_flush( &ad, ADFLAGS_HF );
1474 ad_close( &ad, ADFLAGS_HF );
1477 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1478 *rbuflen = sizeof( u_int32_t );
1479 setvoltime(obj, vol );
1481 restore_uidgid ( &uidgid );
1482 #endif /* FORCE_UIDGID */
1487 int renamedir(src, dst, dir, newparent, newname, noadouble)
1488 char *src, *dst, *newname;
1489 struct dir *dir, *newparent;
1490 const int noadouble;
1497 /* existence check moved to afp_moveandrename */
1498 if ( rename( src, dst ) < 0 ) {
1501 return( AFPERR_NOOBJ );
1503 return( AFPERR_ACCESS );
1505 return AFPERR_VLOCK;
1507 /* tried to move directory into a subdirectory of itself */
1508 return AFPERR_CANTMOVE;
1510 /* this needs to copy and delete. bleah. that means we have
1511 * to deal with entire directory hierarchies. */
1512 if ((err = copydir(src, dst, noadouble)) < 0) {
1516 if ((err = deletedir(src)) < 0)
1520 return( AFPERR_PARAM );
1524 memset(&ad, 0, sizeof(ad));
1525 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1529 len = strlen(newname);
1530 goto renamedir_done;
1532 return( AFPERR_NOOBJ );
1534 return( AFPERR_ACCESS );
1536 return( AFPERR_PARAM );
1539 len = strlen( newname );
1540 ad_setentrylen( &ad, ADEID_NAME, len );
1541 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1542 ad_flush( &ad, ADFLAGS_HF );
1543 ad_close( &ad, ADFLAGS_HF );
1546 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1547 syslog( LOG_ERR, "renamedir: realloc: %m" );
1551 strcpy( dir->d_name, newname );
1553 if (( parent = dir->d_parent ) == NULL ) {
1556 if ( parent == newparent ) {
1560 /* detach from old parent and add to new one. */
1561 dirchildremove(parent, dir);
1562 dir->d_parent = newparent;
1563 dirchildadd(newparent, dir);
1567 #define DOT_APPLEDOUBLE_LEN 13
1568 /* delete an empty directory */
1569 int deletecurdir( vol, path, pathlen )
1570 const struct vol *vol;
1581 memset(&uidgid, 0, sizeof(uidgid));
1582 #endif /* FORCE_UIDGID */
1584 if ( curdir->d_parent == NULL ) {
1585 return( AFPERR_ACCESS );
1588 if ( curdir->d_child != NULL ) {
1589 return( AFPERR_DIRNEMPT );
1595 save_uidgid ( &uidgid );
1597 #endif /* FORCE_UIDGID */
1599 /* delete stray .AppleDouble files. this happens to get .Parent files
1601 if ((dp = opendir(".AppleDouble"))) {
1602 strcpy(path, ".AppleDouble/");
1603 while ((de = readdir(dp))) {
1604 /* skip this and previous directory */
1605 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1608 /* bail if the file exists in the current directory.
1609 * note: this will not fail with dangling symlinks */
1610 if (stat(de->d_name, &st) == 0) {
1613 restore_uidgid ( &uidgid );
1614 #endif /* FORCE_UIDGID */
1615 return AFPERR_DIRNEMPT;
1618 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1619 if (unlink(path) < 0) {
1625 restore_uidgid ( &uidgid );
1626 #endif /* FORCE_UIDGID */
1627 return( AFPERR_ACCESS );
1630 restore_uidgid ( &uidgid );
1631 #endif /* FORCE_UIDGID */
1632 return AFPERR_VLOCK;
1637 restore_uidgid ( &uidgid );
1638 #endif /* FORCE_UIDGID */
1639 return( AFPERR_PARAM );
1646 if ( rmdir( ".AppleDouble" ) < 0 ) {
1652 restore_uidgid ( &uidgid );
1653 #endif /* FORCE_UIDGID */
1654 return( AFPERR_DIRNEMPT );
1657 restore_uidgid ( &uidgid );
1658 #endif /* FORCE_UIDGID */
1659 return AFPERR_VLOCK;
1663 restore_uidgid ( &uidgid );
1664 #endif /* FORCE_UIDGID */
1665 return( AFPERR_ACCESS );
1668 restore_uidgid ( &uidgid );
1669 #endif /* FORCE_UIDGID */
1670 return( AFPERR_PARAM );
1674 /* now get rid of dangling symlinks */
1675 if ((dp = opendir("."))) {
1676 while ((de = readdir(dp))) {
1677 /* skip this and previous directory */
1678 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1681 /* bail if it's not a symlink */
1682 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1684 restore_uidgid ( &uidgid );
1685 #endif /* FORCE_UIDGID */
1686 return AFPERR_DIRNEMPT;
1689 if (unlink(de->d_name) < 0) {
1694 restore_uidgid ( &uidgid );
1695 #endif /* FORCE_UIDGID */
1696 return( AFPERR_ACCESS );
1699 restore_uidgid ( &uidgid );
1700 #endif /* FORCE_UIDGID */
1701 return AFPERR_VLOCK;
1706 restore_uidgid ( &uidgid );
1707 #endif /* FORCE_UIDGID */
1708 return( AFPERR_PARAM );
1715 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1717 restore_uidgid ( &uidgid );
1718 #endif /* FORCE_UIDGID */
1719 return( AFPERR_NOOBJ );
1722 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1726 restore_uidgid ( &uidgid );
1727 #endif /* FORCE_UIDGID */
1728 return( AFPERR_NOOBJ );
1731 restore_uidgid ( &uidgid );
1732 #endif /* FORCE_UIDGID */
1733 return( AFPERR_DIRNEMPT );
1737 restore_uidgid ( &uidgid );
1738 #endif /* FORCE_UIDGID */
1739 return( AFPERR_ACCESS );
1742 restore_uidgid ( &uidgid );
1743 #endif /* FORCE_UIDGID */
1744 return AFPERR_VLOCK;
1747 restore_uidgid ( &uidgid );
1748 #endif /* FORCE_UIDGID */
1749 return( AFPERR_PARAM );
1753 dirchildremove(curdir, fdir);
1754 #if AD_VERSION > AD_VERSION1
1755 cnid_delete(vol->v_db, fdir->d_did);
1756 #endif /* AD_VERSION > AD_VERSION1 */
1757 dir_remove( vol, fdir );
1760 restore_uidgid ( &uidgid );
1761 #endif /* FORCE_UIDGID */
1765 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1768 int ibuflen, *rbuflen;
1777 sfunc = (unsigned char) *ibuf++;
1778 memcpy( &id, ibuf, sizeof( id ));
1783 if (( pw = getpwuid( id )) == NULL ) {
1785 return( AFPERR_NOITEM );
1791 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1793 return( AFPERR_NOITEM );
1800 return( AFPERR_PARAM );
1803 len = strlen( name );
1812 memcpy( rbuf, name, len );
1818 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1821 int ibuflen, *rbuflen;
1829 sfunc = (unsigned char) *ibuf++;
1830 len = (unsigned char) *ibuf++;
1836 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1838 return( AFPERR_NOITEM );
1844 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1846 return( AFPERR_NOITEM );
1852 return( AFPERR_PARAM );
1858 memcpy( rbuf, &id, sizeof( id ));
1859 *rbuflen = sizeof( id );
1863 /* variable DID support */
1864 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1867 int ibuflen, *rbuflen;
1878 /* do nothing as dids are static for the life of the process. */
1882 memcpy(&vid, ibuf, sizeof( vid ));
1883 ibuf += sizeof( vid );
1884 if (( vol = getvolbyvid( vid )) == NULL ) {
1885 return( AFPERR_PARAM );
1888 memcpy( &did, ibuf, sizeof( did ));
1889 ibuf += sizeof( did );
1890 if (( dir = dirsearch( vol, did )) == NULL ) {
1891 return( AFPERR_PARAM );
1894 /* dir_remove -- deletedid */
1900 /* did creation gets done automatically */
1901 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1904 int ibuflen, *rbuflen;
1907 struct dir *dir, *parentdir;
1915 memset(&uidgid, 0, sizeof(uidgid));
1916 #endif /* FORCE_UIDGID */
1921 memcpy(&vid, ibuf, sizeof(vid));
1922 ibuf += sizeof( vid );
1924 if (( vol = getvolbyvid( vid )) == NULL ) {
1925 return( AFPERR_PARAM );
1928 memcpy(&did, ibuf, sizeof(did));
1929 ibuf += sizeof(did);
1931 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1932 return( AFPERR_NOOBJ );
1935 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1936 return( AFPERR_NOOBJ );
1939 /* see if we already have the directory. */
1940 upath = mtoupath(vol, path);
1941 if ( stat( upath, &st ) < 0 ) {
1942 return( AFPERR_NOOBJ );
1945 dir = parentdir->d_child;
1947 if (strdiacasecmp(dir->d_name, path) == 0) {
1948 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1949 *rbuflen = sizeof(dir->d_did);
1952 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1956 save_uidgid ( &uidgid );
1958 #endif /* FORCE_UIDGID */
1960 /* we don't already have a did. add one in. */
1961 if ((dir = adddir(vol, parentdir, path, strlen(path),
1962 upath, strlen(upath), &st)) == NULL) {
1964 restore_uidgid ( &uidgid );
1965 #endif /* FORCE_UIDGID */
1969 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1970 *rbuflen = sizeof(dir->d_did);
1972 restore_uidgid ( &uidgid );
1973 #endif /* FORCE_UIDGID */