2 * $Id: directory.c,v 1.75 2003-06-05 09:17:11 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 /* ------------------- */
126 int path_isadir(struct path *o_path)
128 return o_path->dir != NULL;
130 return o_path->m_name == '\0' || /* we are in a it */
131 !o_path->st_valid || /* in cache but we can't chdir in it */
132 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
137 int get_afp_errno(const int param)
139 if (afp_errno != AFPERR_DID1)
144 /* ------------------- */
146 dirsearch_byname( cdir, name )
152 if (!strcmp(name, "."))
156 if ( strcmp( dir->d_u_name, name ) == 0 ) {
159 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
164 /* -----------------------------------------
165 * if did is not in the cache resolve it with cnid
168 * OSX call it with bogus id, ie file ID not folder ID,
169 * and we are really bad in this case.
172 dirlookup( vol, did )
173 const struct vol *vol;
180 static char path[MAXPATHLEN + 1];
183 static char buffer[12 + MAXPATHLEN + 1];
184 int buflen = 12 + MAXPATHLEN + 1;
189 ret = dirsearch(vol, did);
190 if (ret != NULL || afp_errno == AFPERR_PARAM)
193 utf8 = utf8_encoding();
194 maxpath = (utf8)?MAXPATHLEN -7:255;
196 if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) ) {
197 afp_errno = AFPERR_NOOBJ;
200 ptr = path + MAXPATHLEN;
201 if (NULL == ( mpath = utompath(vol, upath, utf8) ) ) {
202 afp_errno = AFPERR_NOOBJ;
206 pathlen = len; /* no 0 in the last part */
208 strcpy(ptr - len, mpath);
211 ret = dirsearch(vol,id);
215 if ( NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen))
217 NULL == (mpath = utompath(vol, upath, utf8))
219 afp_errno = AFPERR_NOOBJ;
223 len = strlen(mpath) + 1;
225 if (pathlen > maxpath) {
226 afp_errno = AFPERR_PARAM;
229 strcpy(ptr - len, mpath);
233 /* fill the cache, another place where we know about the path type */
239 temp16 = htons(pathlen);
240 memcpy(ptr, &temp16, sizeof(temp16));
242 temp = htonl(kTextEncodingUTF8);
244 memcpy(ptr, &temp, sizeof(temp));
250 *ptr = (unsigned char)pathlen;
254 /* cname is not efficient */
255 if (cname( vol, ret, &ptr ) == NULL )
258 return dirsearch(vol, did);
261 /* --------------------------- */
262 /* rotate the tree to the left */
263 static void dir_leftrotate(vol, dir)
267 struct dir *right = dir->d_right;
269 /* whee. move the right's left tree into dir's right tree */
270 dir->d_right = right->d_left;
271 if (right->d_left != SENTINEL)
272 right->d_left->d_back = dir;
274 if (right != SENTINEL) {
275 right->d_back = dir->d_back;
279 if (!dir->d_back) /* no parent. move the right tree to the top. */
281 else if (dir == dir->d_back->d_left) /* we were on the left */
282 dir->d_back->d_left = right;
284 dir->d_back->d_right = right; /* we were on the right */
286 /* re-insert dir on the left tree */
293 /* rotate the tree to the right */
294 static void dir_rightrotate(vol, dir)
298 struct dir *left = dir->d_left;
300 /* whee. move the left's right tree into dir's left tree */
301 dir->d_left = left->d_right;
302 if (left->d_right != SENTINEL)
303 left->d_right->d_back = dir;
305 if (left != SENTINEL) {
306 left->d_back = dir->d_back;
310 if (!dir->d_back) /* no parent. move the left tree to the top. */
312 else if (dir == dir->d_back->d_right) /* we were on the right */
313 dir->d_back->d_right = left;
315 dir->d_back->d_left = left; /* we were on the left */
317 /* re-insert dir on the right tree */
323 /* recolor after a removal */
324 static struct dir *dir_rmrecolor(vol, dir)
330 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
331 /* are we on the left tree? */
332 if (dir == dir->d_back->d_left) {
333 leaf = dir->d_back->d_right; /* get right side */
334 if (leaf->d_color == DIRTREE_COLOR_RED) {
335 /* we're red. we need to change to black. */
336 leaf->d_color = DIRTREE_COLOR_BLACK;
337 dir->d_back->d_color = DIRTREE_COLOR_RED;
338 dir_leftrotate(vol, dir->d_back);
339 leaf = dir->d_back->d_right;
342 /* right leaf has black end nodes */
343 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
344 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
345 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
346 dir = dir->d_back; /* ascend */
348 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
349 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
350 leaf->d_color = DIRTREE_COLOR_RED;
351 dir_rightrotate(vol, leaf);
352 leaf = dir->d_back->d_right;
354 leaf->d_color = dir->d_back->d_color;
355 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
356 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
357 dir_leftrotate(vol, dir->d_back);
360 } else { /* right tree */
361 leaf = dir->d_back->d_left; /* left tree */
362 if (leaf->d_color == DIRTREE_COLOR_RED) {
363 leaf->d_color = DIRTREE_COLOR_BLACK;
364 dir->d_back->d_color = DIRTREE_COLOR_RED;
365 dir_rightrotate(vol, dir->d_back);
366 leaf = dir->d_back->d_left;
369 /* left leaf has black end nodes */
370 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
371 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
372 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
373 dir = dir->d_back; /* ascend */
375 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
376 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
377 leaf->d_color = DIRTREE_COLOR_RED;
378 dir_leftrotate(vol, leaf);
379 leaf = dir->d_back->d_left;
381 leaf->d_color = dir->d_back->d_color;
382 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
383 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
384 dir_rightrotate(vol, dir->d_back);
389 dir->d_color = DIRTREE_COLOR_BLACK;
396 /* remove the node from the tree. this is just like insertion, but
397 * different. actually, it has to worry about a bunch of things that
398 * insertion doesn't care about. */
399 static void dir_remove( vol, dir )
404 struct ofork *of, *last;
405 struct dir *node, *leaf;
406 #endif /* REMOVE_NODES */
408 if (!dir || (dir == SENTINEL))
411 /* i'm not sure if it really helps to delete stuff. */
414 dir->d_m_name = NULL;
415 dir->d_u_name = NULL;
416 #else /* ! REMOVE_NODES */
418 /* go searching for a node with at most one child */
419 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
423 while (node->d_left != SENTINEL)
428 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
431 leaf->d_back = node->d_back;
434 } else if (node == node->d_back->d_left) { /* left tree */
435 node->d_back->d_left = leaf;
437 node->d_back->d_right = leaf;
440 /* we want to free node, but we also want to free the data in dir.
441 * currently, that's d_name and the directory traversal bits.
442 * we just copy the necessary bits and then fix up all the
443 * various pointers to the directory. needless to say, there are
444 * a bunch of places that store the directory struct. */
446 struct dir save, *tmp;
448 memcpy(&save, dir, sizeof(save));
449 memcpy(dir, node, sizeof(struct dir));
451 /* restore the red-black bits */
452 dir->d_left = save.d_left;
453 dir->d_right = save.d_right;
454 dir->d_back = save.d_back;
455 dir->d_color = save.d_color;
457 if (node == vol->v_dir) {/* we may need to fix up this pointer */
459 rootpar.d_child = vol->v_dir;
461 /* if we aren't the root directory, we have parents and
462 * siblings to worry about */
463 if (dir->d_parent->d_child == node)
464 dir->d_parent->d_child = dir;
465 dir->d_next->d_prev = dir;
466 dir->d_prev->d_next = dir;
469 /* fix up children. */
473 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
476 if (node == curdir) /* another pointer to fixup */
479 /* we also need to fix up oforks. bleah */
480 if ((of = dir->d_ofork)) {
481 last = of->of_d_prev;
484 of = (last == of) ? NULL : of->of_d_next;
488 /* set the node's d_name */
489 node->d_m_name = save.d_m_name;
490 node->d_u_name = save.d_u_name;
493 if (node->d_color == DIRTREE_COLOR_BLACK)
494 dir_rmrecolor(vol, leaf);
496 if (node->d_u_name != node->d_m_name) {
497 free(node->d_u_name);
499 free(node->d_m_name);
501 #endif /* ! REMOVE_NODES */
504 /* ---------------------------------------
505 * remove the node and its childs from the tree
507 * FIXME what about opened forks with refs to it?
508 * it's an afp specs violation because you can't delete
509 * an opened forks. Now afpd doesn't care about forks opened by other
510 * process. It's fixable within afpd if fnctl_lock, doable with smb and
511 * next to impossible for nfs and local filesystem access.
513 static void dir_invalidate( vol, dir )
514 const struct vol *vol;
518 /* v_root can't be deleted */
519 if (movecwd(vol, vol->v_root) < 0) {
520 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
524 dirchildremove(dir->d_parent, dir);
525 dir_remove( vol, dir );
528 /* ------------------------------------ */
529 static struct dir *dir_insert(vol, dir)
530 const struct vol *vol;
536 while (pdir->d_did != dir->d_did ) {
537 if ( pdir->d_did > dir->d_did ) {
538 if ( pdir->d_left == SENTINEL ) {
545 if ( pdir->d_right == SENTINEL ) {
550 pdir = pdir->d_right;
558 * attempt to extend the current dir. tree to include path
559 * as a side-effect, movecwd to that point and return the new dir
562 extenddir( vol, dir, path )
567 path->u_name = mtoupath(vol, path->m_name, utf8_encoding() );
570 if ( path->u_name == NULL) {
571 afp_errno = AFPERR_PARAM;
574 if (of_stat( path ) != 0 ) {
578 if (!S_ISDIR(path->st.st_mode)) {
582 if (( dir = adddir( vol, dir, path)) == NULL ) {
587 if ( movecwd( vol, dir ) < 0 ) {
594 /* -------------------
595 system rmdir with afp error code.
596 ENOENT is not an error.
598 static int netatalk_rmdir(const char *name)
600 if (rmdir(name) < 0) {
605 return AFPERR_DIRNEMPT;
608 return AFPERR_ACCESS;
618 /* -------------------------
619 appledouble mkdir afp error code.
621 static int netatalk_mkdir(const char *name)
623 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
626 return( AFPERR_NOOBJ );
628 return( AFPERR_VLOCK );
631 return( AFPERR_ACCESS );
633 return( AFPERR_EXIST );
636 return( AFPERR_DFULL );
638 return( AFPERR_PARAM );
644 /* -------------------
645 system unlink with afp error code.
646 ENOENT is not an error.
648 int netatalk_unlink(const char *name)
650 if (unlink(name) < 0) {
658 return AFPERR_ACCESS;
666 /* ------------------- */
667 static int deletedir(char *dir)
669 char path[MAXPATHLEN + 1];
677 if ((len = strlen(dir)) +2 > sizeof(path))
681 if ((dp = opendir(dir)) == NULL)
687 remain = sizeof(path) -len -1;
688 while ((de = readdir(dp)) && err == AFP_OK) {
689 /* skip this and previous directory */
690 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
693 if (strlen(de->d_name) > remain) {
697 strcpy(path + len, de->d_name);
698 if (stat(path, &st)) {
701 if (S_ISDIR(st.st_mode)) {
702 err = deletedir(path);
704 err = netatalk_unlink(path);
709 /* okay. the directory is empty. delete it. note: we already got rid
712 err = netatalk_rmdir(dir);
717 /* do a recursive copy. */
718 static int copydir(char *src, char *dst, int noadouble)
720 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
730 /* doesn't exist or the path is too long. */
731 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
732 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
733 ((dp = opendir(src)) == NULL))
736 /* try to create the destination directory */
737 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
742 /* set things up to copy */
746 srem = sizeof(spath) - slen -1;
751 drem = sizeof(dpath) - dlen -1;
754 while ((de = readdir(dp))) {
755 /* skip this and previous directory */
756 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
759 if (strlen(de->d_name) > srem) {
763 strcpy(spath + slen, de->d_name);
765 if (stat(spath, &st) == 0) {
766 if (strlen(de->d_name) > drem) {
770 strcpy(dpath + dlen, de->d_name);
772 if (S_ISDIR(st.st_mode)) {
773 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
775 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
779 /* keep the same time stamp. */
780 ut.actime = ut.modtime = st.st_mtime;
786 /* keep the same time stamp. */
787 if (stat(src, &st) == 0) {
788 ut.actime = ut.modtime = st.st_mtime;
798 /* --- public functions follow --- */
800 /* NOTE: we start off with at least one node (the root directory). */
801 struct dir *dirinsert( vol, dir )
807 if ((node = dir_insert(vol, dir)))
810 /* recolor the tree. the current node is red. */
811 dir->d_color = DIRTREE_COLOR_RED;
813 /* parent of this node has to be black. if the parent node
814 * is red, then we have a grandparent. */
815 while ((dir != vol->v_root) &&
816 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
817 /* are we on the left tree? */
818 if (dir->d_back == dir->d_back->d_back->d_left) {
819 node = dir->d_back->d_back->d_right; /* get the right node */
820 if (node->d_color == DIRTREE_COLOR_RED) {
821 /* we're red. we need to change to black. */
822 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
823 node->d_color = DIRTREE_COLOR_BLACK;
824 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
825 dir = dir->d_back->d_back; /* finished. go up. */
827 if (dir == dir->d_back->d_right) {
829 dir_leftrotate(vol, dir);
831 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
832 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
833 dir_rightrotate(vol, dir->d_back->d_back);
836 node = dir->d_back->d_back->d_left;
837 if (node->d_color == DIRTREE_COLOR_RED) {
838 /* we're red. we need to change to black. */
839 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
840 node->d_color = DIRTREE_COLOR_BLACK;
841 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
842 dir = dir->d_back->d_back; /* finished. ascend */
844 if (dir == dir->d_back->d_left) {
846 dir_rightrotate(vol, dir);
848 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
849 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
850 dir_leftrotate(vol, dir->d_back->d_back);
855 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
859 /* free everything down. we don't bother to recolor as this is only
860 * called to free the entire tree */
861 void dirfreename(struct dir *dir)
863 if (dir->d_u_name != dir->d_m_name) {
872 if (!dir || (dir == SENTINEL))
875 if ( dir->d_left != SENTINEL ) {
876 dirfree( dir->d_left );
878 if ( dir->d_right != SENTINEL ) {
879 dirfree( dir->d_right );
882 if (dir != SENTINEL) {
888 /* --------------------------------------------
889 * most of the time mac name and unix name are the same
891 struct dir *dirnew(const char *m_name, const char *u_name)
895 dir = (struct dir *) calloc(1, sizeof( struct dir ));
899 if ((dir->d_m_name = strdup(m_name)) == NULL) {
904 if (m_name == u_name) {
905 dir->d_u_name = dir->d_m_name;
907 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
913 dir->d_left = dir->d_right = SENTINEL;
914 dir->d_next = dir->d_prev = dir;
918 /* -------------------------------------------------- */
924 stat the file or errno
927 curdir: filename parent directory
933 stat the dir or errno
937 curdir: dir parent directory
945 curdir: dir parent directory
952 cname( vol, dir, cpath )
953 const struct vol *vol;
958 static char path[ MAXPATHLEN + 1];
959 static struct path ret;
970 afp_errno = AFPERR_NOOBJ;
971 switch (ret.m_type = *data) { /* path type */
974 len = (unsigned char) *data++;
979 if (afp_version >= 30) {
981 memcpy(&hint, data, sizeof(hint));
983 data += sizeof(hint);
985 memcpy(&len16, data, sizeof(len16));
992 /* else it's an error */
994 afp_errno = AFPERR_PARAM;
997 *cpath += len + size;
1005 if (movecwd( vol, dir ) < 0 ) {
1007 movecwd failed some of dir path are not there anymore.
1008 FIXME Is it true with other errors?
1009 so we remove dir from the cache
1011 if (dir->d_did == DIRDID_ROOT_PARENT)
1013 if (afp_errno == AFPERR_ACCESS) {
1014 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1017 ret.m_name = dir->d_m_name;
1018 ret.u_name = dir->d_u_name;
1021 } else if (afp_errno == AFPERR_NOOBJ) {
1022 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1025 strcpy(path, dir->d_m_name);
1026 if (dir->d_m_name == dir->d_u_name) {
1030 size_t tp = strlen(path)+1;
1032 strcpy(path +tp, dir->d_u_name);
1033 ret.u_name = path +tp;
1036 dir_invalidate(vol, dir);
1039 dir_invalidate(vol, dir);
1042 if (*path == '\0') {
1049 if (*data == sep ) {
1053 while (*data == sep && len > 0 ) {
1054 if ( dir->d_parent == NULL ) {
1057 dir = dir->d_parent;
1062 /* would this be faster with strlen + strncpy? */
1064 while ( *data != sep && len > 0 ) {
1066 if (p > &path[ MAXPATHLEN]) {
1067 afp_errno = AFPERR_PARAM;
1073 /* short cut bits by chopping off a trailing \0. this also
1074 makes the traversal happy w/ filenames at the end of the
1081 if ( p != path ) { /* we got something */
1083 cdir = dir->d_child;
1085 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1088 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1091 if ( cdir == NULL ) {
1093 /* if dir == curdir it always succeed,
1094 even if curdir is deleted.
1095 it's not a pb because it will fail in extenddir
1097 if ( movecwd( vol, dir ) < 0 ) {
1098 /* dir is not valid anymore
1099 we delete dir from the cache and abort.
1101 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1102 afp_errno = AFPERR_NOOBJ;
1105 if (afp_errno == AFPERR_ACCESS)
1107 dir_invalidate(vol, dir);
1110 cdir = extenddir( vol, dir, &ret );
1114 cdir = extenddir( vol, dir, &ret );
1117 if ( cdir == NULL ) {
1119 if ( len > 0 || !ret.u_name) {
1132 * Move curdir to dir, with a possible chdir()
1134 int movecwd( vol, dir)
1135 const struct vol *vol;
1138 char path[MAXPATHLEN + 1];
1143 if ( dir == curdir ) {
1146 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1147 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1151 p = path + sizeof(path) - 1;
1154 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1157 /* parent directory is deleted */
1158 afp_errno = AFPERR_NOOBJ;
1162 if (p -n -1 < path) {
1163 afp_errno = AFPERR_PARAM;
1170 if ( d != curdir ) {
1171 n = strlen( vol->v_path );
1172 if (p -n -1 < path) {
1173 afp_errno = AFPERR_PARAM;
1178 strncpy( p, vol->v_path, n );
1180 if ( chdir( p ) < 0 ) {
1184 afp_errno = AFPERR_ACCESS;
1187 afp_errno = AFPERR_NOOBJ;
1197 * We can't use unix file's perm to support Apple's inherited protection modes.
1198 * If we aren't the file's owner we can't change its perms when moving it and smb
1199 * nfs,... don't even try.
1201 #define AFP_CHECK_ACCESS
1203 int check_access(char *path, int mode)
1205 #ifdef AFP_CHECK_ACCESS
1213 accessmode(p, &ma, curdir, NULL);
1214 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1216 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1222 /* --------------------- */
1223 int file_access(struct path *path, int mode)
1227 accessmode(path->u_name, &ma, curdir, &path->st);
1228 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1230 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1236 /* ------------------------------
1238 (name, dir) with curdir:name == dir, from afp_enumerate
1241 int getdirparams(const struct vol *vol,
1242 u_int16_t bitmap, struct path *s_path,
1244 char *buf, int *buflen )
1248 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1249 int bit = 0, isad = 0;
1254 struct stat *st = &s_path->st;
1255 char *upath = s_path->u_name;
1257 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1258 (1 << DIRPBIT_CDATE) |
1259 (1 << DIRPBIT_MDATE) |
1260 (1 << DIRPBIT_BDATE) |
1261 (1 << DIRPBIT_FINFO)))) {
1262 memset(&ad, 0, sizeof(ad));
1263 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1264 DIRBITS | 0777, &ad)) {
1270 while ( bitmap != 0 ) {
1271 while (( bitmap & 1 ) == 0 ) {
1279 ad_getattr(&ad, &ashort);
1280 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1281 && strcmp(dir->d_u_name, "..")) {
1282 ashort = htons(ATTRBIT_INVISIBLE);
1285 ashort |= htons(ATTRBIT_SHARED);
1286 memcpy( data, &ashort, sizeof( ashort ));
1287 data += sizeof( ashort );
1291 if ( dir->d_did == DIRDID_ROOT) {
1292 aint = DIRDID_ROOT_PARENT;
1293 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1296 aint = dir->d_parent->d_did;
1298 memcpy( data, &aint, sizeof( aint ));
1299 data += sizeof( aint );
1302 case DIRPBIT_CDATE :
1303 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1304 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1305 memcpy( data, &aint, sizeof( aint ));
1306 data += sizeof( aint );
1309 case DIRPBIT_MDATE :
1310 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1311 memcpy( data, &aint, sizeof( aint ));
1312 data += sizeof( aint );
1315 case DIRPBIT_BDATE :
1316 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1317 aint = AD_DATE_START;
1318 memcpy( data, &aint, sizeof( aint ));
1319 data += sizeof( aint );
1322 case DIRPBIT_FINFO :
1324 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1325 } else { /* no appledouble */
1326 memset( data, 0, 32 );
1327 /* set default view -- this also gets done in ad_open() */
1328 ashort = htons(FINDERINFO_CLOSEDVIEW);
1329 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1331 /* dot files are by default invisible */
1332 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1333 strcmp(dir->d_u_name , "..")) {
1334 ashort = htons(FINDERINFO_INVISIBLE);
1335 memcpy(data + FINDERINFO_FRFLAGOFF,
1336 &ashort, sizeof(ashort));
1342 case DIRPBIT_LNAME :
1343 if (dir->d_m_name) /* root of parent can have a null name */
1346 memset(data, 0, sizeof(u_int16_t));
1347 data += sizeof( u_int16_t );
1350 case DIRPBIT_SNAME :
1351 memset(data, 0, sizeof(u_int16_t));
1352 data += sizeof( u_int16_t );
1356 memcpy( data, &dir->d_did, sizeof( aint ));
1357 data += sizeof( aint );
1360 case DIRPBIT_OFFCNT :
1362 /* this needs to handle current directory access rights */
1363 if (st->st_ctime == dir->ctime) {
1364 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1366 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1368 dir->ctime = st->st_ctime;
1369 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1371 ashort = htons( ashort );
1372 memcpy( data, &ashort, sizeof( ashort ));
1373 data += sizeof( ashort );
1377 aint = htonl(st->st_uid);
1378 memcpy( data, &aint, sizeof( aint ));
1379 data += sizeof( aint );
1383 aint = htonl(st->st_gid);
1384 memcpy( data, &aint, sizeof( aint ));
1385 data += sizeof( aint );
1388 case DIRPBIT_ACCESS :
1389 accessmode( upath, &ma, dir , st);
1391 *data++ = ma.ma_user;
1392 *data++ = ma.ma_world;
1393 *data++ = ma.ma_group;
1394 *data++ = ma.ma_owner;
1397 /* Client has requested the ProDOS information block.
1398 Just pass back the same basic block for all
1399 directories. <shirsch@ibm.net> */
1400 case DIRPBIT_PDINFO :
1401 if (afp_version >= 30) { /* UTF8 name */
1402 utf8 = kTextEncodingUTF8;
1403 if (dir->d_m_name) /* root of parent can have a null name */
1406 memset(data, 0, sizeof(u_int16_t));
1407 data += sizeof( u_int16_t );
1409 memcpy(data, &aint, sizeof( aint ));
1410 data += sizeof( aint );
1412 else { /* ProDOS Info Block */
1415 ashort = htons( 0x0200 );
1416 memcpy( data, &ashort, sizeof( ashort ));
1417 data += sizeof( ashort );
1418 memset( data, 0, sizeof( ashort ));
1419 data += sizeof( ashort );
1423 case DIRPBIT_UNIXPR :
1424 aint = htonl(st->st_uid);
1425 memcpy( data, &aint, sizeof( aint ));
1426 data += sizeof( aint );
1427 aint = htonl(st->st_gid);
1428 memcpy( data, &aint, sizeof( aint ));
1429 data += sizeof( aint );
1432 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1433 memcpy( data, &aint, sizeof( aint ));
1434 data += sizeof( aint );
1436 accessmode( upath, &ma, dir , st);
1438 *data++ = ma.ma_user;
1439 *data++ = ma.ma_world;
1440 *data++ = ma.ma_group;
1441 *data++ = ma.ma_owner;
1447 ad_close( &ad, ADFLAGS_HF );
1449 return( AFPERR_BITMAP );
1455 ashort = htons( data - buf );
1456 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1457 data = set_name(vol, data, dir->d_m_name, 0);
1459 if ( utf_nameoff ) {
1460 ashort = htons( data - buf );
1461 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1462 data = set_name(vol, data, dir->d_m_name, utf8);
1465 ad_close( &ad, ADFLAGS_HF );
1467 *buflen = data - buf;
1471 /* ----------------------------- */
1472 int path_error(struct path *path, int error)
1474 /* - a dir with access error
1475 * - no error it's a file
1478 if (path_isadir(path))
1482 return AFPERR_BADTYPE ;
1485 /* ----------------------------- */
1486 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1489 int ibuflen, *rbuflen;
1494 u_int16_t vid, bitmap;
1500 memcpy( &vid, ibuf, sizeof( vid ));
1501 ibuf += sizeof( vid );
1503 if (NULL == ( vol = getvolbyvid( vid )) ) {
1504 return( AFPERR_PARAM );
1507 if (vol->v_flags & AFPVOL_RO)
1508 return AFPERR_VLOCK;
1510 memcpy( &did, ibuf, sizeof( did ));
1511 ibuf += sizeof( int );
1513 if (NULL == ( dir = dirlookup( vol, did )) ) {
1517 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1518 bitmap = ntohs( bitmap );
1519 ibuf += sizeof( bitmap );
1521 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1522 return get_afp_errno(AFPERR_NOOBJ);
1525 if ( *path->m_name != '\0' ) {
1526 return path_error(path, AFPERR_NOOBJ);
1530 * If ibuf is odd, make it even.
1532 if ((u_long)ibuf & 1 ) {
1536 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1537 setvoltime(obj, vol );
1543 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1545 * assume path == '\0' eg. it's a directory in canonical form
1548 struct path Cur_Path = {
1551 ".", /* unix name */
1552 0, /* stat is not set */
1556 int setdirparams(const struct vol *vol,
1557 struct path *path, u_int16_t bitmap, char *buf )
1565 int bit = 0, aint, isad = 1;
1566 u_int16_t ashort, bshort;
1568 int change_mdate = 0;
1569 int change_parent_mdate = 0;
1572 upath = path->u_name;
1573 memset(&ad, 0, sizeof(ad));
1575 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1576 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1578 * Check to see what we're trying to set. If it's anything
1579 * but ACCESS, UID, or GID, give an error. If it's any of those
1580 * three, we don't need the ad to be open, so just continue.
1582 * note: we also don't need to worry about mdate. also, be quiet
1583 * if we're using the noadouble option.
1585 if (!vol_noadouble(vol) && (bitmap &
1586 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1587 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1588 return AFPERR_ACCESS;
1594 * Check to see if a create was necessary. If it was, we'll want
1595 * to set our name, etc.
1597 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1598 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1599 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1600 ad_getentrylen( &ad, ADEID_NAME ));
1604 while ( bitmap != 0 ) {
1605 while (( bitmap & 1 ) == 0 ) {
1614 memcpy( &ashort, buf, sizeof( ashort ));
1615 ad_getattr(&ad, &bshort);
1616 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1617 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1621 ad_setattr(&ad, bshort);
1622 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1623 change_parent_mdate = 1;
1625 buf += sizeof( ashort );
1628 case DIRPBIT_CDATE :
1631 memcpy(&aint, buf, sizeof(aint));
1632 ad_setdate(&ad, AD_DATE_CREATE, aint);
1634 buf += sizeof( aint );
1637 case DIRPBIT_MDATE :
1638 memcpy(&newdate, buf, sizeof(newdate));
1639 buf += sizeof( newdate );
1642 case DIRPBIT_BDATE :
1645 memcpy(&aint, buf, sizeof(aint));
1646 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1648 buf += sizeof( aint );
1651 case DIRPBIT_FINFO :
1654 * Alright, we admit it, this is *really* sick!
1655 * The 4 bytes that we don't copy, when we're dealing
1656 * with the root of a volume, are the directory's
1657 * location information. This eliminates that annoying
1658 * behavior one sees when mounting above another mount
1662 if ( curdir->d_did == DIRDID_ROOT ) {
1663 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1664 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1666 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1672 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1673 change_parent_mdate = 1;
1674 memcpy( &aint, buf, sizeof(aint));
1675 buf += sizeof( aint );
1676 if ( (curdir->d_did == DIRDID_ROOT) &&
1677 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1681 err = AFPERR_ACCESS;
1682 goto setdirparam_done;
1686 goto setdirparam_done;
1689 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1693 goto setdirparam_done;
1698 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1702 err = AFPERR_ACCESS;
1703 goto setdirparam_done;
1707 goto setdirparam_done;
1710 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1717 change_parent_mdate = 1;
1718 memcpy( &aint, buf, sizeof( aint ));
1719 buf += sizeof( aint );
1720 if (curdir->d_did == DIRDID_ROOT)
1721 setdeskowner( -1, ntohl(aint) );
1723 #if 0 /* don't error if we can't set the desktop owner. */
1727 err = AFPERR_ACCESS;
1728 goto setdirparam_done;
1732 goto setdirparam_done;
1735 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1738 goto setdirparam_done;
1744 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1748 err = AFPERR_ACCESS;
1749 goto setdirparam_done;
1753 goto setdirparam_done;
1756 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1763 case DIRPBIT_ACCESS :
1765 change_parent_mdate = 1;
1766 ma.ma_user = *buf++;
1767 ma.ma_world = *buf++;
1768 ma.ma_group = *buf++;
1769 ma.ma_owner = *buf++;
1771 if (curdir->d_did == DIRDID_ROOT)
1772 setdeskmode(mtoumode( &ma ));
1773 #if 0 /* don't error if we can't set the desktop mode */
1777 err = AFPERR_ACCESS;
1778 goto setdirparam_done;
1781 goto setdirparam_done;
1783 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1787 goto setdirparam_done;
1791 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1792 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1796 err = AFPERR_ACCESS;
1797 goto setdirparam_done;
1800 goto setdirparam_done;
1802 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1805 goto setdirparam_done;
1810 /* Ignore what the client thinks we should do to the
1811 ProDOS information block. Skip over the data and
1812 report nothing amiss. <shirsch@ibm.net> */
1813 case DIRPBIT_PDINFO :
1814 if (afp_version < 30) {
1819 case DIRPBIT_UNIXPR :
1820 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1821 buf += sizeof( aint );
1822 buf += sizeof( aint );
1825 change_parent_mdate = 1;
1826 memcpy( &aint, buf, sizeof( aint ));
1827 buf += sizeof( aint );
1828 aint = ntohl (aint);
1829 if (curdir->d_did == DIRDID_ROOT)
1830 setdeskmode( aint );
1831 #if 0 /* don't error if we can't set the desktop mode */
1835 err = AFPERR_ACCESS;
1836 goto setdirparam_done;
1839 goto setdirparam_done;
1841 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1845 goto setdirparam_done;
1849 if ( setdirunixmode( aint, vol_noadouble(vol),
1850 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1854 err = AFPERR_ACCESS;
1855 goto setdirparam_done;
1858 goto setdirparam_done;
1860 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1863 goto setdirparam_done;
1869 err = AFPERR_BITMAP;
1870 goto setdirparam_done;
1879 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1880 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1884 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1885 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1890 ad_flush( &ad, ADFLAGS_HF );
1891 ad_close( &ad, ADFLAGS_HF );
1894 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1895 && gettimeofday(&tv, NULL) == 0) {
1896 if (!movecwd(vol, curdir->d_parent)) {
1897 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1898 bitmap = 1<<DIRPBIT_MDATE;
1899 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1900 /* should we reset curdir ?*/
1907 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1910 int ibuflen, *rbuflen;
1916 struct path *s_path;
1924 memcpy( &vid, ibuf, sizeof( vid ));
1925 ibuf += sizeof( vid );
1926 if (NULL == ( vol = getvolbyvid( vid )) ) {
1927 return( AFPERR_PARAM );
1930 if (vol->v_flags & AFPVOL_RO)
1931 return AFPERR_VLOCK;
1933 memcpy( &did, ibuf, sizeof( did ));
1934 ibuf += sizeof( did );
1935 if (NULL == ( dir = dirlookup( vol, did )) ) {
1936 return afp_errno; /* was AFPERR_NOOBJ */
1938 /* for concurrent access we need to be sure we are not in the
1939 * folder we want to create...
1943 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1944 return get_afp_errno(AFPERR_PARAM);
1946 /* cname was able to move curdir to it! */
1947 if (*s_path->m_name == '\0')
1948 return AFPERR_EXIST;
1950 upath = s_path->u_name;
1951 if (0 != (err = check_name(vol, upath))) {
1955 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1959 if (of_stat(s_path) < 0) {
1963 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1967 if ( movecwd( vol, dir ) < 0 ) {
1968 return( AFPERR_PARAM );
1971 memset(&ad, 0, sizeof(ad));
1972 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1973 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1974 if (vol_noadouble(vol))
1975 goto createdir_done;
1976 return( AFPERR_ACCESS );
1979 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1980 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1981 ad_getentrylen( &ad, ADEID_NAME ));
1982 ad_flush( &ad, ADFLAGS_HF );
1983 ad_close( &ad, ADFLAGS_HF );
1986 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1987 *rbuflen = sizeof( u_int32_t );
1988 setvoltime(obj, vol );
1993 * dst new unix filename (not a pathname)
1994 * newname new mac name
1998 int renamedir(src, dst, dir, newparent, newname, noadouble)
1999 char *src, *dst, *newname;
2000 struct dir *dir, *newparent;
2001 const int noadouble;
2008 /* existence check moved to afp_moveandrename */
2009 if ( unix_rename( src, dst ) < 0 ) {
2012 return( AFPERR_NOOBJ );
2014 return( AFPERR_ACCESS );
2016 return AFPERR_VLOCK;
2018 /* tried to move directory into a subdirectory of itself */
2019 return AFPERR_CANTMOVE;
2021 /* this needs to copy and delete. bleah. that means we have
2022 * to deal with entire directory hierarchies. */
2023 if ((err = copydir(src, dst, noadouble)) < 0) {
2027 if ((err = deletedir(src)) < 0)
2031 return( AFPERR_PARAM );
2035 memset(&ad, 0, sizeof(ad));
2036 len = strlen( newname );
2037 /* rename() succeeded so we need to update our tree even if we can't open
2040 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2041 ad_setentrylen( &ad, ADEID_NAME, len );
2042 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2043 ad_flush( &ad, ADFLAGS_HF );
2044 ad_close( &ad, ADFLAGS_HF );
2047 if (dir->d_m_name == dir->d_u_name)
2048 dir->d_u_name = NULL;
2050 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2051 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2052 /* FIXME : fatal ? */
2055 dir->d_m_name = buf;
2056 strcpy( dir->d_m_name, newname );
2058 if (newname == dst) {
2059 free(dir->d_u_name);
2060 dir->d_u_name = dir->d_m_name;
2063 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2064 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2067 dir->d_u_name = buf;
2068 strcpy( dir->d_u_name, dst );
2071 if (( parent = dir->d_parent ) == NULL ) {
2074 if ( parent == newparent ) {
2078 /* detach from old parent and add to new one. */
2079 dirchildremove(parent, dir);
2080 dir->d_parent = newparent;
2081 dirchildadd(newparent, dir);
2085 #define DOT_APPLEDOUBLE_LEN 13
2086 /* delete an empty directory */
2087 int deletecurdir( vol, path, pathlen )
2088 const struct vol *vol;
2100 if ( curdir->d_parent == NULL ) {
2101 return( AFPERR_ACCESS );
2106 memset(&ad, 0, sizeof(ad));
2107 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2108 DIRBITS | 0777, &ad) == 0 ) {
2110 ad_getattr(&ad, &ashort);
2111 ad_close( &ad, ADFLAGS_HF );
2112 if ((ashort & htons(ATTRBIT_NODELETE))) {
2113 return AFPERR_OLOCK;
2117 /* delete stray .AppleDouble files. this happens to get .Parent files
2119 if ((dp = opendir(".AppleDouble"))) {
2120 strcpy(path, ".AppleDouble/");
2121 while ((de = readdir(dp))) {
2122 /* skip this and previous directory */
2123 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2126 /* bail if the file exists in the current directory.
2127 * note: this will not fail with dangling symlinks */
2128 if (stat(de->d_name, &st) == 0) {
2130 return AFPERR_DIRNEMPT;
2133 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2134 if ((err = netatalk_unlink(path))) {
2142 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2146 /* now get rid of dangling symlinks */
2147 if ((dp = opendir("."))) {
2148 while ((de = readdir(dp))) {
2149 /* skip this and previous directory */
2150 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2153 /* bail if it's not a symlink */
2154 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2156 return AFPERR_DIRNEMPT;
2159 if ((err = netatalk_unlink(de->d_name))) {
2166 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2171 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2172 dirchildremove(curdir, fdir);
2174 cnid_delete(vol->v_db, fdir->d_did);
2175 #endif /* CNID_DB */
2176 dir_remove( vol, fdir );
2181 /* inode is used as key for cnid.
2182 * Close the descriptor only after cnid_delete
2190 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2193 int ibuflen, *rbuflen;
2203 sfunc = (unsigned char) *ibuf++;
2204 memcpy( &id, ibuf, sizeof( id ));
2209 if (sfunc == 3 || sfunc == 4) {
2210 if (afp_version < 30) {
2211 return( AFPERR_PARAM );
2218 case 3 :/* unicode */
2219 if (( pw = getpwuid( id )) == NULL ) {
2220 return( AFPERR_NOITEM );
2226 case 4 : /* unicode */
2227 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2228 return( AFPERR_NOITEM );
2234 return( AFPERR_PARAM );
2236 len = strlen( name );
2243 u_int16_t tp = htons(len);
2244 memcpy(rbuf, &tp, sizeof(tp));
2253 memcpy( rbuf, name, len );
2259 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2262 int ibuflen, *rbuflen;
2271 sfunc = (unsigned char) *ibuf++;
2275 case 2 : /* unicode */
2276 if (afp_version < 30) {
2277 return( AFPERR_PARAM );
2279 memcpy(&ulen, ibuf, sizeof(ulen));
2285 len = (unsigned char) *ibuf++;
2288 return( AFPERR_PARAM );
2295 case 1 : /* unicode */
2297 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2298 return( AFPERR_NOITEM );
2303 case 2 : /* unicode */
2305 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2306 return( AFPERR_NOITEM );
2315 memcpy( rbuf, &id, sizeof( id ));
2316 *rbuflen = sizeof( id );
2320 /* ------------------------------------
2321 variable DID support
2323 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2326 int ibuflen, *rbuflen;
2337 /* do nothing as dids are static for the life of the process. */
2341 memcpy(&vid, ibuf, sizeof( vid ));
2342 ibuf += sizeof( vid );
2343 if (( vol = getvolbyvid( vid )) == NULL ) {
2344 return( AFPERR_PARAM );
2347 memcpy( &did, ibuf, sizeof( did ));
2348 ibuf += sizeof( did );
2349 if (( dir = dirlookup( vol, did )) == NULL ) {
2350 return( AFPERR_PARAM );
2353 /* dir_remove -- deletedid */
2359 /* did creation gets done automatically
2360 * there's a pb again with case but move it to cname
2362 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2365 int ibuflen, *rbuflen;
2368 struct dir *parentdir;
2376 memcpy(&vid, ibuf, sizeof(vid));
2377 ibuf += sizeof( vid );
2379 if (NULL == ( vol = getvolbyvid( vid )) ) {
2380 return( AFPERR_PARAM );
2383 memcpy(&did, ibuf, sizeof(did));
2384 ibuf += sizeof(did);
2386 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2390 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2391 return get_afp_errno(AFPERR_PARAM);
2394 if ( *path->m_name != '\0' ) {
2395 return path_error(path, AFPERR_NOOBJ);
2398 if ( !path->st_valid && of_stat(path ) < 0 ) {
2399 return( AFPERR_NOOBJ );
2401 if ( path->st_errno ) {
2402 return( AFPERR_NOOBJ );
2405 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2406 *rbuflen = sizeof(curdir->d_did);