2 * $Id: directory.c,v 1.86 2008-08-31 13:25:57 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,
64 0, 0, NULL, NULL, NULL};
65 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
67 0, 0, NULL, NULL, NULL};
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( vol, dir )
447 struct ofork *of, *last;
448 struct dir *node, *leaf;
449 #endif /* REMOVE_NODES */
451 if (!dir || (dir == SENTINEL))
454 /* i'm not sure if it really helps to delete stuff. */
455 dir_hash_del(vol, dir);
458 dir->d_m_name = NULL;
459 dir->d_u_name = NULL;
460 dir->d_m_name_ucs2 = NULL;
461 #else /* ! REMOVE_NODES */
463 /* go searching for a node with at most one child */
464 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
468 while (node->d_left != SENTINEL)
473 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
476 leaf->d_back = node->d_back;
479 } else if (node == node->d_back->d_left) { /* left tree */
480 node->d_back->d_left = leaf;
482 node->d_back->d_right = leaf;
485 /* we want to free node, but we also want to free the data in dir.
486 * currently, that's d_name and the directory traversal bits.
487 * we just copy the necessary bits and then fix up all the
488 * various pointers to the directory. needless to say, there are
489 * a bunch of places that store the directory struct. */
491 struct dir save, *tmp;
493 memcpy(&save, dir, sizeof(save));
494 memcpy(dir, node, sizeof(struct dir));
496 /* restore the red-black bits */
497 dir->d_left = save.d_left;
498 dir->d_right = save.d_right;
499 dir->d_back = save.d_back;
500 dir->d_color = save.d_color;
502 if (node == vol->v_dir) {/* we may need to fix up this pointer */
504 rootpar.d_child = vol->v_dir;
506 /* if we aren't the root directory, we have parents and
507 * siblings to worry about */
508 if (dir->d_parent->d_child == node)
509 dir->d_parent->d_child = dir;
510 dir->d_next->d_prev = dir;
511 dir->d_prev->d_next = dir;
514 /* fix up children. */
518 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
521 if (node == curdir) /* another pointer to fixup */
524 /* we also need to fix up oforks. bleah */
525 if ((of = dir->d_ofork)) {
526 last = of->of_d_prev;
529 of = (last == of) ? NULL : of->of_d_next;
533 /* set the node's d_name */
534 node->d_m_name = save.d_m_name;
535 node->d_u_name = save.d_u_name;
536 node->d_m_name_ucs2 = save.d_m_name_ucs2;
539 if (node->d_color == DIRTREE_COLOR_BLACK)
540 dir_rmrecolor(vol, leaf);
542 if (node->d_m_name_ucs2)
543 free(node->d_u_name_ucs2);
544 if (node->d_u_name != node->d_m_name) {
545 free(node->d_u_name);
547 free(node->d_m_name);
549 #endif /* ! REMOVE_NODES */
552 /* ---------------------------------------
553 * remove the node and its childs from the tree
555 * FIXME what about opened forks with refs to it?
556 * it's an afp specs violation because you can't delete
557 * an opened forks. Now afpd doesn't care about forks opened by other
558 * process. It's fixable within afpd if fnctl_lock, doable with smb and
559 * next to impossible for nfs and local filesystem access.
561 static void dir_invalidate( vol, dir )
562 const struct vol *vol;
566 /* v_root can't be deleted */
567 if (movecwd(vol, vol->v_root) < 0) {
568 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
572 dirchildremove(dir->d_parent, dir);
573 dir_remove( vol, dir );
576 /* ------------------------------------ */
577 static struct dir *dir_insert(vol, dir)
578 const struct vol *vol;
584 while (pdir->d_did != dir->d_did ) {
585 if ( pdir->d_did > dir->d_did ) {
586 if ( pdir->d_left == SENTINEL ) {
593 if ( pdir->d_right == SENTINEL ) {
598 pdir = pdir->d_right;
605 * attempt to extend the current dir. tree to include path
606 * as a side-effect, movecwd to that point and return the new dir
609 extenddir( vol, dir, path )
616 if ( path->u_name == NULL) {
617 afp_errno = AFPERR_PARAM;
620 if (of_stat( path ) != 0 ) {
624 if (!S_ISDIR(path->st.st_mode)) {
628 /* mac name is always with the right encoding (from cname()) */
629 if (( dir = adddir( vol, dir, path)) == NULL ) {
634 if ( movecwd( vol, dir ) < 0 ) {
641 /* -------------------
642 system rmdir with afp error code.
643 ENOENT is not an error.
645 int netatalk_rmdir(const char *name)
647 if (rmdir(name) < 0) {
652 return AFPERR_DIRNEMPT;
655 return AFPERR_ACCESS;
665 /* -------------------------
666 appledouble mkdir afp error code.
668 static int netatalk_mkdir(const char *name)
670 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
673 return( AFPERR_NOOBJ );
675 return( AFPERR_VLOCK );
678 return( AFPERR_ACCESS );
680 return( AFPERR_EXIST );
683 return( AFPERR_DFULL );
685 return( AFPERR_PARAM );
691 /* -------------------
692 system unlink with afp error code.
693 ENOENT is not an error.
695 int netatalk_unlink(const char *name)
697 if (unlink(name) < 0) {
705 return AFPERR_ACCESS;
713 /* ------------------- */
714 static int deletedir(char *dir)
716 char path[MAXPATHLEN + 1];
724 if ((len = strlen(dir)) +2 > sizeof(path))
728 if ((dp = opendir(dir)) == NULL)
734 remain = sizeof(path) -len -1;
735 while ((de = readdir(dp)) && err == AFP_OK) {
736 /* skip this and previous directory */
737 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
740 if (strlen(de->d_name) > remain) {
744 strcpy(path + len, de->d_name);
745 if (stat(path, &st)) {
748 if (S_ISDIR(st.st_mode)) {
749 err = deletedir(path);
751 err = netatalk_unlink(path);
756 /* okay. the directory is empty. delete it. note: we already got rid
759 err = netatalk_rmdir(dir);
764 /* do a recursive copy. */
765 static int copydir(const struct vol *vol, char *src, char *dst)
767 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
776 /* doesn't exist or the path is too long. */
777 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
778 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
779 ((dp = opendir(src)) == NULL))
782 /* try to create the destination directory */
783 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
788 /* set things up to copy */
792 srem = sizeof(spath) - slen -1;
797 drem = sizeof(dpath) - dlen -1;
800 while ((de = readdir(dp))) {
801 /* skip this and previous directory */
802 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
805 if (strlen(de->d_name) > srem) {
809 strcpy(spath + slen, de->d_name);
811 if (stat(spath, &st) == 0) {
812 if (strlen(de->d_name) > drem) {
816 strcpy(dpath + dlen, de->d_name);
818 if (S_ISDIR(st.st_mode)) {
819 if (AFP_OK != (err = copydir(vol, spath, dpath)))
821 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
825 /* keep the same time stamp. */
826 ut.actime = ut.modtime = st.st_mtime;
832 /* keep the same time stamp. */
833 if (stat(src, &st) == 0) {
834 ut.actime = ut.modtime = st.st_mtime;
844 /* --- public functions follow --- */
846 /* NOTE: we start off with at least one node (the root directory). */
847 static struct dir *dirinsert( vol, dir )
853 if ((node = dir_insert(vol, dir)))
856 /* recolor the tree. the current node is red. */
857 dir->d_color = DIRTREE_COLOR_RED;
859 /* parent of this node has to be black. if the parent node
860 * is red, then we have a grandparent. */
861 while ((dir != vol->v_root) &&
862 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
863 /* are we on the left tree? */
864 if (dir->d_back == dir->d_back->d_back->d_left) {
865 node = dir->d_back->d_back->d_right; /* get the right node */
866 if (node->d_color == DIRTREE_COLOR_RED) {
867 /* we're red. we need to change to black. */
868 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
869 node->d_color = DIRTREE_COLOR_BLACK;
870 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
871 dir = dir->d_back->d_back; /* finished. go up. */
873 if (dir == dir->d_back->d_right) {
875 dir_leftrotate(vol, dir);
877 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
878 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
879 dir_rightrotate(vol, dir->d_back->d_back);
882 node = dir->d_back->d_back->d_left;
883 if (node->d_color == DIRTREE_COLOR_RED) {
884 /* we're red. we need to change to black. */
885 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
886 node->d_color = DIRTREE_COLOR_BLACK;
887 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
888 dir = dir->d_back->d_back; /* finished. ascend */
890 if (dir == dir->d_back->d_left) {
892 dir_rightrotate(vol, dir);
894 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
895 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
896 dir_leftrotate(vol, dir->d_back->d_back);
901 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
905 /* ---------------------------- */
907 adddir( vol, dir, path)
912 struct dir *cdir, *edir;
920 upath = path->u_name;
922 upathlen = strlen(upath);
924 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
928 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
932 if ((cdir = dirnew(name, upath)) == NULL) {
933 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
936 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, path->m_name, strlen(path->m_name), &cdir->d_m_name_ucs2)) {
937 LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
938 cdir->d_m_name_ucs2 = NULL;
943 if ((edir = dirinsert( vol, cdir ))) {
944 /* it's not possible with LASTDID
946 - someone else have moved the directory.
947 - it's a symlink inside the share.
948 - it's an ID reused, the old directory was deleted but not
949 the cnid record and the server've reused the inode for
951 for HASH (we should get ride of HASH)
952 - someone else have moved the directory.
953 - it's an ID reused as above
954 - it's a hash duplicate and we are in big trouble
956 deleted = (edir->d_m_name == NULL);
958 dir_hash_del(vol, edir);
960 edir->d_m_name = cdir->d_m_name;
961 edir->d_u_name = cdir->d_u_name;
962 edir->d_m_name_ucs2 = cdir->d_m_name_ucs2;
965 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
966 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
967 hash_alloc_insert(vol->v_hash, cdir, cdir);
970 /* the old was not in the same folder */
972 dirchildremove(cdir->d_parent, cdir);
975 /* parent/child directories */
976 cdir->d_parent = dir;
977 dirchildadd(vol, dir, cdir);
981 /* --- public functions follow --- */
982 /* free everything down. we don't bother to recolor as this is only
983 * called to free the entire tree */
984 void dirfreename(struct dir *dir)
986 if (dir->d_u_name != dir->d_m_name) {
989 if (dir->d_m_name_ucs2)
990 free(dir->d_m_name_ucs2);
997 if (!dir || (dir == SENTINEL))
1000 if ( dir->d_left != SENTINEL ) {
1001 dirfree( dir->d_left );
1003 if ( dir->d_right != SENTINEL ) {
1004 dirfree( dir->d_right );
1007 if (dir != SENTINEL) {
1013 /* --------------------------------------------
1014 * most of the time mac name and unix name are the same
1016 struct dir *dirnew(const char *m_name, const char *u_name)
1020 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1024 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1029 if (m_name == u_name || !strcmp(m_name, u_name)) {
1030 dir->d_u_name = dir->d_m_name;
1032 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1033 free(dir->d_m_name);
1038 dir->d_m_name_ucs2 = NULL;
1039 dir->d_left = dir->d_right = SENTINEL;
1040 dir->d_next = dir->d_prev = dir;
1044 /* ------------------ */
1045 static hash_val_t hash_fun_dir(const void *key)
1047 const struct dir *k = key;
1049 static unsigned long randbox[] = {
1050 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1051 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1052 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1053 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1056 const unsigned char *str = k->d_u_name;
1060 acc ^= randbox[(*str + acc) & 0xf];
1061 acc = (acc << 1) | (acc >> 31);
1063 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1064 acc = (acc << 2) | (acc >> 30);
1070 /* ---------------- */
1071 static int hash_comp_dir(const void *key1, const void *key2)
1073 const struct dir *k1 = key1;
1074 const struct dir *k2 = key2;
1076 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1079 /* ---------------- */
1083 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1086 /* ------------------ */
1087 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1090 movecwd failed some of dir path are not there anymore.
1091 FIXME Is it true with other errors?
1092 so we remove dir from the cache
1094 if (dir->d_did == DIRDID_ROOT_PARENT)
1096 if (afp_errno == AFPERR_ACCESS) {
1097 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1100 /* FIXME should we set these?, don't need to call stat() after:
1102 ret->st_errno = EACCES;
1104 ret->m_name = dir->d_m_name;
1105 ret->u_name = dir->d_u_name;
1108 } else if (afp_errno == AFPERR_NOOBJ) {
1109 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1112 strcpy(ret->m_name, dir->d_m_name);
1113 if (dir->d_m_name == dir->d_u_name) {
1114 ret->u_name = ret->m_name;
1117 size_t tp = strlen(ret->m_name)+1;
1119 ret->u_name = ret->m_name +tp;
1120 strcpy(ret->u_name, dir->d_u_name);
1122 /* FIXME should we set :
1124 ret->st_errno = ENOENT;
1126 dir_invalidate(vol, dir);
1129 dir_invalidate(vol, dir);
1133 /* -------------------------------------------------- */
1139 stat the file or errno
1142 curdir: filename parent directory
1148 stat the dir or errno
1152 curdir: dir parent directory
1160 curdir: dir parent directory
1167 cname( vol, dir, cpath )
1168 const struct vol *vol;
1173 static char path[ MAXPATHLEN + 1];
1174 static struct path ret;
1186 afp_errno = AFPERR_NOOBJ;
1187 memset(&ret, 0, sizeof(ret));
1188 switch (ret.m_type = *data) { /* path type */
1191 len = (unsigned char) *data++;
1194 if (afp_version >= 30) {
1200 if (afp_version >= 30) {
1202 memcpy(&hint, data, sizeof(hint));
1204 data += sizeof(hint);
1206 memcpy(&len16, data, sizeof(len16));
1213 /* else it's an error */
1215 afp_errno = AFPERR_PARAM;
1218 *cpath += len + size;
1223 if (movecwd( vol, dir ) < 0 ) {
1224 return invalidate(vol, dir, &ret );
1226 if (*path == '\0') {
1233 if (*data == sep ) {
1237 while (*data == sep && len > 0 ) {
1238 if ( dir->d_parent == NULL ) {
1241 dir = dir->d_parent;
1246 /* would this be faster with strlen + strncpy? */
1248 while ( *data != sep && len > 0 ) {
1250 if (p > &path[ MAXPATHLEN]) {
1251 afp_errno = AFPERR_PARAM;
1257 /* short cut bits by chopping off a trailing \0. this also
1258 makes the traversal happy w/ filenames at the end of the
1265 if ( p == path ) { /* end of the name parameter */
1269 if (afp_version >= 30) {
1274 static char temp[ MAXPATHLEN + 1];
1276 /* not an UTF8 name */
1277 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1278 afp_errno = AFPERR_PARAM;
1283 /* check for OS X mangled filename :( */
1285 t = demangle_osx(vol, path, dir->d_did, &fileid);
1288 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1289 * flags weren't the same
1291 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1292 /* at last got our view of mac name */
1297 if (ret.u_name == NULL) {
1298 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1299 afp_errno = AFPERR_PARAM;
1304 if (dir->d_did == DIRDID_ROOT_PARENT) {
1305 /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
1306 * d_m_name is the Mac volume name
1307 * d_u_name is the volume unix directory name
1311 if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
1316 cdir = dirsearch_byname(vol, dir, ret.u_name);
1319 if ( cdir == NULL ) {
1321 /* if dir == curdir it always succeed,
1322 even if curdir is deleted.
1323 it's not a pb because it will fail in extenddir
1325 if ( movecwd( vol, dir ) < 0 ) {
1326 /* dir is not valid anymore
1327 we delete dir from the cache and abort.
1329 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1330 afp_errno = AFPERR_NOOBJ;
1333 if (afp_errno == AFPERR_ACCESS)
1335 dir_invalidate(vol, dir);
1338 cdir = extenddir( vol, dir, &ret );
1342 cdir = extenddir( vol, dir, &ret );
1343 } /* if (!extend) */
1345 if ( cdir == NULL ) {
1359 * Move curdir to dir, with a possible chdir()
1361 int movecwd( vol, dir)
1362 const struct vol *vol;
1365 char path[MAXPATHLEN + 1];
1370 if ( dir == curdir ) {
1373 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1374 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1378 p = path + sizeof(path) - 1;
1381 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1384 /* parent directory is deleted */
1385 afp_errno = AFPERR_NOOBJ;
1389 if (p -n -1 < path) {
1390 afp_errno = AFPERR_PARAM;
1397 if ( d != curdir ) {
1398 n = strlen( vol->v_path );
1399 if (p -n -1 < path) {
1400 afp_errno = AFPERR_PARAM;
1405 memcpy( p, vol->v_path, n );
1407 if ( chdir( p ) < 0 ) {
1411 afp_errno = AFPERR_ACCESS;
1414 afp_errno = AFPERR_NOOBJ;
1424 * We can't use unix file's perm to support Apple's inherited protection modes.
1425 * If we aren't the file's owner we can't change its perms when moving it and smb
1426 * nfs,... don't even try.
1428 #define AFP_CHECK_ACCESS
1430 int check_access(char *path, int mode)
1432 #ifdef AFP_CHECK_ACCESS
1440 accessmode(p, &ma, curdir, NULL);
1441 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1443 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1449 /* --------------------- */
1450 int file_access(struct path *path, int mode)
1454 accessmode(path->u_name, &ma, curdir, &path->st);
1455 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1457 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1463 /* --------------------- */
1464 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1466 dir->offcnt = count;
1467 dir->ctime = st->st_ctime;
1468 dir->d_flags &= ~DIRF_CNID;
1471 /* ---------------------
1472 * is our cached offspring count valid?
1475 int diroffcnt(struct dir *dir, struct stat *st)
1477 return st->st_ctime == dir->ctime;
1480 /* ---------------------
1481 * is our cached also for reenumerate id?
1484 int dirreenumerate(struct dir *dir, struct stat *st)
1486 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1489 /* --------------------- */
1490 static int invisible_dots(const struct vol *vol, const char *name)
1492 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1495 /* ------------------------------
1497 (name, dir) with curdir:name == dir, from afp_enumerate
1500 int getdirparams(const struct vol *vol,
1501 u_int16_t bitmap, struct path *s_path,
1503 char *buf, int *buflen )
1507 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1508 int bit = 0, isad = 0;
1514 struct stat *st = &s_path->st;
1515 char *upath = s_path->u_name;
1517 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1518 (1 << DIRPBIT_CDATE) |
1519 (1 << DIRPBIT_MDATE) |
1520 (1 << DIRPBIT_BDATE) |
1521 (1 << DIRPBIT_FINFO)))) {
1523 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1524 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1529 if ( dir->d_did == DIRDID_ROOT) {
1530 pdid = DIRDID_ROOT_PARENT;
1531 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1534 pdid = dir->d_parent->d_did;
1538 while ( bitmap != 0 ) {
1539 while (( bitmap & 1 ) == 0 ) {
1547 ad_getattr(&ad, &ashort);
1548 } else if (invisible_dots(vol, dir->d_u_name)) {
1549 ashort = htons(ATTRBIT_INVISIBLE);
1552 ashort |= htons(ATTRBIT_SHARED);
1553 memcpy( data, &ashort, sizeof( ashort ));
1554 data += sizeof( ashort );
1558 memcpy( data, &pdid, sizeof( pdid ));
1559 data += sizeof( pdid );
1562 case DIRPBIT_CDATE :
1563 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1564 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1565 memcpy( data, &aint, sizeof( aint ));
1566 data += sizeof( aint );
1569 case DIRPBIT_MDATE :
1570 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1571 memcpy( data, &aint, sizeof( aint ));
1572 data += sizeof( aint );
1575 case DIRPBIT_BDATE :
1576 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1577 aint = AD_DATE_START;
1578 memcpy( data, &aint, sizeof( aint ));
1579 data += sizeof( aint );
1582 case DIRPBIT_FINFO :
1584 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1585 } else { /* no appledouble */
1586 memset( data, 0, 32 );
1587 /* set default view -- this also gets done in ad_open() */
1588 ashort = htons(FINDERINFO_CLOSEDVIEW);
1589 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1591 /* dot files are by default visible */
1592 if (invisible_dots(vol, dir->d_u_name)) {
1593 ashort = htons(FINDERINFO_INVISIBLE);
1594 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1600 case DIRPBIT_LNAME :
1601 if (dir->d_m_name) /* root of parent can have a null name */
1604 memset(data, 0, sizeof(u_int16_t));
1605 data += sizeof( u_int16_t );
1608 case DIRPBIT_SNAME :
1609 memset(data, 0, sizeof(u_int16_t));
1610 data += sizeof( u_int16_t );
1614 memcpy( data, &dir->d_did, sizeof( aint ));
1615 data += sizeof( aint );
1618 case DIRPBIT_OFFCNT :
1620 /* this needs to handle current directory access rights */
1621 if (diroffcnt(dir, st)) {
1622 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1624 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1625 setdiroffcnt(dir, st, ret);
1626 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1628 ashort = htons( ashort );
1629 memcpy( data, &ashort, sizeof( ashort ));
1630 data += sizeof( ashort );
1634 aint = htonl(st->st_uid);
1635 memcpy( data, &aint, sizeof( aint ));
1636 data += sizeof( aint );
1640 aint = htonl(st->st_gid);
1641 memcpy( data, &aint, sizeof( aint ));
1642 data += sizeof( aint );
1645 case DIRPBIT_ACCESS :
1646 accessmode( upath, &ma, dir , st);
1648 *data++ = ma.ma_user;
1649 *data++ = ma.ma_world;
1650 *data++ = ma.ma_group;
1651 *data++ = ma.ma_owner;
1654 /* Client has requested the ProDOS information block.
1655 Just pass back the same basic block for all
1656 directories. <shirsch@ibm.net> */
1657 case DIRPBIT_PDINFO :
1658 if (afp_version >= 30) { /* UTF8 name */
1659 utf8 = kTextEncodingUTF8;
1660 if (dir->d_m_name) /* root of parent can have a null name */
1663 memset(data, 0, sizeof(u_int16_t));
1664 data += sizeof( u_int16_t );
1666 memcpy(data, &aint, sizeof( aint ));
1667 data += sizeof( aint );
1669 else { /* ProDOS Info Block */
1672 ashort = htons( 0x0200 );
1673 memcpy( data, &ashort, sizeof( ashort ));
1674 data += sizeof( ashort );
1675 memset( data, 0, sizeof( ashort ));
1676 data += sizeof( ashort );
1680 case DIRPBIT_UNIXPR :
1681 aint = htonl(st->st_uid);
1682 memcpy( data, &aint, sizeof( aint ));
1683 data += sizeof( aint );
1684 aint = htonl(st->st_gid);
1685 memcpy( data, &aint, sizeof( aint ));
1686 data += sizeof( aint );
1689 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1690 memcpy( data, &aint, sizeof( aint ));
1691 data += sizeof( aint );
1693 accessmode( upath, &ma, dir , st);
1695 *data++ = ma.ma_user;
1696 *data++ = ma.ma_world;
1697 *data++ = ma.ma_group;
1698 *data++ = ma.ma_owner;
1703 ad_close_metadata( &ad );
1705 return( AFPERR_BITMAP );
1711 ashort = htons( data - buf );
1712 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1713 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1715 if ( utf_nameoff ) {
1716 ashort = htons( data - buf );
1717 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1718 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1721 ad_close_metadata( &ad );
1723 *buflen = data - buf;
1727 /* ----------------------------- */
1728 int path_error(struct path *path, int error)
1730 /* - a dir with access error
1731 * - no error it's a file
1734 if (path_isadir(path))
1736 if (path->st_valid && path->st_errno)
1738 return AFPERR_BADTYPE ;
1741 /* ----------------------------- */
1742 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1744 char *ibuf, *rbuf _U_;
1745 int ibuflen _U_, *rbuflen;
1750 u_int16_t vid, bitmap;
1756 memcpy( &vid, ibuf, sizeof( vid ));
1757 ibuf += sizeof( vid );
1759 if (NULL == ( vol = getvolbyvid( vid )) ) {
1760 return( AFPERR_PARAM );
1763 if (vol->v_flags & AFPVOL_RO)
1764 return AFPERR_VLOCK;
1766 memcpy( &did, ibuf, sizeof( did ));
1767 ibuf += sizeof( int );
1769 if (NULL == ( dir = dirlookup( vol, did )) ) {
1773 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1774 bitmap = ntohs( bitmap );
1775 ibuf += sizeof( bitmap );
1777 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1778 return get_afp_errno(AFPERR_NOOBJ);
1781 if ( *path->m_name != '\0' ) {
1782 rc = path_error(path, AFPERR_NOOBJ);
1783 /* maybe we are trying to set perms back */
1784 if (rc != AFPERR_ACCESS)
1789 * If ibuf is odd, make it even.
1791 if ((u_long)ibuf & 1 ) {
1795 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1796 setvoltime(obj, vol );
1802 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1804 * assume path == '\0' eg. it's a directory in canonical form
1807 struct path Cur_Path = {
1810 ".", /* unix name */
1812 NULL,/* struct dir */
1813 0, /* stat is not set */
1816 /* ------------------ */
1817 static int set_dir_errors(struct path *path, const char *where, int err)
1822 return AFPERR_ACCESS;
1824 return AFPERR_VLOCK;
1826 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1827 return AFPERR_PARAM;
1830 /* ------------------ */
1831 int setdirparams(const struct vol *vol,
1832 struct path *path, u_int16_t d_bitmap, char *buf )
1841 int bit, aint, isad = 1;
1844 u_int16_t ashort, bshort;
1846 int change_mdate = 0;
1847 int change_parent_mdate = 0;
1849 u_int16_t bitmap = d_bitmap;
1850 u_char finder_buf[32];
1853 u_int16_t upriv_bit = 0;
1856 upath = path->u_name;
1858 while ( bitmap != 0 ) {
1859 while (( bitmap & 1 ) == 0 ) {
1867 memcpy( &ashort, buf, sizeof( ashort ));
1868 buf += sizeof( ashort );
1870 case DIRPBIT_CDATE :
1872 memcpy(&cdate, buf, sizeof(cdate));
1873 buf += sizeof( cdate );
1875 case DIRPBIT_MDATE :
1876 memcpy(&newdate, buf, sizeof(newdate));
1877 buf += sizeof( newdate );
1879 case DIRPBIT_BDATE :
1881 memcpy(&bdate, buf, sizeof(bdate));
1882 buf += sizeof( bdate );
1884 case DIRPBIT_FINFO :
1886 memcpy( finder_buf, buf, 32 );
1889 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1890 change_parent_mdate = 1;
1891 memcpy( &owner, buf, sizeof(owner));
1892 buf += sizeof( owner );
1895 change_parent_mdate = 1;
1896 memcpy( &group, buf, sizeof( group ));
1897 buf += sizeof( group );
1899 case DIRPBIT_ACCESS :
1901 change_parent_mdate = 1;
1902 ma.ma_user = *buf++;
1903 ma.ma_world = *buf++;
1904 ma.ma_group = *buf++;
1905 ma.ma_owner = *buf++;
1906 mpriv = mtoumode( &ma ) | vol->v_perm;
1907 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1908 err = set_dir_errors(path, "setdirmode", errno);
1912 /* Ignore what the client thinks we should do to the
1913 ProDOS information block. Skip over the data and
1914 report nothing amiss. <shirsch@ibm.net> */
1915 case DIRPBIT_PDINFO :
1916 if (afp_version < 30) {
1920 err = AFPERR_BITMAP;
1924 case DIRPBIT_UNIXPR :
1925 if (vol_unix_priv(vol)) {
1926 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1927 buf += sizeof( owner );
1928 memcpy( &group, buf, sizeof( group ));
1929 buf += sizeof( group );
1932 change_parent_mdate = 1;
1933 memcpy( &upriv, buf, sizeof( upriv ));
1934 buf += sizeof( upriv );
1935 upriv = ntohl (upriv) | vol->v_perm;
1936 if (dir_rx_set(upriv)) {
1937 /* maybe we are trying to set perms back */
1938 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1940 err = set_dir_errors(path, "setdirunixmode", errno);
1951 err = AFPERR_BITMAP;
1959 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1961 if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1963 * Check to see what we're trying to set. If it's anything
1964 * but ACCESS, UID, or GID, give an error. If it's any of those
1965 * three, we don't need the ad to be open, so just continue.
1967 * note: we also don't need to worry about mdate. also, be quiet
1968 * if we're using the noadouble option.
1970 if (!vol_noadouble(vol) && (d_bitmap &
1971 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1972 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1973 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1974 return AFPERR_ACCESS;
1980 * Check to see if a create was necessary. If it was, we'll want
1981 * to set our name, etc.
1983 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1984 ad_setname(&ad, curdir->d_m_name);
1990 while ( bitmap != 0 ) {
1991 while (( bitmap & 1 ) == 0 ) {
1999 ad_getattr(&ad, &bshort);
2000 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
2001 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
2002 change_parent_mdate = 1;
2003 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
2004 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
2008 ad_setattr(&ad, bshort);
2011 case DIRPBIT_CDATE :
2013 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2016 case DIRPBIT_MDATE :
2018 case DIRPBIT_BDATE :
2020 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2023 case DIRPBIT_FINFO :
2025 if ( dir->d_did == DIRDID_ROOT ) {
2027 * Alright, we admit it, this is *really* sick!
2028 * The 4 bytes that we don't copy, when we're dealing
2029 * with the root of a volume, are the directory's
2030 * location information. This eliminates that annoying
2031 * behavior one sees when mounting above another mount
2034 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2035 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2037 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2041 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2042 if ( (dir->d_did == DIRDID_ROOT) &&
2043 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2044 err = set_dir_errors(path, "setdeskowner", errno);
2045 if (isad && err == AFPERR_PARAM) {
2046 err = AFP_OK; /* ???*/
2049 goto setdirparam_done;
2052 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2053 err = set_dir_errors(path, "setdirowner", errno);
2054 goto setdirparam_done;
2058 if (dir->d_did == DIRDID_ROOT)
2059 setdeskowner( -1, ntohl(group) );
2060 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2061 err = set_dir_errors(path, "setdirowner", errno);
2062 goto setdirparam_done;
2065 case DIRPBIT_ACCESS :
2066 if (dir->d_did == DIRDID_ROOT) {
2068 if (!dir_rx_set(mpriv)) {
2069 /* we can't remove read and search for owner on volume root */
2070 err = AFPERR_ACCESS;
2071 goto setdirparam_done;
2075 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2076 err = set_dir_errors(path, "setdirmode", errno);
2077 goto setdirparam_done;
2080 case DIRPBIT_PDINFO :
2081 if (afp_version >= 30) {
2082 err = AFPERR_BITMAP;
2083 goto setdirparam_done;
2086 case DIRPBIT_UNIXPR :
2087 if (vol_unix_priv(vol)) {
2088 if (dir->d_did == DIRDID_ROOT) {
2089 if (!dir_rx_set(upriv)) {
2090 /* we can't remove read and search for owner on volume root */
2091 err = AFPERR_ACCESS;
2092 goto setdirparam_done;
2094 setdeskowner( -1, ntohl(group) );
2095 setdeskmode( upriv );
2097 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2098 err = set_dir_errors(path, "setdirowner", errno);
2099 goto setdirparam_done;
2102 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2103 err = set_dir_errors(path, "setdirunixmode", errno);
2104 goto setdirparam_done;
2108 err = AFPERR_BITMAP;
2109 goto setdirparam_done;
2113 err = AFPERR_BITMAP;
2114 goto setdirparam_done;
2123 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2124 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2128 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2129 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2134 if (path->st_valid && !path->st_errno) {
2135 struct stat *st = &path->st;
2137 if (dir && dir->d_parent) {
2138 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2142 ad_close_metadata( &ad);
2145 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2146 && gettimeofday(&tv, NULL) == 0) {
2147 if (!movecwd(vol, dir->d_parent)) {
2148 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2149 /* be careful with bitmap because now dir is null */
2150 bitmap = 1<<DIRPBIT_MDATE;
2151 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2152 /* should we reset curdir ?*/
2160 int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2163 int ibuflen _U_, *rbuflen;
2175 memcpy( &vid, ibuf, sizeof( vid ));
2176 ibuf += sizeof( vid );
2177 if (NULL == (vol = getvolbyvid( vid )) ) {
2178 return( AFPERR_PARAM );
2181 memcpy( &did, ibuf, sizeof( did ));
2182 ibuf += sizeof( did );
2183 if (NULL == ( dir = dirlookup( vol, did )) ) {
2184 return afp_errno; /* was AFPERR_NOOBJ */
2187 if (NULL == ( dp = opendir( "." )) ) {
2190 return( AFPERR_NOOBJ );
2192 return( AFPERR_ACCESS );
2194 return( AFPERR_PARAM );
2199 if ( fsync ( dfd ) < 0 ) {
2200 LOG(log_error, logtype_afpd, "syncdir(%s): ddir(%d) %s", dir->d_u_name, dfd, strerror(errno) );
2206 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2209 int ibuflen _U_, *rbuflen;
2215 struct path *s_path;
2223 memcpy( &vid, ibuf, sizeof( vid ));
2224 ibuf += sizeof( vid );
2225 if (NULL == ( vol = getvolbyvid( vid )) ) {
2226 return( AFPERR_PARAM );
2229 if (vol->v_flags & AFPVOL_RO)
2230 return AFPERR_VLOCK;
2232 memcpy( &did, ibuf, sizeof( did ));
2233 ibuf += sizeof( did );
2234 if (NULL == ( dir = dirlookup( vol, did )) ) {
2235 return afp_errno; /* was AFPERR_NOOBJ */
2237 /* for concurrent access we need to be sure we are not in the
2238 * folder we want to create...
2242 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2243 return get_afp_errno(AFPERR_PARAM);
2245 /* cname was able to move curdir to it! */
2246 if (*s_path->m_name == '\0')
2247 return AFPERR_EXIST;
2249 upath = s_path->u_name;
2250 if (0 != (err = check_name(vol, upath))) {
2254 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2258 if (of_stat(s_path) < 0) {
2262 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2266 if ( movecwd( vol, dir ) < 0 ) {
2267 return( AFPERR_PARAM );
2270 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2271 if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2272 if (vol_noadouble(vol))
2273 goto createdir_done;
2274 return( AFPERR_ACCESS );
2276 ad_setname(&ad, s_path->m_name);
2277 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2280 ad_close_metadata( &ad);
2283 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2284 *rbuflen = sizeof( u_int32_t );
2285 setvoltime(obj, vol );
2290 * dst new unix filename (not a pathname)
2291 * newname new mac name
2295 int renamedir(vol, src, dst, dir, newparent, newname)
2296 const struct vol *vol;
2297 char *src, *dst, *newname;
2298 struct dir *dir, *newparent;
2305 /* existence check moved to afp_moveandrename */
2306 if ( unix_rename( src, dst ) < 0 ) {
2309 return( AFPERR_NOOBJ );
2311 return( AFPERR_ACCESS );
2313 return AFPERR_VLOCK;
2315 /* tried to move directory into a subdirectory of itself */
2316 return AFPERR_CANTMOVE;
2318 /* this needs to copy and delete. bleah. that means we have
2319 * to deal with entire directory hierarchies. */
2320 if ((err = copydir(vol, src, dst)) < 0) {
2324 if ((err = deletedir(src)) < 0)
2328 return( AFPERR_PARAM );
2332 vol->vfs->rf_renamedir(vol, src, dst);
2334 len = strlen( newname );
2335 /* rename() succeeded so we need to update our tree even if we can't open
2339 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2341 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2342 ad_setname(&ad, newname);
2344 ad_close_metadata( &ad);
2347 dir_hash_del(vol, dir);
2348 if (dir->d_m_name == dir->d_u_name)
2349 dir->d_u_name = NULL;
2351 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2352 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2353 /* FIXME : fatal ? */
2356 dir->d_m_name = buf;
2357 strcpy( dir->d_m_name, newname );
2359 if (newname == dst) {
2360 free(dir->d_u_name);
2361 dir->d_u_name = dir->d_m_name;
2364 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2365 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2368 dir->d_u_name = buf;
2369 strcpy( dir->d_u_name, dst );
2372 if (dir->d_m_name_ucs2)
2373 free(dir->d_m_name_ucs2);
2375 dir->d_m_name_ucs2 = NULL;
2376 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, strlen(dir->d_m_name), (char**)&dir->d_m_name_ucs2))
2377 dir->d_m_name_ucs2 = NULL;
2379 if (( parent = dir->d_parent ) == NULL ) {
2382 if ( parent == newparent ) {
2383 hash_alloc_insert(vol->v_hash, dir, dir);
2387 /* detach from old parent and add to new one. */
2388 dirchildremove(parent, dir);
2389 dir->d_parent = newparent;
2390 dirchildadd(vol, newparent, dir);
2394 /* delete an empty directory */
2395 int deletecurdir( vol)
2396 const struct vol *vol;
2406 if ( curdir->d_parent == NULL ) {
2407 return( AFPERR_ACCESS );
2412 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2413 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2415 ad_getattr(&ad, &ashort);
2416 ad_close( &ad, ADFLAGS_HF );
2417 if ((ashort & htons(ATTRBIT_NODELETE))) {
2418 return AFPERR_OLOCK;
2421 err = vol->vfs->rf_deletecurdir(vol);
2426 /* now get rid of dangling symlinks */
2427 if ((dp = opendir("."))) {
2428 while ((de = readdir(dp))) {
2429 /* skip this and previous directory */
2430 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2433 /* bail if it's not a symlink */
2434 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2436 return AFPERR_DIRNEMPT;
2439 if ((err = netatalk_unlink(de->d_name))) {
2446 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2451 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2452 dirchildremove(curdir, fdir);
2453 cnid_delete(vol->v_cdb, fdir->d_did);
2454 dir_remove( vol, fdir );
2459 /* inode is used as key for cnid.
2460 * Close the descriptor only after cnid_delete
2468 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2471 int ibuflen _U_, *rbuflen;
2481 sfunc = (unsigned char) *ibuf++;
2482 memcpy( &id, ibuf, sizeof( id ));
2487 if (sfunc == 3 || sfunc == 4) {
2488 if (afp_version < 30) {
2489 return( AFPERR_PARAM );
2496 case 3 :/* unicode */
2497 if (( pw = getpwuid( id )) == NULL ) {
2498 return( AFPERR_NOITEM );
2500 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2501 pw->pw_name, strlen(pw->pw_name), &name);
2505 case 4 : /* unicode */
2506 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2507 return( AFPERR_NOITEM );
2509 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2510 gr->gr_name, strlen(gr->gr_name), &name);
2514 return( AFPERR_PARAM );
2516 len = strlen( name );
2523 u_int16_t tp = htons(len);
2524 memcpy(rbuf, &tp, sizeof(tp));
2533 memcpy( rbuf, name, len );
2541 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2544 int ibuflen _U_, *rbuflen;
2553 sfunc = (unsigned char) *ibuf++;
2557 case 2 : /* unicode */
2558 if (afp_version < 30) {
2559 return( AFPERR_PARAM );
2561 memcpy(&ulen, ibuf, sizeof(ulen));
2567 len = (unsigned char) *ibuf++;
2570 return( AFPERR_PARAM );
2577 case 1 : /* unicode */
2579 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2580 return( AFPERR_NOITEM );
2585 case 2 : /* unicode */
2587 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2588 return( AFPERR_NOITEM );
2597 memcpy( rbuf, &id, sizeof( id ));
2598 *rbuflen = sizeof( id );
2602 /* ------------------------------------
2603 variable DID support
2605 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2607 char *ibuf _U_, *rbuf _U_;
2608 int ibuflen _U_, *rbuflen;
2619 /* do nothing as dids are static for the life of the process. */
2623 memcpy(&vid, ibuf, sizeof( vid ));
2624 ibuf += sizeof( vid );
2625 if (( vol = getvolbyvid( vid )) == NULL ) {
2626 return( AFPERR_PARAM );
2629 memcpy( &did, ibuf, sizeof( did ));
2630 ibuf += sizeof( did );
2631 if (( dir = dirlookup( vol, did )) == NULL ) {
2632 return( AFPERR_PARAM );
2635 /* dir_remove -- deletedid */
2641 /* did creation gets done automatically
2642 * there's a pb again with case but move it to cname
2644 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2647 int ibuflen _U_, *rbuflen;
2650 struct dir *parentdir;
2658 memcpy(&vid, ibuf, sizeof(vid));
2659 ibuf += sizeof( vid );
2661 if (NULL == ( vol = getvolbyvid( vid )) ) {
2662 return( AFPERR_PARAM );
2665 memcpy(&did, ibuf, sizeof(did));
2666 ibuf += sizeof(did);
2668 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2672 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2673 return get_afp_errno(AFPERR_PARAM);
2676 if ( *path->m_name != '\0' ) {
2677 return path_error(path, AFPERR_NOOBJ);
2680 if ( !path->st_valid && of_stat(path ) < 0 ) {
2681 return( AFPERR_NOOBJ );
2683 if ( path->st_errno ) {
2684 return( AFPERR_NOOBJ );
2687 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2688 *rbuflen = sizeof(curdir->d_did);