2 * $Id: directory.c,v 1.12 2001-06-20 18:33:04 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 #endif /* FORCE_UIDGID */
827 memset(&ad, 0, sizeof(ad));
830 save_uidgid ( uidgid );
832 #endif /* FORCE_UIDGID */
834 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
835 DIRBITS | 0777, &ad) < 0 ) {
840 while ( bitmap != 0 ) {
841 while (( bitmap & 1 ) == 0 ) {
849 ad_getattr(&ad, &ashort);
850 } else if (*upath == '.' && strcmp(upath, ".") &&
851 strcmp(upath, "..")) {
852 ashort = htons(ATTRBIT_INVISIBLE);
855 memcpy( data, &ashort, sizeof( ashort ));
856 data += sizeof( ashort );
860 if ( dir->d_did == DIRDID_ROOT) {
861 aint = DIRDID_ROOT_PARENT;
862 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
865 aint = dir->d_parent->d_did;
867 memcpy( data, &aint, sizeof( aint ));
868 data += sizeof( aint );
872 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
873 aint = AD_DATE_FROM_UNIX(st->st_mtime);
874 memcpy( data, &aint, sizeof( aint ));
875 data += sizeof( aint );
879 aint = AD_DATE_FROM_UNIX(st->st_mtime);
880 memcpy( data, &aint, sizeof( aint ));
881 data += sizeof( aint );
885 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
886 aint = AD_DATE_START;
887 memcpy( data, &aint, sizeof( aint ));
888 data += sizeof( aint );
893 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
894 } else { /* no appledouble */
895 memset( data, 0, 32 );
896 /* set default view -- this also gets done in ad_open() */
897 ashort = htons(FINDERINFO_CLOSEDVIEW);
898 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
900 /* dot files are by default invisible */
901 if (*upath == '.' && strcmp(upath, ".") &&
902 strcmp(upath, "..")) {
903 ashort = htons(FINDERINFO_INVISIBLE);
904 memcpy(data + FINDERINFO_FRFLAGOFF,
905 &ashort, sizeof(ashort));
912 if (dir->d_name) /* root of parent can have a null name */
915 memset(data, 0, sizeof(u_int16_t));
916 data += sizeof( u_int16_t );
920 memset(data, 0, sizeof(u_int16_t));
921 data += sizeof( u_int16_t );
925 memcpy( data, &dir->d_did, sizeof( aint ));
926 data += sizeof( aint );
929 case DIRPBIT_OFFCNT :
931 /* this needs to handle current directory access rights */
932 if ((dp = opendir( upath ))) {
933 while (( de = readdir( dp )) != NULL ) {
934 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
937 if (!validupath(vol, de->d_name))
940 /* now check against too long a filename */
941 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
948 ashort = htons( ashort );
949 memcpy( data, &ashort, sizeof( ashort ));
950 data += sizeof( ashort );
954 aint = htonl(st->st_uid);
955 memcpy( data, &aint, sizeof( aint ));
956 data += sizeof( aint );
960 aint = htonl(st->st_gid);
961 memcpy( data, &aint, sizeof( aint ));
962 data += sizeof( aint );
965 case DIRPBIT_ACCESS :
967 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
969 accessmode( upath, &ma, dir );
970 #endif /* HAVE_ACCESS */
971 #endif /* SENDFILE_FLAVOR_LINUX */
972 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
973 afsmode( upath, &ma, dir );
975 *data++ = ma.ma_user;
976 *data++ = ma.ma_world;
977 *data++ = ma.ma_group;
978 *data++ = ma.ma_owner;
981 /* Client has requested the ProDOS information block.
982 Just pass back the same basic block for all
983 directories. <shirsch@ibm.net> */
984 case DIRPBIT_PDINFO : /* ProDOS Info Block */
987 ashort = htons( 0x0200 );
988 memcpy( data, &ashort, sizeof( ashort ));
989 data += sizeof( ashort );
990 memset( data, 0, sizeof( ashort ));
991 data += sizeof( ashort );
996 ad_close( &ad, ADFLAGS_HF );
999 restore_uidgid ( uidgid );
1000 #endif /* FORCE_UIDGID */
1001 return( AFPERR_BITMAP );
1007 ashort = htons( data - buf );
1008 memcpy( nameoff, &ashort, sizeof( ashort ));
1010 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1014 memcpy( data, dir->d_name, aint );
1018 ad_close( &ad, ADFLAGS_HF );
1020 *buflen = data - buf;
1024 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1027 int ibuflen, *rbuflen;
1032 u_int16_t vid, bitmap;
1038 memcpy( &vid, ibuf, sizeof( vid ));
1039 ibuf += sizeof( vid );
1041 if (( vol = getvolbyvid( vid )) == NULL ) {
1042 return( AFPERR_PARAM );
1045 if (vol->v_flags & AFPVOL_RO)
1046 return AFPERR_VLOCK;
1048 memcpy( &did, ibuf, sizeof( did ));
1049 ibuf += sizeof( int );
1051 if (( dir = dirsearch( vol, did )) == NULL ) {
1052 return( AFPERR_NOOBJ );
1055 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1056 bitmap = ntohs( bitmap );
1057 ibuf += sizeof( bitmap );
1059 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1060 return( AFPERR_NOOBJ );
1064 * If ibuf is odd, make it even.
1066 if ((u_long)ibuf & 1 ) {
1070 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1071 setvoltime(obj, vol );
1076 int setdirparams(vol, path, bitmap, buf )
1077 const struct vol *vol;
1085 int bit = 0, aint, isad = 1;
1086 u_int16_t ashort, bshort;
1090 #endif /* FORCE_UIDGID */
1092 upath = mtoupath(vol, path);
1093 memset(&ad, 0, sizeof(ad));
1095 save_uidgid ( uidgid );
1096 #endif /* FORCE_UIDGID */
1097 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1098 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1100 * Check to see what we're trying to set. If it's anything
1101 * but ACCESS, UID, or GID, give an error. If it's any of those
1102 * three, we don't need the ad to be open, so just continue.
1104 * note: we also don't need to worry about mdate. also, be quiet
1105 * if we're using the noadouble option.
1107 if (!vol_noadouble(vol) && (bitmap &
1108 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1109 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1111 restore_uidgid ( uidgid );
1112 #endif /* FORCE_UIDGID */
1113 return AFPERR_ACCESS;
1119 * Check to see if a create was necessary. If it was, we'll want
1120 * to set our name, etc.
1122 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1123 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1124 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1125 ad_getentrylen( &ad, ADEID_NAME ));
1129 while ( bitmap != 0 ) {
1130 while (( bitmap & 1 ) == 0 ) {
1138 memcpy( &ashort, buf, sizeof( ashort ));
1139 ad_getattr(&ad, &bshort);
1140 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1141 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1145 ad_setattr(&ad, bshort);
1147 buf += sizeof( ashort );
1150 case DIRPBIT_CDATE :
1152 memcpy(&aint, buf, sizeof(aint));
1153 ad_setdate(&ad, AD_DATE_CREATE, aint);
1155 buf += sizeof( aint );
1158 case DIRPBIT_MDATE :
1159 memcpy(&aint, buf, sizeof(aint));
1161 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1162 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1164 buf += sizeof( aint );
1167 case DIRPBIT_BDATE :
1169 memcpy(&aint, buf, sizeof(aint));
1170 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1172 buf += sizeof( aint );
1175 case DIRPBIT_FINFO :
1177 * Alright, we admit it, this is *really* sick!
1178 * The 4 bytes that we don't copy, when we're dealing
1179 * with the root of a volume, are the directory's
1180 * location information. This eliminates that annoying
1181 * behavior one sees when mounting above another mount
1185 if ( curdir->d_did == DIRDID_ROOT ) {
1186 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1187 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1189 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1195 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1196 memcpy( &aint, buf, sizeof(aint));
1197 buf += sizeof( aint );
1198 if ( (curdir->d_did == DIRDID_ROOT) &&
1199 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1203 err = AFPERR_ACCESS;
1204 goto setdirparam_done;
1208 goto setdirparam_done;
1211 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1214 goto setdirparam_done;
1219 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1223 err = AFPERR_ACCESS;
1224 goto setdirparam_done;
1228 goto setdirparam_done;
1231 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1237 memcpy( &aint, buf, sizeof( aint ));
1238 buf += sizeof( aint );
1239 if (curdir->d_did == DIRDID_ROOT)
1240 setdeskowner( -1, ntohl(aint) );
1242 #if 0 /* don't error if we can't set the desktop owner. */
1246 err = AFPERR_ACCESS;
1247 goto setdirparam_done;
1251 goto setdirparam_done;
1254 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1257 goto setdirparam_done;
1263 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1267 err = AFPERR_ACCESS;
1268 goto setdirparam_done;
1272 goto setdirparam_done;
1275 syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1281 case DIRPBIT_ACCESS :
1282 ma.ma_user = *buf++;
1283 ma.ma_world = *buf++;
1284 ma.ma_group = *buf++;
1285 ma.ma_owner = *buf++;
1287 if (curdir->d_did == DIRDID_ROOT)
1288 setdeskmode(mtoumode( &ma ));
1289 #if 0 /* don't error if we can't set the desktop mode */
1293 err = AFPERR_ACCESS;
1294 goto setdirparam_done;
1297 goto setdirparam_done;
1299 syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1302 goto setdirparam_done;
1307 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1308 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1312 err = AFPERR_ACCESS;
1313 goto setdirparam_done;
1316 goto setdirparam_done;
1318 syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1320 goto setdirparam_done;
1325 /* Ignore what the client thinks we should do to the
1326 ProDOS information block. Skip over the data and
1327 report nothing amiss. <shirsch@ibm.net> */
1328 case DIRPBIT_PDINFO :
1333 err = AFPERR_BITMAP;
1334 goto setdirparam_done;
1345 ad_flush( &ad, ADFLAGS_HF );
1346 ad_close( &ad, ADFLAGS_HF );
1350 restore_uidgid ( uidgid );
1351 #endif /* FORCE_UIDGID */
1355 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1358 int ibuflen, *rbuflen;
1370 /* allocate memory */
1371 uidgidset = malloc(sizeof(uidgidset));
1372 #endif /* FORCE_UIDGID */
1377 memcpy( &vid, ibuf, sizeof( vid ));
1378 ibuf += sizeof( vid );
1379 if (( vol = getvolbyvid( vid )) == NULL ) {
1380 return( AFPERR_PARAM );
1383 if (vol->v_flags & AFPVOL_RO)
1384 return AFPERR_VLOCK;
1386 memcpy( &did, ibuf, sizeof( did ));
1387 ibuf += sizeof( did );
1388 if (( dir = dirsearch( vol, did )) == NULL ) {
1389 return( AFPERR_NOOBJ );
1392 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1393 return( AFPERR_NOOBJ );
1396 /* check for illegal bits */
1397 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1398 strpbrk(path, MSWINDOWS_BADCHARS))
1399 return AFPERR_PARAM;
1401 upath = mtoupath(vol, path);
1403 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1404 return AFPERR_PARAM;
1406 if (!validupath(vol, upath))
1407 return AFPERR_EXIST;
1410 save_uidgid ( uidgid );
1412 #endif /* FORCE_UIDGID */
1414 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1416 restore_uidgid ( uidgid );
1417 #endif /* FORCE_UIDGID */
1420 return( AFPERR_NOOBJ );
1422 return( AFPERR_VLOCK );
1424 return( AFPERR_ACCESS );
1426 return( AFPERR_EXIST );
1429 return( AFPERR_DFULL );
1431 return( AFPERR_PARAM );
1435 if (stat(upath, &st) < 0) {
1437 restore_uidgid ( uidgid );
1438 #endif /* FORCE_UIDGID */
1442 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1443 strlen(upath), &st)) == NULL) {
1445 restore_uidgid ( uidgid );
1446 #endif /* FORCE_UIDGID */
1450 if ( movecwd( vol, dir ) < 0 ) {
1452 restore_uidgid ( uidgid );
1453 #endif /* FORCE_UIDGID */
1454 return( AFPERR_PARAM );
1457 memset(&ad, 0, sizeof(ad));
1458 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1459 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1460 if (vol_noadouble(vol))
1461 goto createdir_done;
1463 restore_uidgid ( uidgid );
1464 #endif /* FORCE_UIDGID */
1465 return( AFPERR_ACCESS );
1468 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1469 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1470 ad_getentrylen( &ad, ADEID_NAME ));
1471 ad_flush( &ad, ADFLAGS_HF );
1472 ad_close( &ad, ADFLAGS_HF );
1475 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1476 *rbuflen = sizeof( u_int32_t );
1477 setvoltime(obj, vol );
1479 restore_uidgid ( uidgid );
1480 #endif /* FORCE_UIDGID */
1485 int renamedir(src, dst, dir, newparent, newname, noadouble)
1486 char *src, *dst, *newname;
1487 struct dir *dir, *newparent;
1488 const int noadouble;
1495 /* existence check moved to afp_moveandrename */
1496 if ( rename( src, dst ) < 0 ) {
1499 return( AFPERR_NOOBJ );
1501 return( AFPERR_ACCESS );
1503 return AFPERR_VLOCK;
1505 /* tried to move directory into a subdirectory of itself */
1506 return AFPERR_CANTMOVE;
1508 /* this needs to copy and delete. bleah. that means we have
1509 * to deal with entire directory hierarchies. */
1510 if ((err = copydir(src, dst, noadouble)) < 0) {
1514 if ((err = deletedir(src)) < 0)
1518 return( AFPERR_PARAM );
1522 memset(&ad, 0, sizeof(ad));
1523 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1527 len = strlen(newname);
1528 goto renamedir_done;
1530 return( AFPERR_NOOBJ );
1532 return( AFPERR_ACCESS );
1534 return( AFPERR_PARAM );
1537 len = strlen( newname );
1538 ad_setentrylen( &ad, ADEID_NAME, len );
1539 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1540 ad_flush( &ad, ADFLAGS_HF );
1541 ad_close( &ad, ADFLAGS_HF );
1544 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1545 syslog( LOG_ERR, "renamedir: realloc: %m" );
1549 strcpy( dir->d_name, newname );
1551 if (( parent = dir->d_parent ) == NULL ) {
1554 if ( parent == newparent ) {
1558 /* detach from old parent and add to new one. */
1559 dirchildremove(parent, dir);
1560 dir->d_parent = newparent;
1561 dirchildadd(newparent, dir);
1565 #define DOT_APPLEDOUBLE_LEN 13
1566 /* delete an empty directory */
1567 int deletecurdir( vol, path, pathlen )
1568 const struct vol *vol;
1579 uidgid = malloc(sizeof(uidgidset));
1580 #endif /* FORCE_UIDGID */
1582 if ( curdir->d_parent == NULL ) {
1583 return( AFPERR_ACCESS );
1586 if ( curdir->d_child != NULL ) {
1587 return( AFPERR_DIRNEMPT );
1593 save_uidgid ( uidgid );
1595 #endif /* FORCE_UIDGID */
1597 /* delete stray .AppleDouble files. this happens to get .Parent files
1599 if ((dp = opendir(".AppleDouble"))) {
1600 strcpy(path, ".AppleDouble/");
1601 while ((de = readdir(dp))) {
1602 /* skip this and previous directory */
1603 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1606 /* bail if the file exists in the current directory.
1607 * note: this will not fail with dangling symlinks */
1608 if (stat(de->d_name, &st) == 0) {
1611 restore_uidgid ( uidgid );
1612 #endif /* FORCE_UIDGID */
1613 return AFPERR_DIRNEMPT;
1616 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1617 if (unlink(path) < 0) {
1623 restore_uidgid ( uidgid );
1624 #endif /* FORCE_UIDGID */
1625 return( AFPERR_ACCESS );
1628 restore_uidgid ( uidgid );
1629 #endif /* FORCE_UIDGID */
1630 return AFPERR_VLOCK;
1635 restore_uidgid ( uidgid );
1636 #endif /* FORCE_UIDGID */
1637 return( AFPERR_PARAM );
1644 if ( rmdir( ".AppleDouble" ) < 0 ) {
1650 restore_uidgid ( uidgid );
1651 #endif /* FORCE_UIDGID */
1652 return( AFPERR_DIRNEMPT );
1655 restore_uidgid ( uidgid );
1656 #endif /* FORCE_UIDGID */
1657 return AFPERR_VLOCK;
1661 restore_uidgid ( uidgid );
1662 #endif /* FORCE_UIDGID */
1663 return( AFPERR_ACCESS );
1666 restore_uidgid ( uidgid );
1667 #endif /* FORCE_UIDGID */
1668 return( AFPERR_PARAM );
1672 /* now get rid of dangling symlinks */
1673 if ((dp = opendir("."))) {
1674 while ((de = readdir(dp))) {
1675 /* skip this and previous directory */
1676 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1679 /* bail if it's not a symlink */
1680 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1682 restore_uidgid ( uidgid );
1683 #endif /* FORCE_UIDGID */
1684 return AFPERR_DIRNEMPT;
1687 if (unlink(de->d_name) < 0) {
1692 restore_uidgid ( uidgid );
1693 #endif /* FORCE_UIDGID */
1694 return( AFPERR_ACCESS );
1697 restore_uidgid ( uidgid );
1698 #endif /* FORCE_UIDGID */
1699 return AFPERR_VLOCK;
1704 restore_uidgid ( uidgid );
1705 #endif /* FORCE_UIDGID */
1706 return( AFPERR_PARAM );
1713 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1715 restore_uidgid ( uidgid );
1716 #endif /* FORCE_UIDGID */
1717 return( AFPERR_NOOBJ );
1720 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1724 restore_uidgid ( uidgid );
1725 #endif /* FORCE_UIDGID */
1726 return( AFPERR_NOOBJ );
1729 restore_uidgid ( uidgid );
1730 #endif /* FORCE_UIDGID */
1731 return( AFPERR_DIRNEMPT );
1735 restore_uidgid ( uidgid );
1736 #endif /* FORCE_UIDGID */
1737 return( AFPERR_ACCESS );
1740 restore_uidgid ( uidgid );
1741 #endif /* FORCE_UIDGID */
1742 return AFPERR_VLOCK;
1745 restore_uidgid ( uidgid );
1746 #endif /* FORCE_UIDGID */
1747 return( AFPERR_PARAM );
1751 dirchildremove(curdir, fdir);
1752 #if AD_VERSION > AD_VERSION1
1753 cnid_delete(vol->v_db, fdir->d_did);
1754 #endif /* AD_VERSION > AD_VERSION1 */
1755 dir_remove( vol, fdir );
1758 restore_uidgid ( uidgid );
1759 #endif /* FORCE_UIDGID */
1763 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1766 int ibuflen, *rbuflen;
1775 sfunc = (unsigned char) *ibuf++;
1776 memcpy( &id, ibuf, sizeof( id ));
1781 if (( pw = getpwuid( id )) == NULL ) {
1783 return( AFPERR_NOITEM );
1789 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1791 return( AFPERR_NOITEM );
1798 return( AFPERR_PARAM );
1801 len = strlen( name );
1810 memcpy( rbuf, name, len );
1816 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1819 int ibuflen, *rbuflen;
1827 sfunc = (unsigned char) *ibuf++;
1828 len = (unsigned char) *ibuf++;
1834 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1836 return( AFPERR_NOITEM );
1842 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1844 return( AFPERR_NOITEM );
1850 return( AFPERR_PARAM );
1856 memcpy( rbuf, &id, sizeof( id ));
1857 *rbuflen = sizeof( id );
1861 /* variable DID support */
1862 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1865 int ibuflen, *rbuflen;
1876 /* do nothing as dids are static for the life of the process. */
1880 memcpy(&vid, ibuf, sizeof( vid ));
1881 ibuf += sizeof( vid );
1882 if (( vol = getvolbyvid( vid )) == NULL ) {
1883 return( AFPERR_PARAM );
1886 memcpy( &did, ibuf, sizeof( did ));
1887 ibuf += sizeof( did );
1888 if (( dir = dirsearch( vol, did )) == NULL ) {
1889 return( AFPERR_PARAM );
1892 /* dir_remove -- deletedid */
1898 /* did creation gets done automatically */
1899 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1902 int ibuflen, *rbuflen;
1905 struct dir *dir, *parentdir;
1913 uidgid = malloc(sizeof(uidgidset));
1914 #endif /* FORCE_UIDGID */
1919 memcpy(&vid, ibuf, sizeof(vid));
1920 ibuf += sizeof( vid );
1922 if (( vol = getvolbyvid( vid )) == NULL ) {
1923 return( AFPERR_PARAM );
1926 memcpy(&did, ibuf, sizeof(did));
1927 ibuf += sizeof(did);
1929 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1930 return( AFPERR_NOOBJ );
1933 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1934 return( AFPERR_NOOBJ );
1937 /* see if we already have the directory. */
1938 upath = mtoupath(vol, path);
1939 if ( stat( upath, &st ) < 0 ) {
1940 return( AFPERR_NOOBJ );
1943 dir = parentdir->d_child;
1945 if (strdiacasecmp(dir->d_name, path) == 0) {
1946 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1947 *rbuflen = sizeof(dir->d_did);
1950 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1954 save_uidgid ( uidgid );
1956 #endif /* FORCE_UIDGID */
1958 /* we don't already have a did. add one in. */
1959 if ((dir = adddir(vol, parentdir, path, strlen(path),
1960 upath, strlen(upath), &st)) == NULL) {
1962 restore_uidgid ( uidgid );
1963 #endif /* FORCE_UIDGID */
1967 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1968 *rbuflen = sizeof(dir->d_did);
1970 restore_uidgid ( uidgid );
1971 #endif /* FORCE_UIDGID */