2 * $Id: directory.c,v 1.71.2.4.2.15.2.1 2004-10-20 21:43:33 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))) {
793 /* keep the same time stamp. */
794 ut.actime = ut.modtime = st.st_mtime;
800 /* keep the same time stamp. */
801 if (stat(src, &st) == 0) {
802 ut.actime = ut.modtime = st.st_mtime;
812 /* --- public functions follow --- */
814 /* NOTE: we start off with at least one node (the root directory). */
815 struct dir *dirinsert( vol, dir )
821 if ((node = dir_insert(vol, dir)))
824 /* recolor the tree. the current node is red. */
825 dir->d_color = DIRTREE_COLOR_RED;
827 /* parent of this node has to be black. if the parent node
828 * is red, then we have a grandparent. */
829 while ((dir != vol->v_root) &&
830 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
831 /* are we on the left tree? */
832 if (dir->d_back == dir->d_back->d_back->d_left) {
833 node = dir->d_back->d_back->d_right; /* get the right node */
834 if (node->d_color == DIRTREE_COLOR_RED) {
835 /* we're red. we need to change to black. */
836 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
837 node->d_color = DIRTREE_COLOR_BLACK;
838 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
839 dir = dir->d_back->d_back; /* finished. go up. */
841 if (dir == dir->d_back->d_right) {
843 dir_leftrotate(vol, dir);
845 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
846 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
847 dir_rightrotate(vol, dir->d_back->d_back);
850 node = dir->d_back->d_back->d_left;
851 if (node->d_color == DIRTREE_COLOR_RED) {
852 /* we're red. we need to change to black. */
853 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
854 node->d_color = DIRTREE_COLOR_BLACK;
855 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
856 dir = dir->d_back->d_back; /* finished. ascend */
858 if (dir == dir->d_back->d_left) {
860 dir_rightrotate(vol, dir);
862 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
863 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
864 dir_leftrotate(vol, dir->d_back->d_back);
869 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
873 /* free everything down. we don't bother to recolor as this is only
874 * called to free the entire tree */
875 void dirfreename(struct dir *dir)
877 if (dir->d_u_name != dir->d_m_name) {
886 if (!dir || (dir == SENTINEL))
889 if ( dir->d_left != SENTINEL ) {
890 dirfree( dir->d_left );
892 if ( dir->d_right != SENTINEL ) {
893 dirfree( dir->d_right );
896 if (dir != SENTINEL) {
902 /* --------------------------------------------
903 * most of the time mac name and unix name are the same
905 struct dir *dirnew(const char *m_name, const char *u_name)
909 dir = (struct dir *) calloc(1, sizeof( struct dir ));
913 if ((dir->d_m_name = strdup(m_name)) == NULL) {
918 if (m_name == u_name) {
919 dir->d_u_name = dir->d_m_name;
921 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
927 dir->d_left = dir->d_right = SENTINEL;
928 dir->d_next = dir->d_prev = dir;
932 /* -------------------------------------------------- */
938 stat the file or errno
941 curdir: filename parent directory
947 stat the dir or errno
951 curdir: dir parent directory
959 curdir: dir parent directory
966 cname( vol, dir, cpath )
967 const struct vol *vol;
972 static char path[ MAXPATHLEN + 1];
973 static struct path ret;
984 afp_errno = AFPERR_NOOBJ;
985 switch (ret.m_type = *data) { /* path type */
988 len = (unsigned char) *data++;
993 if (afp_version >= 30) {
995 memcpy(&hint, data, sizeof(hint));
997 data += sizeof(hint);
999 memcpy(&len16, data, sizeof(len16));
1006 /* else it's an error */
1008 afp_errno = AFPERR_PARAM;
1011 *cpath += len + size;
1019 if (movecwd( vol, dir ) < 0 ) {
1021 movecwd failed some of dir path are not there anymore.
1022 FIXME Is it true with other errors?
1023 so we remove dir from the cache
1025 if (dir->d_did == DIRDID_ROOT_PARENT)
1027 if (afp_errno == AFPERR_ACCESS) {
1028 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1031 /* FIXME should we set, don't need to call stat() after:
1033 ret.st_errno = EACCES;
1035 ret.m_name = dir->d_m_name;
1036 ret.u_name = dir->d_u_name;
1039 } else if (afp_errno == AFPERR_NOOBJ) {
1040 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1043 strcpy(path, dir->d_m_name);
1044 if (dir->d_m_name == dir->d_u_name) {
1048 size_t tp = strlen(path)+1;
1050 strcpy(path +tp, dir->d_u_name);
1051 ret.u_name = path +tp;
1054 /* FIXME should we set :
1056 ret.st_errno = ENOENT;
1059 dir_invalidate(vol, dir);
1062 dir_invalidate(vol, dir);
1065 if (*path == '\0') {
1070 } /* if (len == 0) */
1072 if (*data == sep ) {
1076 while (*data == sep && len > 0 ) {
1077 if ( dir->d_parent == NULL ) {
1080 dir = dir->d_parent;
1085 /* would this be faster with strlen + strncpy? */
1087 while ( *data != sep && len > 0 ) {
1089 if (p > &path[ MAXPATHLEN]) {
1090 afp_errno = AFPERR_PARAM;
1096 /* short cut bits by chopping off a trailing \0. this also
1097 makes the traversal happy w/ filenames at the end of the
1104 if ( p != path ) { /* we got something */
1106 if (afp_version >= 30) {
1107 /* check for OS X mangled filename :( */
1111 t = demangle_osx(vol, path, dir->d_did, &fileid);
1114 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1115 * flags weren't the same
1117 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1118 /* at last got our view of mac name */
1124 cdir = dir->d_child;
1126 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1129 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1132 if ( cdir == NULL ) {
1134 /* if dir == curdir it always succeed,
1135 even if curdir is deleted.
1136 it's not a pb because it will fail in extenddir
1138 if ( movecwd( vol, dir ) < 0 ) {
1139 /* dir is not valid anymore
1140 we delete dir from the cache and abort.
1142 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1143 afp_errno = AFPERR_NOOBJ;
1146 if (afp_errno == AFPERR_ACCESS)
1148 dir_invalidate(vol, dir);
1151 cdir = extenddir( vol, dir, &ret );
1155 cdir = extenddir( vol, dir, &ret );
1156 } /* if (!extend) */
1158 if ( cdir == NULL ) {
1160 if ( len > 0 || !ret.u_name) {
1168 } /* if (p != path) */
1173 * Move curdir to dir, with a possible chdir()
1175 int movecwd( vol, dir)
1176 const struct vol *vol;
1179 char path[MAXPATHLEN + 1];
1184 if ( dir == curdir ) {
1187 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1188 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1192 p = path + sizeof(path) - 1;
1195 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1198 /* parent directory is deleted */
1199 afp_errno = AFPERR_NOOBJ;
1203 if (p -n -1 < path) {
1204 afp_errno = AFPERR_PARAM;
1211 if ( d != curdir ) {
1212 n = strlen( vol->v_path );
1213 if (p -n -1 < path) {
1214 afp_errno = AFPERR_PARAM;
1219 memcpy( p, vol->v_path, n );
1221 if ( chdir( p ) < 0 ) {
1225 afp_errno = AFPERR_ACCESS;
1228 afp_errno = AFPERR_NOOBJ;
1238 * We can't use unix file's perm to support Apple's inherited protection modes.
1239 * If we aren't the file's owner we can't change its perms when moving it and smb
1240 * nfs,... don't even try.
1242 #define AFP_CHECK_ACCESS
1244 int check_access(char *path, int mode)
1246 #ifdef AFP_CHECK_ACCESS
1254 accessmode(p, &ma, curdir, NULL);
1255 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1257 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1263 /* --------------------- */
1264 int file_access(struct path *path, int mode)
1268 accessmode(path->u_name, &ma, curdir, &path->st);
1269 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1271 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1277 /* ------------------------------
1279 (name, dir) with curdir:name == dir, from afp_enumerate
1282 int getdirparams(const struct vol *vol,
1283 u_int16_t bitmap, struct path *s_path,
1285 char *buf, int *buflen )
1289 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1290 int bit = 0, isad = 0;
1296 struct stat *st = &s_path->st;
1297 char *upath = s_path->u_name;
1299 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1300 (1 << DIRPBIT_CDATE) |
1301 (1 << DIRPBIT_MDATE) |
1302 (1 << DIRPBIT_BDATE) |
1303 (1 << DIRPBIT_FINFO)))) {
1305 ad_init(&ad, vol->v_adouble);
1306 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1311 if ( dir->d_did == DIRDID_ROOT) {
1312 pdid = DIRDID_ROOT_PARENT;
1313 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1316 pdid = dir->d_parent->d_did;
1320 while ( bitmap != 0 ) {
1321 while (( bitmap & 1 ) == 0 ) {
1329 ad_getattr(&ad, &ashort);
1330 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1331 && strcmp(dir->d_u_name, "..")) {
1332 ashort = htons(ATTRBIT_INVISIBLE);
1335 ashort |= htons(ATTRBIT_SHARED);
1336 memcpy( data, &ashort, sizeof( ashort ));
1337 data += sizeof( ashort );
1341 memcpy( data, &pdid, sizeof( pdid ));
1342 data += sizeof( pdid );
1345 case DIRPBIT_CDATE :
1346 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1347 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1348 memcpy( data, &aint, sizeof( aint ));
1349 data += sizeof( aint );
1352 case DIRPBIT_MDATE :
1353 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1354 memcpy( data, &aint, sizeof( aint ));
1355 data += sizeof( aint );
1358 case DIRPBIT_BDATE :
1359 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1360 aint = AD_DATE_START;
1361 memcpy( data, &aint, sizeof( aint ));
1362 data += sizeof( aint );
1365 case DIRPBIT_FINFO :
1367 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1368 } else { /* no appledouble */
1369 memset( data, 0, 32 );
1370 /* set default view -- this also gets done in ad_open() */
1371 ashort = htons(FINDERINFO_CLOSEDVIEW);
1372 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1374 /* dot files are by default invisible */
1375 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1376 strcmp(dir->d_u_name , "..")) {
1377 ashort = htons(FINDERINFO_INVISIBLE);
1378 memcpy(data + FINDERINFO_FRFLAGOFF,
1379 &ashort, sizeof(ashort));
1385 case DIRPBIT_LNAME :
1386 if (dir->d_m_name) /* root of parent can have a null name */
1389 memset(data, 0, sizeof(u_int16_t));
1390 data += sizeof( u_int16_t );
1393 case DIRPBIT_SNAME :
1394 memset(data, 0, sizeof(u_int16_t));
1395 data += sizeof( u_int16_t );
1399 memcpy( data, &dir->d_did, sizeof( aint ));
1400 data += sizeof( aint );
1403 case DIRPBIT_OFFCNT :
1405 /* this needs to handle current directory access rights */
1406 if (st->st_ctime == dir->ctime) {
1407 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1409 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1411 dir->ctime = st->st_ctime;
1412 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1414 ashort = htons( ashort );
1415 memcpy( data, &ashort, sizeof( ashort ));
1416 data += sizeof( ashort );
1420 aint = htonl(st->st_uid);
1421 memcpy( data, &aint, sizeof( aint ));
1422 data += sizeof( aint );
1426 aint = htonl(st->st_gid);
1427 memcpy( data, &aint, sizeof( aint ));
1428 data += sizeof( aint );
1431 case DIRPBIT_ACCESS :
1432 accessmode( upath, &ma, dir , st);
1434 *data++ = ma.ma_user;
1435 *data++ = ma.ma_world;
1436 *data++ = ma.ma_group;
1437 *data++ = ma.ma_owner;
1440 /* Client has requested the ProDOS information block.
1441 Just pass back the same basic block for all
1442 directories. <shirsch@ibm.net> */
1443 case DIRPBIT_PDINFO :
1444 if (afp_version >= 30) { /* UTF8 name */
1445 utf8 = kTextEncodingUTF8;
1446 if (dir->d_m_name) /* root of parent can have a null name */
1449 memset(data, 0, sizeof(u_int16_t));
1450 data += sizeof( u_int16_t );
1452 memcpy(data, &aint, sizeof( aint ));
1453 data += sizeof( aint );
1455 else { /* ProDOS Info Block */
1458 ashort = htons( 0x0200 );
1459 memcpy( data, &ashort, sizeof( ashort ));
1460 data += sizeof( ashort );
1461 memset( data, 0, sizeof( ashort ));
1462 data += sizeof( ashort );
1466 case DIRPBIT_UNIXPR :
1467 aint = htonl(st->st_uid);
1468 memcpy( data, &aint, sizeof( aint ));
1469 data += sizeof( aint );
1470 aint = htonl(st->st_gid);
1471 memcpy( data, &aint, sizeof( aint ));
1472 data += sizeof( aint );
1475 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1476 memcpy( data, &aint, sizeof( aint ));
1477 data += sizeof( aint );
1479 accessmode( upath, &ma, dir , st);
1481 *data++ = ma.ma_user;
1482 *data++ = ma.ma_world;
1483 *data++ = ma.ma_group;
1484 *data++ = ma.ma_owner;
1489 ad_close( &ad, ADFLAGS_HF );
1491 return( AFPERR_BITMAP );
1497 ashort = htons( data - buf );
1498 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1499 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1501 if ( utf_nameoff ) {
1502 ashort = htons( data - buf );
1503 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1504 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1507 ad_close( &ad, ADFLAGS_HF );
1509 *buflen = data - buf;
1513 /* ----------------------------- */
1514 int path_error(struct path *path, int error)
1516 /* - a dir with access error
1517 * - no error it's a file
1520 if (path_isadir(path))
1522 if (path->st_valid && path->st_errno)
1524 return AFPERR_BADTYPE ;
1527 /* ----------------------------- */
1528 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1531 int ibuflen, *rbuflen;
1536 u_int16_t vid, bitmap;
1542 memcpy( &vid, ibuf, sizeof( vid ));
1543 ibuf += sizeof( vid );
1545 if (NULL == ( vol = getvolbyvid( vid )) ) {
1546 return( AFPERR_PARAM );
1549 if (vol->v_flags & AFPVOL_RO)
1550 return AFPERR_VLOCK;
1552 memcpy( &did, ibuf, sizeof( did ));
1553 ibuf += sizeof( int );
1555 if (NULL == ( dir = dirlookup( vol, did )) ) {
1559 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1560 bitmap = ntohs( bitmap );
1561 ibuf += sizeof( bitmap );
1563 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1564 return get_afp_errno(AFPERR_NOOBJ);
1567 if ( *path->m_name != '\0' ) {
1568 rc = path_error(path, AFPERR_NOOBJ);
1569 /* maybe we are trying to set perms back */
1570 if (rc != AFPERR_ACCESS)
1575 * If ibuf is odd, make it even.
1577 if ((u_long)ibuf & 1 ) {
1581 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1582 setvoltime(obj, vol );
1588 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1590 * assume path == '\0' eg. it's a directory in canonical form
1593 struct path Cur_Path = {
1596 ".", /* unix name */
1597 0, /* stat is not set */
1601 /* ------------------ */
1602 static int set_dir_errors(struct path *path, const char *where, int err)
1607 return AFPERR_ACCESS;
1609 return AFPERR_VLOCK;
1611 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1612 return AFPERR_PARAM;
1615 /* ------------------ */
1616 int setdirparams(const struct vol *vol,
1617 struct path *path, u_int16_t d_bitmap, char *buf )
1626 int bit, aint, isad = 1;
1629 u_int16_t ashort, bshort;
1631 int change_mdate = 0;
1632 int change_parent_mdate = 0;
1634 u_int16_t bitmap = d_bitmap;
1635 u_char finder_buf[32];
1638 u_int16_t upriv_bit = 0;
1641 upath = path->u_name;
1643 while ( bitmap != 0 ) {
1644 while (( bitmap & 1 ) == 0 ) {
1652 memcpy( &ashort, buf, sizeof( ashort ));
1653 buf += sizeof( ashort );
1655 case DIRPBIT_CDATE :
1657 memcpy(&cdate, buf, sizeof(cdate));
1658 buf += sizeof( cdate );
1660 case DIRPBIT_MDATE :
1661 memcpy(&newdate, buf, sizeof(newdate));
1662 buf += sizeof( newdate );
1664 case DIRPBIT_BDATE :
1666 memcpy(&bdate, buf, sizeof(bdate));
1667 buf += sizeof( bdate );
1669 case DIRPBIT_FINFO :
1671 memcpy( finder_buf, buf, 32 );
1674 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1675 change_parent_mdate = 1;
1676 memcpy( &owner, buf, sizeof(owner));
1677 buf += sizeof( owner );
1680 change_parent_mdate = 1;
1681 memcpy( &group, buf, sizeof( group ));
1682 buf += sizeof( group );
1684 case DIRPBIT_ACCESS :
1686 change_parent_mdate = 1;
1687 ma.ma_user = *buf++;
1688 ma.ma_world = *buf++;
1689 ma.ma_group = *buf++;
1690 ma.ma_owner = *buf++;
1691 mpriv = mtoumode( &ma );
1692 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1693 err = set_dir_errors(path, "setdirmode", errno);
1697 /* Ignore what the client thinks we should do to the
1698 ProDOS information block. Skip over the data and
1699 report nothing amiss. <shirsch@ibm.net> */
1700 case DIRPBIT_PDINFO :
1701 if (afp_version < 30) {
1705 err = AFPERR_BITMAP;
1709 case DIRPBIT_UNIXPR :
1710 if (vol_unix_priv(vol)) {
1711 /* Skip UID and GID for now, there seems to be no way to set them from an OSX client anyway */
1712 buf += sizeof( aint );
1713 buf += sizeof( aint );
1716 change_parent_mdate = 1;
1717 memcpy( &upriv, buf, sizeof( upriv ));
1718 buf += sizeof( upriv );
1719 upriv = ntohl (upriv);
1720 if (dir_rx_set(upriv)) {
1721 /* maybe we are trying to set perms back */
1722 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1724 err = set_dir_errors(path, "setdirmode", errno);
1735 err = AFPERR_BITMAP;
1743 ad_init(&ad, vol->v_adouble);
1745 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1746 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1748 * Check to see what we're trying to set. If it's anything
1749 * but ACCESS, UID, or GID, give an error. If it's any of those
1750 * three, we don't need the ad to be open, so just continue.
1752 * note: we also don't need to worry about mdate. also, be quiet
1753 * if we're using the noadouble option.
1755 if (!vol_noadouble(vol) && (d_bitmap &
1756 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1757 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1758 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1759 return AFPERR_ACCESS;
1765 * Check to see if a create was necessary. If it was, we'll want
1766 * to set our name, etc.
1768 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1769 ad_setname(&ad, curdir->d_m_name);
1775 while ( bitmap != 0 ) {
1776 while (( bitmap & 1 ) == 0 ) {
1784 ad_getattr(&ad, &bshort);
1785 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1786 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1787 change_parent_mdate = 1;
1788 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1789 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1793 ad_setattr(&ad, bshort);
1796 case DIRPBIT_CDATE :
1798 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1801 case DIRPBIT_MDATE :
1803 case DIRPBIT_BDATE :
1805 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1808 case DIRPBIT_FINFO :
1810 if ( dir->d_did == DIRDID_ROOT ) {
1812 * Alright, we admit it, this is *really* sick!
1813 * The 4 bytes that we don't copy, when we're dealing
1814 * with the root of a volume, are the directory's
1815 * location information. This eliminates that annoying
1816 * behavior one sees when mounting above another mount
1819 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1820 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1822 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1826 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1827 if ( (dir->d_did == DIRDID_ROOT) &&
1828 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1829 err = set_dir_errors(path, "setdeskowner", errno);
1830 if (isad && err == AFPERR_PARAM) {
1831 err = AFP_OK; /* ???*/
1834 goto setdirparam_done;
1837 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1838 err = set_dir_errors(path, "setdirowner", errno);
1839 goto setdirparam_done;
1844 if (dir->d_did == DIRDID_ROOT)
1845 setdeskowner( -1, ntohl(group) );
1847 #if 0 /* don't error if we can't set the desktop owner. */
1848 err = set_dir_errors(path, "setdeskowner", errno);
1849 if (isad && err == AFPERR_PARAM) {
1850 err = AFP_OK; /* ???*/
1853 goto setdirparam_done;
1857 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1858 err = set_dir_errors(path, "setdirowner", errno);
1859 goto setdirparam_done;
1863 case DIRPBIT_ACCESS :
1864 if (dir->d_did == DIRDID_ROOT) {
1866 if (!dir_rx_set(mpriv)) {
1867 /* we can't remove read and search for owner on volume root */
1868 err = AFPERR_ACCESS;
1869 goto setdirparam_done;
1873 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1874 err = set_dir_errors(path, "setdirmode", errno);
1875 goto setdirparam_done;
1878 case DIRPBIT_PDINFO :
1879 if (afp_version >= 30) {
1880 err = AFPERR_BITMAP;
1881 goto setdirparam_done;
1884 case DIRPBIT_UNIXPR :
1885 if (vol_unix_priv(vol)) {
1886 if (dir->d_did == DIRDID_ROOT) {
1887 setdeskmode( upriv );
1888 if (!dir_rx_set(upriv)) {
1889 /* we can't remove read and search for owner on volume root */
1890 err = AFPERR_ACCESS;
1891 goto setdirparam_done;
1895 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1896 err = set_dir_errors(path, "setdirmode", errno);
1897 goto setdirparam_done;
1903 err = AFPERR_BITMAP;
1904 goto setdirparam_done;
1913 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1914 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1918 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1919 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1924 if (path->st_valid && !path->st_errno) {
1925 struct stat *st = &path->st;
1927 if (dir && dir->d_parent) {
1928 ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev,
1929 st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
1932 ad_flush( &ad, ADFLAGS_HF );
1933 ad_close( &ad, ADFLAGS_HF );
1936 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1937 && gettimeofday(&tv, NULL) == 0) {
1938 if (!movecwd(vol, dir->d_parent)) {
1939 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1940 /* be careful with bitmap because now dir is null */
1941 bitmap = 1<<DIRPBIT_MDATE;
1942 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1943 /* should we reset curdir ?*/
1950 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1953 int ibuflen, *rbuflen;
1959 struct path *s_path;
1967 memcpy( &vid, ibuf, sizeof( vid ));
1968 ibuf += sizeof( vid );
1969 if (NULL == ( vol = getvolbyvid( vid )) ) {
1970 return( AFPERR_PARAM );
1973 if (vol->v_flags & AFPVOL_RO)
1974 return AFPERR_VLOCK;
1976 memcpy( &did, ibuf, sizeof( did ));
1977 ibuf += sizeof( did );
1978 if (NULL == ( dir = dirlookup( vol, did )) ) {
1979 return afp_errno; /* was AFPERR_NOOBJ */
1981 /* for concurrent access we need to be sure we are not in the
1982 * folder we want to create...
1986 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1987 return get_afp_errno(AFPERR_PARAM);
1989 /* cname was able to move curdir to it! */
1990 if (*s_path->m_name == '\0')
1991 return AFPERR_EXIST;
1993 upath = s_path->u_name;
1994 if (0 != (err = check_name(vol, upath))) {
1998 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2002 if (of_stat(s_path) < 0) {
2006 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2010 if ( movecwd( vol, dir ) < 0 ) {
2011 return( AFPERR_PARAM );
2014 ad_init(&ad, vol->v_adouble);
2015 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2016 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2017 if (vol_noadouble(vol))
2018 goto createdir_done;
2019 return( AFPERR_ACCESS );
2021 ad_setname(&ad, s_path->m_name);
2022 ad_setid( &ad, (vol->v_flags & AFPVOL_NODEV)?0:s_path->st.st_dev,
2023 s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2025 ad_flush( &ad, ADFLAGS_HF );
2026 ad_close( &ad, ADFLAGS_HF );
2029 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2030 *rbuflen = sizeof( u_int32_t );
2031 setvoltime(obj, vol );
2036 * dst new unix filename (not a pathname)
2037 * newname new mac name
2041 int renamedir(vol, src, dst, dir, newparent, newname)
2042 const struct vol *vol;
2043 char *src, *dst, *newname;
2044 struct dir *dir, *newparent;
2051 /* existence check moved to afp_moveandrename */
2052 if ( unix_rename( src, dst ) < 0 ) {
2055 return( AFPERR_NOOBJ );
2057 return( AFPERR_ACCESS );
2059 return AFPERR_VLOCK;
2061 /* tried to move directory into a subdirectory of itself */
2062 return AFPERR_CANTMOVE;
2064 /* this needs to copy and delete. bleah. that means we have
2065 * to deal with entire directory hierarchies. */
2066 if ((err = copydir(vol, src, dst)) < 0) {
2070 if ((err = deletedir(src)) < 0)
2074 return( AFPERR_PARAM );
2078 if (vol->v_adouble == AD_VERSION2_OSX) {
2079 /* We simply move the corresponding ad file as well */
2080 char tempbuf[258]="._";
2081 rename(vol->ad_path(src,0),strcat(tempbuf,dst));
2084 len = strlen( newname );
2085 /* rename() succeeded so we need to update our tree even if we can't open
2089 ad_init(&ad, vol->v_adouble);
2091 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2092 ad_setname(&ad, newname);
2093 ad_flush( &ad, ADFLAGS_HF );
2094 ad_close( &ad, ADFLAGS_HF );
2097 if (dir->d_m_name == dir->d_u_name)
2098 dir->d_u_name = NULL;
2100 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2101 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2102 /* FIXME : fatal ? */
2105 dir->d_m_name = buf;
2106 strcpy( dir->d_m_name, newname );
2108 if (newname == dst) {
2109 free(dir->d_u_name);
2110 dir->d_u_name = dir->d_m_name;
2113 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2114 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2117 dir->d_u_name = buf;
2118 strcpy( dir->d_u_name, dst );
2121 if (( parent = dir->d_parent ) == NULL ) {
2124 if ( parent == newparent ) {
2128 /* detach from old parent and add to new one. */
2129 dirchildremove(parent, dir);
2130 dir->d_parent = newparent;
2131 dirchildadd(newparent, dir);
2135 #define DOT_APPLEDOUBLE_LEN 13
2136 /* delete an empty directory */
2137 int deletecurdir( vol, path, pathlen )
2138 const struct vol *vol;
2150 if ( curdir->d_parent == NULL ) {
2151 return( AFPERR_ACCESS );
2156 ad_init(&ad, vol->v_adouble);
2157 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2159 ad_getattr(&ad, &ashort);
2160 ad_close( &ad, ADFLAGS_HF );
2161 if ((ashort & htons(ATTRBIT_NODELETE))) {
2162 return AFPERR_OLOCK;
2166 if (vol->v_adouble == AD_VERSION2_OSX) {
2168 if ((err = netatalk_unlink(vol->ad_path(".",0) )) ) {
2173 /* delete stray .AppleDouble files. this happens to get .Parent files
2175 if ((dp = opendir(".AppleDouble"))) {
2176 strcpy(path, ".AppleDouble/");
2177 while ((de = readdir(dp))) {
2178 /* skip this and previous directory */
2179 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2182 /* bail if the file exists in the current directory.
2183 * note: this will not fail with dangling symlinks */
2184 if (stat(de->d_name, &st) == 0) {
2186 return AFPERR_DIRNEMPT;
2189 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2190 if ((err = netatalk_unlink(path))) {
2198 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2203 /* now get rid of dangling symlinks */
2204 if ((dp = opendir("."))) {
2205 while ((de = readdir(dp))) {
2206 /* skip this and previous directory */
2207 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2210 /* bail if it's not a symlink */
2211 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2213 return AFPERR_DIRNEMPT;
2216 if ((err = netatalk_unlink(de->d_name))) {
2223 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2228 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2229 dirchildremove(curdir, fdir);
2230 cnid_delete(vol->v_cdb, fdir->d_did);
2231 dir_remove( vol, fdir );
2236 /* inode is used as key for cnid.
2237 * Close the descriptor only after cnid_delete
2245 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2248 int ibuflen, *rbuflen;
2258 sfunc = (unsigned char) *ibuf++;
2259 memcpy( &id, ibuf, sizeof( id ));
2264 if (sfunc == 3 || sfunc == 4) {
2265 if (afp_version < 30) {
2266 return( AFPERR_PARAM );
2273 case 3 :/* unicode */
2274 if (( pw = getpwuid( id )) == NULL ) {
2275 return( AFPERR_NOITEM );
2277 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2278 pw->pw_name, strlen(pw->pw_name), &name);
2282 case 4 : /* unicode */
2283 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2284 return( AFPERR_NOITEM );
2286 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2287 gr->gr_name, strlen(gr->gr_name), &name);
2291 return( AFPERR_PARAM );
2293 len = strlen( name );
2300 u_int16_t tp = htons(len);
2301 memcpy(rbuf, &tp, sizeof(tp));
2310 memcpy( rbuf, name, len );
2318 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2321 int ibuflen, *rbuflen;
2330 sfunc = (unsigned char) *ibuf++;
2334 case 2 : /* unicode */
2335 if (afp_version < 30) {
2336 return( AFPERR_PARAM );
2338 memcpy(&ulen, ibuf, sizeof(ulen));
2344 len = (unsigned char) *ibuf++;
2347 return( AFPERR_PARAM );
2354 case 1 : /* unicode */
2356 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2357 return( AFPERR_NOITEM );
2362 case 2 : /* unicode */
2364 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2365 return( AFPERR_NOITEM );
2374 memcpy( rbuf, &id, sizeof( id ));
2375 *rbuflen = sizeof( id );
2379 /* ------------------------------------
2380 variable DID support
2382 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2385 int ibuflen, *rbuflen;
2396 /* do nothing as dids are static for the life of the process. */
2400 memcpy(&vid, ibuf, sizeof( vid ));
2401 ibuf += sizeof( vid );
2402 if (( vol = getvolbyvid( vid )) == NULL ) {
2403 return( AFPERR_PARAM );
2406 memcpy( &did, ibuf, sizeof( did ));
2407 ibuf += sizeof( did );
2408 if (( dir = dirlookup( vol, did )) == NULL ) {
2409 return( AFPERR_PARAM );
2412 /* dir_remove -- deletedid */
2418 /* did creation gets done automatically
2419 * there's a pb again with case but move it to cname
2421 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2424 int ibuflen, *rbuflen;
2427 struct dir *parentdir;
2435 memcpy(&vid, ibuf, sizeof(vid));
2436 ibuf += sizeof( vid );
2438 if (NULL == ( vol = getvolbyvid( vid )) ) {
2439 return( AFPERR_PARAM );
2442 memcpy(&did, ibuf, sizeof(did));
2443 ibuf += sizeof(did);
2445 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2449 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2450 return get_afp_errno(AFPERR_PARAM);
2453 if ( *path->m_name != '\0' ) {
2454 return path_error(path, AFPERR_NOOBJ);
2457 if ( !path->st_valid && of_stat(path ) < 0 ) {
2458 return( AFPERR_NOOBJ );
2460 if ( path->st_errno ) {
2461 return( AFPERR_NOOBJ );
2464 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2465 *rbuflen = sizeof(curdir->d_did);