2 * $Id: directory.c,v 1.41.2.2 2002-11-07 17:10:02 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 <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 #define SENTINEL (&sentinel)
65 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
66 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
67 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
68 NULL, NULL, NULL, NULL, NULL, 0, 0, NULL };
70 /* (from IM: Toolbox Essentials)
71 * dirFinderInfo (DInfo) fields:
73 * frRect 8 folder's window rectangle
75 * frLocation 4 folder's location in window
76 * frView 2 folder's view (default == closedView (256))
78 * extended dirFinderInfo (DXInfo) fields:
79 * frScroll 4 scroll position
80 * frOpenChain: 4 directory ID chain of open folders
81 * frScript: 1 script flag and code
82 * frXFlags: 1 reserved
83 * frComment: 2 comment ID
84 * frPutAway: 4 home directory ID
88 * redid did assignment for directories. now we use red-black trees.
93 const struct vol *vol;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
111 while ( dir != SENTINEL ) {
112 if (dir->d_did == did)
113 return dir->d_name ? dir : NULL;
114 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
119 /* -----------------------------------------
120 * if did is not in the cache resolve it with cnid
124 dirlookup( vol, did )
125 const struct vol *vol;
132 static char path[MAXPATHLEN + 1];
136 static char buffer[12 + MAXPATHLEN + 1];
137 int buflen = 12 + MAXPATHLEN + 1;
140 ret = dirsearch(vol, did);
145 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
148 ptr = path + MAXPATHLEN;
149 mpath = utompath(vol, upath);
151 pathlen = len; /* no 0 in the last part */
153 strcpy(ptr - len, mpath);
156 ret = dirsearch(vol,id);
160 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL)
162 mpath = utompath(vol, upath);
163 len = strlen(mpath) + 1;
167 strcpy(ptr - len, mpath);
172 *ptr = (unsigned char)pathlen;
175 /* cname is not efficient */
176 if (cname( vol, ret, &ptr ) == NULL )
179 return dirsearch(vol, did);
182 /* --------------------------- */
183 /* rotate the tree to the left */
184 static void dir_leftrotate(vol, dir)
188 struct dir *right = dir->d_right;
190 /* whee. move the right's left tree into dir's right tree */
191 dir->d_right = right->d_left;
192 if (right->d_left != SENTINEL)
193 right->d_left->d_back = dir;
195 if (right != SENTINEL) {
196 right->d_back = dir->d_back;
200 if (!dir->d_back) /* no parent. move the right tree to the top. */
202 else if (dir == dir->d_back->d_left) /* we were on the left */
203 dir->d_back->d_left = right;
205 dir->d_back->d_right = right; /* we were on the right */
207 /* re-insert dir on the left tree */
214 /* rotate the tree to the right */
215 static void dir_rightrotate(vol, dir)
219 struct dir *left = dir->d_left;
221 /* whee. move the left's right tree into dir's left tree */
222 dir->d_left = left->d_right;
223 if (left->d_right != SENTINEL)
224 left->d_right->d_back = dir;
226 if (left != SENTINEL) {
227 left->d_back = dir->d_back;
231 if (!dir->d_back) /* no parent. move the left tree to the top. */
233 else if (dir == dir->d_back->d_right) /* we were on the right */
234 dir->d_back->d_right = left;
236 dir->d_back->d_left = left; /* we were on the left */
238 /* re-insert dir on the right tree */
244 /* recolor after a removal */
245 static struct dir *dir_rmrecolor(vol, dir)
251 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
252 /* are we on the left tree? */
253 if (dir == dir->d_back->d_left) {
254 leaf = dir->d_back->d_right; /* get right side */
255 if (leaf->d_color == DIRTREE_COLOR_RED) {
256 /* we're red. we need to change to black. */
257 leaf->d_color = DIRTREE_COLOR_BLACK;
258 dir->d_back->d_color = DIRTREE_COLOR_RED;
259 dir_leftrotate(vol, dir->d_back);
260 leaf = dir->d_back->d_right;
263 /* right leaf has black end nodes */
264 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
265 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
266 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
267 dir = dir->d_back; /* ascend */
269 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
270 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
271 leaf->d_color = DIRTREE_COLOR_RED;
272 dir_rightrotate(vol, leaf);
273 leaf = dir->d_back->d_right;
275 leaf->d_color = dir->d_back->d_color;
276 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
277 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
278 dir_leftrotate(vol, dir->d_back);
281 } else { /* right tree */
282 leaf = dir->d_back->d_left; /* left tree */
283 if (leaf->d_color == DIRTREE_COLOR_RED) {
284 leaf->d_color = DIRTREE_COLOR_BLACK;
285 dir->d_back->d_color = DIRTREE_COLOR_RED;
286 dir_rightrotate(vol, dir->d_back);
287 leaf = dir->d_back->d_left;
290 /* left leaf has black end nodes */
291 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
292 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
293 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
294 dir = dir->d_back; /* ascend */
296 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
297 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
298 leaf->d_color = DIRTREE_COLOR_RED;
299 dir_leftrotate(vol, leaf);
300 leaf = dir->d_back->d_left;
302 leaf->d_color = dir->d_back->d_color;
303 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
304 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
305 dir_rightrotate(vol, dir->d_back);
310 dir->d_color = DIRTREE_COLOR_BLACK;
317 /* remove the node from the tree. this is just like insertion, but
318 * different. actually, it has to worry about a bunch of things that
319 * insertion doesn't care about. */
320 static void dir_remove( vol, dir )
325 struct ofork *of, *last;
326 struct dir *node, *leaf;
327 #endif /* REMOVE_NODES */
329 if (!dir || (dir == SENTINEL))
332 /* i'm not sure if it really helps to delete stuff. */
336 #else /* ! REMOVE_NODES */
338 /* go searching for a node with at most one child */
339 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
343 while (node->d_left != SENTINEL)
348 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
351 leaf->d_back = node->d_back;
354 } else if (node == node->d_back->d_left) { /* left tree */
355 node->d_back->d_left = leaf;
357 node->d_back->d_right = leaf;
360 /* we want to free node, but we also want to free the data in dir.
361 * currently, that's d_name and the directory traversal bits.
362 * we just copy the necessary bits and then fix up all the
363 * various pointers to the directory. needless to say, there are
364 * a bunch of places that store the directory struct. */
366 struct dir save, *tmp;
368 memcpy(&save, dir, sizeof(save));
369 memcpy(dir, node, sizeof(struct dir));
371 /* restore the red-black bits */
372 dir->d_left = save.d_left;
373 dir->d_right = save.d_right;
374 dir->d_back = save.d_back;
375 dir->d_color = save.d_color;
377 if (node == vol->v_dir) {/* we may need to fix up this pointer */
379 rootpar.d_child = vol->v_dir;
381 /* if we aren't the root directory, we have parents and
382 * siblings to worry about */
383 if (dir->d_parent->d_child == node)
384 dir->d_parent->d_child = dir;
385 dir->d_next->d_prev = dir;
386 dir->d_prev->d_next = dir;
389 /* fix up children. */
393 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
396 if (node == curdir) /* another pointer to fixup */
399 /* we also need to fix up oforks. bleah */
400 if ((of = dir->d_ofork)) {
401 last = of->of_d_prev;
404 of = (last == of) ? NULL : of->of_d_next;
408 /* set the node's d_name */
409 node->d_name = save.d_name;
412 if (node->d_color == DIRTREE_COLOR_BLACK)
413 dir_rmrecolor(vol, leaf);
416 #endif /* ! REMOVE_NODES */
419 /* ---------------------------------------
420 * remove the node and its childs from the tree
422 * FIXME what about opened forks with refs to it?
423 * it's an afp specs violation because you can't delete
424 * an opened forks. Now afpd doesn't care about forks opened by other
425 * process. It's fixable within afpd if fnctl_lock, doable with smb and
426 * next to impossible for nfs and local filesystem access.
429 static void dir_invalidate( vol, dir )
430 const struct vol *vol;
434 /* v_root can't be deleted */
435 if (movecwd(vol, vol->v_root) < 0)
436 printf("Yuup cant change dir to v_root\n");
439 dirchildremove(dir->d_parent, dir);
440 dir_remove( vol, dir );
443 /* ------------------------------------ */
444 static struct dir *dir_insert(vol, dir)
445 const struct vol *vol;
451 while (pdir->d_did != dir->d_did ) {
452 if ( pdir->d_did > dir->d_did ) {
453 if ( pdir->d_left == SENTINEL ) {
460 if ( pdir->d_right == SENTINEL ) {
465 pdir = pdir->d_right;
473 * attempt to extend the current dir. tree to include path
474 * as a side-effect, movecwd to that point and return the new dir
478 extenddir( vol, dir, path )
486 p = mtoupath(vol, path );
487 if ( stat( p, &st ) != 0 ) {
490 if (!S_ISDIR(st.st_mode)) {
494 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
499 if ( movecwd( vol, dir ) < 0 ) {
506 static int deletedir(char *dir)
508 char path[MAXPATHLEN + 1];
514 if ((len = strlen(dir)) > sizeof(path))
518 if ((dp = opendir(dir)) == NULL)
524 while ((de = readdir(dp))) {
525 /* skip this and previous directory */
526 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
529 strncpy(path + len, de->d_name, sizeof(path) - len);
530 if (stat(path, &st) == 0) {
531 if (S_ISDIR(st.st_mode)) {
532 if ((err = deletedir(path)) < 0) {
536 } else if (unlink(path) < 0) {
539 continue; /* somebody went and deleted it behind our backs. */
557 /* okay. the directory is empty. delete it. note: we already got rid
559 if (rmdir(dir) < 0) {
563 case ENOTEMPTY : /* should never happen */
564 return( AFPERR_DIRNEMPT );
567 return( AFPERR_ACCESS );
571 return( AFPERR_PARAM );
577 /* do a recursive copy. */
578 static int copydir(char *src, char *dst, int noadouble)
580 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
587 /* doesn't exist or the path is too long. */
588 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
589 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
590 ((dp = opendir(src)) == NULL))
593 /* try to create the destination directory */
594 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
598 return( AFPERR_NOOBJ );
600 return( AFPERR_VLOCK );
603 return( AFPERR_ACCESS );
605 return( AFPERR_EXIST );
608 return( AFPERR_DFULL );
610 return( AFPERR_PARAM );
614 /* set things up to copy */
622 while ((de = readdir(dp))) {
623 /* skip this and previous directory */
624 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
627 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
628 if (stat(spath, &st) == 0) {
629 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
631 if (S_ISDIR(st.st_mode)) {
632 if ((err = copydir(spath, dpath, noadouble)) < 0)
634 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
638 /* keep the same time stamp. */
639 ut.actime = ut.modtime = st.st_mtime;
645 /* keep the same time stamp. */
646 if (stat(src, &st) == 0) {
647 ut.actime = ut.modtime = st.st_mtime;
657 /* --- public functions follow --- */
659 /* NOTE: we start off with at least one node (the root directory). */
660 struct dir *dirinsert( vol, dir )
666 if ((node = dir_insert(vol, dir)))
669 /* recolor the tree. the current node is red. */
670 dir->d_color = DIRTREE_COLOR_RED;
672 /* parent of this node has to be black. if the parent node
673 * is red, then we have a grandparent. */
674 while ((dir != vol->v_root) &&
675 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
676 /* are we on the left tree? */
677 if (dir->d_back == dir->d_back->d_back->d_left) {
678 node = dir->d_back->d_back->d_right; /* get the right node */
679 if (node->d_color == DIRTREE_COLOR_RED) {
680 /* we're red. we need to change to black. */
681 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
682 node->d_color = DIRTREE_COLOR_BLACK;
683 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
684 dir = dir->d_back->d_back; /* finished. go up. */
686 if (dir == dir->d_back->d_right) {
688 dir_leftrotate(vol, dir);
690 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
691 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
692 dir_rightrotate(vol, dir->d_back->d_back);
695 node = dir->d_back->d_back->d_left;
696 if (node->d_color == DIRTREE_COLOR_RED) {
697 /* we're red. we need to change to black. */
698 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
699 node->d_color = DIRTREE_COLOR_BLACK;
700 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
701 dir = dir->d_back->d_back; /* finished. ascend */
703 if (dir == dir->d_back->d_left) {
705 dir_rightrotate(vol, dir);
707 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
708 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
709 dir_leftrotate(vol, dir->d_back->d_back);
714 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
718 /* free everything down. we don't bother to recolor as this is only
719 * called to free the entire tree */
723 if (!dir || (dir == SENTINEL))
726 if ( dir->d_left != SENTINEL ) {
727 dirfree( dir->d_left );
729 if ( dir->d_right != SENTINEL ) {
730 dirfree( dir->d_right );
733 if (dir != SENTINEL) {
740 struct dir *dirnew(const int len)
744 dir = (struct dir *) calloc(1, sizeof( struct dir ));
748 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
753 dir->d_left = dir->d_right = SENTINEL;
754 dir->d_next = dir->d_prev = dir;
759 /* XXX: this needs to be changed to handle path types */
761 cname( vol, dir, cpath )
762 const struct vol *vol;
767 static char path[ MAXPATHLEN + 1];
775 if ( *data++ != 2 ) { /* path type */
778 len = (unsigned char) *data++;
785 if ( !extend && movecwd( vol, dir ) < 0 ) {
787 movecwd failed so dir is not there anymore.
788 FIXME Is it true with other errors?
789 if path == '\0' ==> the cpath parameter is that dir,
790 and maybe we are trying to recreate it! So we can't
794 if ( dir->d_did == DIRDID_ROOT_PARENT)
796 cdir = dir->d_parent;
797 dir_invalidate(vol, dir);
798 if (*path != '\0' || u == NULL) {
799 /* FIXME: if path != '\0' then extend != 0 ?
800 * u == NUL ==> cpath is something like:
805 if (movecwd(vol, cdir) < 0) {
806 printf("can't change to parent\n");
807 return NULL; /* give up the whole tree is out of synch*/
809 /* restore the previous token */
810 strncpy(path, u, olen);
816 if ( *data == '\0' ) {
822 while ( *data == '\0' && len > 0 ) {
823 if ( dir->d_parent == NULL ) {
831 /* would this be faster with strlen + strncpy? */
837 while ( *data != '\0' && len > 0 ) {
842 /* short cut bits by chopping off a trailing \0. this also
843 makes the traversal happy w/ filenames at the end of the
850 * Dung Nguyen <ntd@adb.fr>
852 * AFPD cannot handle paths with "::" if the "::" notation is
853 * not at the beginning of the path. The following path will not
854 * be interpreted correctly:
856 * :a:b:::c: (directory c at the same level as directory a) */
864 if ( p != path ) { /* we got something */
868 if ( strcmp( cdir->d_name, path ) == 0 ) {
871 cdir = (cdir == dir->d_child->d_prev) ? NULL :
874 if ( cdir == NULL ) {
876 /* if dir == curdir it always succeed,
877 even if curdir is deleted.
878 it's not a pb because it will failed in extenddir
880 if ( movecwd( vol, dir ) < 0 ) {
881 /* dir is not valid anymore
882 we delete dir from the cache and abort.
884 if ( dir->d_did != DIRDID_ROOT_PARENT)
885 dir_invalidate(vol, dir);
888 cdir = extenddir( vol, dir, path );
892 cdir = extenddir( vol, dir, path );
895 if ( cdir == NULL ) {
909 * Move curdir to dir, with a possible chdir()
911 int movecwd( vol, dir)
912 const struct vol *vol;
915 char path[MAXPATHLEN + 1];
920 if ( dir == curdir ) {
923 if ( dir->d_did == DIRDID_ROOT_PARENT) {
927 p = path + sizeof(path) - 1;
930 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
932 u = mtoupath(vol, d->d_name );
939 n = strlen( vol->v_path );
941 strncpy( p, vol->v_path, n );
943 if ( chdir( p ) < 0 ) {
950 int getdirparams(const struct vol *vol,
952 char *upath, struct dir *dir, struct stat *st,
953 char *buf, int *buflen )
957 char *data, *nameoff = NULL;
960 int bit = 0, isad = 1;
964 memset(&ad, 0, sizeof(ad));
966 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
967 DIRBITS | 0777, &ad) < 0 ) {
972 while ( bitmap != 0 ) {
973 while (( bitmap & 1 ) == 0 ) {
981 ad_getattr(&ad, &ashort);
982 } else if (*upath == '.' && strcmp(upath, ".") &&
983 strcmp(upath, "..")) {
984 ashort = htons(ATTRBIT_INVISIBLE);
987 ashort |= htons(ATTRBIT_SHARED);
988 memcpy( data, &ashort, sizeof( ashort ));
989 data += sizeof( ashort );
993 if ( dir->d_did == DIRDID_ROOT) {
994 aint = DIRDID_ROOT_PARENT;
995 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
998 aint = dir->d_parent->d_did;
1000 memcpy( data, &aint, sizeof( aint ));
1001 data += sizeof( aint );
1004 case DIRPBIT_CDATE :
1005 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1006 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1007 memcpy( data, &aint, sizeof( aint ));
1008 data += sizeof( aint );
1011 case DIRPBIT_MDATE :
1012 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1013 memcpy( data, &aint, sizeof( aint ));
1014 data += sizeof( aint );
1017 case DIRPBIT_BDATE :
1018 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1019 aint = AD_DATE_START;
1020 memcpy( data, &aint, sizeof( aint ));
1021 data += sizeof( aint );
1024 case DIRPBIT_FINFO :
1026 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1027 } else { /* no appledouble */
1028 memset( data, 0, 32 );
1029 /* set default view -- this also gets done in ad_open() */
1030 ashort = htons(FINDERINFO_CLOSEDVIEW);
1031 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1033 /* dot files are by default invisible */
1034 if (*upath == '.' && strcmp(upath, ".") &&
1035 strcmp(upath, "..")) {
1036 ashort = htons(FINDERINFO_INVISIBLE);
1037 memcpy(data + FINDERINFO_FRFLAGOFF,
1038 &ashort, sizeof(ashort));
1044 case DIRPBIT_LNAME :
1045 if (dir->d_name) /* root of parent can have a null name */
1048 memset(data, 0, sizeof(u_int16_t));
1049 data += sizeof( u_int16_t );
1052 case DIRPBIT_SNAME :
1053 memset(data, 0, sizeof(u_int16_t));
1054 data += sizeof( u_int16_t );
1058 memcpy( data, &dir->d_did, sizeof( aint ));
1059 data += sizeof( aint );
1062 case DIRPBIT_OFFCNT :
1064 /* this needs to handle current directory access rights */
1065 if ((dp = opendir( upath ))) {
1066 while (( de = readdir( dp )) != NULL ) {
1067 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
1070 if (!validupath(vol, de->d_name))
1073 /* check for vetoed filenames */
1074 if (veto_file(vol->v_veto, de->d_name))
1077 /* now check against too long a filename */
1078 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
1085 ashort = htons( ashort );
1086 memcpy( data, &ashort, sizeof( ashort ));
1087 data += sizeof( ashort );
1091 aint = htonl(st->st_uid);
1092 memcpy( data, &aint, sizeof( aint ));
1093 data += sizeof( aint );
1097 aint = htonl(st->st_gid);
1098 memcpy( data, &aint, sizeof( aint ));
1099 data += sizeof( aint );
1102 case DIRPBIT_ACCESS :
1103 accessmode( upath, &ma, dir , st);
1105 *data++ = ma.ma_user;
1106 *data++ = ma.ma_world;
1107 *data++ = ma.ma_group;
1108 *data++ = ma.ma_owner;
1111 /* Client has requested the ProDOS information block.
1112 Just pass back the same basic block for all
1113 directories. <shirsch@ibm.net> */
1114 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1117 ashort = htons( 0x0200 );
1118 memcpy( data, &ashort, sizeof( ashort ));
1119 data += sizeof( ashort );
1120 memset( data, 0, sizeof( ashort ));
1121 data += sizeof( ashort );
1126 ad_close( &ad, ADFLAGS_HF );
1128 return( AFPERR_BITMAP );
1134 ashort = htons( data - buf );
1135 memcpy( nameoff, &ashort, sizeof( ashort ));
1137 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1141 memcpy( data, dir->d_name, aint );
1145 ad_close( &ad, ADFLAGS_HF );
1147 *buflen = data - buf;
1151 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1154 int ibuflen, *rbuflen;
1159 u_int16_t vid, bitmap;
1165 memcpy( &vid, ibuf, sizeof( vid ));
1166 ibuf += sizeof( vid );
1168 if (( vol = getvolbyvid( vid )) == NULL ) {
1169 return( AFPERR_PARAM );
1172 if (vol->v_flags & AFPVOL_RO)
1173 return AFPERR_VLOCK;
1175 memcpy( &did, ibuf, sizeof( did ));
1176 ibuf += sizeof( int );
1178 if (( dir = dirlookup( vol, did )) == NULL ) {
1179 return( AFPERR_NOOBJ );
1182 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1183 bitmap = ntohs( bitmap );
1184 ibuf += sizeof( bitmap );
1186 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1187 return( AFPERR_NOOBJ );
1190 if ( *path != '\0' ) {
1191 return( AFPERR_BADTYPE ); /* not a directory */
1195 * If ibuf is odd, make it even.
1197 if ((u_long)ibuf & 1 ) {
1201 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1202 setvoltime(obj, vol );
1208 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1210 * assume path == '\0' eg. it's a directory in canonical form
1212 int setdirparams(const struct vol *vol,
1213 char *path, u_int16_t bitmap, char *buf )
1221 int bit = 0, aint, isad = 1;
1222 u_int16_t ashort, bshort;
1224 int change_mdate = 0;
1225 int change_parent_mdate = 0;
1228 upath = mtoupath(vol, path);
1229 memset(&ad, 0, sizeof(ad));
1231 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1232 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1234 * Check to see what we're trying to set. If it's anything
1235 * but ACCESS, UID, or GID, give an error. If it's any of those
1236 * three, we don't need the ad to be open, so just continue.
1238 * note: we also don't need to worry about mdate. also, be quiet
1239 * if we're using the noadouble option.
1241 if (!vol_noadouble(vol) && (bitmap &
1242 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1243 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1244 return AFPERR_ACCESS;
1250 * Check to see if a create was necessary. If it was, we'll want
1251 * to set our name, etc.
1253 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1254 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1255 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1256 ad_getentrylen( &ad, ADEID_NAME ));
1260 while ( bitmap != 0 ) {
1261 while (( bitmap & 1 ) == 0 ) {
1270 memcpy( &ashort, buf, sizeof( ashort ));
1271 ad_getattr(&ad, &bshort);
1272 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1273 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1277 ad_setattr(&ad, bshort);
1278 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1279 change_parent_mdate = 1;
1281 buf += sizeof( ashort );
1284 case DIRPBIT_CDATE :
1287 memcpy(&aint, buf, sizeof(aint));
1288 ad_setdate(&ad, AD_DATE_CREATE, aint);
1290 buf += sizeof( aint );
1293 case DIRPBIT_MDATE :
1294 memcpy(&newdate, buf, sizeof(newdate));
1295 buf += sizeof( newdate );
1298 case DIRPBIT_BDATE :
1301 memcpy(&aint, buf, sizeof(aint));
1302 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1304 buf += sizeof( aint );
1307 case DIRPBIT_FINFO :
1310 * Alright, we admit it, this is *really* sick!
1311 * The 4 bytes that we don't copy, when we're dealing
1312 * with the root of a volume, are the directory's
1313 * location information. This eliminates that annoying
1314 * behavior one sees when mounting above another mount
1318 if ( curdir->d_did == DIRDID_ROOT ) {
1319 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1320 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1322 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1328 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1329 change_parent_mdate = 1;
1330 memcpy( &aint, buf, sizeof(aint));
1331 buf += sizeof( aint );
1332 if ( (curdir->d_did == DIRDID_ROOT) &&
1333 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1337 err = AFPERR_ACCESS;
1338 goto setdirparam_done;
1342 goto setdirparam_done;
1345 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1349 goto setdirparam_done;
1354 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1358 err = AFPERR_ACCESS;
1359 goto setdirparam_done;
1363 goto setdirparam_done;
1366 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1373 change_parent_mdate = 1;
1374 memcpy( &aint, buf, sizeof( aint ));
1375 buf += sizeof( aint );
1376 if (curdir->d_did == DIRDID_ROOT)
1377 setdeskowner( -1, ntohl(aint) );
1379 #if 0 /* don't error if we can't set the desktop owner. */
1383 err = AFPERR_ACCESS;
1384 goto setdirparam_done;
1388 goto setdirparam_done;
1391 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1394 goto setdirparam_done;
1400 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1404 err = AFPERR_ACCESS;
1405 goto setdirparam_done;
1409 goto setdirparam_done;
1412 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1419 case DIRPBIT_ACCESS :
1421 change_parent_mdate = 1;
1422 ma.ma_user = *buf++;
1423 ma.ma_world = *buf++;
1424 ma.ma_group = *buf++;
1425 ma.ma_owner = *buf++;
1427 if (curdir->d_did == DIRDID_ROOT)
1428 setdeskmode(mtoumode( &ma ));
1429 #if 0 /* don't error if we can't set the desktop mode */
1433 err = AFPERR_ACCESS;
1434 goto setdirparam_done;
1437 goto setdirparam_done;
1439 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1443 goto setdirparam_done;
1447 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1448 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1452 err = AFPERR_ACCESS;
1453 goto setdirparam_done;
1456 goto setdirparam_done;
1458 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1461 goto setdirparam_done;
1466 /* Ignore what the client thinks we should do to the
1467 ProDOS information block. Skip over the data and
1468 report nothing amiss. <shirsch@ibm.net> */
1469 case DIRPBIT_PDINFO :
1474 err = AFPERR_BITMAP;
1475 goto setdirparam_done;
1484 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1485 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1489 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1490 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1495 ad_flush( &ad, ADFLAGS_HF );
1496 ad_close( &ad, ADFLAGS_HF );
1499 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1500 && gettimeofday(&tv, NULL) == 0) {
1501 if (!movecwd(vol, curdir->d_parent)) {
1502 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1503 bitmap = 1<<DIRPBIT_MDATE;
1504 setdirparams(vol, "", bitmap, (char *)&newdate);
1505 /* should we reset curdir ?*/
1512 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1515 int ibuflen, *rbuflen;
1528 memcpy( &vid, ibuf, sizeof( vid ));
1529 ibuf += sizeof( vid );
1530 if (( vol = getvolbyvid( vid )) == NULL ) {
1531 return( AFPERR_PARAM );
1534 if (vol->v_flags & AFPVOL_RO)
1535 return AFPERR_VLOCK;
1537 memcpy( &did, ibuf, sizeof( did ));
1538 ibuf += sizeof( did );
1539 if (( dir = dirlookup( vol, did )) == NULL ) {
1540 return( AFPERR_NOOBJ );
1543 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1546 return( AFPERR_ACCESS );
1547 case EEXIST: /* FIXME this on is impossible? */
1548 return( AFPERR_EXIST );
1550 return( AFPERR_NOOBJ );
1553 /* FIXME check done elswhere? cname was able to move curdir to it! */
1555 return AFPERR_EXIST;
1556 upath = mtoupath(vol, path);
1559 if (0 != (ret = check_name(vol, upath))) {
1564 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1567 return( AFPERR_NOOBJ );
1569 return( AFPERR_VLOCK );
1571 return( AFPERR_ACCESS );
1573 return( AFPERR_EXIST );
1576 return( AFPERR_DFULL );
1578 return( AFPERR_PARAM );
1582 if (stat(upath, &st) < 0) {
1586 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1587 strlen(upath), &st)) == NULL) {
1591 if ( movecwd( vol, dir ) < 0 ) {
1592 return( AFPERR_PARAM );
1595 memset(&ad, 0, sizeof(ad));
1596 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1597 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1598 if (vol_noadouble(vol))
1599 goto createdir_done;
1600 return( AFPERR_ACCESS );
1603 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1604 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1605 ad_getentrylen( &ad, ADEID_NAME ));
1606 ad_flush( &ad, ADFLAGS_HF );
1607 ad_close( &ad, ADFLAGS_HF );
1610 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1611 *rbuflen = sizeof( u_int32_t );
1612 setvoltime(obj, vol );
1617 int renamedir(src, dst, dir, newparent, newname, noadouble)
1618 char *src, *dst, *newname;
1619 struct dir *dir, *newparent;
1620 const int noadouble;
1627 /* existence check moved to afp_moveandrename */
1628 if ( rename( src, dst ) < 0 ) {
1631 return( AFPERR_NOOBJ );
1633 return( AFPERR_ACCESS );
1635 return AFPERR_VLOCK;
1637 /* tried to move directory into a subdirectory of itself */
1638 return AFPERR_CANTMOVE;
1640 /* this needs to copy and delete. bleah. that means we have
1641 * to deal with entire directory hierarchies. */
1642 if ((err = copydir(src, dst, noadouble)) < 0) {
1646 if ((err = deletedir(src)) < 0)
1650 return( AFPERR_PARAM );
1654 memset(&ad, 0, sizeof(ad));
1655 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1659 len = strlen(newname);
1660 goto renamedir_done;
1662 return( AFPERR_NOOBJ );
1664 return( AFPERR_ACCESS );
1666 return( AFPERR_PARAM );
1669 len = strlen( newname );
1670 ad_setentrylen( &ad, ADEID_NAME, len );
1671 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1672 ad_flush( &ad, ADFLAGS_HF );
1673 ad_close( &ad, ADFLAGS_HF );
1676 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1677 LOG(log_error, logtype_afpd, "renamedir: realloc: %s", strerror(errno) );
1681 strcpy( dir->d_name, newname );
1683 if (( parent = dir->d_parent ) == NULL ) {
1686 if ( parent == newparent ) {
1690 /* detach from old parent and add to new one. */
1691 dirchildremove(parent, dir);
1692 dir->d_parent = newparent;
1693 dirchildadd(newparent, dir);
1697 #define DOT_APPLEDOUBLE_LEN 13
1698 /* delete an empty directory */
1699 int deletecurdir( vol, path, pathlen )
1700 const struct vol *vol;
1711 if ( curdir->d_parent == NULL ) {
1712 return( AFPERR_ACCESS );
1715 if ( curdir->d_child != NULL ) {
1716 return( AFPERR_DIRNEMPT );
1721 memset(&ad, 0, sizeof(ad));
1722 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1723 DIRBITS | 0777, &ad) == 0 ) {
1725 ad_getattr(&ad, &ashort);
1726 ad_close( &ad, ADFLAGS_HF );
1727 if ((ashort & htons(ATTRBIT_NODELETE))) {
1728 return AFPERR_OLOCK;
1732 /* delete stray .AppleDouble files. this happens to get .Parent files
1734 if ((dp = opendir(".AppleDouble"))) {
1735 strcpy(path, ".AppleDouble/");
1736 while ((de = readdir(dp))) {
1737 /* skip this and previous directory */
1738 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1741 /* bail if the file exists in the current directory.
1742 * note: this will not fail with dangling symlinks */
1743 if (stat(de->d_name, &st) == 0) {
1745 return AFPERR_DIRNEMPT;
1748 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1749 if (unlink(path) < 0) {
1754 return( AFPERR_ACCESS );
1756 return AFPERR_VLOCK;
1760 return( AFPERR_PARAM );
1767 if ( rmdir( ".AppleDouble" ) < 0 ) {
1772 return( AFPERR_DIRNEMPT );
1774 return AFPERR_VLOCK;
1777 return( AFPERR_ACCESS );
1779 return( AFPERR_PARAM );
1783 /* now get rid of dangling symlinks */
1784 if ((dp = opendir("."))) {
1785 while ((de = readdir(dp))) {
1786 /* skip this and previous directory */
1787 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1790 /* bail if it's not a symlink */
1791 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1792 return AFPERR_DIRNEMPT;
1795 if (unlink(de->d_name) < 0) {
1799 return( AFPERR_ACCESS );
1801 return AFPERR_VLOCK;
1805 return( AFPERR_PARAM );
1812 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1813 return( AFPERR_NOOBJ );
1816 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1819 return( AFPERR_NOOBJ );
1821 return( AFPERR_DIRNEMPT );
1824 return( AFPERR_ACCESS );
1826 return AFPERR_VLOCK;
1828 return( AFPERR_PARAM );
1832 dirchildremove(curdir, fdir);
1834 cnid_delete(vol->v_db, fdir->d_did);
1835 #endif /* CNID_DB */
1836 dir_remove( vol, fdir );
1841 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1844 int ibuflen, *rbuflen;
1853 sfunc = (unsigned char) *ibuf++;
1854 memcpy( &id, ibuf, sizeof( id ));
1861 if (( pw = getpwuid( id )) == NULL ) {
1863 return( AFPERR_NOITEM );
1869 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1871 return( AFPERR_NOITEM );
1878 return( AFPERR_PARAM );
1881 len = strlen( name );
1890 memcpy( rbuf, name, len );
1896 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1899 int ibuflen, *rbuflen;
1907 sfunc = (unsigned char) *ibuf++;
1908 len = (unsigned char) *ibuf++;
1914 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1916 return( AFPERR_NOITEM );
1922 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1924 return( AFPERR_NOITEM );
1930 return( AFPERR_PARAM );
1936 memcpy( rbuf, &id, sizeof( id ));
1937 *rbuflen = sizeof( id );
1941 /* variable DID support */
1942 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1945 int ibuflen, *rbuflen;
1956 /* do nothing as dids are static for the life of the process. */
1960 memcpy(&vid, ibuf, sizeof( vid ));
1961 ibuf += sizeof( vid );
1962 if (( vol = getvolbyvid( vid )) == NULL ) {
1963 return( AFPERR_PARAM );
1966 memcpy( &did, ibuf, sizeof( did ));
1967 ibuf += sizeof( did );
1968 if (( dir = dirlookup( vol, did )) == NULL ) {
1969 return( AFPERR_PARAM );
1972 /* dir_remove -- deletedid */
1978 /* did creation gets done automatically */
1979 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1982 int ibuflen, *rbuflen;
1985 struct dir *dir, *parentdir;
1994 memcpy(&vid, ibuf, sizeof(vid));
1995 ibuf += sizeof( vid );
1997 if (( vol = getvolbyvid( vid )) == NULL ) {
1998 return( AFPERR_PARAM );
2001 memcpy(&did, ibuf, sizeof(did));
2002 ibuf += sizeof(did);
2004 if (( parentdir = dirlookup( vol, did )) == NULL ) {
2005 return( AFPERR_NOOBJ );
2008 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2011 return( AFPERR_ACCESS );
2013 return( AFPERR_NOOBJ );
2017 /* see if we already have the directory. */
2018 upath = mtoupath(vol, path);
2019 if ( stat( upath, &st ) < 0 ) {
2020 return( AFPERR_NOOBJ );
2023 dir = parentdir->d_child;
2025 if (strdiacasecmp(dir->d_name, path) == 0) {
2026 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2027 *rbuflen = sizeof(dir->d_did);
2030 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
2033 /* we don't already have a did. add one in. */
2034 if ((dir = adddir(vol, parentdir, path, strlen(path),
2035 upath, strlen(upath), &st)) == NULL) {
2039 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2040 *rbuflen = sizeof(dir->d_did);