2 * $Id: directory.c,v 1.71.2.4.2.15.2.9 2006-09-19 02:24:04 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) == (size_t)-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 /* --------------------- */
1294 static int invisible_dots(const struct vol *vol, const char *name)
1296 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1299 /* ------------------------------
1301 (name, dir) with curdir:name == dir, from afp_enumerate
1304 int getdirparams(const struct vol *vol,
1305 u_int16_t bitmap, struct path *s_path,
1307 char *buf, int *buflen )
1311 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1312 int bit = 0, isad = 0;
1318 struct stat *st = &s_path->st;
1319 char *upath = s_path->u_name;
1321 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1322 (1 << DIRPBIT_CDATE) |
1323 (1 << DIRPBIT_MDATE) |
1324 (1 << DIRPBIT_BDATE) |
1325 (1 << DIRPBIT_FINFO)))) {
1327 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1328 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1333 if ( dir->d_did == DIRDID_ROOT) {
1334 pdid = DIRDID_ROOT_PARENT;
1335 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1338 pdid = dir->d_parent->d_did;
1342 while ( bitmap != 0 ) {
1343 while (( bitmap & 1 ) == 0 ) {
1351 ad_getattr(&ad, &ashort);
1352 } else if (invisible_dots(vol, dir->d_u_name)) {
1353 ashort = htons(ATTRBIT_INVISIBLE);
1356 ashort |= htons(ATTRBIT_SHARED);
1357 memcpy( data, &ashort, sizeof( ashort ));
1358 data += sizeof( ashort );
1362 memcpy( data, &pdid, sizeof( pdid ));
1363 data += sizeof( pdid );
1366 case DIRPBIT_CDATE :
1367 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1368 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1369 memcpy( data, &aint, sizeof( aint ));
1370 data += sizeof( aint );
1373 case DIRPBIT_MDATE :
1374 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1375 memcpy( data, &aint, sizeof( aint ));
1376 data += sizeof( aint );
1379 case DIRPBIT_BDATE :
1380 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1381 aint = AD_DATE_START;
1382 memcpy( data, &aint, sizeof( aint ));
1383 data += sizeof( aint );
1386 case DIRPBIT_FINFO :
1388 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1389 } else { /* no appledouble */
1390 memset( data, 0, 32 );
1391 /* set default view -- this also gets done in ad_open() */
1392 ashort = htons(FINDERINFO_CLOSEDVIEW);
1393 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1395 /* dot files are by default visible */
1396 if (invisible_dots(vol, dir->d_u_name)) {
1397 ashort = htons(FINDERINFO_INVISIBLE);
1398 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1404 case DIRPBIT_LNAME :
1405 if (dir->d_m_name) /* root of parent can have a null name */
1408 memset(data, 0, sizeof(u_int16_t));
1409 data += sizeof( u_int16_t );
1412 case DIRPBIT_SNAME :
1413 memset(data, 0, sizeof(u_int16_t));
1414 data += sizeof( u_int16_t );
1418 memcpy( data, &dir->d_did, sizeof( aint ));
1419 data += sizeof( aint );
1422 case DIRPBIT_OFFCNT :
1424 /* this needs to handle current directory access rights */
1425 if (st->st_ctime == dir->ctime) {
1426 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1428 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1430 dir->ctime = st->st_ctime;
1431 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1433 ashort = htons( ashort );
1434 memcpy( data, &ashort, sizeof( ashort ));
1435 data += sizeof( ashort );
1439 aint = htonl(st->st_uid);
1440 memcpy( data, &aint, sizeof( aint ));
1441 data += sizeof( aint );
1445 aint = htonl(st->st_gid);
1446 memcpy( data, &aint, sizeof( aint ));
1447 data += sizeof( aint );
1450 case DIRPBIT_ACCESS :
1451 accessmode( upath, &ma, dir , st);
1453 *data++ = ma.ma_user;
1454 *data++ = ma.ma_world;
1455 *data++ = ma.ma_group;
1456 *data++ = ma.ma_owner;
1459 /* Client has requested the ProDOS information block.
1460 Just pass back the same basic block for all
1461 directories. <shirsch@ibm.net> */
1462 case DIRPBIT_PDINFO :
1463 if (afp_version >= 30) { /* UTF8 name */
1464 utf8 = kTextEncodingUTF8;
1465 if (dir->d_m_name) /* root of parent can have a null name */
1468 memset(data, 0, sizeof(u_int16_t));
1469 data += sizeof( u_int16_t );
1471 memcpy(data, &aint, sizeof( aint ));
1472 data += sizeof( aint );
1474 else { /* ProDOS Info Block */
1477 ashort = htons( 0x0200 );
1478 memcpy( data, &ashort, sizeof( ashort ));
1479 data += sizeof( ashort );
1480 memset( data, 0, sizeof( ashort ));
1481 data += sizeof( ashort );
1485 case DIRPBIT_UNIXPR :
1486 aint = htonl(st->st_uid);
1487 memcpy( data, &aint, sizeof( aint ));
1488 data += sizeof( aint );
1489 aint = htonl(st->st_gid);
1490 memcpy( data, &aint, sizeof( aint ));
1491 data += sizeof( aint );
1494 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1495 memcpy( data, &aint, sizeof( aint ));
1496 data += sizeof( aint );
1498 accessmode( upath, &ma, dir , st);
1500 *data++ = ma.ma_user;
1501 *data++ = ma.ma_world;
1502 *data++ = ma.ma_group;
1503 *data++ = ma.ma_owner;
1508 ad_close( &ad, ADFLAGS_HF );
1510 return( AFPERR_BITMAP );
1516 ashort = htons( data - buf );
1517 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1518 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1520 if ( utf_nameoff ) {
1521 ashort = htons( data - buf );
1522 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1523 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1526 ad_close( &ad, ADFLAGS_HF );
1528 *buflen = data - buf;
1532 /* ----------------------------- */
1533 int path_error(struct path *path, int error)
1535 /* - a dir with access error
1536 * - no error it's a file
1539 if (path_isadir(path))
1541 if (path->st_valid && path->st_errno)
1543 return AFPERR_BADTYPE ;
1546 /* ----------------------------- */
1547 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1549 char *ibuf, *rbuf _U_;
1550 int ibuflen _U_, *rbuflen;
1555 u_int16_t vid, bitmap;
1561 memcpy( &vid, ibuf, sizeof( vid ));
1562 ibuf += sizeof( vid );
1564 if (NULL == ( vol = getvolbyvid( vid )) ) {
1565 return( AFPERR_PARAM );
1568 if (vol->v_flags & AFPVOL_RO)
1569 return AFPERR_VLOCK;
1571 memcpy( &did, ibuf, sizeof( did ));
1572 ibuf += sizeof( int );
1574 if (NULL == ( dir = dirlookup( vol, did )) ) {
1578 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1579 bitmap = ntohs( bitmap );
1580 ibuf += sizeof( bitmap );
1582 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1583 return get_afp_errno(AFPERR_NOOBJ);
1586 if ( *path->m_name != '\0' ) {
1587 rc = path_error(path, AFPERR_NOOBJ);
1588 /* maybe we are trying to set perms back */
1589 if (rc != AFPERR_ACCESS)
1594 * If ibuf is odd, make it even.
1596 if ((u_long)ibuf & 1 ) {
1600 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1601 setvoltime(obj, vol );
1607 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1609 * assume path == '\0' eg. it's a directory in canonical form
1612 struct path Cur_Path = {
1615 ".", /* unix name */
1616 0, /* stat is not set */
1620 /* ------------------ */
1621 static int set_dir_errors(struct path *path, const char *where, int err)
1626 return AFPERR_ACCESS;
1628 return AFPERR_VLOCK;
1630 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1631 return AFPERR_PARAM;
1634 /* ------------------ */
1635 int setdirparams(const struct vol *vol,
1636 struct path *path, u_int16_t d_bitmap, char *buf )
1648 u_int16_t ashort, bshort;
1650 int change_mdate = 0;
1651 int change_parent_mdate = 0;
1653 u_int16_t bitmap = d_bitmap;
1654 u_char finder_buf[32];
1657 u_int16_t upriv_bit = 0;
1660 upath = path->u_name;
1662 while ( bitmap != 0 ) {
1663 while (( bitmap & 1 ) == 0 ) {
1671 memcpy( &ashort, buf, sizeof( ashort ));
1672 buf += sizeof( ashort );
1674 case DIRPBIT_CDATE :
1676 memcpy(&cdate, buf, sizeof(cdate));
1677 buf += sizeof( cdate );
1679 case DIRPBIT_MDATE :
1680 memcpy(&newdate, buf, sizeof(newdate));
1681 buf += sizeof( newdate );
1683 case DIRPBIT_BDATE :
1685 memcpy(&bdate, buf, sizeof(bdate));
1686 buf += sizeof( bdate );
1688 case DIRPBIT_FINFO :
1690 memcpy( finder_buf, buf, 32 );
1693 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1694 change_parent_mdate = 1;
1695 memcpy( &owner, buf, sizeof(owner));
1696 buf += sizeof( owner );
1699 change_parent_mdate = 1;
1700 memcpy( &group, buf, sizeof( group ));
1701 buf += sizeof( group );
1703 case DIRPBIT_ACCESS :
1705 change_parent_mdate = 1;
1706 ma.ma_user = *buf++;
1707 ma.ma_world = *buf++;
1708 ma.ma_group = *buf++;
1709 ma.ma_owner = *buf++;
1710 mpriv = mtoumode( &ma ) | vol->v_perm;
1711 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1712 err = set_dir_errors(path, "setdirmode", errno);
1716 /* Ignore what the client thinks we should do to the
1717 ProDOS information block. Skip over the data and
1718 report nothing amiss. <shirsch@ibm.net> */
1719 case DIRPBIT_PDINFO :
1720 if (afp_version < 30) {
1724 err = AFPERR_BITMAP;
1728 case DIRPBIT_UNIXPR :
1729 if (vol_unix_priv(vol)) {
1730 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1731 buf += sizeof( owner );
1732 memcpy( &group, buf, sizeof( group ));
1733 buf += sizeof( group );
1736 change_parent_mdate = 1;
1737 memcpy( &upriv, buf, sizeof( upriv ));
1738 buf += sizeof( upriv );
1739 upriv = ntohl (upriv) | vol->v_perm;
1740 if (dir_rx_set(upriv)) {
1741 /* maybe we are trying to set perms back */
1742 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1744 err = set_dir_errors(path, "setdirunixmode", errno);
1755 err = AFPERR_BITMAP;
1763 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1765 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1766 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1768 * Check to see what we're trying to set. If it's anything
1769 * but ACCESS, UID, or GID, give an error. If it's any of those
1770 * three, we don't need the ad to be open, so just continue.
1772 * note: we also don't need to worry about mdate. also, be quiet
1773 * if we're using the noadouble option.
1775 if (!vol_noadouble(vol) && (d_bitmap &
1776 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1777 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1778 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1779 return AFPERR_ACCESS;
1785 * Check to see if a create was necessary. If it was, we'll want
1786 * to set our name, etc.
1788 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1789 ad_setname(&ad, curdir->d_m_name);
1795 while ( bitmap != 0 ) {
1796 while (( bitmap & 1 ) == 0 ) {
1804 ad_getattr(&ad, &bshort);
1805 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1806 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1807 change_parent_mdate = 1;
1808 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1809 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1813 ad_setattr(&ad, bshort);
1816 case DIRPBIT_CDATE :
1818 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1821 case DIRPBIT_MDATE :
1823 case DIRPBIT_BDATE :
1825 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1828 case DIRPBIT_FINFO :
1830 if ( dir->d_did == DIRDID_ROOT ) {
1832 * Alright, we admit it, this is *really* sick!
1833 * The 4 bytes that we don't copy, when we're dealing
1834 * with the root of a volume, are the directory's
1835 * location information. This eliminates that annoying
1836 * behavior one sees when mounting above another mount
1839 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1840 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1842 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1846 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1847 if ( (dir->d_did == DIRDID_ROOT) &&
1848 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1849 err = set_dir_errors(path, "setdeskowner", errno);
1850 if (isad && err == AFPERR_PARAM) {
1851 err = AFP_OK; /* ???*/
1854 goto setdirparam_done;
1857 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1858 err = set_dir_errors(path, "setdirowner", errno);
1859 goto setdirparam_done;
1863 if (dir->d_did == DIRDID_ROOT)
1864 setdeskowner( -1, ntohl(group) );
1865 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1866 err = set_dir_errors(path, "setdirowner", errno);
1867 goto setdirparam_done;
1870 case DIRPBIT_ACCESS :
1871 if (dir->d_did == DIRDID_ROOT) {
1873 if (!dir_rx_set(mpriv)) {
1874 /* we can't remove read and search for owner on volume root */
1875 err = AFPERR_ACCESS;
1876 goto setdirparam_done;
1880 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1881 err = set_dir_errors(path, "setdirmode", errno);
1882 goto setdirparam_done;
1885 case DIRPBIT_PDINFO :
1886 if (afp_version >= 30) {
1887 err = AFPERR_BITMAP;
1888 goto setdirparam_done;
1891 case DIRPBIT_UNIXPR :
1892 if (vol_unix_priv(vol)) {
1893 if (dir->d_did == DIRDID_ROOT) {
1894 if (!dir_rx_set(upriv)) {
1895 /* we can't remove read and search for owner on volume root */
1896 err = AFPERR_ACCESS;
1897 goto setdirparam_done;
1899 setdeskowner( -1, ntohl(group) );
1900 setdeskmode( upriv );
1902 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1903 err = set_dir_errors(path, "setdirowner", errno);
1904 goto setdirparam_done;
1907 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1908 err = set_dir_errors(path, "setdirunixmode", errno);
1909 goto setdirparam_done;
1913 err = AFPERR_BITMAP;
1914 goto setdirparam_done;
1918 err = AFPERR_BITMAP;
1919 goto setdirparam_done;
1928 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1929 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1933 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1934 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1939 if (path->st_valid && !path->st_errno) {
1940 struct stat *st = &path->st;
1942 if (dir && dir->d_parent) {
1943 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
1946 ad_flush( &ad, ADFLAGS_HF );
1947 ad_close( &ad, ADFLAGS_HF );
1950 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1951 && gettimeofday(&tv, NULL) == 0) {
1952 if (!movecwd(vol, dir->d_parent)) {
1953 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1954 /* be careful with bitmap because now dir is null */
1955 bitmap = 1<<DIRPBIT_MDATE;
1956 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1957 /* should we reset curdir ?*/
1964 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1967 int ibuflen _U_, *rbuflen;
1973 struct path *s_path;
1981 memcpy( &vid, ibuf, sizeof( vid ));
1982 ibuf += sizeof( vid );
1983 if (NULL == ( vol = getvolbyvid( vid )) ) {
1984 return( AFPERR_PARAM );
1987 if (vol->v_flags & AFPVOL_RO)
1988 return AFPERR_VLOCK;
1990 memcpy( &did, ibuf, sizeof( did ));
1991 ibuf += sizeof( did );
1992 if (NULL == ( dir = dirlookup( vol, did )) ) {
1993 return afp_errno; /* was AFPERR_NOOBJ */
1995 /* for concurrent access we need to be sure we are not in the
1996 * folder we want to create...
2000 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2001 return get_afp_errno(AFPERR_PARAM);
2003 /* cname was able to move curdir to it! */
2004 if (*s_path->m_name == '\0')
2005 return AFPERR_EXIST;
2007 upath = s_path->u_name;
2008 if (0 != (err = check_name(vol, upath))) {
2012 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2016 if (of_stat(s_path) < 0) {
2020 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2024 if ( movecwd( vol, dir ) < 0 ) {
2025 return( AFPERR_PARAM );
2028 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2029 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2030 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2031 if (vol_noadouble(vol))
2032 goto createdir_done;
2033 return( AFPERR_ACCESS );
2035 ad_setname(&ad, s_path->m_name);
2036 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2038 ad_flush( &ad, ADFLAGS_HF );
2039 ad_close( &ad, ADFLAGS_HF );
2042 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2043 *rbuflen = sizeof( u_int32_t );
2044 setvoltime(obj, vol );
2049 * dst new unix filename (not a pathname)
2050 * newname new mac name
2054 int renamedir(vol, src, dst, dir, newparent, newname)
2055 const struct vol *vol;
2056 char *src, *dst, *newname;
2057 struct dir *dir, *newparent;
2064 /* existence check moved to afp_moveandrename */
2065 if ( unix_rename( src, dst ) < 0 ) {
2068 return( AFPERR_NOOBJ );
2070 return( AFPERR_ACCESS );
2072 return AFPERR_VLOCK;
2074 /* tried to move directory into a subdirectory of itself */
2075 return AFPERR_CANTMOVE;
2077 /* this needs to copy and delete. bleah. that means we have
2078 * to deal with entire directory hierarchies. */
2079 if ((err = copydir(vol, src, dst)) < 0) {
2083 if ((err = deletedir(src)) < 0)
2087 return( AFPERR_PARAM );
2091 if (vol->v_adouble == AD_VERSION2_OSX) {
2092 /* We simply move the corresponding ad file as well */
2093 char tempbuf[258]="._";
2094 rename(vol->ad_path(src,0),strcat(tempbuf,dst));
2097 len = strlen( newname );
2098 /* rename() succeeded so we need to update our tree even if we can't open
2102 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2104 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2105 ad_setname(&ad, newname);
2106 ad_flush( &ad, ADFLAGS_HF );
2107 ad_close( &ad, ADFLAGS_HF );
2110 if (dir->d_m_name == dir->d_u_name)
2111 dir->d_u_name = NULL;
2113 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2114 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2115 /* FIXME : fatal ? */
2118 dir->d_m_name = buf;
2119 strcpy( dir->d_m_name, newname );
2121 if (newname == dst) {
2122 free(dir->d_u_name);
2123 dir->d_u_name = dir->d_m_name;
2126 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2127 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2130 dir->d_u_name = buf;
2131 strcpy( dir->d_u_name, dst );
2134 if (( parent = dir->d_parent ) == NULL ) {
2137 if ( parent == newparent ) {
2141 /* detach from old parent and add to new one. */
2142 dirchildremove(parent, dir);
2143 dir->d_parent = newparent;
2144 dirchildadd(newparent, dir);
2148 #define DOT_APPLEDOUBLE_LEN 13
2149 /* delete an empty directory */
2150 int deletecurdir( vol, path, pathlen )
2151 const struct vol *vol;
2163 if ( curdir->d_parent == NULL ) {
2164 return( AFPERR_ACCESS );
2169 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2170 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2172 ad_getattr(&ad, &ashort);
2173 ad_close( &ad, ADFLAGS_HF );
2174 if ((ashort & htons(ATTRBIT_NODELETE))) {
2175 return AFPERR_OLOCK;
2179 if (vol->v_adouble == AD_VERSION2_OSX) {
2181 if ((err = netatalk_unlink(vol->ad_path(".",0) )) ) {
2186 /* delete stray .AppleDouble files. this happens to get .Parent files
2188 if ((dp = opendir(".AppleDouble"))) {
2189 strcpy(path, ".AppleDouble/");
2190 while ((de = readdir(dp))) {
2191 /* skip this and previous directory */
2192 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2195 /* bail if the file exists in the current directory.
2196 * note: this will not fail with dangling symlinks */
2197 if (stat(de->d_name, &st) == 0) {
2199 return AFPERR_DIRNEMPT;
2202 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2203 if ((err = netatalk_unlink(path))) {
2211 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2216 /* now get rid of dangling symlinks */
2217 if ((dp = opendir("."))) {
2218 while ((de = readdir(dp))) {
2219 /* skip this and previous directory */
2220 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2223 /* bail if it's not a symlink */
2224 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2226 return AFPERR_DIRNEMPT;
2229 if ((err = netatalk_unlink(de->d_name))) {
2236 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2241 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2242 dirchildremove(curdir, fdir);
2243 cnid_delete(vol->v_cdb, fdir->d_did);
2244 dir_remove( vol, fdir );
2249 /* inode is used as key for cnid.
2250 * Close the descriptor only after cnid_delete
2258 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2261 int ibuflen _U_, *rbuflen;
2271 sfunc = (unsigned char) *ibuf++;
2272 memcpy( &id, ibuf, sizeof( id ));
2277 if (sfunc == 3 || sfunc == 4) {
2278 if (afp_version < 30) {
2279 return( AFPERR_PARAM );
2286 case 3 :/* unicode */
2287 if (( pw = getpwuid( id )) == NULL ) {
2288 return( AFPERR_NOITEM );
2290 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2291 pw->pw_name, strlen(pw->pw_name), &name);
2295 case 4 : /* unicode */
2296 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2297 return( AFPERR_NOITEM );
2299 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2300 gr->gr_name, strlen(gr->gr_name), &name);
2304 return( AFPERR_PARAM );
2306 len = strlen( name );
2313 u_int16_t tp = htons(len);
2314 memcpy(rbuf, &tp, sizeof(tp));
2323 memcpy( rbuf, name, len );
2331 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2334 int ibuflen _U_, *rbuflen;
2343 sfunc = (unsigned char) *ibuf++;
2347 case 2 : /* unicode */
2348 if (afp_version < 30) {
2349 return( AFPERR_PARAM );
2351 memcpy(&ulen, ibuf, sizeof(ulen));
2357 len = (unsigned char) *ibuf++;
2360 return( AFPERR_PARAM );
2367 case 1 : /* unicode */
2369 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2370 return( AFPERR_NOITEM );
2375 case 2 : /* unicode */
2377 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2378 return( AFPERR_NOITEM );
2387 memcpy( rbuf, &id, sizeof( id ));
2388 *rbuflen = sizeof( id );
2392 /* ------------------------------------
2393 variable DID support
2395 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2397 char *ibuf _U_, *rbuf _U_;
2398 int ibuflen _U_, *rbuflen;
2409 /* do nothing as dids are static for the life of the process. */
2413 memcpy(&vid, ibuf, sizeof( vid ));
2414 ibuf += sizeof( vid );
2415 if (( vol = getvolbyvid( vid )) == NULL ) {
2416 return( AFPERR_PARAM );
2419 memcpy( &did, ibuf, sizeof( did ));
2420 ibuf += sizeof( did );
2421 if (( dir = dirlookup( vol, did )) == NULL ) {
2422 return( AFPERR_PARAM );
2425 /* dir_remove -- deletedid */
2431 /* did creation gets done automatically
2432 * there's a pb again with case but move it to cname
2434 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2437 int ibuflen _U_, *rbuflen;
2440 struct dir *parentdir;
2448 memcpy(&vid, ibuf, sizeof(vid));
2449 ibuf += sizeof( vid );
2451 if (NULL == ( vol = getvolbyvid( vid )) ) {
2452 return( AFPERR_PARAM );
2455 memcpy(&did, ibuf, sizeof(did));
2456 ibuf += sizeof(did);
2458 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2462 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2463 return get_afp_errno(AFPERR_PARAM);
2466 if ( *path->m_name != '\0' ) {
2467 return path_error(path, AFPERR_NOOBJ);
2470 if ( !path->st_valid && of_stat(path ) < 0 ) {
2471 return( AFPERR_NOOBJ );
2473 if ( path->st_errno ) {
2474 return( AFPERR_NOOBJ );
2477 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2478 *rbuflen = sizeof(curdir->d_did);