2 * $Id: directory.c,v 1.71.2.4.2.3 2003-10-30 05:57:44 bfernhomberg 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 */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
38 #endif /* HAVE_FCNTL_H */
41 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <netatalk/endian.h>
47 #include <atalk/adouble.h>
48 #include <atalk/afp.h>
49 #include <atalk/util.h>
50 #include <atalk/cnid.h>
51 #include <atalk/logger.h>
53 #include "directory.h"
66 #define SENTINEL (&sentinel)
67 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
68 NULL, NULL, NULL, NULL, NULL, 0, 0,
70 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
71 NULL, NULL, NULL, NULL, NULL, 0, 0,
74 /* (from IM: Toolbox Essentials)
75 * dirFinderInfo (DInfo) fields:
77 * frRect 8 folder's window rectangle
79 * frLocation 4 folder's location in window
80 * frView 2 folder's view (default == closedView (256))
82 * extended dirFinderInfo (DXInfo) fields:
83 * frScroll 4 scroll position
84 * frOpenChain: 4 directory ID chain of open folders
85 * frScript: 1 script flag and code
86 * frXFlags: 1 reserved
87 * frComment: 2 comment ID
88 * frPutAway: 4 home directory ID
92 * redid did assignment for directories. now we use red-black trees.
97 const struct vol *vol;
103 /* check for 0 did */
105 afp_errno = AFPERR_PARAM;
108 if ( did == DIRDID_ROOT_PARENT ) {
110 rootpar.d_did = DIRDID_ROOT_PARENT;
111 rootpar.d_child = vol->v_dir;
116 afp_errno = AFPERR_NOOBJ;
117 while ( dir != SENTINEL ) {
118 if (dir->d_did == did)
119 return dir->d_m_name ? dir : NULL;
120 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
125 /* ------------------- */
127 int path_isadir(struct path *o_path)
129 return o_path->dir != NULL;
131 return o_path->m_name == '\0' || /* we are in a it */
132 !o_path->st_valid || /* in cache but we can't chdir in it */
133 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
138 int get_afp_errno(const int param)
140 if (afp_errno != AFPERR_DID1)
145 /* ------------------- */
147 dirsearch_byname( cdir, name )
153 if (!strcmp(name, "."))
157 if ( strcmp( dir->d_u_name, name ) == 0 ) {
160 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
165 /* -----------------------------------------
166 * if did is not in the cache resolve it with cnid
169 * OSX call it with bogus id, ie file ID not folder ID,
170 * and we are really bad in this case.
173 dirlookup( vol, did )
174 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_cdb, &id, buffer, buflen)) ) {
197 afp_errno = AFPERR_NOOBJ;
200 ptr = path + MAXPATHLEN;
201 if (NULL == ( mpath = utompath(vol, upath, did, 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);
216 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
218 NULL == (mpath = utompath(vol, upath, cnid, utf8))
220 afp_errno = AFPERR_NOOBJ;
224 len = strlen(mpath) + 1;
226 if (pathlen > maxpath) {
227 afp_errno = AFPERR_PARAM;
230 strcpy(ptr - len, mpath);
234 /* fill the cache, another place where we know about the path type */
240 temp16 = htons(pathlen);
241 memcpy(ptr, &temp16, sizeof(temp16));
243 temp = htonl(kTextEncodingUTF8);
245 memcpy(ptr, &temp, sizeof(temp));
251 *ptr = (unsigned char)pathlen;
255 /* cname is not efficient */
256 if (cname( vol, ret, &ptr ) == NULL )
259 return dirsearch(vol, did);
262 /* --------------------------- */
263 /* rotate the tree to the left */
264 static void dir_leftrotate(vol, dir)
268 struct dir *right = dir->d_right;
270 /* whee. move the right's left tree into dir's right tree */
271 dir->d_right = right->d_left;
272 if (right->d_left != SENTINEL)
273 right->d_left->d_back = dir;
275 if (right != SENTINEL) {
276 right->d_back = dir->d_back;
280 if (!dir->d_back) /* no parent. move the right tree to the top. */
282 else if (dir == dir->d_back->d_left) /* we were on the left */
283 dir->d_back->d_left = right;
285 dir->d_back->d_right = right; /* we were on the right */
287 /* re-insert dir on the left tree */
294 /* rotate the tree to the right */
295 static void dir_rightrotate(vol, dir)
299 struct dir *left = dir->d_left;
301 /* whee. move the left's right tree into dir's left tree */
302 dir->d_left = left->d_right;
303 if (left->d_right != SENTINEL)
304 left->d_right->d_back = dir;
306 if (left != SENTINEL) {
307 left->d_back = dir->d_back;
311 if (!dir->d_back) /* no parent. move the left tree to the top. */
313 else if (dir == dir->d_back->d_right) /* we were on the right */
314 dir->d_back->d_right = left;
316 dir->d_back->d_left = left; /* we were on the left */
318 /* re-insert dir on the right tree */
324 /* recolor after a removal */
325 static struct dir *dir_rmrecolor(vol, dir)
331 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
332 /* are we on the left tree? */
333 if (dir == dir->d_back->d_left) {
334 leaf = dir->d_back->d_right; /* get right side */
335 if (leaf->d_color == DIRTREE_COLOR_RED) {
336 /* we're red. we need to change to black. */
337 leaf->d_color = DIRTREE_COLOR_BLACK;
338 dir->d_back->d_color = DIRTREE_COLOR_RED;
339 dir_leftrotate(vol, dir->d_back);
340 leaf = dir->d_back->d_right;
343 /* right leaf has black end nodes */
344 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
345 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
346 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
347 dir = dir->d_back; /* ascend */
349 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
350 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
351 leaf->d_color = DIRTREE_COLOR_RED;
352 dir_rightrotate(vol, leaf);
353 leaf = dir->d_back->d_right;
355 leaf->d_color = dir->d_back->d_color;
356 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
357 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
358 dir_leftrotate(vol, dir->d_back);
361 } else { /* right tree */
362 leaf = dir->d_back->d_left; /* left tree */
363 if (leaf->d_color == DIRTREE_COLOR_RED) {
364 leaf->d_color = DIRTREE_COLOR_BLACK;
365 dir->d_back->d_color = DIRTREE_COLOR_RED;
366 dir_rightrotate(vol, dir->d_back);
367 leaf = dir->d_back->d_left;
370 /* left leaf has black end nodes */
371 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
372 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
373 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
374 dir = dir->d_back; /* ascend */
376 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
377 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
378 leaf->d_color = DIRTREE_COLOR_RED;
379 dir_leftrotate(vol, leaf);
380 leaf = dir->d_back->d_left;
382 leaf->d_color = dir->d_back->d_color;
383 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
384 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
385 dir_rightrotate(vol, dir->d_back);
390 dir->d_color = DIRTREE_COLOR_BLACK;
397 /* remove the node from the tree. this is just like insertion, but
398 * different. actually, it has to worry about a bunch of things that
399 * insertion doesn't care about. */
400 static void dir_remove( vol, dir )
405 struct ofork *of, *last;
406 struct dir *node, *leaf;
407 #endif /* REMOVE_NODES */
409 if (!dir || (dir == SENTINEL))
412 /* i'm not sure if it really helps to delete stuff. */
415 dir->d_m_name = NULL;
416 dir->d_u_name = NULL;
417 #else /* ! REMOVE_NODES */
419 /* go searching for a node with at most one child */
420 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
424 while (node->d_left != SENTINEL)
429 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
432 leaf->d_back = node->d_back;
435 } else if (node == node->d_back->d_left) { /* left tree */
436 node->d_back->d_left = leaf;
438 node->d_back->d_right = leaf;
441 /* we want to free node, but we also want to free the data in dir.
442 * currently, that's d_name and the directory traversal bits.
443 * we just copy the necessary bits and then fix up all the
444 * various pointers to the directory. needless to say, there are
445 * a bunch of places that store the directory struct. */
447 struct dir save, *tmp;
449 memcpy(&save, dir, sizeof(save));
450 memcpy(dir, node, sizeof(struct dir));
452 /* restore the red-black bits */
453 dir->d_left = save.d_left;
454 dir->d_right = save.d_right;
455 dir->d_back = save.d_back;
456 dir->d_color = save.d_color;
458 if (node == vol->v_dir) {/* we may need to fix up this pointer */
460 rootpar.d_child = vol->v_dir;
462 /* if we aren't the root directory, we have parents and
463 * siblings to worry about */
464 if (dir->d_parent->d_child == node)
465 dir->d_parent->d_child = dir;
466 dir->d_next->d_prev = dir;
467 dir->d_prev->d_next = dir;
470 /* fix up children. */
474 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
477 if (node == curdir) /* another pointer to fixup */
480 /* we also need to fix up oforks. bleah */
481 if ((of = dir->d_ofork)) {
482 last = of->of_d_prev;
485 of = (last == of) ? NULL : of->of_d_next;
489 /* set the node's d_name */
490 node->d_m_name = save.d_m_name;
491 node->d_u_name = save.d_u_name;
494 if (node->d_color == DIRTREE_COLOR_BLACK)
495 dir_rmrecolor(vol, leaf);
497 if (node->d_u_name != node->d_m_name) {
498 free(node->d_u_name);
500 free(node->d_m_name);
502 #endif /* ! REMOVE_NODES */
505 /* ---------------------------------------
506 * remove the node and its childs from the tree
508 * FIXME what about opened forks with refs to it?
509 * it's an afp specs violation because you can't delete
510 * an opened forks. Now afpd doesn't care about forks opened by other
511 * process. It's fixable within afpd if fnctl_lock, doable with smb and
512 * next to impossible for nfs and local filesystem access.
514 static void dir_invalidate( vol, dir )
515 const struct vol *vol;
519 /* v_root can't be deleted */
520 if (movecwd(vol, vol->v_root) < 0) {
521 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
525 dirchildremove(dir->d_parent, dir);
526 dir_remove( vol, dir );
529 /* ------------------------------------ */
530 static struct dir *dir_insert(vol, dir)
531 const struct vol *vol;
537 while (pdir->d_did != dir->d_did ) {
538 if ( pdir->d_did > dir->d_did ) {
539 if ( pdir->d_left == SENTINEL ) {
546 if ( pdir->d_right == SENTINEL ) {
551 pdir = pdir->d_right;
559 * attempt to extend the current dir. tree to include path
560 * as a side-effect, movecwd to that point and return the new dir
563 extenddir( vol, dir, path )
569 if ( path->u_name == NULL) {
570 path->u_name = mtoupath(vol, path->m_name, dir->d_did, utf8_encoding() );
574 if ( path->u_name == NULL) {
575 afp_errno = AFPERR_PARAM;
578 if (of_stat( path ) != 0 ) {
582 if (!S_ISDIR(path->st.st_mode)) {
586 if (( dir = adddir( vol, dir, path)) == NULL ) {
591 if ( movecwd( vol, dir ) < 0 ) {
598 /* -------------------
599 system rmdir with afp error code.
600 ENOENT is not an error.
602 static int netatalk_rmdir(const char *name)
604 if (rmdir(name) < 0) {
609 return AFPERR_DIRNEMPT;
612 return AFPERR_ACCESS;
622 /* -------------------------
623 appledouble mkdir afp error code.
625 static int netatalk_mkdir(const char *name)
627 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
630 return( AFPERR_NOOBJ );
632 return( AFPERR_VLOCK );
635 return( AFPERR_ACCESS );
637 return( AFPERR_EXIST );
640 return( AFPERR_DFULL );
642 return( AFPERR_PARAM );
648 /* -------------------
649 system unlink with afp error code.
650 ENOENT is not an error.
652 int netatalk_unlink(const char *name)
654 if (unlink(name) < 0) {
662 return AFPERR_ACCESS;
670 /* ------------------- */
671 static int deletedir(char *dir)
673 char path[MAXPATHLEN + 1];
681 if ((len = strlen(dir)) +2 > sizeof(path))
685 if ((dp = opendir(dir)) == NULL)
691 remain = sizeof(path) -len -1;
692 while ((de = readdir(dp)) && err == AFP_OK) {
693 /* skip this and previous directory */
694 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
697 if (strlen(de->d_name) > remain) {
701 strcpy(path + len, de->d_name);
702 if (stat(path, &st)) {
705 if (S_ISDIR(st.st_mode)) {
706 err = deletedir(path);
708 err = netatalk_unlink(path);
713 /* okay. the directory is empty. delete it. note: we already got rid
716 err = netatalk_rmdir(dir);
721 /* do a recursive copy. */
722 static int copydir(char *src, char *dst, int noadouble)
724 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
734 /* doesn't exist or the path is too long. */
735 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
736 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
737 ((dp = opendir(src)) == NULL))
740 /* try to create the destination directory */
741 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
746 /* set things up to copy */
750 srem = sizeof(spath) - slen -1;
755 drem = sizeof(dpath) - dlen -1;
758 while ((de = readdir(dp))) {
759 /* skip this and previous directory */
760 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
763 if (strlen(de->d_name) > srem) {
767 strcpy(spath + slen, de->d_name);
769 if (stat(spath, &st) == 0) {
770 if (strlen(de->d_name) > drem) {
774 strcpy(dpath + dlen, de->d_name);
776 if (S_ISDIR(st.st_mode)) {
777 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
779 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
783 /* keep the same time stamp. */
784 ut.actime = ut.modtime = st.st_mtime;
790 /* keep the same time stamp. */
791 if (stat(src, &st) == 0) {
792 ut.actime = ut.modtime = st.st_mtime;
802 /* --- public functions follow --- */
804 /* NOTE: we start off with at least one node (the root directory). */
805 struct dir *dirinsert( vol, dir )
811 if ((node = dir_insert(vol, dir)))
814 /* recolor the tree. the current node is red. */
815 dir->d_color = DIRTREE_COLOR_RED;
817 /* parent of this node has to be black. if the parent node
818 * is red, then we have a grandparent. */
819 while ((dir != vol->v_root) &&
820 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
821 /* are we on the left tree? */
822 if (dir->d_back == dir->d_back->d_back->d_left) {
823 node = dir->d_back->d_back->d_right; /* get the right node */
824 if (node->d_color == DIRTREE_COLOR_RED) {
825 /* we're red. we need to change to black. */
826 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
827 node->d_color = DIRTREE_COLOR_BLACK;
828 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
829 dir = dir->d_back->d_back; /* finished. go up. */
831 if (dir == dir->d_back->d_right) {
833 dir_leftrotate(vol, dir);
835 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
836 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
837 dir_rightrotate(vol, dir->d_back->d_back);
840 node = dir->d_back->d_back->d_left;
841 if (node->d_color == DIRTREE_COLOR_RED) {
842 /* we're red. we need to change to black. */
843 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
844 node->d_color = DIRTREE_COLOR_BLACK;
845 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
846 dir = dir->d_back->d_back; /* finished. ascend */
848 if (dir == dir->d_back->d_left) {
850 dir_rightrotate(vol, dir);
852 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
853 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
854 dir_leftrotate(vol, dir->d_back->d_back);
859 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
863 /* free everything down. we don't bother to recolor as this is only
864 * called to free the entire tree */
865 void dirfreename(struct dir *dir)
867 if (dir->d_u_name != dir->d_m_name) {
876 if (!dir || (dir == SENTINEL))
879 if ( dir->d_left != SENTINEL ) {
880 dirfree( dir->d_left );
882 if ( dir->d_right != SENTINEL ) {
883 dirfree( dir->d_right );
886 if (dir != SENTINEL) {
892 /* --------------------------------------------
893 * most of the time mac name and unix name are the same
895 struct dir *dirnew(const char *m_name, const char *u_name)
899 dir = (struct dir *) calloc(1, sizeof( struct dir ));
903 if ((dir->d_m_name = strdup(m_name)) == NULL) {
908 if (m_name == u_name) {
909 dir->d_u_name = dir->d_m_name;
911 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
917 dir->d_left = dir->d_right = SENTINEL;
918 dir->d_next = dir->d_prev = dir;
922 /* -------------------------------------------------- */
928 stat the file or errno
931 curdir: filename parent directory
937 stat the dir or errno
941 curdir: dir parent directory
949 curdir: dir parent directory
956 cname( vol, dir, cpath )
957 const struct vol *vol;
962 static char path[ MAXPATHLEN + 1];
963 static struct path ret;
974 afp_errno = AFPERR_NOOBJ;
975 switch (ret.m_type = *data) { /* path type */
978 len = (unsigned char) *data++;
983 if (afp_version >= 30) {
985 memcpy(&hint, data, sizeof(hint));
987 data += sizeof(hint);
989 memcpy(&len16, data, sizeof(len16));
996 /* else it's an error */
998 afp_errno = AFPERR_PARAM;
1001 *cpath += len + size;
1009 if (movecwd( vol, dir ) < 0 ) {
1011 movecwd failed some of dir path are not there anymore.
1012 FIXME Is it true with other errors?
1013 so we remove dir from the cache
1015 if (dir->d_did == DIRDID_ROOT_PARENT)
1017 if (afp_errno == AFPERR_ACCESS) {
1018 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1021 ret.m_name = dir->d_m_name;
1022 ret.u_name = dir->d_u_name;
1025 } else if (afp_errno == AFPERR_NOOBJ) {
1026 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1029 strcpy(path, dir->d_m_name);
1030 if (dir->d_m_name == dir->d_u_name) {
1034 size_t tp = strlen(path)+1;
1036 strcpy(path +tp, dir->d_u_name);
1037 ret.u_name = path +tp;
1040 dir_invalidate(vol, dir);
1043 dir_invalidate(vol, dir);
1046 if (*path == '\0') {
1051 } /* if (len == 0) */
1053 if (*data == sep ) {
1057 while (*data == sep && len > 0 ) {
1058 if ( dir->d_parent == NULL ) {
1061 dir = dir->d_parent;
1066 /* would this be faster with strlen + strncpy? */
1068 while ( *data != sep && len > 0 ) {
1070 if (p > &path[ MAXPATHLEN]) {
1071 afp_errno = AFPERR_PARAM;
1077 /* short cut bits by chopping off a trailing \0. this also
1078 makes the traversal happy w/ filenames at the end of the
1085 if ( p != path ) { /* we got something */
1087 if (afp_version >= 30) {
1088 /* check for OS X mangled filename :( */
1092 t = demangle_osx(vol, path, dir->d_did, &fileid);
1095 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1096 * flags weren't the same
1098 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1099 /* at last got our view of mac name */
1105 cdir = dir->d_child;
1107 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1110 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1113 if ( cdir == NULL ) {
1115 /* if dir == curdir it always succeed,
1116 even if curdir is deleted.
1117 it's not a pb because it will fail in extenddir
1119 if ( movecwd( vol, dir ) < 0 ) {
1120 /* dir is not valid anymore
1121 we delete dir from the cache and abort.
1123 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1124 afp_errno = AFPERR_NOOBJ;
1127 if (afp_errno == AFPERR_ACCESS)
1129 dir_invalidate(vol, dir);
1132 cdir = extenddir( vol, dir, &ret );
1136 cdir = extenddir( vol, dir, &ret );
1137 } /* if (!extend) */
1139 if ( cdir == NULL ) {
1141 if ( len > 0 || !ret.u_name) {
1149 } /* if (p != path) */
1154 * Move curdir to dir, with a possible chdir()
1156 int movecwd( vol, dir)
1157 const struct vol *vol;
1160 char path[MAXPATHLEN + 1];
1165 if ( dir == curdir ) {
1168 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1169 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1173 p = path + sizeof(path) - 1;
1176 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1179 /* parent directory is deleted */
1180 afp_errno = AFPERR_NOOBJ;
1184 if (p -n -1 < path) {
1185 afp_errno = AFPERR_PARAM;
1192 if ( d != curdir ) {
1193 n = strlen( vol->v_path );
1194 if (p -n -1 < path) {
1195 afp_errno = AFPERR_PARAM;
1200 strncpy( p, vol->v_path, n );
1202 if ( chdir( p ) < 0 ) {
1206 afp_errno = AFPERR_ACCESS;
1209 afp_errno = AFPERR_NOOBJ;
1219 * We can't use unix file's perm to support Apple's inherited protection modes.
1220 * If we aren't the file's owner we can't change its perms when moving it and smb
1221 * nfs,... don't even try.
1223 #define AFP_CHECK_ACCESS
1225 int check_access(char *path, int mode)
1227 #ifdef AFP_CHECK_ACCESS
1235 accessmode(p, &ma, curdir, NULL);
1236 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1238 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1244 /* --------------------- */
1245 int file_access(struct path *path, int mode)
1249 accessmode(path->u_name, &ma, curdir, &path->st);
1250 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1252 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1258 /* ------------------------------
1260 (name, dir) with curdir:name == dir, from afp_enumerate
1263 int getdirparams(const struct vol *vol,
1264 u_int16_t bitmap, struct path *s_path,
1266 char *buf, int *buflen )
1270 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1271 int bit = 0, isad = 0;
1277 struct stat *st = &s_path->st;
1278 char *upath = s_path->u_name;
1280 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1281 (1 << DIRPBIT_CDATE) |
1282 (1 << DIRPBIT_MDATE) |
1283 (1 << DIRPBIT_BDATE) |
1284 (1 << DIRPBIT_FINFO)))) {
1286 ad_init(&ad, vol->v_adouble);
1287 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1288 DIRBITS | 0777, &ad)) {
1293 if ( dir->d_did == DIRDID_ROOT) {
1294 pdid = DIRDID_ROOT_PARENT;
1295 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1298 pdid = dir->d_parent->d_did;
1302 while ( bitmap != 0 ) {
1303 while (( bitmap & 1 ) == 0 ) {
1311 ad_getattr(&ad, &ashort);
1312 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1313 && strcmp(dir->d_u_name, "..")) {
1314 ashort = htons(ATTRBIT_INVISIBLE);
1317 ashort |= htons(ATTRBIT_SHARED);
1318 memcpy( data, &ashort, sizeof( ashort ));
1319 data += sizeof( ashort );
1323 memcpy( data, &pdid, sizeof( pdid ));
1324 data += sizeof( pdid );
1327 case DIRPBIT_CDATE :
1328 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1329 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1330 memcpy( data, &aint, sizeof( aint ));
1331 data += sizeof( aint );
1334 case DIRPBIT_MDATE :
1335 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1336 memcpy( data, &aint, sizeof( aint ));
1337 data += sizeof( aint );
1340 case DIRPBIT_BDATE :
1341 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1342 aint = AD_DATE_START;
1343 memcpy( data, &aint, sizeof( aint ));
1344 data += sizeof( aint );
1347 case DIRPBIT_FINFO :
1349 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1350 } else { /* no appledouble */
1351 memset( data, 0, 32 );
1352 /* set default view -- this also gets done in ad_open() */
1353 ashort = htons(FINDERINFO_CLOSEDVIEW);
1354 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1356 /* dot files are by default invisible */
1357 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1358 strcmp(dir->d_u_name , "..")) {
1359 ashort = htons(FINDERINFO_INVISIBLE);
1360 memcpy(data + FINDERINFO_FRFLAGOFF,
1361 &ashort, sizeof(ashort));
1367 case DIRPBIT_LNAME :
1368 if (dir->d_m_name) /* root of parent can have a null name */
1371 memset(data, 0, sizeof(u_int16_t));
1372 data += sizeof( u_int16_t );
1375 case DIRPBIT_SNAME :
1376 memset(data, 0, sizeof(u_int16_t));
1377 data += sizeof( u_int16_t );
1381 memcpy( data, &dir->d_did, sizeof( aint ));
1382 data += sizeof( aint );
1385 case DIRPBIT_OFFCNT :
1387 /* this needs to handle current directory access rights */
1388 if (st->st_ctime == dir->ctime) {
1389 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1391 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1393 dir->ctime = st->st_ctime;
1394 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1396 ashort = htons( ashort );
1397 memcpy( data, &ashort, sizeof( ashort ));
1398 data += sizeof( ashort );
1402 aint = htonl(st->st_uid);
1403 memcpy( data, &aint, sizeof( aint ));
1404 data += sizeof( aint );
1408 aint = htonl(st->st_gid);
1409 memcpy( data, &aint, sizeof( aint ));
1410 data += sizeof( aint );
1413 case DIRPBIT_ACCESS :
1414 accessmode( upath, &ma, dir , st);
1416 *data++ = ma.ma_user;
1417 *data++ = ma.ma_world;
1418 *data++ = ma.ma_group;
1419 *data++ = ma.ma_owner;
1422 /* Client has requested the ProDOS information block.
1423 Just pass back the same basic block for all
1424 directories. <shirsch@ibm.net> */
1425 case DIRPBIT_PDINFO :
1426 if (afp_version >= 30) { /* UTF8 name */
1427 utf8 = kTextEncodingUTF8;
1428 if (dir->d_m_name) /* root of parent can have a null name */
1431 memset(data, 0, sizeof(u_int16_t));
1432 data += sizeof( u_int16_t );
1434 memcpy(data, &aint, sizeof( aint ));
1435 data += sizeof( aint );
1437 else { /* ProDOS Info Block */
1440 ashort = htons( 0x0200 );
1441 memcpy( data, &ashort, sizeof( ashort ));
1442 data += sizeof( ashort );
1443 memset( data, 0, sizeof( ashort ));
1444 data += sizeof( ashort );
1448 case DIRPBIT_UNIXPR :
1449 aint = htonl(st->st_uid);
1450 memcpy( data, &aint, sizeof( aint ));
1451 data += sizeof( aint );
1452 aint = htonl(st->st_gid);
1453 memcpy( data, &aint, sizeof( aint ));
1454 data += sizeof( aint );
1457 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1458 memcpy( data, &aint, sizeof( aint ));
1459 data += sizeof( aint );
1461 accessmode( upath, &ma, dir , st);
1463 *data++ = ma.ma_user;
1464 *data++ = ma.ma_world;
1465 *data++ = ma.ma_group;
1466 *data++ = ma.ma_owner;
1472 ad_close( &ad, ADFLAGS_HF );
1474 return( AFPERR_BITMAP );
1480 ashort = htons( data - buf );
1481 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1482 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1484 if ( utf_nameoff ) {
1485 ashort = htons( data - buf );
1486 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1487 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1490 ad_close( &ad, ADFLAGS_HF );
1492 *buflen = data - buf;
1496 /* ----------------------------- */
1497 int path_error(struct path *path, int error)
1499 /* - a dir with access error
1500 * - no error it's a file
1503 if (path_isadir(path))
1507 return AFPERR_BADTYPE ;
1510 /* ----------------------------- */
1511 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1514 int ibuflen, *rbuflen;
1519 u_int16_t vid, bitmap;
1525 memcpy( &vid, ibuf, sizeof( vid ));
1526 ibuf += sizeof( vid );
1528 if (NULL == ( vol = getvolbyvid( vid )) ) {
1529 return( AFPERR_PARAM );
1532 if (vol->v_flags & AFPVOL_RO)
1533 return AFPERR_VLOCK;
1535 memcpy( &did, ibuf, sizeof( did ));
1536 ibuf += sizeof( int );
1538 if (NULL == ( dir = dirlookup( vol, did )) ) {
1542 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1543 bitmap = ntohs( bitmap );
1544 ibuf += sizeof( bitmap );
1546 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1547 return get_afp_errno(AFPERR_NOOBJ);
1550 if ( *path->m_name != '\0' ) {
1551 return path_error(path, AFPERR_NOOBJ);
1555 * If ibuf is odd, make it even.
1557 if ((u_long)ibuf & 1 ) {
1561 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1562 setvoltime(obj, vol );
1568 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1570 * assume path == '\0' eg. it's a directory in canonical form
1573 struct path Cur_Path = {
1576 ".", /* unix name */
1577 0, /* stat is not set */
1581 int setdirparams(const struct vol *vol,
1582 struct path *path, u_int16_t bitmap, char *buf )
1590 int bit = 0, aint, isad = 1;
1591 u_int16_t ashort, bshort;
1593 int change_mdate = 0;
1594 int change_parent_mdate = 0;
1597 upath = path->u_name;
1598 ad_init(&ad, vol->v_adouble);
1600 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1601 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1603 * Check to see what we're trying to set. If it's anything
1604 * but ACCESS, UID, or GID, give an error. If it's any of those
1605 * three, we don't need the ad to be open, so just continue.
1607 * note: we also don't need to worry about mdate. also, be quiet
1608 * if we're using the noadouble option.
1610 if (!vol_noadouble(vol) && (bitmap &
1611 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1612 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1613 return AFPERR_ACCESS;
1619 * Check to see if a create was necessary. If it was, we'll want
1620 * to set our name, etc.
1622 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1623 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1624 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1625 ad_getentrylen( &ad, ADEID_NAME ));
1629 while ( bitmap != 0 ) {
1630 while (( bitmap & 1 ) == 0 ) {
1639 memcpy( &ashort, buf, sizeof( ashort ));
1640 ad_getattr(&ad, &bshort);
1641 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1642 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1646 ad_setattr(&ad, bshort);
1647 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1648 change_parent_mdate = 1;
1650 buf += sizeof( ashort );
1653 case DIRPBIT_CDATE :
1656 memcpy(&aint, buf, sizeof(aint));
1657 ad_setdate(&ad, AD_DATE_CREATE, aint);
1659 buf += sizeof( aint );
1662 case DIRPBIT_MDATE :
1663 memcpy(&newdate, buf, sizeof(newdate));
1664 buf += sizeof( newdate );
1667 case DIRPBIT_BDATE :
1670 memcpy(&aint, buf, sizeof(aint));
1671 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1673 buf += sizeof( aint );
1676 case DIRPBIT_FINFO :
1679 * Alright, we admit it, this is *really* sick!
1680 * The 4 bytes that we don't copy, when we're dealing
1681 * with the root of a volume, are the directory's
1682 * location information. This eliminates that annoying
1683 * behavior one sees when mounting above another mount
1687 if ( curdir->d_did == DIRDID_ROOT ) {
1688 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1689 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1691 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1697 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1698 change_parent_mdate = 1;
1699 memcpy( &aint, buf, sizeof(aint));
1700 buf += sizeof( aint );
1701 if ( (curdir->d_did == DIRDID_ROOT) &&
1702 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1706 err = AFPERR_ACCESS;
1707 goto setdirparam_done;
1711 goto setdirparam_done;
1714 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1718 goto setdirparam_done;
1723 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1727 err = AFPERR_ACCESS;
1728 goto setdirparam_done;
1732 goto setdirparam_done;
1735 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1742 change_parent_mdate = 1;
1743 memcpy( &aint, buf, sizeof( aint ));
1744 buf += sizeof( aint );
1745 if (curdir->d_did == DIRDID_ROOT)
1746 setdeskowner( -1, ntohl(aint) );
1748 #if 0 /* don't error if we can't set the desktop owner. */
1752 err = AFPERR_ACCESS;
1753 goto setdirparam_done;
1757 goto setdirparam_done;
1760 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1763 goto setdirparam_done;
1769 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1773 err = AFPERR_ACCESS;
1774 goto setdirparam_done;
1778 goto setdirparam_done;
1781 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1788 case DIRPBIT_ACCESS :
1790 change_parent_mdate = 1;
1791 ma.ma_user = *buf++;
1792 ma.ma_world = *buf++;
1793 ma.ma_group = *buf++;
1794 ma.ma_owner = *buf++;
1796 if (curdir->d_did == DIRDID_ROOT)
1797 setdeskmode(mtoumode( &ma ));
1798 #if 0 /* don't error if we can't set the desktop mode */
1802 err = AFPERR_ACCESS;
1803 goto setdirparam_done;
1806 goto setdirparam_done;
1808 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1812 goto setdirparam_done;
1816 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1817 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1821 err = AFPERR_ACCESS;
1822 goto setdirparam_done;
1825 goto setdirparam_done;
1827 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1830 goto setdirparam_done;
1835 /* Ignore what the client thinks we should do to the
1836 ProDOS information block. Skip over the data and
1837 report nothing amiss. <shirsch@ibm.net> */
1838 case DIRPBIT_PDINFO :
1839 if (afp_version < 30) {
1844 case DIRPBIT_UNIXPR :
1845 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1846 buf += sizeof( aint );
1847 buf += sizeof( aint );
1850 change_parent_mdate = 1;
1851 memcpy( &aint, buf, sizeof( aint ));
1852 buf += sizeof( aint );
1853 aint = ntohl (aint);
1854 if (curdir->d_did == DIRDID_ROOT)
1855 setdeskmode( aint );
1856 #if 0 /* don't error if we can't set the desktop mode */
1860 err = AFPERR_ACCESS;
1861 goto setdirparam_done;
1864 goto setdirparam_done;
1866 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1870 goto setdirparam_done;
1874 if ( setdirunixmode( aint, vol_noadouble(vol),
1875 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1879 err = AFPERR_ACCESS;
1880 goto setdirparam_done;
1883 goto setdirparam_done;
1885 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1888 goto setdirparam_done;
1894 err = AFPERR_BITMAP;
1895 goto setdirparam_done;
1904 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1905 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1909 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1910 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1915 if (path->st_valid && !path->st_errno) {
1916 ad_setid(&ad, &path->st, curdir->d_did, vol->v_stamp);
1919 ad_flush( &ad, ADFLAGS_HF );
1920 ad_close( &ad, ADFLAGS_HF );
1923 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1924 && gettimeofday(&tv, NULL) == 0) {
1925 if (!movecwd(vol, curdir->d_parent)) {
1926 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1927 bitmap = 1<<DIRPBIT_MDATE;
1928 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1929 /* should we reset curdir ?*/
1936 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1939 int ibuflen, *rbuflen;
1945 struct path *s_path;
1953 memcpy( &vid, ibuf, sizeof( vid ));
1954 ibuf += sizeof( vid );
1955 if (NULL == ( vol = getvolbyvid( vid )) ) {
1956 return( AFPERR_PARAM );
1959 if (vol->v_flags & AFPVOL_RO)
1960 return AFPERR_VLOCK;
1962 memcpy( &did, ibuf, sizeof( did ));
1963 ibuf += sizeof( did );
1964 if (NULL == ( dir = dirlookup( vol, did )) ) {
1965 return afp_errno; /* was AFPERR_NOOBJ */
1967 /* for concurrent access we need to be sure we are not in the
1968 * folder we want to create...
1972 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1973 return get_afp_errno(AFPERR_PARAM);
1975 /* cname was able to move curdir to it! */
1976 if (*s_path->m_name == '\0')
1977 return AFPERR_EXIST;
1979 upath = s_path->u_name;
1980 if (0 != (err = check_name(vol, upath))) {
1984 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1988 if (of_stat(s_path) < 0) {
1992 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1996 if ( movecwd( vol, dir ) < 0 ) {
1997 return( AFPERR_PARAM );
2000 ad_init(&ad, vol->v_adouble);
2001 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2002 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2003 if (vol_noadouble(vol))
2004 goto createdir_done;
2005 return( AFPERR_ACCESS );
2008 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
2009 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
2010 ad_getentrylen( &ad, ADEID_NAME ));
2011 ad_setid( &ad, &s_path->st, dir->d_did, vol->v_stamp);
2012 ad_flush( &ad, ADFLAGS_HF );
2013 ad_close( &ad, ADFLAGS_HF );
2016 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2017 *rbuflen = sizeof( u_int32_t );
2018 setvoltime(obj, vol );
2023 * dst new unix filename (not a pathname)
2024 * newname new mac name
2028 int renamedir(src, dst, dir, newparent, newname, noadouble)
2029 char *src, *dst, *newname;
2030 struct dir *dir, *newparent;
2031 const int noadouble;
2038 /* existence check moved to afp_moveandrename */
2039 if ( unix_rename( src, dst ) < 0 ) {
2042 return( AFPERR_NOOBJ );
2044 return( AFPERR_ACCESS );
2046 return AFPERR_VLOCK;
2048 /* tried to move directory into a subdirectory of itself */
2049 return AFPERR_CANTMOVE;
2051 /* this needs to copy and delete. bleah. that means we have
2052 * to deal with entire directory hierarchies. */
2053 if ((err = copydir(src, dst, noadouble)) < 0) {
2057 if ((err = deletedir(src)) < 0)
2061 return( AFPERR_PARAM );
2065 ad_init(&ad, 0); /* FIXME */
2067 len = strlen( newname );
2068 /* rename() succeeded so we need to update our tree even if we can't open
2071 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2072 ad_setentrylen( &ad, ADEID_NAME, len );
2073 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2074 ad_flush( &ad, ADFLAGS_HF );
2075 ad_close( &ad, ADFLAGS_HF );
2078 if (dir->d_m_name == dir->d_u_name)
2079 dir->d_u_name = NULL;
2081 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2082 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2083 /* FIXME : fatal ? */
2086 dir->d_m_name = buf;
2087 strcpy( dir->d_m_name, newname );
2089 if (newname == dst) {
2090 free(dir->d_u_name);
2091 dir->d_u_name = dir->d_m_name;
2094 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2095 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2098 dir->d_u_name = buf;
2099 strcpy( dir->d_u_name, dst );
2102 if (( parent = dir->d_parent ) == NULL ) {
2105 if ( parent == newparent ) {
2109 /* detach from old parent and add to new one. */
2110 dirchildremove(parent, dir);
2111 dir->d_parent = newparent;
2112 dirchildadd(newparent, dir);
2116 #define DOT_APPLEDOUBLE_LEN 13
2117 /* delete an empty directory */
2118 int deletecurdir( vol, path, pathlen )
2119 const struct vol *vol;
2131 if ( curdir->d_parent == NULL ) {
2132 return( AFPERR_ACCESS );
2137 ad_init(&ad, vol->v_adouble);
2138 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2139 DIRBITS | 0777, &ad) == 0 ) {
2141 ad_getattr(&ad, &ashort);
2142 ad_close( &ad, ADFLAGS_HF );
2143 if ((ashort & htons(ATTRBIT_NODELETE))) {
2144 return AFPERR_OLOCK;
2148 /* delete stray .AppleDouble files. this happens to get .Parent files
2150 if ((dp = opendir(".AppleDouble"))) {
2151 strcpy(path, ".AppleDouble/");
2152 while ((de = readdir(dp))) {
2153 /* skip this and previous directory */
2154 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2157 /* bail if the file exists in the current directory.
2158 * note: this will not fail with dangling symlinks */
2159 if (stat(de->d_name, &st) == 0) {
2161 return AFPERR_DIRNEMPT;
2164 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2165 if ((err = netatalk_unlink(path))) {
2173 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2177 /* now get rid of dangling symlinks */
2178 if ((dp = opendir("."))) {
2179 while ((de = readdir(dp))) {
2180 /* skip this and previous directory */
2181 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2184 /* bail if it's not a symlink */
2185 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2187 return AFPERR_DIRNEMPT;
2190 if ((err = netatalk_unlink(de->d_name))) {
2197 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2202 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2203 dirchildremove(curdir, fdir);
2204 cnid_delete(vol->v_cdb, fdir->d_did);
2205 dir_remove( vol, fdir );
2210 /* inode is used as key for cnid.
2211 * Close the descriptor only after cnid_delete
2219 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2222 int ibuflen, *rbuflen;
2232 sfunc = (unsigned char) *ibuf++;
2233 memcpy( &id, ibuf, sizeof( id ));
2238 if (sfunc == 3 || sfunc == 4) {
2239 if (afp_version < 30) {
2240 return( AFPERR_PARAM );
2247 case 3 :/* unicode */
2248 if (( pw = getpwuid( id )) == NULL ) {
2249 return( AFPERR_NOITEM );
2255 case 4 : /* unicode */
2256 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2257 return( AFPERR_NOITEM );
2263 return( AFPERR_PARAM );
2265 len = strlen( name );
2272 u_int16_t tp = htons(len);
2273 memcpy(rbuf, &tp, sizeof(tp));
2282 memcpy( rbuf, name, len );
2288 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2291 int ibuflen, *rbuflen;
2300 sfunc = (unsigned char) *ibuf++;
2304 case 2 : /* unicode */
2305 if (afp_version < 30) {
2306 return( AFPERR_PARAM );
2308 memcpy(&ulen, ibuf, sizeof(ulen));
2314 len = (unsigned char) *ibuf++;
2317 return( AFPERR_PARAM );
2324 case 1 : /* unicode */
2326 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2327 return( AFPERR_NOITEM );
2332 case 2 : /* unicode */
2334 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2335 return( AFPERR_NOITEM );
2344 memcpy( rbuf, &id, sizeof( id ));
2345 *rbuflen = sizeof( id );
2349 /* ------------------------------------
2350 variable DID support
2352 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2355 int ibuflen, *rbuflen;
2366 /* do nothing as dids are static for the life of the process. */
2370 memcpy(&vid, ibuf, sizeof( vid ));
2371 ibuf += sizeof( vid );
2372 if (( vol = getvolbyvid( vid )) == NULL ) {
2373 return( AFPERR_PARAM );
2376 memcpy( &did, ibuf, sizeof( did ));
2377 ibuf += sizeof( did );
2378 if (( dir = dirlookup( vol, did )) == NULL ) {
2379 return( AFPERR_PARAM );
2382 /* dir_remove -- deletedid */
2388 /* did creation gets done automatically
2389 * there's a pb again with case but move it to cname
2391 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2394 int ibuflen, *rbuflen;
2397 struct dir *parentdir;
2405 memcpy(&vid, ibuf, sizeof(vid));
2406 ibuf += sizeof( vid );
2408 if (NULL == ( vol = getvolbyvid( vid )) ) {
2409 return( AFPERR_PARAM );
2412 memcpy(&did, ibuf, sizeof(did));
2413 ibuf += sizeof(did);
2415 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2419 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2420 return get_afp_errno(AFPERR_PARAM);
2423 if ( *path->m_name != '\0' ) {
2424 return path_error(path, AFPERR_NOOBJ);
2427 if ( !path->st_valid && of_stat(path ) < 0 ) {
2428 return( AFPERR_NOOBJ );
2430 if ( path->st_errno ) {
2431 return( AFPERR_NOOBJ );
2434 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2435 *rbuflen = sizeof(curdir->d_did);