2 * $Id: directory.c,v 1.17 2001-09-04 13:52:45 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>
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 /* check for vetoed filenames */
940 if (veto_file(vol->v_veto, de->d_name))
943 /* now check against too long a filename */
944 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
951 ashort = htons( ashort );
952 memcpy( data, &ashort, sizeof( ashort ));
953 data += sizeof( ashort );
957 aint = htonl(st->st_uid);
958 memcpy( data, &aint, sizeof( aint ));
959 data += sizeof( aint );
963 aint = htonl(st->st_gid);
964 memcpy( data, &aint, sizeof( aint ));
965 data += sizeof( aint );
968 case DIRPBIT_ACCESS :
970 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
972 accessmode( upath, &ma, dir );
973 #endif /* HAVE_ACCESS */
974 #endif /* SENDFILE_FLAVOR_LINUX */
975 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
976 afsmode( upath, &ma, dir );
978 *data++ = ma.ma_user;
979 *data++ = ma.ma_world;
980 *data++ = ma.ma_group;
981 *data++ = ma.ma_owner;
984 /* Client has requested the ProDOS information block.
985 Just pass back the same basic block for all
986 directories. <shirsch@ibm.net> */
987 case DIRPBIT_PDINFO : /* ProDOS Info Block */
990 ashort = htons( 0x0200 );
991 memcpy( data, &ashort, sizeof( ashort ));
992 data += sizeof( ashort );
993 memset( data, 0, sizeof( ashort ));
994 data += sizeof( ashort );
999 ad_close( &ad, ADFLAGS_HF );
1002 restore_uidgid ( &uidgid );
1003 #endif /* FORCE_UIDGID */
1004 return( AFPERR_BITMAP );
1010 ashort = htons( data - buf );
1011 memcpy( nameoff, &ashort, sizeof( ashort ));
1013 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1017 memcpy( data, dir->d_name, aint );
1021 ad_close( &ad, ADFLAGS_HF );
1023 *buflen = data - buf;
1027 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1030 int ibuflen, *rbuflen;
1035 u_int16_t vid, bitmap;
1041 memcpy( &vid, ibuf, sizeof( vid ));
1042 ibuf += sizeof( vid );
1044 if (( vol = getvolbyvid( vid )) == NULL ) {
1045 return( AFPERR_PARAM );
1048 if (vol->v_flags & AFPVOL_RO)
1049 return AFPERR_VLOCK;
1051 memcpy( &did, ibuf, sizeof( did ));
1052 ibuf += sizeof( int );
1054 if (( dir = dirsearch( vol, did )) == NULL ) {
1055 return( AFPERR_NOOBJ );
1058 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1059 bitmap = ntohs( bitmap );
1060 ibuf += sizeof( bitmap );
1062 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1063 return( AFPERR_NOOBJ );
1067 * If ibuf is odd, make it even.
1069 if ((u_long)ibuf & 1 ) {
1073 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1074 setvoltime(obj, vol );
1079 int setdirparams(const struct vol *vol,
1080 char *path, u_int16_t bitmap, char *buf )
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: %s",
1218 goto setdirparam_done;
1223 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1227 err = AFPERR_ACCESS;
1228 goto setdirparam_done;
1232 goto setdirparam_done;
1235 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1242 memcpy( &aint, buf, sizeof( aint ));
1243 buf += sizeof( aint );
1244 if (curdir->d_did == DIRDID_ROOT)
1245 setdeskowner( -1, ntohl(aint) );
1247 #if 0 /* don't error if we can't set the desktop owner. */
1251 err = AFPERR_ACCESS;
1252 goto setdirparam_done;
1256 goto setdirparam_done;
1259 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1262 goto setdirparam_done;
1268 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1272 err = AFPERR_ACCESS;
1273 goto setdirparam_done;
1277 goto setdirparam_done;
1280 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1287 case DIRPBIT_ACCESS :
1288 ma.ma_user = *buf++;
1289 ma.ma_world = *buf++;
1290 ma.ma_group = *buf++;
1291 ma.ma_owner = *buf++;
1293 if (curdir->d_did == DIRDID_ROOT)
1294 setdeskmode(mtoumode( &ma ));
1295 #if 0 /* don't error if we can't set the desktop mode */
1299 err = AFPERR_ACCESS;
1300 goto setdirparam_done;
1303 goto setdirparam_done;
1305 syslog( LOG_ERR, "setdirparam: setdeskmode: %s",
1309 goto setdirparam_done;
1314 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1315 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1319 err = AFPERR_ACCESS;
1320 goto setdirparam_done;
1323 goto setdirparam_done;
1325 syslog( LOG_ERR, "setdirparam: setdirmode: %s",
1328 goto setdirparam_done;
1333 /* Ignore what the client thinks we should do to the
1334 ProDOS information block. Skip over the data and
1335 report nothing amiss. <shirsch@ibm.net> */
1336 case DIRPBIT_PDINFO :
1341 err = AFPERR_BITMAP;
1342 goto setdirparam_done;
1353 ad_flush( &ad, ADFLAGS_HF );
1354 ad_close( &ad, ADFLAGS_HF );
1358 restore_uidgid ( &uidgid );
1359 #endif /* FORCE_UIDGID */
1363 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1366 int ibuflen, *rbuflen;
1378 memset(&uidgid, 0, sizeof(uidgid));
1379 #endif /* FORCE_UIDGID */
1384 memcpy( &vid, ibuf, sizeof( vid ));
1385 ibuf += sizeof( vid );
1386 if (( vol = getvolbyvid( vid )) == NULL ) {
1387 return( AFPERR_PARAM );
1390 if (vol->v_flags & AFPVOL_RO)
1391 return AFPERR_VLOCK;
1393 memcpy( &did, ibuf, sizeof( did ));
1394 ibuf += sizeof( did );
1395 if (( dir = dirsearch( vol, did )) == NULL ) {
1396 return( AFPERR_NOOBJ );
1399 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1400 return( AFPERR_NOOBJ );
1403 /* check for illegal bits */
1404 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1405 strpbrk(path, MSWINDOWS_BADCHARS))
1406 return AFPERR_PARAM;
1408 upath = mtoupath(vol, path);
1410 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1411 return AFPERR_PARAM;
1413 if (!validupath(vol, upath))
1414 return AFPERR_EXIST;
1416 /* check for vetoed filenames */
1417 if (veto_file(vol->v_veto, upath))
1418 return AFPERR_EXIST;
1421 save_uidgid ( &uidgid );
1423 #endif /* FORCE_UIDGID */
1425 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1427 restore_uidgid ( &uidgid );
1428 #endif /* FORCE_UIDGID */
1431 return( AFPERR_NOOBJ );
1433 return( AFPERR_VLOCK );
1435 return( AFPERR_ACCESS );
1437 return( AFPERR_EXIST );
1440 return( AFPERR_DFULL );
1442 return( AFPERR_PARAM );
1446 if (stat(upath, &st) < 0) {
1448 restore_uidgid ( &uidgid );
1449 #endif /* FORCE_UIDGID */
1453 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1454 strlen(upath), &st)) == NULL) {
1456 restore_uidgid ( &uidgid );
1457 #endif /* FORCE_UIDGID */
1461 if ( movecwd( vol, dir ) < 0 ) {
1463 restore_uidgid ( &uidgid );
1464 #endif /* FORCE_UIDGID */
1465 return( AFPERR_PARAM );
1468 memset(&ad, 0, sizeof(ad));
1469 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1470 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1471 if (vol_noadouble(vol))
1472 goto createdir_done;
1474 restore_uidgid ( &uidgid );
1475 #endif /* FORCE_UIDGID */
1476 return( AFPERR_ACCESS );
1479 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1480 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1481 ad_getentrylen( &ad, ADEID_NAME ));
1482 ad_flush( &ad, ADFLAGS_HF );
1483 ad_close( &ad, ADFLAGS_HF );
1486 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1487 *rbuflen = sizeof( u_int32_t );
1488 setvoltime(obj, vol );
1490 restore_uidgid ( &uidgid );
1491 #endif /* FORCE_UIDGID */
1496 int renamedir(src, dst, dir, newparent, newname, noadouble)
1497 char *src, *dst, *newname;
1498 struct dir *dir, *newparent;
1499 const int noadouble;
1506 /* existence check moved to afp_moveandrename */
1507 if ( rename( src, dst ) < 0 ) {
1510 return( AFPERR_NOOBJ );
1512 return( AFPERR_ACCESS );
1514 return AFPERR_VLOCK;
1516 /* tried to move directory into a subdirectory of itself */
1517 return AFPERR_CANTMOVE;
1519 /* this needs to copy and delete. bleah. that means we have
1520 * to deal with entire directory hierarchies. */
1521 if ((err = copydir(src, dst, noadouble)) < 0) {
1525 if ((err = deletedir(src)) < 0)
1529 return( AFPERR_PARAM );
1533 memset(&ad, 0, sizeof(ad));
1534 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1538 len = strlen(newname);
1539 goto renamedir_done;
1541 return( AFPERR_NOOBJ );
1543 return( AFPERR_ACCESS );
1545 return( AFPERR_PARAM );
1548 len = strlen( newname );
1549 ad_setentrylen( &ad, ADEID_NAME, len );
1550 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1551 ad_flush( &ad, ADFLAGS_HF );
1552 ad_close( &ad, ADFLAGS_HF );
1555 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1556 syslog( LOG_ERR, "renamedir: realloc: %s", strerror(errno) );
1560 strcpy( dir->d_name, newname );
1562 if (( parent = dir->d_parent ) == NULL ) {
1565 if ( parent == newparent ) {
1569 /* detach from old parent and add to new one. */
1570 dirchildremove(parent, dir);
1571 dir->d_parent = newparent;
1572 dirchildadd(newparent, dir);
1576 #define DOT_APPLEDOUBLE_LEN 13
1577 /* delete an empty directory */
1578 int deletecurdir( vol, path, pathlen )
1579 const struct vol *vol;
1590 memset(&uidgid, 0, sizeof(uidgid));
1591 #endif /* FORCE_UIDGID */
1593 if ( curdir->d_parent == NULL ) {
1594 return( AFPERR_ACCESS );
1597 if ( curdir->d_child != NULL ) {
1598 return( AFPERR_DIRNEMPT );
1604 save_uidgid ( &uidgid );
1606 #endif /* FORCE_UIDGID */
1608 /* delete stray .AppleDouble files. this happens to get .Parent files
1610 if ((dp = opendir(".AppleDouble"))) {
1611 strcpy(path, ".AppleDouble/");
1612 while ((de = readdir(dp))) {
1613 /* skip this and previous directory */
1614 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1617 /* bail if the file exists in the current directory.
1618 * note: this will not fail with dangling symlinks */
1619 if (stat(de->d_name, &st) == 0) {
1622 restore_uidgid ( &uidgid );
1623 #endif /* FORCE_UIDGID */
1624 return AFPERR_DIRNEMPT;
1627 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1628 if (unlink(path) < 0) {
1634 restore_uidgid ( &uidgid );
1635 #endif /* FORCE_UIDGID */
1636 return( AFPERR_ACCESS );
1639 restore_uidgid ( &uidgid );
1640 #endif /* FORCE_UIDGID */
1641 return AFPERR_VLOCK;
1646 restore_uidgid ( &uidgid );
1647 #endif /* FORCE_UIDGID */
1648 return( AFPERR_PARAM );
1655 if ( rmdir( ".AppleDouble" ) < 0 ) {
1661 restore_uidgid ( &uidgid );
1662 #endif /* FORCE_UIDGID */
1663 return( AFPERR_DIRNEMPT );
1666 restore_uidgid ( &uidgid );
1667 #endif /* FORCE_UIDGID */
1668 return AFPERR_VLOCK;
1672 restore_uidgid ( &uidgid );
1673 #endif /* FORCE_UIDGID */
1674 return( AFPERR_ACCESS );
1677 restore_uidgid ( &uidgid );
1678 #endif /* FORCE_UIDGID */
1679 return( AFPERR_PARAM );
1683 /* now get rid of dangling symlinks */
1684 if ((dp = opendir("."))) {
1685 while ((de = readdir(dp))) {
1686 /* skip this and previous directory */
1687 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1690 /* bail if it's not a symlink */
1691 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1693 restore_uidgid ( &uidgid );
1694 #endif /* FORCE_UIDGID */
1695 return AFPERR_DIRNEMPT;
1698 if (unlink(de->d_name) < 0) {
1703 restore_uidgid ( &uidgid );
1704 #endif /* FORCE_UIDGID */
1705 return( AFPERR_ACCESS );
1708 restore_uidgid ( &uidgid );
1709 #endif /* FORCE_UIDGID */
1710 return AFPERR_VLOCK;
1715 restore_uidgid ( &uidgid );
1716 #endif /* FORCE_UIDGID */
1717 return( AFPERR_PARAM );
1724 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1726 restore_uidgid ( &uidgid );
1727 #endif /* FORCE_UIDGID */
1728 return( AFPERR_NOOBJ );
1731 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1735 restore_uidgid ( &uidgid );
1736 #endif /* FORCE_UIDGID */
1737 return( AFPERR_NOOBJ );
1740 restore_uidgid ( &uidgid );
1741 #endif /* FORCE_UIDGID */
1742 return( AFPERR_DIRNEMPT );
1746 restore_uidgid ( &uidgid );
1747 #endif /* FORCE_UIDGID */
1748 return( AFPERR_ACCESS );
1751 restore_uidgid ( &uidgid );
1752 #endif /* FORCE_UIDGID */
1753 return AFPERR_VLOCK;
1756 restore_uidgid ( &uidgid );
1757 #endif /* FORCE_UIDGID */
1758 return( AFPERR_PARAM );
1762 dirchildremove(curdir, fdir);
1764 cnid_delete(vol->v_db, fdir->d_did);
1765 #endif /* CNID_DB */
1766 dir_remove( vol, fdir );
1769 restore_uidgid ( &uidgid );
1770 #endif /* FORCE_UIDGID */
1774 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1777 int ibuflen, *rbuflen;
1786 sfunc = (unsigned char) *ibuf++;
1787 memcpy( &id, ibuf, sizeof( id ));
1794 if (( pw = getpwuid( id )) == NULL ) {
1796 return( AFPERR_NOITEM );
1802 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1804 return( AFPERR_NOITEM );
1811 return( AFPERR_PARAM );
1814 len = strlen( name );
1823 memcpy( rbuf, name, len );
1829 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1832 int ibuflen, *rbuflen;
1840 sfunc = (unsigned char) *ibuf++;
1841 len = (unsigned char) *ibuf++;
1847 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1849 return( AFPERR_NOITEM );
1855 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1857 return( AFPERR_NOITEM );
1863 return( AFPERR_PARAM );
1869 memcpy( rbuf, &id, sizeof( id ));
1870 *rbuflen = sizeof( id );
1874 /* variable DID support */
1875 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1878 int ibuflen, *rbuflen;
1889 /* do nothing as dids are static for the life of the process. */
1893 memcpy(&vid, ibuf, sizeof( vid ));
1894 ibuf += sizeof( vid );
1895 if (( vol = getvolbyvid( vid )) == NULL ) {
1896 return( AFPERR_PARAM );
1899 memcpy( &did, ibuf, sizeof( did ));
1900 ibuf += sizeof( did );
1901 if (( dir = dirsearch( vol, did )) == NULL ) {
1902 return( AFPERR_PARAM );
1905 /* dir_remove -- deletedid */
1911 /* did creation gets done automatically */
1912 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1915 int ibuflen, *rbuflen;
1918 struct dir *dir, *parentdir;
1926 memset(&uidgid, 0, sizeof(uidgid));
1927 #endif /* FORCE_UIDGID */
1932 memcpy(&vid, ibuf, sizeof(vid));
1933 ibuf += sizeof( vid );
1935 if (( vol = getvolbyvid( vid )) == NULL ) {
1936 return( AFPERR_PARAM );
1939 memcpy(&did, ibuf, sizeof(did));
1940 ibuf += sizeof(did);
1942 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1943 return( AFPERR_NOOBJ );
1946 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1947 return( AFPERR_NOOBJ );
1950 /* see if we already have the directory. */
1951 upath = mtoupath(vol, path);
1952 if ( stat( upath, &st ) < 0 ) {
1953 return( AFPERR_NOOBJ );
1956 dir = parentdir->d_child;
1958 if (strdiacasecmp(dir->d_name, path) == 0) {
1959 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1960 *rbuflen = sizeof(dir->d_did);
1963 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1967 save_uidgid ( &uidgid );
1969 #endif /* FORCE_UIDGID */
1971 /* we don't already have a did. add one in. */
1972 if ((dir = adddir(vol, parentdir, path, strlen(path),
1973 upath, strlen(upath), &st)) == NULL) {
1975 restore_uidgid ( &uidgid );
1976 #endif /* FORCE_UIDGID */
1980 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1981 *rbuflen = sizeof(dir->d_did);
1983 restore_uidgid ( &uidgid );
1984 #endif /* FORCE_UIDGID */