2 * $Id: directory.c,v 1.71.2.4.2.12 2004-03-11 16:16:40 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
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 #include <sys/param.h>
41 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/logger.h>
48 #include "directory.h"
61 #define SENTINEL (&sentinel)
62 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
63 NULL, NULL, NULL, NULL, NULL, 0, 0,
65 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
69 /* (from IM: Toolbox Essentials)
70 * dirFinderInfo (DInfo) fields:
72 * frRect 8 folder's window rectangle
74 * frLocation 4 folder's location in window
75 * frView 2 folder's view (default == closedView (256))
77 * extended dirFinderInfo (DXInfo) fields:
78 * frScroll 4 scroll position
79 * frOpenChain: 4 directory ID chain of open folders
80 * frScript: 1 script flag and code
81 * frXFlags: 1 reserved
82 * frComment: 2 comment ID
83 * frPutAway: 4 home directory ID
87 * redid did assignment for directories. now we use red-black trees.
92 const struct vol *vol;
100 afp_errno = AFPERR_PARAM;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
111 afp_errno = AFPERR_NOOBJ;
112 while ( dir != SENTINEL ) {
113 if (dir->d_did == did)
114 return dir->d_m_name ? dir : NULL;
115 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
120 /* ------------------- */
122 int path_isadir(struct path *o_path)
124 return o_path->dir != NULL;
126 return o_path->m_name == '\0' || /* we are in a it */
127 !o_path->st_valid || /* in cache but we can't chdir in it */
128 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
133 int get_afp_errno(const int param)
135 if (afp_errno != AFPERR_DID1)
140 /* ------------------- */
142 dirsearch_byname( cdir, name )
148 if (!strcmp(name, "."))
152 if ( strcmp( dir->d_u_name, name ) == 0 ) {
155 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
160 /* -----------------------------------------
161 * if did is not in the cache resolve it with cnid
164 * OSX call it with bogus id, ie file ID not folder ID,
165 * and we are really bad in this case.
168 dirlookup( vol, did )
169 const struct vol *vol;
175 static char path[MAXPATHLEN + 1];
178 static char buffer[12 + MAXPATHLEN + 1];
179 int buflen = 12 + MAXPATHLEN + 1;
184 ret = dirsearch(vol, did);
185 if (ret != NULL || afp_errno == AFPERR_PARAM)
188 utf8 = utf8_encoding();
189 maxpath = (utf8)?MAXPATHLEN -7:255;
191 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
192 afp_errno = AFPERR_NOOBJ;
195 ptr = path + MAXPATHLEN;
196 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
197 afp_errno = AFPERR_NOOBJ;
201 pathlen = len; /* no 0 in the last part */
203 strcpy(ptr - len, mpath);
206 ret = dirsearch(vol,id);
211 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
213 NULL == (mpath = utompath(vol, upath, cnid, utf8))
215 afp_errno = AFPERR_NOOBJ;
219 len = strlen(mpath) + 1;
221 if (pathlen > maxpath) {
222 afp_errno = AFPERR_PARAM;
225 strcpy(ptr - len, mpath);
229 /* fill the cache, another place where we know about the path type */
235 temp16 = htons(pathlen);
236 memcpy(ptr, &temp16, sizeof(temp16));
238 temp = htonl(kTextEncodingUTF8);
240 memcpy(ptr, &temp, sizeof(temp));
246 *ptr = (unsigned char)pathlen;
250 /* cname is not efficient */
251 if (cname( vol, ret, &ptr ) == NULL )
254 return dirsearch(vol, did);
257 /* --------------------------- */
258 /* rotate the tree to the left */
259 static void dir_leftrotate(vol, dir)
263 struct dir *right = dir->d_right;
265 /* whee. move the right's left tree into dir's right tree */
266 dir->d_right = right->d_left;
267 if (right->d_left != SENTINEL)
268 right->d_left->d_back = dir;
270 if (right != SENTINEL) {
271 right->d_back = dir->d_back;
275 if (!dir->d_back) /* no parent. move the right tree to the top. */
277 else if (dir == dir->d_back->d_left) /* we were on the left */
278 dir->d_back->d_left = right;
280 dir->d_back->d_right = right; /* we were on the right */
282 /* re-insert dir on the left tree */
289 /* rotate the tree to the right */
290 static void dir_rightrotate(vol, dir)
294 struct dir *left = dir->d_left;
296 /* whee. move the left's right tree into dir's left tree */
297 dir->d_left = left->d_right;
298 if (left->d_right != SENTINEL)
299 left->d_right->d_back = dir;
301 if (left != SENTINEL) {
302 left->d_back = dir->d_back;
306 if (!dir->d_back) /* no parent. move the left tree to the top. */
308 else if (dir == dir->d_back->d_right) /* we were on the right */
309 dir->d_back->d_right = left;
311 dir->d_back->d_left = left; /* we were on the left */
313 /* re-insert dir on the right tree */
319 /* recolor after a removal */
320 static struct dir *dir_rmrecolor(vol, dir)
326 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
327 /* are we on the left tree? */
328 if (dir == dir->d_back->d_left) {
329 leaf = dir->d_back->d_right; /* get right side */
330 if (leaf->d_color == DIRTREE_COLOR_RED) {
331 /* we're red. we need to change to black. */
332 leaf->d_color = DIRTREE_COLOR_BLACK;
333 dir->d_back->d_color = DIRTREE_COLOR_RED;
334 dir_leftrotate(vol, dir->d_back);
335 leaf = dir->d_back->d_right;
338 /* right leaf has black end nodes */
339 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
340 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
341 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
342 dir = dir->d_back; /* ascend */
344 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
345 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
346 leaf->d_color = DIRTREE_COLOR_RED;
347 dir_rightrotate(vol, leaf);
348 leaf = dir->d_back->d_right;
350 leaf->d_color = dir->d_back->d_color;
351 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
352 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
353 dir_leftrotate(vol, dir->d_back);
356 } else { /* right tree */
357 leaf = dir->d_back->d_left; /* left tree */
358 if (leaf->d_color == DIRTREE_COLOR_RED) {
359 leaf->d_color = DIRTREE_COLOR_BLACK;
360 dir->d_back->d_color = DIRTREE_COLOR_RED;
361 dir_rightrotate(vol, dir->d_back);
362 leaf = dir->d_back->d_left;
365 /* left leaf has black end nodes */
366 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
367 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
368 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
369 dir = dir->d_back; /* ascend */
371 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
372 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
373 leaf->d_color = DIRTREE_COLOR_RED;
374 dir_leftrotate(vol, leaf);
375 leaf = dir->d_back->d_left;
377 leaf->d_color = dir->d_back->d_color;
378 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
379 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
380 dir_rightrotate(vol, dir->d_back);
385 dir->d_color = DIRTREE_COLOR_BLACK;
392 /* remove the node from the tree. this is just like insertion, but
393 * different. actually, it has to worry about a bunch of things that
394 * insertion doesn't care about. */
395 static void dir_remove( vol, dir )
400 struct ofork *of, *last;
401 struct dir *node, *leaf;
402 #endif /* REMOVE_NODES */
404 if (!dir || (dir == SENTINEL))
407 /* i'm not sure if it really helps to delete stuff. */
410 dir->d_m_name = NULL;
411 dir->d_u_name = NULL;
412 #else /* ! REMOVE_NODES */
414 /* go searching for a node with at most one child */
415 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
419 while (node->d_left != SENTINEL)
424 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
427 leaf->d_back = node->d_back;
430 } else if (node == node->d_back->d_left) { /* left tree */
431 node->d_back->d_left = leaf;
433 node->d_back->d_right = leaf;
436 /* we want to free node, but we also want to free the data in dir.
437 * currently, that's d_name and the directory traversal bits.
438 * we just copy the necessary bits and then fix up all the
439 * various pointers to the directory. needless to say, there are
440 * a bunch of places that store the directory struct. */
442 struct dir save, *tmp;
444 memcpy(&save, dir, sizeof(save));
445 memcpy(dir, node, sizeof(struct dir));
447 /* restore the red-black bits */
448 dir->d_left = save.d_left;
449 dir->d_right = save.d_right;
450 dir->d_back = save.d_back;
451 dir->d_color = save.d_color;
453 if (node == vol->v_dir) {/* we may need to fix up this pointer */
455 rootpar.d_child = vol->v_dir;
457 /* if we aren't the root directory, we have parents and
458 * siblings to worry about */
459 if (dir->d_parent->d_child == node)
460 dir->d_parent->d_child = dir;
461 dir->d_next->d_prev = dir;
462 dir->d_prev->d_next = dir;
465 /* fix up children. */
469 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
472 if (node == curdir) /* another pointer to fixup */
475 /* we also need to fix up oforks. bleah */
476 if ((of = dir->d_ofork)) {
477 last = of->of_d_prev;
480 of = (last == of) ? NULL : of->of_d_next;
484 /* set the node's d_name */
485 node->d_m_name = save.d_m_name;
486 node->d_u_name = save.d_u_name;
489 if (node->d_color == DIRTREE_COLOR_BLACK)
490 dir_rmrecolor(vol, leaf);
492 if (node->d_u_name != node->d_m_name) {
493 free(node->d_u_name);
495 free(node->d_m_name);
497 #endif /* ! REMOVE_NODES */
500 /* ---------------------------------------
501 * remove the node and its childs from the tree
503 * FIXME what about opened forks with refs to it?
504 * it's an afp specs violation because you can't delete
505 * an opened forks. Now afpd doesn't care about forks opened by other
506 * process. It's fixable within afpd if fnctl_lock, doable with smb and
507 * next to impossible for nfs and local filesystem access.
509 static void dir_invalidate( vol, dir )
510 const struct vol *vol;
514 /* v_root can't be deleted */
515 if (movecwd(vol, vol->v_root) < 0) {
516 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
520 dirchildremove(dir->d_parent, dir);
521 dir_remove( vol, dir );
524 /* ------------------------------------ */
525 static struct dir *dir_insert(vol, dir)
526 const struct vol *vol;
532 while (pdir->d_did != dir->d_did ) {
533 if ( pdir->d_did > dir->d_did ) {
534 if ( pdir->d_left == SENTINEL ) {
541 if ( pdir->d_right == SENTINEL ) {
546 pdir = pdir->d_right;
554 * attempt to extend the current dir. tree to include path
555 * as a side-effect, movecwd to that point and return the new dir
558 extenddir( vol, dir, path )
565 if ( path->u_name == NULL) {
566 path->u_name = mtoupath(vol, path->m_name, dir->d_did, (path->m_type==3) );
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 /* FIXME: if this is an AFP3 connection and path->m_type != 3 we might screw dircache here */
583 if ( utf8_encoding() && path->m_type != 3)
585 save_m_name = path->m_name;
587 if (( dir = adddir( vol, dir, path)) == NULL ) {
591 path->m_name = save_m_name;
592 LOG(log_debug, logtype_afpd, "AFP3 connection, mismatch in mtype: %u, unix: %s, mac: %s",
593 path->m_type, path->u_name, path->m_name);
596 if (( dir = adddir( vol, dir, path)) == NULL ) {
602 if ( movecwd( vol, dir ) < 0 ) {
609 /* -------------------
610 system rmdir with afp error code.
611 ENOENT is not an error.
613 static int netatalk_rmdir(const char *name)
615 if (rmdir(name) < 0) {
620 return AFPERR_DIRNEMPT;
623 return AFPERR_ACCESS;
633 /* -------------------------
634 appledouble mkdir afp error code.
636 static int netatalk_mkdir(const char *name)
638 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
641 return( AFPERR_NOOBJ );
643 return( AFPERR_VLOCK );
646 return( AFPERR_ACCESS );
648 return( AFPERR_EXIST );
651 return( AFPERR_DFULL );
653 return( AFPERR_PARAM );
659 /* -------------------
660 system unlink with afp error code.
661 ENOENT is not an error.
663 int netatalk_unlink(const char *name)
665 if (unlink(name) < 0) {
673 return AFPERR_ACCESS;
681 /* ------------------- */
682 static int deletedir(char *dir)
684 char path[MAXPATHLEN + 1];
692 if ((len = strlen(dir)) +2 > sizeof(path))
696 if ((dp = opendir(dir)) == NULL)
702 remain = sizeof(path) -len -1;
703 while ((de = readdir(dp)) && err == AFP_OK) {
704 /* skip this and previous directory */
705 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
708 if (strlen(de->d_name) > remain) {
712 strcpy(path + len, de->d_name);
713 if (stat(path, &st)) {
716 if (S_ISDIR(st.st_mode)) {
717 err = deletedir(path);
719 err = netatalk_unlink(path);
724 /* okay. the directory is empty. delete it. note: we already got rid
727 err = netatalk_rmdir(dir);
732 /* do a recursive copy. */
733 static int copydir(const struct vol *vol, char *src, char *dst)
735 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
744 /* doesn't exist or the path is too long. */
745 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
746 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
747 ((dp = opendir(src)) == NULL))
750 /* try to create the destination directory */
751 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
756 /* set things up to copy */
760 srem = sizeof(spath) - slen -1;
765 drem = sizeof(dpath) - dlen -1;
768 while ((de = readdir(dp))) {
769 /* skip this and previous directory */
770 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
773 if (strlen(de->d_name) > srem) {
777 strcpy(spath + slen, de->d_name);
779 if (stat(spath, &st) == 0) {
780 if (strlen(de->d_name) > drem) {
784 strcpy(dpath + dlen, de->d_name);
786 if (S_ISDIR(st.st_mode)) {
787 if (AFP_OK != (err = copydir(vol, spath, dpath)))
789 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL))) {
793 /* keep the same time stamp. */
794 ut.actime = ut.modtime = st.st_mtime;
800 /* keep the same time stamp. */
801 if (stat(src, &st) == 0) {
802 ut.actime = ut.modtime = st.st_mtime;
812 /* --- public functions follow --- */
814 /* NOTE: we start off with at least one node (the root directory). */
815 struct dir *dirinsert( vol, dir )
821 if ((node = dir_insert(vol, dir)))
824 /* recolor the tree. the current node is red. */
825 dir->d_color = DIRTREE_COLOR_RED;
827 /* parent of this node has to be black. if the parent node
828 * is red, then we have a grandparent. */
829 while ((dir != vol->v_root) &&
830 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
831 /* are we on the left tree? */
832 if (dir->d_back == dir->d_back->d_back->d_left) {
833 node = dir->d_back->d_back->d_right; /* get the right node */
834 if (node->d_color == DIRTREE_COLOR_RED) {
835 /* we're red. we need to change to black. */
836 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
837 node->d_color = DIRTREE_COLOR_BLACK;
838 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
839 dir = dir->d_back->d_back; /* finished. go up. */
841 if (dir == dir->d_back->d_right) {
843 dir_leftrotate(vol, dir);
845 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
846 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
847 dir_rightrotate(vol, dir->d_back->d_back);
850 node = dir->d_back->d_back->d_left;
851 if (node->d_color == DIRTREE_COLOR_RED) {
852 /* we're red. we need to change to black. */
853 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
854 node->d_color = DIRTREE_COLOR_BLACK;
855 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
856 dir = dir->d_back->d_back; /* finished. ascend */
858 if (dir == dir->d_back->d_left) {
860 dir_rightrotate(vol, dir);
862 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
863 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
864 dir_leftrotate(vol, dir->d_back->d_back);
869 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
873 /* free everything down. we don't bother to recolor as this is only
874 * called to free the entire tree */
875 void dirfreename(struct dir *dir)
877 if (dir->d_u_name != dir->d_m_name) {
886 if (!dir || (dir == SENTINEL))
889 if ( dir->d_left != SENTINEL ) {
890 dirfree( dir->d_left );
892 if ( dir->d_right != SENTINEL ) {
893 dirfree( dir->d_right );
896 if (dir != SENTINEL) {
902 /* --------------------------------------------
903 * most of the time mac name and unix name are the same
905 struct dir *dirnew(const char *m_name, const char *u_name)
909 dir = (struct dir *) calloc(1, sizeof( struct dir ));
913 if ((dir->d_m_name = strdup(m_name)) == NULL) {
918 if (m_name == u_name) {
919 dir->d_u_name = dir->d_m_name;
921 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
927 dir->d_left = dir->d_right = SENTINEL;
928 dir->d_next = dir->d_prev = dir;
932 /* -------------------------------------------------- */
938 stat the file or errno
941 curdir: filename parent directory
947 stat the dir or errno
951 curdir: dir parent directory
959 curdir: dir parent directory
966 cname( vol, dir, cpath )
967 const struct vol *vol;
972 static char path[ MAXPATHLEN + 1];
973 static struct path ret;
984 afp_errno = AFPERR_NOOBJ;
985 switch (ret.m_type = *data) { /* path type */
988 len = (unsigned char) *data++;
993 if (afp_version >= 30) {
995 memcpy(&hint, data, sizeof(hint));
997 data += sizeof(hint);
999 memcpy(&len16, data, sizeof(len16));
1006 /* else it's an error */
1008 afp_errno = AFPERR_PARAM;
1011 *cpath += len + size;
1019 if (movecwd( vol, dir ) < 0 ) {
1021 movecwd failed some of dir path are not there anymore.
1022 FIXME Is it true with other errors?
1023 so we remove dir from the cache
1025 if (dir->d_did == DIRDID_ROOT_PARENT)
1027 if (afp_errno == AFPERR_ACCESS) {
1028 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1031 /* FIXME should we set, don't need to call stat() after:
1033 ret.st_errno = EACCES;
1035 ret.m_name = dir->d_m_name;
1036 ret.u_name = dir->d_u_name;
1039 } else if (afp_errno == AFPERR_NOOBJ) {
1040 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1043 strcpy(path, dir->d_m_name);
1044 if (dir->d_m_name == dir->d_u_name) {
1048 size_t tp = strlen(path)+1;
1050 strcpy(path +tp, dir->d_u_name);
1051 ret.u_name = path +tp;
1054 /* FIXME should we set :
1056 ret.st_errno = ENOENT;
1059 dir_invalidate(vol, dir);
1062 dir_invalidate(vol, dir);
1065 if (*path == '\0') {
1070 } /* if (len == 0) */
1072 if (*data == sep ) {
1076 while (*data == sep && len > 0 ) {
1077 if ( dir->d_parent == NULL ) {
1080 dir = dir->d_parent;
1085 /* would this be faster with strlen + strncpy? */
1087 while ( *data != sep && len > 0 ) {
1089 if (p > &path[ MAXPATHLEN]) {
1090 afp_errno = AFPERR_PARAM;
1096 /* short cut bits by chopping off a trailing \0. this also
1097 makes the traversal happy w/ filenames at the end of the
1104 if ( p != path ) { /* we got something */
1106 if (afp_version >= 30) {
1107 /* check for OS X mangled filename :( */
1111 t = demangle_osx(vol, path, dir->d_did, &fileid);
1114 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1115 * flags weren't the same
1117 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1118 /* at last got our view of mac name */
1124 cdir = dir->d_child;
1126 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1129 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1132 if ( cdir == NULL ) {
1134 /* if dir == curdir it always succeed,
1135 even if curdir is deleted.
1136 it's not a pb because it will fail in extenddir
1138 if ( movecwd( vol, dir ) < 0 ) {
1139 /* dir is not valid anymore
1140 we delete dir from the cache and abort.
1142 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1143 afp_errno = AFPERR_NOOBJ;
1146 if (afp_errno == AFPERR_ACCESS)
1148 dir_invalidate(vol, dir);
1151 cdir = extenddir( vol, dir, &ret );
1155 cdir = extenddir( vol, dir, &ret );
1156 } /* if (!extend) */
1158 if ( cdir == NULL ) {
1160 if ( len > 0 || !ret.u_name) {
1168 } /* if (p != path) */
1173 * Move curdir to dir, with a possible chdir()
1175 int movecwd( vol, dir)
1176 const struct vol *vol;
1179 char path[MAXPATHLEN + 1];
1184 if ( dir == curdir ) {
1187 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1188 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1192 p = path + sizeof(path) - 1;
1195 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1198 /* parent directory is deleted */
1199 afp_errno = AFPERR_NOOBJ;
1203 if (p -n -1 < path) {
1204 afp_errno = AFPERR_PARAM;
1211 if ( d != curdir ) {
1212 n = strlen( vol->v_path );
1213 if (p -n -1 < path) {
1214 afp_errno = AFPERR_PARAM;
1219 memcpy( p, vol->v_path, n );
1221 if ( chdir( p ) < 0 ) {
1225 afp_errno = AFPERR_ACCESS;
1228 afp_errno = AFPERR_NOOBJ;
1238 * We can't use unix file's perm to support Apple's inherited protection modes.
1239 * If we aren't the file's owner we can't change its perms when moving it and smb
1240 * nfs,... don't even try.
1242 #define AFP_CHECK_ACCESS
1244 int check_access(char *path, int mode)
1246 #ifdef AFP_CHECK_ACCESS
1254 accessmode(p, &ma, curdir, NULL);
1255 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1257 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1263 /* --------------------- */
1264 int file_access(struct path *path, int mode)
1268 accessmode(path->u_name, &ma, curdir, &path->st);
1269 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1271 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1277 /* ------------------------------
1279 (name, dir) with curdir:name == dir, from afp_enumerate
1282 int getdirparams(const struct vol *vol,
1283 u_int16_t bitmap, struct path *s_path,
1285 char *buf, int *buflen )
1289 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1290 int bit = 0, isad = 0;
1296 struct stat *st = &s_path->st;
1297 char *upath = s_path->u_name;
1299 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1300 (1 << DIRPBIT_CDATE) |
1301 (1 << DIRPBIT_MDATE) |
1302 (1 << DIRPBIT_BDATE) |
1303 (1 << DIRPBIT_FINFO)))) {
1305 ad_init(&ad, vol->v_adouble);
1306 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1307 DIRBITS | 0777, &ad)) {
1312 if ( dir->d_did == DIRDID_ROOT) {
1313 pdid = DIRDID_ROOT_PARENT;
1314 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1317 pdid = dir->d_parent->d_did;
1321 while ( bitmap != 0 ) {
1322 while (( bitmap & 1 ) == 0 ) {
1330 ad_getattr(&ad, &ashort);
1331 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1332 && strcmp(dir->d_u_name, "..")) {
1333 ashort = htons(ATTRBIT_INVISIBLE);
1336 ashort |= htons(ATTRBIT_SHARED);
1337 memcpy( data, &ashort, sizeof( ashort ));
1338 data += sizeof( ashort );
1342 memcpy( data, &pdid, sizeof( pdid ));
1343 data += sizeof( pdid );
1346 case DIRPBIT_CDATE :
1347 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1348 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1349 memcpy( data, &aint, sizeof( aint ));
1350 data += sizeof( aint );
1353 case DIRPBIT_MDATE :
1354 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1355 memcpy( data, &aint, sizeof( aint ));
1356 data += sizeof( aint );
1359 case DIRPBIT_BDATE :
1360 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1361 aint = AD_DATE_START;
1362 memcpy( data, &aint, sizeof( aint ));
1363 data += sizeof( aint );
1366 case DIRPBIT_FINFO :
1368 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1369 } else { /* no appledouble */
1370 memset( data, 0, 32 );
1371 /* set default view -- this also gets done in ad_open() */
1372 ashort = htons(FINDERINFO_CLOSEDVIEW);
1373 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1375 /* dot files are by default invisible */
1376 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1377 strcmp(dir->d_u_name , "..")) {
1378 ashort = htons(FINDERINFO_INVISIBLE);
1379 memcpy(data + FINDERINFO_FRFLAGOFF,
1380 &ashort, sizeof(ashort));
1386 case DIRPBIT_LNAME :
1387 if (dir->d_m_name) /* root of parent can have a null name */
1390 memset(data, 0, sizeof(u_int16_t));
1391 data += sizeof( u_int16_t );
1394 case DIRPBIT_SNAME :
1395 memset(data, 0, sizeof(u_int16_t));
1396 data += sizeof( u_int16_t );
1400 memcpy( data, &dir->d_did, sizeof( aint ));
1401 data += sizeof( aint );
1404 case DIRPBIT_OFFCNT :
1406 /* this needs to handle current directory access rights */
1407 if (st->st_ctime == dir->ctime) {
1408 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1410 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1412 dir->ctime = st->st_ctime;
1413 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1415 ashort = htons( ashort );
1416 memcpy( data, &ashort, sizeof( ashort ));
1417 data += sizeof( ashort );
1421 aint = htonl(st->st_uid);
1422 memcpy( data, &aint, sizeof( aint ));
1423 data += sizeof( aint );
1427 aint = htonl(st->st_gid);
1428 memcpy( data, &aint, sizeof( aint ));
1429 data += sizeof( aint );
1432 case DIRPBIT_ACCESS :
1433 accessmode( upath, &ma, dir , st);
1435 *data++ = ma.ma_user;
1436 *data++ = ma.ma_world;
1437 *data++ = ma.ma_group;
1438 *data++ = ma.ma_owner;
1441 /* Client has requested the ProDOS information block.
1442 Just pass back the same basic block for all
1443 directories. <shirsch@ibm.net> */
1444 case DIRPBIT_PDINFO :
1445 if (afp_version >= 30) { /* UTF8 name */
1446 utf8 = kTextEncodingUTF8;
1447 if (dir->d_m_name) /* root of parent can have a null name */
1450 memset(data, 0, sizeof(u_int16_t));
1451 data += sizeof( u_int16_t );
1453 memcpy(data, &aint, sizeof( aint ));
1454 data += sizeof( aint );
1456 else { /* ProDOS Info Block */
1459 ashort = htons( 0x0200 );
1460 memcpy( data, &ashort, sizeof( ashort ));
1461 data += sizeof( ashort );
1462 memset( data, 0, sizeof( ashort ));
1463 data += sizeof( ashort );
1467 case DIRPBIT_UNIXPR :
1468 aint = htonl(st->st_uid);
1469 memcpy( data, &aint, sizeof( aint ));
1470 data += sizeof( aint );
1471 aint = htonl(st->st_gid);
1472 memcpy( data, &aint, sizeof( aint ));
1473 data += sizeof( aint );
1476 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1477 memcpy( data, &aint, sizeof( aint ));
1478 data += sizeof( aint );
1480 accessmode( upath, &ma, dir , st);
1482 *data++ = ma.ma_user;
1483 *data++ = ma.ma_world;
1484 *data++ = ma.ma_group;
1485 *data++ = ma.ma_owner;
1491 ad_close( &ad, ADFLAGS_HF );
1493 return( AFPERR_BITMAP );
1499 ashort = htons( data - buf );
1500 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1501 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1503 if ( utf_nameoff ) {
1504 ashort = htons( data - buf );
1505 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1506 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1509 ad_close( &ad, ADFLAGS_HF );
1511 *buflen = data - buf;
1515 /* ----------------------------- */
1516 int path_error(struct path *path, int error)
1518 /* - a dir with access error
1519 * - no error it's a file
1522 if (path_isadir(path))
1526 return AFPERR_BADTYPE ;
1529 /* ----------------------------- */
1530 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1533 int ibuflen, *rbuflen;
1538 u_int16_t vid, bitmap;
1544 memcpy( &vid, ibuf, sizeof( vid ));
1545 ibuf += sizeof( vid );
1547 if (NULL == ( vol = getvolbyvid( vid )) ) {
1548 return( AFPERR_PARAM );
1551 if (vol->v_flags & AFPVOL_RO)
1552 return AFPERR_VLOCK;
1554 memcpy( &did, ibuf, sizeof( did ));
1555 ibuf += sizeof( int );
1557 if (NULL == ( dir = dirlookup( vol, did )) ) {
1561 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1562 bitmap = ntohs( bitmap );
1563 ibuf += sizeof( bitmap );
1565 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1566 return get_afp_errno(AFPERR_NOOBJ);
1569 if ( *path->m_name != '\0' ) {
1570 return path_error(path, AFPERR_NOOBJ);
1574 * If ibuf is odd, make it even.
1576 if ((u_long)ibuf & 1 ) {
1580 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1581 setvoltime(obj, vol );
1587 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1589 * assume path == '\0' eg. it's a directory in canonical form
1592 struct path Cur_Path = {
1595 ".", /* unix name */
1596 0, /* stat is not set */
1600 int setdirparams(const struct vol *vol,
1601 struct path *path, u_int16_t bitmap, char *buf )
1609 int bit = 0, aint, isad = 1;
1610 u_int16_t ashort, bshort;
1612 int change_mdate = 0;
1613 int change_parent_mdate = 0;
1616 upath = path->u_name;
1617 ad_init(&ad, vol->v_adouble);
1619 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1620 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1622 * Check to see what we're trying to set. If it's anything
1623 * but ACCESS, UID, or GID, give an error. If it's any of those
1624 * three, we don't need the ad to be open, so just continue.
1626 * note: we also don't need to worry about mdate. also, be quiet
1627 * if we're using the noadouble option.
1629 if (!vol_noadouble(vol) && (bitmap &
1630 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1631 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1632 return AFPERR_ACCESS;
1638 * Check to see if a create was necessary. If it was, we'll want
1639 * to set our name, etc.
1641 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1642 ad_setname(&ad, curdir->d_m_name);
1646 while ( bitmap != 0 ) {
1647 while (( bitmap & 1 ) == 0 ) {
1656 memcpy( &ashort, buf, sizeof( ashort ));
1657 ad_getattr(&ad, &bshort);
1658 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1659 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1663 ad_setattr(&ad, bshort);
1664 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1665 change_parent_mdate = 1;
1667 buf += sizeof( ashort );
1670 case DIRPBIT_CDATE :
1673 memcpy(&aint, buf, sizeof(aint));
1674 ad_setdate(&ad, AD_DATE_CREATE, aint);
1676 buf += sizeof( aint );
1679 case DIRPBIT_MDATE :
1680 memcpy(&newdate, buf, sizeof(newdate));
1681 buf += sizeof( newdate );
1684 case DIRPBIT_BDATE :
1687 memcpy(&aint, buf, sizeof(aint));
1688 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1690 buf += sizeof( aint );
1693 case DIRPBIT_FINFO :
1696 * Alright, we admit it, this is *really* sick!
1697 * The 4 bytes that we don't copy, when we're dealing
1698 * with the root of a volume, are the directory's
1699 * location information. This eliminates that annoying
1700 * behavior one sees when mounting above another mount
1704 if ( curdir->d_did == DIRDID_ROOT ) {
1705 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1706 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1708 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1714 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1715 change_parent_mdate = 1;
1716 memcpy( &aint, buf, sizeof(aint));
1717 buf += sizeof( aint );
1718 if ( (curdir->d_did == DIRDID_ROOT) &&
1719 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1723 err = AFPERR_ACCESS;
1724 goto setdirparam_done;
1728 goto setdirparam_done;
1731 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1735 goto setdirparam_done;
1740 if ( setdirowner(vol, ntohl(aint), -1 ) < 0 ) {
1744 err = AFPERR_ACCESS;
1745 goto setdirparam_done;
1749 goto setdirparam_done;
1752 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1759 change_parent_mdate = 1;
1760 memcpy( &aint, buf, sizeof( aint ));
1761 buf += sizeof( aint );
1762 if (curdir->d_did == DIRDID_ROOT)
1763 setdeskowner( -1, ntohl(aint) );
1765 #if 0 /* don't error if we can't set the desktop owner. */
1769 err = AFPERR_ACCESS;
1770 goto setdirparam_done;
1774 goto setdirparam_done;
1777 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1780 goto setdirparam_done;
1786 if ( setdirowner(vol, -1, ntohl(aint) ) < 0 ) {
1790 err = AFPERR_ACCESS;
1791 goto setdirparam_done;
1795 goto setdirparam_done;
1798 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1805 case DIRPBIT_ACCESS :
1807 change_parent_mdate = 1;
1808 ma.ma_user = *buf++;
1809 ma.ma_world = *buf++;
1810 ma.ma_group = *buf++;
1811 ma.ma_owner = *buf++;
1813 if (curdir->d_did == DIRDID_ROOT)
1814 setdeskmode(mtoumode( &ma ));
1815 #if 0 /* don't error if we can't set the desktop mode */
1819 err = AFPERR_ACCESS;
1820 goto setdirparam_done;
1823 goto setdirparam_done;
1825 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1829 goto setdirparam_done;
1833 if ( setdirmode( vol, mtoumode( &ma )) < 0 ) {
1837 err = AFPERR_ACCESS;
1838 goto setdirparam_done;
1841 goto setdirparam_done;
1843 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1846 goto setdirparam_done;
1851 /* Ignore what the client thinks we should do to the
1852 ProDOS information block. Skip over the data and
1853 report nothing amiss. <shirsch@ibm.net> */
1854 case DIRPBIT_PDINFO :
1855 if (afp_version < 30) {
1860 case DIRPBIT_UNIXPR :
1861 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1862 buf += sizeof( aint );
1863 buf += sizeof( aint );
1866 change_parent_mdate = 1;
1867 memcpy( &aint, buf, sizeof( aint ));
1868 buf += sizeof( aint );
1869 aint = ntohl (aint);
1870 if (curdir->d_did == DIRDID_ROOT)
1871 setdeskmode( aint );
1872 #if 0 /* don't error if we can't set the desktop mode */
1876 err = AFPERR_ACCESS;
1877 goto setdirparam_done;
1880 goto setdirparam_done;
1882 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1886 goto setdirparam_done;
1890 if ( setdirunixmode(vol, aint) < 0 ) {
1894 err = AFPERR_ACCESS;
1895 goto setdirparam_done;
1898 goto setdirparam_done;
1900 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1903 goto setdirparam_done;
1909 err = AFPERR_BITMAP;
1910 goto setdirparam_done;
1919 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1920 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1924 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1925 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1930 if (path->st_valid && !path->st_errno) {
1931 struct stat *st = &path->st;
1933 if (curdir->d_parent) {
1934 ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev,
1935 st->st_ino, curdir->d_did, curdir->d_parent->d_did, vol->v_stamp);
1939 ad_flush( &ad, ADFLAGS_HF );
1940 ad_close( &ad, ADFLAGS_HF );
1943 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1944 && gettimeofday(&tv, NULL) == 0) {
1945 if (!movecwd(vol, curdir->d_parent)) {
1946 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1947 bitmap = 1<<DIRPBIT_MDATE;
1948 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1949 /* should we reset curdir ?*/
1956 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1959 int ibuflen, *rbuflen;
1965 struct path *s_path;
1973 memcpy( &vid, ibuf, sizeof( vid ));
1974 ibuf += sizeof( vid );
1975 if (NULL == ( vol = getvolbyvid( vid )) ) {
1976 return( AFPERR_PARAM );
1979 if (vol->v_flags & AFPVOL_RO)
1980 return AFPERR_VLOCK;
1982 memcpy( &did, ibuf, sizeof( did ));
1983 ibuf += sizeof( did );
1984 if (NULL == ( dir = dirlookup( vol, did )) ) {
1985 return afp_errno; /* was AFPERR_NOOBJ */
1987 /* for concurrent access we need to be sure we are not in the
1988 * folder we want to create...
1992 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1993 return get_afp_errno(AFPERR_PARAM);
1995 /* cname was able to move curdir to it! */
1996 if (*s_path->m_name == '\0')
1997 return AFPERR_EXIST;
1999 upath = s_path->u_name;
2000 if (0 != (err = check_name(vol, upath))) {
2004 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2008 if (of_stat(s_path) < 0) {
2012 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2016 if ( movecwd( vol, dir ) < 0 ) {
2017 return( AFPERR_PARAM );
2020 ad_init(&ad, vol->v_adouble);
2021 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2022 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2023 if (vol_noadouble(vol))
2024 goto createdir_done;
2025 return( AFPERR_ACCESS );
2027 ad_setname(&ad, s_path->m_name);
2028 ad_setid( &ad, (vol->v_flags & AFPVOL_NODEV)?0:s_path->st.st_dev,
2029 s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2031 ad_flush( &ad, ADFLAGS_HF );
2032 ad_close( &ad, ADFLAGS_HF );
2035 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2036 *rbuflen = sizeof( u_int32_t );
2037 setvoltime(obj, vol );
2042 * dst new unix filename (not a pathname)
2043 * newname new mac name
2047 int renamedir(vol, src, dst, dir, newparent, newname)
2048 const struct vol *vol;
2049 char *src, *dst, *newname;
2050 struct dir *dir, *newparent;
2057 /* existence check moved to afp_moveandrename */
2058 if ( unix_rename( src, dst ) < 0 ) {
2061 return( AFPERR_NOOBJ );
2063 return( AFPERR_ACCESS );
2065 return AFPERR_VLOCK;
2067 /* tried to move directory into a subdirectory of itself */
2068 return AFPERR_CANTMOVE;
2070 /* this needs to copy and delete. bleah. that means we have
2071 * to deal with entire directory hierarchies. */
2072 if ((err = copydir(vol, src, dst)) < 0) {
2076 if ((err = deletedir(src)) < 0)
2080 return( AFPERR_PARAM );
2084 if (vol->v_adouble == AD_VERSION2_OSX) {
2085 /* We simply move the corresponding ad file as well */
2086 char tempbuf[258]="._";
2087 rename(vol->ad_path(src,0),strcat(tempbuf,dst));
2090 len = strlen( newname );
2091 /* rename() succeeded so we need to update our tree even if we can't open
2095 ad_init(&ad, vol->v_adouble);
2097 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2098 ad_setname(&ad, newname);
2099 ad_flush( &ad, ADFLAGS_HF );
2100 ad_close( &ad, ADFLAGS_HF );
2103 if (dir->d_m_name == dir->d_u_name)
2104 dir->d_u_name = NULL;
2106 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2107 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2108 /* FIXME : fatal ? */
2111 dir->d_m_name = buf;
2112 strcpy( dir->d_m_name, newname );
2114 if (newname == dst) {
2115 free(dir->d_u_name);
2116 dir->d_u_name = dir->d_m_name;
2119 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2120 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2123 dir->d_u_name = buf;
2124 strcpy( dir->d_u_name, dst );
2127 if (( parent = dir->d_parent ) == NULL ) {
2130 if ( parent == newparent ) {
2134 /* detach from old parent and add to new one. */
2135 dirchildremove(parent, dir);
2136 dir->d_parent = newparent;
2137 dirchildadd(newparent, dir);
2141 #define DOT_APPLEDOUBLE_LEN 13
2142 /* delete an empty directory */
2143 int deletecurdir( vol, path, pathlen )
2144 const struct vol *vol;
2156 if ( curdir->d_parent == NULL ) {
2157 return( AFPERR_ACCESS );
2162 ad_init(&ad, vol->v_adouble);
2163 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2164 DIRBITS | 0777, &ad) == 0 ) {
2166 ad_getattr(&ad, &ashort);
2167 ad_close( &ad, ADFLAGS_HF );
2168 if ((ashort & htons(ATTRBIT_NODELETE))) {
2169 return AFPERR_OLOCK;
2173 if (vol->v_adouble == AD_VERSION2_OSX) {
2175 if ((err = netatalk_unlink(vol->ad_path(".",0) )) ) {
2180 /* delete stray .AppleDouble files. this happens to get .Parent files
2182 if ((dp = opendir(".AppleDouble"))) {
2183 strcpy(path, ".AppleDouble/");
2184 while ((de = readdir(dp))) {
2185 /* skip this and previous directory */
2186 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2189 /* bail if the file exists in the current directory.
2190 * note: this will not fail with dangling symlinks */
2191 if (stat(de->d_name, &st) == 0) {
2193 return AFPERR_DIRNEMPT;
2196 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2197 if ((err = netatalk_unlink(path))) {
2205 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2210 /* now get rid of dangling symlinks */
2211 if ((dp = opendir("."))) {
2212 while ((de = readdir(dp))) {
2213 /* skip this and previous directory */
2214 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2217 /* bail if it's not a symlink */
2218 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2220 return AFPERR_DIRNEMPT;
2223 if ((err = netatalk_unlink(de->d_name))) {
2230 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2235 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2236 dirchildremove(curdir, fdir);
2237 cnid_delete(vol->v_cdb, fdir->d_did);
2238 dir_remove( vol, fdir );
2243 /* inode is used as key for cnid.
2244 * Close the descriptor only after cnid_delete
2252 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2255 int ibuflen, *rbuflen;
2265 sfunc = (unsigned char) *ibuf++;
2266 memcpy( &id, ibuf, sizeof( id ));
2271 if (sfunc == 3 || sfunc == 4) {
2272 if (afp_version < 30) {
2273 return( AFPERR_PARAM );
2280 case 3 :/* unicode */
2281 if (( pw = getpwuid( id )) == NULL ) {
2282 return( AFPERR_NOITEM );
2284 len = convert_string_allocate( obj->options.unixcharset, ((sfunc == 1)?obj->options.maccharset:CH_UTF8_MAC),
2285 pw->pw_name, strlen(pw->pw_name), &name);
2289 case 4 : /* unicode */
2290 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2291 return( AFPERR_NOITEM );
2293 len = convert_string_allocate( obj->options.unixcharset, (sfunc == 1)?obj->options.maccharset:CH_UTF8_MAC,
2294 gr->gr_name, strlen(gr->gr_name), &name);
2298 return( AFPERR_PARAM );
2300 len = strlen( name );
2307 u_int16_t tp = htons(len);
2308 memcpy(rbuf, &tp, sizeof(tp));
2317 memcpy( rbuf, name, len );
2325 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2328 int ibuflen, *rbuflen;
2337 sfunc = (unsigned char) *ibuf++;
2341 case 2 : /* unicode */
2342 if (afp_version < 30) {
2343 return( AFPERR_PARAM );
2345 memcpy(&ulen, ibuf, sizeof(ulen));
2351 len = (unsigned char) *ibuf++;
2354 return( AFPERR_PARAM );
2361 case 1 : /* unicode */
2363 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2364 return( AFPERR_NOITEM );
2369 case 2 : /* unicode */
2371 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2372 return( AFPERR_NOITEM );
2381 memcpy( rbuf, &id, sizeof( id ));
2382 *rbuflen = sizeof( id );
2386 /* ------------------------------------
2387 variable DID support
2389 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2392 int ibuflen, *rbuflen;
2403 /* do nothing as dids are static for the life of the process. */
2407 memcpy(&vid, ibuf, sizeof( vid ));
2408 ibuf += sizeof( vid );
2409 if (( vol = getvolbyvid( vid )) == NULL ) {
2410 return( AFPERR_PARAM );
2413 memcpy( &did, ibuf, sizeof( did ));
2414 ibuf += sizeof( did );
2415 if (( dir = dirlookup( vol, did )) == NULL ) {
2416 return( AFPERR_PARAM );
2419 /* dir_remove -- deletedid */
2425 /* did creation gets done automatically
2426 * there's a pb again with case but move it to cname
2428 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2431 int ibuflen, *rbuflen;
2434 struct dir *parentdir;
2442 memcpy(&vid, ibuf, sizeof(vid));
2443 ibuf += sizeof( vid );
2445 if (NULL == ( vol = getvolbyvid( vid )) ) {
2446 return( AFPERR_PARAM );
2449 memcpy(&did, ibuf, sizeof(did));
2450 ibuf += sizeof(did);
2452 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2456 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2457 return get_afp_errno(AFPERR_PARAM);
2460 if ( *path->m_name != '\0' ) {
2461 return path_error(path, AFPERR_NOOBJ);
2464 if ( !path->st_valid && of_stat(path ) < 0 ) {
2465 return( AFPERR_NOOBJ );
2467 if ( path->st_errno ) {
2468 return( AFPERR_NOOBJ );
2471 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2472 *rbuflen = sizeof(curdir->d_did);