2 * $Id: directory.c,v 1.16 2001-08-15 01:37:34 srittau 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>
26 #include <atalk/cnid.h>
34 #endif /* HAVE_FCNTL_H */
39 #include "directory.h"
49 #endif /* FORCE_UIDGID */
53 #define SENTINEL (&sentinel)
54 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
55 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
56 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
57 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
59 /* (from IM: Toolbox Essentials)
60 * dirFinderInfo (DInfo) fields:
62 * frRect 8 folder's window rectangle
64 * frLocation 4 folder's location in window
65 * frView 2 folder's view (default == closedView (256))
67 * extended dirFinderInfo (DXInfo) fields:
68 * frScroll 4 scroll position
69 * frOpenChain: 4 directory ID chain of open folders
70 * frScript: 1 script flag and code
71 * frXFlags: 1 reserved
72 * frComment: 2 comment ID
73 * frPutAway: 4 home directory ID
77 * redid did assignment for directories. now we use red-black trees.
82 const struct vol *vol;
92 if ( did == DIRDID_ROOT_PARENT ) {
94 rootpar.d_did = DIRDID_ROOT_PARENT;
95 rootpar.d_child = vol->v_dir;
100 while ( dir != SENTINEL ) {
101 if (dir->d_did == did)
102 return dir->d_name ? dir : NULL;
103 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
109 /* rotate the tree to the left */
110 static void dir_leftrotate(vol, dir)
114 struct dir *right = dir->d_right;
116 /* whee. move the right's left tree into dir's right tree */
117 dir->d_right = right->d_left;
118 if (right->d_left != SENTINEL)
119 right->d_left->d_back = dir;
121 if (right != SENTINEL) {
122 right->d_back = dir->d_back;
126 if (!dir->d_back) /* no parent. move the right tree to the top. */
128 else if (dir == dir->d_back->d_left) /* we were on the left */
129 dir->d_back->d_left = right;
131 dir->d_back->d_right = right; /* we were on the right */
133 /* re-insert dir on the left tree */
140 /* rotate the tree to the right */
141 static void dir_rightrotate(vol, dir)
145 struct dir *left = dir->d_left;
147 /* whee. move the left's right tree into dir's left tree */
148 dir->d_left = left->d_right;
149 if (left->d_right != SENTINEL)
150 left->d_right->d_back = dir;
152 if (left != SENTINEL) {
153 left->d_back = dir->d_back;
157 if (!dir->d_back) /* no parent. move the left tree to the top. */
159 else if (dir == dir->d_back->d_right) /* we were on the right */
160 dir->d_back->d_right = left;
162 dir->d_back->d_left = left; /* we were on the left */
164 /* re-insert dir on the right tree */
170 /* recolor after a removal */
171 static struct dir *dir_rmrecolor(vol, dir)
177 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
178 /* are we on the left tree? */
179 if (dir == dir->d_back->d_left) {
180 leaf = dir->d_back->d_right; /* get right side */
181 if (leaf->d_color == DIRTREE_COLOR_RED) {
182 /* we're red. we need to change to black. */
183 leaf->d_color = DIRTREE_COLOR_BLACK;
184 dir->d_back->d_color = DIRTREE_COLOR_RED;
185 dir_leftrotate(vol, dir->d_back);
186 leaf = dir->d_back->d_right;
189 /* right leaf has black end nodes */
190 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
191 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
192 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
193 dir = dir->d_back; /* ascend */
195 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
196 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
197 leaf->d_color = DIRTREE_COLOR_RED;
198 dir_rightrotate(vol, leaf);
199 leaf = dir->d_back->d_right;
201 leaf->d_color = dir->d_back->d_color;
202 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
203 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
204 dir_leftrotate(vol, dir->d_back);
207 } else { /* right tree */
208 leaf = dir->d_back->d_left; /* left tree */
209 if (leaf->d_color == DIRTREE_COLOR_RED) {
210 leaf->d_color = DIRTREE_COLOR_BLACK;
211 dir->d_back->d_color = DIRTREE_COLOR_RED;
212 dir_rightrotate(vol, dir->d_back);
213 leaf = dir->d_back->d_left;
216 /* left leaf has black end nodes */
217 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
218 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
219 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
220 dir = dir->d_back; /* ascend */
222 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
223 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
224 leaf->d_color = DIRTREE_COLOR_RED;
225 dir_leftrotate(vol, leaf);
226 leaf = dir->d_back->d_left;
228 leaf->d_color = dir->d_back->d_color;
229 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
230 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
231 dir_rightrotate(vol, dir->d_back);
236 dir->d_color = DIRTREE_COLOR_BLACK;
243 /* remove the node from the tree. this is just like insertion, but
244 * different. actually, it has to worry about a bunch of things that
245 * insertion doesn't care about. */
246 static void dir_remove( vol, dir )
251 struct ofork *of, *last;
252 struct dir *node, *leaf;
253 #endif /* REMOVE_NODES */
255 if (!dir || (dir == SENTINEL))
258 /* i'm not sure if it really helps to delete stuff. */
262 #else /* ! REMOVE_NODES */
264 /* go searching for a node with at most one child */
265 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
269 while (node->d_left != SENTINEL)
274 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
277 leaf->d_back = node->d_back;
280 } else if (node == node->d_back->d_left) { /* left tree */
281 node->d_back->d_left = leaf;
283 node->d_back->d_right = leaf;
286 /* we want to free node, but we also want to free the data in dir.
287 * currently, that's d_name and the directory traversal bits.
288 * we just copy the necessary bits and then fix up all the
289 * various pointers to the directory. needless to say, there are
290 * a bunch of places that store the directory struct. */
292 struct dir save, *tmp;
294 memcpy(&save, dir, sizeof(save));
295 memcpy(dir, node, sizeof(struct dir));
297 /* restore the red-black bits */
298 dir->d_left = save.d_left;
299 dir->d_right = save.d_right;
300 dir->d_back = save.d_back;
301 dir->d_color = save.d_color;
303 if (node == vol->v_dir) {/* we may need to fix up this pointer */
305 rootpar.d_child = vol->v_dir;
307 /* if we aren't the root directory, we have parents and
308 * siblings to worry about */
309 if (dir->d_parent->d_child == node)
310 dir->d_parent->d_child = dir;
311 dir->d_next->d_prev = dir;
312 dir->d_prev->d_next = dir;
315 /* fix up children. */
319 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
322 if (node == curdir) /* another pointer to fixup */
325 /* we also need to fix up oforks. bleah */
326 if ((of = dir->d_ofork)) {
327 last = of->of_d_prev;
330 of = (last == of) ? NULL : of->of_d_next;
334 /* set the node's d_name */
335 node->d_name = save.d_name;
338 if (node->d_color == DIRTREE_COLOR_BLACK)
339 dir_rmrecolor(vol, leaf);
342 #endif /* ! REMOVE_NODES */
346 static struct dir *dir_insert(vol, dir)
347 const struct vol *vol;
353 while (pdir->d_did != dir->d_did ) {
354 if ( pdir->d_did > dir->d_did ) {
355 if ( pdir->d_left == SENTINEL ) {
362 if ( pdir->d_right == SENTINEL ) {
367 pdir = pdir->d_right;
375 * attempt to extend the current dir. tree to include path
376 * as a side-effect, movecwd to that point and return the new dir
380 extenddir( vol, dir, path )
388 p = mtoupath(vol, path );
389 if ( stat( p, &st ) != 0 ) {
392 if (!S_ISDIR(st.st_mode)) {
396 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
401 if ( movecwd( vol, dir ) < 0 ) {
408 static int deletedir(char *dir)
410 char path[MAXPATHLEN + 1];
416 if ((len = strlen(dir)) > sizeof(path))
420 if ((dp = opendir(dir)) == NULL)
426 while ((de = readdir(dp))) {
427 /* skip this and previous directory */
428 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
431 strncpy(path + len, de->d_name, sizeof(path) - len);
432 if (stat(path, &st) == 0) {
433 if (S_ISDIR(st.st_mode)) {
434 if ((err = deletedir(path)) < 0) {
438 } else if (unlink(path) < 0) {
441 continue; /* somebody went and deleted it behind our backs. */
457 /* okay. the directory is empty. delete it. note: we already got rid
459 if (rmdir(dir) < 0) {
463 case ENOTEMPTY : /* should never happen */
464 return( AFPERR_DIRNEMPT );
467 return( AFPERR_ACCESS );
471 return( AFPERR_PARAM );
477 /* do a recursive copy. */
478 static int copydir(char *src, char *dst, int noadouble)
480 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
487 /* doesn't exist or the path is too long. */
488 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
489 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
490 ((dp = opendir(src)) == NULL))
493 /* try to create the destination directory */
494 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
498 return( AFPERR_NOOBJ );
500 return( AFPERR_VLOCK );
503 return( AFPERR_ACCESS );
505 return( AFPERR_EXIST );
508 return( AFPERR_DFULL );
510 return( AFPERR_PARAM );
514 /* set things up to copy */
522 while ((de = readdir(dp))) {
523 /* skip this and previous directory */
524 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
527 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
528 if (stat(spath, &st) == 0) {
529 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
531 if (S_ISDIR(st.st_mode)) {
532 if ((err = copydir(spath, dpath, noadouble)) < 0)
534 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
538 /* keep the same time stamp. */
539 ut.actime = ut.modtime = st.st_mtime;
545 /* keep the same time stamp. */
546 if (stat(src, &st) == 0) {
547 ut.actime = ut.modtime = st.st_mtime;
557 /* --- public functions follow --- */
559 /* NOTE: we start off with at least one node (the root directory). */
560 struct dir *dirinsert( vol, dir )
566 if ((node = dir_insert(vol, dir)))
569 /* recolor the tree. the current node is red. */
570 dir->d_color = DIRTREE_COLOR_RED;
572 /* parent of this node has to be black. if the parent node
573 * is red, then we have a grandparent. */
574 while ((dir != vol->v_root) &&
575 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
576 /* are we on the left tree? */
577 if (dir->d_back == dir->d_back->d_back->d_left) {
578 node = dir->d_back->d_back->d_right; /* get the right node */
579 if (node->d_color == DIRTREE_COLOR_RED) {
580 /* we're red. we need to change to black. */
581 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
582 node->d_color = DIRTREE_COLOR_BLACK;
583 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
584 dir = dir->d_back->d_back; /* finished. go up. */
586 if (dir == dir->d_back->d_right) {
588 dir_leftrotate(vol, dir);
590 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
591 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
592 dir_rightrotate(vol, dir->d_back->d_back);
595 node = dir->d_back->d_back->d_left;
596 if (node->d_color == DIRTREE_COLOR_RED) {
597 /* we're red. we need to change to black. */
598 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
599 node->d_color = DIRTREE_COLOR_BLACK;
600 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
601 dir = dir->d_back->d_back; /* finished. ascend */
603 if (dir == dir->d_back->d_left) {
605 dir_rightrotate(vol, dir);
607 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
608 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
609 dir_leftrotate(vol, dir->d_back->d_back);
614 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
618 /* free everything down. we don't bother to recolor as this is only
619 * called to free the entire tree */
623 if (!dir || (dir == SENTINEL))
626 if ( dir->d_left != SENTINEL ) {
627 dirfree( dir->d_left );
629 if ( dir->d_right != SENTINEL ) {
630 dirfree( dir->d_right );
633 if (dir != SENTINEL) {
640 struct dir *dirnew(const int len)
644 dir = (struct dir *) calloc(1, sizeof( struct dir ));
648 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
653 dir->d_left = dir->d_right = SENTINEL;
654 dir->d_next = dir->d_prev = dir;
659 /* XXX: this needs to be changed to handle path types */
661 cname( vol, dir, cpath )
662 const struct vol *vol;
667 static char path[ MAXPATHLEN + 1];
673 if ( *data++ != 2 ) { /* path type */
676 len = (unsigned char) *data++;
682 if ( !extend && movecwd( vol, dir ) < 0 ) {
688 if ( *data == '\0' ) {
693 while ( *data == '\0' && len > 0 ) {
694 if ( dir->d_parent == NULL ) {
702 /* would this be faster with strlen + strncpy? */
704 while ( *data != '\0' && len > 0 ) {
709 /* short cut bits by chopping off a trailing \0. this also
710 makes the traversal happy w/ filenames at the end of the
717 * Dung Nguyen <ntd@adb.fr>
719 * AFPD cannot handle paths with "::" if the "::" notation is
720 * not at the beginning of the path. The following path will not
721 * be interpreted correctly:
723 * :a:b:::c: (directory c at the same level as directory a) */
731 if ( p != path ) { /* we got something */
735 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
738 cdir = (cdir == dir->d_child->d_prev) ? NULL :
741 if ( cdir == NULL ) {
743 if ( movecwd( vol, dir ) < 0 ) {
746 cdir = extenddir( vol, dir, path );
750 cdir = extenddir( vol, dir, path );
753 if ( cdir == NULL ) {
767 * Move curdir to dir, with a possible chdir()
769 int movecwd( vol, dir)
770 const struct vol *vol;
773 char path[MAXPATHLEN + 1];
778 if ( dir == curdir ) {
781 if ( dir->d_did == DIRDID_ROOT_PARENT) {
785 p = path + sizeof(path) - 1;
788 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
790 u = mtoupath(vol, d->d_name );
797 n = strlen( vol->v_path );
799 strncpy( p, vol->v_path, n );
801 if ( chdir( p ) < 0 ) {
808 int getdirparams(const struct vol *vol,
810 char *upath, struct dir *dir, struct stat *st,
811 char *buf, int *buflen )
815 char *data, *nameoff = NULL;
818 int bit = 0, isad = 1;
823 memset(&uidgid, 0, sizeof(uidgid));
824 #endif /* FORCE_UIDGID */
826 memset(&ad, 0, sizeof(ad));
829 save_uidgid ( &uidgid );
831 #endif /* FORCE_UIDGID */
833 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
834 DIRBITS | 0777, &ad) < 0 ) {
839 while ( bitmap != 0 ) {
840 while (( bitmap & 1 ) == 0 ) {
848 ad_getattr(&ad, &ashort);
849 } else if (*upath == '.' && strcmp(upath, ".") &&
850 strcmp(upath, "..")) {
851 ashort = htons(ATTRBIT_INVISIBLE);
854 memcpy( data, &ashort, sizeof( ashort ));
855 data += sizeof( ashort );
859 if ( dir->d_did == DIRDID_ROOT) {
860 aint = DIRDID_ROOT_PARENT;
861 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
864 aint = dir->d_parent->d_did;
866 memcpy( data, &aint, sizeof( aint ));
867 data += sizeof( aint );
871 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
872 aint = AD_DATE_FROM_UNIX(st->st_mtime);
873 memcpy( data, &aint, sizeof( aint ));
874 data += sizeof( aint );
878 aint = AD_DATE_FROM_UNIX(st->st_mtime);
879 memcpy( data, &aint, sizeof( aint ));
880 data += sizeof( aint );
884 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
885 aint = AD_DATE_START;
886 memcpy( data, &aint, sizeof( aint ));
887 data += sizeof( aint );
892 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
893 } else { /* no appledouble */
894 memset( data, 0, 32 );
895 /* set default view -- this also gets done in ad_open() */
896 ashort = htons(FINDERINFO_CLOSEDVIEW);
897 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
899 /* dot files are by default invisible */
900 if (*upath == '.' && strcmp(upath, ".") &&
901 strcmp(upath, "..")) {
902 ashort = htons(FINDERINFO_INVISIBLE);
903 memcpy(data + FINDERINFO_FRFLAGOFF,
904 &ashort, sizeof(ashort));
911 if (dir->d_name) /* root of parent can have a null name */
914 memset(data, 0, sizeof(u_int16_t));
915 data += sizeof( u_int16_t );
919 memset(data, 0, sizeof(u_int16_t));
920 data += sizeof( u_int16_t );
924 memcpy( data, &dir->d_did, sizeof( aint ));
925 data += sizeof( aint );
928 case DIRPBIT_OFFCNT :
930 /* this needs to handle current directory access rights */
931 if ((dp = opendir( upath ))) {
932 while (( de = readdir( dp )) != NULL ) {
933 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
936 if (!validupath(vol, de->d_name))
939 /* now check against too long a filename */
940 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
947 ashort = htons( ashort );
948 memcpy( data, &ashort, sizeof( ashort ));
949 data += sizeof( ashort );
953 aint = htonl(st->st_uid);
954 memcpy( data, &aint, sizeof( aint ));
955 data += sizeof( aint );
959 aint = htonl(st->st_gid);
960 memcpy( data, &aint, sizeof( aint ));
961 data += sizeof( aint );
964 case DIRPBIT_ACCESS :
966 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
968 accessmode( upath, &ma, dir );
969 #endif /* HAVE_ACCESS */
970 #endif /* SENDFILE_FLAVOR_LINUX */
971 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
972 afsmode( upath, &ma, dir );
974 *data++ = ma.ma_user;
975 *data++ = ma.ma_world;
976 *data++ = ma.ma_group;
977 *data++ = ma.ma_owner;
980 /* Client has requested the ProDOS information block.
981 Just pass back the same basic block for all
982 directories. <shirsch@ibm.net> */
983 case DIRPBIT_PDINFO : /* ProDOS Info Block */
986 ashort = htons( 0x0200 );
987 memcpy( data, &ashort, sizeof( ashort ));
988 data += sizeof( ashort );
989 memset( data, 0, sizeof( ashort ));
990 data += sizeof( ashort );
995 ad_close( &ad, ADFLAGS_HF );
998 restore_uidgid ( &uidgid );
999 #endif /* FORCE_UIDGID */
1000 return( AFPERR_BITMAP );
1006 ashort = htons( data - buf );
1007 memcpy( nameoff, &ashort, sizeof( ashort ));
1009 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1013 memcpy( data, dir->d_name, aint );
1017 ad_close( &ad, ADFLAGS_HF );
1019 *buflen = data - buf;
1023 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1026 int ibuflen, *rbuflen;
1031 u_int16_t vid, bitmap;
1037 memcpy( &vid, ibuf, sizeof( vid ));
1038 ibuf += sizeof( vid );
1040 if (( vol = getvolbyvid( vid )) == NULL ) {
1041 return( AFPERR_PARAM );
1044 if (vol->v_flags & AFPVOL_RO)
1045 return AFPERR_VLOCK;
1047 memcpy( &did, ibuf, sizeof( did ));
1048 ibuf += sizeof( int );
1050 if (( dir = dirsearch( vol, did )) == NULL ) {
1051 return( AFPERR_NOOBJ );
1054 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1055 bitmap = ntohs( bitmap );
1056 ibuf += sizeof( bitmap );
1058 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1059 return( AFPERR_NOOBJ );
1063 * If ibuf is odd, make it even.
1065 if ((u_long)ibuf & 1 ) {
1069 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1070 setvoltime(obj, vol );
1075 int setdirparams(const struct vol *vol,
1076 char *path, u_int16_t bitmap, char *buf )
1082 int bit = 0, aint, isad = 1;
1083 u_int16_t ashort, bshort;
1088 memset(&uidgid, 0, sizeof(uidgid));
1089 #endif /* FORCE_UIDGID */
1091 upath = mtoupath(vol, path);
1092 memset(&ad, 0, sizeof(ad));
1094 save_uidgid ( &uidgid );
1095 #endif /* FORCE_UIDGID */
1096 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1097 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1099 * Check to see what we're trying to set. If it's anything
1100 * but ACCESS, UID, or GID, give an error. If it's any of those
1101 * three, we don't need the ad to be open, so just continue.
1103 * note: we also don't need to worry about mdate. also, be quiet
1104 * if we're using the noadouble option.
1106 if (!vol_noadouble(vol) && (bitmap &
1107 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1108 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1110 restore_uidgid ( &uidgid );
1111 #endif /* FORCE_UIDGID */
1112 return AFPERR_ACCESS;
1118 * Check to see if a create was necessary. If it was, we'll want
1119 * to set our name, etc.
1121 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1122 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1123 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1124 ad_getentrylen( &ad, ADEID_NAME ));
1128 while ( bitmap != 0 ) {
1129 while (( bitmap & 1 ) == 0 ) {
1137 memcpy( &ashort, buf, sizeof( ashort ));
1138 ad_getattr(&ad, &bshort);
1139 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1140 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1144 ad_setattr(&ad, bshort);
1146 buf += sizeof( ashort );
1149 case DIRPBIT_CDATE :
1151 memcpy(&aint, buf, sizeof(aint));
1152 ad_setdate(&ad, AD_DATE_CREATE, aint);
1154 buf += sizeof( aint );
1157 case DIRPBIT_MDATE :
1158 memcpy(&aint, buf, sizeof(aint));
1160 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1161 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1163 buf += sizeof( aint );
1166 case DIRPBIT_BDATE :
1168 memcpy(&aint, buf, sizeof(aint));
1169 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1171 buf += sizeof( aint );
1174 case DIRPBIT_FINFO :
1176 * Alright, we admit it, this is *really* sick!
1177 * The 4 bytes that we don't copy, when we're dealing
1178 * with the root of a volume, are the directory's
1179 * location information. This eliminates that annoying
1180 * behavior one sees when mounting above another mount
1184 if ( curdir->d_did == DIRDID_ROOT ) {
1185 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1186 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1188 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1194 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1195 memcpy( &aint, buf, sizeof(aint));
1196 buf += sizeof( aint );
1197 if ( (curdir->d_did == DIRDID_ROOT) &&
1198 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1202 err = AFPERR_ACCESS;
1203 goto setdirparam_done;
1207 goto setdirparam_done;
1210 syslog( LOG_ERR, "setdirparam: setdeskowner: %s",
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: %s",
1238 memcpy( &aint, buf, sizeof( aint ));
1239 buf += sizeof( aint );
1240 if (curdir->d_did == DIRDID_ROOT)
1241 setdeskowner( -1, ntohl(aint) );
1243 #if 0 /* don't error if we can't set the desktop owner. */
1247 err = AFPERR_ACCESS;
1248 goto setdirparam_done;
1252 goto setdirparam_done;
1255 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1258 goto setdirparam_done;
1264 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1268 err = AFPERR_ACCESS;
1269 goto setdirparam_done;
1273 goto setdirparam_done;
1276 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1283 case DIRPBIT_ACCESS :
1284 ma.ma_user = *buf++;
1285 ma.ma_world = *buf++;
1286 ma.ma_group = *buf++;
1287 ma.ma_owner = *buf++;
1289 if (curdir->d_did == DIRDID_ROOT)
1290 setdeskmode(mtoumode( &ma ));
1291 #if 0 /* don't error if we can't set the desktop mode */
1295 err = AFPERR_ACCESS;
1296 goto setdirparam_done;
1299 goto setdirparam_done;
1301 syslog( LOG_ERR, "setdirparam: setdeskmode: %s",
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: %s",
1324 goto setdirparam_done;
1329 /* Ignore what the client thinks we should do to the
1330 ProDOS information block. Skip over the data and
1331 report nothing amiss. <shirsch@ibm.net> */
1332 case DIRPBIT_PDINFO :
1337 err = AFPERR_BITMAP;
1338 goto setdirparam_done;
1349 ad_flush( &ad, ADFLAGS_HF );
1350 ad_close( &ad, ADFLAGS_HF );
1354 restore_uidgid ( &uidgid );
1355 #endif /* FORCE_UIDGID */
1359 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1362 int ibuflen, *rbuflen;
1374 memset(&uidgid, 0, sizeof(uidgid));
1375 #endif /* FORCE_UIDGID */
1380 memcpy( &vid, ibuf, sizeof( vid ));
1381 ibuf += sizeof( vid );
1382 if (( vol = getvolbyvid( vid )) == NULL ) {
1383 return( AFPERR_PARAM );
1386 if (vol->v_flags & AFPVOL_RO)
1387 return AFPERR_VLOCK;
1389 memcpy( &did, ibuf, sizeof( did ));
1390 ibuf += sizeof( did );
1391 if (( dir = dirsearch( vol, did )) == NULL ) {
1392 return( AFPERR_NOOBJ );
1395 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1396 return( AFPERR_NOOBJ );
1399 /* check for illegal bits */
1400 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1401 strpbrk(path, MSWINDOWS_BADCHARS))
1402 return AFPERR_PARAM;
1404 upath = mtoupath(vol, path);
1406 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1407 return AFPERR_PARAM;
1409 if (!validupath(vol, upath))
1410 return AFPERR_EXIST;
1413 save_uidgid ( &uidgid );
1415 #endif /* FORCE_UIDGID */
1417 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1419 restore_uidgid ( &uidgid );
1420 #endif /* FORCE_UIDGID */
1423 return( AFPERR_NOOBJ );
1425 return( AFPERR_VLOCK );
1427 return( AFPERR_ACCESS );
1429 return( AFPERR_EXIST );
1432 return( AFPERR_DFULL );
1434 return( AFPERR_PARAM );
1438 if (stat(upath, &st) < 0) {
1440 restore_uidgid ( &uidgid );
1441 #endif /* FORCE_UIDGID */
1445 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1446 strlen(upath), &st)) == NULL) {
1448 restore_uidgid ( &uidgid );
1449 #endif /* FORCE_UIDGID */
1453 if ( movecwd( vol, dir ) < 0 ) {
1455 restore_uidgid ( &uidgid );
1456 #endif /* FORCE_UIDGID */
1457 return( AFPERR_PARAM );
1460 memset(&ad, 0, sizeof(ad));
1461 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1462 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1463 if (vol_noadouble(vol))
1464 goto createdir_done;
1466 restore_uidgid ( &uidgid );
1467 #endif /* FORCE_UIDGID */
1468 return( AFPERR_ACCESS );
1471 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1472 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1473 ad_getentrylen( &ad, ADEID_NAME ));
1474 ad_flush( &ad, ADFLAGS_HF );
1475 ad_close( &ad, ADFLAGS_HF );
1478 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1479 *rbuflen = sizeof( u_int32_t );
1480 setvoltime(obj, vol );
1482 restore_uidgid ( &uidgid );
1483 #endif /* FORCE_UIDGID */
1488 int renamedir(src, dst, dir, newparent, newname, noadouble)
1489 char *src, *dst, *newname;
1490 struct dir *dir, *newparent;
1491 const int noadouble;
1498 /* existence check moved to afp_moveandrename */
1499 if ( rename( src, dst ) < 0 ) {
1502 return( AFPERR_NOOBJ );
1504 return( AFPERR_ACCESS );
1506 return AFPERR_VLOCK;
1508 /* tried to move directory into a subdirectory of itself */
1509 return AFPERR_CANTMOVE;
1511 /* this needs to copy and delete. bleah. that means we have
1512 * to deal with entire directory hierarchies. */
1513 if ((err = copydir(src, dst, noadouble)) < 0) {
1517 if ((err = deletedir(src)) < 0)
1521 return( AFPERR_PARAM );
1525 memset(&ad, 0, sizeof(ad));
1526 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1530 len = strlen(newname);
1531 goto renamedir_done;
1533 return( AFPERR_NOOBJ );
1535 return( AFPERR_ACCESS );
1537 return( AFPERR_PARAM );
1540 len = strlen( newname );
1541 ad_setentrylen( &ad, ADEID_NAME, len );
1542 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1543 ad_flush( &ad, ADFLAGS_HF );
1544 ad_close( &ad, ADFLAGS_HF );
1547 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1548 syslog( LOG_ERR, "renamedir: realloc: %s", strerror(errno) );
1552 strcpy( dir->d_name, newname );
1554 if (( parent = dir->d_parent ) == NULL ) {
1557 if ( parent == newparent ) {
1561 /* detach from old parent and add to new one. */
1562 dirchildremove(parent, dir);
1563 dir->d_parent = newparent;
1564 dirchildadd(newparent, dir);
1568 #define DOT_APPLEDOUBLE_LEN 13
1569 /* delete an empty directory */
1570 int deletecurdir( vol, path, pathlen )
1571 const struct vol *vol;
1582 memset(&uidgid, 0, sizeof(uidgid));
1583 #endif /* FORCE_UIDGID */
1585 if ( curdir->d_parent == NULL ) {
1586 return( AFPERR_ACCESS );
1589 if ( curdir->d_child != NULL ) {
1590 return( AFPERR_DIRNEMPT );
1596 save_uidgid ( &uidgid );
1598 #endif /* FORCE_UIDGID */
1600 /* delete stray .AppleDouble files. this happens to get .Parent files
1602 if ((dp = opendir(".AppleDouble"))) {
1603 strcpy(path, ".AppleDouble/");
1604 while ((de = readdir(dp))) {
1605 /* skip this and previous directory */
1606 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1609 /* bail if the file exists in the current directory.
1610 * note: this will not fail with dangling symlinks */
1611 if (stat(de->d_name, &st) == 0) {
1614 restore_uidgid ( &uidgid );
1615 #endif /* FORCE_UIDGID */
1616 return AFPERR_DIRNEMPT;
1619 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1620 if (unlink(path) < 0) {
1626 restore_uidgid ( &uidgid );
1627 #endif /* FORCE_UIDGID */
1628 return( AFPERR_ACCESS );
1631 restore_uidgid ( &uidgid );
1632 #endif /* FORCE_UIDGID */
1633 return AFPERR_VLOCK;
1638 restore_uidgid ( &uidgid );
1639 #endif /* FORCE_UIDGID */
1640 return( AFPERR_PARAM );
1647 if ( rmdir( ".AppleDouble" ) < 0 ) {
1653 restore_uidgid ( &uidgid );
1654 #endif /* FORCE_UIDGID */
1655 return( AFPERR_DIRNEMPT );
1658 restore_uidgid ( &uidgid );
1659 #endif /* FORCE_UIDGID */
1660 return AFPERR_VLOCK;
1664 restore_uidgid ( &uidgid );
1665 #endif /* FORCE_UIDGID */
1666 return( AFPERR_ACCESS );
1669 restore_uidgid ( &uidgid );
1670 #endif /* FORCE_UIDGID */
1671 return( AFPERR_PARAM );
1675 /* now get rid of dangling symlinks */
1676 if ((dp = opendir("."))) {
1677 while ((de = readdir(dp))) {
1678 /* skip this and previous directory */
1679 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1682 /* bail if it's not a symlink */
1683 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1685 restore_uidgid ( &uidgid );
1686 #endif /* FORCE_UIDGID */
1687 return AFPERR_DIRNEMPT;
1690 if (unlink(de->d_name) < 0) {
1695 restore_uidgid ( &uidgid );
1696 #endif /* FORCE_UIDGID */
1697 return( AFPERR_ACCESS );
1700 restore_uidgid ( &uidgid );
1701 #endif /* FORCE_UIDGID */
1702 return AFPERR_VLOCK;
1707 restore_uidgid ( &uidgid );
1708 #endif /* FORCE_UIDGID */
1709 return( AFPERR_PARAM );
1716 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1718 restore_uidgid ( &uidgid );
1719 #endif /* FORCE_UIDGID */
1720 return( AFPERR_NOOBJ );
1723 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1727 restore_uidgid ( &uidgid );
1728 #endif /* FORCE_UIDGID */
1729 return( AFPERR_NOOBJ );
1732 restore_uidgid ( &uidgid );
1733 #endif /* FORCE_UIDGID */
1734 return( AFPERR_DIRNEMPT );
1738 restore_uidgid ( &uidgid );
1739 #endif /* FORCE_UIDGID */
1740 return( AFPERR_ACCESS );
1743 restore_uidgid ( &uidgid );
1744 #endif /* FORCE_UIDGID */
1745 return AFPERR_VLOCK;
1748 restore_uidgid ( &uidgid );
1749 #endif /* FORCE_UIDGID */
1750 return( AFPERR_PARAM );
1754 dirchildremove(curdir, fdir);
1756 cnid_delete(vol->v_db, fdir->d_did);
1757 #endif /* CNID_DB */
1758 dir_remove( vol, fdir );
1761 restore_uidgid ( &uidgid );
1762 #endif /* FORCE_UIDGID */
1766 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1769 int ibuflen, *rbuflen;
1778 sfunc = (unsigned char) *ibuf++;
1779 memcpy( &id, ibuf, sizeof( id ));
1786 if (( pw = getpwuid( id )) == NULL ) {
1788 return( AFPERR_NOITEM );
1794 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1796 return( AFPERR_NOITEM );
1803 return( AFPERR_PARAM );
1806 len = strlen( name );
1815 memcpy( rbuf, name, len );
1821 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1824 int ibuflen, *rbuflen;
1832 sfunc = (unsigned char) *ibuf++;
1833 len = (unsigned char) *ibuf++;
1839 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1841 return( AFPERR_NOITEM );
1847 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1849 return( AFPERR_NOITEM );
1855 return( AFPERR_PARAM );
1861 memcpy( rbuf, &id, sizeof( id ));
1862 *rbuflen = sizeof( id );
1866 /* variable DID support */
1867 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1870 int ibuflen, *rbuflen;
1881 /* do nothing as dids are static for the life of the process. */
1885 memcpy(&vid, ibuf, sizeof( vid ));
1886 ibuf += sizeof( vid );
1887 if (( vol = getvolbyvid( vid )) == NULL ) {
1888 return( AFPERR_PARAM );
1891 memcpy( &did, ibuf, sizeof( did ));
1892 ibuf += sizeof( did );
1893 if (( dir = dirsearch( vol, did )) == NULL ) {
1894 return( AFPERR_PARAM );
1897 /* dir_remove -- deletedid */
1903 /* did creation gets done automatically */
1904 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1907 int ibuflen, *rbuflen;
1910 struct dir *dir, *parentdir;
1918 memset(&uidgid, 0, sizeof(uidgid));
1919 #endif /* FORCE_UIDGID */
1924 memcpy(&vid, ibuf, sizeof(vid));
1925 ibuf += sizeof( vid );
1927 if (( vol = getvolbyvid( vid )) == NULL ) {
1928 return( AFPERR_PARAM );
1931 memcpy(&did, ibuf, sizeof(did));
1932 ibuf += sizeof(did);
1934 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1935 return( AFPERR_NOOBJ );
1938 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1939 return( AFPERR_NOOBJ );
1942 /* see if we already have the directory. */
1943 upath = mtoupath(vol, path);
1944 if ( stat( upath, &st ) < 0 ) {
1945 return( AFPERR_NOOBJ );
1948 dir = parentdir->d_child;
1950 if (strdiacasecmp(dir->d_name, path) == 0) {
1951 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1952 *rbuflen = sizeof(dir->d_did);
1955 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1959 save_uidgid ( &uidgid );
1961 #endif /* FORCE_UIDGID */
1963 /* we don't already have a did. add one in. */
1964 if ((dir = adddir(vol, parentdir, path, strlen(path),
1965 upath, strlen(upath), &st)) == NULL) {
1967 restore_uidgid ( &uidgid );
1968 #endif /* FORCE_UIDGID */
1972 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1973 *rbuflen = sizeof(dir->d_did);
1975 restore_uidgid ( &uidgid );
1976 #endif /* FORCE_UIDGID */