2 * $Id: directory.c,v 1.71.2.4.2.15.2.13 2009-09-01 12:34:38 franklahm 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( const struct vol *vol _U_, struct dir *dir)
398 struct ofork *of, *last;
399 struct dir *node, *leaf;
400 #endif /* REMOVE_NODES */
402 if (!dir || (dir == SENTINEL))
405 /* i'm not sure if it really helps to delete stuff. */
408 dir->d_m_name = NULL;
409 dir->d_u_name = NULL;
410 #else /* ! REMOVE_NODES */
412 /* go searching for a node with at most one child */
413 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
417 while (node->d_left != SENTINEL)
422 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
425 leaf->d_back = node->d_back;
428 } else if (node == node->d_back->d_left) { /* left tree */
429 node->d_back->d_left = leaf;
431 node->d_back->d_right = leaf;
434 /* we want to free node, but we also want to free the data in dir.
435 * currently, that's d_name and the directory traversal bits.
436 * we just copy the necessary bits and then fix up all the
437 * various pointers to the directory. needless to say, there are
438 * a bunch of places that store the directory struct. */
440 struct dir save, *tmp;
442 memcpy(&save, dir, sizeof(save));
443 memcpy(dir, node, sizeof(struct dir));
445 /* restore the red-black bits */
446 dir->d_left = save.d_left;
447 dir->d_right = save.d_right;
448 dir->d_back = save.d_back;
449 dir->d_color = save.d_color;
451 if (node == vol->v_dir) {/* we may need to fix up this pointer */
453 rootpar.d_child = vol->v_dir;
455 /* if we aren't the root directory, we have parents and
456 * siblings to worry about */
457 if (dir->d_parent->d_child == node)
458 dir->d_parent->d_child = dir;
459 dir->d_next->d_prev = dir;
460 dir->d_prev->d_next = dir;
463 /* fix up children. */
467 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
470 if (node == curdir) /* another pointer to fixup */
473 /* we also need to fix up oforks. bleah */
474 if ((of = dir->d_ofork)) {
475 last = of->of_d_prev;
478 of = (last == of) ? NULL : of->of_d_next;
482 /* set the node's d_name */
483 node->d_m_name = save.d_m_name;
484 node->d_u_name = save.d_u_name;
487 if (node->d_color == DIRTREE_COLOR_BLACK)
488 dir_rmrecolor(vol, leaf);
490 if (node->d_u_name != node->d_m_name) {
491 free(node->d_u_name);
493 free(node->d_m_name);
495 #endif /* ! REMOVE_NODES */
498 /* ---------------------------------------
499 * remove the node and its childs from the tree
501 * FIXME what about opened forks with refs to it?
502 * it's an afp specs violation because you can't delete
503 * an opened forks. Now afpd doesn't care about forks opened by other
504 * process. It's fixable within afpd if fnctl_lock, doable with smb and
505 * next to impossible for nfs and local filesystem access.
507 static void dir_invalidate( vol, dir )
508 const struct vol *vol;
512 /* v_root can't be deleted */
513 if (movecwd(vol, vol->v_root) < 0) {
514 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
518 dirchildremove(dir->d_parent, dir);
519 dir_remove( vol, dir );
522 /* ------------------------------------ */
523 static struct dir *dir_insert(vol, dir)
524 const struct vol *vol;
530 while (pdir->d_did != dir->d_did ) {
531 if ( pdir->d_did > dir->d_did ) {
532 if ( pdir->d_left == SENTINEL ) {
539 if ( pdir->d_right == SENTINEL ) {
544 pdir = pdir->d_right;
552 * attempt to extend the current dir. tree to include path
553 * as a side-effect, movecwd to that point and return the new dir
556 extenddir( vol, dir, path )
563 if ( path->u_name == NULL) {
564 path->u_name = mtoupath(vol, path->m_name, dir->d_did, (path->m_type==3) );
568 if ( path->u_name == NULL) {
569 afp_errno = AFPERR_PARAM;
573 if (check_name(vol, path->u_name)) {
574 /* the name is illegal */
575 LOG(log_info, logtype_afpd, "extenddir: illegal path: '%s'", path->u_name);
577 afp_errno = AFPERR_PARAM;
581 if (of_stat( path ) != 0 ) {
585 if (!S_ISDIR(path->st.st_mode)) {
589 /* FIXME: if this is an AFP3 connection and path->m_type != 3 we might screw dircache here */
590 if ( utf8_encoding() && path->m_type != 3)
592 save_m_name = path->m_name;
594 if (( dir = adddir( vol, dir, path)) == NULL ) {
598 path->m_name = save_m_name;
599 LOG(log_debug, logtype_afpd, "AFP3 connection, mismatch in mtype: %u, unix: %s, mac: %s",
600 path->m_type, path->u_name, path->m_name);
603 if (( dir = adddir( vol, dir, path)) == NULL ) {
609 if ( movecwd( vol, dir ) < 0 ) {
616 /* -------------------
617 system rmdir with afp error code.
618 ENOENT is not an error.
620 static int netatalk_rmdir(const char *name)
622 if (rmdir(name) < 0) {
627 return AFPERR_DIRNEMPT;
630 return AFPERR_ACCESS;
640 /* -------------------------
641 appledouble mkdir afp error code.
643 static int netatalk_mkdir(const char *name)
645 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
648 return( AFPERR_NOOBJ );
650 return( AFPERR_VLOCK );
653 return( AFPERR_ACCESS );
655 return( AFPERR_EXIST );
658 return( AFPERR_DFULL );
660 return( AFPERR_PARAM );
666 /* -------------------
667 system unlink with afp error code.
668 ENOENT is not an error.
670 int netatalk_unlink(const char *name)
672 if (unlink(name) < 0) {
680 return AFPERR_ACCESS;
688 /* ------------------- */
689 static int deletedir(char *dir)
691 char path[MAXPATHLEN + 1];
699 if ((len = strlen(dir)) +2 > sizeof(path))
703 if ((dp = opendir(dir)) == NULL)
709 remain = sizeof(path) -len -1;
710 while ((de = readdir(dp)) && err == AFP_OK) {
711 /* skip this and previous directory */
712 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
715 if (strlen(de->d_name) > remain) {
719 strcpy(path + len, de->d_name);
720 if (stat(path, &st)) {
723 if (S_ISDIR(st.st_mode)) {
724 err = deletedir(path);
726 err = netatalk_unlink(path);
731 /* okay. the directory is empty. delete it. note: we already got rid
734 err = netatalk_rmdir(dir);
739 /* do a recursive copy. */
740 static int copydir(const struct vol *vol, char *src, char *dst)
742 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
751 /* doesn't exist or the path is too long. */
752 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
753 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
754 ((dp = opendir(src)) == NULL))
757 /* try to create the destination directory */
758 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
763 /* set things up to copy */
767 srem = sizeof(spath) - slen -1;
772 drem = sizeof(dpath) - dlen -1;
775 while ((de = readdir(dp))) {
776 /* skip this and previous directory */
777 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
780 if (strlen(de->d_name) > srem) {
784 strcpy(spath + slen, de->d_name);
786 if (stat(spath, &st) == 0) {
787 if (strlen(de->d_name) > drem) {
791 strcpy(dpath + dlen, de->d_name);
793 if (S_ISDIR(st.st_mode)) {
794 if (AFP_OK != (err = copydir(vol, spath, dpath)))
796 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
800 /* keep the same time stamp. */
801 ut.actime = ut.modtime = st.st_mtime;
807 /* keep the same time stamp. */
808 if (stat(src, &st) == 0) {
809 ut.actime = ut.modtime = st.st_mtime;
819 /* --- public functions follow --- */
821 /* NOTE: we start off with at least one node (the root directory). */
822 struct dir *dirinsert( vol, dir )
828 if ((node = dir_insert(vol, dir)))
831 /* recolor the tree. the current node is red. */
832 dir->d_color = DIRTREE_COLOR_RED;
834 /* parent of this node has to be black. if the parent node
835 * is red, then we have a grandparent. */
836 while ((dir != vol->v_root) &&
837 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
838 /* are we on the left tree? */
839 if (dir->d_back == dir->d_back->d_back->d_left) {
840 node = dir->d_back->d_back->d_right; /* get the right node */
841 if (node->d_color == DIRTREE_COLOR_RED) {
842 /* we're red. we need to change to black. */
843 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
844 node->d_color = DIRTREE_COLOR_BLACK;
845 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
846 dir = dir->d_back->d_back; /* finished. go up. */
848 if (dir == dir->d_back->d_right) {
850 dir_leftrotate(vol, dir);
852 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
853 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
854 dir_rightrotate(vol, dir->d_back->d_back);
857 node = dir->d_back->d_back->d_left;
858 if (node->d_color == DIRTREE_COLOR_RED) {
859 /* we're red. we need to change to black. */
860 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
861 node->d_color = DIRTREE_COLOR_BLACK;
862 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
863 dir = dir->d_back->d_back; /* finished. ascend */
865 if (dir == dir->d_back->d_left) {
867 dir_rightrotate(vol, dir);
869 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
870 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
871 dir_leftrotate(vol, dir->d_back->d_back);
876 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
880 /* free everything down. we don't bother to recolor as this is only
881 * called to free the entire tree */
882 void dirfreename(struct dir *dir)
884 if (dir->d_u_name != dir->d_m_name) {
893 if (!dir || (dir == SENTINEL))
896 if ( dir->d_left != SENTINEL ) {
897 dirfree( dir->d_left );
899 if ( dir->d_right != SENTINEL ) {
900 dirfree( dir->d_right );
903 if (dir != SENTINEL) {
909 /* --------------------------------------------
910 * most of the time mac name and unix name are the same
912 struct dir *dirnew(const char *m_name, const char *u_name)
916 dir = (struct dir *) calloc(1, sizeof( struct dir ));
920 if ((dir->d_m_name = strdup(m_name)) == NULL) {
925 if (m_name == u_name) {
926 dir->d_u_name = dir->d_m_name;
928 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
934 dir->d_left = dir->d_right = SENTINEL;
935 dir->d_next = dir->d_prev = dir;
939 /* -------------------------------------------------- */
945 stat the file or errno
948 curdir: filename parent directory
954 stat the dir or errno
958 curdir: dir parent directory
966 curdir: dir parent directory
973 cname( vol, dir, cpath )
974 const struct vol *vol;
979 static char path[ MAXPATHLEN + 1];
980 static struct path ret;
992 afp_errno = AFPERR_NOOBJ;
993 switch (ret.m_type = *data) { /* path type */
996 len = (unsigned char) *data++;
999 if (afp_version >= 30) {
1005 if (afp_version >= 30) {
1007 memcpy(&hint, data, sizeof(hint));
1009 data += sizeof(hint);
1011 memcpy(&len16, data, sizeof(len16));
1018 /* else it's an error */
1020 afp_errno = AFPERR_PARAM;
1023 *cpath += len + size;
1031 if (movecwd( vol, dir ) < 0 ) {
1033 movecwd failed some of dir path are not there anymore.
1034 FIXME Is it true with other errors?
1035 so we remove dir from the cache
1037 if (dir->d_did == DIRDID_ROOT_PARENT)
1039 if (afp_errno == AFPERR_ACCESS) {
1040 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1043 /* FIXME should we set, don't need to call stat() after:
1045 ret.st_errno = EACCES;
1047 ret.m_name = dir->d_m_name;
1048 ret.u_name = dir->d_u_name;
1051 } else if (afp_errno == AFPERR_NOOBJ) {
1052 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1055 strcpy(path, dir->d_m_name);
1056 if (dir->d_m_name == dir->d_u_name) {
1060 size_t tp = strlen(path)+1;
1062 strcpy(path +tp, dir->d_u_name);
1063 ret.u_name = path +tp;
1066 /* FIXME should we set :
1068 ret.st_errno = ENOENT;
1071 dir_invalidate(vol, dir);
1074 dir_invalidate(vol, dir);
1077 if (*path == '\0') {
1082 } /* if (len == 0) */
1084 if (*data == sep ) {
1088 while (*data == sep && len > 0 ) {
1089 if ( dir->d_parent == NULL ) {
1092 dir = dir->d_parent;
1097 /* would this be faster with strlen + strncpy? */
1099 while ( *data != sep && len > 0 ) {
1101 if (p > &path[ MAXPATHLEN]) {
1102 afp_errno = AFPERR_PARAM;
1108 /* short cut bits by chopping off a trailing \0. this also
1109 makes the traversal happy w/ filenames at the end of the
1116 if ( p != path ) { /* we got something */
1118 if (afp_version >= 30) {
1123 static char temp[ MAXPATHLEN + 1];
1125 /* not an UTF8 name */
1126 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1127 afp_errno = AFPERR_PARAM;
1132 /* check for OS X mangled filename :( */
1134 t = demangle_osx(vol, path, dir->d_did, &fileid);
1137 /* duplicate work but we can't reuse all convert_char we did in demagnle_osx
1138 * flags weren't the same
1140 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1141 /* at last got our view of mac name */
1147 cdir = dir->d_child;
1149 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1152 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1155 if ( cdir == NULL ) {
1157 /* if dir == curdir it always succeed,
1158 even if curdir is deleted.
1159 it's not a pb because it will fail in extenddir
1161 if ( movecwd( vol, dir ) < 0 ) {
1162 /* dir is not valid anymore
1163 we delete dir from the cache and abort.
1165 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1166 afp_errno = AFPERR_NOOBJ;
1169 if (afp_errno == AFPERR_ACCESS)
1171 dir_invalidate(vol, dir);
1174 cdir = extenddir( vol, dir, &ret );
1178 cdir = extenddir( vol, dir, &ret );
1179 } /* if (!extend) */
1181 if ( cdir == NULL ) {
1183 if ( len > 0 || !ret.u_name) {
1191 } /* if (p != path) */
1196 * Move curdir to dir, with a possible chdir()
1198 int movecwd( vol, dir)
1199 const struct vol *vol;
1202 char path[MAXPATHLEN + 1];
1207 if ( dir == curdir ) {
1210 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1211 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1215 p = path + sizeof(path) - 1;
1218 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1221 /* parent directory is deleted */
1222 afp_errno = AFPERR_NOOBJ;
1226 if (p -n -1 < path) {
1227 afp_errno = AFPERR_PARAM;
1234 if ( d != curdir ) {
1235 n = strlen( vol->v_path );
1236 if (p -n -1 < path) {
1237 afp_errno = AFPERR_PARAM;
1242 memcpy( p, vol->v_path, n );
1244 if ( chdir( p ) < 0 ) {
1248 afp_errno = AFPERR_ACCESS;
1251 afp_errno = AFPERR_NOOBJ;
1261 * We can't use unix file's perm to support Apple's inherited protection modes.
1262 * If we aren't the file's owner we can't change its perms when moving it and smb
1263 * nfs,... don't even try.
1265 #define AFP_CHECK_ACCESS
1267 int check_access(char *path, int mode)
1269 #ifdef AFP_CHECK_ACCESS
1277 accessmode(p, &ma, curdir, NULL);
1278 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1280 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1286 /* --------------------- */
1287 int file_access(struct path *path, int mode)
1291 accessmode(path->u_name, &ma, curdir, &path->st);
1292 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1294 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1300 /* --------------------- */
1301 static int invisible_dots(const struct vol *vol, const char *name)
1303 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1306 /* ------------------------------
1308 (name, dir) with curdir:name == dir, from afp_enumerate
1311 int getdirparams(const struct vol *vol,
1312 u_int16_t bitmap, struct path *s_path,
1314 char *buf, int *buflen )
1318 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1319 int bit = 0, isad = 0;
1325 struct stat *st = &s_path->st;
1326 char *upath = s_path->u_name;
1328 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1329 (1 << DIRPBIT_CDATE) |
1330 (1 << DIRPBIT_MDATE) |
1331 (1 << DIRPBIT_BDATE) |
1332 (1 << DIRPBIT_FINFO)))) {
1334 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1335 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1340 if ( dir->d_did == DIRDID_ROOT) {
1341 pdid = DIRDID_ROOT_PARENT;
1342 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1345 pdid = dir->d_parent->d_did;
1349 while ( bitmap != 0 ) {
1350 while (( bitmap & 1 ) == 0 ) {
1358 ad_getattr(&ad, &ashort);
1359 } else if (invisible_dots(vol, dir->d_u_name)) {
1360 ashort = htons(ATTRBIT_INVISIBLE);
1363 ashort |= htons(ATTRBIT_SHARED);
1364 memcpy( data, &ashort, sizeof( ashort ));
1365 data += sizeof( ashort );
1369 memcpy( data, &pdid, sizeof( pdid ));
1370 data += sizeof( pdid );
1373 case DIRPBIT_CDATE :
1374 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1375 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1376 memcpy( data, &aint, sizeof( aint ));
1377 data += sizeof( aint );
1380 case DIRPBIT_MDATE :
1381 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1382 memcpy( data, &aint, sizeof( aint ));
1383 data += sizeof( aint );
1386 case DIRPBIT_BDATE :
1387 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1388 aint = AD_DATE_START;
1389 memcpy( data, &aint, sizeof( aint ));
1390 data += sizeof( aint );
1393 case DIRPBIT_FINFO :
1395 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1396 } else { /* no appledouble */
1397 memset( data, 0, 32 );
1398 /* set default view -- this also gets done in ad_open() */
1399 ashort = htons(FINDERINFO_CLOSEDVIEW);
1400 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1402 /* dot files are by default visible */
1403 if (invisible_dots(vol, dir->d_u_name)) {
1404 ashort = htons(FINDERINFO_INVISIBLE);
1405 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1411 case DIRPBIT_LNAME :
1412 if (dir->d_m_name) /* root of parent can have a null name */
1415 memset(data, 0, sizeof(u_int16_t));
1416 data += sizeof( u_int16_t );
1419 case DIRPBIT_SNAME :
1420 memset(data, 0, sizeof(u_int16_t));
1421 data += sizeof( u_int16_t );
1425 memcpy( data, &dir->d_did, sizeof( aint ));
1426 data += sizeof( aint );
1429 case DIRPBIT_OFFCNT :
1431 /* this needs to handle current directory access rights */
1432 if (st->st_ctime == dir->ctime) {
1433 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1435 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1437 dir->ctime = st->st_ctime;
1438 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1440 ashort = htons( ashort );
1441 memcpy( data, &ashort, sizeof( ashort ));
1442 data += sizeof( ashort );
1446 aint = htonl(st->st_uid);
1447 memcpy( data, &aint, sizeof( aint ));
1448 data += sizeof( aint );
1452 aint = htonl(st->st_gid);
1453 memcpy( data, &aint, sizeof( aint ));
1454 data += sizeof( aint );
1457 case DIRPBIT_ACCESS :
1458 accessmode( upath, &ma, dir , st);
1460 *data++ = ma.ma_user;
1461 *data++ = ma.ma_world;
1462 *data++ = ma.ma_group;
1463 *data++ = ma.ma_owner;
1466 /* Client has requested the ProDOS information block.
1467 Just pass back the same basic block for all
1468 directories. <shirsch@ibm.net> */
1469 case DIRPBIT_PDINFO :
1470 if (afp_version >= 30) { /* UTF8 name */
1471 utf8 = kTextEncodingUTF8;
1472 if (dir->d_m_name) /* root of parent can have a null name */
1475 memset(data, 0, sizeof(u_int16_t));
1476 data += sizeof( u_int16_t );
1478 memcpy(data, &aint, sizeof( aint ));
1479 data += sizeof( aint );
1481 else { /* ProDOS Info Block */
1484 ashort = htons( 0x0200 );
1485 memcpy( data, &ashort, sizeof( ashort ));
1486 data += sizeof( ashort );
1487 memset( data, 0, sizeof( ashort ));
1488 data += sizeof( ashort );
1492 case DIRPBIT_UNIXPR :
1493 aint = htonl(st->st_uid);
1494 memcpy( data, &aint, sizeof( aint ));
1495 data += sizeof( aint );
1496 aint = htonl(st->st_gid);
1497 memcpy( data, &aint, sizeof( aint ));
1498 data += sizeof( aint );
1501 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1502 memcpy( data, &aint, sizeof( aint ));
1503 data += sizeof( aint );
1505 accessmode( upath, &ma, dir , st);
1507 *data++ = ma.ma_user;
1508 *data++ = ma.ma_world;
1509 *data++ = ma.ma_group;
1510 *data++ = ma.ma_owner;
1515 ad_close( &ad, ADFLAGS_HF );
1517 return( AFPERR_BITMAP );
1523 ashort = htons( data - buf );
1524 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1525 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1527 if ( utf_nameoff ) {
1528 ashort = htons( data - buf );
1529 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1530 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1533 ad_close( &ad, ADFLAGS_HF );
1535 *buflen = data - buf;
1539 /* ----------------------------- */
1540 int path_error(struct path *path, int error)
1542 /* - a dir with access error
1543 * - no error it's a file
1546 if (path_isadir(path))
1548 if (path->st_valid && path->st_errno)
1550 return AFPERR_BADTYPE ;
1553 /* ----------------------------- */
1554 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1556 char *ibuf, *rbuf _U_;
1557 int ibuflen _U_, *rbuflen;
1562 u_int16_t vid, bitmap;
1568 memcpy( &vid, ibuf, sizeof( vid ));
1569 ibuf += sizeof( vid );
1571 if (NULL == ( vol = getvolbyvid( vid )) ) {
1572 return( AFPERR_PARAM );
1575 if (vol->v_flags & AFPVOL_RO)
1576 return AFPERR_VLOCK;
1578 memcpy( &did, ibuf, sizeof( did ));
1579 ibuf += sizeof( int );
1581 if (NULL == ( dir = dirlookup( vol, did )) ) {
1585 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1586 bitmap = ntohs( bitmap );
1587 ibuf += sizeof( bitmap );
1589 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1590 return get_afp_errno(AFPERR_NOOBJ);
1593 if ( *path->m_name != '\0' ) {
1594 rc = path_error(path, AFPERR_NOOBJ);
1595 /* maybe we are trying to set perms back */
1596 if (rc != AFPERR_ACCESS)
1601 * If ibuf is odd, make it even.
1603 if ((u_long)ibuf & 1 ) {
1607 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1608 setvoltime(obj, vol );
1614 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1616 * assume path == '\0' eg. it's a directory in canonical form
1619 struct path Cur_Path = {
1622 ".", /* unix name */
1624 0, /* stat is not set */
1628 /* ------------------ */
1629 static int set_dir_errors(struct path *path, const char *where, int err)
1634 return AFPERR_ACCESS;
1636 return AFPERR_VLOCK;
1638 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1639 return AFPERR_PARAM;
1642 /* ------------------ */
1643 int setdirparams(const struct vol *vol,
1644 struct path *path, u_int16_t d_bitmap, char *buf )
1656 u_int16_t ashort, bshort;
1658 int change_mdate = 0;
1659 int change_parent_mdate = 0;
1661 u_int16_t bitmap = d_bitmap;
1662 u_char finder_buf[32];
1665 u_int16_t upriv_bit = 0;
1668 upath = path->u_name;
1670 while ( bitmap != 0 ) {
1671 while (( bitmap & 1 ) == 0 ) {
1679 memcpy( &ashort, buf, sizeof( ashort ));
1680 buf += sizeof( ashort );
1682 case DIRPBIT_CDATE :
1684 memcpy(&cdate, buf, sizeof(cdate));
1685 buf += sizeof( cdate );
1687 case DIRPBIT_MDATE :
1688 memcpy(&newdate, buf, sizeof(newdate));
1689 buf += sizeof( newdate );
1691 case DIRPBIT_BDATE :
1693 memcpy(&bdate, buf, sizeof(bdate));
1694 buf += sizeof( bdate );
1696 case DIRPBIT_FINFO :
1698 memcpy( finder_buf, buf, 32 );
1701 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1702 change_parent_mdate = 1;
1703 memcpy( &owner, buf, sizeof(owner));
1704 buf += sizeof( owner );
1707 change_parent_mdate = 1;
1708 memcpy( &group, buf, sizeof( group ));
1709 buf += sizeof( group );
1711 case DIRPBIT_ACCESS :
1713 change_parent_mdate = 1;
1714 ma.ma_user = *buf++;
1715 ma.ma_world = *buf++;
1716 ma.ma_group = *buf++;
1717 ma.ma_owner = *buf++;
1718 mpriv = mtoumode( &ma ) | vol->v_dperm;
1719 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1720 err = set_dir_errors(path, "setdirmode", errno);
1724 /* Ignore what the client thinks we should do to the
1725 ProDOS information block. Skip over the data and
1726 report nothing amiss. <shirsch@ibm.net> */
1727 case DIRPBIT_PDINFO :
1728 if (afp_version < 30) {
1732 err = AFPERR_BITMAP;
1736 case DIRPBIT_UNIXPR :
1737 if (vol_unix_priv(vol)) {
1738 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1739 buf += sizeof( owner );
1740 memcpy( &group, buf, sizeof( group ));
1741 buf += sizeof( group );
1744 change_parent_mdate = 1;
1745 memcpy( &upriv, buf, sizeof( upriv ));
1746 buf += sizeof( upriv );
1747 upriv = ntohl (upriv) | vol->v_dperm;
1748 if (dir_rx_set(upriv)) {
1749 /* maybe we are trying to set perms back */
1750 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1752 err = set_dir_errors(path, "setdirunixmode", errno);
1763 err = AFPERR_BITMAP;
1771 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1773 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1774 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1776 * Check to see what we're trying to set. If it's anything
1777 * but ACCESS, UID, or GID, give an error. If it's any of those
1778 * three, we don't need the ad to be open, so just continue.
1780 * note: we also don't need to worry about mdate. also, be quiet
1781 * if we're using the noadouble option.
1783 if (!vol_noadouble(vol) && (d_bitmap &
1784 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1785 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1786 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1787 return AFPERR_ACCESS;
1793 * Check to see if a create was necessary. If it was, we'll want
1794 * to set our name, etc.
1796 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1797 ad_setname(&ad, curdir->d_m_name);
1803 while ( bitmap != 0 ) {
1804 while (( bitmap & 1 ) == 0 ) {
1812 ad_getattr(&ad, &bshort);
1813 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1814 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1815 change_parent_mdate = 1;
1816 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1817 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1821 ad_setattr(&ad, bshort);
1824 case DIRPBIT_CDATE :
1826 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1829 case DIRPBIT_MDATE :
1831 case DIRPBIT_BDATE :
1833 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1836 case DIRPBIT_FINFO :
1838 if ( dir->d_did == DIRDID_ROOT ) {
1840 * Alright, we admit it, this is *really* sick!
1841 * The 4 bytes that we don't copy, when we're dealing
1842 * with the root of a volume, are the directory's
1843 * location information. This eliminates that annoying
1844 * behavior one sees when mounting above another mount
1847 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1848 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1850 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1854 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1855 if ( (dir->d_did == DIRDID_ROOT) &&
1856 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1857 err = set_dir_errors(path, "setdeskowner", errno);
1858 if (isad && err == AFPERR_PARAM) {
1859 err = AFP_OK; /* ???*/
1862 goto setdirparam_done;
1865 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1866 err = set_dir_errors(path, "setdirowner", errno);
1867 goto setdirparam_done;
1871 if (dir->d_did == DIRDID_ROOT)
1872 setdeskowner( -1, ntohl(group) );
1873 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1874 err = set_dir_errors(path, "setdirowner", errno);
1875 goto setdirparam_done;
1878 case DIRPBIT_ACCESS :
1879 if (dir->d_did == DIRDID_ROOT) {
1881 if (!dir_rx_set(mpriv)) {
1882 /* we can't remove read and search for owner on volume root */
1883 err = AFPERR_ACCESS;
1884 goto setdirparam_done;
1888 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1889 err = set_dir_errors(path, "setdirmode", errno);
1890 goto setdirparam_done;
1893 case DIRPBIT_PDINFO :
1894 if (afp_version >= 30) {
1895 err = AFPERR_BITMAP;
1896 goto setdirparam_done;
1899 case DIRPBIT_UNIXPR :
1900 if (vol_unix_priv(vol)) {
1901 if (dir->d_did == DIRDID_ROOT) {
1902 if (!dir_rx_set(upriv)) {
1903 /* we can't remove read and search for owner on volume root */
1904 err = AFPERR_ACCESS;
1905 goto setdirparam_done;
1907 setdeskowner( -1, ntohl(group) );
1908 setdeskmode( upriv );
1910 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1911 err = set_dir_errors(path, "setdirowner", errno);
1912 goto setdirparam_done;
1915 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1916 err = set_dir_errors(path, "setdirunixmode", errno);
1917 goto setdirparam_done;
1921 err = AFPERR_BITMAP;
1922 goto setdirparam_done;
1926 err = AFPERR_BITMAP;
1927 goto setdirparam_done;
1936 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1937 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1941 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1942 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1947 if (path->st_valid && !path->st_errno) {
1948 struct stat *st = &path->st;
1950 if (dir && dir->d_parent) {
1951 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
1954 ad_flush( &ad, ADFLAGS_HF );
1955 ad_close( &ad, ADFLAGS_HF );
1958 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1959 && gettimeofday(&tv, NULL) == 0) {
1960 if (!movecwd(vol, dir->d_parent)) {
1961 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1962 /* be careful with bitmap because now dir is null */
1963 bitmap = 1<<DIRPBIT_MDATE;
1964 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1965 /* should we reset curdir ?*/
1972 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1975 int ibuflen _U_, *rbuflen;
1981 struct path *s_path;
1989 memcpy( &vid, ibuf, sizeof( vid ));
1990 ibuf += sizeof( vid );
1991 if (NULL == ( vol = getvolbyvid( vid )) ) {
1992 return( AFPERR_PARAM );
1995 if (vol->v_flags & AFPVOL_RO)
1996 return AFPERR_VLOCK;
1998 memcpy( &did, ibuf, sizeof( did ));
1999 ibuf += sizeof( did );
2000 if (NULL == ( dir = dirlookup( vol, did )) ) {
2001 return afp_errno; /* was AFPERR_NOOBJ */
2003 /* for concurrent access we need to be sure we are not in the
2004 * folder we want to create...
2008 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2009 return get_afp_errno(AFPERR_PARAM);
2011 /* cname was able to move curdir to it! */
2012 if (*s_path->m_name == '\0')
2013 return AFPERR_EXIST;
2015 upath = s_path->u_name;
2017 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2021 if (of_stat(s_path) < 0) {
2025 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2029 if ( movecwd( vol, dir ) < 0 ) {
2030 return( AFPERR_PARAM );
2033 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2034 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2035 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2036 if (vol_noadouble(vol))
2037 goto createdir_done;
2038 return( AFPERR_ACCESS );
2040 ad_setname(&ad, s_path->m_name);
2041 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2043 ad_flush( &ad, ADFLAGS_HF );
2044 ad_close( &ad, ADFLAGS_HF );
2047 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2048 *rbuflen = sizeof( u_int32_t );
2049 setvoltime(obj, vol );
2054 * dst new unix filename (not a pathname)
2055 * newname new mac name
2059 int renamedir(vol, src, dst, dir, newparent, newname)
2060 const struct vol *vol;
2061 char *src, *dst, *newname;
2062 struct dir *dir, *newparent;
2069 /* existence check moved to afp_moveandrename */
2070 if ( unix_rename( src, dst ) < 0 ) {
2073 return( AFPERR_NOOBJ );
2075 return( AFPERR_ACCESS );
2077 return AFPERR_VLOCK;
2079 /* tried to move directory into a subdirectory of itself */
2080 return AFPERR_CANTMOVE;
2082 /* this needs to copy and delete. bleah. that means we have
2083 * to deal with entire directory hierarchies. */
2084 if ((err = copydir(vol, src, dst)) < 0) {
2088 if ((err = deletedir(src)) < 0)
2092 return( AFPERR_PARAM );
2096 if (vol->v_adouble == AD_VERSION2_OSX) {
2097 /* We simply move the corresponding ad file as well */
2098 char tempbuf[258]="._";
2099 rename(vol->ad_path(src,0),strcat(tempbuf,dst));
2102 len = strlen( newname );
2103 /* rename() succeeded so we need to update our tree even if we can't open
2107 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2109 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2110 ad_setname(&ad, newname);
2111 ad_flush( &ad, ADFLAGS_HF );
2112 ad_close( &ad, ADFLAGS_HF );
2115 if (dir->d_m_name == dir->d_u_name)
2116 dir->d_u_name = NULL;
2118 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2119 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2120 /* FIXME : fatal ? */
2123 dir->d_m_name = buf;
2124 strcpy( dir->d_m_name, newname );
2126 if (newname == dst) {
2127 free(dir->d_u_name);
2128 dir->d_u_name = dir->d_m_name;
2131 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2132 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2135 dir->d_u_name = buf;
2136 strcpy( dir->d_u_name, dst );
2139 if (( parent = dir->d_parent ) == NULL ) {
2142 if ( parent == newparent ) {
2146 /* detach from old parent and add to new one. */
2147 dirchildremove(parent, dir);
2148 dir->d_parent = newparent;
2149 dirchildadd(newparent, dir);
2153 #define DOT_APPLEDOUBLE_LEN 13
2154 /* delete an empty directory */
2155 int deletecurdir( vol, path )
2156 const struct vol *vol;
2167 if ( curdir->d_parent == NULL ) {
2168 return( AFPERR_ACCESS );
2173 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2174 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2176 ad_getattr(&ad, &ashort);
2177 ad_close( &ad, ADFLAGS_HF );
2178 if ((ashort & htons(ATTRBIT_NODELETE))) {
2179 return AFPERR_OLOCK;
2183 if (vol->v_adouble == AD_VERSION2_OSX) {
2185 if ((err = netatalk_unlink(vol->ad_path(".",0) )) ) {
2190 /* delete stray .AppleDouble files. this happens to get .Parent files
2192 if ((dp = opendir(".AppleDouble"))) {
2193 strcpy(path, ".AppleDouble/");
2194 while ((de = readdir(dp))) {
2195 /* skip this and previous directory */
2196 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2199 /* bail if the file exists in the current directory.
2200 * note: this will not fail with dangling symlinks */
2201 if (stat(de->d_name, &st) == 0) {
2203 return AFPERR_DIRNEMPT;
2206 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2207 if ((err = netatalk_unlink(path))) {
2215 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2220 /* now get rid of dangling symlinks */
2221 if ((dp = opendir("."))) {
2222 while ((de = readdir(dp))) {
2223 /* skip this and previous directory */
2224 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2227 /* bail if it's not a symlink */
2228 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2230 return AFPERR_DIRNEMPT;
2233 if ((err = netatalk_unlink(de->d_name))) {
2240 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2245 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2246 dirchildremove(curdir, fdir);
2247 cnid_delete(vol->v_cdb, fdir->d_did);
2248 dir_remove( vol, fdir );
2253 /* inode is used as key for cnid.
2254 * Close the descriptor only after cnid_delete
2262 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2265 int ibuflen _U_, *rbuflen;
2275 sfunc = (unsigned char) *ibuf++;
2276 memcpy( &id, ibuf, sizeof( id ));
2281 if (sfunc == 3 || sfunc == 4) {
2282 if (afp_version < 30) {
2283 return( AFPERR_PARAM );
2290 case 3 :/* unicode */
2291 if (( pw = getpwuid( id )) == NULL ) {
2292 return( AFPERR_NOITEM );
2294 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2295 pw->pw_name, strlen(pw->pw_name), &name);
2299 case 4 : /* unicode */
2300 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2301 return( AFPERR_NOITEM );
2303 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2304 gr->gr_name, strlen(gr->gr_name), &name);
2308 return( AFPERR_PARAM );
2310 len = strlen( name );
2317 u_int16_t tp = htons(len);
2318 memcpy(rbuf, &tp, sizeof(tp));
2327 memcpy( rbuf, name, len );
2335 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2338 int ibuflen _U_, *rbuflen;
2347 sfunc = (unsigned char) *ibuf++;
2351 case 2 : /* unicode */
2352 if (afp_version < 30) {
2353 return( AFPERR_PARAM );
2355 memcpy(&ulen, ibuf, sizeof(ulen));
2361 len = (unsigned char) *ibuf++;
2364 return( AFPERR_PARAM );
2371 case 1 : /* unicode */
2373 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2374 return( AFPERR_NOITEM );
2379 case 2 : /* unicode */
2381 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2382 return( AFPERR_NOITEM );
2391 memcpy( rbuf, &id, sizeof( id ));
2392 *rbuflen = sizeof( id );
2396 /* ------------------------------------
2397 variable DID support
2399 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2401 char *ibuf _U_, *rbuf _U_;
2402 int ibuflen _U_, *rbuflen;
2413 /* do nothing as dids are static for the life of the process. */
2417 memcpy(&vid, ibuf, sizeof( vid ));
2418 ibuf += sizeof( vid );
2419 if (( vol = getvolbyvid( vid )) == NULL ) {
2420 return( AFPERR_PARAM );
2423 memcpy( &did, ibuf, sizeof( did ));
2424 ibuf += sizeof( did );
2425 if (( dir = dirlookup( vol, did )) == NULL ) {
2426 return( AFPERR_PARAM );
2429 /* dir_remove -- deletedid */
2435 /* did creation gets done automatically
2436 * there's a pb again with case but move it to cname
2438 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2441 int ibuflen _U_, *rbuflen;
2444 struct dir *parentdir;
2452 memcpy(&vid, ibuf, sizeof(vid));
2453 ibuf += sizeof( vid );
2455 if (NULL == ( vol = getvolbyvid( vid )) ) {
2456 return( AFPERR_PARAM );
2459 memcpy(&did, ibuf, sizeof(did));
2460 ibuf += sizeof(did);
2462 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2466 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2467 return get_afp_errno(AFPERR_PARAM);
2470 if ( *path->m_name != '\0' ) {
2471 return path_error(path, AFPERR_NOOBJ);
2474 if ( !path->st_valid && of_stat(path ) < 0 ) {
2475 return( AFPERR_NOOBJ );
2477 if ( path->st_errno ) {
2478 return( AFPERR_NOOBJ );
2481 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2482 *rbuflen = sizeof(curdir->d_did);