2 * $Id: directory.c,v 1.19.2.3 2002-01-14 02:50: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 */
41 #else /* STDC_HEADERS */
45 #endif /* HAVE_STRCHR */
46 char *strchr (), *strrchr ();
48 #define memcpy(d,s,n) bcopy ((s), (d), (n))
49 #define memmove(d,s,n) bcopy ((s), (d), (n))
50 #endif /* ! HAVE_MEMCPY */
51 #endif /* STDC_HEADERS */
53 #include "directory.h"
64 #endif /* FORCE_UIDGID */
68 #define SENTINEL (&sentinel)
69 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
70 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
71 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
72 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
74 /* (from IM: Toolbox Essentials)
75 * dirFinderInfo (DInfo) fields:
77 * frRect 8 folder's window rectangle
79 * frLocation 4 folder's location in window
80 * frView 2 folder's view (default == closedView (256))
82 * extended dirFinderInfo (DXInfo) fields:
83 * frScroll 4 scroll position
84 * frOpenChain: 4 directory ID chain of open folders
85 * frScript: 1 script flag and code
86 * frXFlags: 1 reserved
87 * frComment: 2 comment ID
88 * frPutAway: 4 home directory ID
92 * redid did assignment for directories. now we use red-black trees.
97 const struct vol *vol;
103 /* check for 0 did */
107 if ( did == DIRDID_ROOT_PARENT ) {
109 rootpar.d_did = DIRDID_ROOT_PARENT;
110 rootpar.d_child = vol->v_dir;
115 while ( dir != SENTINEL ) {
116 if (dir->d_did == did)
117 return dir->d_name ? dir : NULL;
118 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
124 /* rotate the tree to the left */
125 static void dir_leftrotate(vol, dir)
129 struct dir *right = dir->d_right;
131 /* whee. move the right's left tree into dir's right tree */
132 dir->d_right = right->d_left;
133 if (right->d_left != SENTINEL)
134 right->d_left->d_back = dir;
136 if (right != SENTINEL) {
137 right->d_back = dir->d_back;
141 if (!dir->d_back) /* no parent. move the right tree to the top. */
143 else if (dir == dir->d_back->d_left) /* we were on the left */
144 dir->d_back->d_left = right;
146 dir->d_back->d_right = right; /* we were on the right */
148 /* re-insert dir on the left tree */
155 /* rotate the tree to the right */
156 static void dir_rightrotate(vol, dir)
160 struct dir *left = dir->d_left;
162 /* whee. move the left's right tree into dir's left tree */
163 dir->d_left = left->d_right;
164 if (left->d_right != SENTINEL)
165 left->d_right->d_back = dir;
167 if (left != SENTINEL) {
168 left->d_back = dir->d_back;
172 if (!dir->d_back) /* no parent. move the left tree to the top. */
174 else if (dir == dir->d_back->d_right) /* we were on the right */
175 dir->d_back->d_right = left;
177 dir->d_back->d_left = left; /* we were on the left */
179 /* re-insert dir on the right tree */
185 /* recolor after a removal */
186 static struct dir *dir_rmrecolor(vol, dir)
192 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
193 /* are we on the left tree? */
194 if (dir == dir->d_back->d_left) {
195 leaf = dir->d_back->d_right; /* get right side */
196 if (leaf->d_color == DIRTREE_COLOR_RED) {
197 /* we're red. we need to change to black. */
198 leaf->d_color = DIRTREE_COLOR_BLACK;
199 dir->d_back->d_color = DIRTREE_COLOR_RED;
200 dir_leftrotate(vol, dir->d_back);
201 leaf = dir->d_back->d_right;
204 /* right leaf has black end nodes */
205 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
206 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
207 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
208 dir = dir->d_back; /* ascend */
210 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
211 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
212 leaf->d_color = DIRTREE_COLOR_RED;
213 dir_rightrotate(vol, leaf);
214 leaf = dir->d_back->d_right;
216 leaf->d_color = dir->d_back->d_color;
217 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
218 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
219 dir_leftrotate(vol, dir->d_back);
222 } else { /* right tree */
223 leaf = dir->d_back->d_left; /* left tree */
224 if (leaf->d_color == DIRTREE_COLOR_RED) {
225 leaf->d_color = DIRTREE_COLOR_BLACK;
226 dir->d_back->d_color = DIRTREE_COLOR_RED;
227 dir_rightrotate(vol, dir->d_back);
228 leaf = dir->d_back->d_left;
231 /* left leaf has black end nodes */
232 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
233 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
234 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
235 dir = dir->d_back; /* ascend */
237 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
238 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
239 leaf->d_color = DIRTREE_COLOR_RED;
240 dir_leftrotate(vol, leaf);
241 leaf = dir->d_back->d_left;
243 leaf->d_color = dir->d_back->d_color;
244 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
245 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
246 dir_rightrotate(vol, dir->d_back);
251 dir->d_color = DIRTREE_COLOR_BLACK;
258 /* remove the node from the tree. this is just like insertion, but
259 * different. actually, it has to worry about a bunch of things that
260 * insertion doesn't care about. */
261 static void dir_remove( vol, dir )
266 struct ofork *of, *last;
267 struct dir *node, *leaf;
268 #endif /* REMOVE_NODES */
270 if (!dir || (dir == SENTINEL))
273 /* i'm not sure if it really helps to delete stuff. */
277 #else /* ! REMOVE_NODES */
279 /* go searching for a node with at most one child */
280 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
284 while (node->d_left != SENTINEL)
289 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
292 leaf->d_back = node->d_back;
295 } else if (node == node->d_back->d_left) { /* left tree */
296 node->d_back->d_left = leaf;
298 node->d_back->d_right = leaf;
301 /* we want to free node, but we also want to free the data in dir.
302 * currently, that's d_name and the directory traversal bits.
303 * we just copy the necessary bits and then fix up all the
304 * various pointers to the directory. needless to say, there are
305 * a bunch of places that store the directory struct. */
307 struct dir save, *tmp;
309 memcpy(&save, dir, sizeof(save));
310 memcpy(dir, node, sizeof(struct dir));
312 /* restore the red-black bits */
313 dir->d_left = save.d_left;
314 dir->d_right = save.d_right;
315 dir->d_back = save.d_back;
316 dir->d_color = save.d_color;
318 if (node == vol->v_dir) {/* we may need to fix up this pointer */
320 rootpar.d_child = vol->v_dir;
322 /* if we aren't the root directory, we have parents and
323 * siblings to worry about */
324 if (dir->d_parent->d_child == node)
325 dir->d_parent->d_child = dir;
326 dir->d_next->d_prev = dir;
327 dir->d_prev->d_next = dir;
330 /* fix up children. */
334 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
337 if (node == curdir) /* another pointer to fixup */
340 /* we also need to fix up oforks. bleah */
341 if ((of = dir->d_ofork)) {
342 last = of->of_d_prev;
345 of = (last == of) ? NULL : of->of_d_next;
349 /* set the node's d_name */
350 node->d_name = save.d_name;
353 if (node->d_color == DIRTREE_COLOR_BLACK)
354 dir_rmrecolor(vol, leaf);
357 #endif /* ! REMOVE_NODES */
361 static struct dir *dir_insert(vol, dir)
362 const struct vol *vol;
368 while (pdir->d_did != dir->d_did ) {
369 if ( pdir->d_did > dir->d_did ) {
370 if ( pdir->d_left == SENTINEL ) {
377 if ( pdir->d_right == SENTINEL ) {
382 pdir = pdir->d_right;
390 * attempt to extend the current dir. tree to include path
391 * as a side-effect, movecwd to that point and return the new dir
395 extenddir( vol, dir, path )
403 p = mtoupath(vol, path );
404 if ( stat( p, &st ) != 0 ) {
407 if (!S_ISDIR(st.st_mode)) {
411 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
416 if ( movecwd( vol, dir ) < 0 ) {
423 static int deletedir(char *dir)
425 char path[MAXPATHLEN + 1];
431 if ((len = strlen(dir)) > sizeof(path))
435 if ((dp = opendir(dir)) == NULL)
441 while ((de = readdir(dp))) {
442 /* skip this and previous directory */
443 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
446 strncpy(path + len, de->d_name, sizeof(path) - len);
447 if (stat(path, &st) == 0) {
448 if (S_ISDIR(st.st_mode)) {
449 if ((err = deletedir(path)) < 0) {
453 } else if (unlink(path) < 0) {
456 continue; /* somebody went and deleted it behind our backs. */
474 /* okay. the directory is empty. delete it. note: we already got rid
476 if (rmdir(dir) < 0) {
480 case ENOTEMPTY : /* should never happen */
481 return( AFPERR_DIRNEMPT );
484 return( AFPERR_ACCESS );
488 return( AFPERR_PARAM );
494 /* do a recursive copy. */
495 static int copydir(char *src, char *dst, int noadouble)
497 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
504 /* doesn't exist or the path is too long. */
505 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
506 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
507 ((dp = opendir(src)) == NULL))
510 /* try to create the destination directory */
511 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
515 return( AFPERR_NOOBJ );
517 return( AFPERR_VLOCK );
520 return( AFPERR_ACCESS );
522 return( AFPERR_EXIST );
525 return( AFPERR_DFULL );
527 return( AFPERR_PARAM );
531 /* set things up to copy */
539 while ((de = readdir(dp))) {
540 /* skip this and previous directory */
541 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
544 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
545 if (stat(spath, &st) == 0) {
546 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
548 if (S_ISDIR(st.st_mode)) {
549 if ((err = copydir(spath, dpath, noadouble)) < 0)
551 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
555 /* keep the same time stamp. */
556 ut.actime = ut.modtime = st.st_mtime;
562 /* keep the same time stamp. */
563 if (stat(src, &st) == 0) {
564 ut.actime = ut.modtime = st.st_mtime;
574 /* --- public functions follow --- */
576 /* NOTE: we start off with at least one node (the root directory). */
577 struct dir *dirinsert( vol, dir )
583 if ((node = dir_insert(vol, dir)))
586 /* recolor the tree. the current node is red. */
587 dir->d_color = DIRTREE_COLOR_RED;
589 /* parent of this node has to be black. if the parent node
590 * is red, then we have a grandparent. */
591 while ((dir != vol->v_root) &&
592 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
593 /* are we on the left tree? */
594 if (dir->d_back == dir->d_back->d_back->d_left) {
595 node = dir->d_back->d_back->d_right; /* get the right node */
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. go up. */
603 if (dir == dir->d_back->d_right) {
605 dir_leftrotate(vol, dir);
607 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
608 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
609 dir_rightrotate(vol, dir->d_back->d_back);
612 node = dir->d_back->d_back->d_left;
613 if (node->d_color == DIRTREE_COLOR_RED) {
614 /* we're red. we need to change to black. */
615 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
616 node->d_color = DIRTREE_COLOR_BLACK;
617 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
618 dir = dir->d_back->d_back; /* finished. ascend */
620 if (dir == dir->d_back->d_left) {
622 dir_rightrotate(vol, dir);
624 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
625 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
626 dir_leftrotate(vol, dir->d_back->d_back);
631 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
635 /* free everything down. we don't bother to recolor as this is only
636 * called to free the entire tree */
640 if (!dir || (dir == SENTINEL))
643 if ( dir->d_left != SENTINEL ) {
644 dirfree( dir->d_left );
646 if ( dir->d_right != SENTINEL ) {
647 dirfree( dir->d_right );
650 if (dir != SENTINEL) {
657 struct dir *dirnew(const int len)
661 dir = (struct dir *) calloc(1, sizeof( struct dir ));
665 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
670 dir->d_left = dir->d_right = SENTINEL;
671 dir->d_next = dir->d_prev = dir;
676 /* XXX: this needs to be changed to handle path types */
678 cname( vol, dir, cpath )
679 const struct vol *vol;
684 static char path[ MAXPATHLEN + 1];
690 if ( *data++ != 2 ) { /* path type */
693 len = (unsigned char) *data++;
699 if ( !extend && movecwd( vol, dir ) < 0 ) {
705 if ( *data == '\0' ) {
710 while ( *data == '\0' && len > 0 ) {
711 if ( dir->d_parent == NULL ) {
719 /* would this be faster with strlen + strncpy? */
721 while ( *data != '\0' && len > 0 ) {
726 /* short cut bits by chopping off a trailing \0. this also
727 makes the traversal happy w/ filenames at the end of the
734 * Dung Nguyen <ntd@adb.fr>
736 * AFPD cannot handle paths with "::" if the "::" notation is
737 * not at the beginning of the path. The following path will not
738 * be interpreted correctly:
740 * :a:b:::c: (directory c at the same level as directory a) */
748 if ( p != path ) { /* we got something */
752 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
755 cdir = (cdir == dir->d_child->d_prev) ? NULL :
758 if ( cdir == NULL ) {
760 if ( movecwd( vol, dir ) < 0 ) {
763 cdir = extenddir( vol, dir, path );
767 cdir = extenddir( vol, dir, path );
770 if ( cdir == NULL ) {
784 * Move curdir to dir, with a possible chdir()
786 int movecwd( vol, dir)
787 const struct vol *vol;
790 char path[MAXPATHLEN + 1];
795 if ( dir == curdir ) {
798 if ( dir->d_did == DIRDID_ROOT_PARENT) {
802 p = path + sizeof(path) - 1;
805 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
807 u = mtoupath(vol, d->d_name );
814 n = strlen( vol->v_path );
816 strncpy( p, vol->v_path, n );
818 if ( chdir( p ) < 0 ) {
825 int getdirparams(const struct vol *vol,
827 char *upath, struct dir *dir, struct stat *st,
828 char *buf, int *buflen )
832 char *data, *nameoff = NULL;
835 int bit = 0, isad = 1;
840 memset(&uidgid, 0, sizeof(uidgid));
841 #endif /* FORCE_UIDGID */
843 memset(&ad, 0, sizeof(ad));
846 save_uidgid ( &uidgid );
848 #endif /* FORCE_UIDGID */
850 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
851 DIRBITS | 0777, &ad) < 0 ) {
856 while ( bitmap != 0 ) {
857 while (( bitmap & 1 ) == 0 ) {
865 ad_getattr(&ad, &ashort);
866 } else if (*upath == '.' && strcmp(upath, ".") &&
867 strcmp(upath, "..")) {
868 ashort = htons(ATTRBIT_INVISIBLE);
871 memcpy( data, &ashort, sizeof( ashort ));
872 data += sizeof( ashort );
876 if ( dir->d_did == DIRDID_ROOT) {
877 aint = DIRDID_ROOT_PARENT;
878 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
881 aint = dir->d_parent->d_did;
883 memcpy( data, &aint, sizeof( aint ));
884 data += sizeof( aint );
888 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
889 aint = AD_DATE_FROM_UNIX(st->st_mtime);
890 memcpy( data, &aint, sizeof( aint ));
891 data += sizeof( aint );
895 aint = AD_DATE_FROM_UNIX(st->st_mtime);
896 memcpy( data, &aint, sizeof( aint ));
897 data += sizeof( aint );
901 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
902 aint = AD_DATE_START;
903 memcpy( data, &aint, sizeof( aint ));
904 data += sizeof( aint );
909 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
910 } else { /* no appledouble */
911 memset( data, 0, 32 );
912 /* set default view -- this also gets done in ad_open() */
913 ashort = htons(FINDERINFO_CLOSEDVIEW);
914 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
916 /* dot files are by default invisible */
917 if (*upath == '.' && strcmp(upath, ".") &&
918 strcmp(upath, "..")) {
919 ashort = htons(FINDERINFO_INVISIBLE);
920 memcpy(data + FINDERINFO_FRFLAGOFF,
921 &ashort, sizeof(ashort));
928 if (dir->d_name) /* root of parent can have a null name */
931 memset(data, 0, sizeof(u_int16_t));
932 data += sizeof( u_int16_t );
936 memset(data, 0, sizeof(u_int16_t));
937 data += sizeof( u_int16_t );
941 memcpy( data, &dir->d_did, sizeof( aint ));
942 data += sizeof( aint );
945 case DIRPBIT_OFFCNT :
947 /* this needs to handle current directory access rights */
948 if ((dp = opendir( upath ))) {
949 while (( de = readdir( dp )) != NULL ) {
950 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
953 if (!validupath(vol, de->d_name))
956 /* check for vetoed filenames */
957 if (veto_file(vol->v_veto, de->d_name))
960 /* now check against too long a filename */
961 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
968 ashort = htons( ashort );
969 memcpy( data, &ashort, sizeof( ashort ));
970 data += sizeof( ashort );
974 aint = htonl(st->st_uid);
975 memcpy( data, &aint, sizeof( aint ));
976 data += sizeof( aint );
980 aint = htonl(st->st_gid);
981 memcpy( data, &aint, sizeof( aint ));
982 data += sizeof( aint );
985 case DIRPBIT_ACCESS :
987 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
989 accessmode( upath, &ma, dir );
990 #endif /* HAVE_ACCESS */
991 #endif /* SENDFILE_FLAVOR_LINUX */
992 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
993 afsmode( upath, &ma, dir );
995 *data++ = ma.ma_user;
996 *data++ = ma.ma_world;
997 *data++ = ma.ma_group;
998 *data++ = ma.ma_owner;
1001 /* Client has requested the ProDOS information block.
1002 Just pass back the same basic block for all
1003 directories. <shirsch@ibm.net> */
1004 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1007 ashort = htons( 0x0200 );
1008 memcpy( data, &ashort, sizeof( ashort ));
1009 data += sizeof( ashort );
1010 memset( data, 0, sizeof( ashort ));
1011 data += sizeof( ashort );
1016 ad_close( &ad, ADFLAGS_HF );
1019 restore_uidgid ( &uidgid );
1020 #endif /* FORCE_UIDGID */
1021 return( AFPERR_BITMAP );
1027 ashort = htons( data - buf );
1028 memcpy( nameoff, &ashort, sizeof( ashort ));
1030 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1034 memcpy( data, dir->d_name, aint );
1038 ad_close( &ad, ADFLAGS_HF );
1040 *buflen = data - buf;
1044 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1047 int ibuflen, *rbuflen;
1052 u_int16_t vid, bitmap;
1058 memcpy( &vid, ibuf, sizeof( vid ));
1059 ibuf += sizeof( vid );
1061 if (( vol = getvolbyvid( vid )) == NULL ) {
1062 return( AFPERR_PARAM );
1065 if (vol->v_flags & AFPVOL_RO)
1066 return AFPERR_VLOCK;
1068 memcpy( &did, ibuf, sizeof( did ));
1069 ibuf += sizeof( int );
1071 if (( dir = dirsearch( vol, did )) == NULL ) {
1072 return( AFPERR_NOOBJ );
1075 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1076 bitmap = ntohs( bitmap );
1077 ibuf += sizeof( bitmap );
1079 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1080 return( AFPERR_NOOBJ );
1084 * If ibuf is odd, make it even.
1086 if ((u_long)ibuf & 1 ) {
1090 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1091 setvoltime(obj, vol );
1096 int setdirparams(const struct vol *vol,
1097 char *path, u_int16_t bitmap, char *buf )
1103 int bit = 0, aint, isad = 1;
1104 u_int16_t ashort, bshort;
1109 memset(&uidgid, 0, sizeof(uidgid));
1110 #endif /* FORCE_UIDGID */
1112 upath = mtoupath(vol, path);
1113 memset(&ad, 0, sizeof(ad));
1115 save_uidgid ( &uidgid );
1116 #endif /* FORCE_UIDGID */
1117 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1118 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1120 * Check to see what we're trying to set. If it's anything
1121 * but ACCESS, UID, or GID, give an error. If it's any of those
1122 * three, we don't need the ad to be open, so just continue.
1124 * note: we also don't need to worry about mdate. also, be quiet
1125 * if we're using the noadouble option.
1127 if (!vol_noadouble(vol) && (bitmap &
1128 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1129 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1131 restore_uidgid ( &uidgid );
1132 #endif /* FORCE_UIDGID */
1133 return AFPERR_ACCESS;
1139 * Check to see if a create was necessary. If it was, we'll want
1140 * to set our name, etc.
1142 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1143 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1144 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1145 ad_getentrylen( &ad, ADEID_NAME ));
1149 while ( bitmap != 0 ) {
1150 while (( bitmap & 1 ) == 0 ) {
1158 memcpy( &ashort, buf, sizeof( ashort ));
1159 ad_getattr(&ad, &bshort);
1160 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1161 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1165 ad_setattr(&ad, bshort);
1167 buf += sizeof( ashort );
1170 case DIRPBIT_CDATE :
1172 memcpy(&aint, buf, sizeof(aint));
1173 ad_setdate(&ad, AD_DATE_CREATE, aint);
1175 buf += sizeof( aint );
1178 case DIRPBIT_MDATE :
1179 memcpy(&aint, buf, sizeof(aint));
1181 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1182 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1184 buf += sizeof( aint );
1187 case DIRPBIT_BDATE :
1189 memcpy(&aint, buf, sizeof(aint));
1190 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1192 buf += sizeof( aint );
1195 case DIRPBIT_FINFO :
1197 * Alright, we admit it, this is *really* sick!
1198 * The 4 bytes that we don't copy, when we're dealing
1199 * with the root of a volume, are the directory's
1200 * location information. This eliminates that annoying
1201 * behavior one sees when mounting above another mount
1205 if ( curdir->d_did == DIRDID_ROOT ) {
1206 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1207 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1209 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1215 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1216 memcpy( &aint, buf, sizeof(aint));
1217 buf += sizeof( aint );
1218 if ( (curdir->d_did == DIRDID_ROOT) &&
1219 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1223 err = AFPERR_ACCESS;
1224 goto setdirparam_done;
1228 goto setdirparam_done;
1231 syslog( LOG_ERR, "setdirparam: setdeskowner: %s",
1235 goto setdirparam_done;
1240 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1244 err = AFPERR_ACCESS;
1245 goto setdirparam_done;
1249 goto setdirparam_done;
1252 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1259 memcpy( &aint, buf, sizeof( aint ));
1260 buf += sizeof( aint );
1261 if (curdir->d_did == DIRDID_ROOT)
1262 setdeskowner( -1, ntohl(aint) );
1264 #if 0 /* don't error if we can't set the desktop owner. */
1268 err = AFPERR_ACCESS;
1269 goto setdirparam_done;
1273 goto setdirparam_done;
1276 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1279 goto setdirparam_done;
1285 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1289 err = AFPERR_ACCESS;
1290 goto setdirparam_done;
1294 goto setdirparam_done;
1297 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1304 case DIRPBIT_ACCESS :
1305 ma.ma_user = *buf++;
1306 ma.ma_world = *buf++;
1307 ma.ma_group = *buf++;
1308 ma.ma_owner = *buf++;
1310 if (curdir->d_did == DIRDID_ROOT)
1311 setdeskmode(mtoumode( &ma ));
1312 #if 0 /* don't error if we can't set the desktop mode */
1316 err = AFPERR_ACCESS;
1317 goto setdirparam_done;
1320 goto setdirparam_done;
1322 syslog( LOG_ERR, "setdirparam: setdeskmode: %s",
1326 goto setdirparam_done;
1331 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1332 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1336 err = AFPERR_ACCESS;
1337 goto setdirparam_done;
1340 goto setdirparam_done;
1342 syslog( LOG_ERR, "setdirparam: setdirmode: %s",
1345 goto setdirparam_done;
1350 /* Ignore what the client thinks we should do to the
1351 ProDOS information block. Skip over the data and
1352 report nothing amiss. <shirsch@ibm.net> */
1353 case DIRPBIT_PDINFO :
1358 err = AFPERR_BITMAP;
1359 goto setdirparam_done;
1370 ad_flush( &ad, ADFLAGS_HF );
1371 ad_close( &ad, ADFLAGS_HF );
1375 restore_uidgid ( &uidgid );
1376 #endif /* FORCE_UIDGID */
1380 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1383 int ibuflen, *rbuflen;
1395 memset(&uidgid, 0, sizeof(uidgid));
1396 #endif /* FORCE_UIDGID */
1401 memcpy( &vid, ibuf, sizeof( vid ));
1402 ibuf += sizeof( vid );
1403 if (( vol = getvolbyvid( vid )) == NULL ) {
1404 return( AFPERR_PARAM );
1407 if (vol->v_flags & AFPVOL_RO)
1408 return AFPERR_VLOCK;
1410 memcpy( &did, ibuf, sizeof( did ));
1411 ibuf += sizeof( did );
1412 if (( dir = dirsearch( vol, did )) == NULL ) {
1413 return( AFPERR_NOOBJ );
1416 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1419 return( AFPERR_ACCESS );
1421 return( AFPERR_EXIST );
1423 return( AFPERR_NOOBJ );
1427 /* check for illegal bits */
1428 if (!wincheck(vol, path))
1429 return AFPERR_PARAM;
1431 upath = mtoupath(vol, path);
1433 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1434 return AFPERR_PARAM;
1436 if (!validupath(vol, upath))
1437 return AFPERR_EXIST;
1439 /* check for vetoed filenames */
1440 if (veto_file(vol->v_veto, upath))
1441 return AFPERR_EXIST;
1444 save_uidgid ( &uidgid );
1446 #endif /* FORCE_UIDGID */
1448 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1450 restore_uidgid ( &uidgid );
1451 #endif /* FORCE_UIDGID */
1454 return( AFPERR_NOOBJ );
1456 return( AFPERR_VLOCK );
1458 return( AFPERR_ACCESS );
1460 return( AFPERR_EXIST );
1463 return( AFPERR_DFULL );
1465 return( AFPERR_PARAM );
1469 if (stat(upath, &st) < 0) {
1471 restore_uidgid ( &uidgid );
1472 #endif /* FORCE_UIDGID */
1476 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1477 strlen(upath), &st)) == NULL) {
1479 restore_uidgid ( &uidgid );
1480 #endif /* FORCE_UIDGID */
1484 if ( movecwd( vol, dir ) < 0 ) {
1486 restore_uidgid ( &uidgid );
1487 #endif /* FORCE_UIDGID */
1488 return( AFPERR_PARAM );
1491 memset(&ad, 0, sizeof(ad));
1492 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1493 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1494 if (vol_noadouble(vol))
1495 goto createdir_done;
1497 restore_uidgid ( &uidgid );
1498 #endif /* FORCE_UIDGID */
1499 return( AFPERR_ACCESS );
1502 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1503 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1504 ad_getentrylen( &ad, ADEID_NAME ));
1505 ad_flush( &ad, ADFLAGS_HF );
1506 ad_close( &ad, ADFLAGS_HF );
1509 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1510 *rbuflen = sizeof( u_int32_t );
1511 setvoltime(obj, vol );
1513 restore_uidgid ( &uidgid );
1514 #endif /* FORCE_UIDGID */
1519 int renamedir(src, dst, dir, newparent, newname, noadouble)
1520 char *src, *dst, *newname;
1521 struct dir *dir, *newparent;
1522 const int noadouble;
1529 /* existence check moved to afp_moveandrename */
1530 if ( rename( src, dst ) < 0 ) {
1533 return( AFPERR_NOOBJ );
1535 return( AFPERR_ACCESS );
1537 return AFPERR_VLOCK;
1539 /* tried to move directory into a subdirectory of itself */
1540 return AFPERR_CANTMOVE;
1542 /* this needs to copy and delete. bleah. that means we have
1543 * to deal with entire directory hierarchies. */
1544 if ((err = copydir(src, dst, noadouble)) < 0) {
1548 if ((err = deletedir(src)) < 0)
1552 return( AFPERR_PARAM );
1556 memset(&ad, 0, sizeof(ad));
1557 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1561 len = strlen(newname);
1562 goto renamedir_done;
1564 return( AFPERR_NOOBJ );
1566 return( AFPERR_ACCESS );
1568 return( AFPERR_PARAM );
1571 len = strlen( newname );
1572 ad_setentrylen( &ad, ADEID_NAME, len );
1573 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1574 ad_flush( &ad, ADFLAGS_HF );
1575 ad_close( &ad, ADFLAGS_HF );
1578 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1579 syslog( LOG_ERR, "renamedir: realloc: %s", strerror(errno) );
1583 strcpy( dir->d_name, newname );
1585 if (( parent = dir->d_parent ) == NULL ) {
1588 if ( parent == newparent ) {
1592 /* detach from old parent and add to new one. */
1593 dirchildremove(parent, dir);
1594 dir->d_parent = newparent;
1595 dirchildadd(newparent, dir);
1599 #define DOT_APPLEDOUBLE_LEN 13
1600 /* delete an empty directory */
1601 int deletecurdir( vol, path, pathlen )
1602 const struct vol *vol;
1613 memset(&uidgid, 0, sizeof(uidgid));
1614 #endif /* FORCE_UIDGID */
1616 if ( curdir->d_parent == NULL ) {
1617 return( AFPERR_ACCESS );
1620 if ( curdir->d_child != NULL ) {
1621 return( AFPERR_DIRNEMPT );
1627 save_uidgid ( &uidgid );
1629 #endif /* FORCE_UIDGID */
1631 /* delete stray .AppleDouble files. this happens to get .Parent files
1633 if ((dp = opendir(".AppleDouble"))) {
1634 strcpy(path, ".AppleDouble/");
1635 while ((de = readdir(dp))) {
1636 /* skip this and previous directory */
1637 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1640 /* bail if the file exists in the current directory.
1641 * note: this will not fail with dangling symlinks */
1642 if (stat(de->d_name, &st) == 0) {
1645 restore_uidgid ( &uidgid );
1646 #endif /* FORCE_UIDGID */
1647 return AFPERR_DIRNEMPT;
1650 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1651 if (unlink(path) < 0) {
1657 restore_uidgid ( &uidgid );
1658 #endif /* FORCE_UIDGID */
1659 return( AFPERR_ACCESS );
1662 restore_uidgid ( &uidgid );
1663 #endif /* FORCE_UIDGID */
1664 return AFPERR_VLOCK;
1669 restore_uidgid ( &uidgid );
1670 #endif /* FORCE_UIDGID */
1671 return( AFPERR_PARAM );
1678 if ( rmdir( ".AppleDouble" ) < 0 ) {
1684 restore_uidgid ( &uidgid );
1685 #endif /* FORCE_UIDGID */
1686 return( AFPERR_DIRNEMPT );
1689 restore_uidgid ( &uidgid );
1690 #endif /* FORCE_UIDGID */
1691 return AFPERR_VLOCK;
1695 restore_uidgid ( &uidgid );
1696 #endif /* FORCE_UIDGID */
1697 return( AFPERR_ACCESS );
1700 restore_uidgid ( &uidgid );
1701 #endif /* FORCE_UIDGID */
1702 return( AFPERR_PARAM );
1706 /* now get rid of dangling symlinks */
1707 if ((dp = opendir("."))) {
1708 while ((de = readdir(dp))) {
1709 /* skip this and previous directory */
1710 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1713 /* bail if it's not a symlink */
1714 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1716 restore_uidgid ( &uidgid );
1717 #endif /* FORCE_UIDGID */
1718 return AFPERR_DIRNEMPT;
1721 if (unlink(de->d_name) < 0) {
1726 restore_uidgid ( &uidgid );
1727 #endif /* FORCE_UIDGID */
1728 return( AFPERR_ACCESS );
1731 restore_uidgid ( &uidgid );
1732 #endif /* FORCE_UIDGID */
1733 return AFPERR_VLOCK;
1738 restore_uidgid ( &uidgid );
1739 #endif /* FORCE_UIDGID */
1740 return( AFPERR_PARAM );
1747 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1749 restore_uidgid ( &uidgid );
1750 #endif /* FORCE_UIDGID */
1751 return( AFPERR_NOOBJ );
1754 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1758 restore_uidgid ( &uidgid );
1759 #endif /* FORCE_UIDGID */
1760 return( AFPERR_NOOBJ );
1763 restore_uidgid ( &uidgid );
1764 #endif /* FORCE_UIDGID */
1765 return( AFPERR_DIRNEMPT );
1769 restore_uidgid ( &uidgid );
1770 #endif /* FORCE_UIDGID */
1771 return( AFPERR_ACCESS );
1774 restore_uidgid ( &uidgid );
1775 #endif /* FORCE_UIDGID */
1776 return AFPERR_VLOCK;
1779 restore_uidgid ( &uidgid );
1780 #endif /* FORCE_UIDGID */
1781 return( AFPERR_PARAM );
1785 dirchildremove(curdir, fdir);
1787 cnid_delete(vol->v_db, fdir->d_did);
1788 #endif /* CNID_DB */
1789 dir_remove( vol, fdir );
1792 restore_uidgid ( &uidgid );
1793 #endif /* FORCE_UIDGID */
1797 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1800 int ibuflen, *rbuflen;
1809 sfunc = (unsigned char) *ibuf++;
1810 memcpy( &id, ibuf, sizeof( id ));
1817 if (( pw = getpwuid( id )) == NULL ) {
1819 return( AFPERR_NOITEM );
1825 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1827 return( AFPERR_NOITEM );
1834 return( AFPERR_PARAM );
1837 len = strlen( name );
1846 memcpy( rbuf, name, len );
1852 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1855 int ibuflen, *rbuflen;
1863 sfunc = (unsigned char) *ibuf++;
1864 len = (unsigned char) *ibuf++;
1870 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1872 return( AFPERR_NOITEM );
1878 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1880 return( AFPERR_NOITEM );
1886 return( AFPERR_PARAM );
1892 memcpy( rbuf, &id, sizeof( id ));
1893 *rbuflen = sizeof( id );
1897 /* variable DID support */
1898 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1901 int ibuflen, *rbuflen;
1912 /* do nothing as dids are static for the life of the process. */
1916 memcpy(&vid, ibuf, sizeof( vid ));
1917 ibuf += sizeof( vid );
1918 if (( vol = getvolbyvid( vid )) == NULL ) {
1919 return( AFPERR_PARAM );
1922 memcpy( &did, ibuf, sizeof( did ));
1923 ibuf += sizeof( did );
1924 if (( dir = dirsearch( vol, did )) == NULL ) {
1925 return( AFPERR_PARAM );
1928 /* dir_remove -- deletedid */
1934 /* did creation gets done automatically */
1935 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1938 int ibuflen, *rbuflen;
1941 struct dir *dir, *parentdir;
1949 memset(&uidgid, 0, sizeof(uidgid));
1950 #endif /* FORCE_UIDGID */
1955 memcpy(&vid, ibuf, sizeof(vid));
1956 ibuf += sizeof( vid );
1958 if (( vol = getvolbyvid( vid )) == NULL ) {
1959 return( AFPERR_PARAM );
1962 memcpy(&did, ibuf, sizeof(did));
1963 ibuf += sizeof(did);
1965 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1966 return( AFPERR_NOOBJ );
1969 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1972 return( AFPERR_ACCESS );
1974 return( AFPERR_NOOBJ );
1978 /* see if we already have the directory. */
1979 upath = mtoupath(vol, path);
1980 if ( stat( upath, &st ) < 0 ) {
1981 return( AFPERR_NOOBJ );
1984 dir = parentdir->d_child;
1986 if (strdiacasecmp(dir->d_name, path) == 0) {
1987 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1988 *rbuflen = sizeof(dir->d_did);
1991 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1995 save_uidgid ( &uidgid );
1997 #endif /* FORCE_UIDGID */
1999 /* we don't already have a did. add one in. */
2000 if ((dir = adddir(vol, parentdir, path, strlen(path),
2001 upath, strlen(upath), &st)) == NULL) {
2003 restore_uidgid ( &uidgid );
2004 #endif /* FORCE_UIDGID */
2008 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2009 *rbuflen = sizeof(dir->d_did);
2011 restore_uidgid ( &uidgid );
2012 #endif /* FORCE_UIDGID */