2 * $Id: directory.c,v 1.21 2001-12-10 20:16:54 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. */
472 /* okay. the directory is empty. delete it. note: we already got rid
474 if (rmdir(dir) < 0) {
478 case ENOTEMPTY : /* should never happen */
479 return( AFPERR_DIRNEMPT );
482 return( AFPERR_ACCESS );
486 return( AFPERR_PARAM );
492 /* do a recursive copy. */
493 static int copydir(char *src, char *dst, int noadouble)
495 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
502 /* doesn't exist or the path is too long. */
503 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
504 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
505 ((dp = opendir(src)) == NULL))
508 /* try to create the destination directory */
509 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
513 return( AFPERR_NOOBJ );
515 return( AFPERR_VLOCK );
518 return( AFPERR_ACCESS );
520 return( AFPERR_EXIST );
523 return( AFPERR_DFULL );
525 return( AFPERR_PARAM );
529 /* set things up to copy */
537 while ((de = readdir(dp))) {
538 /* skip this and previous directory */
539 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
542 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
543 if (stat(spath, &st) == 0) {
544 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
546 if (S_ISDIR(st.st_mode)) {
547 if ((err = copydir(spath, dpath, noadouble)) < 0)
549 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
553 /* keep the same time stamp. */
554 ut.actime = ut.modtime = st.st_mtime;
560 /* keep the same time stamp. */
561 if (stat(src, &st) == 0) {
562 ut.actime = ut.modtime = st.st_mtime;
572 /* --- public functions follow --- */
574 /* NOTE: we start off with at least one node (the root directory). */
575 struct dir *dirinsert( vol, dir )
581 if ((node = dir_insert(vol, dir)))
584 /* recolor the tree. the current node is red. */
585 dir->d_color = DIRTREE_COLOR_RED;
587 /* parent of this node has to be black. if the parent node
588 * is red, then we have a grandparent. */
589 while ((dir != vol->v_root) &&
590 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
591 /* are we on the left tree? */
592 if (dir->d_back == dir->d_back->d_back->d_left) {
593 node = dir->d_back->d_back->d_right; /* get the right node */
594 if (node->d_color == DIRTREE_COLOR_RED) {
595 /* we're red. we need to change to black. */
596 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
597 node->d_color = DIRTREE_COLOR_BLACK;
598 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
599 dir = dir->d_back->d_back; /* finished. go up. */
601 if (dir == dir->d_back->d_right) {
603 dir_leftrotate(vol, dir);
605 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
606 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
607 dir_rightrotate(vol, dir->d_back->d_back);
610 node = dir->d_back->d_back->d_left;
611 if (node->d_color == DIRTREE_COLOR_RED) {
612 /* we're red. we need to change to black. */
613 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
614 node->d_color = DIRTREE_COLOR_BLACK;
615 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
616 dir = dir->d_back->d_back; /* finished. ascend */
618 if (dir == dir->d_back->d_left) {
620 dir_rightrotate(vol, dir);
622 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
623 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
624 dir_leftrotate(vol, dir->d_back->d_back);
629 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
633 /* free everything down. we don't bother to recolor as this is only
634 * called to free the entire tree */
638 if (!dir || (dir == SENTINEL))
641 if ( dir->d_left != SENTINEL ) {
642 dirfree( dir->d_left );
644 if ( dir->d_right != SENTINEL ) {
645 dirfree( dir->d_right );
648 if (dir != SENTINEL) {
655 struct dir *dirnew(const int len)
659 dir = (struct dir *) calloc(1, sizeof( struct dir ));
663 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
668 dir->d_left = dir->d_right = SENTINEL;
669 dir->d_next = dir->d_prev = dir;
674 /* XXX: this needs to be changed to handle path types */
676 cname( vol, dir, cpath )
677 const struct vol *vol;
682 static char path[ MAXPATHLEN + 1];
688 if ( *data++ != 2 ) { /* path type */
691 len = (unsigned char) *data++;
697 if ( !extend && movecwd( vol, dir ) < 0 ) {
703 if ( *data == '\0' ) {
708 while ( *data == '\0' && len > 0 ) {
709 if ( dir->d_parent == NULL ) {
717 /* would this be faster with strlen + strncpy? */
719 while ( *data != '\0' && len > 0 ) {
724 /* short cut bits by chopping off a trailing \0. this also
725 makes the traversal happy w/ filenames at the end of the
732 * Dung Nguyen <ntd@adb.fr>
734 * AFPD cannot handle paths with "::" if the "::" notation is
735 * not at the beginning of the path. The following path will not
736 * be interpreted correctly:
738 * :a:b:::c: (directory c at the same level as directory a) */
746 if ( p != path ) { /* we got something */
750 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
753 cdir = (cdir == dir->d_child->d_prev) ? NULL :
756 if ( cdir == NULL ) {
758 if ( movecwd( vol, dir ) < 0 ) {
761 cdir = extenddir( vol, dir, path );
765 cdir = extenddir( vol, dir, path );
768 if ( cdir == NULL ) {
782 * Move curdir to dir, with a possible chdir()
784 int movecwd( vol, dir)
785 const struct vol *vol;
788 char path[MAXPATHLEN + 1];
793 if ( dir == curdir ) {
796 if ( dir->d_did == DIRDID_ROOT_PARENT) {
800 p = path + sizeof(path) - 1;
803 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
805 u = mtoupath(vol, d->d_name );
812 n = strlen( vol->v_path );
814 strncpy( p, vol->v_path, n );
816 if ( chdir( p ) < 0 ) {
823 int getdirparams(const struct vol *vol,
825 char *upath, struct dir *dir, struct stat *st,
826 char *buf, int *buflen )
830 char *data, *nameoff = NULL;
833 int bit = 0, isad = 1;
838 memset(&uidgid, 0, sizeof(uidgid));
839 #endif /* FORCE_UIDGID */
841 memset(&ad, 0, sizeof(ad));
844 save_uidgid ( &uidgid );
846 #endif /* FORCE_UIDGID */
848 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
849 DIRBITS | 0777, &ad) < 0 ) {
854 while ( bitmap != 0 ) {
855 while (( bitmap & 1 ) == 0 ) {
863 ad_getattr(&ad, &ashort);
864 } else if (*upath == '.' && strcmp(upath, ".") &&
865 strcmp(upath, "..")) {
866 ashort = htons(ATTRBIT_INVISIBLE);
869 memcpy( data, &ashort, sizeof( ashort ));
870 data += sizeof( ashort );
874 if ( dir->d_did == DIRDID_ROOT) {
875 aint = DIRDID_ROOT_PARENT;
876 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
879 aint = dir->d_parent->d_did;
881 memcpy( data, &aint, sizeof( aint ));
882 data += sizeof( aint );
886 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
887 aint = AD_DATE_FROM_UNIX(st->st_mtime);
888 memcpy( data, &aint, sizeof( aint ));
889 data += sizeof( aint );
893 aint = AD_DATE_FROM_UNIX(st->st_mtime);
894 memcpy( data, &aint, sizeof( aint ));
895 data += sizeof( aint );
899 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
900 aint = AD_DATE_START;
901 memcpy( data, &aint, sizeof( aint ));
902 data += sizeof( aint );
907 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
908 } else { /* no appledouble */
909 memset( data, 0, 32 );
910 /* set default view -- this also gets done in ad_open() */
911 ashort = htons(FINDERINFO_CLOSEDVIEW);
912 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
914 /* dot files are by default invisible */
915 if (*upath == '.' && strcmp(upath, ".") &&
916 strcmp(upath, "..")) {
917 ashort = htons(FINDERINFO_INVISIBLE);
918 memcpy(data + FINDERINFO_FRFLAGOFF,
919 &ashort, sizeof(ashort));
926 if (dir->d_name) /* root of parent can have a null name */
929 memset(data, 0, sizeof(u_int16_t));
930 data += sizeof( u_int16_t );
934 memset(data, 0, sizeof(u_int16_t));
935 data += sizeof( u_int16_t );
939 memcpy( data, &dir->d_did, sizeof( aint ));
940 data += sizeof( aint );
943 case DIRPBIT_OFFCNT :
945 /* this needs to handle current directory access rights */
946 if ((dp = opendir( upath ))) {
947 while (( de = readdir( dp )) != NULL ) {
948 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
951 if (!validupath(vol, de->d_name))
954 /* check for vetoed filenames */
955 if (veto_file(vol->v_veto, de->d_name))
958 /* now check against too long a filename */
959 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
966 ashort = htons( ashort );
967 memcpy( data, &ashort, sizeof( ashort ));
968 data += sizeof( ashort );
972 aint = htonl(st->st_uid);
973 memcpy( data, &aint, sizeof( aint ));
974 data += sizeof( aint );
978 aint = htonl(st->st_gid);
979 memcpy( data, &aint, sizeof( aint ));
980 data += sizeof( aint );
983 case DIRPBIT_ACCESS :
985 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
987 accessmode( upath, &ma, dir );
988 #endif /* HAVE_ACCESS */
989 #endif /* SENDFILE_FLAVOR_LINUX */
990 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
991 afsmode( upath, &ma, dir );
993 *data++ = ma.ma_user;
994 *data++ = ma.ma_world;
995 *data++ = ma.ma_group;
996 *data++ = ma.ma_owner;
999 /* Client has requested the ProDOS information block.
1000 Just pass back the same basic block for all
1001 directories. <shirsch@ibm.net> */
1002 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1005 ashort = htons( 0x0200 );
1006 memcpy( data, &ashort, sizeof( ashort ));
1007 data += sizeof( ashort );
1008 memset( data, 0, sizeof( ashort ));
1009 data += sizeof( ashort );
1014 ad_close( &ad, ADFLAGS_HF );
1017 restore_uidgid ( &uidgid );
1018 #endif /* FORCE_UIDGID */
1019 return( AFPERR_BITMAP );
1025 ashort = htons( data - buf );
1026 memcpy( nameoff, &ashort, sizeof( ashort ));
1028 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1032 memcpy( data, dir->d_name, aint );
1036 ad_close( &ad, ADFLAGS_HF );
1038 *buflen = data - buf;
1042 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1045 int ibuflen, *rbuflen;
1050 u_int16_t vid, bitmap;
1056 memcpy( &vid, ibuf, sizeof( vid ));
1057 ibuf += sizeof( vid );
1059 if (( vol = getvolbyvid( vid )) == NULL ) {
1060 return( AFPERR_PARAM );
1063 if (vol->v_flags & AFPVOL_RO)
1064 return AFPERR_VLOCK;
1066 memcpy( &did, ibuf, sizeof( did ));
1067 ibuf += sizeof( int );
1069 if (( dir = dirsearch( vol, did )) == NULL ) {
1070 return( AFPERR_NOOBJ );
1073 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1074 bitmap = ntohs( bitmap );
1075 ibuf += sizeof( bitmap );
1077 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1078 return( AFPERR_NOOBJ );
1082 * If ibuf is odd, make it even.
1084 if ((u_long)ibuf & 1 ) {
1088 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1089 setvoltime(obj, vol );
1094 int setdirparams(const struct vol *vol,
1095 char *path, u_int16_t bitmap, char *buf )
1101 int bit = 0, aint, isad = 1;
1102 u_int16_t ashort, bshort;
1107 memset(&uidgid, 0, sizeof(uidgid));
1108 #endif /* FORCE_UIDGID */
1110 upath = mtoupath(vol, path);
1111 memset(&ad, 0, sizeof(ad));
1113 save_uidgid ( &uidgid );
1114 #endif /* FORCE_UIDGID */
1115 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1116 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1118 * Check to see what we're trying to set. If it's anything
1119 * but ACCESS, UID, or GID, give an error. If it's any of those
1120 * three, we don't need the ad to be open, so just continue.
1122 * note: we also don't need to worry about mdate. also, be quiet
1123 * if we're using the noadouble option.
1125 if (!vol_noadouble(vol) && (bitmap &
1126 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1127 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1129 restore_uidgid ( &uidgid );
1130 #endif /* FORCE_UIDGID */
1131 return AFPERR_ACCESS;
1137 * Check to see if a create was necessary. If it was, we'll want
1138 * to set our name, etc.
1140 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1141 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1142 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1143 ad_getentrylen( &ad, ADEID_NAME ));
1147 while ( bitmap != 0 ) {
1148 while (( bitmap & 1 ) == 0 ) {
1156 memcpy( &ashort, buf, sizeof( ashort ));
1157 ad_getattr(&ad, &bshort);
1158 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1159 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1163 ad_setattr(&ad, bshort);
1165 buf += sizeof( ashort );
1168 case DIRPBIT_CDATE :
1170 memcpy(&aint, buf, sizeof(aint));
1171 ad_setdate(&ad, AD_DATE_CREATE, aint);
1173 buf += sizeof( aint );
1176 case DIRPBIT_MDATE :
1177 memcpy(&aint, buf, sizeof(aint));
1179 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1180 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1182 buf += sizeof( aint );
1185 case DIRPBIT_BDATE :
1187 memcpy(&aint, buf, sizeof(aint));
1188 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1190 buf += sizeof( aint );
1193 case DIRPBIT_FINFO :
1195 * Alright, we admit it, this is *really* sick!
1196 * The 4 bytes that we don't copy, when we're dealing
1197 * with the root of a volume, are the directory's
1198 * location information. This eliminates that annoying
1199 * behavior one sees when mounting above another mount
1203 if ( curdir->d_did == DIRDID_ROOT ) {
1204 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1205 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1207 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1213 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1214 memcpy( &aint, buf, sizeof(aint));
1215 buf += sizeof( aint );
1216 if ( (curdir->d_did == DIRDID_ROOT) &&
1217 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1221 err = AFPERR_ACCESS;
1222 goto setdirparam_done;
1226 goto setdirparam_done;
1229 syslog( LOG_ERR, "setdirparam: setdeskowner: %s",
1233 goto setdirparam_done;
1238 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1242 err = AFPERR_ACCESS;
1243 goto setdirparam_done;
1247 goto setdirparam_done;
1250 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1257 memcpy( &aint, buf, sizeof( aint ));
1258 buf += sizeof( aint );
1259 if (curdir->d_did == DIRDID_ROOT)
1260 setdeskowner( -1, ntohl(aint) );
1262 #if 0 /* don't error if we can't set the desktop owner. */
1266 err = AFPERR_ACCESS;
1267 goto setdirparam_done;
1271 goto setdirparam_done;
1274 syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1277 goto setdirparam_done;
1283 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1287 err = AFPERR_ACCESS;
1288 goto setdirparam_done;
1292 goto setdirparam_done;
1295 syslog( LOG_ERR, "setdirparam: setdirowner: %s",
1302 case DIRPBIT_ACCESS :
1303 ma.ma_user = *buf++;
1304 ma.ma_world = *buf++;
1305 ma.ma_group = *buf++;
1306 ma.ma_owner = *buf++;
1308 if (curdir->d_did == DIRDID_ROOT)
1309 setdeskmode(mtoumode( &ma ));
1310 #if 0 /* don't error if we can't set the desktop mode */
1314 err = AFPERR_ACCESS;
1315 goto setdirparam_done;
1318 goto setdirparam_done;
1320 syslog( LOG_ERR, "setdirparam: setdeskmode: %s",
1324 goto setdirparam_done;
1329 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1330 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1334 err = AFPERR_ACCESS;
1335 goto setdirparam_done;
1338 goto setdirparam_done;
1340 syslog( LOG_ERR, "setdirparam: setdirmode: %s",
1343 goto setdirparam_done;
1348 /* Ignore what the client thinks we should do to the
1349 ProDOS information block. Skip over the data and
1350 report nothing amiss. <shirsch@ibm.net> */
1351 case DIRPBIT_PDINFO :
1356 err = AFPERR_BITMAP;
1357 goto setdirparam_done;
1368 ad_flush( &ad, ADFLAGS_HF );
1369 ad_close( &ad, ADFLAGS_HF );
1373 restore_uidgid ( &uidgid );
1374 #endif /* FORCE_UIDGID */
1378 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1381 int ibuflen, *rbuflen;
1393 memset(&uidgid, 0, sizeof(uidgid));
1394 #endif /* FORCE_UIDGID */
1399 memcpy( &vid, ibuf, sizeof( vid ));
1400 ibuf += sizeof( vid );
1401 if (( vol = getvolbyvid( vid )) == NULL ) {
1402 return( AFPERR_PARAM );
1405 if (vol->v_flags & AFPVOL_RO)
1406 return AFPERR_VLOCK;
1408 memcpy( &did, ibuf, sizeof( did ));
1409 ibuf += sizeof( did );
1410 if (( dir = dirsearch( vol, did )) == NULL ) {
1411 return( AFPERR_NOOBJ );
1414 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1417 return( AFPERR_ACCESS );
1419 return( AFPERR_EXIST );
1421 return( AFPERR_NOOBJ );
1425 /* check for illegal bits */
1426 if ((vol->v_flags & AFPVOL_MSWINDOWS) &&
1427 strpbrk(path, MSWINDOWS_BADCHARS))
1428 return AFPERR_PARAM;
1430 upath = mtoupath(vol, path);
1432 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1433 return AFPERR_PARAM;
1435 if (!validupath(vol, upath))
1436 return AFPERR_EXIST;
1438 /* check for vetoed filenames */
1439 if (veto_file(vol->v_veto, upath))
1440 return AFPERR_EXIST;
1443 save_uidgid ( &uidgid );
1445 #endif /* FORCE_UIDGID */
1447 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1449 restore_uidgid ( &uidgid );
1450 #endif /* FORCE_UIDGID */
1453 return( AFPERR_NOOBJ );
1455 return( AFPERR_VLOCK );
1457 return( AFPERR_ACCESS );
1459 return( AFPERR_EXIST );
1462 return( AFPERR_DFULL );
1464 return( AFPERR_PARAM );
1468 if (stat(upath, &st) < 0) {
1470 restore_uidgid ( &uidgid );
1471 #endif /* FORCE_UIDGID */
1475 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1476 strlen(upath), &st)) == NULL) {
1478 restore_uidgid ( &uidgid );
1479 #endif /* FORCE_UIDGID */
1483 if ( movecwd( vol, dir ) < 0 ) {
1485 restore_uidgid ( &uidgid );
1486 #endif /* FORCE_UIDGID */
1487 return( AFPERR_PARAM );
1490 memset(&ad, 0, sizeof(ad));
1491 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1492 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1493 if (vol_noadouble(vol))
1494 goto createdir_done;
1496 restore_uidgid ( &uidgid );
1497 #endif /* FORCE_UIDGID */
1498 return( AFPERR_ACCESS );
1501 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1502 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1503 ad_getentrylen( &ad, ADEID_NAME ));
1504 ad_flush( &ad, ADFLAGS_HF );
1505 ad_close( &ad, ADFLAGS_HF );
1508 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1509 *rbuflen = sizeof( u_int32_t );
1510 setvoltime(obj, vol );
1512 restore_uidgid ( &uidgid );
1513 #endif /* FORCE_UIDGID */
1518 int renamedir(src, dst, dir, newparent, newname, noadouble)
1519 char *src, *dst, *newname;
1520 struct dir *dir, *newparent;
1521 const int noadouble;
1528 /* existence check moved to afp_moveandrename */
1529 if ( rename( src, dst ) < 0 ) {
1532 return( AFPERR_NOOBJ );
1534 return( AFPERR_ACCESS );
1536 return AFPERR_VLOCK;
1538 /* tried to move directory into a subdirectory of itself */
1539 return AFPERR_CANTMOVE;
1541 /* this needs to copy and delete. bleah. that means we have
1542 * to deal with entire directory hierarchies. */
1543 if ((err = copydir(src, dst, noadouble)) < 0) {
1547 if ((err = deletedir(src)) < 0)
1551 return( AFPERR_PARAM );
1555 memset(&ad, 0, sizeof(ad));
1556 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1560 len = strlen(newname);
1561 goto renamedir_done;
1563 return( AFPERR_NOOBJ );
1565 return( AFPERR_ACCESS );
1567 return( AFPERR_PARAM );
1570 len = strlen( newname );
1571 ad_setentrylen( &ad, ADEID_NAME, len );
1572 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1573 ad_flush( &ad, ADFLAGS_HF );
1574 ad_close( &ad, ADFLAGS_HF );
1577 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1578 syslog( LOG_ERR, "renamedir: realloc: %s", strerror(errno) );
1582 strcpy( dir->d_name, newname );
1584 if (( parent = dir->d_parent ) == NULL ) {
1587 if ( parent == newparent ) {
1591 /* detach from old parent and add to new one. */
1592 dirchildremove(parent, dir);
1593 dir->d_parent = newparent;
1594 dirchildadd(newparent, dir);
1598 #define DOT_APPLEDOUBLE_LEN 13
1599 /* delete an empty directory */
1600 int deletecurdir( vol, path, pathlen )
1601 const struct vol *vol;
1612 memset(&uidgid, 0, sizeof(uidgid));
1613 #endif /* FORCE_UIDGID */
1615 if ( curdir->d_parent == NULL ) {
1616 return( AFPERR_ACCESS );
1619 if ( curdir->d_child != NULL ) {
1620 return( AFPERR_DIRNEMPT );
1626 save_uidgid ( &uidgid );
1628 #endif /* FORCE_UIDGID */
1630 /* delete stray .AppleDouble files. this happens to get .Parent files
1632 if ((dp = opendir(".AppleDouble"))) {
1633 strcpy(path, ".AppleDouble/");
1634 while ((de = readdir(dp))) {
1635 /* skip this and previous directory */
1636 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1639 /* bail if the file exists in the current directory.
1640 * note: this will not fail with dangling symlinks */
1641 if (stat(de->d_name, &st) == 0) {
1644 restore_uidgid ( &uidgid );
1645 #endif /* FORCE_UIDGID */
1646 return AFPERR_DIRNEMPT;
1649 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1650 if (unlink(path) < 0) {
1656 restore_uidgid ( &uidgid );
1657 #endif /* FORCE_UIDGID */
1658 return( AFPERR_ACCESS );
1661 restore_uidgid ( &uidgid );
1662 #endif /* FORCE_UIDGID */
1663 return AFPERR_VLOCK;
1668 restore_uidgid ( &uidgid );
1669 #endif /* FORCE_UIDGID */
1670 return( AFPERR_PARAM );
1677 if ( rmdir( ".AppleDouble" ) < 0 ) {
1683 restore_uidgid ( &uidgid );
1684 #endif /* FORCE_UIDGID */
1685 return( AFPERR_DIRNEMPT );
1688 restore_uidgid ( &uidgid );
1689 #endif /* FORCE_UIDGID */
1690 return AFPERR_VLOCK;
1694 restore_uidgid ( &uidgid );
1695 #endif /* FORCE_UIDGID */
1696 return( AFPERR_ACCESS );
1699 restore_uidgid ( &uidgid );
1700 #endif /* FORCE_UIDGID */
1701 return( AFPERR_PARAM );
1705 /* now get rid of dangling symlinks */
1706 if ((dp = opendir("."))) {
1707 while ((de = readdir(dp))) {
1708 /* skip this and previous directory */
1709 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1712 /* bail if it's not a symlink */
1713 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1715 restore_uidgid ( &uidgid );
1716 #endif /* FORCE_UIDGID */
1717 return AFPERR_DIRNEMPT;
1720 if (unlink(de->d_name) < 0) {
1725 restore_uidgid ( &uidgid );
1726 #endif /* FORCE_UIDGID */
1727 return( AFPERR_ACCESS );
1730 restore_uidgid ( &uidgid );
1731 #endif /* FORCE_UIDGID */
1732 return AFPERR_VLOCK;
1737 restore_uidgid ( &uidgid );
1738 #endif /* FORCE_UIDGID */
1739 return( AFPERR_PARAM );
1746 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1748 restore_uidgid ( &uidgid );
1749 #endif /* FORCE_UIDGID */
1750 return( AFPERR_NOOBJ );
1753 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1757 restore_uidgid ( &uidgid );
1758 #endif /* FORCE_UIDGID */
1759 return( AFPERR_NOOBJ );
1762 restore_uidgid ( &uidgid );
1763 #endif /* FORCE_UIDGID */
1764 return( AFPERR_DIRNEMPT );
1768 restore_uidgid ( &uidgid );
1769 #endif /* FORCE_UIDGID */
1770 return( AFPERR_ACCESS );
1773 restore_uidgid ( &uidgid );
1774 #endif /* FORCE_UIDGID */
1775 return AFPERR_VLOCK;
1778 restore_uidgid ( &uidgid );
1779 #endif /* FORCE_UIDGID */
1780 return( AFPERR_PARAM );
1784 dirchildremove(curdir, fdir);
1786 cnid_delete(vol->v_db, fdir->d_did);
1787 #endif /* CNID_DB */
1788 dir_remove( vol, fdir );
1791 restore_uidgid ( &uidgid );
1792 #endif /* FORCE_UIDGID */
1796 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1799 int ibuflen, *rbuflen;
1808 sfunc = (unsigned char) *ibuf++;
1809 memcpy( &id, ibuf, sizeof( id ));
1816 if (( pw = getpwuid( id )) == NULL ) {
1818 return( AFPERR_NOITEM );
1824 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1826 return( AFPERR_NOITEM );
1833 return( AFPERR_PARAM );
1836 len = strlen( name );
1845 memcpy( rbuf, name, len );
1851 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1854 int ibuflen, *rbuflen;
1862 sfunc = (unsigned char) *ibuf++;
1863 len = (unsigned char) *ibuf++;
1869 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1871 return( AFPERR_NOITEM );
1877 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1879 return( AFPERR_NOITEM );
1885 return( AFPERR_PARAM );
1891 memcpy( rbuf, &id, sizeof( id ));
1892 *rbuflen = sizeof( id );
1896 /* variable DID support */
1897 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1900 int ibuflen, *rbuflen;
1911 /* do nothing as dids are static for the life of the process. */
1915 memcpy(&vid, ibuf, sizeof( vid ));
1916 ibuf += sizeof( vid );
1917 if (( vol = getvolbyvid( vid )) == NULL ) {
1918 return( AFPERR_PARAM );
1921 memcpy( &did, ibuf, sizeof( did ));
1922 ibuf += sizeof( did );
1923 if (( dir = dirsearch( vol, did )) == NULL ) {
1924 return( AFPERR_PARAM );
1927 /* dir_remove -- deletedid */
1933 /* did creation gets done automatically */
1934 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1937 int ibuflen, *rbuflen;
1940 struct dir *dir, *parentdir;
1948 memset(&uidgid, 0, sizeof(uidgid));
1949 #endif /* FORCE_UIDGID */
1954 memcpy(&vid, ibuf, sizeof(vid));
1955 ibuf += sizeof( vid );
1957 if (( vol = getvolbyvid( vid )) == NULL ) {
1958 return( AFPERR_PARAM );
1961 memcpy(&did, ibuf, sizeof(did));
1962 ibuf += sizeof(did);
1964 if (( parentdir = dirsearch( vol, did )) == NULL ) {
1965 return( AFPERR_NOOBJ );
1968 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1971 return( AFPERR_ACCESS );
1973 return( AFPERR_NOOBJ );
1977 /* see if we already have the directory. */
1978 upath = mtoupath(vol, path);
1979 if ( stat( upath, &st ) < 0 ) {
1980 return( AFPERR_NOOBJ );
1983 dir = parentdir->d_child;
1985 if (strdiacasecmp(dir->d_name, path) == 0) {
1986 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1987 *rbuflen = sizeof(dir->d_did);
1990 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1994 save_uidgid ( &uidgid );
1996 #endif /* FORCE_UIDGID */
1998 /* we don't already have a did. add one in. */
1999 if ((dir = adddir(vol, parentdir, path, strlen(path),
2000 upath, strlen(upath), &st)) == NULL) {
2002 restore_uidgid ( &uidgid );
2003 #endif /* FORCE_UIDGID */
2007 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2008 *rbuflen = sizeof(dir->d_did);
2010 restore_uidgid ( &uidgid );
2011 #endif /* FORCE_UIDGID */