2 * $Id: directory.c,v 1.55 2003-01-08 15:01:33 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"
65 #define SENTINEL (&sentinel)
66 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
67 NULL, NULL, NULL, NULL, NULL, 0, 0,
69 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
70 NULL, NULL, NULL, NULL, NULL, 0, 0,
73 /* (from IM: Toolbox Essentials)
74 * dirFinderInfo (DInfo) fields:
76 * frRect 8 folder's window rectangle
78 * frLocation 4 folder's location in window
79 * frView 2 folder's view (default == closedView (256))
81 * extended dirFinderInfo (DXInfo) fields:
82 * frScroll 4 scroll position
83 * frOpenChain: 4 directory ID chain of open folders
84 * frScript: 1 script flag and code
85 * frXFlags: 1 reserved
86 * frComment: 2 comment ID
87 * frPutAway: 4 home directory ID
91 * redid did assignment for directories. now we use red-black trees.
96 const struct vol *vol;
102 /* check for 0 did */
104 afp_errno = AFPERR_PARAM;
107 if ( did == DIRDID_ROOT_PARENT ) {
109 rootpar.d_did = DIRDID_ROOT_PARENT;
110 rootpar.d_child = vol->v_dir;
115 afp_errno = AFPERR_NOOBJ;
116 while ( dir != SENTINEL ) {
117 if (dir->d_did == did)
118 return dir->d_m_name ? dir : NULL;
119 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
124 /* -----------------------------------------
125 * if did is not in the cache resolve it with cnid
129 dirlookup( vol, did )
130 const struct vol *vol;
137 static char path[MAXPATHLEN + 1];
141 static char buffer[12 + MAXPATHLEN + 1];
142 int buflen = 12 + MAXPATHLEN + 1;
145 ret = dirsearch(vol, did);
150 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
151 afp_errno = AFPERR_NOOBJ;
154 ptr = path + MAXPATHLEN;
155 mpath = utompath(vol, upath);
157 pathlen = len; /* no 0 in the last part */
159 strcpy(ptr - len, mpath);
162 ret = dirsearch(vol,id);
166 if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
167 afp_errno = AFPERR_NOOBJ;
170 mpath = utompath(vol, upath);
171 len = strlen(mpath) + 1;
174 afp_errno = AFPERR_PARAM;
177 strcpy(ptr - len, mpath);
182 *ptr = (unsigned char)pathlen;
185 /* cname is not efficient */
186 if (cname( vol, ret, &ptr ) == NULL )
189 return dirsearch(vol, did);
192 /* --------------------------- */
193 /* rotate the tree to the left */
194 static void dir_leftrotate(vol, dir)
198 struct dir *right = dir->d_right;
200 /* whee. move the right's left tree into dir's right tree */
201 dir->d_right = right->d_left;
202 if (right->d_left != SENTINEL)
203 right->d_left->d_back = dir;
205 if (right != SENTINEL) {
206 right->d_back = dir->d_back;
210 if (!dir->d_back) /* no parent. move the right tree to the top. */
212 else if (dir == dir->d_back->d_left) /* we were on the left */
213 dir->d_back->d_left = right;
215 dir->d_back->d_right = right; /* we were on the right */
217 /* re-insert dir on the left tree */
224 /* rotate the tree to the right */
225 static void dir_rightrotate(vol, dir)
229 struct dir *left = dir->d_left;
231 /* whee. move the left's right tree into dir's left tree */
232 dir->d_left = left->d_right;
233 if (left->d_right != SENTINEL)
234 left->d_right->d_back = dir;
236 if (left != SENTINEL) {
237 left->d_back = dir->d_back;
241 if (!dir->d_back) /* no parent. move the left tree to the top. */
243 else if (dir == dir->d_back->d_right) /* we were on the right */
244 dir->d_back->d_right = left;
246 dir->d_back->d_left = left; /* we were on the left */
248 /* re-insert dir on the right tree */
254 /* recolor after a removal */
255 static struct dir *dir_rmrecolor(vol, dir)
261 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
262 /* are we on the left tree? */
263 if (dir == dir->d_back->d_left) {
264 leaf = dir->d_back->d_right; /* get right side */
265 if (leaf->d_color == DIRTREE_COLOR_RED) {
266 /* we're red. we need to change to black. */
267 leaf->d_color = DIRTREE_COLOR_BLACK;
268 dir->d_back->d_color = DIRTREE_COLOR_RED;
269 dir_leftrotate(vol, dir->d_back);
270 leaf = dir->d_back->d_right;
273 /* right leaf has black end nodes */
274 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
275 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
276 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
277 dir = dir->d_back; /* ascend */
279 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
280 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
281 leaf->d_color = DIRTREE_COLOR_RED;
282 dir_rightrotate(vol, leaf);
283 leaf = dir->d_back->d_right;
285 leaf->d_color = dir->d_back->d_color;
286 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
287 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
288 dir_leftrotate(vol, dir->d_back);
291 } else { /* right tree */
292 leaf = dir->d_back->d_left; /* left tree */
293 if (leaf->d_color == DIRTREE_COLOR_RED) {
294 leaf->d_color = DIRTREE_COLOR_BLACK;
295 dir->d_back->d_color = DIRTREE_COLOR_RED;
296 dir_rightrotate(vol, dir->d_back);
297 leaf = dir->d_back->d_left;
300 /* left leaf has black end nodes */
301 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
302 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
303 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
304 dir = dir->d_back; /* ascend */
306 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
307 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
308 leaf->d_color = DIRTREE_COLOR_RED;
309 dir_leftrotate(vol, leaf);
310 leaf = dir->d_back->d_left;
312 leaf->d_color = dir->d_back->d_color;
313 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
314 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
315 dir_rightrotate(vol, dir->d_back);
320 dir->d_color = DIRTREE_COLOR_BLACK;
327 /* remove the node from the tree. this is just like insertion, but
328 * different. actually, it has to worry about a bunch of things that
329 * insertion doesn't care about. */
330 static void dir_remove( vol, dir )
335 struct ofork *of, *last;
336 struct dir *node, *leaf;
337 #endif /* REMOVE_NODES */
339 if (!dir || (dir == SENTINEL))
342 /* i'm not sure if it really helps to delete stuff. */
344 if (dir->d_u_name != dir->d_m_name) {
348 dir->d_m_name = NULL;
349 dir->d_u_name = NULL;
350 #else /* ! REMOVE_NODES */
352 /* go searching for a node with at most one child */
353 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
357 while (node->d_left != SENTINEL)
362 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
365 leaf->d_back = node->d_back;
368 } else if (node == node->d_back->d_left) { /* left tree */
369 node->d_back->d_left = leaf;
371 node->d_back->d_right = leaf;
374 /* we want to free node, but we also want to free the data in dir.
375 * currently, that's d_name and the directory traversal bits.
376 * we just copy the necessary bits and then fix up all the
377 * various pointers to the directory. needless to say, there are
378 * a bunch of places that store the directory struct. */
380 struct dir save, *tmp;
382 memcpy(&save, dir, sizeof(save));
383 memcpy(dir, node, sizeof(struct dir));
385 /* restore the red-black bits */
386 dir->d_left = save.d_left;
387 dir->d_right = save.d_right;
388 dir->d_back = save.d_back;
389 dir->d_color = save.d_color;
391 if (node == vol->v_dir) {/* we may need to fix up this pointer */
393 rootpar.d_child = vol->v_dir;
395 /* if we aren't the root directory, we have parents and
396 * siblings to worry about */
397 if (dir->d_parent->d_child == node)
398 dir->d_parent->d_child = dir;
399 dir->d_next->d_prev = dir;
400 dir->d_prev->d_next = dir;
403 /* fix up children. */
407 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
410 if (node == curdir) /* another pointer to fixup */
413 /* we also need to fix up oforks. bleah */
414 if ((of = dir->d_ofork)) {
415 last = of->of_d_prev;
418 of = (last == of) ? NULL : of->of_d_next;
422 /* set the node's d_name */
423 node->d_m_name = save.d_m_name;
424 node->d_u_name = save.d_u_name;
427 if (node->d_color == DIRTREE_COLOR_BLACK)
428 dir_rmrecolor(vol, leaf);
430 if (node->d_u_name != node->d_m_name) {
431 free(node->d_u_name);
433 free(node->d_m_name);
435 #endif /* ! REMOVE_NODES */
438 /* ---------------------------------------
439 * remove the node and its childs from the tree
441 * FIXME what about opened forks with refs to it?
442 * it's an afp specs violation because you can't delete
443 * an opened forks. Now afpd doesn't care about forks opened by other
444 * process. It's fixable within afpd if fnctl_lock, doable with smb and
445 * next to impossible for nfs and local filesystem access.
447 static void dir_invalidate( vol, dir )
448 const struct vol *vol;
452 /* v_root can't be deleted */
453 if (movecwd(vol, vol->v_root) < 0)
454 printf("Yuup cant change dir to v_root\n");
457 dirchildremove(dir->d_parent, dir);
458 dir_remove( vol, dir );
461 /* ------------------------------------ */
462 static struct dir *dir_insert(vol, dir)
463 const struct vol *vol;
469 while (pdir->d_did != dir->d_did ) {
470 if ( pdir->d_did > dir->d_did ) {
471 if ( pdir->d_left == SENTINEL ) {
478 if ( pdir->d_right == SENTINEL ) {
483 pdir = pdir->d_right;
491 * attempt to extend the current dir. tree to include path
492 * as a side-effect, movecwd to that point and return the new dir
495 extenddir( vol, dir, path )
502 path->u_name = p = mtoupath(vol, path->m_name );
503 if ( of_stat( path ) != 0 ) {
507 if (!S_ISDIR(path->st.st_mode)) {
511 if (( dir = adddir( vol, dir, path)) == NULL ) {
515 if ( movecwd( vol, dir ) < 0 ) {
522 static int deletedir(char *dir)
524 char path[MAXPATHLEN + 1];
531 if ((len = strlen(dir)) > sizeof(path))
535 if ((dp = opendir(dir)) == NULL)
541 while ((de = readdir(dp))) {
542 /* skip this and previous directory */
543 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
546 strncpy(path + len, de->d_name, sizeof(path) - len);
547 if (stat(path, &st) == 0) {
548 if (S_ISDIR(st.st_mode)) {
549 if ((err = deletedir(path)) < 0) {
553 } else if (unlink(path) < 0) {
556 continue; /* somebody went and deleted it behind our backs. */
574 /* okay. the directory is empty. delete it. note: we already got rid
576 if (rmdir(dir) < 0) {
580 case ENOTEMPTY : /* should never happen */
581 return( AFPERR_DIRNEMPT );
584 return( AFPERR_ACCESS );
588 return( AFPERR_PARAM );
594 /* do a recursive copy. */
595 static int copydir(char *src, char *dst, int noadouble)
597 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
605 /* doesn't exist or the path is too long. */
606 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
607 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
608 ((dp = opendir(src)) == NULL))
611 /* try to create the destination directory */
612 if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
616 return( AFPERR_NOOBJ );
618 return( AFPERR_VLOCK );
621 return( AFPERR_ACCESS );
623 return( AFPERR_EXIST );
626 return( AFPERR_DFULL );
628 return( AFPERR_PARAM );
632 /* set things up to copy */
640 while ((de = readdir(dp))) {
641 /* skip this and previous directory */
642 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
645 strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
646 if (stat(spath, &st) == 0) {
647 strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
649 if (S_ISDIR(st.st_mode)) {
650 if ((err = copydir(spath, dpath, noadouble)) < 0)
652 } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
656 /* keep the same time stamp. */
657 ut.actime = ut.modtime = st.st_mtime;
663 /* keep the same time stamp. */
664 if (stat(src, &st) == 0) {
665 ut.actime = ut.modtime = st.st_mtime;
675 /* --- public functions follow --- */
677 /* NOTE: we start off with at least one node (the root directory). */
678 struct dir *dirinsert( vol, dir )
684 if ((node = dir_insert(vol, dir)))
687 /* recolor the tree. the current node is red. */
688 dir->d_color = DIRTREE_COLOR_RED;
690 /* parent of this node has to be black. if the parent node
691 * is red, then we have a grandparent. */
692 while ((dir != vol->v_root) &&
693 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
694 /* are we on the left tree? */
695 if (dir->d_back == dir->d_back->d_back->d_left) {
696 node = dir->d_back->d_back->d_right; /* get the right node */
697 if (node->d_color == DIRTREE_COLOR_RED) {
698 /* we're red. we need to change to black. */
699 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
700 node->d_color = DIRTREE_COLOR_BLACK;
701 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
702 dir = dir->d_back->d_back; /* finished. go up. */
704 if (dir == dir->d_back->d_right) {
706 dir_leftrotate(vol, dir);
708 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
709 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
710 dir_rightrotate(vol, dir->d_back->d_back);
713 node = dir->d_back->d_back->d_left;
714 if (node->d_color == DIRTREE_COLOR_RED) {
715 /* we're red. we need to change to black. */
716 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
717 node->d_color = DIRTREE_COLOR_BLACK;
718 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
719 dir = dir->d_back->d_back; /* finished. ascend */
721 if (dir == dir->d_back->d_left) {
723 dir_rightrotate(vol, dir);
725 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
726 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
727 dir_leftrotate(vol, dir->d_back->d_back);
732 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
736 /* free everything down. we don't bother to recolor as this is only
737 * called to free the entire tree */
741 if (!dir || (dir == SENTINEL))
744 if ( dir->d_left != SENTINEL ) {
745 dirfree( dir->d_left );
747 if ( dir->d_right != SENTINEL ) {
748 dirfree( dir->d_right );
751 if (dir != SENTINEL) {
752 if (dir->d_u_name != dir->d_m_name) {
760 /* --------------------------------------------
761 * most of the time mac name and unix name are the same
763 struct dir *dirnew(const char *m_name, const char *u_name)
767 dir = (struct dir *) calloc(1, sizeof( struct dir ));
771 if ((dir->d_m_name = strdup(m_name)) == NULL) {
776 if (m_name == u_name) {
777 dir->d_u_name = dir->d_m_name;
779 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
785 dir->d_left = dir->d_right = SENTINEL;
786 dir->d_next = dir->d_prev = dir;
790 /* -------------------------------------------------- */
791 /* XXX: this needs to be changed to handle path types
806 XXXX u_name can be an alias on m_name if the m_name is
811 cname( vol, dir, cpath )
812 const struct vol *vol;
817 static char path[ MAXPATHLEN + 1];
818 static struct path ret;
829 afp_errno = AFPERR_NOOBJ;
830 switch (ret.m_type = *data) { /* path type */
833 len = (unsigned char) *data++;
838 if (afp_version >= 30) {
840 memcpy(&hint, data, sizeof(hint));
842 data += sizeof(hint);
844 memcpy(&len16, data, sizeof(len16));
851 /* else it's an error */
853 afp_errno = AFPERR_PARAM;
857 *cpath += len + size;
864 if ( !extend && movecwd( vol, dir ) < 0 ) {
866 movecwd failed some of dir path are not there anymore.
867 FIXME Is it true with other errors?
868 so we remove dir from the cache
870 if (dir->d_did == DIRDID_ROOT_PARENT)
872 if (afp_errno == AFPERR_ACCESS)
875 dir_invalidate(vol, dir);
888 while (*data == sep && len > 0 ) {
889 if ( dir->d_parent == NULL ) {
897 /* would this be faster with strlen + strncpy? */
899 while ( *data != sep && len > 0 ) {
904 /* short cut bits by chopping off a trailing \0. this also
905 makes the traversal happy w/ filenames at the end of the
912 if ( p != path ) { /* we got something */
916 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
919 cdir = (cdir == dir->d_child->d_prev) ? NULL :
922 if ( cdir == NULL ) {
924 /* if dir == curdir it always succeed,
925 even if curdir is deleted.
926 it's not a pb because it will fail in extenddir
928 if ( movecwd( vol, dir ) < 0 ) {
929 /* dir is not valid anymore
930 we delete dir from the cache and abort.
932 if ( dir->d_did != DIRDID_ROOT_PARENT &&
933 (afp_errno != AFPERR_ACCESS)) {
934 dir_invalidate(vol, dir);
938 cdir = extenddir( vol, dir, &ret );
942 cdir = extenddir( vol, dir, &ret );
945 if ( cdir == NULL ) {
960 * Move curdir to dir, with a possible chdir()
962 int movecwd( vol, dir)
963 const struct vol *vol;
966 char path[MAXPATHLEN + 1];
971 if ( dir == curdir ) {
974 if ( dir->d_did == DIRDID_ROOT_PARENT) {
975 afp_errno = AFPERR_PARAM;
979 p = path + sizeof(path) - 1;
982 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
985 if (p -n -1 < path) {
986 afp_errno = AFPERR_PARAM;
994 n = strlen( vol->v_path );
995 if (p -n -1 < path) {
996 afp_errno = AFPERR_PARAM;
1001 strncpy( p, vol->v_path, n );
1003 if ( chdir( p ) < 0 ) {
1007 afp_errno = AFPERR_ACCESS;
1010 afp_errno = AFPERR_NOOBJ;
1020 * We can't use unix file's perm to support Apple's inherited protection modes.
1021 * If we aren't the file's owner we can't change its perms when moving it and smb
1022 * nfs,... don't even try.
1024 #define AFP_CHECK_ACCESS
1026 int check_access(char *path, int mode)
1028 #ifdef AFP_CHECK_ACCESS
1036 accessmode(p, &ma, curdir, NULL);
1037 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1039 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1045 /* ------------------------------
1047 (name, dir) with curdir:name == dir, from afp_enumerate
1050 int getdirparams(const struct vol *vol,
1051 u_int16_t bitmap, struct path *s_path,
1053 char *buf, int *buflen )
1057 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1058 int bit = 0, isad = 0;
1063 struct stat *st = &s_path->st;
1064 char *upath = s_path->u_name;
1066 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1067 (1 << DIRPBIT_CDATE) |
1068 (1 << DIRPBIT_MDATE) |
1069 (1 << DIRPBIT_BDATE) |
1070 (1 << DIRPBIT_FINFO)))) {
1071 memset(&ad, 0, sizeof(ad));
1072 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1073 DIRBITS | 0777, &ad)) {
1079 while ( bitmap != 0 ) {
1080 while (( bitmap & 1 ) == 0 ) {
1088 ad_getattr(&ad, &ashort);
1089 } else if (*upath == '.' && strcmp(upath, ".") &&
1090 strcmp(upath, "..")) {
1091 ashort = htons(ATTRBIT_INVISIBLE);
1094 ashort |= htons(ATTRBIT_SHARED);
1095 memcpy( data, &ashort, sizeof( ashort ));
1096 data += sizeof( ashort );
1100 if ( dir->d_did == DIRDID_ROOT) {
1101 aint = DIRDID_ROOT_PARENT;
1102 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1105 aint = dir->d_parent->d_did;
1107 memcpy( data, &aint, sizeof( aint ));
1108 data += sizeof( aint );
1111 case DIRPBIT_CDATE :
1112 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1113 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1114 memcpy( data, &aint, sizeof( aint ));
1115 data += sizeof( aint );
1118 case DIRPBIT_MDATE :
1119 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1120 memcpy( data, &aint, sizeof( aint ));
1121 data += sizeof( aint );
1124 case DIRPBIT_BDATE :
1125 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1126 aint = AD_DATE_START;
1127 memcpy( data, &aint, sizeof( aint ));
1128 data += sizeof( aint );
1131 case DIRPBIT_FINFO :
1133 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1134 } else { /* no appledouble */
1135 memset( data, 0, 32 );
1136 /* set default view -- this also gets done in ad_open() */
1137 ashort = htons(FINDERINFO_CLOSEDVIEW);
1138 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1140 /* dot files are by default invisible */
1141 if (*upath == '.' && strcmp(upath, ".") &&
1142 strcmp(upath, "..")) {
1143 ashort = htons(FINDERINFO_INVISIBLE);
1144 memcpy(data + FINDERINFO_FRFLAGOFF,
1145 &ashort, sizeof(ashort));
1151 case DIRPBIT_LNAME :
1152 if (dir->d_m_name) /* root of parent can have a null name */
1155 memset(data, 0, sizeof(u_int16_t));
1156 data += sizeof( u_int16_t );
1159 case DIRPBIT_SNAME :
1160 memset(data, 0, sizeof(u_int16_t));
1161 data += sizeof( u_int16_t );
1165 memcpy( data, &dir->d_did, sizeof( aint ));
1166 data += sizeof( aint );
1169 case DIRPBIT_OFFCNT :
1171 /* this needs to handle current directory access rights */
1172 if (st->st_ctime == dir->ctime) {
1173 ashort = dir->offcnt;
1175 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1177 dir->offcnt = ashort;
1178 dir->ctime = st->st_ctime;
1180 ashort = htons( ashort );
1181 memcpy( data, &ashort, sizeof( ashort ));
1182 data += sizeof( ashort );
1186 aint = htonl(st->st_uid);
1187 memcpy( data, &aint, sizeof( aint ));
1188 data += sizeof( aint );
1192 aint = htonl(st->st_gid);
1193 memcpy( data, &aint, sizeof( aint ));
1194 data += sizeof( aint );
1197 case DIRPBIT_ACCESS :
1198 accessmode( upath, &ma, dir , st);
1200 *data++ = ma.ma_user;
1201 *data++ = ma.ma_world;
1202 *data++ = ma.ma_group;
1203 *data++ = ma.ma_owner;
1206 /* Client has requested the ProDOS information block.
1207 Just pass back the same basic block for all
1208 directories. <shirsch@ibm.net> */
1209 case DIRPBIT_PDINFO :
1210 if (afp_version >= 30) { /* UTF8 name */
1211 utf8 = kTextEncodingUTF8;
1212 if (dir->d_m_name) /* root of parent can have a null name */
1215 memset(data, 0, sizeof(u_int16_t));
1216 data += sizeof( u_int16_t );
1218 memcpy(data, &aint, sizeof( aint ));
1219 data += sizeof( aint );
1221 else { /* ProDOS Info Block */
1224 ashort = htons( 0x0200 );
1225 memcpy( data, &ashort, sizeof( ashort ));
1226 data += sizeof( ashort );
1227 memset( data, 0, sizeof( ashort ));
1228 data += sizeof( ashort );
1234 ad_close( &ad, ADFLAGS_HF );
1236 return( AFPERR_BITMAP );
1242 ashort = htons( data - buf );
1243 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1244 data = set_name(data, dir->d_m_name, 0);
1246 if ( utf_nameoff ) {
1247 ashort = htons( data - buf );
1248 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1249 data = set_name(data, dir->d_m_name, utf8);
1252 ad_close( &ad, ADFLAGS_HF );
1254 *buflen = data - buf;
1258 /* ----------------------------- */
1259 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1262 int ibuflen, *rbuflen;
1267 u_int16_t vid, bitmap;
1273 memcpy( &vid, ibuf, sizeof( vid ));
1274 ibuf += sizeof( vid );
1276 if (( vol = getvolbyvid( vid )) == NULL ) {
1277 return( AFPERR_PARAM );
1280 if (vol->v_flags & AFPVOL_RO)
1281 return AFPERR_VLOCK;
1283 memcpy( &did, ibuf, sizeof( did ));
1284 ibuf += sizeof( int );
1286 if (( dir = dirlookup( vol, did )) == NULL ) {
1290 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1291 bitmap = ntohs( bitmap );
1292 ibuf += sizeof( bitmap );
1294 if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1298 if ( *path->m_name != '\0' ) {
1299 return( AFPERR_BADTYPE ); /* not a directory */
1303 * If ibuf is odd, make it even.
1305 if ((u_long)ibuf & 1 ) {
1309 if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1310 setvoltime(obj, vol );
1316 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1318 * assume path == '\0' eg. it's a directory in canonical form
1321 struct path Cur_Path = {
1324 ".", /* unix name */
1325 0, /* stat is not set */
1329 int setdirparams(const struct vol *vol,
1330 struct path *path, u_int16_t bitmap, char *buf )
1338 int bit = 0, aint, isad = 1;
1339 u_int16_t ashort, bshort;
1341 int change_mdate = 0;
1342 int change_parent_mdate = 0;
1345 upath = path->u_name;
1346 memset(&ad, 0, sizeof(ad));
1348 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1349 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1351 * Check to see what we're trying to set. If it's anything
1352 * but ACCESS, UID, or GID, give an error. If it's any of those
1353 * three, we don't need the ad to be open, so just continue.
1355 * note: we also don't need to worry about mdate. also, be quiet
1356 * if we're using the noadouble option.
1358 if (!vol_noadouble(vol) && (bitmap &
1359 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1360 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1361 return AFPERR_ACCESS;
1367 * Check to see if a create was necessary. If it was, we'll want
1368 * to set our name, etc.
1370 if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1371 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1372 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1373 ad_getentrylen( &ad, ADEID_NAME ));
1377 while ( bitmap != 0 ) {
1378 while (( bitmap & 1 ) == 0 ) {
1387 memcpy( &ashort, buf, sizeof( ashort ));
1388 ad_getattr(&ad, &bshort);
1389 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1390 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1394 ad_setattr(&ad, bshort);
1395 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1396 change_parent_mdate = 1;
1398 buf += sizeof( ashort );
1401 case DIRPBIT_CDATE :
1404 memcpy(&aint, buf, sizeof(aint));
1405 ad_setdate(&ad, AD_DATE_CREATE, aint);
1407 buf += sizeof( aint );
1410 case DIRPBIT_MDATE :
1411 memcpy(&newdate, buf, sizeof(newdate));
1412 buf += sizeof( newdate );
1415 case DIRPBIT_BDATE :
1418 memcpy(&aint, buf, sizeof(aint));
1419 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1421 buf += sizeof( aint );
1424 case DIRPBIT_FINFO :
1427 * Alright, we admit it, this is *really* sick!
1428 * The 4 bytes that we don't copy, when we're dealing
1429 * with the root of a volume, are the directory's
1430 * location information. This eliminates that annoying
1431 * behavior one sees when mounting above another mount
1435 if ( curdir->d_did == DIRDID_ROOT ) {
1436 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1437 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1439 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1445 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1446 change_parent_mdate = 1;
1447 memcpy( &aint, buf, sizeof(aint));
1448 buf += sizeof( aint );
1449 if ( (curdir->d_did == DIRDID_ROOT) &&
1450 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1454 err = AFPERR_ACCESS;
1455 goto setdirparam_done;
1459 goto setdirparam_done;
1462 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1466 goto setdirparam_done;
1471 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1475 err = AFPERR_ACCESS;
1476 goto setdirparam_done;
1480 goto setdirparam_done;
1483 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1490 change_parent_mdate = 1;
1491 memcpy( &aint, buf, sizeof( aint ));
1492 buf += sizeof( aint );
1493 if (curdir->d_did == DIRDID_ROOT)
1494 setdeskowner( -1, ntohl(aint) );
1496 #if 0 /* don't error if we can't set the desktop owner. */
1500 err = AFPERR_ACCESS;
1501 goto setdirparam_done;
1505 goto setdirparam_done;
1508 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1511 goto setdirparam_done;
1517 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1521 err = AFPERR_ACCESS;
1522 goto setdirparam_done;
1526 goto setdirparam_done;
1529 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1536 case DIRPBIT_ACCESS :
1538 change_parent_mdate = 1;
1539 ma.ma_user = *buf++;
1540 ma.ma_world = *buf++;
1541 ma.ma_group = *buf++;
1542 ma.ma_owner = *buf++;
1544 if (curdir->d_did == DIRDID_ROOT)
1545 setdeskmode(mtoumode( &ma ));
1546 #if 0 /* don't error if we can't set the desktop mode */
1550 err = AFPERR_ACCESS;
1551 goto setdirparam_done;
1554 goto setdirparam_done;
1556 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1560 goto setdirparam_done;
1564 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1565 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1569 err = AFPERR_ACCESS;
1570 goto setdirparam_done;
1573 goto setdirparam_done;
1575 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1578 goto setdirparam_done;
1583 /* Ignore what the client thinks we should do to the
1584 ProDOS information block. Skip over the data and
1585 report nothing amiss. <shirsch@ibm.net> */
1586 case DIRPBIT_PDINFO :
1587 if (afp_version < 30) {
1592 err = AFPERR_BITMAP;
1593 goto setdirparam_done;
1602 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1603 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1607 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1608 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1613 ad_flush( &ad, ADFLAGS_HF );
1614 ad_close( &ad, ADFLAGS_HF );
1617 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1618 && gettimeofday(&tv, NULL) == 0) {
1619 if (!movecwd(vol, curdir->d_parent)) {
1620 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1621 bitmap = 1<<DIRPBIT_MDATE;
1622 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1623 /* should we reset curdir ?*/
1630 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1633 int ibuflen, *rbuflen;
1639 struct path *s_path;
1646 memcpy( &vid, ibuf, sizeof( vid ));
1647 ibuf += sizeof( vid );
1648 if (( vol = getvolbyvid( vid )) == NULL ) {
1649 return( AFPERR_PARAM );
1652 if (vol->v_flags & AFPVOL_RO)
1653 return AFPERR_VLOCK;
1655 memcpy( &did, ibuf, sizeof( did ));
1656 ibuf += sizeof( did );
1657 if (( dir = dirlookup( vol, did )) == NULL ) {
1658 return( AFPERR_NOOBJ );
1661 if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
1664 /* FIXME check done elswhere? cname was able to move curdir to it! */
1665 if (*s_path->m_name == '\0')
1666 return AFPERR_EXIST;
1668 upath = s_path->u_name;
1671 if (0 != (ret = check_name(vol, upath))) {
1676 if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1679 return( AFPERR_NOOBJ );
1681 return( AFPERR_VLOCK );
1683 return( AFPERR_ACCESS );
1685 return( AFPERR_EXIST );
1688 return( AFPERR_DFULL );
1690 return( AFPERR_PARAM );
1694 if (of_stat(s_path) < 0) {
1698 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1702 if ( movecwd( vol, dir ) < 0 ) {
1703 return( AFPERR_PARAM );
1706 memset(&ad, 0, sizeof(ad));
1707 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1708 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1709 if (vol_noadouble(vol))
1710 goto createdir_done;
1711 return( AFPERR_ACCESS );
1714 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1715 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1716 ad_getentrylen( &ad, ADEID_NAME ));
1717 ad_flush( &ad, ADFLAGS_HF );
1718 ad_close( &ad, ADFLAGS_HF );
1721 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1722 *rbuflen = sizeof( u_int32_t );
1723 setvoltime(obj, vol );
1728 * dst new unix filename (not a pathname)
1729 * newname new mac name
1733 int renamedir(src, dst, dir, newparent, newname, noadouble)
1734 char *src, *dst, *newname;
1735 struct dir *dir, *newparent;
1736 const int noadouble;
1743 /* existence check moved to afp_moveandrename */
1744 if ( unix_rename( src, dst ) < 0 ) {
1747 return( AFPERR_NOOBJ );
1749 return( AFPERR_ACCESS );
1751 return AFPERR_VLOCK;
1753 /* tried to move directory into a subdirectory of itself */
1754 return AFPERR_CANTMOVE;
1756 /* this needs to copy and delete. bleah. that means we have
1757 * to deal with entire directory hierarchies. */
1758 if ((err = copydir(src, dst, noadouble)) < 0) {
1762 if ((err = deletedir(src)) < 0)
1766 return( AFPERR_PARAM );
1770 memset(&ad, 0, sizeof(ad));
1771 len = strlen( newname );
1772 /* rename() succeeded so we need to update our tree even if we can't open
1775 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
1776 ad_setentrylen( &ad, ADEID_NAME, len );
1777 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1778 ad_flush( &ad, ADFLAGS_HF );
1779 ad_close( &ad, ADFLAGS_HF );
1782 if (dir->d_m_name == dir->d_u_name)
1783 dir->d_u_name = NULL;
1785 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
1786 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
1787 /* FIXME : fatal ? */
1790 dir->d_m_name = buf;
1791 strcpy( dir->d_m_name, newname );
1793 if (newname == dst) {
1794 free(dir->d_u_name);
1795 dir->d_u_name = dir->d_m_name;
1798 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
1799 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
1802 dir->d_u_name = buf;
1803 strcpy( dir->d_u_name, dst );
1806 if (( parent = dir->d_parent ) == NULL ) {
1809 if ( parent == newparent ) {
1813 /* detach from old parent and add to new one. */
1814 dirchildremove(parent, dir);
1815 dir->d_parent = newparent;
1816 dirchildadd(newparent, dir);
1820 #define DOT_APPLEDOUBLE_LEN 13
1821 /* delete an empty directory */
1822 int deletecurdir( vol, path, pathlen )
1823 const struct vol *vol;
1834 if ( curdir->d_parent == NULL ) {
1835 return( AFPERR_ACCESS );
1838 if ( curdir->d_child != NULL ) {
1839 return( AFPERR_DIRNEMPT );
1844 memset(&ad, 0, sizeof(ad));
1845 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1846 DIRBITS | 0777, &ad) == 0 ) {
1848 ad_getattr(&ad, &ashort);
1849 ad_close( &ad, ADFLAGS_HF );
1850 if ((ashort & htons(ATTRBIT_NODELETE))) {
1851 return AFPERR_OLOCK;
1855 /* delete stray .AppleDouble files. this happens to get .Parent files
1857 if ((dp = opendir(".AppleDouble"))) {
1858 strcpy(path, ".AppleDouble/");
1859 while ((de = readdir(dp))) {
1860 /* skip this and previous directory */
1861 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1864 /* bail if the file exists in the current directory.
1865 * note: this will not fail with dangling symlinks */
1866 if (stat(de->d_name, &st) == 0) {
1868 return AFPERR_DIRNEMPT;
1871 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1872 if (unlink(path) < 0) {
1877 return( AFPERR_ACCESS );
1879 return AFPERR_VLOCK;
1883 return( AFPERR_PARAM );
1890 if ( rmdir( ".AppleDouble" ) < 0 ) {
1895 return( AFPERR_DIRNEMPT );
1897 return AFPERR_VLOCK;
1900 return( AFPERR_ACCESS );
1902 return( AFPERR_PARAM );
1906 /* now get rid of dangling symlinks */
1907 if ((dp = opendir("."))) {
1908 while ((de = readdir(dp))) {
1909 /* skip this and previous directory */
1910 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1913 /* bail if it's not a symlink */
1914 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1915 return AFPERR_DIRNEMPT;
1918 if (unlink(de->d_name) < 0) {
1922 return( AFPERR_ACCESS );
1924 return AFPERR_VLOCK;
1928 return( AFPERR_PARAM );
1935 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1939 if ( rmdir(fdir->d_u_name) < 0 ) {
1942 return( AFPERR_NOOBJ );
1944 return( AFPERR_DIRNEMPT );
1947 return( AFPERR_ACCESS );
1949 return AFPERR_VLOCK;
1951 return( AFPERR_PARAM );
1955 dirchildremove(curdir, fdir);
1957 cnid_delete(vol->v_db, fdir->d_did);
1958 #endif /* CNID_DB */
1959 dir_remove( vol, fdir );
1964 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1967 int ibuflen, *rbuflen;
1977 sfunc = (unsigned char) *ibuf++;
1978 memcpy( &id, ibuf, sizeof( id ));
1986 case 3 :/* unicode */
1987 if (( pw = getpwuid( id )) == NULL ) {
1988 return( AFPERR_NOITEM );
1994 case 4 : /* unicode */
1995 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1996 return( AFPERR_NOITEM );
2002 return( AFPERR_PARAM );
2007 if (afp_version < 30) {
2008 return( AFPERR_PARAM );
2011 /* map to unicode */
2014 len = strlen( name );
2021 u_int16_t tp = htons(len);
2022 memcpy(rbuf, &tp, sizeof(tp));
2031 memcpy( rbuf, name, len );
2037 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2040 int ibuflen, *rbuflen;
2049 sfunc = (unsigned char) *ibuf++;
2052 case 2 : /* unicode */
2053 memcpy(&ulen, ibuf, sizeof(ulen));
2059 len = (unsigned char) *ibuf++;
2063 return( AFPERR_PARAM );
2070 case 1 : /* unicode */
2072 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
2074 return( AFPERR_NOITEM );
2079 case 2 : /* unicode */
2081 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
2083 return( AFPERR_NOITEM );
2092 memcpy( rbuf, &id, sizeof( id ));
2093 *rbuflen = sizeof( id );
2097 /* ------------------------------------
2098 variable DID support
2100 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2103 int ibuflen, *rbuflen;
2114 /* do nothing as dids are static for the life of the process. */
2118 memcpy(&vid, ibuf, sizeof( vid ));
2119 ibuf += sizeof( vid );
2120 if (( vol = getvolbyvid( vid )) == NULL ) {
2121 return( AFPERR_PARAM );
2124 memcpy( &did, ibuf, sizeof( did ));
2125 ibuf += sizeof( did );
2126 if (( dir = dirlookup( vol, did )) == NULL ) {
2127 return( AFPERR_PARAM );
2130 /* dir_remove -- deletedid */
2136 /* did creation gets done automatically
2137 * there's a pb again with case but move it to cname
2139 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2142 int ibuflen, *rbuflen;
2145 struct dir *parentdir;
2153 memcpy(&vid, ibuf, sizeof(vid));
2154 ibuf += sizeof( vid );
2156 if (( vol = getvolbyvid( vid )) == NULL ) {
2157 return( AFPERR_PARAM );
2160 memcpy(&did, ibuf, sizeof(did));
2161 ibuf += sizeof(did);
2163 if (( parentdir = dirlookup( vol, did )) == NULL ) {
2167 if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2171 if ( *path->m_name != '\0' ) {
2172 return( AFPERR_BADTYPE ); /* not a directory */
2175 if ( !path->st_valid && of_stat(path ) < 0 ) {
2176 return( AFPERR_NOOBJ );
2178 if ( path->st_errno ) {
2179 return( AFPERR_NOOBJ );
2182 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2183 *rbuflen = sizeof(curdir->d_did);