2 * $Id: directory.c,v 1.30 2002-04-29 06:53:25 morgana 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 <atalk/logger.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;
123 /* -----------------------------------------
124 * if did is not in the cache resolve it with cnid
128 dirlookup( vol, did )
129 const struct vol *vol;
136 static char path[MAXPATHLEN + 1];
140 static char buffer[12 + MAXPATHLEN + 1];
141 int buflen = 12 + MAXPATHLEN + 1;
143 ret = dirsearch(vol, did);
148 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
151 ptr = path + MAXPATHLEN;
153 pathlen = len; /* no 0 in the last part */
155 strcpy(ptr - len, upath);
158 ret = dirsearch(vol,id);
162 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL)
164 len = strlen(upath) + 1;
168 strcpy(ptr - len, upath);
173 *ptr = (unsigned char)pathlen;
176 /* cname is not efficient */
177 if (cname( vol, ret, &ptr ) == NULL )
180 return dirsearch(vol, did);
183 /* --------------------------- */
184 /* rotate the tree to the left */
185 static void dir_leftrotate(vol, dir)
189 struct dir *right = dir->d_right;
191 /* whee. move the right's left tree into dir's right tree */
192 dir->d_right = right->d_left;
193 if (right->d_left != SENTINEL)
194 right->d_left->d_back = dir;
196 if (right != SENTINEL) {
197 right->d_back = dir->d_back;
201 if (!dir->d_back) /* no parent. move the right tree to the top. */
203 else if (dir == dir->d_back->d_left) /* we were on the left */
204 dir->d_back->d_left = right;
206 dir->d_back->d_right = right; /* we were on the right */
208 /* re-insert dir on the left tree */
215 /* rotate the tree to the right */
216 static void dir_rightrotate(vol, dir)
220 struct dir *left = dir->d_left;
222 /* whee. move the left's right tree into dir's left tree */
223 dir->d_left = left->d_right;
224 if (left->d_right != SENTINEL)
225 left->d_right->d_back = dir;
227 if (left != SENTINEL) {
228 left->d_back = dir->d_back;
232 if (!dir->d_back) /* no parent. move the left tree to the top. */
234 else if (dir == dir->d_back->d_right) /* we were on the right */
235 dir->d_back->d_right = left;
237 dir->d_back->d_left = left; /* we were on the left */
239 /* re-insert dir on the right tree */
245 /* recolor after a removal */
246 static struct dir *dir_rmrecolor(vol, dir)
252 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
253 /* are we on the left tree? */
254 if (dir == dir->d_back->d_left) {
255 leaf = dir->d_back->d_right; /* get right side */
256 if (leaf->d_color == DIRTREE_COLOR_RED) {
257 /* we're red. we need to change to black. */
258 leaf->d_color = DIRTREE_COLOR_BLACK;
259 dir->d_back->d_color = DIRTREE_COLOR_RED;
260 dir_leftrotate(vol, dir->d_back);
261 leaf = dir->d_back->d_right;
264 /* right leaf has black end nodes */
265 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
266 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
267 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
268 dir = dir->d_back; /* ascend */
270 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
271 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
272 leaf->d_color = DIRTREE_COLOR_RED;
273 dir_rightrotate(vol, leaf);
274 leaf = dir->d_back->d_right;
276 leaf->d_color = dir->d_back->d_color;
277 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
278 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
279 dir_leftrotate(vol, dir->d_back);
282 } else { /* right tree */
283 leaf = dir->d_back->d_left; /* left tree */
284 if (leaf->d_color == DIRTREE_COLOR_RED) {
285 leaf->d_color = DIRTREE_COLOR_BLACK;
286 dir->d_back->d_color = DIRTREE_COLOR_RED;
287 dir_rightrotate(vol, dir->d_back);
288 leaf = dir->d_back->d_left;
291 /* left leaf has black end nodes */
292 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
293 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
294 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
295 dir = dir->d_back; /* ascend */
297 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
298 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
299 leaf->d_color = DIRTREE_COLOR_RED;
300 dir_leftrotate(vol, leaf);
301 leaf = dir->d_back->d_left;
303 leaf->d_color = dir->d_back->d_color;
304 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
305 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
306 dir_rightrotate(vol, dir->d_back);
311 dir->d_color = DIRTREE_COLOR_BLACK;
318 /* remove the node from the tree. this is just like insertion, but
319 * different. actually, it has to worry about a bunch of things that
320 * insertion doesn't care about. */
321 static void dir_remove( vol, dir )
326 struct ofork *of, *last;
327 struct dir *node, *leaf;
328 #endif /* REMOVE_NODES */
330 if (!dir || (dir == SENTINEL))
333 /* i'm not sure if it really helps to delete stuff. */
337 #else /* ! REMOVE_NODES */
339 /* go searching for a node with at most one child */
340 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
344 while (node->d_left != SENTINEL)
349 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
352 leaf->d_back = node->d_back;
355 } else if (node == node->d_back->d_left) { /* left tree */
356 node->d_back->d_left = leaf;
358 node->d_back->d_right = leaf;
361 /* we want to free node, but we also want to free the data in dir.
362 * currently, that's d_name and the directory traversal bits.
363 * we just copy the necessary bits and then fix up all the
364 * various pointers to the directory. needless to say, there are
365 * a bunch of places that store the directory struct. */
367 struct dir save, *tmp;
369 memcpy(&save, dir, sizeof(save));
370 memcpy(dir, node, sizeof(struct dir));
372 /* restore the red-black bits */
373 dir->d_left = save.d_left;
374 dir->d_right = save.d_right;
375 dir->d_back = save.d_back;
376 dir->d_color = save.d_color;
378 if (node == vol->v_dir) {/* we may need to fix up this pointer */
380 rootpar.d_child = vol->v_dir;
382 /* if we aren't the root directory, we have parents and
383 * siblings to worry about */
384 if (dir->d_parent->d_child == node)
385 dir->d_parent->d_child = dir;
386 dir->d_next->d_prev = dir;
387 dir->d_prev->d_next = dir;
390 /* fix up children. */
394 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
397 if (node == curdir) /* another pointer to fixup */
400 /* we also need to fix up oforks. bleah */
401 if ((of = dir->d_ofork)) {
402 last = of->of_d_prev;
405 of = (last == of) ? NULL : of->of_d_next;
409 /* set the node's d_name */
410 node->d_name = save.d_name;
413 if (node->d_color == DIRTREE_COLOR_BLACK)
414 dir_rmrecolor(vol, leaf);
417 #endif /* ! REMOVE_NODES */
421 static struct dir *dir_insert(vol, dir)
422 const struct vol *vol;
428 while (pdir->d_did != dir->d_did ) {
429 if ( pdir->d_did > dir->d_did ) {
430 if ( pdir->d_left == SENTINEL ) {
437 if ( pdir->d_right == SENTINEL ) {
442 pdir = pdir->d_right;
450 * attempt to extend the current dir. tree to include path
451 * as a side-effect, movecwd to that point and return the new dir
455 extenddir( vol, dir, path )
463 p = mtoupath(vol, path );
464 if ( stat( p, &st ) != 0 ) {
467 if (!S_ISDIR(st.st_mode)) {
471 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
476 if ( movecwd( vol, dir ) < 0 ) {
483 static int deletedir(char *dir)
485 char path[MAXPATHLEN + 1];
491 if ((len = strlen(dir)) > sizeof(path))
495 if ((dp = opendir(dir)) == NULL)
501 while ((de = readdir(dp))) {
502 /* skip this and previous directory */
503 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
506 strncpy(path + len, de->d_name, sizeof(path) - len);
507 if (stat(path, &st) == 0) {
508 if (S_ISDIR(st.st_mode)) {
509 if ((err = deletedir(path)) < 0) {
513 } else if (unlink(path) < 0) {
516 continue; /* somebody went and deleted it behind our backs. */
534 /* okay. the directory is empty. delete it. note: we already got rid
536 if (rmdir(dir) < 0) {
540 case ENOTEMPTY : /* should never happen */
541 return( AFPERR_DIRNEMPT );
544 return( AFPERR_ACCESS );
548 return( AFPERR_PARAM );
554 /* do a recursive copy. */
555 static int copydir(char *src, char *dst, int noadouble)
557 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
564 /* doesn't exist or the path is too long. */
565 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
566 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
567 ((dp = opendir(src)) == NULL))
570 /* try to create the destination directory */
571 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
575 return( AFPERR_NOOBJ );
577 return( AFPERR_VLOCK );
580 return( AFPERR_ACCESS );
582 return( AFPERR_EXIST );
585 return( AFPERR_DFULL );
587 return( AFPERR_PARAM );
591 /* set things up to copy */
599 while ((de = readdir(dp))) {
600 /* skip this and previous directory */
601 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
604 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
605 if (stat(spath, &st) == 0) {
606 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
608 if (S_ISDIR(st.st_mode)) {
609 if ((err = copydir(spath, dpath, noadouble)) < 0)
611 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
615 /* keep the same time stamp. */
616 ut.actime = ut.modtime = st.st_mtime;
622 /* keep the same time stamp. */
623 if (stat(src, &st) == 0) {
624 ut.actime = ut.modtime = st.st_mtime;
634 /* --- public functions follow --- */
636 /* NOTE: we start off with at least one node (the root directory). */
637 struct dir *dirinsert( vol, dir )
643 if ((node = dir_insert(vol, dir)))
646 /* recolor the tree. the current node is red. */
647 dir->d_color = DIRTREE_COLOR_RED;
649 /* parent of this node has to be black. if the parent node
650 * is red, then we have a grandparent. */
651 while ((dir != vol->v_root) &&
652 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
653 /* are we on the left tree? */
654 if (dir->d_back == dir->d_back->d_back->d_left) {
655 node = dir->d_back->d_back->d_right; /* get the right node */
656 if (node->d_color == DIRTREE_COLOR_RED) {
657 /* we're red. we need to change to black. */
658 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
659 node->d_color = DIRTREE_COLOR_BLACK;
660 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
661 dir = dir->d_back->d_back; /* finished. go up. */
663 if (dir == dir->d_back->d_right) {
665 dir_leftrotate(vol, dir);
667 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
668 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
669 dir_rightrotate(vol, dir->d_back->d_back);
672 node = dir->d_back->d_back->d_left;
673 if (node->d_color == DIRTREE_COLOR_RED) {
674 /* we're red. we need to change to black. */
675 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
676 node->d_color = DIRTREE_COLOR_BLACK;
677 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
678 dir = dir->d_back->d_back; /* finished. ascend */
680 if (dir == dir->d_back->d_left) {
682 dir_rightrotate(vol, dir);
684 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
685 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
686 dir_leftrotate(vol, dir->d_back->d_back);
691 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
695 /* free everything down. we don't bother to recolor as this is only
696 * called to free the entire tree */
700 if (!dir || (dir == SENTINEL))
703 if ( dir->d_left != SENTINEL ) {
704 dirfree( dir->d_left );
706 if ( dir->d_right != SENTINEL ) {
707 dirfree( dir->d_right );
710 if (dir != SENTINEL) {
717 struct dir *dirnew(const int len)
721 dir = (struct dir *) calloc(1, sizeof( struct dir ));
725 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
730 dir->d_left = dir->d_right = SENTINEL;
731 dir->d_next = dir->d_prev = dir;
736 /* XXX: this needs to be changed to handle path types */
737 char * cname( vol, dir, cpath )
738 const struct vol *vol;
743 static char path[ MAXPATHLEN + 1];
749 if ( *data++ != 2 ) { /* path type */
752 len = (unsigned char) *data++;
758 if ( !extend && movecwd( vol, dir ) < 0 ) {
764 if ( *data == '\0' ) {
769 while ( *data == '\0' && len > 0 ) {
770 if ( dir->d_parent == NULL ) {
778 /* would this be faster with strlen + strncpy? */
780 while ( *data != '\0' && len > 0 ) {
785 /* short cut bits by chopping off a trailing \0. this also
786 makes the traversal happy w/ filenames at the end of the
793 * Dung Nguyen <ntd@adb.fr>
795 * AFPD cannot handle paths with "::" if the "::" notation is
796 * not at the beginning of the path. The following path will not
797 * be interpreted correctly:
799 * :a:b:::c: (directory c at the same level as directory a) */
807 if ( p != path ) { /* we got something */
811 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
814 cdir = (cdir == dir->d_child->d_prev) ? NULL :
817 if ( cdir == NULL ) {
819 if ( movecwd( vol, dir ) < 0 ) {
822 cdir = extenddir( vol, dir, path );
826 cdir = extenddir( vol, dir, path );
829 if ( cdir == NULL ) {
843 * Move curdir to dir, with a possible chdir()
845 int movecwd( vol, dir)
846 const struct vol *vol;
849 char path[MAXPATHLEN + 1];
854 if ( dir == curdir ) {
857 if ( dir->d_did == DIRDID_ROOT_PARENT) {
861 p = path + sizeof(path) - 1;
864 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
866 u = mtoupath(vol, d->d_name );
873 n = strlen( vol->v_path );
875 strncpy( p, vol->v_path, n );
877 if ( chdir( p ) < 0 ) {
884 int getdirparams(const struct vol *vol,
886 char *upath, struct dir *dir, struct stat *st,
887 char *buf, int *buflen )
891 char *data, *nameoff = NULL;
894 int bit = 0, isad = 1;
899 memset(&uidgid, 0, sizeof(uidgid));
900 #endif /* FORCE_UIDGID */
902 memset(&ad, 0, sizeof(ad));
905 save_uidgid ( &uidgid );
907 #endif /* FORCE_UIDGID */
909 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
910 DIRBITS | 0777, &ad) < 0 ) {
915 while ( bitmap != 0 ) {
916 while (( bitmap & 1 ) == 0 ) {
924 ad_getattr(&ad, &ashort);
925 } else if (*upath == '.' && strcmp(upath, ".") &&
926 strcmp(upath, "..")) {
927 ashort = htons(ATTRBIT_INVISIBLE);
930 memcpy( data, &ashort, sizeof( ashort ));
931 data += sizeof( ashort );
935 if ( dir->d_did == DIRDID_ROOT) {
936 aint = DIRDID_ROOT_PARENT;
937 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
940 aint = dir->d_parent->d_did;
942 memcpy( data, &aint, sizeof( aint ));
943 data += sizeof( aint );
947 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
948 aint = AD_DATE_FROM_UNIX(st->st_mtime);
949 memcpy( data, &aint, sizeof( aint ));
950 data += sizeof( aint );
954 aint = AD_DATE_FROM_UNIX(st->st_mtime);
955 memcpy( data, &aint, sizeof( aint ));
956 data += sizeof( aint );
960 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
961 aint = AD_DATE_START;
962 memcpy( data, &aint, sizeof( aint ));
963 data += sizeof( aint );
968 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
969 } else { /* no appledouble */
970 memset( data, 0, 32 );
971 /* set default view -- this also gets done in ad_open() */
972 ashort = htons(FINDERINFO_CLOSEDVIEW);
973 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
975 /* dot files are by default invisible */
976 if (*upath == '.' && strcmp(upath, ".") &&
977 strcmp(upath, "..")) {
978 ashort = htons(FINDERINFO_INVISIBLE);
979 memcpy(data + FINDERINFO_FRFLAGOFF,
980 &ashort, sizeof(ashort));
987 if (dir->d_name) /* root of parent can have a null name */
990 memset(data, 0, sizeof(u_int16_t));
991 data += sizeof( u_int16_t );
995 memset(data, 0, sizeof(u_int16_t));
996 data += sizeof( u_int16_t );
1000 memcpy( data, &dir->d_did, sizeof( aint ));
1001 data += sizeof( aint );
1004 case DIRPBIT_OFFCNT :
1006 /* this needs to handle current directory access rights */
1007 if ((dp = opendir( upath ))) {
1008 while (( de = readdir( dp )) != NULL ) {
1009 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
1012 if (!validupath(vol, de->d_name))
1015 /* check for vetoed filenames */
1016 if (veto_file(vol->v_veto, de->d_name))
1019 /* now check against too long a filename */
1020 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
1027 ashort = htons( ashort );
1028 memcpy( data, &ashort, sizeof( ashort ));
1029 data += sizeof( ashort );
1033 aint = htonl(st->st_uid);
1034 memcpy( data, &aint, sizeof( aint ));
1035 data += sizeof( aint );
1039 aint = htonl(st->st_gid);
1040 memcpy( data, &aint, sizeof( aint ));
1041 data += sizeof( aint );
1044 case DIRPBIT_ACCESS :
1045 utommode( st, &ma );
1046 #ifndef SENDFILE_FLAVOR_LINUX /* ignore this section if it's linux */
1048 accessmode( upath, &ma, dir );
1049 #endif /* HAVE_ACCESS */
1050 #endif /* SENDFILE_FLAVOR_LINUX */
1051 #ifdef AFS /* If only AFS defined, access() works only for AFS filesystems */
1052 afsmode( upath, &ma, dir );
1054 *data++ = ma.ma_user;
1055 *data++ = ma.ma_world;
1056 *data++ = ma.ma_group;
1057 *data++ = ma.ma_owner;
1060 /* Client has requested the ProDOS information block.
1061 Just pass back the same basic block for all
1062 directories. <shirsch@ibm.net> */
1063 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1066 ashort = htons( 0x0200 );
1067 memcpy( data, &ashort, sizeof( ashort ));
1068 data += sizeof( ashort );
1069 memset( data, 0, sizeof( ashort ));
1070 data += sizeof( ashort );
1075 ad_close( &ad, ADFLAGS_HF );
1078 restore_uidgid ( &uidgid );
1079 #endif /* FORCE_UIDGID */
1080 return( AFPERR_BITMAP );
1086 ashort = htons( data - buf );
1087 memcpy( nameoff, &ashort, sizeof( ashort ));
1089 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1093 memcpy( data, dir->d_name, aint );
1097 ad_close( &ad, ADFLAGS_HF );
1099 *buflen = data - buf;
1103 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1106 int ibuflen, *rbuflen;
1111 u_int16_t vid, bitmap;
1117 memcpy( &vid, ibuf, sizeof( vid ));
1118 ibuf += sizeof( vid );
1120 if (( vol = getvolbyvid( vid )) == NULL ) {
1121 return( AFPERR_PARAM );
1124 if (vol->v_flags & AFPVOL_RO)
1125 return AFPERR_VLOCK;
1127 memcpy( &did, ibuf, sizeof( did ));
1128 ibuf += sizeof( int );
1130 if (( dir = dirsearch( vol, did )) == NULL ) {
1131 return( AFPERR_NOOBJ );
1134 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1135 bitmap = ntohs( bitmap );
1136 ibuf += sizeof( bitmap );
1138 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1139 return( AFPERR_NOOBJ );
1143 * If ibuf is odd, make it even.
1145 if ((u_long)ibuf & 1 ) {
1149 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1150 setvoltime(obj, vol );
1155 int setdirparams(const struct vol *vol,
1156 char *path, u_int16_t bitmap, char *buf )
1162 int bit = 0, aint, isad = 1;
1163 u_int16_t ashort, bshort;
1168 memset(&uidgid, 0, sizeof(uidgid));
1169 #endif /* FORCE_UIDGID */
1171 upath = mtoupath(vol, path);
1172 memset(&ad, 0, sizeof(ad));
1174 save_uidgid ( &uidgid );
1175 #endif /* FORCE_UIDGID */
1176 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1177 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1179 * Check to see what we're trying to set. If it's anything
1180 * but ACCESS, UID, or GID, give an error. If it's any of those
1181 * three, we don't need the ad to be open, so just continue.
1183 * note: we also don't need to worry about mdate. also, be quiet
1184 * if we're using the noadouble option.
1186 if (!vol_noadouble(vol) && (bitmap &
1187 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1188 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1190 restore_uidgid ( &uidgid );
1191 #endif /* FORCE_UIDGID */
1192 return AFPERR_ACCESS;
1198 * Check to see if a create was necessary. If it was, we'll want
1199 * to set our name, etc.
1201 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1202 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1203 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1204 ad_getentrylen( &ad, ADEID_NAME ));
1208 while ( bitmap != 0 ) {
1209 while (( bitmap & 1 ) == 0 ) {
1217 memcpy( &ashort, buf, sizeof( ashort ));
1218 ad_getattr(&ad, &bshort);
1219 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1220 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1224 ad_setattr(&ad, bshort);
1226 buf += sizeof( ashort );
1229 case DIRPBIT_CDATE :
1231 memcpy(&aint, buf, sizeof(aint));
1232 ad_setdate(&ad, AD_DATE_CREATE, aint);
1234 buf += sizeof( aint );
1237 case DIRPBIT_MDATE :
1238 memcpy(&aint, buf, sizeof(aint));
1240 ad_setdate(&ad, AD_DATE_MODIFY, aint);
1241 ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1243 buf += sizeof( aint );
1246 case DIRPBIT_BDATE :
1248 memcpy(&aint, buf, sizeof(aint));
1249 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1251 buf += sizeof( aint );
1254 case DIRPBIT_FINFO :
1256 * Alright, we admit it, this is *really* sick!
1257 * The 4 bytes that we don't copy, when we're dealing
1258 * with the root of a volume, are the directory's
1259 * location information. This eliminates that annoying
1260 * behavior one sees when mounting above another mount
1264 if ( curdir->d_did == DIRDID_ROOT ) {
1265 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1266 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1268 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1274 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1275 memcpy( &aint, buf, sizeof(aint));
1276 buf += sizeof( aint );
1277 if ( (curdir->d_did == DIRDID_ROOT) &&
1278 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1282 err = AFPERR_ACCESS;
1283 goto setdirparam_done;
1287 goto setdirparam_done;
1290 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1294 goto setdirparam_done;
1299 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1303 err = AFPERR_ACCESS;
1304 goto setdirparam_done;
1308 goto setdirparam_done;
1311 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1318 memcpy( &aint, buf, sizeof( aint ));
1319 buf += sizeof( aint );
1320 if (curdir->d_did == DIRDID_ROOT)
1321 setdeskowner( -1, ntohl(aint) );
1323 #if 0 /* don't error if we can't set the desktop owner. */
1327 err = AFPERR_ACCESS;
1328 goto setdirparam_done;
1332 goto setdirparam_done;
1335 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1338 goto setdirparam_done;
1344 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1348 err = AFPERR_ACCESS;
1349 goto setdirparam_done;
1353 goto setdirparam_done;
1356 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1363 case DIRPBIT_ACCESS :
1364 ma.ma_user = *buf++;
1365 ma.ma_world = *buf++;
1366 ma.ma_group = *buf++;
1367 ma.ma_owner = *buf++;
1369 if (curdir->d_did == DIRDID_ROOT)
1370 setdeskmode(mtoumode( &ma ));
1371 #if 0 /* don't error if we can't set the desktop mode */
1375 err = AFPERR_ACCESS;
1376 goto setdirparam_done;
1379 goto setdirparam_done;
1381 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1385 goto setdirparam_done;
1390 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1391 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1395 err = AFPERR_ACCESS;
1396 goto setdirparam_done;
1399 goto setdirparam_done;
1401 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1404 goto setdirparam_done;
1409 /* Ignore what the client thinks we should do to the
1410 ProDOS information block. Skip over the data and
1411 report nothing amiss. <shirsch@ibm.net> */
1412 case DIRPBIT_PDINFO :
1417 err = AFPERR_BITMAP;
1418 goto setdirparam_done;
1429 ad_flush( &ad, ADFLAGS_HF );
1430 ad_close( &ad, ADFLAGS_HF );
1434 restore_uidgid ( &uidgid );
1435 #endif /* FORCE_UIDGID */
1439 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1442 int ibuflen, *rbuflen;
1454 memset(&uidgid, 0, sizeof(uidgid));
1455 #endif /* FORCE_UIDGID */
1460 memcpy( &vid, ibuf, sizeof( vid ));
1461 ibuf += sizeof( vid );
1462 if (( vol = getvolbyvid( vid )) == NULL ) {
1463 return( AFPERR_PARAM );
1466 if (vol->v_flags & AFPVOL_RO)
1467 return AFPERR_VLOCK;
1469 memcpy( &did, ibuf, sizeof( did ));
1470 ibuf += sizeof( did );
1471 if (( dir = dirsearch( vol, did )) == NULL ) {
1472 return( AFPERR_NOOBJ );
1475 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1478 return( AFPERR_ACCESS );
1480 return( AFPERR_EXIST );
1482 return( AFPERR_NOOBJ );
1486 upath = mtoupath(vol, path);
1488 /* check for illegal bits in the unix filename */
1489 if (!wincheck(vol, upath))
1490 return AFPERR_PARAM;
1492 if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1493 return AFPERR_PARAM;
1495 if (!validupath(vol, upath))
1496 return AFPERR_EXIST;
1498 /* check for vetoed filenames */
1499 if (veto_file(vol->v_veto, upath))
1500 return AFPERR_EXIST;
1503 save_uidgid ( &uidgid );
1505 #endif /* FORCE_UIDGID */
1507 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1509 restore_uidgid ( &uidgid );
1510 #endif /* FORCE_UIDGID */
1513 return( AFPERR_NOOBJ );
1515 return( AFPERR_VLOCK );
1517 return( AFPERR_ACCESS );
1519 return( AFPERR_EXIST );
1522 return( AFPERR_DFULL );
1524 return( AFPERR_PARAM );
1528 if (stat(upath, &st) < 0) {
1530 restore_uidgid ( &uidgid );
1531 #endif /* FORCE_UIDGID */
1535 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1536 strlen(upath), &st)) == NULL) {
1538 restore_uidgid ( &uidgid );
1539 #endif /* FORCE_UIDGID */
1543 if ( movecwd( vol, dir ) < 0 ) {
1545 restore_uidgid ( &uidgid );
1546 #endif /* FORCE_UIDGID */
1547 return( AFPERR_PARAM );
1550 memset(&ad, 0, sizeof(ad));
1551 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1552 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1553 if (vol_noadouble(vol))
1554 goto createdir_done;
1556 restore_uidgid ( &uidgid );
1557 #endif /* FORCE_UIDGID */
1558 return( AFPERR_ACCESS );
1561 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1562 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1563 ad_getentrylen( &ad, ADEID_NAME ));
1564 ad_flush( &ad, ADFLAGS_HF );
1565 ad_close( &ad, ADFLAGS_HF );
1568 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1569 *rbuflen = sizeof( u_int32_t );
1570 setvoltime(obj, vol );
1572 restore_uidgid ( &uidgid );
1573 #endif /* FORCE_UIDGID */
1578 int renamedir(src, dst, dir, newparent, newname, noadouble)
1579 char *src, *dst, *newname;
1580 struct dir *dir, *newparent;
1581 const int noadouble;
1588 /* existence check moved to afp_moveandrename */
1589 if ( rename( src, dst ) < 0 ) {
1592 return( AFPERR_NOOBJ );
1594 return( AFPERR_ACCESS );
1596 return AFPERR_VLOCK;
1598 /* tried to move directory into a subdirectory of itself */
1599 return AFPERR_CANTMOVE;
1601 /* this needs to copy and delete. bleah. that means we have
1602 * to deal with entire directory hierarchies. */
1603 if ((err = copydir(src, dst, noadouble)) < 0) {
1607 if ((err = deletedir(src)) < 0)
1611 return( AFPERR_PARAM );
1615 memset(&ad, 0, sizeof(ad));
1616 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1620 len = strlen(newname);
1621 goto renamedir_done;
1623 return( AFPERR_NOOBJ );
1625 return( AFPERR_ACCESS );
1627 return( AFPERR_PARAM );
1630 len = strlen( newname );
1631 ad_setentrylen( &ad, ADEID_NAME, len );
1632 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1633 ad_flush( &ad, ADFLAGS_HF );
1634 ad_close( &ad, ADFLAGS_HF );
1637 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1638 LOG(log_error, logtype_afpd, "renamedir: realloc: %s", strerror(errno) );
1642 strcpy( dir->d_name, newname );
1644 if (( parent = dir->d_parent ) == NULL ) {
1647 if ( parent == newparent ) {
1651 /* detach from old parent and add to new one. */
1652 dirchildremove(parent, dir);
1653 dir->d_parent = newparent;
1654 dirchildadd(newparent, dir);
1658 #define DOT_APPLEDOUBLE_LEN 13
1659 /* delete an empty directory */
1660 int deletecurdir( vol, path, pathlen )
1661 const struct vol *vol;
1674 memset(&uidgid, 0, sizeof(uidgid));
1675 #endif /* FORCE_UIDGID */
1677 if ( curdir->d_parent == NULL ) {
1678 return( AFPERR_ACCESS );
1681 if ( curdir->d_child != NULL ) {
1682 return( AFPERR_DIRNEMPT );
1688 save_uidgid ( &uidgid );
1690 #endif /* FORCE_UIDGID */
1692 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1693 DIRBITS | 0777, &ad) == 0 ) {
1695 ad_getattr(&ad, &ashort);
1696 ad_close( &ad, ADFLAGS_HF );
1697 if ((ashort & htons(ATTRBIT_NODELETE))) {
1699 restore_uidgid ( &uidgid );
1700 #endif /* FORCE_UIDGID */
1701 return AFPERR_OLOCK;
1705 /* delete stray .AppleDouble files. this happens to get .Parent files
1707 if ((dp = opendir(".AppleDouble"))) {
1708 strcpy(path, ".AppleDouble/");
1709 while ((de = readdir(dp))) {
1710 /* skip this and previous directory */
1711 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1714 /* bail if the file exists in the current directory.
1715 * note: this will not fail with dangling symlinks */
1716 if (stat(de->d_name, &st) == 0) {
1719 restore_uidgid ( &uidgid );
1720 #endif /* FORCE_UIDGID */
1721 return AFPERR_DIRNEMPT;
1724 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1725 if (unlink(path) < 0) {
1731 restore_uidgid ( &uidgid );
1732 #endif /* FORCE_UIDGID */
1733 return( AFPERR_ACCESS );
1736 restore_uidgid ( &uidgid );
1737 #endif /* FORCE_UIDGID */
1738 return AFPERR_VLOCK;
1743 restore_uidgid ( &uidgid );
1744 #endif /* FORCE_UIDGID */
1745 return( AFPERR_PARAM );
1752 if ( rmdir( ".AppleDouble" ) < 0 ) {
1758 restore_uidgid ( &uidgid );
1759 #endif /* FORCE_UIDGID */
1760 return( AFPERR_DIRNEMPT );
1763 restore_uidgid ( &uidgid );
1764 #endif /* FORCE_UIDGID */
1765 return AFPERR_VLOCK;
1769 restore_uidgid ( &uidgid );
1770 #endif /* FORCE_UIDGID */
1771 return( AFPERR_ACCESS );
1774 restore_uidgid ( &uidgid );
1775 #endif /* FORCE_UIDGID */
1776 return( AFPERR_PARAM );
1780 /* now get rid of dangling symlinks */
1781 if ((dp = opendir("."))) {
1782 while ((de = readdir(dp))) {
1783 /* skip this and previous directory */
1784 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1787 /* bail if it's not a symlink */
1788 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1790 restore_uidgid ( &uidgid );
1791 #endif /* FORCE_UIDGID */
1792 return AFPERR_DIRNEMPT;
1795 if (unlink(de->d_name) < 0) {
1800 restore_uidgid ( &uidgid );
1801 #endif /* FORCE_UIDGID */
1802 return( AFPERR_ACCESS );
1805 restore_uidgid ( &uidgid );
1806 #endif /* FORCE_UIDGID */
1807 return AFPERR_VLOCK;
1812 restore_uidgid ( &uidgid );
1813 #endif /* FORCE_UIDGID */
1814 return( AFPERR_PARAM );
1821 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1823 restore_uidgid ( &uidgid );
1824 #endif /* FORCE_UIDGID */
1825 return( AFPERR_NOOBJ );
1828 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1832 restore_uidgid ( &uidgid );
1833 #endif /* FORCE_UIDGID */
1834 return( AFPERR_NOOBJ );
1837 restore_uidgid ( &uidgid );
1838 #endif /* FORCE_UIDGID */
1839 return( AFPERR_DIRNEMPT );
1843 restore_uidgid ( &uidgid );
1844 #endif /* FORCE_UIDGID */
1845 return( AFPERR_ACCESS );
1848 restore_uidgid ( &uidgid );
1849 #endif /* FORCE_UIDGID */
1850 return AFPERR_VLOCK;
1853 restore_uidgid ( &uidgid );
1854 #endif /* FORCE_UIDGID */
1855 return( AFPERR_PARAM );
1859 dirchildremove(curdir, fdir);
1861 cnid_delete(vol->v_db, fdir->d_did);
1862 #endif /* CNID_DB */
1863 dir_remove( vol, fdir );
1866 restore_uidgid ( &uidgid );
1867 #endif /* FORCE_UIDGID */
1871 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1874 int ibuflen, *rbuflen;
1883 sfunc = (unsigned char) *ibuf++;
1884 memcpy( &id, ibuf, sizeof( id ));
1891 if (( pw = getpwuid( id )) == NULL ) {
1893 return( AFPERR_NOITEM );
1899 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1901 return( AFPERR_NOITEM );
1908 return( AFPERR_PARAM );
1911 len = strlen( name );
1920 memcpy( rbuf, name, len );
1926 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1929 int ibuflen, *rbuflen;
1937 sfunc = (unsigned char) *ibuf++;
1938 len = (unsigned char) *ibuf++;
1944 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1946 return( AFPERR_NOITEM );
1952 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1954 return( AFPERR_NOITEM );
1960 return( AFPERR_PARAM );
1966 memcpy( rbuf, &id, sizeof( id ));
1967 *rbuflen = sizeof( id );
1971 /* variable DID support */
1972 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1975 int ibuflen, *rbuflen;
1986 /* do nothing as dids are static for the life of the process. */
1990 memcpy(&vid, ibuf, sizeof( vid ));
1991 ibuf += sizeof( vid );
1992 if (( vol = getvolbyvid( vid )) == NULL ) {
1993 return( AFPERR_PARAM );
1996 memcpy( &did, ibuf, sizeof( did ));
1997 ibuf += sizeof( did );
1998 if (( dir = dirsearch( vol, did )) == NULL ) {
1999 return( AFPERR_PARAM );
2002 /* dir_remove -- deletedid */
2008 /* did creation gets done automatically */
2009 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2012 int ibuflen, *rbuflen;
2015 struct dir *dir, *parentdir;
2023 memset(&uidgid, 0, sizeof(uidgid));
2024 #endif /* FORCE_UIDGID */
2029 memcpy(&vid, ibuf, sizeof(vid));
2030 ibuf += sizeof( vid );
2032 if (( vol = getvolbyvid( vid )) == NULL ) {
2033 return( AFPERR_PARAM );
2036 memcpy(&did, ibuf, sizeof(did));
2037 ibuf += sizeof(did);
2039 if (( parentdir = dirsearch( vol, did )) == NULL ) {
2040 return( AFPERR_NOOBJ );
2043 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2046 return( AFPERR_ACCESS );
2048 return( AFPERR_NOOBJ );
2052 /* see if we already have the directory. */
2053 upath = mtoupath(vol, path);
2054 if ( stat( upath, &st ) < 0 ) {
2055 return( AFPERR_NOOBJ );
2058 dir = parentdir->d_child;
2060 if (strdiacasecmp(dir->d_name, path) == 0) {
2061 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2062 *rbuflen = sizeof(dir->d_did);
2065 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
2069 save_uidgid ( &uidgid );
2071 #endif /* FORCE_UIDGID */
2073 /* we don't already have a did. add one in. */
2074 if ((dir = adddir(vol, parentdir, path, strlen(path),
2075 upath, strlen(upath), &st)) == NULL) {
2077 restore_uidgid ( &uidgid );
2078 #endif /* FORCE_UIDGID */
2082 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2083 *rbuflen = sizeof(dir->d_did);
2085 restore_uidgid ( &uidgid );
2086 #endif /* FORCE_UIDGID */