2 * $Id: directory.c,v 1.71.2.4.2.15.2.4 2005-01-31 17:00:43 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->d_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, 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;
985 afp_errno = AFPERR_NOOBJ;
986 switch (ret.m_type = *data) { /* path type */
989 len = (unsigned char) *data++;
992 if (afp_version >= 30) {
998 if (afp_version >= 30) {
1000 memcpy(&hint, data, sizeof(hint));
1002 data += sizeof(hint);
1004 memcpy(&len16, data, sizeof(len16));
1011 /* else it's an error */
1013 afp_errno = AFPERR_PARAM;
1016 *cpath += len + size;
1024 if (movecwd( vol, dir ) < 0 ) {
1026 movecwd failed some of dir path are not there anymore.
1027 FIXME Is it true with other errors?
1028 so we remove dir from the cache
1030 if (dir->d_did == DIRDID_ROOT_PARENT)
1032 if (afp_errno == AFPERR_ACCESS) {
1033 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1036 /* FIXME should we set, don't need to call stat() after:
1038 ret.st_errno = EACCES;
1040 ret.m_name = dir->d_m_name;
1041 ret.u_name = dir->d_u_name;
1044 } else if (afp_errno == AFPERR_NOOBJ) {
1045 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1048 strcpy(path, dir->d_m_name);
1049 if (dir->d_m_name == dir->d_u_name) {
1053 size_t tp = strlen(path)+1;
1055 strcpy(path +tp, dir->d_u_name);
1056 ret.u_name = path +tp;
1059 /* FIXME should we set :
1061 ret.st_errno = ENOENT;
1064 dir_invalidate(vol, dir);
1067 dir_invalidate(vol, dir);
1070 if (*path == '\0') {
1075 } /* if (len == 0) */
1077 if (*data == sep ) {
1081 while (*data == sep && len > 0 ) {
1082 if ( dir->d_parent == NULL ) {
1085 dir = dir->d_parent;
1090 /* would this be faster with strlen + strncpy? */
1092 while ( *data != sep && len > 0 ) {
1094 if (p > &path[ MAXPATHLEN]) {
1095 afp_errno = AFPERR_PARAM;
1101 /* short cut bits by chopping off a trailing \0. this also
1102 makes the traversal happy w/ filenames at the end of the
1109 if ( p != path ) { /* we got something */
1111 if (afp_version >= 30) {
1116 static char temp[ MAXPATHLEN + 1];
1118 /* not an UTF8 name */
1119 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == -1) {
1120 afp_errno = AFPERR_PARAM;
1125 /* check for OS X mangled filename :( */
1127 t = demangle_osx(vol, path, dir->d_did, &fileid);
1130 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1131 * flags weren't the same
1133 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1134 /* at last got our view of mac name */
1140 cdir = dir->d_child;
1142 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1145 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1148 if ( cdir == NULL ) {
1150 /* if dir == curdir it always succeed,
1151 even if curdir is deleted.
1152 it's not a pb because it will fail in extenddir
1154 if ( movecwd( vol, dir ) < 0 ) {
1155 /* dir is not valid anymore
1156 we delete dir from the cache and abort.
1158 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1159 afp_errno = AFPERR_NOOBJ;
1162 if (afp_errno == AFPERR_ACCESS)
1164 dir_invalidate(vol, dir);
1167 cdir = extenddir( vol, dir, &ret );
1171 cdir = extenddir( vol, dir, &ret );
1172 } /* if (!extend) */
1174 if ( cdir == NULL ) {
1176 if ( len > 0 || !ret.u_name) {
1184 } /* if (p != path) */
1189 * Move curdir to dir, with a possible chdir()
1191 int movecwd( vol, dir)
1192 const struct vol *vol;
1195 char path[MAXPATHLEN + 1];
1200 if ( dir == curdir ) {
1203 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1204 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1208 p = path + sizeof(path) - 1;
1211 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1214 /* parent directory is deleted */
1215 afp_errno = AFPERR_NOOBJ;
1219 if (p -n -1 < path) {
1220 afp_errno = AFPERR_PARAM;
1227 if ( d != curdir ) {
1228 n = strlen( vol->v_path );
1229 if (p -n -1 < path) {
1230 afp_errno = AFPERR_PARAM;
1235 memcpy( p, vol->v_path, n );
1237 if ( chdir( p ) < 0 ) {
1241 afp_errno = AFPERR_ACCESS;
1244 afp_errno = AFPERR_NOOBJ;
1254 * We can't use unix file's perm to support Apple's inherited protection modes.
1255 * If we aren't the file's owner we can't change its perms when moving it and smb
1256 * nfs,... don't even try.
1258 #define AFP_CHECK_ACCESS
1260 int check_access(char *path, int mode)
1262 #ifdef AFP_CHECK_ACCESS
1270 accessmode(p, &ma, curdir, NULL);
1271 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1273 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1279 /* --------------------- */
1280 int file_access(struct path *path, int mode)
1284 accessmode(path->u_name, &ma, curdir, &path->st);
1285 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1287 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1293 /* ------------------------------
1295 (name, dir) with curdir:name == dir, from afp_enumerate
1298 int getdirparams(const struct vol *vol,
1299 u_int16_t bitmap, struct path *s_path,
1301 char *buf, int *buflen )
1305 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1306 int bit = 0, isad = 0;
1312 struct stat *st = &s_path->st;
1313 char *upath = s_path->u_name;
1315 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1316 (1 << DIRPBIT_CDATE) |
1317 (1 << DIRPBIT_MDATE) |
1318 (1 << DIRPBIT_BDATE) |
1319 (1 << DIRPBIT_FINFO)))) {
1321 ad_init(&ad, vol->v_adouble);
1322 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1327 if ( dir->d_did == DIRDID_ROOT) {
1328 pdid = DIRDID_ROOT_PARENT;
1329 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1332 pdid = dir->d_parent->d_did;
1336 while ( bitmap != 0 ) {
1337 while (( bitmap & 1 ) == 0 ) {
1345 ad_getattr(&ad, &ashort);
1346 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1347 && strcmp(dir->d_u_name, "..")) {
1348 ashort = htons(ATTRBIT_INVISIBLE);
1351 ashort |= htons(ATTRBIT_SHARED);
1352 memcpy( data, &ashort, sizeof( ashort ));
1353 data += sizeof( ashort );
1357 memcpy( data, &pdid, sizeof( pdid ));
1358 data += sizeof( pdid );
1361 case DIRPBIT_CDATE :
1362 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1363 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1364 memcpy( data, &aint, sizeof( aint ));
1365 data += sizeof( aint );
1368 case DIRPBIT_MDATE :
1369 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1370 memcpy( data, &aint, sizeof( aint ));
1371 data += sizeof( aint );
1374 case DIRPBIT_BDATE :
1375 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1376 aint = AD_DATE_START;
1377 memcpy( data, &aint, sizeof( aint ));
1378 data += sizeof( aint );
1381 case DIRPBIT_FINFO :
1383 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1384 } else { /* no appledouble */
1385 memset( data, 0, 32 );
1386 /* set default view -- this also gets done in ad_open() */
1387 ashort = htons(FINDERINFO_CLOSEDVIEW);
1388 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1390 /* dot files are by default invisible */
1391 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1392 strcmp(dir->d_u_name , "..")) {
1393 ashort = htons(FINDERINFO_INVISIBLE);
1394 memcpy(data + FINDERINFO_FRFLAGOFF,
1395 &ashort, sizeof(ashort));
1401 case DIRPBIT_LNAME :
1402 if (dir->d_m_name) /* root of parent can have a null name */
1405 memset(data, 0, sizeof(u_int16_t));
1406 data += sizeof( u_int16_t );
1409 case DIRPBIT_SNAME :
1410 memset(data, 0, sizeof(u_int16_t));
1411 data += sizeof( u_int16_t );
1415 memcpy( data, &dir->d_did, sizeof( aint ));
1416 data += sizeof( aint );
1419 case DIRPBIT_OFFCNT :
1421 /* this needs to handle current directory access rights */
1422 if (st->st_ctime == dir->ctime) {
1423 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1425 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1427 dir->ctime = st->st_ctime;
1428 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1430 ashort = htons( ashort );
1431 memcpy( data, &ashort, sizeof( ashort ));
1432 data += sizeof( ashort );
1436 aint = htonl(st->st_uid);
1437 memcpy( data, &aint, sizeof( aint ));
1438 data += sizeof( aint );
1442 aint = htonl(st->st_gid);
1443 memcpy( data, &aint, sizeof( aint ));
1444 data += sizeof( aint );
1447 case DIRPBIT_ACCESS :
1448 accessmode( upath, &ma, dir , st);
1450 *data++ = ma.ma_user;
1451 *data++ = ma.ma_world;
1452 *data++ = ma.ma_group;
1453 *data++ = ma.ma_owner;
1456 /* Client has requested the ProDOS information block.
1457 Just pass back the same basic block for all
1458 directories. <shirsch@ibm.net> */
1459 case DIRPBIT_PDINFO :
1460 if (afp_version >= 30) { /* UTF8 name */
1461 utf8 = kTextEncodingUTF8;
1462 if (dir->d_m_name) /* root of parent can have a null name */
1465 memset(data, 0, sizeof(u_int16_t));
1466 data += sizeof( u_int16_t );
1468 memcpy(data, &aint, sizeof( aint ));
1469 data += sizeof( aint );
1471 else { /* ProDOS Info Block */
1474 ashort = htons( 0x0200 );
1475 memcpy( data, &ashort, sizeof( ashort ));
1476 data += sizeof( ashort );
1477 memset( data, 0, sizeof( ashort ));
1478 data += sizeof( ashort );
1482 case DIRPBIT_UNIXPR :
1483 aint = htonl(st->st_uid);
1484 memcpy( data, &aint, sizeof( aint ));
1485 data += sizeof( aint );
1486 aint = htonl(st->st_gid);
1487 memcpy( data, &aint, sizeof( aint ));
1488 data += sizeof( aint );
1491 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1492 memcpy( data, &aint, sizeof( aint ));
1493 data += sizeof( aint );
1495 accessmode( upath, &ma, dir , st);
1497 *data++ = ma.ma_user;
1498 *data++ = ma.ma_world;
1499 *data++ = ma.ma_group;
1500 *data++ = ma.ma_owner;
1505 ad_close( &ad, ADFLAGS_HF );
1507 return( AFPERR_BITMAP );
1513 ashort = htons( data - buf );
1514 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1515 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1517 if ( utf_nameoff ) {
1518 ashort = htons( data - buf );
1519 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1520 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1523 ad_close( &ad, ADFLAGS_HF );
1525 *buflen = data - buf;
1529 /* ----------------------------- */
1530 int path_error(struct path *path, int error)
1532 /* - a dir with access error
1533 * - no error it's a file
1536 if (path_isadir(path))
1538 if (path->st_valid && path->st_errno)
1540 return AFPERR_BADTYPE ;
1543 /* ----------------------------- */
1544 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1547 int ibuflen, *rbuflen;
1552 u_int16_t vid, bitmap;
1558 memcpy( &vid, ibuf, sizeof( vid ));
1559 ibuf += sizeof( vid );
1561 if (NULL == ( vol = getvolbyvid( vid )) ) {
1562 return( AFPERR_PARAM );
1565 if (vol->v_flags & AFPVOL_RO)
1566 return AFPERR_VLOCK;
1568 memcpy( &did, ibuf, sizeof( did ));
1569 ibuf += sizeof( int );
1571 if (NULL == ( dir = dirlookup( vol, did )) ) {
1575 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1576 bitmap = ntohs( bitmap );
1577 ibuf += sizeof( bitmap );
1579 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1580 return get_afp_errno(AFPERR_NOOBJ);
1583 if ( *path->m_name != '\0' ) {
1584 rc = path_error(path, AFPERR_NOOBJ);
1585 /* maybe we are trying to set perms back */
1586 if (rc != AFPERR_ACCESS)
1591 * If ibuf is odd, make it even.
1593 if ((u_long)ibuf & 1 ) {
1597 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1598 setvoltime(obj, vol );
1604 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1606 * assume path == '\0' eg. it's a directory in canonical form
1609 struct path Cur_Path = {
1612 ".", /* unix name */
1613 0, /* stat is not set */
1617 /* ------------------ */
1618 static int set_dir_errors(struct path *path, const char *where, int err)
1623 return AFPERR_ACCESS;
1625 return AFPERR_VLOCK;
1627 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1628 return AFPERR_PARAM;
1631 /* ------------------ */
1632 int setdirparams(const struct vol *vol,
1633 struct path *path, u_int16_t d_bitmap, char *buf )
1642 int bit, aint, isad = 1;
1645 u_int16_t ashort, bshort;
1647 int change_mdate = 0;
1648 int change_parent_mdate = 0;
1650 u_int16_t bitmap = d_bitmap;
1651 u_char finder_buf[32];
1653 mode_t mpriv; /* uninitialized, OK 310105 */
1654 u_int16_t upriv_bit = 0;
1657 upath = path->u_name;
1659 while ( bitmap != 0 ) {
1660 while (( bitmap & 1 ) == 0 ) {
1668 memcpy( &ashort, buf, sizeof( ashort ));
1669 buf += sizeof( ashort );
1671 case DIRPBIT_CDATE :
1673 memcpy(&cdate, buf, sizeof(cdate));
1674 buf += sizeof( cdate );
1676 case DIRPBIT_MDATE :
1677 memcpy(&newdate, buf, sizeof(newdate));
1678 buf += sizeof( newdate );
1680 case DIRPBIT_BDATE :
1682 memcpy(&bdate, buf, sizeof(bdate));
1683 buf += sizeof( bdate );
1685 case DIRPBIT_FINFO :
1687 memcpy( finder_buf, buf, 32 );
1690 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1691 change_parent_mdate = 1;
1692 memcpy( &owner, buf, sizeof(owner));
1693 buf += sizeof( owner );
1696 change_parent_mdate = 1;
1697 memcpy( &group, buf, sizeof( group ));
1698 buf += sizeof( group );
1700 case DIRPBIT_ACCESS :
1702 change_parent_mdate = 1;
1703 ma.ma_user = *buf++;
1704 ma.ma_world = *buf++;
1705 ma.ma_group = *buf++;
1706 ma.ma_owner = *buf++;
1707 mpriv = mtoumode( &ma );
1708 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1709 err = set_dir_errors(path, "setdirmode", errno);
1713 /* Ignore what the client thinks we should do to the
1714 ProDOS information block. Skip over the data and
1715 report nothing amiss. <shirsch@ibm.net> */
1716 case DIRPBIT_PDINFO :
1717 if (afp_version < 30) {
1721 err = AFPERR_BITMAP;
1725 case DIRPBIT_UNIXPR :
1726 if (vol_unix_priv(vol)) {
1727 /* Skip UID and GID for now, there seems to be no way to set them from an OSX client anyway */
1728 buf += sizeof( aint );
1729 buf += sizeof( aint );
1732 change_parent_mdate = 1;
1733 memcpy( &upriv, buf, sizeof( upriv ));
1734 buf += sizeof( upriv );
1735 upriv = ntohl (upriv);
1736 if (dir_rx_set(upriv)) {
1737 /* maybe we are trying to set perms back */
1738 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1740 err = set_dir_errors(path, "setdirmode", errno);
1751 err = AFPERR_BITMAP;
1759 ad_init(&ad, vol->v_adouble);
1761 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1762 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1764 * Check to see what we're trying to set. If it's anything
1765 * but ACCESS, UID, or GID, give an error. If it's any of those
1766 * three, we don't need the ad to be open, so just continue.
1768 * note: we also don't need to worry about mdate. also, be quiet
1769 * if we're using the noadouble option.
1771 if (!vol_noadouble(vol) && (d_bitmap &
1772 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1773 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1774 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1775 return AFPERR_ACCESS;
1781 * Check to see if a create was necessary. If it was, we'll want
1782 * to set our name, etc.
1784 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1785 ad_setname(&ad, curdir->d_m_name);
1791 while ( bitmap != 0 ) {
1792 while (( bitmap & 1 ) == 0 ) {
1800 ad_getattr(&ad, &bshort);
1801 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1802 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1803 change_parent_mdate = 1;
1804 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1805 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1809 ad_setattr(&ad, bshort);
1812 case DIRPBIT_CDATE :
1814 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1817 case DIRPBIT_MDATE :
1819 case DIRPBIT_BDATE :
1821 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1824 case DIRPBIT_FINFO :
1826 if ( dir->d_did == DIRDID_ROOT ) {
1828 * Alright, we admit it, this is *really* sick!
1829 * The 4 bytes that we don't copy, when we're dealing
1830 * with the root of a volume, are the directory's
1831 * location information. This eliminates that annoying
1832 * behavior one sees when mounting above another mount
1835 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1836 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1838 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1842 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1843 if ( (dir->d_did == DIRDID_ROOT) &&
1844 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1845 err = set_dir_errors(path, "setdeskowner", errno);
1846 if (isad && err == AFPERR_PARAM) {
1847 err = AFP_OK; /* ???*/
1850 goto setdirparam_done;
1853 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1854 err = set_dir_errors(path, "setdirowner", errno);
1855 goto setdirparam_done;
1860 if (dir->d_did == DIRDID_ROOT)
1861 setdeskowner( -1, ntohl(group) );
1863 #if 0 /* don't error if we can't set the desktop owner. */
1864 err = set_dir_errors(path, "setdeskowner", errno);
1865 if (isad && err == AFPERR_PARAM) {
1866 err = AFP_OK; /* ???*/
1869 goto setdirparam_done;
1873 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1874 err = set_dir_errors(path, "setdirowner", errno);
1875 goto setdirparam_done;
1879 case DIRPBIT_ACCESS :
1880 if (dir->d_did == DIRDID_ROOT) {
1882 if (!dir_rx_set(mpriv)) {
1883 /* we can't remove read and search for owner on volume root */
1884 err = AFPERR_ACCESS;
1885 goto setdirparam_done;
1889 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1890 err = set_dir_errors(path, "setdirmode", errno);
1891 goto setdirparam_done;
1894 case DIRPBIT_PDINFO :
1895 if (afp_version >= 30) {
1896 err = AFPERR_BITMAP;
1897 goto setdirparam_done;
1900 case DIRPBIT_UNIXPR :
1901 if (vol_unix_priv(vol)) {
1902 if (dir->d_did == DIRDID_ROOT) {
1903 setdeskmode( upriv );
1904 if (!dir_rx_set(upriv)) {
1905 /* we can't remove read and search for owner on volume root */
1906 err = AFPERR_ACCESS;
1907 goto setdirparam_done;
1911 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1912 err = set_dir_errors(path, "setdirmode", errno);
1913 goto setdirparam_done;
1919 err = AFPERR_BITMAP;
1920 goto setdirparam_done;
1929 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1930 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1934 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1935 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1940 if (path->st_valid && !path->st_errno) {
1941 struct stat *st = &path->st;
1943 if (dir && dir->d_parent) {
1944 ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev,
1945 st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
1948 ad_flush( &ad, ADFLAGS_HF );
1949 ad_close( &ad, ADFLAGS_HF );
1952 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1953 && gettimeofday(&tv, NULL) == 0) {
1954 if (!movecwd(vol, dir->d_parent)) {
1955 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1956 /* be careful with bitmap because now dir is null */
1957 bitmap = 1<<DIRPBIT_MDATE;
1958 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1959 /* should we reset curdir ?*/
1966 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1969 int ibuflen, *rbuflen;
1975 struct path *s_path;
1983 memcpy( &vid, ibuf, sizeof( vid ));
1984 ibuf += sizeof( vid );
1985 if (NULL == ( vol = getvolbyvid( vid )) ) {
1986 return( AFPERR_PARAM );
1989 if (vol->v_flags & AFPVOL_RO)
1990 return AFPERR_VLOCK;
1992 memcpy( &did, ibuf, sizeof( did ));
1993 ibuf += sizeof( did );
1994 if (NULL == ( dir = dirlookup( vol, did )) ) {
1995 return afp_errno; /* was AFPERR_NOOBJ */
1997 /* for concurrent access we need to be sure we are not in the
1998 * folder we want to create...
2002 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2003 return get_afp_errno(AFPERR_PARAM);
2005 /* cname was able to move curdir to it! */
2006 if (*s_path->m_name == '\0')
2007 return AFPERR_EXIST;
2009 upath = s_path->u_name;
2010 if (0 != (err = check_name(vol, upath))) {
2014 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2018 if (of_stat(s_path) < 0) {
2022 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2026 if ( movecwd( vol, dir ) < 0 ) {
2027 return( AFPERR_PARAM );
2030 ad_init(&ad, vol->v_adouble);
2031 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2032 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2033 if (vol_noadouble(vol))
2034 goto createdir_done;
2035 return( AFPERR_ACCESS );
2037 ad_setname(&ad, s_path->m_name);
2038 ad_setid( &ad, (vol->v_flags & AFPVOL_NODEV)?0:s_path->st.st_dev,
2039 s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2041 ad_flush( &ad, ADFLAGS_HF );
2042 ad_close( &ad, ADFLAGS_HF );
2045 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2046 *rbuflen = sizeof( u_int32_t );
2047 setvoltime(obj, vol );
2052 * dst new unix filename (not a pathname)
2053 * newname new mac name
2057 int renamedir(vol, src, dst, dir, newparent, newname)
2058 const struct vol *vol;
2059 char *src, *dst, *newname;
2060 struct dir *dir, *newparent;
2067 /* existence check moved to afp_moveandrename */
2068 if ( unix_rename( src, dst ) < 0 ) {
2071 return( AFPERR_NOOBJ );
2073 return( AFPERR_ACCESS );
2075 return AFPERR_VLOCK;
2077 /* tried to move directory into a subdirectory of itself */
2078 return AFPERR_CANTMOVE;
2080 /* this needs to copy and delete. bleah. that means we have
2081 * to deal with entire directory hierarchies. */
2082 if ((err = copydir(vol, src, dst)) < 0) {
2086 if ((err = deletedir(src)) < 0)
2090 return( AFPERR_PARAM );
2094 if (vol->v_adouble == AD_VERSION2_OSX) {
2095 /* We simply move the corresponding ad file as well */
2096 char tempbuf[258]="._";
2097 rename(vol->ad_path(src,0),strcat(tempbuf,dst));
2100 len = strlen( newname );
2101 /* rename() succeeded so we need to update our tree even if we can't open
2105 ad_init(&ad, vol->v_adouble);
2107 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2108 ad_setname(&ad, newname);
2109 ad_flush( &ad, ADFLAGS_HF );
2110 ad_close( &ad, ADFLAGS_HF );
2113 if (dir->d_m_name == dir->d_u_name)
2114 dir->d_u_name = NULL;
2116 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2117 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2118 /* FIXME : fatal ? */
2121 dir->d_m_name = buf;
2122 strcpy( dir->d_m_name, newname );
2124 if (newname == dst) {
2125 free(dir->d_u_name);
2126 dir->d_u_name = dir->d_m_name;
2129 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2130 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2133 dir->d_u_name = buf;
2134 strcpy( dir->d_u_name, dst );
2137 if (( parent = dir->d_parent ) == NULL ) {
2140 if ( parent == newparent ) {
2144 /* detach from old parent and add to new one. */
2145 dirchildremove(parent, dir);
2146 dir->d_parent = newparent;
2147 dirchildadd(newparent, dir);
2151 #define DOT_APPLEDOUBLE_LEN 13
2152 /* delete an empty directory */
2153 int deletecurdir( vol, path, pathlen )
2154 const struct vol *vol;
2166 if ( curdir->d_parent == NULL ) {
2167 return( AFPERR_ACCESS );
2172 ad_init(&ad, vol->v_adouble);
2173 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2175 ad_getattr(&ad, &ashort);
2176 ad_close( &ad, ADFLAGS_HF );
2177 if ((ashort & htons(ATTRBIT_NODELETE))) {
2178 return AFPERR_OLOCK;
2182 if (vol->v_adouble == AD_VERSION2_OSX) {
2184 if ((err = netatalk_unlink(vol->ad_path(".",0) )) ) {
2189 /* delete stray .AppleDouble files. this happens to get .Parent files
2191 if ((dp = opendir(".AppleDouble"))) {
2192 strcpy(path, ".AppleDouble/");
2193 while ((de = readdir(dp))) {
2194 /* skip this and previous directory */
2195 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2198 /* bail if the file exists in the current directory.
2199 * note: this will not fail with dangling symlinks */
2200 if (stat(de->d_name, &st) == 0) {
2202 return AFPERR_DIRNEMPT;
2205 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2206 if ((err = netatalk_unlink(path))) {
2214 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2219 /* now get rid of dangling symlinks */
2220 if ((dp = opendir("."))) {
2221 while ((de = readdir(dp))) {
2222 /* skip this and previous directory */
2223 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2226 /* bail if it's not a symlink */
2227 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2229 return AFPERR_DIRNEMPT;
2232 if ((err = netatalk_unlink(de->d_name))) {
2239 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2244 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2245 dirchildremove(curdir, fdir);
2246 cnid_delete(vol->v_cdb, fdir->d_did);
2247 dir_remove( vol, fdir );
2252 /* inode is used as key for cnid.
2253 * Close the descriptor only after cnid_delete
2261 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2264 int ibuflen, *rbuflen;
2274 sfunc = (unsigned char) *ibuf++;
2275 memcpy( &id, ibuf, sizeof( id ));
2280 if (sfunc == 3 || sfunc == 4) {
2281 if (afp_version < 30) {
2282 return( AFPERR_PARAM );
2289 case 3 :/* unicode */
2290 if (( pw = getpwuid( id )) == NULL ) {
2291 return( AFPERR_NOITEM );
2293 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2294 pw->pw_name, strlen(pw->pw_name), &name);
2298 case 4 : /* unicode */
2299 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2300 return( AFPERR_NOITEM );
2302 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2303 gr->gr_name, strlen(gr->gr_name), &name);
2307 return( AFPERR_PARAM );
2309 len = strlen( name );
2316 u_int16_t tp = htons(len);
2317 memcpy(rbuf, &tp, sizeof(tp));
2326 memcpy( rbuf, name, len );
2334 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2337 int ibuflen, *rbuflen;
2346 sfunc = (unsigned char) *ibuf++;
2350 case 2 : /* unicode */
2351 if (afp_version < 30) {
2352 return( AFPERR_PARAM );
2354 memcpy(&ulen, ibuf, sizeof(ulen));
2360 len = (unsigned char) *ibuf++;
2363 return( AFPERR_PARAM );
2370 case 1 : /* unicode */
2372 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2373 return( AFPERR_NOITEM );
2378 case 2 : /* unicode */
2380 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2381 return( AFPERR_NOITEM );
2390 memcpy( rbuf, &id, sizeof( id ));
2391 *rbuflen = sizeof( id );
2395 /* ------------------------------------
2396 variable DID support
2398 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2401 int ibuflen, *rbuflen;
2412 /* do nothing as dids are static for the life of the process. */
2416 memcpy(&vid, ibuf, sizeof( vid ));
2417 ibuf += sizeof( vid );
2418 if (( vol = getvolbyvid( vid )) == NULL ) {
2419 return( AFPERR_PARAM );
2422 memcpy( &did, ibuf, sizeof( did ));
2423 ibuf += sizeof( did );
2424 if (( dir = dirlookup( vol, did )) == NULL ) {
2425 return( AFPERR_PARAM );
2428 /* dir_remove -- deletedid */
2434 /* did creation gets done automatically
2435 * there's a pb again with case but move it to cname
2437 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2440 int ibuflen, *rbuflen;
2443 struct dir *parentdir;
2451 memcpy(&vid, ibuf, sizeof(vid));
2452 ibuf += sizeof( vid );
2454 if (NULL == ( vol = getvolbyvid( vid )) ) {
2455 return( AFPERR_PARAM );
2458 memcpy(&did, ibuf, sizeof(did));
2459 ibuf += sizeof(did);
2461 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2465 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2466 return get_afp_errno(AFPERR_PARAM);
2469 if ( *path->m_name != '\0' ) {
2470 return path_error(path, AFPERR_NOOBJ);
2473 if ( !path->st_valid && of_stat(path ) < 0 ) {
2474 return( AFPERR_NOOBJ );
2476 if ( path->st_errno ) {
2477 return( AFPERR_NOOBJ );
2480 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2481 *rbuflen = sizeof(curdir->d_did);