2 * $Id: directory.c,v 1.89 2008-12-23 07:51:12 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;
110 /* XXX would be nice to check against curdir but we need to keep its volume */
111 if (vol->curdir && curdir->d_did == did) {
120 afp_errno = AFPERR_NOOBJ;
121 while ( dir != SENTINEL ) {
122 if (dir->d_did == did)
123 return dir->d_m_name ? dir : NULL;
124 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
129 /* ------------------- */
131 int path_isadir(struct path *o_path)
133 return o_path->d_dir != NULL;
135 return o_path->m_name == '\0' || /* we are in a it */
136 !o_path->st_valid || /* in cache but we can't chdir in it */
137 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
142 int get_afp_errno(const int param)
144 if (afp_errno != AFPERR_DID1)
149 /* ------------------- */
151 dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
153 struct dir *dir = NULL;
155 if ((cdir->d_did != DIRDID_ROOT_PARENT) && (cdir->d_child)) {
161 hn = hash_lookup(vol->v_hash, &key);
169 /* -----------------------------------------
170 * if did is not in the cache resolve it with cnid
173 * OSX call it with bogus id, ie file ID not folder ID,
174 * and we are really bad in this case.
177 dirlookup( vol, did )
178 const struct vol *vol;
184 static char path[MAXPATHLEN + 1];
187 static char buffer[12 + MAXPATHLEN + 1];
188 int buflen = 12 + MAXPATHLEN + 1;
193 ret = dirsearch(vol, did);
194 if (ret != NULL || afp_errno == AFPERR_PARAM)
197 utf8 = utf8_encoding();
198 maxpath = (utf8)?MAXPATHLEN -7:255;
200 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
201 afp_errno = AFPERR_NOOBJ;
204 ptr = path + MAXPATHLEN;
205 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
206 afp_errno = AFPERR_NOOBJ;
210 pathlen = len; /* no 0 in the last part */
212 strcpy(ptr - len, mpath);
215 ret = dirsearch(vol,id);
220 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
222 NULL == (mpath = utompath(vol, upath, cnid, utf8))
224 afp_errno = AFPERR_NOOBJ;
228 len = strlen(mpath) + 1;
230 if (pathlen > maxpath) {
231 afp_errno = AFPERR_PARAM;
234 strcpy(ptr - len, mpath);
238 /* fill the cache, another place where we know about the path type */
244 temp16 = htons(pathlen);
245 memcpy(ptr, &temp16, sizeof(temp16));
247 temp = htonl(kTextEncodingUTF8);
249 memcpy(ptr, &temp, sizeof(temp));
255 *ptr = (unsigned char)pathlen;
259 /* cname is not efficient */
260 if (cname( vol, ret, &ptr ) == NULL )
263 return dirsearch(vol, did);
266 /* child addition/removal */
267 static void dirchildadd(const struct vol *vol, struct dir *a, struct dir *b)
272 b->d_next = a->d_child;
273 b->d_prev = b->d_next->d_prev;
274 b->d_next->d_prev = b;
275 b->d_prev->d_next = b;
277 if (!hash_alloc_insert(vol->v_hash, b, b)) {
278 LOG(log_error, logtype_afpd, "dirchildadd: can't hash %s", b->d_u_name);
282 static void dirchildremove(struct dir *a,struct dir *b)
285 a->d_child = (b == b->d_next) ? NULL : b->d_next;
286 b->d_next->d_prev = b->d_prev;
287 b->d_prev->d_next = b->d_next;
288 b->d_next = b->d_prev = b;
291 /* --------------------------- */
292 /* rotate the tree to the left */
293 static void dir_leftrotate(vol, dir)
297 struct dir *right = dir->d_right;
299 /* whee. move the right's left tree into dir's right tree */
300 dir->d_right = right->d_left;
301 if (right->d_left != SENTINEL)
302 right->d_left->d_back = dir;
304 if (right != SENTINEL) {
305 right->d_back = dir->d_back;
309 if (!dir->d_back) /* no parent. move the right tree to the top. */
311 else if (dir == dir->d_back->d_left) /* we were on the left */
312 dir->d_back->d_left = right;
314 dir->d_back->d_right = right; /* we were on the right */
316 /* re-insert dir on the left tree */
323 /* rotate the tree to the right */
324 static void dir_rightrotate(vol, dir)
328 struct dir *left = dir->d_left;
330 /* whee. move the left's right tree into dir's left tree */
331 dir->d_left = left->d_right;
332 if (left->d_right != SENTINEL)
333 left->d_right->d_back = dir;
335 if (left != SENTINEL) {
336 left->d_back = dir->d_back;
340 if (!dir->d_back) /* no parent. move the left tree to the top. */
342 else if (dir == dir->d_back->d_right) /* we were on the right */
343 dir->d_back->d_right = left;
345 dir->d_back->d_left = left; /* we were on the left */
347 /* re-insert dir on the right tree */
353 /* recolor after a removal */
354 static struct dir *dir_rmrecolor(vol, dir)
360 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
361 /* are we on the left tree? */
362 if (dir == dir->d_back->d_left) {
363 leaf = dir->d_back->d_right; /* get right side */
364 if (leaf->d_color == DIRTREE_COLOR_RED) {
365 /* we're red. we need to change to black. */
366 leaf->d_color = DIRTREE_COLOR_BLACK;
367 dir->d_back->d_color = DIRTREE_COLOR_RED;
368 dir_leftrotate(vol, dir->d_back);
369 leaf = dir->d_back->d_right;
372 /* right leaf has black end nodes */
373 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
374 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
375 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
376 dir = dir->d_back; /* ascend */
378 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
379 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
380 leaf->d_color = DIRTREE_COLOR_RED;
381 dir_rightrotate(vol, leaf);
382 leaf = dir->d_back->d_right;
384 leaf->d_color = dir->d_back->d_color;
385 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
386 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
387 dir_leftrotate(vol, dir->d_back);
390 } else { /* right tree */
391 leaf = dir->d_back->d_left; /* left tree */
392 if (leaf->d_color == DIRTREE_COLOR_RED) {
393 leaf->d_color = DIRTREE_COLOR_BLACK;
394 dir->d_back->d_color = DIRTREE_COLOR_RED;
395 dir_rightrotate(vol, dir->d_back);
396 leaf = dir->d_back->d_left;
399 /* left leaf has black end nodes */
400 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
401 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
402 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
403 dir = dir->d_back; /* ascend */
405 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
406 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
407 leaf->d_color = DIRTREE_COLOR_RED;
408 dir_leftrotate(vol, leaf);
409 leaf = dir->d_back->d_left;
411 leaf->d_color = dir->d_back->d_color;
412 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
413 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
414 dir_rightrotate(vol, dir->d_back);
419 dir->d_color = DIRTREE_COLOR_BLACK;
425 /* --------------------- */
426 static void dir_hash_del(const struct vol *vol, struct dir *dir)
430 hn = hash_lookup(vol->v_hash, dir);
432 LOG(log_error, logtype_afpd, "dir_hash_del: %s not hashed", dir->d_u_name);
435 hash_delete(vol->v_hash, hn);
439 /* remove the node from the tree. this is just like insertion, but
440 * different. actually, it has to worry about a bunch of things that
441 * insertion doesn't care about. */
442 static void dir_remove( const struct vol *vol _U_, struct dir *dir)
445 struct ofork *of, *last;
446 struct dir *node, *leaf;
447 #endif /* REMOVE_NODES */
449 if (!dir || (dir == SENTINEL))
452 /* i'm not sure if it really helps to delete stuff. */
453 dir_hash_del(vol, dir);
456 dir->d_m_name = NULL;
457 dir->d_u_name = NULL;
458 #else /* ! REMOVE_NODES */
460 /* go searching for a node with at most one child */
461 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
465 while (node->d_left != SENTINEL)
470 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
473 leaf->d_back = node->d_back;
476 } else if (node == node->d_back->d_left) { /* left tree */
477 node->d_back->d_left = leaf;
479 node->d_back->d_right = leaf;
482 /* we want to free node, but we also want to free the data in dir.
483 * currently, that's d_name and the directory traversal bits.
484 * we just copy the necessary bits and then fix up all the
485 * various pointers to the directory. needless to say, there are
486 * a bunch of places that store the directory struct. */
488 struct dir save, *tmp;
490 memcpy(&save, dir, sizeof(save));
491 memcpy(dir, node, sizeof(struct dir));
493 /* restore the red-black bits */
494 dir->d_left = save.d_left;
495 dir->d_right = save.d_right;
496 dir->d_back = save.d_back;
497 dir->d_color = save.d_color;
499 if (node == vol->v_dir) {/* we may need to fix up this pointer */
501 rootpar.d_child = vol->v_dir;
503 /* if we aren't the root directory, we have parents and
504 * siblings to worry about */
505 if (dir->d_parent->d_child == node)
506 dir->d_parent->d_child = dir;
507 dir->d_next->d_prev = dir;
508 dir->d_prev->d_next = dir;
511 /* fix up children. */
515 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
518 if (node == curdir) /* another pointer to fixup */
521 /* we also need to fix up oforks. bleah */
522 if ((of = dir->d_ofork)) {
523 last = of->of_d_prev;
526 of = (last == of) ? NULL : of->of_d_next;
530 /* set the node's d_name */
531 node->d_m_name = save.d_m_name;
532 node->d_u_name = save.d_u_name;
535 if (node->d_color == DIRTREE_COLOR_BLACK)
536 dir_rmrecolor(vol, leaf);
538 if (node->d_u_name != node->d_m_name) {
539 free(node->d_u_name);
541 free(node->d_m_name);
543 #endif /* ! REMOVE_NODES */
546 /* ---------------------------------------
547 * remove the node and its childs from the tree
549 * FIXME what about opened forks with refs to it?
550 * it's an afp specs violation because you can't delete
551 * an opened forks. Now afpd doesn't care about forks opened by other
552 * process. It's fixable within afpd if fnctl_lock, doable with smb and
553 * next to impossible for nfs and local filesystem access.
555 static void dir_invalidate( vol, dir )
556 const struct vol *vol;
560 /* v_root can't be deleted */
561 if (movecwd(vol, vol->v_root) < 0) {
562 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
566 dirchildremove(dir->d_parent, dir);
567 dir_remove( vol, dir );
570 /* ------------------------------------ */
571 static struct dir *dir_insert(vol, dir)
572 const struct vol *vol;
578 while (pdir->d_did != dir->d_did ) {
579 if ( pdir->d_did > dir->d_did ) {
580 if ( pdir->d_left == SENTINEL ) {
587 if ( pdir->d_right == SENTINEL ) {
592 pdir = pdir->d_right;
599 * attempt to extend the current dir. tree to include path
600 * as a side-effect, movecwd to that point and return the new dir
603 extenddir( vol, dir, path )
610 if ( path->u_name == NULL) {
611 afp_errno = AFPERR_PARAM;
614 if (of_stat( path ) != 0 ) {
618 if (!S_ISDIR(path->st.st_mode)) {
622 /* mac name is always with the right encoding (from cname()) */
623 if (( dir = adddir( vol, dir, path)) == NULL ) {
628 if ( movecwd( vol, dir ) < 0 ) {
635 /* -------------------
636 system rmdir with afp error code.
637 ENOENT is not an error.
639 int netatalk_rmdir(const char *name)
641 if (rmdir(name) < 0) {
646 return AFPERR_DIRNEMPT;
649 return AFPERR_ACCESS;
659 /* -------------------------
660 appledouble mkdir afp error code.
662 static int netatalk_mkdir(const char *name)
664 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
667 return( AFPERR_NOOBJ );
669 return( AFPERR_VLOCK );
672 return( AFPERR_ACCESS );
674 return( AFPERR_EXIST );
677 return( AFPERR_DFULL );
679 return( AFPERR_PARAM );
685 /* -------------------
686 system unlink with afp error code.
687 ENOENT is not an error.
689 int netatalk_unlink(const char *name)
691 if (unlink(name) < 0) {
699 return AFPERR_ACCESS;
707 /* ------------------- */
708 static int deletedir(char *dir)
710 char path[MAXPATHLEN + 1];
718 if ((len = strlen(dir)) +2 > sizeof(path))
722 if ((dp = opendir(dir)) == NULL)
728 remain = sizeof(path) -len -1;
729 while ((de = readdir(dp)) && err == AFP_OK) {
730 /* skip this and previous directory */
731 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
734 if (strlen(de->d_name) > remain) {
738 strcpy(path + len, de->d_name);
739 if (stat(path, &st)) {
742 if (S_ISDIR(st.st_mode)) {
743 err = deletedir(path);
745 err = netatalk_unlink(path);
750 /* okay. the directory is empty. delete it. note: we already got rid
753 err = netatalk_rmdir(dir);
758 /* do a recursive copy. */
759 static int copydir(const struct vol *vol, char *src, char *dst)
761 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
770 /* doesn't exist or the path is too long. */
771 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
772 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
773 ((dp = opendir(src)) == NULL))
776 /* try to create the destination directory */
777 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
782 /* set things up to copy */
786 srem = sizeof(spath) - slen -1;
791 drem = sizeof(dpath) - dlen -1;
794 while ((de = readdir(dp))) {
795 /* skip this and previous directory */
796 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
799 if (strlen(de->d_name) > srem) {
803 strcpy(spath + slen, de->d_name);
805 if (stat(spath, &st) == 0) {
806 if (strlen(de->d_name) > drem) {
810 strcpy(dpath + dlen, de->d_name);
812 if (S_ISDIR(st.st_mode)) {
813 if (AFP_OK != (err = copydir(vol, spath, dpath)))
815 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
819 /* keep the same time stamp. */
820 ut.actime = ut.modtime = st.st_mtime;
826 /* keep the same time stamp. */
827 if (stat(src, &st) == 0) {
828 ut.actime = ut.modtime = st.st_mtime;
838 /* --- public functions follow --- */
840 /* NOTE: we start off with at least one node (the root directory). */
841 static struct dir *dirinsert( vol, dir )
847 if ((node = dir_insert(vol, dir)))
850 /* recolor the tree. the current node is red. */
851 dir->d_color = DIRTREE_COLOR_RED;
853 /* parent of this node has to be black. if the parent node
854 * is red, then we have a grandparent. */
855 while ((dir != vol->v_root) &&
856 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
857 /* are we on the left tree? */
858 if (dir->d_back == dir->d_back->d_back->d_left) {
859 node = dir->d_back->d_back->d_right; /* get the right node */
860 if (node->d_color == DIRTREE_COLOR_RED) {
861 /* we're red. we need to change to black. */
862 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
863 node->d_color = DIRTREE_COLOR_BLACK;
864 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
865 dir = dir->d_back->d_back; /* finished. go up. */
867 if (dir == dir->d_back->d_right) {
869 dir_leftrotate(vol, dir);
871 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
872 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
873 dir_rightrotate(vol, dir->d_back->d_back);
876 node = dir->d_back->d_back->d_left;
877 if (node->d_color == DIRTREE_COLOR_RED) {
878 /* we're red. we need to change to black. */
879 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
880 node->d_color = DIRTREE_COLOR_BLACK;
881 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
882 dir = dir->d_back->d_back; /* finished. ascend */
884 if (dir == dir->d_back->d_left) {
886 dir_rightrotate(vol, dir);
888 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
889 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
890 dir_leftrotate(vol, dir->d_back->d_back);
895 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
899 /* ---------------------------- */
901 adddir( vol, dir, path)
906 struct dir *cdir, *edir;
914 upath = path->u_name;
916 upathlen = strlen(upath);
918 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
922 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
926 if ((cdir = dirnew(name, upath)) == NULL) {
927 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
933 if ((edir = dirinsert( vol, cdir ))) {
934 /* it's not possible with LASTDID
936 - someone else have moved the directory.
937 - it's a symlink inside the share.
938 - it's an ID reused, the old directory was deleted but not
939 the cnid record and the server've reused the inode for
941 for HASH (we should get ride of HASH)
942 - someone else have moved the directory.
943 - it's an ID reused as above
944 - it's a hash duplicate and we are in big trouble
946 deleted = (edir->d_m_name == NULL);
948 dir_hash_del(vol, edir);
950 edir->d_m_name = cdir->d_m_name;
951 edir->d_u_name = cdir->d_u_name;
954 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
955 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
956 hash_alloc_insert(vol->v_hash, cdir, cdir);
959 /* the old was not in the same folder */
961 dirchildremove(cdir->d_parent, cdir);
964 /* parent/child directories */
965 cdir->d_parent = dir;
966 dirchildadd(vol, dir, cdir);
970 /* --- public functions follow --- */
971 /* free everything down. we don't bother to recolor as this is only
972 * called to free the entire tree */
973 void dirfreename(struct dir *dir)
975 if (dir->d_u_name != dir->d_m_name) {
984 if (!dir || (dir == SENTINEL))
987 if ( dir->d_left != SENTINEL ) {
988 dirfree( dir->d_left );
990 if ( dir->d_right != SENTINEL ) {
991 dirfree( dir->d_right );
994 if (dir != SENTINEL) {
1000 /* --------------------------------------------
1001 * most of the time mac name and unix name are the same
1003 struct dir *dirnew(const char *m_name, const char *u_name)
1007 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1011 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1016 if (m_name == u_name || !strcmp(m_name, u_name)) {
1017 dir->d_u_name = dir->d_m_name;
1019 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1020 free(dir->d_m_name);
1025 dir->d_left = dir->d_right = SENTINEL;
1026 dir->d_next = dir->d_prev = dir;
1030 /* ------------------ */
1031 static hash_val_t hash_fun_dir(const void *key)
1033 const struct dir *k = key;
1035 static unsigned long randbox[] = {
1036 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1037 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1038 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1039 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1042 const unsigned char *str = (unsigned char *)k->d_u_name;
1046 acc ^= randbox[(*str + acc) & 0xf];
1047 acc = (acc << 1) | (acc >> 31);
1049 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1050 acc = (acc << 2) | (acc >> 30);
1056 /* ---------------- */
1057 static int hash_comp_dir(const void *key1, const void *key2)
1059 const struct dir *k1 = key1;
1060 const struct dir *k2 = key2;
1062 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1065 /* ---------------- */
1069 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1072 /* ------------------ */
1073 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1076 movecwd failed some of dir path are not there anymore.
1077 FIXME Is it true with other errors?
1078 so we remove dir from the cache
1080 if (dir->d_did == DIRDID_ROOT_PARENT)
1082 if (afp_errno == AFPERR_ACCESS) {
1083 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1086 /* FIXME should we set these?, don't need to call stat() after:
1088 ret->st_errno = EACCES;
1090 ret->m_name = dir->d_m_name;
1091 ret->u_name = dir->d_u_name;
1094 } else if (afp_errno == AFPERR_NOOBJ) {
1095 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1098 strcpy(ret->m_name, dir->d_m_name);
1099 if (dir->d_m_name == dir->d_u_name) {
1100 ret->u_name = ret->m_name;
1103 size_t tp = strlen(ret->m_name)+1;
1105 ret->u_name = ret->m_name +tp;
1106 strcpy(ret->u_name, dir->d_u_name);
1108 /* FIXME should we set :
1110 ret->st_errno = ENOENT;
1112 dir_invalidate(vol, dir);
1115 dir_invalidate(vol, dir);
1119 /* -------------------------------------------------- */
1125 stat the file or errno
1128 curdir: filename parent directory
1134 stat the dir or errno
1138 curdir: dir parent directory
1146 curdir: dir parent directory
1153 cname( vol, dir, cpath )
1154 const struct vol *vol;
1159 static char path[ MAXPATHLEN + 1];
1160 static struct path ret;
1172 afp_errno = AFPERR_NOOBJ;
1173 memset(&ret, 0, sizeof(ret));
1174 switch (ret.m_type = *data) { /* path type */
1177 len = (unsigned char) *data++;
1180 if (afp_version >= 30) {
1186 if (afp_version >= 30) {
1188 memcpy(&hint, data, sizeof(hint));
1190 data += sizeof(hint);
1192 memcpy(&len16, data, sizeof(len16));
1199 /* else it's an error */
1201 afp_errno = AFPERR_PARAM;
1204 *cpath += len + size;
1209 if (movecwd( vol, dir ) < 0 ) {
1210 return invalidate(vol, dir, &ret );
1212 if (*path == '\0') {
1219 if (*data == sep ) {
1223 while (*data == sep && len > 0 ) {
1224 if ( dir->d_parent == NULL ) {
1227 dir = dir->d_parent;
1232 /* would this be faster with strlen + strncpy? */
1234 while ( *data != sep && len > 0 ) {
1236 if (p > &path[ MAXPATHLEN]) {
1237 afp_errno = AFPERR_PARAM;
1243 /* short cut bits by chopping off a trailing \0. this also
1244 makes the traversal happy w/ filenames at the end of the
1251 if ( p == path ) { /* end of the name parameter */
1255 if (afp_version >= 30) {
1260 static char temp[ MAXPATHLEN + 1];
1262 /* not an UTF8 name */
1263 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1264 afp_errno = AFPERR_PARAM;
1269 /* check for OS X mangled filename :( */
1271 t = demangle_osx(vol, path, dir->d_did, &fileid);
1274 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1275 * flags weren't the same
1277 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1278 /* at last got our view of mac name */
1283 if (ret.u_name == NULL) {
1284 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1285 afp_errno = AFPERR_PARAM;
1290 if (dir->d_did == DIRDID_ROOT_PARENT) {
1291 /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
1292 * d_m_name is the Mac volume name
1293 * d_u_name is the volume unix directory name
1297 if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
1302 cdir = dirsearch_byname(vol, dir, ret.u_name);
1305 if ( cdir == NULL ) {
1307 /* if dir == curdir it always succeed,
1308 even if curdir is deleted.
1309 it's not a pb because it will fail in extenddir
1311 if ( movecwd( vol, dir ) < 0 ) {
1312 /* dir is not valid anymore
1313 we delete dir from the cache and abort.
1315 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1316 afp_errno = AFPERR_NOOBJ;
1319 if (afp_errno == AFPERR_ACCESS)
1321 dir_invalidate(vol, dir);
1324 cdir = extenddir( vol, dir, &ret );
1328 cdir = extenddir( vol, dir, &ret );
1329 } /* if (!extend) */
1331 if ( cdir == NULL ) {
1345 * Move curdir to dir, with a possible chdir()
1347 int movecwd( vol, dir)
1348 const struct vol *vol;
1351 char path[MAXPATHLEN + 1];
1356 if ( dir == curdir ) {
1359 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1360 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1364 p = path + sizeof(path) - 1;
1367 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1370 /* parent directory is deleted */
1371 afp_errno = AFPERR_NOOBJ;
1375 if (p -n -1 < path) {
1376 afp_errno = AFPERR_PARAM;
1383 if ( d != curdir ) {
1384 n = strlen( vol->v_path );
1385 if (p -n -1 < path) {
1386 afp_errno = AFPERR_PARAM;
1391 memcpy( p, vol->v_path, n );
1393 if ( chdir( p ) < 0 ) {
1397 afp_errno = AFPERR_ACCESS;
1400 afp_errno = AFPERR_NOOBJ;
1410 * We can't use unix file's perm to support Apple's inherited protection modes.
1411 * If we aren't the file's owner we can't change its perms when moving it and smb
1412 * nfs,... don't even try.
1414 #define AFP_CHECK_ACCESS
1416 int check_access(char *path, int mode)
1418 #ifdef AFP_CHECK_ACCESS
1426 accessmode(p, &ma, curdir, NULL);
1427 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1429 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1435 /* --------------------- */
1436 int file_access(struct path *path, int mode)
1440 accessmode(path->u_name, &ma, curdir, &path->st);
1441 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1443 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1449 /* --------------------- */
1450 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1452 dir->offcnt = count;
1453 dir->ctime = st->st_ctime;
1454 dir->d_flags &= ~DIRF_CNID;
1457 /* ---------------------
1458 * is our cached offspring count valid?
1461 int diroffcnt(struct dir *dir, struct stat *st)
1463 return st->st_ctime == dir->ctime;
1466 /* ---------------------
1467 * is our cached also for reenumerate id?
1470 int dirreenumerate(struct dir *dir, struct stat *st)
1472 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1475 /* --------------------- */
1476 static int invisible_dots(const struct vol *vol, const char *name)
1478 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1481 /* ------------------------------
1483 (name, dir) with curdir:name == dir, from afp_enumerate
1486 int getdirparams(const struct vol *vol,
1487 u_int16_t bitmap, struct path *s_path,
1489 char *buf, int *buflen )
1493 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1494 int bit = 0, isad = 0;
1500 struct stat *st = &s_path->st;
1501 char *upath = s_path->u_name;
1503 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1504 (1 << DIRPBIT_CDATE) |
1505 (1 << DIRPBIT_MDATE) |
1506 (1 << DIRPBIT_BDATE) |
1507 (1 << DIRPBIT_FINFO)))) {
1509 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1510 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1515 if ( dir->d_did == DIRDID_ROOT) {
1516 pdid = DIRDID_ROOT_PARENT;
1517 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1520 pdid = dir->d_parent->d_did;
1524 while ( bitmap != 0 ) {
1525 while (( bitmap & 1 ) == 0 ) {
1533 ad_getattr(&ad, &ashort);
1534 } else if (invisible_dots(vol, dir->d_u_name)) {
1535 ashort = htons(ATTRBIT_INVISIBLE);
1538 ashort |= htons(ATTRBIT_SHARED);
1539 memcpy( data, &ashort, sizeof( ashort ));
1540 data += sizeof( ashort );
1544 memcpy( data, &pdid, sizeof( pdid ));
1545 data += sizeof( pdid );
1548 case DIRPBIT_CDATE :
1549 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1550 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1551 memcpy( data, &aint, sizeof( aint ));
1552 data += sizeof( aint );
1555 case DIRPBIT_MDATE :
1556 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1557 memcpy( data, &aint, sizeof( aint ));
1558 data += sizeof( aint );
1561 case DIRPBIT_BDATE :
1562 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1563 aint = AD_DATE_START;
1564 memcpy( data, &aint, sizeof( aint ));
1565 data += sizeof( aint );
1568 case DIRPBIT_FINFO :
1570 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1571 } else { /* no appledouble */
1572 memset( data, 0, 32 );
1573 /* set default view -- this also gets done in ad_open() */
1574 ashort = htons(FINDERINFO_CLOSEDVIEW);
1575 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1577 /* dot files are by default visible */
1578 if (invisible_dots(vol, dir->d_u_name)) {
1579 ashort = htons(FINDERINFO_INVISIBLE);
1580 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1586 case DIRPBIT_LNAME :
1587 if (dir->d_m_name) /* root of parent can have a null name */
1590 memset(data, 0, sizeof(u_int16_t));
1591 data += sizeof( u_int16_t );
1594 case DIRPBIT_SNAME :
1595 memset(data, 0, sizeof(u_int16_t));
1596 data += sizeof( u_int16_t );
1600 memcpy( data, &dir->d_did, sizeof( aint ));
1601 data += sizeof( aint );
1604 case DIRPBIT_OFFCNT :
1606 /* this needs to handle current directory access rights */
1607 if (diroffcnt(dir, st)) {
1608 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1610 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1611 setdiroffcnt(dir, st, ret);
1612 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1614 ashort = htons( ashort );
1615 memcpy( data, &ashort, sizeof( ashort ));
1616 data += sizeof( ashort );
1620 aint = htonl(st->st_uid);
1621 memcpy( data, &aint, sizeof( aint ));
1622 data += sizeof( aint );
1626 aint = htonl(st->st_gid);
1627 memcpy( data, &aint, sizeof( aint ));
1628 data += sizeof( aint );
1631 case DIRPBIT_ACCESS :
1632 accessmode( upath, &ma, dir , st);
1634 *data++ = ma.ma_user;
1635 *data++ = ma.ma_world;
1636 *data++ = ma.ma_group;
1637 *data++ = ma.ma_owner;
1640 /* Client has requested the ProDOS information block.
1641 Just pass back the same basic block for all
1642 directories. <shirsch@ibm.net> */
1643 case DIRPBIT_PDINFO :
1644 if (afp_version >= 30) { /* UTF8 name */
1645 utf8 = kTextEncodingUTF8;
1646 if (dir->d_m_name) /* root of parent can have a null name */
1649 memset(data, 0, sizeof(u_int16_t));
1650 data += sizeof( u_int16_t );
1652 memcpy(data, &aint, sizeof( aint ));
1653 data += sizeof( aint );
1655 else { /* ProDOS Info Block */
1658 ashort = htons( 0x0200 );
1659 memcpy( data, &ashort, sizeof( ashort ));
1660 data += sizeof( ashort );
1661 memset( data, 0, sizeof( ashort ));
1662 data += sizeof( ashort );
1666 case DIRPBIT_UNIXPR :
1667 aint = htonl(st->st_uid);
1668 memcpy( data, &aint, sizeof( aint ));
1669 data += sizeof( aint );
1670 aint = htonl(st->st_gid);
1671 memcpy( data, &aint, sizeof( aint ));
1672 data += sizeof( aint );
1675 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1676 memcpy( data, &aint, sizeof( aint ));
1677 data += sizeof( aint );
1679 accessmode( upath, &ma, dir , st);
1681 *data++ = ma.ma_user;
1682 *data++ = ma.ma_world;
1683 *data++ = ma.ma_group;
1684 *data++ = ma.ma_owner;
1689 ad_close_metadata( &ad );
1691 return( AFPERR_BITMAP );
1697 ashort = htons( data - buf );
1698 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1699 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1701 if ( utf_nameoff ) {
1702 ashort = htons( data - buf );
1703 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1704 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1707 ad_close_metadata( &ad );
1709 *buflen = data - buf;
1713 /* ----------------------------- */
1714 int path_error(struct path *path, int error)
1716 /* - a dir with access error
1717 * - no error it's a file
1720 if (path_isadir(path))
1722 if (path->st_valid && path->st_errno)
1724 return AFPERR_BADTYPE ;
1727 /* ----------------------------- */
1728 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1730 char *ibuf, *rbuf _U_;
1731 int ibuflen _U_, *rbuflen;
1736 u_int16_t vid, bitmap;
1742 memcpy( &vid, ibuf, sizeof( vid ));
1743 ibuf += sizeof( vid );
1745 if (NULL == ( vol = getvolbyvid( vid )) ) {
1746 return( AFPERR_PARAM );
1749 if (vol->v_flags & AFPVOL_RO)
1750 return AFPERR_VLOCK;
1752 memcpy( &did, ibuf, sizeof( did ));
1753 ibuf += sizeof( int );
1755 if (NULL == ( dir = dirlookup( vol, did )) ) {
1759 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1760 bitmap = ntohs( bitmap );
1761 ibuf += sizeof( bitmap );
1763 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1764 return get_afp_errno(AFPERR_NOOBJ);
1767 if ( *path->m_name != '\0' ) {
1768 rc = path_error(path, AFPERR_NOOBJ);
1769 /* maybe we are trying to set perms back */
1770 if (rc != AFPERR_ACCESS)
1775 * If ibuf is odd, make it even.
1777 if ((u_long)ibuf & 1 ) {
1781 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1782 setvoltime(obj, vol );
1788 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1790 * assume path == '\0' eg. it's a directory in canonical form
1793 struct path Cur_Path = {
1796 ".", /* unix name */
1798 NULL,/* struct dir */
1799 0, /* stat is not set */
1802 /* ------------------ */
1803 static int set_dir_errors(struct path *path, const char *where, int err)
1808 return AFPERR_ACCESS;
1810 return AFPERR_VLOCK;
1812 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1813 return AFPERR_PARAM;
1816 /* ------------------ */
1817 int setdirparams(const struct vol *vol,
1818 struct path *path, u_int16_t d_bitmap, char *buf )
1830 u_int16_t ashort, bshort;
1832 int change_mdate = 0;
1833 int change_parent_mdate = 0;
1835 u_int16_t bitmap = d_bitmap;
1836 u_char finder_buf[32];
1839 u_int16_t upriv_bit = 0;
1842 upath = path->u_name;
1844 while ( bitmap != 0 ) {
1845 while (( bitmap & 1 ) == 0 ) {
1853 memcpy( &ashort, buf, sizeof( ashort ));
1854 buf += sizeof( ashort );
1856 case DIRPBIT_CDATE :
1858 memcpy(&cdate, buf, sizeof(cdate));
1859 buf += sizeof( cdate );
1861 case DIRPBIT_MDATE :
1862 memcpy(&newdate, buf, sizeof(newdate));
1863 buf += sizeof( newdate );
1865 case DIRPBIT_BDATE :
1867 memcpy(&bdate, buf, sizeof(bdate));
1868 buf += sizeof( bdate );
1870 case DIRPBIT_FINFO :
1872 memcpy( finder_buf, buf, 32 );
1875 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1876 change_parent_mdate = 1;
1877 memcpy( &owner, buf, sizeof(owner));
1878 buf += sizeof( owner );
1881 change_parent_mdate = 1;
1882 memcpy( &group, buf, sizeof( group ));
1883 buf += sizeof( group );
1885 case DIRPBIT_ACCESS :
1887 change_parent_mdate = 1;
1888 ma.ma_user = *buf++;
1889 ma.ma_world = *buf++;
1890 ma.ma_group = *buf++;
1891 ma.ma_owner = *buf++;
1892 mpriv = mtoumode( &ma ) | vol->v_perm;
1893 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1894 err = set_dir_errors(path, "setdirmode", errno);
1898 /* Ignore what the client thinks we should do to the
1899 ProDOS information block. Skip over the data and
1900 report nothing amiss. <shirsch@ibm.net> */
1901 case DIRPBIT_PDINFO :
1902 if (afp_version < 30) {
1906 err = AFPERR_BITMAP;
1910 case DIRPBIT_UNIXPR :
1911 if (vol_unix_priv(vol)) {
1912 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1913 buf += sizeof( owner );
1914 memcpy( &group, buf, sizeof( group ));
1915 buf += sizeof( group );
1918 change_parent_mdate = 1;
1919 memcpy( &upriv, buf, sizeof( upriv ));
1920 buf += sizeof( upriv );
1921 upriv = ntohl (upriv) | vol->v_perm;
1922 if (dir_rx_set(upriv)) {
1923 /* maybe we are trying to set perms back */
1924 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1926 err = set_dir_errors(path, "setdirunixmode", errno);
1937 err = AFPERR_BITMAP;
1945 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1947 if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1949 * Check to see what we're trying to set. If it's anything
1950 * but ACCESS, UID, or GID, give an error. If it's any of those
1951 * three, we don't need the ad to be open, so just continue.
1953 * note: we also don't need to worry about mdate. also, be quiet
1954 * if we're using the noadouble option.
1956 if (!vol_noadouble(vol) && (d_bitmap &
1957 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1958 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1959 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1960 return AFPERR_ACCESS;
1966 * Check to see if a create was necessary. If it was, we'll want
1967 * to set our name, etc.
1969 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1970 ad_setname(&ad, curdir->d_m_name);
1976 while ( bitmap != 0 ) {
1977 while (( bitmap & 1 ) == 0 ) {
1985 ad_getattr(&ad, &bshort);
1986 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1987 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1988 change_parent_mdate = 1;
1989 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1990 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1994 ad_setattr(&ad, bshort);
1997 case DIRPBIT_CDATE :
1999 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2002 case DIRPBIT_MDATE :
2004 case DIRPBIT_BDATE :
2006 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2009 case DIRPBIT_FINFO :
2011 if ( dir->d_did == DIRDID_ROOT ) {
2013 * Alright, we admit it, this is *really* sick!
2014 * The 4 bytes that we don't copy, when we're dealing
2015 * with the root of a volume, are the directory's
2016 * location information. This eliminates that annoying
2017 * behavior one sees when mounting above another mount
2020 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2021 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2023 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2027 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2028 if ( (dir->d_did == DIRDID_ROOT) &&
2029 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2030 err = set_dir_errors(path, "setdeskowner", errno);
2031 if (isad && err == AFPERR_PARAM) {
2032 err = AFP_OK; /* ???*/
2035 goto setdirparam_done;
2038 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2039 err = set_dir_errors(path, "setdirowner", errno);
2040 goto setdirparam_done;
2044 if (dir->d_did == DIRDID_ROOT)
2045 setdeskowner( -1, ntohl(group) );
2046 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2047 err = set_dir_errors(path, "setdirowner", errno);
2048 goto setdirparam_done;
2051 case DIRPBIT_ACCESS :
2052 if (dir->d_did == DIRDID_ROOT) {
2054 if (!dir_rx_set(mpriv)) {
2055 /* we can't remove read and search for owner on volume root */
2056 err = AFPERR_ACCESS;
2057 goto setdirparam_done;
2061 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2062 err = set_dir_errors(path, "setdirmode", errno);
2063 goto setdirparam_done;
2066 case DIRPBIT_PDINFO :
2067 if (afp_version >= 30) {
2068 err = AFPERR_BITMAP;
2069 goto setdirparam_done;
2072 case DIRPBIT_UNIXPR :
2073 if (vol_unix_priv(vol)) {
2074 if (dir->d_did == DIRDID_ROOT) {
2075 if (!dir_rx_set(upriv)) {
2076 /* we can't remove read and search for owner on volume root */
2077 err = AFPERR_ACCESS;
2078 goto setdirparam_done;
2080 setdeskowner( -1, ntohl(group) );
2081 setdeskmode( upriv );
2083 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2084 err = set_dir_errors(path, "setdirowner", errno);
2085 goto setdirparam_done;
2088 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2089 err = set_dir_errors(path, "setdirunixmode", errno);
2090 goto setdirparam_done;
2094 err = AFPERR_BITMAP;
2095 goto setdirparam_done;
2099 err = AFPERR_BITMAP;
2100 goto setdirparam_done;
2109 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2110 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2114 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2115 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2120 if (path->st_valid && !path->st_errno) {
2121 struct stat *st = &path->st;
2123 if (dir && dir->d_parent) {
2124 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2128 ad_close_metadata( &ad);
2131 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2132 && gettimeofday(&tv, NULL) == 0) {
2133 if (!movecwd(vol, dir->d_parent)) {
2134 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2135 /* be careful with bitmap because now dir is null */
2136 bitmap = 1<<DIRPBIT_MDATE;
2137 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2138 /* should we reset curdir ?*/
2146 int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2148 char *ibuf, *rbuf _U_;
2149 int ibuflen _U_, *rbuflen;
2161 memcpy( &vid, ibuf, sizeof( vid ));
2162 ibuf += sizeof( vid );
2163 if (NULL == (vol = getvolbyvid( vid )) ) {
2164 return( AFPERR_PARAM );
2167 memcpy( &did, ibuf, sizeof( did ));
2168 ibuf += sizeof( did );
2169 if (NULL == ( dir = dirlookup( vol, did )) ) {
2170 return afp_errno; /* was AFPERR_NOOBJ */
2173 if (NULL == ( dp = opendir( "." )) ) {
2176 return( AFPERR_NOOBJ );
2178 return( AFPERR_ACCESS );
2180 return( AFPERR_PARAM );
2185 if ( fsync ( dfd ) < 0 ) {
2186 LOG(log_error, logtype_afpd, "syncdir(%s): ddir(%d) %s", dir->d_u_name, dfd, strerror(errno) );
2193 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2196 int ibuflen _U_, *rbuflen;
2202 struct path *s_path;
2210 memcpy( &vid, ibuf, sizeof( vid ));
2211 ibuf += sizeof( vid );
2212 if (NULL == ( vol = getvolbyvid( vid )) ) {
2213 return( AFPERR_PARAM );
2216 if (vol->v_flags & AFPVOL_RO)
2217 return AFPERR_VLOCK;
2219 memcpy( &did, ibuf, sizeof( did ));
2220 ibuf += sizeof( did );
2221 if (NULL == ( dir = dirlookup( vol, did )) ) {
2222 return afp_errno; /* was AFPERR_NOOBJ */
2224 /* for concurrent access we need to be sure we are not in the
2225 * folder we want to create...
2229 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2230 return get_afp_errno(AFPERR_PARAM);
2232 /* cname was able to move curdir to it! */
2233 if (*s_path->m_name == '\0')
2234 return AFPERR_EXIST;
2236 upath = s_path->u_name;
2237 if (0 != (err = check_name(vol, upath))) {
2241 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2245 if (of_stat(s_path) < 0) {
2249 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2253 if ( movecwd( vol, dir ) < 0 ) {
2254 return( AFPERR_PARAM );
2257 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2258 if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2259 if (vol_noadouble(vol))
2260 goto createdir_done;
2261 return( AFPERR_ACCESS );
2263 ad_setname(&ad, s_path->m_name);
2264 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2267 ad_close_metadata( &ad);
2270 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2271 *rbuflen = sizeof( u_int32_t );
2272 setvoltime(obj, vol );
2277 * dst new unix filename (not a pathname)
2278 * newname new mac name
2282 int renamedir(vol, src, dst, dir, newparent, newname)
2283 const struct vol *vol;
2284 char *src, *dst, *newname;
2285 struct dir *dir, *newparent;
2292 /* existence check moved to afp_moveandrename */
2293 if ( unix_rename( src, dst ) < 0 ) {
2296 return( AFPERR_NOOBJ );
2298 return( AFPERR_ACCESS );
2300 return AFPERR_VLOCK;
2302 /* tried to move directory into a subdirectory of itself */
2303 return AFPERR_CANTMOVE;
2305 /* this needs to copy and delete. bleah. that means we have
2306 * to deal with entire directory hierarchies. */
2307 if ((err = copydir(vol, src, dst)) < 0) {
2311 if ((err = deletedir(src)) < 0)
2315 return( AFPERR_PARAM );
2319 vol->vfs->rf_renamedir(vol, src, dst);
2321 len = strlen( newname );
2322 /* rename() succeeded so we need to update our tree even if we can't open
2326 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2328 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2329 ad_setname(&ad, newname);
2331 ad_close_metadata( &ad);
2334 dir_hash_del(vol, dir);
2335 if (dir->d_m_name == dir->d_u_name)
2336 dir->d_u_name = NULL;
2338 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2339 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2340 /* FIXME : fatal ? */
2343 dir->d_m_name = buf;
2344 strcpy( dir->d_m_name, newname );
2346 if (newname == dst) {
2347 free(dir->d_u_name);
2348 dir->d_u_name = dir->d_m_name;
2351 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2352 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2355 dir->d_u_name = buf;
2356 strcpy( dir->d_u_name, dst );
2359 if (( parent = dir->d_parent ) == NULL ) {
2362 if ( parent == newparent ) {
2363 hash_alloc_insert(vol->v_hash, dir, dir);
2367 /* detach from old parent and add to new one. */
2368 dirchildremove(parent, dir);
2369 dir->d_parent = newparent;
2370 dirchildadd(vol, newparent, dir);
2374 /* delete an empty directory */
2375 int deletecurdir( vol)
2376 const struct vol *vol;
2386 if ( curdir->d_parent == NULL ) {
2387 return( AFPERR_ACCESS );
2392 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2393 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2395 ad_getattr(&ad, &ashort);
2396 ad_close( &ad, ADFLAGS_HF );
2397 if ((ashort & htons(ATTRBIT_NODELETE))) {
2398 return AFPERR_OLOCK;
2401 err = vol->vfs->rf_deletecurdir(vol);
2406 /* now get rid of dangling symlinks */
2407 if ((dp = opendir("."))) {
2408 while ((de = readdir(dp))) {
2409 /* skip this and previous directory */
2410 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2413 /* bail if it's not a symlink */
2414 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2416 return AFPERR_DIRNEMPT;
2419 if ((err = netatalk_unlink(de->d_name))) {
2426 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2431 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2432 dirchildremove(curdir, fdir);
2433 cnid_delete(vol->v_cdb, fdir->d_did);
2434 dir_remove( vol, fdir );
2439 /* inode is used as key for cnid.
2440 * Close the descriptor only after cnid_delete
2448 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2451 int ibuflen _U_, *rbuflen;
2461 sfunc = (unsigned char) *ibuf++;
2462 memcpy( &id, ibuf, sizeof( id ));
2467 if (sfunc == 3 || sfunc == 4) {
2468 if (afp_version < 30) {
2469 return( AFPERR_PARAM );
2476 case 3 :/* unicode */
2477 if (( pw = getpwuid( id )) == NULL ) {
2478 return( AFPERR_NOITEM );
2480 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2481 pw->pw_name, strlen(pw->pw_name), &name);
2485 case 4 : /* unicode */
2486 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2487 return( AFPERR_NOITEM );
2489 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2490 gr->gr_name, strlen(gr->gr_name), &name);
2494 return( AFPERR_PARAM );
2496 len = strlen( name );
2503 u_int16_t tp = htons(len);
2504 memcpy(rbuf, &tp, sizeof(tp));
2513 memcpy( rbuf, name, len );
2521 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2524 int ibuflen _U_, *rbuflen;
2533 sfunc = (unsigned char) *ibuf++;
2537 case 2 : /* unicode */
2538 if (afp_version < 30) {
2539 return( AFPERR_PARAM );
2541 memcpy(&ulen, ibuf, sizeof(ulen));
2547 len = (unsigned char) *ibuf++;
2550 return( AFPERR_PARAM );
2557 case 1 : /* unicode */
2559 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2560 return( AFPERR_NOITEM );
2565 case 2 : /* unicode */
2567 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2568 return( AFPERR_NOITEM );
2577 memcpy( rbuf, &id, sizeof( id ));
2578 *rbuflen = sizeof( id );
2582 /* ------------------------------------
2583 variable DID support
2585 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2587 char *ibuf _U_, *rbuf _U_;
2588 int ibuflen _U_, *rbuflen;
2599 /* do nothing as dids are static for the life of the process. */
2603 memcpy(&vid, ibuf, sizeof( vid ));
2604 ibuf += sizeof( vid );
2605 if (( vol = getvolbyvid( vid )) == NULL ) {
2606 return( AFPERR_PARAM );
2609 memcpy( &did, ibuf, sizeof( did ));
2610 ibuf += sizeof( did );
2611 if (( dir = dirlookup( vol, did )) == NULL ) {
2612 return( AFPERR_PARAM );
2615 /* dir_remove -- deletedid */
2621 /* did creation gets done automatically
2622 * there's a pb again with case but move it to cname
2624 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2627 int ibuflen _U_, *rbuflen;
2630 struct dir *parentdir;
2638 memcpy(&vid, ibuf, sizeof(vid));
2639 ibuf += sizeof( vid );
2641 if (NULL == ( vol = getvolbyvid( vid )) ) {
2642 return( AFPERR_PARAM );
2645 memcpy(&did, ibuf, sizeof(did));
2646 ibuf += sizeof(did);
2648 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2652 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2653 return get_afp_errno(AFPERR_PARAM);
2656 if ( *path->m_name != '\0' ) {
2657 return path_error(path, AFPERR_NOOBJ);
2660 if ( !path->st_valid && of_stat(path ) < 0 ) {
2661 return( AFPERR_NOOBJ );
2663 if ( path->st_errno ) {
2664 return( AFPERR_NOOBJ );
2667 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2668 *rbuflen = sizeof(curdir->d_did);