2 * $Id: directory.c,v 1.39 2002-08-30 19:32:40 didg 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;
139 ret = dirsearch(vol, did);
144 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
147 ptr = path + MAXPATHLEN;
149 pathlen = len; /* no 0 in the last part */
151 strcpy(ptr - len, upath);
154 ret = dirsearch(vol,id);
158 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL)
160 len = strlen(upath) + 1;
164 strcpy(ptr - len, upath);
169 *ptr = (unsigned char)pathlen;
172 /* cname is not efficient */
173 if (cname( vol, ret, &ptr ) == NULL )
176 return dirsearch(vol, did);
179 /* --------------------------- */
180 /* rotate the tree to the left */
181 static void dir_leftrotate(vol, dir)
185 struct dir *right = dir->d_right;
187 /* whee. move the right's left tree into dir's right tree */
188 dir->d_right = right->d_left;
189 if (right->d_left != SENTINEL)
190 right->d_left->d_back = dir;
192 if (right != SENTINEL) {
193 right->d_back = dir->d_back;
197 if (!dir->d_back) /* no parent. move the right tree to the top. */
199 else if (dir == dir->d_back->d_left) /* we were on the left */
200 dir->d_back->d_left = right;
202 dir->d_back->d_right = right; /* we were on the right */
204 /* re-insert dir on the left tree */
211 /* rotate the tree to the right */
212 static void dir_rightrotate(vol, dir)
216 struct dir *left = dir->d_left;
218 /* whee. move the left's right tree into dir's left tree */
219 dir->d_left = left->d_right;
220 if (left->d_right != SENTINEL)
221 left->d_right->d_back = dir;
223 if (left != SENTINEL) {
224 left->d_back = dir->d_back;
228 if (!dir->d_back) /* no parent. move the left tree to the top. */
230 else if (dir == dir->d_back->d_right) /* we were on the right */
231 dir->d_back->d_right = left;
233 dir->d_back->d_left = left; /* we were on the left */
235 /* re-insert dir on the right tree */
241 /* recolor after a removal */
242 static struct dir *dir_rmrecolor(vol, dir)
248 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
249 /* are we on the left tree? */
250 if (dir == dir->d_back->d_left) {
251 leaf = dir->d_back->d_right; /* get right side */
252 if (leaf->d_color == DIRTREE_COLOR_RED) {
253 /* we're red. we need to change to black. */
254 leaf->d_color = DIRTREE_COLOR_BLACK;
255 dir->d_back->d_color = DIRTREE_COLOR_RED;
256 dir_leftrotate(vol, dir->d_back);
257 leaf = dir->d_back->d_right;
260 /* right leaf has black end nodes */
261 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
262 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
263 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
264 dir = dir->d_back; /* ascend */
266 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
267 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
268 leaf->d_color = DIRTREE_COLOR_RED;
269 dir_rightrotate(vol, leaf);
270 leaf = dir->d_back->d_right;
272 leaf->d_color = dir->d_back->d_color;
273 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
274 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
275 dir_leftrotate(vol, dir->d_back);
278 } else { /* right tree */
279 leaf = dir->d_back->d_left; /* left tree */
280 if (leaf->d_color == DIRTREE_COLOR_RED) {
281 leaf->d_color = DIRTREE_COLOR_BLACK;
282 dir->d_back->d_color = DIRTREE_COLOR_RED;
283 dir_rightrotate(vol, dir->d_back);
284 leaf = dir->d_back->d_left;
287 /* left leaf has black end nodes */
288 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
289 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
290 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
291 dir = dir->d_back; /* ascend */
293 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
294 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
295 leaf->d_color = DIRTREE_COLOR_RED;
296 dir_leftrotate(vol, leaf);
297 leaf = dir->d_back->d_left;
299 leaf->d_color = dir->d_back->d_color;
300 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
301 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
302 dir_rightrotate(vol, dir->d_back);
307 dir->d_color = DIRTREE_COLOR_BLACK;
314 /* remove the node from the tree. this is just like insertion, but
315 * different. actually, it has to worry about a bunch of things that
316 * insertion doesn't care about. */
317 static void dir_remove( vol, dir )
322 struct ofork *of, *last;
323 struct dir *node, *leaf;
324 #endif /* REMOVE_NODES */
326 if (!dir || (dir == SENTINEL))
329 /* i'm not sure if it really helps to delete stuff. */
333 #else /* ! REMOVE_NODES */
335 /* go searching for a node with at most one child */
336 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
340 while (node->d_left != SENTINEL)
345 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
348 leaf->d_back = node->d_back;
351 } else if (node == node->d_back->d_left) { /* left tree */
352 node->d_back->d_left = leaf;
354 node->d_back->d_right = leaf;
357 /* we want to free node, but we also want to free the data in dir.
358 * currently, that's d_name and the directory traversal bits.
359 * we just copy the necessary bits and then fix up all the
360 * various pointers to the directory. needless to say, there are
361 * a bunch of places that store the directory struct. */
363 struct dir save, *tmp;
365 memcpy(&save, dir, sizeof(save));
366 memcpy(dir, node, sizeof(struct dir));
368 /* restore the red-black bits */
369 dir->d_left = save.d_left;
370 dir->d_right = save.d_right;
371 dir->d_back = save.d_back;
372 dir->d_color = save.d_color;
374 if (node == vol->v_dir) {/* we may need to fix up this pointer */
376 rootpar.d_child = vol->v_dir;
378 /* if we aren't the root directory, we have parents and
379 * siblings to worry about */
380 if (dir->d_parent->d_child == node)
381 dir->d_parent->d_child = dir;
382 dir->d_next->d_prev = dir;
383 dir->d_prev->d_next = dir;
386 /* fix up children. */
390 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
393 if (node == curdir) /* another pointer to fixup */
396 /* we also need to fix up oforks. bleah */
397 if ((of = dir->d_ofork)) {
398 last = of->of_d_prev;
401 of = (last == of) ? NULL : of->of_d_next;
405 /* set the node's d_name */
406 node->d_name = save.d_name;
409 if (node->d_color == DIRTREE_COLOR_BLACK)
410 dir_rmrecolor(vol, leaf);
413 #endif /* ! REMOVE_NODES */
416 /* ---------------------------------------
417 * remove the node and its childs from the tree
419 * FIXME what about opened forks with refs to it?
420 * it's an afp specs violation because you can't delete
421 * an opened forks. Now afpd doesn't care about forks opened by other
422 * process. It's fixable within afpd if fnctl_lock, doable with smb and
423 * next to impossible for nfs and local filesystem access.
426 static void dir_invalidate( vol, dir )
427 const struct vol *vol;
431 /* v_root can't be deleted */
432 if (movecwd(vol, vol->v_root) < 0)
433 printf("Yuup cant change dir to v_root\n");
436 dirchildremove(dir->d_parent, dir);
437 dir_remove( vol, dir );
440 /* ------------------------------------ */
441 static struct dir *dir_insert(vol, dir)
442 const struct vol *vol;
448 while (pdir->d_did != dir->d_did ) {
449 if ( pdir->d_did > dir->d_did ) {
450 if ( pdir->d_left == SENTINEL ) {
457 if ( pdir->d_right == SENTINEL ) {
462 pdir = pdir->d_right;
470 * attempt to extend the current dir. tree to include path
471 * as a side-effect, movecwd to that point and return the new dir
475 extenddir( vol, dir, path )
483 p = mtoupath(vol, path );
484 if ( stat( p, &st ) != 0 ) {
487 if (!S_ISDIR(st.st_mode)) {
491 if (( dir = adddir( vol, dir, path, strlen( path ), p, strlen(p),
496 if ( movecwd( vol, dir ) < 0 ) {
503 static int deletedir(char *dir)
505 char path[MAXPATHLEN + 1];
511 if ((len = strlen(dir)) > sizeof(path))
515 if ((dp = opendir(dir)) == NULL)
521 while ((de = readdir(dp))) {
522 /* skip this and previous directory */
523 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
526 strncpy(path + len, de->d_name, sizeof(path) - len);
527 if (stat(path, &st) == 0) {
528 if (S_ISDIR(st.st_mode)) {
529 if ((err = deletedir(path)) < 0) {
533 } else if (unlink(path) < 0) {
536 continue; /* somebody went and deleted it behind our backs. */
554 /* okay. the directory is empty. delete it. note: we already got rid
556 if (rmdir(dir) < 0) {
560 case ENOTEMPTY : /* should never happen */
561 return( AFPERR_DIRNEMPT );
564 return( AFPERR_ACCESS );
568 return( AFPERR_PARAM );
574 /* do a recursive copy. */
575 static int copydir(char *src, char *dst, int noadouble)
577 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
584 /* doesn't exist or the path is too long. */
585 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
586 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
587 ((dp = opendir(src)) == NULL))
590 /* try to create the destination directory */
591 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
595 return( AFPERR_NOOBJ );
597 return( AFPERR_VLOCK );
600 return( AFPERR_ACCESS );
602 return( AFPERR_EXIST );
605 return( AFPERR_DFULL );
607 return( AFPERR_PARAM );
611 /* set things up to copy */
619 while ((de = readdir(dp))) {
620 /* skip this and previous directory */
621 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
624 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
625 if (stat(spath, &st) == 0) {
626 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
628 if (S_ISDIR(st.st_mode)) {
629 if ((err = copydir(spath, dpath, noadouble)) < 0)
631 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
635 /* keep the same time stamp. */
636 ut.actime = ut.modtime = st.st_mtime;
642 /* keep the same time stamp. */
643 if (stat(src, &st) == 0) {
644 ut.actime = ut.modtime = st.st_mtime;
654 /* --- public functions follow --- */
656 /* NOTE: we start off with at least one node (the root directory). */
657 struct dir *dirinsert( vol, dir )
663 if ((node = dir_insert(vol, dir)))
666 /* recolor the tree. the current node is red. */
667 dir->d_color = DIRTREE_COLOR_RED;
669 /* parent of this node has to be black. if the parent node
670 * is red, then we have a grandparent. */
671 while ((dir != vol->v_root) &&
672 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
673 /* are we on the left tree? */
674 if (dir->d_back == dir->d_back->d_back->d_left) {
675 node = dir->d_back->d_back->d_right; /* get the right node */
676 if (node->d_color == DIRTREE_COLOR_RED) {
677 /* we're red. we need to change to black. */
678 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
679 node->d_color = DIRTREE_COLOR_BLACK;
680 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
681 dir = dir->d_back->d_back; /* finished. go up. */
683 if (dir == dir->d_back->d_right) {
685 dir_leftrotate(vol, dir);
687 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
688 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
689 dir_rightrotate(vol, dir->d_back->d_back);
692 node = dir->d_back->d_back->d_left;
693 if (node->d_color == DIRTREE_COLOR_RED) {
694 /* we're red. we need to change to black. */
695 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
696 node->d_color = DIRTREE_COLOR_BLACK;
697 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
698 dir = dir->d_back->d_back; /* finished. ascend */
700 if (dir == dir->d_back->d_left) {
702 dir_rightrotate(vol, dir);
704 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
705 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
706 dir_leftrotate(vol, dir->d_back->d_back);
711 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
715 /* free everything down. we don't bother to recolor as this is only
716 * called to free the entire tree */
720 if (!dir || (dir == SENTINEL))
723 if ( dir->d_left != SENTINEL ) {
724 dirfree( dir->d_left );
726 if ( dir->d_right != SENTINEL ) {
727 dirfree( dir->d_right );
730 if (dir != SENTINEL) {
737 struct dir *dirnew(const int len)
741 dir = (struct dir *) calloc(1, sizeof( struct dir ));
745 if ((dir->d_name = (char *) malloc(sizeof(char)*len)) == NULL) {
750 dir->d_left = dir->d_right = SENTINEL;
751 dir->d_next = dir->d_prev = dir;
756 /* XXX: this needs to be changed to handle path types */
758 cname( vol, dir, cpath )
759 const struct vol *vol;
764 static char path[ MAXPATHLEN + 1];
772 if ( *data++ != 2 ) { /* path type */
775 len = (unsigned char) *data++;
782 if ( !extend && movecwd( vol, dir ) < 0 ) {
784 movecwd failed so dir is not there anymore.
785 FIXME Is it true with other errors?
786 if path == '\0' ==> the cpath parameter is that dir,
787 and maybe we are trying to recreate it! So we can't
791 if ( dir->d_did == DIRDID_ROOT_PARENT)
793 cdir = dir->d_parent;
794 dir_invalidate(vol, dir);
795 if (*path != '\0' || u == NULL) {
796 /* FIXME: if path != '\0' then extend != 0 ?
797 * u == NUL ==> cpath is something like:
802 if (movecwd(vol, cdir) < 0) {
803 printf("can't change to parent\n");
804 return NULL; /* give up the whole tree is out of synch*/
806 /* restore the previous token */
807 strncpy(path, u, olen);
813 if ( *data == '\0' ) {
819 while ( *data == '\0' && len > 0 ) {
820 if ( dir->d_parent == NULL ) {
828 /* would this be faster with strlen + strncpy? */
834 while ( *data != '\0' && len > 0 ) {
839 /* short cut bits by chopping off a trailing \0. this also
840 makes the traversal happy w/ filenames at the end of the
847 * Dung Nguyen <ntd@adb.fr>
849 * AFPD cannot handle paths with "::" if the "::" notation is
850 * not at the beginning of the path. The following path will not
851 * be interpreted correctly:
853 * :a:b:::c: (directory c at the same level as directory a) */
861 if ( p != path ) { /* we got something */
865 if ( strcasecmp( cdir->d_name, path ) == 0 ) {
868 cdir = (cdir == dir->d_child->d_prev) ? NULL :
871 if ( cdir == NULL ) {
873 /* if dir == curdir it always succeed,
874 even if curdir is deleted.
875 it's not a pb because it will failed in extenddir
877 if ( movecwd( vol, dir ) < 0 ) {
878 /* dir is not valid anymore
879 we delete dir from the cache and abort.
881 dir_invalidate(vol, dir);
884 cdir = extenddir( vol, dir, path );
888 cdir = extenddir( vol, dir, path );
891 if ( cdir == NULL ) {
905 * Move curdir to dir, with a possible chdir()
907 int movecwd( vol, dir)
908 const struct vol *vol;
911 char path[MAXPATHLEN + 1];
916 if ( dir == curdir ) {
919 if ( dir->d_did == DIRDID_ROOT_PARENT) {
923 p = path + sizeof(path) - 1;
926 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
928 u = mtoupath(vol, d->d_name );
935 n = strlen( vol->v_path );
937 strncpy( p, vol->v_path, n );
939 if ( chdir( p ) < 0 ) {
946 int getdirparams(const struct vol *vol,
948 char *upath, struct dir *dir, struct stat *st,
949 char *buf, int *buflen )
953 char *data, *nameoff = NULL;
956 int bit = 0, isad = 1;
960 memset(&ad, 0, sizeof(ad));
962 if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
963 DIRBITS | 0777, &ad) < 0 ) {
968 while ( bitmap != 0 ) {
969 while (( bitmap & 1 ) == 0 ) {
977 ad_getattr(&ad, &ashort);
978 } else if (*upath == '.' && strcmp(upath, ".") &&
979 strcmp(upath, "..")) {
980 ashort = htons(ATTRBIT_INVISIBLE);
983 ashort |= htons(ATTRBIT_SHARED);
984 memcpy( data, &ashort, sizeof( ashort ));
985 data += sizeof( ashort );
989 if ( dir->d_did == DIRDID_ROOT) {
990 aint = DIRDID_ROOT_PARENT;
991 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
994 aint = dir->d_parent->d_did;
996 memcpy( data, &aint, sizeof( aint ));
997 data += sizeof( aint );
1000 case DIRPBIT_CDATE :
1001 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1002 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1003 memcpy( data, &aint, sizeof( aint ));
1004 data += sizeof( aint );
1007 case DIRPBIT_MDATE :
1008 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1009 memcpy( data, &aint, sizeof( aint ));
1010 data += sizeof( aint );
1013 case DIRPBIT_BDATE :
1014 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1015 aint = AD_DATE_START;
1016 memcpy( data, &aint, sizeof( aint ));
1017 data += sizeof( aint );
1020 case DIRPBIT_FINFO :
1022 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1023 } else { /* no appledouble */
1024 memset( data, 0, 32 );
1025 /* set default view -- this also gets done in ad_open() */
1026 ashort = htons(FINDERINFO_CLOSEDVIEW);
1027 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1029 /* dot files are by default invisible */
1030 if (*upath == '.' && strcmp(upath, ".") &&
1031 strcmp(upath, "..")) {
1032 ashort = htons(FINDERINFO_INVISIBLE);
1033 memcpy(data + FINDERINFO_FRFLAGOFF,
1034 &ashort, sizeof(ashort));
1040 case DIRPBIT_LNAME :
1041 if (dir->d_name) /* root of parent can have a null name */
1044 memset(data, 0, sizeof(u_int16_t));
1045 data += sizeof( u_int16_t );
1048 case DIRPBIT_SNAME :
1049 memset(data, 0, sizeof(u_int16_t));
1050 data += sizeof( u_int16_t );
1054 memcpy( data, &dir->d_did, sizeof( aint ));
1055 data += sizeof( aint );
1058 case DIRPBIT_OFFCNT :
1060 /* this needs to handle current directory access rights */
1061 if ((dp = opendir( upath ))) {
1062 while (( de = readdir( dp )) != NULL ) {
1063 if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
1066 if (!validupath(vol, de->d_name))
1069 /* check for vetoed filenames */
1070 if (veto_file(vol->v_veto, de->d_name))
1073 /* now check against too long a filename */
1074 if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
1081 ashort = htons( ashort );
1082 memcpy( data, &ashort, sizeof( ashort ));
1083 data += sizeof( ashort );
1087 aint = htonl(st->st_uid);
1088 memcpy( data, &aint, sizeof( aint ));
1089 data += sizeof( aint );
1093 aint = htonl(st->st_gid);
1094 memcpy( data, &aint, sizeof( aint ));
1095 data += sizeof( aint );
1098 case DIRPBIT_ACCESS :
1099 accessmode( upath, &ma, dir , st);
1101 *data++ = ma.ma_user;
1102 *data++ = ma.ma_world;
1103 *data++ = ma.ma_group;
1104 *data++ = ma.ma_owner;
1107 /* Client has requested the ProDOS information block.
1108 Just pass back the same basic block for all
1109 directories. <shirsch@ibm.net> */
1110 case DIRPBIT_PDINFO : /* ProDOS Info Block */
1113 ashort = htons( 0x0200 );
1114 memcpy( data, &ashort, sizeof( ashort ));
1115 data += sizeof( ashort );
1116 memset( data, 0, sizeof( ashort ));
1117 data += sizeof( ashort );
1122 ad_close( &ad, ADFLAGS_HF );
1124 return( AFPERR_BITMAP );
1130 ashort = htons( data - buf );
1131 memcpy( nameoff, &ashort, sizeof( ashort ));
1133 if ((aint = strlen( dir->d_name )) > MACFILELEN)
1137 memcpy( data, dir->d_name, aint );
1141 ad_close( &ad, ADFLAGS_HF );
1143 *buflen = data - buf;
1147 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1150 int ibuflen, *rbuflen;
1155 u_int16_t vid, bitmap;
1161 memcpy( &vid, ibuf, sizeof( vid ));
1162 ibuf += sizeof( vid );
1164 if (( vol = getvolbyvid( vid )) == NULL ) {
1165 return( AFPERR_PARAM );
1168 if (vol->v_flags & AFPVOL_RO)
1169 return AFPERR_VLOCK;
1171 memcpy( &did, ibuf, sizeof( did ));
1172 ibuf += sizeof( int );
1174 if (( dir = dirsearch( vol, did )) == NULL ) {
1175 return( AFPERR_NOOBJ );
1178 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1179 bitmap = ntohs( bitmap );
1180 ibuf += sizeof( bitmap );
1182 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1183 return( AFPERR_NOOBJ );
1186 if ( *path != '\0' ) {
1187 return( AFPERR_BADTYPE ); /* not a directory */
1191 * If ibuf is odd, make it even.
1193 if ((u_long)ibuf & 1 ) {
1197 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1198 setvoltime(obj, vol );
1204 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1206 * assume path == '\0' eg. it's a directory in canonical form
1208 int setdirparams(const struct vol *vol,
1209 char *path, u_int16_t bitmap, char *buf )
1217 int bit = 0, aint, isad = 1;
1218 u_int16_t ashort, bshort;
1220 int change_mdate = 0;
1221 int change_parent_mdate = 0;
1224 upath = mtoupath(vol, path);
1225 memset(&ad, 0, sizeof(ad));
1227 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1228 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1230 * Check to see what we're trying to set. If it's anything
1231 * but ACCESS, UID, or GID, give an error. If it's any of those
1232 * three, we don't need the ad to be open, so just continue.
1234 * note: we also don't need to worry about mdate. also, be quiet
1235 * if we're using the noadouble option.
1237 if (!vol_noadouble(vol) && (bitmap &
1238 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1239 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1240 return AFPERR_ACCESS;
1246 * Check to see if a create was necessary. If it was, we'll want
1247 * to set our name, etc.
1249 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1250 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1251 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1252 ad_getentrylen( &ad, ADEID_NAME ));
1256 while ( bitmap != 0 ) {
1257 while (( bitmap & 1 ) == 0 ) {
1266 memcpy( &ashort, buf, sizeof( ashort ));
1267 ad_getattr(&ad, &bshort);
1268 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1269 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1273 ad_setattr(&ad, bshort);
1274 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1275 change_parent_mdate = 1;
1277 buf += sizeof( ashort );
1280 case DIRPBIT_CDATE :
1283 memcpy(&aint, buf, sizeof(aint));
1284 ad_setdate(&ad, AD_DATE_CREATE, aint);
1286 buf += sizeof( aint );
1289 case DIRPBIT_MDATE :
1290 memcpy(&newdate, buf, sizeof(newdate));
1291 buf += sizeof( newdate );
1294 case DIRPBIT_BDATE :
1297 memcpy(&aint, buf, sizeof(aint));
1298 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1300 buf += sizeof( aint );
1303 case DIRPBIT_FINFO :
1306 * Alright, we admit it, this is *really* sick!
1307 * The 4 bytes that we don't copy, when we're dealing
1308 * with the root of a volume, are the directory's
1309 * location information. This eliminates that annoying
1310 * behavior one sees when mounting above another mount
1314 if ( curdir->d_did == DIRDID_ROOT ) {
1315 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1316 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1318 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1324 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1325 change_parent_mdate = 1;
1326 memcpy( &aint, buf, sizeof(aint));
1327 buf += sizeof( aint );
1328 if ( (curdir->d_did == DIRDID_ROOT) &&
1329 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1333 err = AFPERR_ACCESS;
1334 goto setdirparam_done;
1338 goto setdirparam_done;
1341 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1345 goto setdirparam_done;
1350 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1354 err = AFPERR_ACCESS;
1355 goto setdirparam_done;
1359 goto setdirparam_done;
1362 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1369 change_parent_mdate = 1;
1370 memcpy( &aint, buf, sizeof( aint ));
1371 buf += sizeof( aint );
1372 if (curdir->d_did == DIRDID_ROOT)
1373 setdeskowner( -1, ntohl(aint) );
1375 #if 0 /* don't error if we can't set the desktop owner. */
1379 err = AFPERR_ACCESS;
1380 goto setdirparam_done;
1384 goto setdirparam_done;
1387 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1390 goto setdirparam_done;
1396 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1400 err = AFPERR_ACCESS;
1401 goto setdirparam_done;
1405 goto setdirparam_done;
1408 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1415 case DIRPBIT_ACCESS :
1417 change_parent_mdate = 1;
1418 ma.ma_user = *buf++;
1419 ma.ma_world = *buf++;
1420 ma.ma_group = *buf++;
1421 ma.ma_owner = *buf++;
1423 if (curdir->d_did == DIRDID_ROOT)
1424 setdeskmode(mtoumode( &ma ));
1425 #if 0 /* don't error if we can't set the desktop mode */
1429 err = AFPERR_ACCESS;
1430 goto setdirparam_done;
1433 goto setdirparam_done;
1435 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1439 goto setdirparam_done;
1443 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1444 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1448 err = AFPERR_ACCESS;
1449 goto setdirparam_done;
1452 goto setdirparam_done;
1454 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1457 goto setdirparam_done;
1462 /* Ignore what the client thinks we should do to the
1463 ProDOS information block. Skip over the data and
1464 report nothing amiss. <shirsch@ibm.net> */
1465 case DIRPBIT_PDINFO :
1470 err = AFPERR_BITMAP;
1471 goto setdirparam_done;
1480 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1481 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1485 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1486 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1491 ad_flush( &ad, ADFLAGS_HF );
1492 ad_close( &ad, ADFLAGS_HF );
1495 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1496 && gettimeofday(&tv, NULL) == 0) {
1497 if (!movecwd(vol, curdir->d_parent)) {
1498 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1499 bitmap = 1<<DIRPBIT_MDATE;
1500 setdirparams(vol, "", bitmap, (char *)&newdate);
1501 /* should we reset curdir ?*/
1508 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1511 int ibuflen, *rbuflen;
1524 memcpy( &vid, ibuf, sizeof( vid ));
1525 ibuf += sizeof( vid );
1526 if (( vol = getvolbyvid( vid )) == NULL ) {
1527 return( AFPERR_PARAM );
1530 if (vol->v_flags & AFPVOL_RO)
1531 return AFPERR_VLOCK;
1533 memcpy( &did, ibuf, sizeof( did ));
1534 ibuf += sizeof( did );
1535 if (( dir = dirsearch( vol, did )) == NULL ) {
1536 return( AFPERR_NOOBJ );
1539 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1542 return( AFPERR_ACCESS );
1543 case EEXIST: /* FIXME this on is impossible? */
1544 return( AFPERR_EXIST );
1546 return( AFPERR_NOOBJ );
1549 /* FIXME check done elswhere? cname was able to move curdir to it! */
1551 return AFPERR_EXIST;
1552 upath = mtoupath(vol, path);
1555 if (0 != (ret = check_name(vol, upath))) {
1560 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1563 return( AFPERR_NOOBJ );
1565 return( AFPERR_VLOCK );
1567 return( AFPERR_ACCESS );
1569 return( AFPERR_EXIST );
1572 return( AFPERR_DFULL );
1574 return( AFPERR_PARAM );
1578 if (stat(upath, &st) < 0) {
1582 if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1583 strlen(upath), &st)) == NULL) {
1587 if ( movecwd( vol, dir ) < 0 ) {
1588 return( AFPERR_PARAM );
1591 memset(&ad, 0, sizeof(ad));
1592 if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1593 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1594 if (vol_noadouble(vol))
1595 goto createdir_done;
1596 return( AFPERR_ACCESS );
1599 ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1600 memcpy( ad_entry( &ad, ADEID_NAME ), path,
1601 ad_getentrylen( &ad, ADEID_NAME ));
1602 ad_flush( &ad, ADFLAGS_HF );
1603 ad_close( &ad, ADFLAGS_HF );
1606 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1607 *rbuflen = sizeof( u_int32_t );
1608 setvoltime(obj, vol );
1613 int renamedir(src, dst, dir, newparent, newname, noadouble)
1614 char *src, *dst, *newname;
1615 struct dir *dir, *newparent;
1616 const int noadouble;
1623 /* existence check moved to afp_moveandrename */
1624 if ( rename( src, dst ) < 0 ) {
1627 return( AFPERR_NOOBJ );
1629 return( AFPERR_ACCESS );
1631 return AFPERR_VLOCK;
1633 /* tried to move directory into a subdirectory of itself */
1634 return AFPERR_CANTMOVE;
1636 /* this needs to copy and delete. bleah. that means we have
1637 * to deal with entire directory hierarchies. */
1638 if ((err = copydir(src, dst, noadouble)) < 0) {
1642 if ((err = deletedir(src)) < 0)
1646 return( AFPERR_PARAM );
1650 memset(&ad, 0, sizeof(ad));
1651 if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1655 len = strlen(newname);
1656 goto renamedir_done;
1658 return( AFPERR_NOOBJ );
1660 return( AFPERR_ACCESS );
1662 return( AFPERR_PARAM );
1665 len = strlen( newname );
1666 ad_setentrylen( &ad, ADEID_NAME, len );
1667 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1668 ad_flush( &ad, ADFLAGS_HF );
1669 ad_close( &ad, ADFLAGS_HF );
1672 if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1673 LOG(log_error, logtype_afpd, "renamedir: realloc: %s", strerror(errno) );
1677 strcpy( dir->d_name, newname );
1679 if (( parent = dir->d_parent ) == NULL ) {
1682 if ( parent == newparent ) {
1686 /* detach from old parent and add to new one. */
1687 dirchildremove(parent, dir);
1688 dir->d_parent = newparent;
1689 dirchildadd(newparent, dir);
1693 #define DOT_APPLEDOUBLE_LEN 13
1694 /* delete an empty directory */
1695 int deletecurdir( vol, path, pathlen )
1696 const struct vol *vol;
1707 if ( curdir->d_parent == NULL ) {
1708 return( AFPERR_ACCESS );
1711 if ( curdir->d_child != NULL ) {
1712 return( AFPERR_DIRNEMPT );
1717 memset(&ad, 0, sizeof(ad));
1718 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1719 DIRBITS | 0777, &ad) == 0 ) {
1721 ad_getattr(&ad, &ashort);
1722 ad_close( &ad, ADFLAGS_HF );
1723 if ((ashort & htons(ATTRBIT_NODELETE))) {
1724 return AFPERR_OLOCK;
1728 /* delete stray .AppleDouble files. this happens to get .Parent files
1730 if ((dp = opendir(".AppleDouble"))) {
1731 strcpy(path, ".AppleDouble/");
1732 while ((de = readdir(dp))) {
1733 /* skip this and previous directory */
1734 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1737 /* bail if the file exists in the current directory.
1738 * note: this will not fail with dangling symlinks */
1739 if (stat(de->d_name, &st) == 0) {
1741 return AFPERR_DIRNEMPT;
1744 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1745 if (unlink(path) < 0) {
1750 return( AFPERR_ACCESS );
1752 return AFPERR_VLOCK;
1756 return( AFPERR_PARAM );
1763 if ( rmdir( ".AppleDouble" ) < 0 ) {
1768 return( AFPERR_DIRNEMPT );
1770 return AFPERR_VLOCK;
1773 return( AFPERR_ACCESS );
1775 return( AFPERR_PARAM );
1779 /* now get rid of dangling symlinks */
1780 if ((dp = opendir("."))) {
1781 while ((de = readdir(dp))) {
1782 /* skip this and previous directory */
1783 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1786 /* bail if it's not a symlink */
1787 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1788 return AFPERR_DIRNEMPT;
1791 if (unlink(de->d_name) < 0) {
1795 return( AFPERR_ACCESS );
1797 return AFPERR_VLOCK;
1801 return( AFPERR_PARAM );
1808 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1809 return( AFPERR_NOOBJ );
1812 if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1815 return( AFPERR_NOOBJ );
1817 return( AFPERR_DIRNEMPT );
1820 return( AFPERR_ACCESS );
1822 return AFPERR_VLOCK;
1824 return( AFPERR_PARAM );
1828 dirchildremove(curdir, fdir);
1830 cnid_delete(vol->v_db, fdir->d_did);
1831 #endif /* CNID_DB */
1832 dir_remove( vol, fdir );
1837 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1840 int ibuflen, *rbuflen;
1849 sfunc = (unsigned char) *ibuf++;
1850 memcpy( &id, ibuf, sizeof( id ));
1857 if (( pw = getpwuid( id )) == NULL ) {
1859 return( AFPERR_NOITEM );
1865 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1867 return( AFPERR_NOITEM );
1874 return( AFPERR_PARAM );
1877 len = strlen( name );
1886 memcpy( rbuf, name, len );
1892 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1895 int ibuflen, *rbuflen;
1903 sfunc = (unsigned char) *ibuf++;
1904 len = (unsigned char) *ibuf++;
1910 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1912 return( AFPERR_NOITEM );
1918 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1920 return( AFPERR_NOITEM );
1926 return( AFPERR_PARAM );
1932 memcpy( rbuf, &id, sizeof( id ));
1933 *rbuflen = sizeof( id );
1937 /* variable DID support */
1938 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1941 int ibuflen, *rbuflen;
1952 /* do nothing as dids are static for the life of the process. */
1956 memcpy(&vid, ibuf, sizeof( vid ));
1957 ibuf += sizeof( vid );
1958 if (( vol = getvolbyvid( vid )) == NULL ) {
1959 return( AFPERR_PARAM );
1962 memcpy( &did, ibuf, sizeof( did ));
1963 ibuf += sizeof( did );
1964 if (( dir = dirsearch( vol, did )) == NULL ) {
1965 return( AFPERR_PARAM );
1968 /* dir_remove -- deletedid */
1974 /* did creation gets done automatically */
1975 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1978 int ibuflen, *rbuflen;
1981 struct dir *dir, *parentdir;
1990 memcpy(&vid, ibuf, sizeof(vid));
1991 ibuf += sizeof( vid );
1993 if (( vol = getvolbyvid( vid )) == NULL ) {
1994 return( AFPERR_PARAM );
1997 memcpy(&did, ibuf, sizeof(did));
1998 ibuf += sizeof(did);
2000 if (( parentdir = dirsearch( vol, did )) == NULL ) {
2001 return( AFPERR_NOOBJ );
2004 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2007 return( AFPERR_ACCESS );
2009 return( AFPERR_NOOBJ );
2013 /* see if we already have the directory. */
2014 upath = mtoupath(vol, path);
2015 if ( stat( upath, &st ) < 0 ) {
2016 return( AFPERR_NOOBJ );
2019 dir = parentdir->d_child;
2021 if (strdiacasecmp(dir->d_name, path) == 0) {
2022 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2023 *rbuflen = sizeof(dir->d_did);
2026 dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
2029 /* we don't already have a did. add one in. */
2030 if ((dir = adddir(vol, parentdir, path, strlen(path),
2031 upath, strlen(upath), &st)) == NULL) {
2035 memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2036 *rbuflen = sizeof(dir->d_did);