2 * $Id: directory.c,v 1.91 2009-02-02 11:55:00 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
38 #include <sys/param.h>
41 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/logger.h>
47 #include <atalk/uuid.h>
49 #include "directory.h"
59 #ifdef HAVE_NFSv4_ACLS
60 extern void addir_inherit_acl(const struct vol *vol);
66 #define SENTINEL (&sentinel)
67 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
68 NULL, NULL, NULL, NULL, NULL, 0, 0,
70 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
71 NULL, NULL, NULL, NULL, NULL, 0, 0,
74 /* (from IM: Toolbox Essentials)
75 * dirFinderInfo (DInfo) fields:
77 * frRect 8 folder's window rectangle
79 * frLocation 4 folder's location in window
80 * frView 2 folder's view (default == closedView (256))
82 * extended dirFinderInfo (DXInfo) fields:
83 * frScroll 4 scroll position
84 * frOpenChain: 4 directory ID chain of open folders
85 * frScript: 1 script flag and code
86 * frXFlags: 1 reserved
87 * frComment: 2 comment ID
88 * frPutAway: 4 home directory ID
92 * redid did assignment for directories. now we use red-black trees.
97 const struct vol *vol;
103 /* check for 0 did */
105 afp_errno = AFPERR_PARAM;
108 if ( did == DIRDID_ROOT_PARENT ) {
110 rootpar.d_did = DIRDID_ROOT_PARENT;
111 rootpar.d_child = vol->v_dir;
115 /* XXX would be nice to check against curdir but we need to keep its volume */
116 if (vol->curdir && curdir->d_did == did) {
125 afp_errno = AFPERR_NOOBJ;
126 while ( dir != SENTINEL ) {
127 if (dir->d_did == did)
128 return dir->d_m_name ? dir : NULL;
129 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
134 /* ------------------- */
136 int path_isadir(struct path *o_path)
138 return o_path->d_dir != NULL;
140 return o_path->m_name == '\0' || /* we are in a it */
141 !o_path->st_valid || /* in cache but we can't chdir in it */
142 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
147 int get_afp_errno(const int param)
149 if (afp_errno != AFPERR_DID1)
154 /* ------------------- */
156 dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
158 struct dir *dir = NULL;
160 if ((cdir->d_did != DIRDID_ROOT_PARENT) && (cdir->d_child)) {
166 hn = hash_lookup(vol->v_hash, &key);
174 /* -----------------------------------------
175 * if did is not in the cache resolve it with cnid
178 * OSX call it with bogus id, ie file ID not folder ID,
179 * and we are really bad in this case.
182 dirlookup( vol, did )
183 const struct vol *vol;
189 static char path[MAXPATHLEN + 1];
192 static char buffer[12 + MAXPATHLEN + 1];
193 int buflen = 12 + MAXPATHLEN + 1;
198 ret = dirsearch(vol, did);
199 if (ret != NULL || afp_errno == AFPERR_PARAM)
202 utf8 = utf8_encoding();
203 maxpath = (utf8)?MAXPATHLEN -7:255;
205 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
206 afp_errno = AFPERR_NOOBJ;
209 ptr = path + MAXPATHLEN;
210 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
211 afp_errno = AFPERR_NOOBJ;
215 pathlen = len; /* no 0 in the last part */
217 strcpy(ptr - len, mpath);
220 ret = dirsearch(vol,id);
225 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
227 NULL == (mpath = utompath(vol, upath, cnid, utf8))
229 afp_errno = AFPERR_NOOBJ;
233 len = strlen(mpath) + 1;
235 if (pathlen > maxpath) {
236 afp_errno = AFPERR_PARAM;
239 strcpy(ptr - len, mpath);
243 /* fill the cache, another place where we know about the path type */
249 temp16 = htons(pathlen);
250 memcpy(ptr, &temp16, sizeof(temp16));
252 temp = htonl(kTextEncodingUTF8);
254 memcpy(ptr, &temp, sizeof(temp));
260 *ptr = (unsigned char)pathlen;
264 /* cname is not efficient */
265 if (cname( vol, ret, &ptr ) == NULL )
268 return dirsearch(vol, did);
271 /* child addition/removal */
272 static void dirchildadd(const struct vol *vol, struct dir *a, struct dir *b)
277 b->d_next = a->d_child;
278 b->d_prev = b->d_next->d_prev;
279 b->d_next->d_prev = b;
280 b->d_prev->d_next = b;
282 if (!hash_alloc_insert(vol->v_hash, b, b)) {
283 LOG(log_error, logtype_afpd, "dirchildadd: can't hash %s", b->d_u_name);
287 static void dirchildremove(struct dir *a,struct dir *b)
290 a->d_child = (b == b->d_next) ? NULL : b->d_next;
291 b->d_next->d_prev = b->d_prev;
292 b->d_prev->d_next = b->d_next;
293 b->d_next = b->d_prev = b;
296 /* --------------------------- */
297 /* rotate the tree to the left */
298 static void dir_leftrotate(vol, dir)
302 struct dir *right = dir->d_right;
304 /* whee. move the right's left tree into dir's right tree */
305 dir->d_right = right->d_left;
306 if (right->d_left != SENTINEL)
307 right->d_left->d_back = dir;
309 if (right != SENTINEL) {
310 right->d_back = dir->d_back;
314 if (!dir->d_back) /* no parent. move the right tree to the top. */
316 else if (dir == dir->d_back->d_left) /* we were on the left */
317 dir->d_back->d_left = right;
319 dir->d_back->d_right = right; /* we were on the right */
321 /* re-insert dir on the left tree */
328 /* rotate the tree to the right */
329 static void dir_rightrotate(vol, dir)
333 struct dir *left = dir->d_left;
335 /* whee. move the left's right tree into dir's left tree */
336 dir->d_left = left->d_right;
337 if (left->d_right != SENTINEL)
338 left->d_right->d_back = dir;
340 if (left != SENTINEL) {
341 left->d_back = dir->d_back;
345 if (!dir->d_back) /* no parent. move the left tree to the top. */
347 else if (dir == dir->d_back->d_right) /* we were on the right */
348 dir->d_back->d_right = left;
350 dir->d_back->d_left = left; /* we were on the left */
352 /* re-insert dir on the right tree */
358 /* recolor after a removal */
359 static struct dir *dir_rmrecolor(vol, dir)
365 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
366 /* are we on the left tree? */
367 if (dir == dir->d_back->d_left) {
368 leaf = dir->d_back->d_right; /* get right side */
369 if (leaf->d_color == DIRTREE_COLOR_RED) {
370 /* we're red. we need to change to black. */
371 leaf->d_color = DIRTREE_COLOR_BLACK;
372 dir->d_back->d_color = DIRTREE_COLOR_RED;
373 dir_leftrotate(vol, dir->d_back);
374 leaf = dir->d_back->d_right;
377 /* right leaf has black end nodes */
378 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
379 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
380 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
381 dir = dir->d_back; /* ascend */
383 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
384 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
385 leaf->d_color = DIRTREE_COLOR_RED;
386 dir_rightrotate(vol, leaf);
387 leaf = dir->d_back->d_right;
389 leaf->d_color = dir->d_back->d_color;
390 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
391 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
392 dir_leftrotate(vol, dir->d_back);
395 } else { /* right tree */
396 leaf = dir->d_back->d_left; /* left tree */
397 if (leaf->d_color == DIRTREE_COLOR_RED) {
398 leaf->d_color = DIRTREE_COLOR_BLACK;
399 dir->d_back->d_color = DIRTREE_COLOR_RED;
400 dir_rightrotate(vol, dir->d_back);
401 leaf = dir->d_back->d_left;
404 /* left leaf has black end nodes */
405 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
406 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
407 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
408 dir = dir->d_back; /* ascend */
410 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
411 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
412 leaf->d_color = DIRTREE_COLOR_RED;
413 dir_leftrotate(vol, leaf);
414 leaf = dir->d_back->d_left;
416 leaf->d_color = dir->d_back->d_color;
417 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
418 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
419 dir_rightrotate(vol, dir->d_back);
424 dir->d_color = DIRTREE_COLOR_BLACK;
430 /* --------------------- */
431 static void dir_hash_del(const struct vol *vol, struct dir *dir)
435 hn = hash_lookup(vol->v_hash, dir);
437 LOG(log_error, logtype_afpd, "dir_hash_del: %s not hashed", dir->d_u_name);
440 hash_delete(vol->v_hash, hn);
444 /* remove the node from the tree. this is just like insertion, but
445 * different. actually, it has to worry about a bunch of things that
446 * insertion doesn't care about. */
447 static void dir_remove( const struct vol *vol _U_, struct dir *dir)
450 struct ofork *of, *last;
451 struct dir *node, *leaf;
452 #endif /* REMOVE_NODES */
454 if (!dir || (dir == SENTINEL))
457 /* i'm not sure if it really helps to delete stuff. */
458 dir_hash_del(vol, dir);
461 dir->d_m_name = NULL;
462 dir->d_u_name = NULL;
463 #else /* ! REMOVE_NODES */
465 /* go searching for a node with at most one child */
466 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
470 while (node->d_left != SENTINEL)
475 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
478 leaf->d_back = node->d_back;
481 } else if (node == node->d_back->d_left) { /* left tree */
482 node->d_back->d_left = leaf;
484 node->d_back->d_right = leaf;
487 /* we want to free node, but we also want to free the data in dir.
488 * currently, that's d_name and the directory traversal bits.
489 * we just copy the necessary bits and then fix up all the
490 * various pointers to the directory. needless to say, there are
491 * a bunch of places that store the directory struct. */
493 struct dir save, *tmp;
495 memcpy(&save, dir, sizeof(save));
496 memcpy(dir, node, sizeof(struct dir));
498 /* restore the red-black bits */
499 dir->d_left = save.d_left;
500 dir->d_right = save.d_right;
501 dir->d_back = save.d_back;
502 dir->d_color = save.d_color;
504 if (node == vol->v_dir) {/* we may need to fix up this pointer */
506 rootpar.d_child = vol->v_dir;
508 /* if we aren't the root directory, we have parents and
509 * siblings to worry about */
510 if (dir->d_parent->d_child == node)
511 dir->d_parent->d_child = dir;
512 dir->d_next->d_prev = dir;
513 dir->d_prev->d_next = dir;
516 /* fix up children. */
520 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
523 if (node == curdir) /* another pointer to fixup */
526 /* we also need to fix up oforks. bleah */
527 if ((of = dir->d_ofork)) {
528 last = of->of_d_prev;
531 of = (last == of) ? NULL : of->of_d_next;
535 /* set the node's d_name */
536 node->d_m_name = save.d_m_name;
537 node->d_u_name = save.d_u_name;
540 if (node->d_color == DIRTREE_COLOR_BLACK)
541 dir_rmrecolor(vol, leaf);
543 if (node->d_u_name != node->d_m_name) {
544 free(node->d_u_name);
546 free(node->d_m_name);
548 #endif /* ! REMOVE_NODES */
551 /* ---------------------------------------
552 * remove the node and its childs from the tree
554 * FIXME what about opened forks with refs to it?
555 * it's an afp specs violation because you can't delete
556 * an opened forks. Now afpd doesn't care about forks opened by other
557 * process. It's fixable within afpd if fnctl_lock, doable with smb and
558 * next to impossible for nfs and local filesystem access.
560 static void dir_invalidate( vol, dir )
561 const struct vol *vol;
565 /* v_root can't be deleted */
566 if (movecwd(vol, vol->v_root) < 0) {
567 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
571 dirchildremove(dir->d_parent, dir);
572 dir_remove( vol, dir );
575 /* ------------------------------------ */
576 static struct dir *dir_insert(vol, dir)
577 const struct vol *vol;
583 while (pdir->d_did != dir->d_did ) {
584 if ( pdir->d_did > dir->d_did ) {
585 if ( pdir->d_left == SENTINEL ) {
592 if ( pdir->d_right == SENTINEL ) {
597 pdir = pdir->d_right;
604 * attempt to extend the current dir. tree to include path
605 * as a side-effect, movecwd to that point and return the new dir
608 extenddir( vol, dir, path )
615 if ( path->u_name == NULL) {
616 afp_errno = AFPERR_PARAM;
619 if (of_stat( path ) != 0 ) {
623 if (!S_ISDIR(path->st.st_mode)) {
627 /* mac name is always with the right encoding (from cname()) */
628 if (( dir = adddir( vol, dir, path)) == NULL ) {
633 if ( movecwd( vol, dir ) < 0 ) {
640 /* -------------------
641 system rmdir with afp error code.
642 ENOENT is not an error.
644 int netatalk_rmdir(const char *name)
646 if (rmdir(name) < 0) {
651 return AFPERR_DIRNEMPT;
654 return AFPERR_ACCESS;
664 /* -------------------------
665 appledouble mkdir afp error code.
667 static int netatalk_mkdir(const char *name)
669 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
672 return( AFPERR_NOOBJ );
674 return( AFPERR_VLOCK );
677 return( AFPERR_ACCESS );
679 return( AFPERR_EXIST );
682 return( AFPERR_DFULL );
684 return( AFPERR_PARAM );
690 /* -------------------
691 system unlink with afp error code.
692 ENOENT is not an error.
694 int netatalk_unlink(const char *name)
696 if (unlink(name) < 0) {
704 return AFPERR_ACCESS;
712 /* ------------------- */
713 static int deletedir(char *dir)
715 char path[MAXPATHLEN + 1];
723 if ((len = strlen(dir)) +2 > sizeof(path))
727 if ((dp = opendir(dir)) == NULL)
733 remain = sizeof(path) -len -1;
734 while ((de = readdir(dp)) && err == AFP_OK) {
735 /* skip this and previous directory */
736 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
739 if (strlen(de->d_name) > remain) {
743 strcpy(path + len, de->d_name);
744 if (stat(path, &st)) {
747 if (S_ISDIR(st.st_mode)) {
748 err = deletedir(path);
750 err = netatalk_unlink(path);
755 /* okay. the directory is empty. delete it. note: we already got rid
758 err = netatalk_rmdir(dir);
763 /* do a recursive copy. */
764 static int copydir(const struct vol *vol, char *src, char *dst)
766 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
775 /* doesn't exist or the path is too long. */
776 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
777 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
778 ((dp = opendir(src)) == NULL))
781 /* try to create the destination directory */
782 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
787 /* set things up to copy */
791 srem = sizeof(spath) - slen -1;
796 drem = sizeof(dpath) - dlen -1;
799 while ((de = readdir(dp))) {
800 /* skip this and previous directory */
801 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
804 if (strlen(de->d_name) > srem) {
808 strcpy(spath + slen, de->d_name);
810 if (stat(spath, &st) == 0) {
811 if (strlen(de->d_name) > drem) {
815 strcpy(dpath + dlen, de->d_name);
817 if (S_ISDIR(st.st_mode)) {
818 if (AFP_OK != (err = copydir(vol, spath, dpath)))
820 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
824 /* keep the same time stamp. */
825 ut.actime = ut.modtime = st.st_mtime;
831 /* keep the same time stamp. */
832 if (stat(src, &st) == 0) {
833 ut.actime = ut.modtime = st.st_mtime;
843 /* --- public functions follow --- */
845 /* NOTE: we start off with at least one node (the root directory). */
846 static struct dir *dirinsert( vol, dir )
852 if ((node = dir_insert(vol, dir)))
855 /* recolor the tree. the current node is red. */
856 dir->d_color = DIRTREE_COLOR_RED;
858 /* parent of this node has to be black. if the parent node
859 * is red, then we have a grandparent. */
860 while ((dir != vol->v_root) &&
861 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
862 /* are we on the left tree? */
863 if (dir->d_back == dir->d_back->d_back->d_left) {
864 node = dir->d_back->d_back->d_right; /* get the right node */
865 if (node->d_color == DIRTREE_COLOR_RED) {
866 /* we're red. we need to change to black. */
867 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
868 node->d_color = DIRTREE_COLOR_BLACK;
869 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
870 dir = dir->d_back->d_back; /* finished. go up. */
872 if (dir == dir->d_back->d_right) {
874 dir_leftrotate(vol, dir);
876 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
877 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
878 dir_rightrotate(vol, dir->d_back->d_back);
881 node = dir->d_back->d_back->d_left;
882 if (node->d_color == DIRTREE_COLOR_RED) {
883 /* we're red. we need to change to black. */
884 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
885 node->d_color = DIRTREE_COLOR_BLACK;
886 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
887 dir = dir->d_back->d_back; /* finished. ascend */
889 if (dir == dir->d_back->d_left) {
891 dir_rightrotate(vol, dir);
893 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
894 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
895 dir_leftrotate(vol, dir->d_back->d_back);
900 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
904 /* ---------------------------- */
906 adddir( vol, dir, path)
911 struct dir *cdir, *edir;
919 upath = path->u_name;
921 upathlen = strlen(upath);
923 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
927 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
931 if ((cdir = dirnew(name, upath)) == NULL) {
932 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
938 if ((edir = dirinsert( vol, cdir ))) {
939 /* it's not possible with LASTDID
941 - someone else have moved the directory.
942 - it's a symlink inside the share.
943 - it's an ID reused, the old directory was deleted but not
944 the cnid record and the server've reused the inode for
946 for HASH (we should get ride of HASH)
947 - someone else have moved the directory.
948 - it's an ID reused as above
949 - it's a hash duplicate and we are in big trouble
951 deleted = (edir->d_m_name == NULL);
953 dir_hash_del(vol, edir);
955 edir->d_m_name = cdir->d_m_name;
956 edir->d_u_name = cdir->d_u_name;
959 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
960 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
961 hash_alloc_insert(vol->v_hash, cdir, cdir);
964 /* the old was not in the same folder */
966 dirchildremove(cdir->d_parent, cdir);
969 /* parent/child directories */
970 cdir->d_parent = dir;
971 dirchildadd(vol, dir, cdir);
975 /* --- public functions follow --- */
976 /* free everything down. we don't bother to recolor as this is only
977 * called to free the entire tree */
978 void dirfreename(struct dir *dir)
980 if (dir->d_u_name != dir->d_m_name) {
989 if (!dir || (dir == SENTINEL))
992 if ( dir->d_left != SENTINEL ) {
993 dirfree( dir->d_left );
995 if ( dir->d_right != SENTINEL ) {
996 dirfree( dir->d_right );
999 if (dir != SENTINEL) {
1005 /* --------------------------------------------
1006 * most of the time mac name and unix name are the same
1008 struct dir *dirnew(const char *m_name, const char *u_name)
1012 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1016 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1021 if (m_name == u_name || !strcmp(m_name, u_name)) {
1022 dir->d_u_name = dir->d_m_name;
1024 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1025 free(dir->d_m_name);
1030 dir->d_left = dir->d_right = SENTINEL;
1031 dir->d_next = dir->d_prev = dir;
1035 /* ------------------ */
1036 static hash_val_t hash_fun_dir(const void *key)
1038 const struct dir *k = key;
1040 static unsigned long randbox[] = {
1041 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1042 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1043 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1044 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1047 const unsigned char *str = (unsigned char *)k->d_u_name;
1051 acc ^= randbox[(*str + acc) & 0xf];
1052 acc = (acc << 1) | (acc >> 31);
1054 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1055 acc = (acc << 2) | (acc >> 30);
1061 /* ---------------- */
1062 static int hash_comp_dir(const void *key1, const void *key2)
1064 const struct dir *k1 = key1;
1065 const struct dir *k2 = key2;
1067 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1070 /* ---------------- */
1074 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1077 /* ------------------ */
1078 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1081 movecwd failed some of dir path are not there anymore.
1082 FIXME Is it true with other errors?
1083 so we remove dir from the cache
1085 if (dir->d_did == DIRDID_ROOT_PARENT)
1087 if (afp_errno == AFPERR_ACCESS) {
1088 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1091 /* FIXME should we set these?, don't need to call stat() after:
1093 ret->st_errno = EACCES;
1095 ret->m_name = dir->d_m_name;
1096 ret->u_name = dir->d_u_name;
1099 } else if (afp_errno == AFPERR_NOOBJ) {
1100 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1103 strcpy(ret->m_name, dir->d_m_name);
1104 if (dir->d_m_name == dir->d_u_name) {
1105 ret->u_name = ret->m_name;
1108 size_t tp = strlen(ret->m_name)+1;
1110 ret->u_name = ret->m_name +tp;
1111 strcpy(ret->u_name, dir->d_u_name);
1113 /* FIXME should we set :
1115 ret->st_errno = ENOENT;
1117 dir_invalidate(vol, dir);
1120 dir_invalidate(vol, dir);
1124 /* -------------------------------------------------- */
1130 stat the file or errno
1133 curdir: filename parent directory
1139 stat the dir or errno
1143 curdir: dir parent directory
1151 curdir: dir parent directory
1158 cname( vol, dir, cpath )
1159 const struct vol *vol;
1164 static char path[ MAXPATHLEN + 1];
1165 static struct path ret;
1177 afp_errno = AFPERR_NOOBJ;
1178 memset(&ret, 0, sizeof(ret));
1179 switch (ret.m_type = *data) { /* path type */
1182 len = (unsigned char) *data++;
1185 if (afp_version >= 30) {
1191 if (afp_version >= 30) {
1193 memcpy(&hint, data, sizeof(hint));
1195 data += sizeof(hint);
1197 memcpy(&len16, data, sizeof(len16));
1204 /* else it's an error */
1206 afp_errno = AFPERR_PARAM;
1209 *cpath += len + size;
1214 if (movecwd( vol, dir ) < 0 ) {
1215 return invalidate(vol, dir, &ret );
1217 if (*path == '\0') {
1224 if (*data == sep ) {
1228 while (*data == sep && len > 0 ) {
1229 if ( dir->d_parent == NULL ) {
1232 dir = dir->d_parent;
1237 /* would this be faster with strlen + strncpy? */
1239 while ( *data != sep && len > 0 ) {
1241 if (p > &path[ MAXPATHLEN]) {
1242 afp_errno = AFPERR_PARAM;
1248 /* short cut bits by chopping off a trailing \0. this also
1249 makes the traversal happy w/ filenames at the end of the
1256 if ( p == path ) { /* end of the name parameter */
1260 if (afp_version >= 30) {
1265 static char temp[ MAXPATHLEN + 1];
1267 /* not an UTF8 name */
1268 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1269 afp_errno = AFPERR_PARAM;
1274 /* check for OS X mangled filename :( */
1276 t = demangle_osx(vol, path, dir->d_did, &fileid);
1279 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1280 * flags weren't the same
1282 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1283 /* at last got our view of mac name */
1288 if (ret.u_name == NULL) {
1289 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1290 afp_errno = AFPERR_PARAM;
1295 if (dir->d_did == DIRDID_ROOT_PARENT) {
1296 /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
1297 * d_m_name is the Mac volume name
1298 * d_u_name is the volume unix directory name
1302 if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
1307 cdir = dirsearch_byname(vol, dir, ret.u_name);
1310 if ( cdir == NULL ) {
1312 /* if dir == curdir it always succeed,
1313 even if curdir is deleted.
1314 it's not a pb because it will fail in extenddir
1316 if ( movecwd( vol, dir ) < 0 ) {
1317 /* dir is not valid anymore
1318 we delete dir from the cache and abort.
1320 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1321 afp_errno = AFPERR_NOOBJ;
1324 if (afp_errno == AFPERR_ACCESS)
1326 dir_invalidate(vol, dir);
1329 cdir = extenddir( vol, dir, &ret );
1333 cdir = extenddir( vol, dir, &ret );
1334 } /* if (!extend) */
1336 if ( cdir == NULL ) {
1350 * Move curdir to dir, with a possible chdir()
1352 int movecwd( vol, dir)
1353 const struct vol *vol;
1356 char path[MAXPATHLEN + 1];
1361 if ( dir == curdir ) {
1364 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1365 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1369 p = path + sizeof(path) - 1;
1372 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1375 /* parent directory is deleted */
1376 afp_errno = AFPERR_NOOBJ;
1380 if (p -n -1 < path) {
1381 afp_errno = AFPERR_PARAM;
1388 if ( d != curdir ) {
1389 n = strlen( vol->v_path );
1390 if (p -n -1 < path) {
1391 afp_errno = AFPERR_PARAM;
1396 memcpy( p, vol->v_path, n );
1398 if ( chdir( p ) < 0 ) {
1402 afp_errno = AFPERR_ACCESS;
1405 afp_errno = AFPERR_NOOBJ;
1415 * We can't use unix file's perm to support Apple's inherited protection modes.
1416 * If we aren't the file's owner we can't change its perms when moving it and smb
1417 * nfs,... don't even try.
1419 #define AFP_CHECK_ACCESS
1421 int check_access(char *path, int mode)
1423 #ifdef AFP_CHECK_ACCESS
1431 accessmode(p, &ma, curdir, NULL);
1432 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1434 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1440 /* --------------------- */
1441 int file_access(struct path *path, int mode)
1445 accessmode(path->u_name, &ma, curdir, &path->st);
1446 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1448 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1454 /* --------------------- */
1455 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1457 dir->offcnt = count;
1458 dir->ctime = st->st_ctime;
1459 dir->d_flags &= ~DIRF_CNID;
1462 /* ---------------------
1463 * is our cached offspring count valid?
1466 int diroffcnt(struct dir *dir, struct stat *st)
1468 return st->st_ctime == dir->ctime;
1471 /* ---------------------
1472 * is our cached also for reenumerate id?
1475 int dirreenumerate(struct dir *dir, struct stat *st)
1477 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1480 /* --------------------- */
1481 static int invisible_dots(const struct vol *vol, const char *name)
1483 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1486 /* ------------------------------
1488 (name, dir) with curdir:name == dir, from afp_enumerate
1491 int getdirparams(const struct vol *vol,
1492 u_int16_t bitmap, struct path *s_path,
1494 char *buf, int *buflen )
1498 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1499 int bit = 0, isad = 0;
1505 struct stat *st = &s_path->st;
1506 char *upath = s_path->u_name;
1508 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1509 (1 << DIRPBIT_CDATE) |
1510 (1 << DIRPBIT_MDATE) |
1511 (1 << DIRPBIT_BDATE) |
1512 (1 << DIRPBIT_FINFO)))) {
1514 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1515 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1520 if ( dir->d_did == DIRDID_ROOT) {
1521 pdid = DIRDID_ROOT_PARENT;
1522 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1525 pdid = dir->d_parent->d_did;
1529 while ( bitmap != 0 ) {
1530 while (( bitmap & 1 ) == 0 ) {
1538 ad_getattr(&ad, &ashort);
1539 } else if (invisible_dots(vol, dir->d_u_name)) {
1540 ashort = htons(ATTRBIT_INVISIBLE);
1543 ashort |= htons(ATTRBIT_SHARED);
1544 memcpy( data, &ashort, sizeof( ashort ));
1545 data += sizeof( ashort );
1549 memcpy( data, &pdid, sizeof( pdid ));
1550 data += sizeof( pdid );
1553 case DIRPBIT_CDATE :
1554 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1555 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1556 memcpy( data, &aint, sizeof( aint ));
1557 data += sizeof( aint );
1560 case DIRPBIT_MDATE :
1561 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1562 memcpy( data, &aint, sizeof( aint ));
1563 data += sizeof( aint );
1566 case DIRPBIT_BDATE :
1567 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1568 aint = AD_DATE_START;
1569 memcpy( data, &aint, sizeof( aint ));
1570 data += sizeof( aint );
1573 case DIRPBIT_FINFO :
1575 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1576 } else { /* no appledouble */
1577 memset( data, 0, 32 );
1578 /* set default view -- this also gets done in ad_open() */
1579 ashort = htons(FINDERINFO_CLOSEDVIEW);
1580 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1582 /* dot files are by default visible */
1583 if (invisible_dots(vol, dir->d_u_name)) {
1584 ashort = htons(FINDERINFO_INVISIBLE);
1585 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1591 case DIRPBIT_LNAME :
1592 if (dir->d_m_name) /* root of parent can have a null name */
1595 memset(data, 0, sizeof(u_int16_t));
1596 data += sizeof( u_int16_t );
1599 case DIRPBIT_SNAME :
1600 memset(data, 0, sizeof(u_int16_t));
1601 data += sizeof( u_int16_t );
1605 memcpy( data, &dir->d_did, sizeof( aint ));
1606 data += sizeof( aint );
1609 case DIRPBIT_OFFCNT :
1611 /* this needs to handle current directory access rights */
1612 if (diroffcnt(dir, st)) {
1613 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1615 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1616 setdiroffcnt(dir, st, ret);
1617 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1619 ashort = htons( ashort );
1620 memcpy( data, &ashort, sizeof( ashort ));
1621 data += sizeof( ashort );
1625 aint = htonl(st->st_uid);
1626 memcpy( data, &aint, sizeof( aint ));
1627 data += sizeof( aint );
1631 aint = htonl(st->st_gid);
1632 memcpy( data, &aint, sizeof( aint ));
1633 data += sizeof( aint );
1636 case DIRPBIT_ACCESS :
1637 accessmode( upath, &ma, dir , st);
1639 *data++ = ma.ma_user;
1640 *data++ = ma.ma_world;
1641 *data++ = ma.ma_group;
1642 *data++ = ma.ma_owner;
1645 /* Client has requested the ProDOS information block.
1646 Just pass back the same basic block for all
1647 directories. <shirsch@ibm.net> */
1648 case DIRPBIT_PDINFO :
1649 if (afp_version >= 30) { /* UTF8 name */
1650 utf8 = kTextEncodingUTF8;
1651 if (dir->d_m_name) /* root of parent can have a null name */
1654 memset(data, 0, sizeof(u_int16_t));
1655 data += sizeof( u_int16_t );
1657 memcpy(data, &aint, sizeof( aint ));
1658 data += sizeof( aint );
1660 else { /* ProDOS Info Block */
1663 ashort = htons( 0x0200 );
1664 memcpy( data, &ashort, sizeof( ashort ));
1665 data += sizeof( ashort );
1666 memset( data, 0, sizeof( ashort ));
1667 data += sizeof( ashort );
1671 case DIRPBIT_UNIXPR :
1672 aint = htonl(st->st_uid);
1673 memcpy( data, &aint, sizeof( aint ));
1674 data += sizeof( aint );
1675 aint = htonl(st->st_gid);
1676 memcpy( data, &aint, sizeof( aint ));
1677 data += sizeof( aint );
1680 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1681 memcpy( data, &aint, sizeof( aint ));
1682 data += sizeof( aint );
1684 accessmode( upath, &ma, dir , st);
1686 *data++ = ma.ma_user;
1687 *data++ = ma.ma_world;
1688 *data++ = ma.ma_group;
1689 *data++ = ma.ma_owner;
1694 ad_close_metadata( &ad );
1696 return( AFPERR_BITMAP );
1702 ashort = htons( data - buf );
1703 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1704 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1706 if ( utf_nameoff ) {
1707 ashort = htons( data - buf );
1708 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1709 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1712 ad_close_metadata( &ad );
1714 *buflen = data - buf;
1718 /* ----------------------------- */
1719 int path_error(struct path *path, int error)
1721 /* - a dir with access error
1722 * - no error it's a file
1725 if (path_isadir(path))
1727 if (path->st_valid && path->st_errno)
1729 return AFPERR_BADTYPE ;
1732 /* ----------------------------- */
1733 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1735 char *ibuf, *rbuf _U_;
1736 int ibuflen _U_, *rbuflen;
1741 u_int16_t vid, bitmap;
1747 memcpy( &vid, ibuf, sizeof( vid ));
1748 ibuf += sizeof( vid );
1750 if (NULL == ( vol = getvolbyvid( vid )) ) {
1751 return( AFPERR_PARAM );
1754 if (vol->v_flags & AFPVOL_RO)
1755 return AFPERR_VLOCK;
1757 memcpy( &did, ibuf, sizeof( did ));
1758 ibuf += sizeof( int );
1760 if (NULL == ( dir = dirlookup( vol, did )) ) {
1764 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1765 bitmap = ntohs( bitmap );
1766 ibuf += sizeof( bitmap );
1768 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1769 return get_afp_errno(AFPERR_NOOBJ);
1772 if ( *path->m_name != '\0' ) {
1773 rc = path_error(path, AFPERR_NOOBJ);
1774 /* maybe we are trying to set perms back */
1775 if (rc != AFPERR_ACCESS)
1780 * If ibuf is odd, make it even.
1782 if ((u_long)ibuf & 1 ) {
1786 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1787 setvoltime(obj, vol );
1793 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1795 * assume path == '\0' eg. it's a directory in canonical form
1798 struct path Cur_Path = {
1801 ".", /* unix name */
1803 NULL,/* struct dir */
1804 0, /* stat is not set */
1807 /* ------------------ */
1808 static int set_dir_errors(struct path *path, const char *where, int err)
1813 return AFPERR_ACCESS;
1815 return AFPERR_VLOCK;
1817 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1818 return AFPERR_PARAM;
1821 /* ------------------ */
1822 int setdirparams(const struct vol *vol,
1823 struct path *path, u_int16_t d_bitmap, char *buf )
1835 u_int16_t ashort, bshort;
1837 int change_mdate = 0;
1838 int change_parent_mdate = 0;
1840 u_int16_t bitmap = d_bitmap;
1841 u_char finder_buf[32];
1844 u_int16_t upriv_bit = 0;
1847 upath = path->u_name;
1849 while ( bitmap != 0 ) {
1850 while (( bitmap & 1 ) == 0 ) {
1858 memcpy( &ashort, buf, sizeof( ashort ));
1859 buf += sizeof( ashort );
1861 case DIRPBIT_CDATE :
1863 memcpy(&cdate, buf, sizeof(cdate));
1864 buf += sizeof( cdate );
1866 case DIRPBIT_MDATE :
1867 memcpy(&newdate, buf, sizeof(newdate));
1868 buf += sizeof( newdate );
1870 case DIRPBIT_BDATE :
1872 memcpy(&bdate, buf, sizeof(bdate));
1873 buf += sizeof( bdate );
1875 case DIRPBIT_FINFO :
1877 memcpy( finder_buf, buf, 32 );
1880 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1881 change_parent_mdate = 1;
1882 memcpy( &owner, buf, sizeof(owner));
1883 buf += sizeof( owner );
1886 change_parent_mdate = 1;
1887 memcpy( &group, buf, sizeof( group ));
1888 buf += sizeof( group );
1890 case DIRPBIT_ACCESS :
1892 change_parent_mdate = 1;
1893 ma.ma_user = *buf++;
1894 ma.ma_world = *buf++;
1895 ma.ma_group = *buf++;
1896 ma.ma_owner = *buf++;
1897 mpriv = mtoumode( &ma ) | vol->v_dperm;
1898 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1899 err = set_dir_errors(path, "setdirmode", errno);
1903 /* Ignore what the client thinks we should do to the
1904 ProDOS information block. Skip over the data and
1905 report nothing amiss. <shirsch@ibm.net> */
1906 case DIRPBIT_PDINFO :
1907 if (afp_version < 30) {
1911 err = AFPERR_BITMAP;
1915 case DIRPBIT_UNIXPR :
1916 if (vol_unix_priv(vol)) {
1917 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1918 buf += sizeof( owner );
1919 memcpy( &group, buf, sizeof( group ));
1920 buf += sizeof( group );
1923 change_parent_mdate = 1;
1924 memcpy( &upriv, buf, sizeof( upriv ));
1925 buf += sizeof( upriv );
1926 upriv = ntohl (upriv) | vol->v_dperm;
1927 if (dir_rx_set(upriv)) {
1928 /* maybe we are trying to set perms back */
1929 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1931 err = set_dir_errors(path, "setdirunixmode", errno);
1942 err = AFPERR_BITMAP;
1950 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1952 if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1954 * Check to see what we're trying to set. If it's anything
1955 * but ACCESS, UID, or GID, give an error. If it's any of those
1956 * three, we don't need the ad to be open, so just continue.
1958 * note: we also don't need to worry about mdate. also, be quiet
1959 * if we're using the noadouble option.
1961 if (!vol_noadouble(vol) && (d_bitmap &
1962 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1963 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1964 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1965 return AFPERR_ACCESS;
1971 * Check to see if a create was necessary. If it was, we'll want
1972 * to set our name, etc.
1974 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1975 ad_setname(&ad, curdir->d_m_name);
1981 while ( bitmap != 0 ) {
1982 while (( bitmap & 1 ) == 0 ) {
1990 ad_getattr(&ad, &bshort);
1991 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1992 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1993 change_parent_mdate = 1;
1994 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1995 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1999 ad_setattr(&ad, bshort);
2002 case DIRPBIT_CDATE :
2004 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2007 case DIRPBIT_MDATE :
2009 case DIRPBIT_BDATE :
2011 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2014 case DIRPBIT_FINFO :
2016 if ( dir->d_did == DIRDID_ROOT ) {
2018 * Alright, we admit it, this is *really* sick!
2019 * The 4 bytes that we don't copy, when we're dealing
2020 * with the root of a volume, are the directory's
2021 * location information. This eliminates that annoying
2022 * behavior one sees when mounting above another mount
2025 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2026 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2028 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2032 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2033 if ( (dir->d_did == DIRDID_ROOT) &&
2034 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2035 err = set_dir_errors(path, "setdeskowner", errno);
2036 if (isad && err == AFPERR_PARAM) {
2037 err = AFP_OK; /* ???*/
2040 goto setdirparam_done;
2043 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2044 err = set_dir_errors(path, "setdirowner", errno);
2045 goto setdirparam_done;
2049 if (dir->d_did == DIRDID_ROOT)
2050 setdeskowner( -1, ntohl(group) );
2051 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2052 err = set_dir_errors(path, "setdirowner", errno);
2053 goto setdirparam_done;
2056 case DIRPBIT_ACCESS :
2057 if (dir->d_did == DIRDID_ROOT) {
2059 if (!dir_rx_set(mpriv)) {
2060 /* we can't remove read and search for owner on volume root */
2061 err = AFPERR_ACCESS;
2062 goto setdirparam_done;
2066 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2067 err = set_dir_errors(path, "setdirmode", errno);
2068 goto setdirparam_done;
2071 case DIRPBIT_PDINFO :
2072 if (afp_version >= 30) {
2073 err = AFPERR_BITMAP;
2074 goto setdirparam_done;
2077 case DIRPBIT_UNIXPR :
2078 if (vol_unix_priv(vol)) {
2079 if (dir->d_did == DIRDID_ROOT) {
2080 if (!dir_rx_set(upriv)) {
2081 /* we can't remove read and search for owner on volume root */
2082 err = AFPERR_ACCESS;
2083 goto setdirparam_done;
2085 setdeskowner( -1, ntohl(group) );
2086 setdeskmode( upriv );
2088 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2089 err = set_dir_errors(path, "setdirowner", errno);
2090 goto setdirparam_done;
2093 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2094 err = set_dir_errors(path, "setdirunixmode", errno);
2095 goto setdirparam_done;
2099 err = AFPERR_BITMAP;
2100 goto setdirparam_done;
2104 err = AFPERR_BITMAP;
2105 goto setdirparam_done;
2114 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2115 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2119 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2120 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2125 if (path->st_valid && !path->st_errno) {
2126 struct stat *st = &path->st;
2128 if (dir && dir->d_parent) {
2129 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2133 ad_close_metadata( &ad);
2136 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2137 && gettimeofday(&tv, NULL) == 0) {
2138 if (!movecwd(vol, dir->d_parent)) {
2139 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2140 /* be careful with bitmap because now dir is null */
2141 bitmap = 1<<DIRPBIT_MDATE;
2142 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2143 /* should we reset curdir ?*/
2151 int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2153 char *ibuf, *rbuf _U_;
2154 int ibuflen _U_, *rbuflen;
2166 memcpy( &vid, ibuf, sizeof( vid ));
2167 ibuf += sizeof( vid );
2168 if (NULL == (vol = getvolbyvid( vid )) ) {
2169 return( AFPERR_PARAM );
2172 memcpy( &did, ibuf, sizeof( did ));
2173 ibuf += sizeof( did );
2174 if (NULL == ( dir = dirlookup( vol, did )) ) {
2175 return afp_errno; /* was AFPERR_NOOBJ */
2178 if (NULL == ( dp = opendir( "." )) ) {
2181 return( AFPERR_NOOBJ );
2183 return( AFPERR_ACCESS );
2185 return( AFPERR_PARAM );
2190 if ( fsync ( dfd ) < 0 ) {
2191 LOG(log_error, logtype_afpd, "syncdir(%s): ddir(%d) %s", dir->d_u_name, dfd, strerror(errno) );
2198 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2201 int ibuflen _U_, *rbuflen;
2207 struct path *s_path;
2215 memcpy( &vid, ibuf, sizeof( vid ));
2216 ibuf += sizeof( vid );
2217 if (NULL == ( vol = getvolbyvid( vid )) ) {
2218 return( AFPERR_PARAM );
2221 if (vol->v_flags & AFPVOL_RO)
2222 return AFPERR_VLOCK;
2224 memcpy( &did, ibuf, sizeof( did ));
2225 ibuf += sizeof( did );
2226 if (NULL == ( dir = dirlookup( vol, did )) ) {
2227 return afp_errno; /* was AFPERR_NOOBJ */
2229 /* for concurrent access we need to be sure we are not in the
2230 * folder we want to create...
2234 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2235 return get_afp_errno(AFPERR_PARAM);
2237 /* cname was able to move curdir to it! */
2238 if (*s_path->m_name == '\0')
2239 return AFPERR_EXIST;
2241 upath = s_path->u_name;
2242 if (0 != (err = check_name(vol, upath))) {
2246 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2250 if (of_stat(s_path) < 0) {
2254 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2258 if ( movecwd( vol, dir ) < 0 ) {
2259 return( AFPERR_PARAM );
2262 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2263 if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2264 if (vol_noadouble(vol))
2265 goto createdir_done;
2266 return( AFPERR_ACCESS );
2268 ad_setname(&ad, s_path->m_name);
2269 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2272 ad_close_metadata( &ad);
2275 #ifdef HAVE_NFSv4_ACLS
2276 /* FIXME: are we really inside the created dir? */
2277 addir_inherit_acl(vol);
2280 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2281 *rbuflen = sizeof( u_int32_t );
2282 setvoltime(obj, vol );
2287 * dst new unix filename (not a pathname)
2288 * newname new mac name
2292 int renamedir(vol, src, dst, dir, newparent, newname)
2293 const struct vol *vol;
2294 char *src, *dst, *newname;
2295 struct dir *dir, *newparent;
2302 /* existence check moved to afp_moveandrename */
2303 if ( unix_rename( src, dst ) < 0 ) {
2306 return( AFPERR_NOOBJ );
2308 return( AFPERR_ACCESS );
2310 return AFPERR_VLOCK;
2312 /* tried to move directory into a subdirectory of itself */
2313 return AFPERR_CANTMOVE;
2315 /* this needs to copy and delete. bleah. that means we have
2316 * to deal with entire directory hierarchies. */
2317 if ((err = copydir(vol, src, dst)) < 0) {
2321 if ((err = deletedir(src)) < 0)
2325 return( AFPERR_PARAM );
2329 vol->vfs->rf_renamedir(vol, src, dst);
2331 len = strlen( newname );
2332 /* rename() succeeded so we need to update our tree even if we can't open
2336 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2338 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2339 ad_setname(&ad, newname);
2341 ad_close_metadata( &ad);
2344 dir_hash_del(vol, dir);
2345 if (dir->d_m_name == dir->d_u_name)
2346 dir->d_u_name = NULL;
2348 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2349 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2350 /* FIXME : fatal ? */
2353 dir->d_m_name = buf;
2354 strcpy( dir->d_m_name, newname );
2356 if (newname == dst) {
2357 free(dir->d_u_name);
2358 dir->d_u_name = dir->d_m_name;
2361 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2362 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2365 dir->d_u_name = buf;
2366 strcpy( dir->d_u_name, dst );
2369 if (( parent = dir->d_parent ) == NULL ) {
2372 if ( parent == newparent ) {
2373 hash_alloc_insert(vol->v_hash, dir, dir);
2377 /* detach from old parent and add to new one. */
2378 dirchildremove(parent, dir);
2379 dir->d_parent = newparent;
2380 dirchildadd(vol, newparent, dir);
2384 /* delete an empty directory */
2385 int deletecurdir( vol)
2386 const struct vol *vol;
2396 if ( curdir->d_parent == NULL ) {
2397 return( AFPERR_ACCESS );
2402 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2403 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2405 ad_getattr(&ad, &ashort);
2406 ad_close( &ad, ADFLAGS_HF );
2407 if ((ashort & htons(ATTRBIT_NODELETE))) {
2408 return AFPERR_OLOCK;
2411 err = vol->vfs->rf_deletecurdir(vol);
2416 /* now get rid of dangling symlinks */
2417 if ((dp = opendir("."))) {
2418 while ((de = readdir(dp))) {
2419 /* skip this and previous directory */
2420 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2423 /* bail if it's not a symlink */
2424 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2426 return AFPERR_DIRNEMPT;
2429 if ((err = netatalk_unlink(de->d_name))) {
2436 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2441 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2442 dirchildremove(curdir, fdir);
2443 cnid_delete(vol->v_cdb, fdir->d_did);
2444 dir_remove( vol, fdir );
2449 /* inode is used as key for cnid.
2450 * Close the descriptor only after cnid_delete
2458 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2461 int ibuflen _U_, *rbuflen;
2471 LOG(log_debug, logtype_afpd, "afp_mapid: BEGIN");
2474 sfunc = (unsigned char) *ibuf++;
2478 if (sfunc >= 3 && sfunc <= 6) {
2479 if (afp_version < 30) {
2480 return( AFPERR_PARAM );
2487 case 3 :/* unicode */
2488 memcpy( &id, ibuf, sizeof( id ));
2491 if (( pw = getpwuid( id )) == NULL ) {
2492 return( AFPERR_NOITEM );
2494 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2495 pw->pw_name, strlen(pw->pw_name), &name);
2502 case 4 : /* unicode */
2503 memcpy( &id, ibuf, sizeof( id ));
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);
2516 #ifdef HAVE_NFSv4_ACLS
2517 case 5 : /* username -> UUID */
2518 case 6 : /* groupname -> UUID */
2519 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2520 return AFPERR_PARAM;
2521 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2522 len = getnamefromuuid( ibuf, &name, &type);
2523 if (len != 0) /* its a error code, not len */
2524 return AFPERR_NOITEM;
2525 if (type == UUID_USER) {
2526 if (( pw = getpwnam( name )) == NULL )
2527 return( AFPERR_NOITEM );
2528 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2529 id = htonl(UUID_USER);
2530 memcpy( rbuf, &id, sizeof( id ));
2531 id = htonl( pw->pw_uid);
2532 rbuf += sizeof( id );
2533 memcpy( rbuf, &id, sizeof( id ));
2534 rbuf += sizeof( id );
2535 *rbuflen = 2 * sizeof( id );
2536 } else { /* type == UUID_GROUP */
2537 if (( gr = getgrnam( name )) == NULL )
2538 return( AFPERR_NOITEM );
2539 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2540 id = htonl(UUID_GROUP);
2541 memcpy( rbuf, &id, sizeof( id ));
2542 rbuf += sizeof( id );
2543 id = htonl( gr->gr_gid);
2544 memcpy( rbuf, &id, sizeof( id ));
2545 rbuf += sizeof( id );
2546 *rbuflen = 2 * sizeof( id );
2551 return( AFPERR_PARAM );
2554 len = strlen( name );
2557 u_int16_t tp = htons(len);
2558 memcpy(rbuf, &tp, sizeof(tp));
2567 memcpy( rbuf, name, len );
2575 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2578 int ibuflen _U_, *rbuflen;
2587 LOG(log_debug, logtype_afpd, "afp_mapname: BEGIN");
2590 sfunc = (unsigned char) *ibuf++;
2592 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2595 case 2 : /* unicode */
2596 if (afp_version < 30) {
2597 return( AFPERR_PARAM );
2599 memcpy(&ulen, ibuf, sizeof(ulen));
2602 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2606 len = (unsigned char) *ibuf++;
2608 #ifdef HAVE_NFSv4_ACLS
2609 case 5 : /* username -> UUID */
2610 case 6 : /* groupname -> UUID */
2611 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2612 return AFPERR_PARAM;
2613 memcpy(&ulen, ibuf, sizeof(ulen));
2619 return( AFPERR_PARAM );
2625 return AFPERR_PARAM;
2628 case 1 : /* unicode */
2630 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2631 return( AFPERR_NOITEM );
2635 memcpy( rbuf, &id, sizeof( id ));
2636 *rbuflen = sizeof( id );
2639 case 2 : /* unicode */
2641 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2642 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2643 return( AFPERR_NOITEM );
2646 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2648 memcpy( rbuf, &id, sizeof( id ));
2649 *rbuflen = sizeof( id );
2651 #ifdef HAVE_NFSv4_ACLS
2652 case 5 : /* username -> UUID */
2653 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2654 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2655 return AFPERR_NOITEM;
2656 *rbuflen = UUID_BINSIZE;
2658 case 6 : /* groupname -> UUID */
2659 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2660 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2661 return AFPERR_NOITEM;
2662 *rbuflen = UUID_BINSIZE;
2670 /* ------------------------------------
2671 variable DID support
2673 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2675 char *ibuf _U_, *rbuf _U_;
2676 int ibuflen _U_, *rbuflen;
2687 /* do nothing as dids are static for the life of the process. */
2691 memcpy(&vid, ibuf, sizeof( vid ));
2692 ibuf += sizeof( vid );
2693 if (( vol = getvolbyvid( vid )) == NULL ) {
2694 return( AFPERR_PARAM );
2697 memcpy( &did, ibuf, sizeof( did ));
2698 ibuf += sizeof( did );
2699 if (( dir = dirlookup( vol, did )) == NULL ) {
2700 return( AFPERR_PARAM );
2703 /* dir_remove -- deletedid */
2709 /* did creation gets done automatically
2710 * there's a pb again with case but move it to cname
2712 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2715 int ibuflen _U_, *rbuflen;
2718 struct dir *parentdir;
2726 memcpy(&vid, ibuf, sizeof(vid));
2727 ibuf += sizeof( vid );
2729 if (NULL == ( vol = getvolbyvid( vid )) ) {
2730 return( AFPERR_PARAM );
2733 memcpy(&did, ibuf, sizeof(did));
2734 ibuf += sizeof(did);
2736 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2740 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2741 return get_afp_errno(AFPERR_PARAM);
2744 if ( *path->m_name != '\0' ) {
2745 return path_error(path, AFPERR_NOOBJ);
2748 if ( !path->st_valid && of_stat(path ) < 0 ) {
2749 return( AFPERR_NOOBJ );
2751 if ( path->st_errno ) {
2752 return( AFPERR_NOOBJ );
2755 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2756 *rbuflen = sizeof(curdir->d_did);