2 * $Id: directory.c,v 1.71.2.4.2.2 2003-09-28 13:58:56 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>
25 #include <atalk/cnid.h>
32 #endif /* HAVE_FCNTL_H */
39 #else /* STDC_HEADERS */
43 #endif /* HAVE_STRCHR */
44 char *strchr (), *strrchr ();
46 #define memcpy(d,s,n) bcopy ((s), (d), (n))
47 #define memmove(d,s,n) bcopy ((s), (d), (n))
48 #endif /* ! HAVE_MEMCPY */
49 #endif /* STDC_HEADERS */
51 #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;
179 static char path[MAXPATHLEN + 1];
182 static char buffer[12 + MAXPATHLEN + 1];
183 int buflen = 12 + MAXPATHLEN + 1;
188 ret = dirsearch(vol, did);
189 if (ret != NULL || afp_errno == AFPERR_PARAM)
192 utf8 = utf8_encoding();
193 maxpath = (utf8)?MAXPATHLEN -7:255;
195 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
196 afp_errno = AFPERR_NOOBJ;
199 ptr = path + MAXPATHLEN;
200 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
201 afp_errno = AFPERR_NOOBJ;
205 pathlen = len; /* no 0 in the last part */
207 strcpy(ptr - len, mpath);
210 ret = dirsearch(vol,id);
215 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
217 NULL == (mpath = utompath(vol, upath, cnid, 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 )
568 if ( path->u_name == NULL) {
569 path->u_name = mtoupath(vol, path->m_name, dir->d_did, utf8_encoding() );
573 if ( path->u_name == NULL) {
574 afp_errno = AFPERR_PARAM;
577 if (of_stat( path ) != 0 ) {
581 if (!S_ISDIR(path->st.st_mode)) {
585 if (( dir = adddir( vol, dir, path)) == NULL ) {
590 if ( movecwd( vol, dir ) < 0 ) {
597 /* -------------------
598 system rmdir with afp error code.
599 ENOENT is not an error.
601 static int netatalk_rmdir(const char *name)
603 if (rmdir(name) < 0) {
608 return AFPERR_DIRNEMPT;
611 return AFPERR_ACCESS;
621 /* -------------------------
622 appledouble mkdir afp error code.
624 static int netatalk_mkdir(const char *name)
626 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
629 return( AFPERR_NOOBJ );
631 return( AFPERR_VLOCK );
634 return( AFPERR_ACCESS );
636 return( AFPERR_EXIST );
639 return( AFPERR_DFULL );
641 return( AFPERR_PARAM );
647 /* -------------------
648 system unlink with afp error code.
649 ENOENT is not an error.
651 int netatalk_unlink(const char *name)
653 if (unlink(name) < 0) {
661 return AFPERR_ACCESS;
669 /* ------------------- */
670 static int deletedir(char *dir)
672 char path[MAXPATHLEN + 1];
680 if ((len = strlen(dir)) +2 > sizeof(path))
684 if ((dp = opendir(dir)) == NULL)
690 remain = sizeof(path) -len -1;
691 while ((de = readdir(dp)) && err == AFP_OK) {
692 /* skip this and previous directory */
693 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
696 if (strlen(de->d_name) > remain) {
700 strcpy(path + len, de->d_name);
701 if (stat(path, &st)) {
704 if (S_ISDIR(st.st_mode)) {
705 err = deletedir(path);
707 err = netatalk_unlink(path);
712 /* okay. the directory is empty. delete it. note: we already got rid
715 err = netatalk_rmdir(dir);
720 /* do a recursive copy. */
721 static int copydir(char *src, char *dst, int noadouble)
723 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
733 /* doesn't exist or the path is too long. */
734 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
735 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
736 ((dp = opendir(src)) == NULL))
739 /* try to create the destination directory */
740 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
745 /* set things up to copy */
749 srem = sizeof(spath) - slen -1;
754 drem = sizeof(dpath) - dlen -1;
757 while ((de = readdir(dp))) {
758 /* skip this and previous directory */
759 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
762 if (strlen(de->d_name) > srem) {
766 strcpy(spath + slen, de->d_name);
768 if (stat(spath, &st) == 0) {
769 if (strlen(de->d_name) > drem) {
773 strcpy(dpath + dlen, de->d_name);
775 if (S_ISDIR(st.st_mode)) {
776 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
778 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
782 /* keep the same time stamp. */
783 ut.actime = ut.modtime = st.st_mtime;
789 /* keep the same time stamp. */
790 if (stat(src, &st) == 0) {
791 ut.actime = ut.modtime = st.st_mtime;
801 /* --- public functions follow --- */
803 /* NOTE: we start off with at least one node (the root directory). */
804 struct dir *dirinsert( vol, dir )
810 if ((node = dir_insert(vol, dir)))
813 /* recolor the tree. the current node is red. */
814 dir->d_color = DIRTREE_COLOR_RED;
816 /* parent of this node has to be black. if the parent node
817 * is red, then we have a grandparent. */
818 while ((dir != vol->v_root) &&
819 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
820 /* are we on the left tree? */
821 if (dir->d_back == dir->d_back->d_back->d_left) {
822 node = dir->d_back->d_back->d_right; /* get the right node */
823 if (node->d_color == DIRTREE_COLOR_RED) {
824 /* we're red. we need to change to black. */
825 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
826 node->d_color = DIRTREE_COLOR_BLACK;
827 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
828 dir = dir->d_back->d_back; /* finished. go up. */
830 if (dir == dir->d_back->d_right) {
832 dir_leftrotate(vol, dir);
834 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
835 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
836 dir_rightrotate(vol, dir->d_back->d_back);
839 node = dir->d_back->d_back->d_left;
840 if (node->d_color == DIRTREE_COLOR_RED) {
841 /* we're red. we need to change to black. */
842 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
843 node->d_color = DIRTREE_COLOR_BLACK;
844 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
845 dir = dir->d_back->d_back; /* finished. ascend */
847 if (dir == dir->d_back->d_left) {
849 dir_rightrotate(vol, dir);
851 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
852 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
853 dir_leftrotate(vol, dir->d_back->d_back);
858 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
862 /* free everything down. we don't bother to recolor as this is only
863 * called to free the entire tree */
864 void dirfreename(struct dir *dir)
866 if (dir->d_u_name != dir->d_m_name) {
875 if (!dir || (dir == SENTINEL))
878 if ( dir->d_left != SENTINEL ) {
879 dirfree( dir->d_left );
881 if ( dir->d_right != SENTINEL ) {
882 dirfree( dir->d_right );
885 if (dir != SENTINEL) {
891 /* --------------------------------------------
892 * most of the time mac name and unix name are the same
894 struct dir *dirnew(const char *m_name, const char *u_name)
898 dir = (struct dir *) calloc(1, sizeof( struct dir ));
902 if ((dir->d_m_name = strdup(m_name)) == NULL) {
907 if (m_name == u_name) {
908 dir->d_u_name = dir->d_m_name;
910 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
916 dir->d_left = dir->d_right = SENTINEL;
917 dir->d_next = dir->d_prev = dir;
921 /* -------------------------------------------------- */
927 stat the file or errno
930 curdir: filename parent directory
936 stat the dir or errno
940 curdir: dir parent directory
948 curdir: dir parent directory
955 cname( vol, dir, cpath )
956 const struct vol *vol;
961 static char path[ MAXPATHLEN + 1];
962 static struct path ret;
973 afp_errno = AFPERR_NOOBJ;
974 switch (ret.m_type = *data) { /* path type */
977 len = (unsigned char) *data++;
982 if (afp_version >= 30) {
984 memcpy(&hint, data, sizeof(hint));
986 data += sizeof(hint);
988 memcpy(&len16, data, sizeof(len16));
995 /* else it's an error */
997 afp_errno = AFPERR_PARAM;
1000 *cpath += len + size;
1008 if (movecwd( vol, dir ) < 0 ) {
1010 movecwd failed some of dir path are not there anymore.
1011 FIXME Is it true with other errors?
1012 so we remove dir from the cache
1014 if (dir->d_did == DIRDID_ROOT_PARENT)
1016 if (afp_errno == AFPERR_ACCESS) {
1017 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1020 ret.m_name = dir->d_m_name;
1021 ret.u_name = dir->d_u_name;
1024 } else if (afp_errno == AFPERR_NOOBJ) {
1025 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1028 strcpy(path, dir->d_m_name);
1029 if (dir->d_m_name == dir->d_u_name) {
1033 size_t tp = strlen(path)+1;
1035 strcpy(path +tp, dir->d_u_name);
1036 ret.u_name = path +tp;
1039 dir_invalidate(vol, dir);
1042 dir_invalidate(vol, dir);
1045 if (*path == '\0') {
1050 } /* if (len == 0) */
1052 if (*data == sep ) {
1056 while (*data == sep && len > 0 ) {
1057 if ( dir->d_parent == NULL ) {
1060 dir = dir->d_parent;
1065 /* would this be faster with strlen + strncpy? */
1067 while ( *data != sep && len > 0 ) {
1069 if (p > &path[ MAXPATHLEN]) {
1070 afp_errno = AFPERR_PARAM;
1076 /* short cut bits by chopping off a trailing \0. this also
1077 makes the traversal happy w/ filenames at the end of the
1084 if ( p != path ) { /* we got something */
1086 if (afp_version >= 30) {
1087 /* check for OS X mangled filename :( */
1091 t = demangle_osx(vol, path, dir->d_did, &fileid);
1094 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1095 * flags weren't the same
1097 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1098 /* at last got our view of mac name */
1104 cdir = dir->d_child;
1106 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1109 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1112 if ( cdir == NULL ) {
1114 /* if dir == curdir it always succeed,
1115 even if curdir is deleted.
1116 it's not a pb because it will fail in extenddir
1118 if ( movecwd( vol, dir ) < 0 ) {
1119 /* dir is not valid anymore
1120 we delete dir from the cache and abort.
1122 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1123 afp_errno = AFPERR_NOOBJ;
1126 if (afp_errno == AFPERR_ACCESS)
1128 dir_invalidate(vol, dir);
1131 cdir = extenddir( vol, dir, &ret );
1135 cdir = extenddir( vol, dir, &ret );
1136 } /* if (!extend) */
1138 if ( cdir == NULL ) {
1140 if ( len > 0 || !ret.u_name) {
1148 } /* if (p != path) */
1153 * Move curdir to dir, with a possible chdir()
1155 int movecwd( vol, dir)
1156 const struct vol *vol;
1159 char path[MAXPATHLEN + 1];
1164 if ( dir == curdir ) {
1167 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1168 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1172 p = path + sizeof(path) - 1;
1175 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1178 /* parent directory is deleted */
1179 afp_errno = AFPERR_NOOBJ;
1183 if (p -n -1 < path) {
1184 afp_errno = AFPERR_PARAM;
1191 if ( d != curdir ) {
1192 n = strlen( vol->v_path );
1193 if (p -n -1 < path) {
1194 afp_errno = AFPERR_PARAM;
1199 strncpy( p, vol->v_path, n );
1201 if ( chdir( p ) < 0 ) {
1205 afp_errno = AFPERR_ACCESS;
1208 afp_errno = AFPERR_NOOBJ;
1218 * We can't use unix file's perm to support Apple's inherited protection modes.
1219 * If we aren't the file's owner we can't change its perms when moving it and smb
1220 * nfs,... don't even try.
1222 #define AFP_CHECK_ACCESS
1224 int check_access(char *path, int mode)
1226 #ifdef AFP_CHECK_ACCESS
1234 accessmode(p, &ma, curdir, NULL);
1235 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1237 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1243 /* --------------------- */
1244 int file_access(struct path *path, int mode)
1248 accessmode(path->u_name, &ma, curdir, &path->st);
1249 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1251 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1257 /* ------------------------------
1259 (name, dir) with curdir:name == dir, from afp_enumerate
1262 int getdirparams(const struct vol *vol,
1263 u_int16_t bitmap, struct path *s_path,
1265 char *buf, int *buflen )
1269 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1270 int bit = 0, isad = 0;
1276 struct stat *st = &s_path->st;
1277 char *upath = s_path->u_name;
1279 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1280 (1 << DIRPBIT_CDATE) |
1281 (1 << DIRPBIT_MDATE) |
1282 (1 << DIRPBIT_BDATE) |
1283 (1 << DIRPBIT_FINFO)))) {
1285 ad_init(&ad, vol->v_adouble);
1286 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1287 DIRBITS | 0777, &ad)) {
1292 if ( dir->d_did == DIRDID_ROOT) {
1293 pdid = DIRDID_ROOT_PARENT;
1294 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1297 pdid = dir->d_parent->d_did;
1301 while ( bitmap != 0 ) {
1302 while (( bitmap & 1 ) == 0 ) {
1310 ad_getattr(&ad, &ashort);
1311 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1312 && strcmp(dir->d_u_name, "..")) {
1313 ashort = htons(ATTRBIT_INVISIBLE);
1316 ashort |= htons(ATTRBIT_SHARED);
1317 memcpy( data, &ashort, sizeof( ashort ));
1318 data += sizeof( ashort );
1322 memcpy( data, &pdid, sizeof( pdid ));
1323 data += sizeof( pdid );
1326 case DIRPBIT_CDATE :
1327 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1328 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1329 memcpy( data, &aint, sizeof( aint ));
1330 data += sizeof( aint );
1333 case DIRPBIT_MDATE :
1334 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1335 memcpy( data, &aint, sizeof( aint ));
1336 data += sizeof( aint );
1339 case DIRPBIT_BDATE :
1340 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1341 aint = AD_DATE_START;
1342 memcpy( data, &aint, sizeof( aint ));
1343 data += sizeof( aint );
1346 case DIRPBIT_FINFO :
1348 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1349 } else { /* no appledouble */
1350 memset( data, 0, 32 );
1351 /* set default view -- this also gets done in ad_open() */
1352 ashort = htons(FINDERINFO_CLOSEDVIEW);
1353 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1355 /* dot files are by default invisible */
1356 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1357 strcmp(dir->d_u_name , "..")) {
1358 ashort = htons(FINDERINFO_INVISIBLE);
1359 memcpy(data + FINDERINFO_FRFLAGOFF,
1360 &ashort, sizeof(ashort));
1366 case DIRPBIT_LNAME :
1367 if (dir->d_m_name) /* root of parent can have a null name */
1370 memset(data, 0, sizeof(u_int16_t));
1371 data += sizeof( u_int16_t );
1374 case DIRPBIT_SNAME :
1375 memset(data, 0, sizeof(u_int16_t));
1376 data += sizeof( u_int16_t );
1380 memcpy( data, &dir->d_did, sizeof( aint ));
1381 data += sizeof( aint );
1384 case DIRPBIT_OFFCNT :
1386 /* this needs to handle current directory access rights */
1387 if (st->st_ctime == dir->ctime) {
1388 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1390 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1392 dir->ctime = st->st_ctime;
1393 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1395 ashort = htons( ashort );
1396 memcpy( data, &ashort, sizeof( ashort ));
1397 data += sizeof( ashort );
1401 aint = htonl(st->st_uid);
1402 memcpy( data, &aint, sizeof( aint ));
1403 data += sizeof( aint );
1407 aint = htonl(st->st_gid);
1408 memcpy( data, &aint, sizeof( aint ));
1409 data += sizeof( aint );
1412 case DIRPBIT_ACCESS :
1413 accessmode( upath, &ma, dir , st);
1415 *data++ = ma.ma_user;
1416 *data++ = ma.ma_world;
1417 *data++ = ma.ma_group;
1418 *data++ = ma.ma_owner;
1421 /* Client has requested the ProDOS information block.
1422 Just pass back the same basic block for all
1423 directories. <shirsch@ibm.net> */
1424 case DIRPBIT_PDINFO :
1425 if (afp_version >= 30) { /* UTF8 name */
1426 utf8 = kTextEncodingUTF8;
1427 if (dir->d_m_name) /* root of parent can have a null name */
1430 memset(data, 0, sizeof(u_int16_t));
1431 data += sizeof( u_int16_t );
1433 memcpy(data, &aint, sizeof( aint ));
1434 data += sizeof( aint );
1436 else { /* ProDOS Info Block */
1439 ashort = htons( 0x0200 );
1440 memcpy( data, &ashort, sizeof( ashort ));
1441 data += sizeof( ashort );
1442 memset( data, 0, sizeof( ashort ));
1443 data += sizeof( ashort );
1447 case DIRPBIT_UNIXPR :
1448 aint = htonl(st->st_uid);
1449 memcpy( data, &aint, sizeof( aint ));
1450 data += sizeof( aint );
1451 aint = htonl(st->st_gid);
1452 memcpy( data, &aint, sizeof( aint ));
1453 data += sizeof( aint );
1456 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1457 memcpy( data, &aint, sizeof( aint ));
1458 data += sizeof( aint );
1460 accessmode( upath, &ma, dir , st);
1462 *data++ = ma.ma_user;
1463 *data++ = ma.ma_world;
1464 *data++ = ma.ma_group;
1465 *data++ = ma.ma_owner;
1471 ad_close( &ad, ADFLAGS_HF );
1473 return( AFPERR_BITMAP );
1479 ashort = htons( data - buf );
1480 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1481 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1483 if ( utf_nameoff ) {
1484 ashort = htons( data - buf );
1485 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1486 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1489 ad_close( &ad, ADFLAGS_HF );
1491 *buflen = data - buf;
1495 /* ----------------------------- */
1496 int path_error(struct path *path, int error)
1498 /* - a dir with access error
1499 * - no error it's a file
1502 if (path_isadir(path))
1506 return AFPERR_BADTYPE ;
1509 /* ----------------------------- */
1510 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1513 int ibuflen, *rbuflen;
1518 u_int16_t vid, bitmap;
1524 memcpy( &vid, ibuf, sizeof( vid ));
1525 ibuf += sizeof( vid );
1527 if (NULL == ( vol = getvolbyvid( vid )) ) {
1528 return( AFPERR_PARAM );
1531 if (vol->v_flags & AFPVOL_RO)
1532 return AFPERR_VLOCK;
1534 memcpy( &did, ibuf, sizeof( did ));
1535 ibuf += sizeof( int );
1537 if (NULL == ( dir = dirlookup( vol, did )) ) {
1541 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1542 bitmap = ntohs( bitmap );
1543 ibuf += sizeof( bitmap );
1545 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1546 return get_afp_errno(AFPERR_NOOBJ);
1549 if ( *path->m_name != '\0' ) {
1550 return path_error(path, AFPERR_NOOBJ);
1554 * If ibuf is odd, make it even.
1556 if ((u_long)ibuf & 1 ) {
1560 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1561 setvoltime(obj, vol );
1567 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1569 * assume path == '\0' eg. it's a directory in canonical form
1572 struct path Cur_Path = {
1575 ".", /* unix name */
1576 0, /* stat is not set */
1580 int setdirparams(const struct vol *vol,
1581 struct path *path, u_int16_t bitmap, char *buf )
1589 int bit = 0, aint, isad = 1;
1590 u_int16_t ashort, bshort;
1592 int change_mdate = 0;
1593 int change_parent_mdate = 0;
1596 upath = path->u_name;
1597 ad_init(&ad, vol->v_adouble);
1599 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1600 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1602 * Check to see what we're trying to set. If it's anything
1603 * but ACCESS, UID, or GID, give an error. If it's any of those
1604 * three, we don't need the ad to be open, so just continue.
1606 * note: we also don't need to worry about mdate. also, be quiet
1607 * if we're using the noadouble option.
1609 if (!vol_noadouble(vol) && (bitmap &
1610 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1611 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1612 return AFPERR_ACCESS;
1618 * Check to see if a create was necessary. If it was, we'll want
1619 * to set our name, etc.
1621 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1622 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1623 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1624 ad_getentrylen( &ad, ADEID_NAME ));
1628 while ( bitmap != 0 ) {
1629 while (( bitmap & 1 ) == 0 ) {
1638 memcpy( &ashort, buf, sizeof( ashort ));
1639 ad_getattr(&ad, &bshort);
1640 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1641 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1645 ad_setattr(&ad, bshort);
1646 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1647 change_parent_mdate = 1;
1649 buf += sizeof( ashort );
1652 case DIRPBIT_CDATE :
1655 memcpy(&aint, buf, sizeof(aint));
1656 ad_setdate(&ad, AD_DATE_CREATE, aint);
1658 buf += sizeof( aint );
1661 case DIRPBIT_MDATE :
1662 memcpy(&newdate, buf, sizeof(newdate));
1663 buf += sizeof( newdate );
1666 case DIRPBIT_BDATE :
1669 memcpy(&aint, buf, sizeof(aint));
1670 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1672 buf += sizeof( aint );
1675 case DIRPBIT_FINFO :
1678 * Alright, we admit it, this is *really* sick!
1679 * The 4 bytes that we don't copy, when we're dealing
1680 * with the root of a volume, are the directory's
1681 * location information. This eliminates that annoying
1682 * behavior one sees when mounting above another mount
1686 if ( curdir->d_did == DIRDID_ROOT ) {
1687 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1688 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1690 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1696 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1697 change_parent_mdate = 1;
1698 memcpy( &aint, buf, sizeof(aint));
1699 buf += sizeof( aint );
1700 if ( (curdir->d_did == DIRDID_ROOT) &&
1701 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1705 err = AFPERR_ACCESS;
1706 goto setdirparam_done;
1710 goto setdirparam_done;
1713 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1717 goto setdirparam_done;
1722 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1726 err = AFPERR_ACCESS;
1727 goto setdirparam_done;
1731 goto setdirparam_done;
1734 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1741 change_parent_mdate = 1;
1742 memcpy( &aint, buf, sizeof( aint ));
1743 buf += sizeof( aint );
1744 if (curdir->d_did == DIRDID_ROOT)
1745 setdeskowner( -1, ntohl(aint) );
1747 #if 0 /* don't error if we can't set the desktop owner. */
1751 err = AFPERR_ACCESS;
1752 goto setdirparam_done;
1756 goto setdirparam_done;
1759 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1762 goto setdirparam_done;
1768 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1772 err = AFPERR_ACCESS;
1773 goto setdirparam_done;
1777 goto setdirparam_done;
1780 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1787 case DIRPBIT_ACCESS :
1789 change_parent_mdate = 1;
1790 ma.ma_user = *buf++;
1791 ma.ma_world = *buf++;
1792 ma.ma_group = *buf++;
1793 ma.ma_owner = *buf++;
1795 if (curdir->d_did == DIRDID_ROOT)
1796 setdeskmode(mtoumode( &ma ));
1797 #if 0 /* don't error if we can't set the desktop mode */
1801 err = AFPERR_ACCESS;
1802 goto setdirparam_done;
1805 goto setdirparam_done;
1807 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1811 goto setdirparam_done;
1815 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1816 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1820 err = AFPERR_ACCESS;
1821 goto setdirparam_done;
1824 goto setdirparam_done;
1826 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1829 goto setdirparam_done;
1834 /* Ignore what the client thinks we should do to the
1835 ProDOS information block. Skip over the data and
1836 report nothing amiss. <shirsch@ibm.net> */
1837 case DIRPBIT_PDINFO :
1838 if (afp_version < 30) {
1843 case DIRPBIT_UNIXPR :
1844 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1845 buf += sizeof( aint );
1846 buf += sizeof( aint );
1849 change_parent_mdate = 1;
1850 memcpy( &aint, buf, sizeof( aint ));
1851 buf += sizeof( aint );
1852 aint = ntohl (aint);
1853 if (curdir->d_did == DIRDID_ROOT)
1854 setdeskmode( aint );
1855 #if 0 /* don't error if we can't set the desktop mode */
1859 err = AFPERR_ACCESS;
1860 goto setdirparam_done;
1863 goto setdirparam_done;
1865 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1869 goto setdirparam_done;
1873 if ( setdirunixmode( aint, vol_noadouble(vol),
1874 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1878 err = AFPERR_ACCESS;
1879 goto setdirparam_done;
1882 goto setdirparam_done;
1884 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1887 goto setdirparam_done;
1893 err = AFPERR_BITMAP;
1894 goto setdirparam_done;
1903 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1904 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1908 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1909 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1914 if (path->st_valid && !path->st_errno) {
1915 ad_setid(&ad, &path->st, curdir->d_did, vol->v_stamp);
1918 ad_flush( &ad, ADFLAGS_HF );
1919 ad_close( &ad, ADFLAGS_HF );
1922 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1923 && gettimeofday(&tv, NULL) == 0) {
1924 if (!movecwd(vol, curdir->d_parent)) {
1925 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1926 bitmap = 1<<DIRPBIT_MDATE;
1927 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1928 /* should we reset curdir ?*/
1935 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1938 int ibuflen, *rbuflen;
1944 struct path *s_path;
1952 memcpy( &vid, ibuf, sizeof( vid ));
1953 ibuf += sizeof( vid );
1954 if (NULL == ( vol = getvolbyvid( vid )) ) {
1955 return( AFPERR_PARAM );
1958 if (vol->v_flags & AFPVOL_RO)
1959 return AFPERR_VLOCK;
1961 memcpy( &did, ibuf, sizeof( did ));
1962 ibuf += sizeof( did );
1963 if (NULL == ( dir = dirlookup( vol, did )) ) {
1964 return afp_errno; /* was AFPERR_NOOBJ */
1966 /* for concurrent access we need to be sure we are not in the
1967 * folder we want to create...
1971 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1972 return get_afp_errno(AFPERR_PARAM);
1974 /* cname was able to move curdir to it! */
1975 if (*s_path->m_name == '\0')
1976 return AFPERR_EXIST;
1978 upath = s_path->u_name;
1979 if (0 != (err = check_name(vol, upath))) {
1983 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1987 if (of_stat(s_path) < 0) {
1991 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1995 if ( movecwd( vol, dir ) < 0 ) {
1996 return( AFPERR_PARAM );
1999 ad_init(&ad, vol->v_adouble);
2000 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2001 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2002 if (vol_noadouble(vol))
2003 goto createdir_done;
2004 return( AFPERR_ACCESS );
2007 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
2008 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
2009 ad_getentrylen( &ad, ADEID_NAME ));
2010 ad_setid( &ad, &s_path->st, dir->d_did, vol->v_stamp);
2011 ad_flush( &ad, ADFLAGS_HF );
2012 ad_close( &ad, ADFLAGS_HF );
2015 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2016 *rbuflen = sizeof( u_int32_t );
2017 setvoltime(obj, vol );
2022 * dst new unix filename (not a pathname)
2023 * newname new mac name
2027 int renamedir(src, dst, dir, newparent, newname, noadouble)
2028 char *src, *dst, *newname;
2029 struct dir *dir, *newparent;
2030 const int noadouble;
2037 /* existence check moved to afp_moveandrename */
2038 if ( unix_rename( src, dst ) < 0 ) {
2041 return( AFPERR_NOOBJ );
2043 return( AFPERR_ACCESS );
2045 return AFPERR_VLOCK;
2047 /* tried to move directory into a subdirectory of itself */
2048 return AFPERR_CANTMOVE;
2050 /* this needs to copy and delete. bleah. that means we have
2051 * to deal with entire directory hierarchies. */
2052 if ((err = copydir(src, dst, noadouble)) < 0) {
2056 if ((err = deletedir(src)) < 0)
2060 return( AFPERR_PARAM );
2064 ad_init(&ad, 0); /* FIXME */
2066 len = strlen( newname );
2067 /* rename() succeeded so we need to update our tree even if we can't open
2070 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2071 ad_setentrylen( &ad, ADEID_NAME, len );
2072 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2073 ad_flush( &ad, ADFLAGS_HF );
2074 ad_close( &ad, ADFLAGS_HF );
2077 if (dir->d_m_name == dir->d_u_name)
2078 dir->d_u_name = NULL;
2080 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2081 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2082 /* FIXME : fatal ? */
2085 dir->d_m_name = buf;
2086 strcpy( dir->d_m_name, newname );
2088 if (newname == dst) {
2089 free(dir->d_u_name);
2090 dir->d_u_name = dir->d_m_name;
2093 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2094 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2097 dir->d_u_name = buf;
2098 strcpy( dir->d_u_name, dst );
2101 if (( parent = dir->d_parent ) == NULL ) {
2104 if ( parent == newparent ) {
2108 /* detach from old parent and add to new one. */
2109 dirchildremove(parent, dir);
2110 dir->d_parent = newparent;
2111 dirchildadd(newparent, dir);
2115 #define DOT_APPLEDOUBLE_LEN 13
2116 /* delete an empty directory */
2117 int deletecurdir( vol, path, pathlen )
2118 const struct vol *vol;
2130 if ( curdir->d_parent == NULL ) {
2131 return( AFPERR_ACCESS );
2136 ad_init(&ad, vol->v_adouble);
2137 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2138 DIRBITS | 0777, &ad) == 0 ) {
2140 ad_getattr(&ad, &ashort);
2141 ad_close( &ad, ADFLAGS_HF );
2142 if ((ashort & htons(ATTRBIT_NODELETE))) {
2143 return AFPERR_OLOCK;
2147 /* delete stray .AppleDouble files. this happens to get .Parent files
2149 if ((dp = opendir(".AppleDouble"))) {
2150 strcpy(path, ".AppleDouble/");
2151 while ((de = readdir(dp))) {
2152 /* skip this and previous directory */
2153 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2156 /* bail if the file exists in the current directory.
2157 * note: this will not fail with dangling symlinks */
2158 if (stat(de->d_name, &st) == 0) {
2160 return AFPERR_DIRNEMPT;
2163 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2164 if ((err = netatalk_unlink(path))) {
2172 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2176 /* now get rid of dangling symlinks */
2177 if ((dp = opendir("."))) {
2178 while ((de = readdir(dp))) {
2179 /* skip this and previous directory */
2180 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2183 /* bail if it's not a symlink */
2184 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2186 return AFPERR_DIRNEMPT;
2189 if ((err = netatalk_unlink(de->d_name))) {
2196 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2201 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2202 dirchildremove(curdir, fdir);
2203 cnid_delete(vol->v_cdb, fdir->d_did);
2204 dir_remove( vol, fdir );
2209 /* inode is used as key for cnid.
2210 * Close the descriptor only after cnid_delete
2218 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2221 int ibuflen, *rbuflen;
2231 sfunc = (unsigned char) *ibuf++;
2232 memcpy( &id, ibuf, sizeof( id ));
2237 if (sfunc == 3 || sfunc == 4) {
2238 if (afp_version < 30) {
2239 return( AFPERR_PARAM );
2246 case 3 :/* unicode */
2247 if (( pw = getpwuid( id )) == NULL ) {
2248 return( AFPERR_NOITEM );
2254 case 4 : /* unicode */
2255 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2256 return( AFPERR_NOITEM );
2262 return( AFPERR_PARAM );
2264 len = strlen( name );
2271 u_int16_t tp = htons(len);
2272 memcpy(rbuf, &tp, sizeof(tp));
2281 memcpy( rbuf, name, len );
2287 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2290 int ibuflen, *rbuflen;
2299 sfunc = (unsigned char) *ibuf++;
2303 case 2 : /* unicode */
2304 if (afp_version < 30) {
2305 return( AFPERR_PARAM );
2307 memcpy(&ulen, ibuf, sizeof(ulen));
2313 len = (unsigned char) *ibuf++;
2316 return( AFPERR_PARAM );
2323 case 1 : /* unicode */
2325 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2326 return( AFPERR_NOITEM );
2331 case 2 : /* unicode */
2333 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2334 return( AFPERR_NOITEM );
2343 memcpy( rbuf, &id, sizeof( id ));
2344 *rbuflen = sizeof( id );
2348 /* ------------------------------------
2349 variable DID support
2351 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2354 int ibuflen, *rbuflen;
2365 /* do nothing as dids are static for the life of the process. */
2369 memcpy(&vid, ibuf, sizeof( vid ));
2370 ibuf += sizeof( vid );
2371 if (( vol = getvolbyvid( vid )) == NULL ) {
2372 return( AFPERR_PARAM );
2375 memcpy( &did, ibuf, sizeof( did ));
2376 ibuf += sizeof( did );
2377 if (( dir = dirlookup( vol, did )) == NULL ) {
2378 return( AFPERR_PARAM );
2381 /* dir_remove -- deletedid */
2387 /* did creation gets done automatically
2388 * there's a pb again with case but move it to cname
2390 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2393 int ibuflen, *rbuflen;
2396 struct dir *parentdir;
2404 memcpy(&vid, ibuf, sizeof(vid));
2405 ibuf += sizeof( vid );
2407 if (NULL == ( vol = getvolbyvid( vid )) ) {
2408 return( AFPERR_PARAM );
2411 memcpy(&did, ibuf, sizeof(did));
2412 ibuf += sizeof(did);
2414 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2418 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2419 return get_afp_errno(AFPERR_PARAM);
2422 if ( *path->m_name != '\0' ) {
2423 return path_error(path, AFPERR_NOOBJ);
2426 if ( !path->st_valid && of_stat(path ) < 0 ) {
2427 return( AFPERR_NOOBJ );
2429 if ( path->st_errno ) {
2430 return( AFPERR_NOOBJ );
2433 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2434 *rbuflen = sizeof(curdir->d_did);