2 * $Id: directory.c,v 1.71.2.4.2.8 2004-02-09 23:49:31 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 */
37 #endif /* HAVE_FCNTL_H */
40 #include <sys/types.h>
43 #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 )
570 if ( path->u_name == NULL) {
571 path->u_name = mtoupath(vol, path->m_name, dir->d_did, (path->m_type==3) );
575 if ( path->u_name == NULL) {
576 afp_errno = AFPERR_PARAM;
579 if (of_stat( path ) != 0 ) {
583 if (!S_ISDIR(path->st.st_mode)) {
587 /* FIXME: if this is an AFP3 connection and path->m_type != 3 we might screw dircache here */
588 if ( utf8_encoding() && path->m_type != 3)
590 save_m_name = path->m_name;
592 if (( dir = adddir( vol, dir, path)) == NULL ) {
596 path->m_name = save_m_name;
597 LOG(log_debug, logtype_afpd, "AFP3 connection, mismatch in mtype: %u, unix: %s, mac: %s",
598 path->m_type, path->u_name, path->m_name);
601 if (( dir = adddir( vol, dir, path)) == NULL ) {
607 if ( movecwd( vol, dir ) < 0 ) {
614 /* -------------------
615 system rmdir with afp error code.
616 ENOENT is not an error.
618 static int netatalk_rmdir(const char *name)
620 if (rmdir(name) < 0) {
625 return AFPERR_DIRNEMPT;
628 return AFPERR_ACCESS;
638 /* -------------------------
639 appledouble mkdir afp error code.
641 static int netatalk_mkdir(const char *name)
643 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
646 return( AFPERR_NOOBJ );
648 return( AFPERR_VLOCK );
651 return( AFPERR_ACCESS );
653 return( AFPERR_EXIST );
656 return( AFPERR_DFULL );
658 return( AFPERR_PARAM );
664 /* -------------------
665 system unlink with afp error code.
666 ENOENT is not an error.
668 int netatalk_unlink(const char *name)
670 if (unlink(name) < 0) {
678 return AFPERR_ACCESS;
686 /* ------------------- */
687 static int deletedir(char *dir)
689 char path[MAXPATHLEN + 1];
697 if ((len = strlen(dir)) +2 > sizeof(path))
701 if ((dp = opendir(dir)) == NULL)
707 remain = sizeof(path) -len -1;
708 while ((de = readdir(dp)) && err == AFP_OK) {
709 /* skip this and previous directory */
710 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
713 if (strlen(de->d_name) > remain) {
717 strcpy(path + len, de->d_name);
718 if (stat(path, &st)) {
721 if (S_ISDIR(st.st_mode)) {
722 err = deletedir(path);
724 err = netatalk_unlink(path);
729 /* okay. the directory is empty. delete it. note: we already got rid
732 err = netatalk_rmdir(dir);
737 /* do a recursive copy. */
738 static int copydir(char *src, char *dst, int noadouble)
740 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
750 /* doesn't exist or the path is too long. */
751 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
752 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
753 ((dp = opendir(src)) == NULL))
756 /* try to create the destination directory */
757 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
762 /* set things up to copy */
766 srem = sizeof(spath) - slen -1;
771 drem = sizeof(dpath) - dlen -1;
774 while ((de = readdir(dp))) {
775 /* skip this and previous directory */
776 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
779 if (strlen(de->d_name) > srem) {
783 strcpy(spath + slen, de->d_name);
785 if (stat(spath, &st) == 0) {
786 if (strlen(de->d_name) > drem) {
790 strcpy(dpath + dlen, de->d_name);
792 if (S_ISDIR(st.st_mode)) {
793 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
795 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
799 /* keep the same time stamp. */
800 ut.actime = ut.modtime = st.st_mtime;
806 /* keep the same time stamp. */
807 if (stat(src, &st) == 0) {
808 ut.actime = ut.modtime = st.st_mtime;
818 /* --- public functions follow --- */
820 /* NOTE: we start off with at least one node (the root directory). */
821 struct dir *dirinsert( vol, dir )
827 if ((node = dir_insert(vol, dir)))
830 /* recolor the tree. the current node is red. */
831 dir->d_color = DIRTREE_COLOR_RED;
833 /* parent of this node has to be black. if the parent node
834 * is red, then we have a grandparent. */
835 while ((dir != vol->v_root) &&
836 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
837 /* are we on the left tree? */
838 if (dir->d_back == dir->d_back->d_back->d_left) {
839 node = dir->d_back->d_back->d_right; /* get the right node */
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. go up. */
847 if (dir == dir->d_back->d_right) {
849 dir_leftrotate(vol, dir);
851 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
852 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
853 dir_rightrotate(vol, dir->d_back->d_back);
856 node = dir->d_back->d_back->d_left;
857 if (node->d_color == DIRTREE_COLOR_RED) {
858 /* we're red. we need to change to black. */
859 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
860 node->d_color = DIRTREE_COLOR_BLACK;
861 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
862 dir = dir->d_back->d_back; /* finished. ascend */
864 if (dir == dir->d_back->d_left) {
866 dir_rightrotate(vol, dir);
868 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
869 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
870 dir_leftrotate(vol, dir->d_back->d_back);
875 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
879 /* free everything down. we don't bother to recolor as this is only
880 * called to free the entire tree */
881 void dirfreename(struct dir *dir)
883 if (dir->d_u_name != dir->d_m_name) {
892 if (!dir || (dir == SENTINEL))
895 if ( dir->d_left != SENTINEL ) {
896 dirfree( dir->d_left );
898 if ( dir->d_right != SENTINEL ) {
899 dirfree( dir->d_right );
902 if (dir != SENTINEL) {
908 /* --------------------------------------------
909 * most of the time mac name and unix name are the same
911 struct dir *dirnew(const char *m_name, const char *u_name)
915 dir = (struct dir *) calloc(1, sizeof( struct dir ));
919 if ((dir->d_m_name = strdup(m_name)) == NULL) {
924 if (m_name == u_name) {
925 dir->d_u_name = dir->d_m_name;
927 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
933 dir->d_left = dir->d_right = SENTINEL;
934 dir->d_next = dir->d_prev = dir;
938 /* -------------------------------------------------- */
944 stat the file or errno
947 curdir: filename parent directory
953 stat the dir or errno
957 curdir: dir parent directory
965 curdir: dir parent directory
972 cname( vol, dir, cpath )
973 const struct vol *vol;
978 static char path[ MAXPATHLEN + 1];
979 static struct path ret;
990 afp_errno = AFPERR_NOOBJ;
991 switch (ret.m_type = *data) { /* path type */
994 len = (unsigned char) *data++;
999 if (afp_version >= 30) {
1001 memcpy(&hint, data, sizeof(hint));
1003 data += sizeof(hint);
1005 memcpy(&len16, data, sizeof(len16));
1012 /* else it's an error */
1014 afp_errno = AFPERR_PARAM;
1017 *cpath += len + size;
1025 if (movecwd( vol, dir ) < 0 ) {
1027 movecwd failed some of dir path are not there anymore.
1028 FIXME Is it true with other errors?
1029 so we remove dir from the cache
1031 if (dir->d_did == DIRDID_ROOT_PARENT)
1033 if (afp_errno == AFPERR_ACCESS) {
1034 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1037 ret.m_name = dir->d_m_name;
1038 ret.u_name = dir->d_u_name;
1041 } else if (afp_errno == AFPERR_NOOBJ) {
1042 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1045 strcpy(path, dir->d_m_name);
1046 if (dir->d_m_name == dir->d_u_name) {
1050 size_t tp = strlen(path)+1;
1052 strcpy(path +tp, dir->d_u_name);
1053 ret.u_name = path +tp;
1056 dir_invalidate(vol, dir);
1059 dir_invalidate(vol, dir);
1062 if (*path == '\0') {
1067 } /* if (len == 0) */
1069 if (*data == sep ) {
1073 while (*data == sep && len > 0 ) {
1074 if ( dir->d_parent == NULL ) {
1077 dir = dir->d_parent;
1082 /* would this be faster with strlen + strncpy? */
1084 while ( *data != sep && len > 0 ) {
1086 if (p > &path[ MAXPATHLEN]) {
1087 afp_errno = AFPERR_PARAM;
1093 /* short cut bits by chopping off a trailing \0. this also
1094 makes the traversal happy w/ filenames at the end of the
1101 if ( p != path ) { /* we got something */
1103 if (afp_version >= 30) {
1104 /* check for OS X mangled filename :( */
1108 t = demangle_osx(vol, path, dir->d_did, &fileid);
1111 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1112 * flags weren't the same
1114 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1115 /* at last got our view of mac name */
1121 cdir = dir->d_child;
1123 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1126 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1129 if ( cdir == NULL ) {
1131 /* if dir == curdir it always succeed,
1132 even if curdir is deleted.
1133 it's not a pb because it will fail in extenddir
1135 if ( movecwd( vol, dir ) < 0 ) {
1136 /* dir is not valid anymore
1137 we delete dir from the cache and abort.
1139 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1140 afp_errno = AFPERR_NOOBJ;
1143 if (afp_errno == AFPERR_ACCESS)
1145 dir_invalidate(vol, dir);
1148 cdir = extenddir( vol, dir, &ret );
1152 cdir = extenddir( vol, dir, &ret );
1153 } /* if (!extend) */
1155 if ( cdir == NULL ) {
1157 if ( len > 0 || !ret.u_name) {
1165 } /* if (p != path) */
1170 * Move curdir to dir, with a possible chdir()
1172 int movecwd( vol, dir)
1173 const struct vol *vol;
1176 char path[MAXPATHLEN + 1];
1181 if ( dir == curdir ) {
1184 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1185 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1189 p = path + sizeof(path) - 1;
1192 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1195 /* parent directory is deleted */
1196 afp_errno = AFPERR_NOOBJ;
1200 if (p -n -1 < path) {
1201 afp_errno = AFPERR_PARAM;
1208 if ( d != curdir ) {
1209 n = strlen( vol->v_path );
1210 if (p -n -1 < path) {
1211 afp_errno = AFPERR_PARAM;
1216 strncpy( p, vol->v_path, n );
1218 if ( chdir( p ) < 0 ) {
1222 afp_errno = AFPERR_ACCESS;
1225 afp_errno = AFPERR_NOOBJ;
1235 * We can't use unix file's perm to support Apple's inherited protection modes.
1236 * If we aren't the file's owner we can't change its perms when moving it and smb
1237 * nfs,... don't even try.
1239 #define AFP_CHECK_ACCESS
1241 int check_access(char *path, int mode)
1243 #ifdef AFP_CHECK_ACCESS
1251 accessmode(p, &ma, curdir, NULL);
1252 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1254 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1260 /* --------------------- */
1261 int file_access(struct path *path, int mode)
1265 accessmode(path->u_name, &ma, curdir, &path->st);
1266 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1268 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1274 /* ------------------------------
1276 (name, dir) with curdir:name == dir, from afp_enumerate
1279 int getdirparams(const struct vol *vol,
1280 u_int16_t bitmap, struct path *s_path,
1282 char *buf, int *buflen )
1286 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1287 int bit = 0, isad = 0;
1293 struct stat *st = &s_path->st;
1294 char *upath = s_path->u_name;
1296 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1297 (1 << DIRPBIT_CDATE) |
1298 (1 << DIRPBIT_MDATE) |
1299 (1 << DIRPBIT_BDATE) |
1300 (1 << DIRPBIT_FINFO)))) {
1302 ad_init(&ad, vol->v_adouble);
1303 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1304 DIRBITS | 0777, &ad)) {
1309 if ( dir->d_did == DIRDID_ROOT) {
1310 pdid = DIRDID_ROOT_PARENT;
1311 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1314 pdid = dir->d_parent->d_did;
1318 while ( bitmap != 0 ) {
1319 while (( bitmap & 1 ) == 0 ) {
1327 ad_getattr(&ad, &ashort);
1328 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1329 && strcmp(dir->d_u_name, "..")) {
1330 ashort = htons(ATTRBIT_INVISIBLE);
1333 ashort |= htons(ATTRBIT_SHARED);
1334 memcpy( data, &ashort, sizeof( ashort ));
1335 data += sizeof( ashort );
1339 memcpy( data, &pdid, sizeof( pdid ));
1340 data += sizeof( pdid );
1343 case DIRPBIT_CDATE :
1344 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1345 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1346 memcpy( data, &aint, sizeof( aint ));
1347 data += sizeof( aint );
1350 case DIRPBIT_MDATE :
1351 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1352 memcpy( data, &aint, sizeof( aint ));
1353 data += sizeof( aint );
1356 case DIRPBIT_BDATE :
1357 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1358 aint = AD_DATE_START;
1359 memcpy( data, &aint, sizeof( aint ));
1360 data += sizeof( aint );
1363 case DIRPBIT_FINFO :
1365 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1366 } else { /* no appledouble */
1367 memset( data, 0, 32 );
1368 /* set default view -- this also gets done in ad_open() */
1369 ashort = htons(FINDERINFO_CLOSEDVIEW);
1370 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1372 /* dot files are by default invisible */
1373 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1374 strcmp(dir->d_u_name , "..")) {
1375 ashort = htons(FINDERINFO_INVISIBLE);
1376 memcpy(data + FINDERINFO_FRFLAGOFF,
1377 &ashort, sizeof(ashort));
1383 case DIRPBIT_LNAME :
1384 if (dir->d_m_name) /* root of parent can have a null name */
1387 memset(data, 0, sizeof(u_int16_t));
1388 data += sizeof( u_int16_t );
1391 case DIRPBIT_SNAME :
1392 memset(data, 0, sizeof(u_int16_t));
1393 data += sizeof( u_int16_t );
1397 memcpy( data, &dir->d_did, sizeof( aint ));
1398 data += sizeof( aint );
1401 case DIRPBIT_OFFCNT :
1403 /* this needs to handle current directory access rights */
1404 if (st->st_ctime == dir->ctime) {
1405 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1407 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1409 dir->ctime = st->st_ctime;
1410 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1412 ashort = htons( ashort );
1413 memcpy( data, &ashort, sizeof( ashort ));
1414 data += sizeof( ashort );
1418 aint = htonl(st->st_uid);
1419 memcpy( data, &aint, sizeof( aint ));
1420 data += sizeof( aint );
1424 aint = htonl(st->st_gid);
1425 memcpy( data, &aint, sizeof( aint ));
1426 data += sizeof( aint );
1429 case DIRPBIT_ACCESS :
1430 accessmode( upath, &ma, dir , st);
1432 *data++ = ma.ma_user;
1433 *data++ = ma.ma_world;
1434 *data++ = ma.ma_group;
1435 *data++ = ma.ma_owner;
1438 /* Client has requested the ProDOS information block.
1439 Just pass back the same basic block for all
1440 directories. <shirsch@ibm.net> */
1441 case DIRPBIT_PDINFO :
1442 if (afp_version >= 30) { /* UTF8 name */
1443 utf8 = kTextEncodingUTF8;
1444 if (dir->d_m_name) /* root of parent can have a null name */
1447 memset(data, 0, sizeof(u_int16_t));
1448 data += sizeof( u_int16_t );
1450 memcpy(data, &aint, sizeof( aint ));
1451 data += sizeof( aint );
1453 else { /* ProDOS Info Block */
1456 ashort = htons( 0x0200 );
1457 memcpy( data, &ashort, sizeof( ashort ));
1458 data += sizeof( ashort );
1459 memset( data, 0, sizeof( ashort ));
1460 data += sizeof( ashort );
1464 case DIRPBIT_UNIXPR :
1465 aint = htonl(st->st_uid);
1466 memcpy( data, &aint, sizeof( aint ));
1467 data += sizeof( aint );
1468 aint = htonl(st->st_gid);
1469 memcpy( data, &aint, sizeof( aint ));
1470 data += sizeof( aint );
1473 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1474 memcpy( data, &aint, sizeof( aint ));
1475 data += sizeof( aint );
1477 accessmode( upath, &ma, dir , st);
1479 *data++ = ma.ma_user;
1480 *data++ = ma.ma_world;
1481 *data++ = ma.ma_group;
1482 *data++ = ma.ma_owner;
1488 ad_close( &ad, ADFLAGS_HF );
1490 return( AFPERR_BITMAP );
1496 ashort = htons( data - buf );
1497 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1498 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1500 if ( utf_nameoff ) {
1501 ashort = htons( data - buf );
1502 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1503 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1506 ad_close( &ad, ADFLAGS_HF );
1508 *buflen = data - buf;
1512 /* ----------------------------- */
1513 int path_error(struct path *path, int error)
1515 /* - a dir with access error
1516 * - no error it's a file
1519 if (path_isadir(path))
1523 return AFPERR_BADTYPE ;
1526 /* ----------------------------- */
1527 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1530 int ibuflen, *rbuflen;
1535 u_int16_t vid, bitmap;
1541 memcpy( &vid, ibuf, sizeof( vid ));
1542 ibuf += sizeof( vid );
1544 if (NULL == ( vol = getvolbyvid( vid )) ) {
1545 return( AFPERR_PARAM );
1548 if (vol->v_flags & AFPVOL_RO)
1549 return AFPERR_VLOCK;
1551 memcpy( &did, ibuf, sizeof( did ));
1552 ibuf += sizeof( int );
1554 if (NULL == ( dir = dirlookup( vol, did )) ) {
1558 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1559 bitmap = ntohs( bitmap );
1560 ibuf += sizeof( bitmap );
1562 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1563 return get_afp_errno(AFPERR_NOOBJ);
1566 if ( *path->m_name != '\0' ) {
1567 return path_error(path, AFPERR_NOOBJ);
1571 * If ibuf is odd, make it even.
1573 if ((u_long)ibuf & 1 ) {
1577 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1578 setvoltime(obj, vol );
1584 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1586 * assume path == '\0' eg. it's a directory in canonical form
1589 struct path Cur_Path = {
1592 ".", /* unix name */
1593 0, /* stat is not set */
1597 int setdirparams(const struct vol *vol,
1598 struct path *path, u_int16_t bitmap, char *buf )
1606 int bit = 0, aint, isad = 1;
1607 u_int16_t ashort, bshort;
1609 int change_mdate = 0;
1610 int change_parent_mdate = 0;
1613 upath = path->u_name;
1614 ad_init(&ad, vol->v_adouble);
1616 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1617 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1619 * Check to see what we're trying to set. If it's anything
1620 * but ACCESS, UID, or GID, give an error. If it's any of those
1621 * three, we don't need the ad to be open, so just continue.
1623 * note: we also don't need to worry about mdate. also, be quiet
1624 * if we're using the noadouble option.
1626 if (!vol_noadouble(vol) && (bitmap &
1627 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1628 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1629 return AFPERR_ACCESS;
1635 * Check to see if a create was necessary. If it was, we'll want
1636 * to set our name, etc.
1638 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1639 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1640 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1641 ad_getentrylen( &ad, ADEID_NAME ));
1645 while ( bitmap != 0 ) {
1646 while (( bitmap & 1 ) == 0 ) {
1655 memcpy( &ashort, buf, sizeof( ashort ));
1656 ad_getattr(&ad, &bshort);
1657 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1658 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1662 ad_setattr(&ad, bshort);
1663 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1664 change_parent_mdate = 1;
1666 buf += sizeof( ashort );
1669 case DIRPBIT_CDATE :
1672 memcpy(&aint, buf, sizeof(aint));
1673 ad_setdate(&ad, AD_DATE_CREATE, aint);
1675 buf += sizeof( aint );
1678 case DIRPBIT_MDATE :
1679 memcpy(&newdate, buf, sizeof(newdate));
1680 buf += sizeof( newdate );
1683 case DIRPBIT_BDATE :
1686 memcpy(&aint, buf, sizeof(aint));
1687 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1689 buf += sizeof( aint );
1692 case DIRPBIT_FINFO :
1695 * Alright, we admit it, this is *really* sick!
1696 * The 4 bytes that we don't copy, when we're dealing
1697 * with the root of a volume, are the directory's
1698 * location information. This eliminates that annoying
1699 * behavior one sees when mounting above another mount
1703 if ( curdir->d_did == DIRDID_ROOT ) {
1704 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1705 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1707 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1713 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1714 change_parent_mdate = 1;
1715 memcpy( &aint, buf, sizeof(aint));
1716 buf += sizeof( aint );
1717 if ( (curdir->d_did == DIRDID_ROOT) &&
1718 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1722 err = AFPERR_ACCESS;
1723 goto setdirparam_done;
1727 goto setdirparam_done;
1730 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1734 goto setdirparam_done;
1739 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1743 err = AFPERR_ACCESS;
1744 goto setdirparam_done;
1748 goto setdirparam_done;
1751 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1758 change_parent_mdate = 1;
1759 memcpy( &aint, buf, sizeof( aint ));
1760 buf += sizeof( aint );
1761 if (curdir->d_did == DIRDID_ROOT)
1762 setdeskowner( -1, ntohl(aint) );
1764 #if 0 /* don't error if we can't set the desktop owner. */
1768 err = AFPERR_ACCESS;
1769 goto setdirparam_done;
1773 goto setdirparam_done;
1776 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1779 goto setdirparam_done;
1785 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1789 err = AFPERR_ACCESS;
1790 goto setdirparam_done;
1794 goto setdirparam_done;
1797 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1804 case DIRPBIT_ACCESS :
1806 change_parent_mdate = 1;
1807 ma.ma_user = *buf++;
1808 ma.ma_world = *buf++;
1809 ma.ma_group = *buf++;
1810 ma.ma_owner = *buf++;
1812 if (curdir->d_did == DIRDID_ROOT)
1813 setdeskmode(mtoumode( &ma ));
1814 #if 0 /* don't error if we can't set the desktop mode */
1818 err = AFPERR_ACCESS;
1819 goto setdirparam_done;
1822 goto setdirparam_done;
1824 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1828 goto setdirparam_done;
1832 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1833 (vol->v_flags & AFPVOL_DROPBOX)) < 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( aint, vol_noadouble(vol),
1891 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1895 err = AFPERR_ACCESS;
1896 goto setdirparam_done;
1899 goto setdirparam_done;
1901 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1904 goto setdirparam_done;
1910 err = AFPERR_BITMAP;
1911 goto setdirparam_done;
1920 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1921 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1925 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1926 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1931 if (path->st_valid && !path->st_errno) {
1932 struct stat *st = &path->st;
1934 if (curdir->d_parent) {
1935 ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev,
1936 st->st_ino, curdir->d_did, curdir->d_parent->d_did, vol->v_stamp);
1940 ad_flush( &ad, ADFLAGS_HF );
1941 ad_close( &ad, ADFLAGS_HF );
1944 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1945 && gettimeofday(&tv, NULL) == 0) {
1946 if (!movecwd(vol, curdir->d_parent)) {
1947 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1948 bitmap = 1<<DIRPBIT_MDATE;
1949 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1950 /* should we reset curdir ?*/
1957 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1960 int ibuflen, *rbuflen;
1966 struct path *s_path;
1974 memcpy( &vid, ibuf, sizeof( vid ));
1975 ibuf += sizeof( vid );
1976 if (NULL == ( vol = getvolbyvid( vid )) ) {
1977 return( AFPERR_PARAM );
1980 if (vol->v_flags & AFPVOL_RO)
1981 return AFPERR_VLOCK;
1983 memcpy( &did, ibuf, sizeof( did ));
1984 ibuf += sizeof( did );
1985 if (NULL == ( dir = dirlookup( vol, did )) ) {
1986 return afp_errno; /* was AFPERR_NOOBJ */
1988 /* for concurrent access we need to be sure we are not in the
1989 * folder we want to create...
1993 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1994 return get_afp_errno(AFPERR_PARAM);
1996 /* cname was able to move curdir to it! */
1997 if (*s_path->m_name == '\0')
1998 return AFPERR_EXIST;
2000 upath = s_path->u_name;
2001 if (0 != (err = check_name(vol, upath))) {
2005 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2009 if (of_stat(s_path) < 0) {
2013 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2017 if ( movecwd( vol, dir ) < 0 ) {
2018 return( AFPERR_PARAM );
2021 ad_init(&ad, vol->v_adouble);
2022 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2023 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2024 if (vol_noadouble(vol))
2025 goto createdir_done;
2026 return( AFPERR_ACCESS );
2029 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
2030 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
2031 ad_getentrylen( &ad, ADEID_NAME ));
2033 ad_setid( &ad, (vol->v_flags & AFPVOL_NODEV)?0:s_path->st.st_dev,
2034 s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2036 ad_flush( &ad, ADFLAGS_HF );
2037 ad_close( &ad, ADFLAGS_HF );
2040 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2041 *rbuflen = sizeof( u_int32_t );
2042 setvoltime(obj, vol );
2047 * dst new unix filename (not a pathname)
2048 * newname new mac name
2052 int renamedir(src, dst, dir, newparent, newname, noadouble)
2053 char *src, *dst, *newname;
2054 struct dir *dir, *newparent;
2055 const int noadouble;
2062 /* existence check moved to afp_moveandrename */
2063 if ( unix_rename( src, dst ) < 0 ) {
2066 return( AFPERR_NOOBJ );
2068 return( AFPERR_ACCESS );
2070 return AFPERR_VLOCK;
2072 /* tried to move directory into a subdirectory of itself */
2073 return AFPERR_CANTMOVE;
2075 /* this needs to copy and delete. bleah. that means we have
2076 * to deal with entire directory hierarchies. */
2077 if ((err = copydir(src, dst, noadouble)) < 0) {
2081 if ((err = deletedir(src)) < 0)
2085 return( AFPERR_PARAM );
2089 ad_init(&ad, 0); /* FIXME */
2091 len = strlen( newname );
2092 /* rename() succeeded so we need to update our tree even if we can't open
2095 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2096 ad_setentrylen( &ad, ADEID_NAME, len );
2097 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2098 ad_flush( &ad, ADFLAGS_HF );
2099 ad_close( &ad, ADFLAGS_HF );
2102 if (dir->d_m_name == dir->d_u_name)
2103 dir->d_u_name = NULL;
2105 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2106 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2107 /* FIXME : fatal ? */
2110 dir->d_m_name = buf;
2111 strcpy( dir->d_m_name, newname );
2113 if (newname == dst) {
2114 free(dir->d_u_name);
2115 dir->d_u_name = dir->d_m_name;
2118 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2119 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2122 dir->d_u_name = buf;
2123 strcpy( dir->d_u_name, dst );
2126 if (( parent = dir->d_parent ) == NULL ) {
2129 if ( parent == newparent ) {
2133 /* detach from old parent and add to new one. */
2134 dirchildremove(parent, dir);
2135 dir->d_parent = newparent;
2136 dirchildadd(newparent, dir);
2140 #define DOT_APPLEDOUBLE_LEN 13
2141 /* delete an empty directory */
2142 int deletecurdir( vol, path, pathlen )
2143 const struct vol *vol;
2155 if ( curdir->d_parent == NULL ) {
2156 return( AFPERR_ACCESS );
2161 ad_init(&ad, vol->v_adouble);
2162 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2163 DIRBITS | 0777, &ad) == 0 ) {
2165 ad_getattr(&ad, &ashort);
2166 ad_close( &ad, ADFLAGS_HF );
2167 if ((ashort & htons(ATTRBIT_NODELETE))) {
2168 return AFPERR_OLOCK;
2172 /* delete stray .AppleDouble files. this happens to get .Parent files
2174 if ((dp = opendir(".AppleDouble"))) {
2175 strcpy(path, ".AppleDouble/");
2176 while ((de = readdir(dp))) {
2177 /* skip this and previous directory */
2178 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2181 /* bail if the file exists in the current directory.
2182 * note: this will not fail with dangling symlinks */
2183 if (stat(de->d_name, &st) == 0) {
2185 return AFPERR_DIRNEMPT;
2188 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2189 if ((err = netatalk_unlink(path))) {
2197 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2201 /* now get rid of dangling symlinks */
2202 if ((dp = opendir("."))) {
2203 while ((de = readdir(dp))) {
2204 /* skip this and previous directory */
2205 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2208 /* bail if it's not a symlink */
2209 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2211 return AFPERR_DIRNEMPT;
2214 if ((err = netatalk_unlink(de->d_name))) {
2221 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2226 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2227 dirchildremove(curdir, fdir);
2228 cnid_delete(vol->v_cdb, fdir->d_did);
2229 dir_remove( vol, fdir );
2234 /* inode is used as key for cnid.
2235 * Close the descriptor only after cnid_delete
2243 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2246 int ibuflen, *rbuflen;
2256 sfunc = (unsigned char) *ibuf++;
2257 memcpy( &id, ibuf, sizeof( id ));
2262 if (sfunc == 3 || sfunc == 4) {
2263 if (afp_version < 30) {
2264 return( AFPERR_PARAM );
2271 case 3 :/* unicode */
2272 if (( pw = getpwuid( id )) == NULL ) {
2273 return( AFPERR_NOITEM );
2275 len = convert_string_allocate( obj->options.unixcharset, ((sfunc == 1)?obj->options.maccharset:CH_UTF8_MAC),
2276 pw->pw_name, strlen(pw->pw_name), &name);
2280 case 4 : /* unicode */
2281 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2282 return( AFPERR_NOITEM );
2284 len = convert_string_allocate( obj->options.unixcharset, (sfunc == 1)?obj->options.maccharset:CH_UTF8_MAC,
2285 gr->gr_name, strlen(gr->gr_name), &name);
2289 return( AFPERR_PARAM );
2291 len = strlen( name );
2298 u_int16_t tp = htons(len);
2299 memcpy(rbuf, &tp, sizeof(tp));
2308 memcpy( rbuf, name, len );
2316 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2319 int ibuflen, *rbuflen;
2328 sfunc = (unsigned char) *ibuf++;
2332 case 2 : /* unicode */
2333 if (afp_version < 30) {
2334 return( AFPERR_PARAM );
2336 memcpy(&ulen, ibuf, sizeof(ulen));
2342 len = (unsigned char) *ibuf++;
2345 return( AFPERR_PARAM );
2352 case 1 : /* unicode */
2354 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2355 return( AFPERR_NOITEM );
2360 case 2 : /* unicode */
2362 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2363 return( AFPERR_NOITEM );
2372 memcpy( rbuf, &id, sizeof( id ));
2373 *rbuflen = sizeof( id );
2377 /* ------------------------------------
2378 variable DID support
2380 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2383 int ibuflen, *rbuflen;
2394 /* do nothing as dids are static for the life of the process. */
2398 memcpy(&vid, ibuf, sizeof( vid ));
2399 ibuf += sizeof( vid );
2400 if (( vol = getvolbyvid( vid )) == NULL ) {
2401 return( AFPERR_PARAM );
2404 memcpy( &did, ibuf, sizeof( did ));
2405 ibuf += sizeof( did );
2406 if (( dir = dirlookup( vol, did )) == NULL ) {
2407 return( AFPERR_PARAM );
2410 /* dir_remove -- deletedid */
2416 /* did creation gets done automatically
2417 * there's a pb again with case but move it to cname
2419 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2422 int ibuflen, *rbuflen;
2425 struct dir *parentdir;
2433 memcpy(&vid, ibuf, sizeof(vid));
2434 ibuf += sizeof( vid );
2436 if (NULL == ( vol = getvolbyvid( vid )) ) {
2437 return( AFPERR_PARAM );
2440 memcpy(&did, ibuf, sizeof(did));
2441 ibuf += sizeof(did);
2443 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2447 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2448 return get_afp_errno(AFPERR_PARAM);
2451 if ( *path->m_name != '\0' ) {
2452 return path_error(path, AFPERR_NOOBJ);
2455 if ( !path->st_valid && of_stat(path ) < 0 ) {
2456 return( AFPERR_NOOBJ );
2458 if ( path->st_errno ) {
2459 return( AFPERR_NOOBJ );
2462 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2463 *rbuflen = sizeof(curdir->d_did);