2 * $Id: directory.c,v 1.87 2008-09-01 15:18:36 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
38 #include <sys/param.h>
41 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/logger.h>
48 #include "directory.h"
61 #define SENTINEL (&sentinel)
62 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
63 NULL, NULL, NULL, NULL, NULL, 0, 0,
65 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
69 /* (from IM: Toolbox Essentials)
70 * dirFinderInfo (DInfo) fields:
72 * frRect 8 folder's window rectangle
74 * frLocation 4 folder's location in window
75 * frView 2 folder's view (default == closedView (256))
77 * extended dirFinderInfo (DXInfo) fields:
78 * frScroll 4 scroll position
79 * frOpenChain: 4 directory ID chain of open folders
80 * frScript: 1 script flag and code
81 * frXFlags: 1 reserved
82 * frComment: 2 comment ID
83 * frPutAway: 4 home directory ID
87 * redid did assignment for directories. now we use red-black trees.
92 const struct vol *vol;
100 afp_errno = AFPERR_PARAM;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
110 /* XXX would be nice to check against curdir but we need to keep its volume */
111 if (vol->curdir && curdir->d_did == did) {
120 afp_errno = AFPERR_NOOBJ;
121 while ( dir != SENTINEL ) {
122 if (dir->d_did == did)
123 return dir->d_m_name ? dir : NULL;
124 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
129 /* ------------------- */
131 int path_isadir(struct path *o_path)
133 return o_path->d_dir != NULL;
135 return o_path->m_name == '\0' || /* we are in a it */
136 !o_path->st_valid || /* in cache but we can't chdir in it */
137 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
142 int get_afp_errno(const int param)
144 if (afp_errno != AFPERR_DID1)
149 /* ------------------- */
151 dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
153 struct dir *dir = NULL;
155 if ((cdir->d_did != DIRDID_ROOT_PARENT) && (cdir->d_child)) {
161 hn = hash_lookup(vol->v_hash, &key);
169 /* -----------------------------------------
170 * if did is not in the cache resolve it with cnid
173 * OSX call it with bogus id, ie file ID not folder ID,
174 * and we are really bad in this case.
177 dirlookup( vol, did )
178 const struct vol *vol;
184 static char path[MAXPATHLEN + 1];
187 static char buffer[12 + MAXPATHLEN + 1];
188 int buflen = 12 + MAXPATHLEN + 1;
193 ret = dirsearch(vol, did);
194 if (ret != NULL || afp_errno == AFPERR_PARAM)
197 utf8 = utf8_encoding();
198 maxpath = (utf8)?MAXPATHLEN -7:255;
200 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
201 afp_errno = AFPERR_NOOBJ;
204 ptr = path + MAXPATHLEN;
205 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
206 afp_errno = AFPERR_NOOBJ;
210 pathlen = len; /* no 0 in the last part */
212 strcpy(ptr - len, mpath);
215 ret = dirsearch(vol,id);
220 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
222 NULL == (mpath = utompath(vol, upath, cnid, utf8))
224 afp_errno = AFPERR_NOOBJ;
228 len = strlen(mpath) + 1;
230 if (pathlen > maxpath) {
231 afp_errno = AFPERR_PARAM;
234 strcpy(ptr - len, mpath);
238 /* fill the cache, another place where we know about the path type */
244 temp16 = htons(pathlen);
245 memcpy(ptr, &temp16, sizeof(temp16));
247 temp = htonl(kTextEncodingUTF8);
249 memcpy(ptr, &temp, sizeof(temp));
255 *ptr = (unsigned char)pathlen;
259 /* cname is not efficient */
260 if (cname( vol, ret, &ptr ) == NULL )
263 return dirsearch(vol, did);
266 /* child addition/removal */
267 static void dirchildadd(const struct vol *vol, struct dir *a, struct dir *b)
272 b->d_next = a->d_child;
273 b->d_prev = b->d_next->d_prev;
274 b->d_next->d_prev = b;
275 b->d_prev->d_next = b;
277 if (!hash_alloc_insert(vol->v_hash, b, b)) {
278 LOG(log_error, logtype_afpd, "dirchildadd: can't hash %s", b->d_u_name);
282 static void dirchildremove(struct dir *a,struct dir *b)
285 a->d_child = (b == b->d_next) ? NULL : b->d_next;
286 b->d_next->d_prev = b->d_prev;
287 b->d_prev->d_next = b->d_next;
288 b->d_next = b->d_prev = b;
291 /* --------------------------- */
292 /* rotate the tree to the left */
293 static void dir_leftrotate(vol, dir)
297 struct dir *right = dir->d_right;
299 /* whee. move the right's left tree into dir's right tree */
300 dir->d_right = right->d_left;
301 if (right->d_left != SENTINEL)
302 right->d_left->d_back = dir;
304 if (right != SENTINEL) {
305 right->d_back = dir->d_back;
309 if (!dir->d_back) /* no parent. move the right tree to the top. */
311 else if (dir == dir->d_back->d_left) /* we were on the left */
312 dir->d_back->d_left = right;
314 dir->d_back->d_right = right; /* we were on the right */
316 /* re-insert dir on the left tree */
323 /* rotate the tree to the right */
324 static void dir_rightrotate(vol, dir)
328 struct dir *left = dir->d_left;
330 /* whee. move the left's right tree into dir's left tree */
331 dir->d_left = left->d_right;
332 if (left->d_right != SENTINEL)
333 left->d_right->d_back = dir;
335 if (left != SENTINEL) {
336 left->d_back = dir->d_back;
340 if (!dir->d_back) /* no parent. move the left tree to the top. */
342 else if (dir == dir->d_back->d_right) /* we were on the right */
343 dir->d_back->d_right = left;
345 dir->d_back->d_left = left; /* we were on the left */
347 /* re-insert dir on the right tree */
353 /* recolor after a removal */
354 static struct dir *dir_rmrecolor(vol, dir)
360 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
361 /* are we on the left tree? */
362 if (dir == dir->d_back->d_left) {
363 leaf = dir->d_back->d_right; /* get right side */
364 if (leaf->d_color == DIRTREE_COLOR_RED) {
365 /* we're red. we need to change to black. */
366 leaf->d_color = DIRTREE_COLOR_BLACK;
367 dir->d_back->d_color = DIRTREE_COLOR_RED;
368 dir_leftrotate(vol, dir->d_back);
369 leaf = dir->d_back->d_right;
372 /* right leaf has black end nodes */
373 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
374 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
375 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
376 dir = dir->d_back; /* ascend */
378 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
379 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
380 leaf->d_color = DIRTREE_COLOR_RED;
381 dir_rightrotate(vol, leaf);
382 leaf = dir->d_back->d_right;
384 leaf->d_color = dir->d_back->d_color;
385 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
386 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
387 dir_leftrotate(vol, dir->d_back);
390 } else { /* right tree */
391 leaf = dir->d_back->d_left; /* left tree */
392 if (leaf->d_color == DIRTREE_COLOR_RED) {
393 leaf->d_color = DIRTREE_COLOR_BLACK;
394 dir->d_back->d_color = DIRTREE_COLOR_RED;
395 dir_rightrotate(vol, dir->d_back);
396 leaf = dir->d_back->d_left;
399 /* left leaf has black end nodes */
400 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
401 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
402 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
403 dir = dir->d_back; /* ascend */
405 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
406 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
407 leaf->d_color = DIRTREE_COLOR_RED;
408 dir_leftrotate(vol, leaf);
409 leaf = dir->d_back->d_left;
411 leaf->d_color = dir->d_back->d_color;
412 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
413 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
414 dir_rightrotate(vol, dir->d_back);
419 dir->d_color = DIRTREE_COLOR_BLACK;
425 /* --------------------- */
426 static void dir_hash_del(const struct vol *vol, struct dir *dir)
430 hn = hash_lookup(vol->v_hash, dir);
432 LOG(log_error, logtype_afpd, "dir_hash_del: %s not hashed", dir->d_u_name);
435 hash_delete(vol->v_hash, hn);
439 /* remove the node from the tree. this is just like insertion, but
440 * different. actually, it has to worry about a bunch of things that
441 * insertion doesn't care about. */
442 static void dir_remove( 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 #else /* ! REMOVE_NODES */
462 /* go searching for a node with at most one child */
463 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
467 while (node->d_left != SENTINEL)
472 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
475 leaf->d_back = node->d_back;
478 } else if (node == node->d_back->d_left) { /* left tree */
479 node->d_back->d_left = leaf;
481 node->d_back->d_right = leaf;
484 /* we want to free node, but we also want to free the data in dir.
485 * currently, that's d_name and the directory traversal bits.
486 * we just copy the necessary bits and then fix up all the
487 * various pointers to the directory. needless to say, there are
488 * a bunch of places that store the directory struct. */
490 struct dir save, *tmp;
492 memcpy(&save, dir, sizeof(save));
493 memcpy(dir, node, sizeof(struct dir));
495 /* restore the red-black bits */
496 dir->d_left = save.d_left;
497 dir->d_right = save.d_right;
498 dir->d_back = save.d_back;
499 dir->d_color = save.d_color;
501 if (node == vol->v_dir) {/* we may need to fix up this pointer */
503 rootpar.d_child = vol->v_dir;
505 /* if we aren't the root directory, we have parents and
506 * siblings to worry about */
507 if (dir->d_parent->d_child == node)
508 dir->d_parent->d_child = dir;
509 dir->d_next->d_prev = dir;
510 dir->d_prev->d_next = dir;
513 /* fix up children. */
517 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
520 if (node == curdir) /* another pointer to fixup */
523 /* we also need to fix up oforks. bleah */
524 if ((of = dir->d_ofork)) {
525 last = of->of_d_prev;
528 of = (last == of) ? NULL : of->of_d_next;
532 /* set the node's d_name */
533 node->d_m_name = save.d_m_name;
534 node->d_u_name = save.d_u_name;
537 if (node->d_color == DIRTREE_COLOR_BLACK)
538 dir_rmrecolor(vol, leaf);
540 if (node->d_u_name != node->d_m_name) {
541 free(node->d_u_name);
543 free(node->d_m_name);
545 #endif /* ! REMOVE_NODES */
548 /* ---------------------------------------
549 * remove the node and its childs from the tree
551 * FIXME what about opened forks with refs to it?
552 * it's an afp specs violation because you can't delete
553 * an opened forks. Now afpd doesn't care about forks opened by other
554 * process. It's fixable within afpd if fnctl_lock, doable with smb and
555 * next to impossible for nfs and local filesystem access.
557 static void dir_invalidate( vol, dir )
558 const struct vol *vol;
562 /* v_root can't be deleted */
563 if (movecwd(vol, vol->v_root) < 0) {
564 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
568 dirchildremove(dir->d_parent, dir);
569 dir_remove( vol, dir );
572 /* ------------------------------------ */
573 static struct dir *dir_insert(vol, dir)
574 const struct vol *vol;
580 while (pdir->d_did != dir->d_did ) {
581 if ( pdir->d_did > dir->d_did ) {
582 if ( pdir->d_left == SENTINEL ) {
589 if ( pdir->d_right == SENTINEL ) {
594 pdir = pdir->d_right;
601 * attempt to extend the current dir. tree to include path
602 * as a side-effect, movecwd to that point and return the new dir
605 extenddir( vol, dir, path )
612 if ( path->u_name == NULL) {
613 afp_errno = AFPERR_PARAM;
616 if (of_stat( path ) != 0 ) {
620 if (!S_ISDIR(path->st.st_mode)) {
624 /* mac name is always with the right encoding (from cname()) */
625 if (( dir = adddir( vol, dir, path)) == NULL ) {
630 if ( movecwd( vol, dir ) < 0 ) {
637 /* -------------------
638 system rmdir with afp error code.
639 ENOENT is not an error.
641 int netatalk_rmdir(const char *name)
643 if (rmdir(name) < 0) {
648 return AFPERR_DIRNEMPT;
651 return AFPERR_ACCESS;
661 /* -------------------------
662 appledouble mkdir afp error code.
664 static int netatalk_mkdir(const char *name)
666 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
669 return( AFPERR_NOOBJ );
671 return( AFPERR_VLOCK );
674 return( AFPERR_ACCESS );
676 return( AFPERR_EXIST );
679 return( AFPERR_DFULL );
681 return( AFPERR_PARAM );
687 /* -------------------
688 system unlink with afp error code.
689 ENOENT is not an error.
691 int netatalk_unlink(const char *name)
693 if (unlink(name) < 0) {
701 return AFPERR_ACCESS;
709 /* ------------------- */
710 static int deletedir(char *dir)
712 char path[MAXPATHLEN + 1];
720 if ((len = strlen(dir)) +2 > sizeof(path))
724 if ((dp = opendir(dir)) == NULL)
730 remain = sizeof(path) -len -1;
731 while ((de = readdir(dp)) && err == AFP_OK) {
732 /* skip this and previous directory */
733 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
736 if (strlen(de->d_name) > remain) {
740 strcpy(path + len, de->d_name);
741 if (stat(path, &st)) {
744 if (S_ISDIR(st.st_mode)) {
745 err = deletedir(path);
747 err = netatalk_unlink(path);
752 /* okay. the directory is empty. delete it. note: we already got rid
755 err = netatalk_rmdir(dir);
760 /* do a recursive copy. */
761 static int copydir(const struct vol *vol, char *src, char *dst)
763 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
772 /* doesn't exist or the path is too long. */
773 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
774 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
775 ((dp = opendir(src)) == NULL))
778 /* try to create the destination directory */
779 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
784 /* set things up to copy */
788 srem = sizeof(spath) - slen -1;
793 drem = sizeof(dpath) - dlen -1;
796 while ((de = readdir(dp))) {
797 /* skip this and previous directory */
798 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
801 if (strlen(de->d_name) > srem) {
805 strcpy(spath + slen, de->d_name);
807 if (stat(spath, &st) == 0) {
808 if (strlen(de->d_name) > drem) {
812 strcpy(dpath + dlen, de->d_name);
814 if (S_ISDIR(st.st_mode)) {
815 if (AFP_OK != (err = copydir(vol, spath, dpath)))
817 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
821 /* keep the same time stamp. */
822 ut.actime = ut.modtime = st.st_mtime;
828 /* keep the same time stamp. */
829 if (stat(src, &st) == 0) {
830 ut.actime = ut.modtime = st.st_mtime;
840 /* --- public functions follow --- */
842 /* NOTE: we start off with at least one node (the root directory). */
843 static struct dir *dirinsert( vol, dir )
849 if ((node = dir_insert(vol, dir)))
852 /* recolor the tree. the current node is red. */
853 dir->d_color = DIRTREE_COLOR_RED;
855 /* parent of this node has to be black. if the parent node
856 * is red, then we have a grandparent. */
857 while ((dir != vol->v_root) &&
858 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
859 /* are we on the left tree? */
860 if (dir->d_back == dir->d_back->d_back->d_left) {
861 node = dir->d_back->d_back->d_right; /* get the right node */
862 if (node->d_color == DIRTREE_COLOR_RED) {
863 /* we're red. we need to change to black. */
864 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
865 node->d_color = DIRTREE_COLOR_BLACK;
866 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
867 dir = dir->d_back->d_back; /* finished. go up. */
869 if (dir == dir->d_back->d_right) {
871 dir_leftrotate(vol, dir);
873 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
874 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
875 dir_rightrotate(vol, dir->d_back->d_back);
878 node = dir->d_back->d_back->d_left;
879 if (node->d_color == DIRTREE_COLOR_RED) {
880 /* we're red. we need to change to black. */
881 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
882 node->d_color = DIRTREE_COLOR_BLACK;
883 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
884 dir = dir->d_back->d_back; /* finished. ascend */
886 if (dir == dir->d_back->d_left) {
888 dir_rightrotate(vol, dir);
890 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
891 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
892 dir_leftrotate(vol, dir->d_back->d_back);
897 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
901 /* ---------------------------- */
903 adddir( vol, dir, path)
908 struct dir *cdir, *edir;
916 upath = path->u_name;
918 upathlen = strlen(upath);
920 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
924 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
928 if ((cdir = dirnew(name, upath)) == NULL) {
929 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
935 if ((edir = dirinsert( vol, cdir ))) {
936 /* it's not possible with LASTDID
938 - someone else have moved the directory.
939 - it's a symlink inside the share.
940 - it's an ID reused, the old directory was deleted but not
941 the cnid record and the server've reused the inode for
943 for HASH (we should get ride of HASH)
944 - someone else have moved the directory.
945 - it's an ID reused as above
946 - it's a hash duplicate and we are in big trouble
948 deleted = (edir->d_m_name == NULL);
950 dir_hash_del(vol, edir);
952 edir->d_m_name = cdir->d_m_name;
953 edir->d_u_name = cdir->d_u_name;
956 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
957 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
958 hash_alloc_insert(vol->v_hash, cdir, cdir);
961 /* the old was not in the same folder */
963 dirchildremove(cdir->d_parent, cdir);
966 /* parent/child directories */
967 cdir->d_parent = dir;
968 dirchildadd(vol, dir, cdir);
972 /* --- public functions follow --- */
973 /* free everything down. we don't bother to recolor as this is only
974 * called to free the entire tree */
975 void dirfreename(struct dir *dir)
977 if (dir->d_u_name != dir->d_m_name) {
986 if (!dir || (dir == SENTINEL))
989 if ( dir->d_left != SENTINEL ) {
990 dirfree( dir->d_left );
992 if ( dir->d_right != SENTINEL ) {
993 dirfree( dir->d_right );
996 if (dir != SENTINEL) {
1002 /* --------------------------------------------
1003 * most of the time mac name and unix name are the same
1005 struct dir *dirnew(const char *m_name, const char *u_name)
1009 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1013 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1018 if (m_name == u_name || !strcmp(m_name, u_name)) {
1019 dir->d_u_name = dir->d_m_name;
1021 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1022 free(dir->d_m_name);
1027 dir->d_left = dir->d_right = SENTINEL;
1028 dir->d_next = dir->d_prev = dir;
1032 /* ------------------ */
1033 static hash_val_t hash_fun_dir(const void *key)
1035 const struct dir *k = key;
1037 static unsigned long randbox[] = {
1038 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1039 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1040 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1041 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1044 const unsigned char *str = (unsigned char *)k->d_u_name;
1048 acc ^= randbox[(*str + acc) & 0xf];
1049 acc = (acc << 1) | (acc >> 31);
1051 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1052 acc = (acc << 2) | (acc >> 30);
1058 /* ---------------- */
1059 static int hash_comp_dir(const void *key1, const void *key2)
1061 const struct dir *k1 = key1;
1062 const struct dir *k2 = key2;
1064 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1067 /* ---------------- */
1071 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1074 /* ------------------ */
1075 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1078 movecwd failed some of dir path are not there anymore.
1079 FIXME Is it true with other errors?
1080 so we remove dir from the cache
1082 if (dir->d_did == DIRDID_ROOT_PARENT)
1084 if (afp_errno == AFPERR_ACCESS) {
1085 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1088 /* FIXME should we set these?, don't need to call stat() after:
1090 ret->st_errno = EACCES;
1092 ret->m_name = dir->d_m_name;
1093 ret->u_name = dir->d_u_name;
1096 } else if (afp_errno == AFPERR_NOOBJ) {
1097 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1100 strcpy(ret->m_name, dir->d_m_name);
1101 if (dir->d_m_name == dir->d_u_name) {
1102 ret->u_name = ret->m_name;
1105 size_t tp = strlen(ret->m_name)+1;
1107 ret->u_name = ret->m_name +tp;
1108 strcpy(ret->u_name, dir->d_u_name);
1110 /* FIXME should we set :
1112 ret->st_errno = ENOENT;
1114 dir_invalidate(vol, dir);
1117 dir_invalidate(vol, dir);
1121 /* -------------------------------------------------- */
1127 stat the file or errno
1130 curdir: filename parent directory
1136 stat the dir or errno
1140 curdir: dir parent directory
1148 curdir: dir parent directory
1155 cname( vol, dir, cpath )
1156 const struct vol *vol;
1161 static char path[ MAXPATHLEN + 1];
1162 static struct path ret;
1174 afp_errno = AFPERR_NOOBJ;
1175 memset(&ret, 0, sizeof(ret));
1176 switch (ret.m_type = *data) { /* path type */
1179 len = (unsigned char) *data++;
1182 if (afp_version >= 30) {
1188 if (afp_version >= 30) {
1190 memcpy(&hint, data, sizeof(hint));
1192 data += sizeof(hint);
1194 memcpy(&len16, data, sizeof(len16));
1201 /* else it's an error */
1203 afp_errno = AFPERR_PARAM;
1206 *cpath += len + size;
1211 if (movecwd( vol, dir ) < 0 ) {
1212 return invalidate(vol, dir, &ret );
1214 if (*path == '\0') {
1221 if (*data == sep ) {
1225 while (*data == sep && len > 0 ) {
1226 if ( dir->d_parent == NULL ) {
1229 dir = dir->d_parent;
1234 /* would this be faster with strlen + strncpy? */
1236 while ( *data != sep && len > 0 ) {
1238 if (p > &path[ MAXPATHLEN]) {
1239 afp_errno = AFPERR_PARAM;
1245 /* short cut bits by chopping off a trailing \0. this also
1246 makes the traversal happy w/ filenames at the end of the
1253 if ( p == path ) { /* end of the name parameter */
1257 if (afp_version >= 30) {
1262 static char temp[ MAXPATHLEN + 1];
1264 /* not an UTF8 name */
1265 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1266 afp_errno = AFPERR_PARAM;
1271 /* check for OS X mangled filename :( */
1273 t = demangle_osx(vol, path, dir->d_did, &fileid);
1276 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1277 * flags weren't the same
1279 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1280 /* at last got our view of mac name */
1285 if (ret.u_name == NULL) {
1286 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1287 afp_errno = AFPERR_PARAM;
1292 if (dir->d_did == DIRDID_ROOT_PARENT) {
1293 /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
1294 * d_m_name is the Mac volume name
1295 * d_u_name is the volume unix directory name
1299 if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
1304 cdir = dirsearch_byname(vol, dir, ret.u_name);
1307 if ( cdir == NULL ) {
1309 /* if dir == curdir it always succeed,
1310 even if curdir is deleted.
1311 it's not a pb because it will fail in extenddir
1313 if ( movecwd( vol, dir ) < 0 ) {
1314 /* dir is not valid anymore
1315 we delete dir from the cache and abort.
1317 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1318 afp_errno = AFPERR_NOOBJ;
1321 if (afp_errno == AFPERR_ACCESS)
1323 dir_invalidate(vol, dir);
1326 cdir = extenddir( vol, dir, &ret );
1330 cdir = extenddir( vol, dir, &ret );
1331 } /* if (!extend) */
1333 if ( cdir == NULL ) {
1347 * Move curdir to dir, with a possible chdir()
1349 int movecwd( vol, dir)
1350 const struct vol *vol;
1353 char path[MAXPATHLEN + 1];
1358 if ( dir == curdir ) {
1361 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1362 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1366 p = path + sizeof(path) - 1;
1369 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1372 /* parent directory is deleted */
1373 afp_errno = AFPERR_NOOBJ;
1377 if (p -n -1 < path) {
1378 afp_errno = AFPERR_PARAM;
1385 if ( d != curdir ) {
1386 n = strlen( vol->v_path );
1387 if (p -n -1 < path) {
1388 afp_errno = AFPERR_PARAM;
1393 memcpy( p, vol->v_path, n );
1395 if ( chdir( p ) < 0 ) {
1399 afp_errno = AFPERR_ACCESS;
1402 afp_errno = AFPERR_NOOBJ;
1412 * We can't use unix file's perm to support Apple's inherited protection modes.
1413 * If we aren't the file's owner we can't change its perms when moving it and smb
1414 * nfs,... don't even try.
1416 #define AFP_CHECK_ACCESS
1418 int check_access(char *path, int mode)
1420 #ifdef AFP_CHECK_ACCESS
1428 accessmode(p, &ma, curdir, NULL);
1429 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1431 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1437 /* --------------------- */
1438 int file_access(struct path *path, int mode)
1442 accessmode(path->u_name, &ma, curdir, &path->st);
1443 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1445 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1451 /* --------------------- */
1452 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1454 dir->offcnt = count;
1455 dir->ctime = st->st_ctime;
1456 dir->d_flags &= ~DIRF_CNID;
1459 /* ---------------------
1460 * is our cached offspring count valid?
1463 int diroffcnt(struct dir *dir, struct stat *st)
1465 return st->st_ctime == dir->ctime;
1468 /* ---------------------
1469 * is our cached also for reenumerate id?
1472 int dirreenumerate(struct dir *dir, struct stat *st)
1474 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1477 /* --------------------- */
1478 static int invisible_dots(const struct vol *vol, const char *name)
1480 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
1483 /* ------------------------------
1485 (name, dir) with curdir:name == dir, from afp_enumerate
1488 int getdirparams(const struct vol *vol,
1489 u_int16_t bitmap, struct path *s_path,
1491 char *buf, int *buflen )
1495 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1496 int bit = 0, isad = 0;
1502 struct stat *st = &s_path->st;
1503 char *upath = s_path->u_name;
1505 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1506 (1 << DIRPBIT_CDATE) |
1507 (1 << DIRPBIT_MDATE) |
1508 (1 << DIRPBIT_BDATE) |
1509 (1 << DIRPBIT_FINFO)))) {
1511 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1512 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1517 if ( dir->d_did == DIRDID_ROOT) {
1518 pdid = DIRDID_ROOT_PARENT;
1519 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1522 pdid = dir->d_parent->d_did;
1526 while ( bitmap != 0 ) {
1527 while (( bitmap & 1 ) == 0 ) {
1535 ad_getattr(&ad, &ashort);
1536 } else if (invisible_dots(vol, dir->d_u_name)) {
1537 ashort = htons(ATTRBIT_INVISIBLE);
1540 ashort |= htons(ATTRBIT_SHARED);
1541 memcpy( data, &ashort, sizeof( ashort ));
1542 data += sizeof( ashort );
1546 memcpy( data, &pdid, sizeof( pdid ));
1547 data += sizeof( pdid );
1550 case DIRPBIT_CDATE :
1551 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1552 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1553 memcpy( data, &aint, sizeof( aint ));
1554 data += sizeof( aint );
1557 case DIRPBIT_MDATE :
1558 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1559 memcpy( data, &aint, sizeof( aint ));
1560 data += sizeof( aint );
1563 case DIRPBIT_BDATE :
1564 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1565 aint = AD_DATE_START;
1566 memcpy( data, &aint, sizeof( aint ));
1567 data += sizeof( aint );
1570 case DIRPBIT_FINFO :
1572 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1573 } else { /* no appledouble */
1574 memset( data, 0, 32 );
1575 /* set default view -- this also gets done in ad_open() */
1576 ashort = htons(FINDERINFO_CLOSEDVIEW);
1577 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1579 /* dot files are by default visible */
1580 if (invisible_dots(vol, dir->d_u_name)) {
1581 ashort = htons(FINDERINFO_INVISIBLE);
1582 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1588 case DIRPBIT_LNAME :
1589 if (dir->d_m_name) /* root of parent can have a null name */
1592 memset(data, 0, sizeof(u_int16_t));
1593 data += sizeof( u_int16_t );
1596 case DIRPBIT_SNAME :
1597 memset(data, 0, sizeof(u_int16_t));
1598 data += sizeof( u_int16_t );
1602 memcpy( data, &dir->d_did, sizeof( aint ));
1603 data += sizeof( aint );
1606 case DIRPBIT_OFFCNT :
1608 /* this needs to handle current directory access rights */
1609 if (diroffcnt(dir, st)) {
1610 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1612 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1613 setdiroffcnt(dir, st, ret);
1614 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1616 ashort = htons( ashort );
1617 memcpy( data, &ashort, sizeof( ashort ));
1618 data += sizeof( ashort );
1622 aint = htonl(st->st_uid);
1623 memcpy( data, &aint, sizeof( aint ));
1624 data += sizeof( aint );
1628 aint = htonl(st->st_gid);
1629 memcpy( data, &aint, sizeof( aint ));
1630 data += sizeof( aint );
1633 case DIRPBIT_ACCESS :
1634 accessmode( upath, &ma, dir , st);
1636 *data++ = ma.ma_user;
1637 *data++ = ma.ma_world;
1638 *data++ = ma.ma_group;
1639 *data++ = ma.ma_owner;
1642 /* Client has requested the ProDOS information block.
1643 Just pass back the same basic block for all
1644 directories. <shirsch@ibm.net> */
1645 case DIRPBIT_PDINFO :
1646 if (afp_version >= 30) { /* UTF8 name */
1647 utf8 = kTextEncodingUTF8;
1648 if (dir->d_m_name) /* root of parent can have a null name */
1651 memset(data, 0, sizeof(u_int16_t));
1652 data += sizeof( u_int16_t );
1654 memcpy(data, &aint, sizeof( aint ));
1655 data += sizeof( aint );
1657 else { /* ProDOS Info Block */
1660 ashort = htons( 0x0200 );
1661 memcpy( data, &ashort, sizeof( ashort ));
1662 data += sizeof( ashort );
1663 memset( data, 0, sizeof( ashort ));
1664 data += sizeof( ashort );
1668 case DIRPBIT_UNIXPR :
1669 aint = htonl(st->st_uid);
1670 memcpy( data, &aint, sizeof( aint ));
1671 data += sizeof( aint );
1672 aint = htonl(st->st_gid);
1673 memcpy( data, &aint, sizeof( aint ));
1674 data += sizeof( aint );
1677 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1678 memcpy( data, &aint, sizeof( aint ));
1679 data += sizeof( aint );
1681 accessmode( upath, &ma, dir , st);
1683 *data++ = ma.ma_user;
1684 *data++ = ma.ma_world;
1685 *data++ = ma.ma_group;
1686 *data++ = ma.ma_owner;
1691 ad_close_metadata( &ad );
1693 return( AFPERR_BITMAP );
1699 ashort = htons( data - buf );
1700 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1701 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1703 if ( utf_nameoff ) {
1704 ashort = htons( data - buf );
1705 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1706 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1709 ad_close_metadata( &ad );
1711 *buflen = data - buf;
1715 /* ----------------------------- */
1716 int path_error(struct path *path, int error)
1718 /* - a dir with access error
1719 * - no error it's a file
1722 if (path_isadir(path))
1724 if (path->st_valid && path->st_errno)
1726 return AFPERR_BADTYPE ;
1729 /* ----------------------------- */
1730 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1732 char *ibuf, *rbuf _U_;
1733 int ibuflen _U_, *rbuflen;
1738 u_int16_t vid, bitmap;
1744 memcpy( &vid, ibuf, sizeof( vid ));
1745 ibuf += sizeof( vid );
1747 if (NULL == ( vol = getvolbyvid( vid )) ) {
1748 return( AFPERR_PARAM );
1751 if (vol->v_flags & AFPVOL_RO)
1752 return AFPERR_VLOCK;
1754 memcpy( &did, ibuf, sizeof( did ));
1755 ibuf += sizeof( int );
1757 if (NULL == ( dir = dirlookup( vol, did )) ) {
1761 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1762 bitmap = ntohs( bitmap );
1763 ibuf += sizeof( bitmap );
1765 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1766 return get_afp_errno(AFPERR_NOOBJ);
1769 if ( *path->m_name != '\0' ) {
1770 rc = path_error(path, AFPERR_NOOBJ);
1771 /* maybe we are trying to set perms back */
1772 if (rc != AFPERR_ACCESS)
1777 * If ibuf is odd, make it even.
1779 if ((u_long)ibuf & 1 ) {
1783 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1784 setvoltime(obj, vol );
1790 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1792 * assume path == '\0' eg. it's a directory in canonical form
1795 struct path Cur_Path = {
1798 ".", /* unix name */
1800 NULL,/* struct dir */
1801 0, /* stat is not set */
1804 /* ------------------ */
1805 static int set_dir_errors(struct path *path, const char *where, int err)
1810 return AFPERR_ACCESS;
1812 return AFPERR_VLOCK;
1814 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1815 return AFPERR_PARAM;
1818 /* ------------------ */
1819 int setdirparams(const struct vol *vol,
1820 struct path *path, u_int16_t d_bitmap, char *buf )
1832 u_int16_t ashort, bshort;
1834 int change_mdate = 0;
1835 int change_parent_mdate = 0;
1837 u_int16_t bitmap = d_bitmap;
1838 u_char finder_buf[32];
1841 u_int16_t upriv_bit = 0;
1844 upath = path->u_name;
1846 while ( bitmap != 0 ) {
1847 while (( bitmap & 1 ) == 0 ) {
1855 memcpy( &ashort, buf, sizeof( ashort ));
1856 buf += sizeof( ashort );
1858 case DIRPBIT_CDATE :
1860 memcpy(&cdate, buf, sizeof(cdate));
1861 buf += sizeof( cdate );
1863 case DIRPBIT_MDATE :
1864 memcpy(&newdate, buf, sizeof(newdate));
1865 buf += sizeof( newdate );
1867 case DIRPBIT_BDATE :
1869 memcpy(&bdate, buf, sizeof(bdate));
1870 buf += sizeof( bdate );
1872 case DIRPBIT_FINFO :
1874 memcpy( finder_buf, buf, 32 );
1877 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1878 change_parent_mdate = 1;
1879 memcpy( &owner, buf, sizeof(owner));
1880 buf += sizeof( owner );
1883 change_parent_mdate = 1;
1884 memcpy( &group, buf, sizeof( group ));
1885 buf += sizeof( group );
1887 case DIRPBIT_ACCESS :
1889 change_parent_mdate = 1;
1890 ma.ma_user = *buf++;
1891 ma.ma_world = *buf++;
1892 ma.ma_group = *buf++;
1893 ma.ma_owner = *buf++;
1894 mpriv = mtoumode( &ma ) | vol->v_perm;
1895 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1896 err = set_dir_errors(path, "setdirmode", errno);
1900 /* Ignore what the client thinks we should do to the
1901 ProDOS information block. Skip over the data and
1902 report nothing amiss. <shirsch@ibm.net> */
1903 case DIRPBIT_PDINFO :
1904 if (afp_version < 30) {
1908 err = AFPERR_BITMAP;
1912 case DIRPBIT_UNIXPR :
1913 if (vol_unix_priv(vol)) {
1914 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1915 buf += sizeof( owner );
1916 memcpy( &group, buf, sizeof( group ));
1917 buf += sizeof( group );
1920 change_parent_mdate = 1;
1921 memcpy( &upriv, buf, sizeof( upriv ));
1922 buf += sizeof( upriv );
1923 upriv = ntohl (upriv) | vol->v_perm;
1924 if (dir_rx_set(upriv)) {
1925 /* maybe we are trying to set perms back */
1926 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1928 err = set_dir_errors(path, "setdirunixmode", errno);
1939 err = AFPERR_BITMAP;
1947 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1949 if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1951 * Check to see what we're trying to set. If it's anything
1952 * but ACCESS, UID, or GID, give an error. If it's any of those
1953 * three, we don't need the ad to be open, so just continue.
1955 * note: we also don't need to worry about mdate. also, be quiet
1956 * if we're using the noadouble option.
1958 if (!vol_noadouble(vol) && (d_bitmap &
1959 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1960 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1961 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1962 return AFPERR_ACCESS;
1968 * Check to see if a create was necessary. If it was, we'll want
1969 * to set our name, etc.
1971 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1972 ad_setname(&ad, curdir->d_m_name);
1978 while ( bitmap != 0 ) {
1979 while (( bitmap & 1 ) == 0 ) {
1987 ad_getattr(&ad, &bshort);
1988 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
1989 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
1990 change_parent_mdate = 1;
1991 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1992 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1996 ad_setattr(&ad, bshort);
1999 case DIRPBIT_CDATE :
2001 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2004 case DIRPBIT_MDATE :
2006 case DIRPBIT_BDATE :
2008 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2011 case DIRPBIT_FINFO :
2013 if ( dir->d_did == DIRDID_ROOT ) {
2015 * Alright, we admit it, this is *really* sick!
2016 * The 4 bytes that we don't copy, when we're dealing
2017 * with the root of a volume, are the directory's
2018 * location information. This eliminates that annoying
2019 * behavior one sees when mounting above another mount
2022 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2023 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2025 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2029 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2030 if ( (dir->d_did == DIRDID_ROOT) &&
2031 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2032 err = set_dir_errors(path, "setdeskowner", errno);
2033 if (isad && err == AFPERR_PARAM) {
2034 err = AFP_OK; /* ???*/
2037 goto setdirparam_done;
2040 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2041 err = set_dir_errors(path, "setdirowner", errno);
2042 goto setdirparam_done;
2046 if (dir->d_did == DIRDID_ROOT)
2047 setdeskowner( -1, ntohl(group) );
2048 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2049 err = set_dir_errors(path, "setdirowner", errno);
2050 goto setdirparam_done;
2053 case DIRPBIT_ACCESS :
2054 if (dir->d_did == DIRDID_ROOT) {
2056 if (!dir_rx_set(mpriv)) {
2057 /* we can't remove read and search for owner on volume root */
2058 err = AFPERR_ACCESS;
2059 goto setdirparam_done;
2063 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2064 err = set_dir_errors(path, "setdirmode", errno);
2065 goto setdirparam_done;
2068 case DIRPBIT_PDINFO :
2069 if (afp_version >= 30) {
2070 err = AFPERR_BITMAP;
2071 goto setdirparam_done;
2074 case DIRPBIT_UNIXPR :
2075 if (vol_unix_priv(vol)) {
2076 if (dir->d_did == DIRDID_ROOT) {
2077 if (!dir_rx_set(upriv)) {
2078 /* we can't remove read and search for owner on volume root */
2079 err = AFPERR_ACCESS;
2080 goto setdirparam_done;
2082 setdeskowner( -1, ntohl(group) );
2083 setdeskmode( upriv );
2085 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2086 err = set_dir_errors(path, "setdirowner", errno);
2087 goto setdirparam_done;
2090 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2091 err = set_dir_errors(path, "setdirunixmode", errno);
2092 goto setdirparam_done;
2096 err = AFPERR_BITMAP;
2097 goto setdirparam_done;
2101 err = AFPERR_BITMAP;
2102 goto setdirparam_done;
2111 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2112 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2116 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2117 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2122 if (path->st_valid && !path->st_errno) {
2123 struct stat *st = &path->st;
2125 if (dir && dir->d_parent) {
2126 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2130 ad_close_metadata( &ad);
2133 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2134 && gettimeofday(&tv, NULL) == 0) {
2135 if (!movecwd(vol, dir->d_parent)) {
2136 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2137 /* be careful with bitmap because now dir is null */
2138 bitmap = 1<<DIRPBIT_MDATE;
2139 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2140 /* should we reset curdir ?*/
2148 int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2151 int ibuflen _U_, *rbuflen;
2163 memcpy( &vid, ibuf, sizeof( vid ));
2164 ibuf += sizeof( vid );
2165 if (NULL == (vol = getvolbyvid( vid )) ) {
2166 return( AFPERR_PARAM );
2169 memcpy( &did, ibuf, sizeof( did ));
2170 ibuf += sizeof( did );
2171 if (NULL == ( dir = dirlookup( vol, did )) ) {
2172 return afp_errno; /* was AFPERR_NOOBJ */
2175 if (NULL == ( dp = opendir( "." )) ) {
2178 return( AFPERR_NOOBJ );
2180 return( AFPERR_ACCESS );
2182 return( AFPERR_PARAM );
2187 if ( fsync ( dfd ) < 0 ) {
2188 LOG(log_error, logtype_afpd, "syncdir(%s): ddir(%d) %s", dir->d_u_name, dfd, strerror(errno) );
2194 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2197 int ibuflen _U_, *rbuflen;
2203 struct path *s_path;
2211 memcpy( &vid, ibuf, sizeof( vid ));
2212 ibuf += sizeof( vid );
2213 if (NULL == ( vol = getvolbyvid( vid )) ) {
2214 return( AFPERR_PARAM );
2217 if (vol->v_flags & AFPVOL_RO)
2218 return AFPERR_VLOCK;
2220 memcpy( &did, ibuf, sizeof( did ));
2221 ibuf += sizeof( did );
2222 if (NULL == ( dir = dirlookup( vol, did )) ) {
2223 return afp_errno; /* was AFPERR_NOOBJ */
2225 /* for concurrent access we need to be sure we are not in the
2226 * folder we want to create...
2230 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2231 return get_afp_errno(AFPERR_PARAM);
2233 /* cname was able to move curdir to it! */
2234 if (*s_path->m_name == '\0')
2235 return AFPERR_EXIST;
2237 upath = s_path->u_name;
2238 if (0 != (err = check_name(vol, upath))) {
2242 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2246 if (of_stat(s_path) < 0) {
2250 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2254 if ( movecwd( vol, dir ) < 0 ) {
2255 return( AFPERR_PARAM );
2258 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2259 if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2260 if (vol_noadouble(vol))
2261 goto createdir_done;
2262 return( AFPERR_ACCESS );
2264 ad_setname(&ad, s_path->m_name);
2265 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2268 ad_close_metadata( &ad);
2271 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2272 *rbuflen = sizeof( u_int32_t );
2273 setvoltime(obj, vol );
2278 * dst new unix filename (not a pathname)
2279 * newname new mac name
2283 int renamedir(vol, src, dst, dir, newparent, newname)
2284 const struct vol *vol;
2285 char *src, *dst, *newname;
2286 struct dir *dir, *newparent;
2293 /* existence check moved to afp_moveandrename */
2294 if ( unix_rename( src, dst ) < 0 ) {
2297 return( AFPERR_NOOBJ );
2299 return( AFPERR_ACCESS );
2301 return AFPERR_VLOCK;
2303 /* tried to move directory into a subdirectory of itself */
2304 return AFPERR_CANTMOVE;
2306 /* this needs to copy and delete. bleah. that means we have
2307 * to deal with entire directory hierarchies. */
2308 if ((err = copydir(vol, src, dst)) < 0) {
2312 if ((err = deletedir(src)) < 0)
2316 return( AFPERR_PARAM );
2320 vol->vfs->rf_renamedir(vol, src, dst);
2322 len = strlen( newname );
2323 /* rename() succeeded so we need to update our tree even if we can't open
2327 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2329 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2330 ad_setname(&ad, newname);
2332 ad_close_metadata( &ad);
2335 dir_hash_del(vol, dir);
2336 if (dir->d_m_name == dir->d_u_name)
2337 dir->d_u_name = NULL;
2339 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2340 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2341 /* FIXME : fatal ? */
2344 dir->d_m_name = buf;
2345 strcpy( dir->d_m_name, newname );
2347 if (newname == dst) {
2348 free(dir->d_u_name);
2349 dir->d_u_name = dir->d_m_name;
2352 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2353 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2356 dir->d_u_name = buf;
2357 strcpy( dir->d_u_name, dst );
2360 if (( parent = dir->d_parent ) == NULL ) {
2363 if ( parent == newparent ) {
2364 hash_alloc_insert(vol->v_hash, dir, dir);
2368 /* detach from old parent and add to new one. */
2369 dirchildremove(parent, dir);
2370 dir->d_parent = newparent;
2371 dirchildadd(vol, newparent, dir);
2375 /* delete an empty directory */
2376 int deletecurdir( vol)
2377 const struct vol *vol;
2387 if ( curdir->d_parent == NULL ) {
2388 return( AFPERR_ACCESS );
2393 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2394 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2396 ad_getattr(&ad, &ashort);
2397 ad_close( &ad, ADFLAGS_HF );
2398 if ((ashort & htons(ATTRBIT_NODELETE))) {
2399 return AFPERR_OLOCK;
2402 err = vol->vfs->rf_deletecurdir(vol);
2407 /* now get rid of dangling symlinks */
2408 if ((dp = opendir("."))) {
2409 while ((de = readdir(dp))) {
2410 /* skip this and previous directory */
2411 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2414 /* bail if it's not a symlink */
2415 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2417 return AFPERR_DIRNEMPT;
2420 if ((err = netatalk_unlink(de->d_name))) {
2427 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2432 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2433 dirchildremove(curdir, fdir);
2434 cnid_delete(vol->v_cdb, fdir->d_did);
2435 dir_remove( vol, fdir );
2440 /* inode is used as key for cnid.
2441 * Close the descriptor only after cnid_delete
2449 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2452 int ibuflen _U_, *rbuflen;
2462 sfunc = (unsigned char) *ibuf++;
2463 memcpy( &id, ibuf, sizeof( id ));
2468 if (sfunc == 3 || sfunc == 4) {
2469 if (afp_version < 30) {
2470 return( AFPERR_PARAM );
2477 case 3 :/* unicode */
2478 if (( pw = getpwuid( id )) == NULL ) {
2479 return( AFPERR_NOITEM );
2481 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2482 pw->pw_name, strlen(pw->pw_name), &name);
2486 case 4 : /* unicode */
2487 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2488 return( AFPERR_NOITEM );
2490 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2491 gr->gr_name, strlen(gr->gr_name), &name);
2495 return( AFPERR_PARAM );
2497 len = strlen( name );
2504 u_int16_t tp = htons(len);
2505 memcpy(rbuf, &tp, sizeof(tp));
2514 memcpy( rbuf, name, len );
2522 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2525 int ibuflen _U_, *rbuflen;
2534 sfunc = (unsigned char) *ibuf++;
2538 case 2 : /* unicode */
2539 if (afp_version < 30) {
2540 return( AFPERR_PARAM );
2542 memcpy(&ulen, ibuf, sizeof(ulen));
2548 len = (unsigned char) *ibuf++;
2551 return( AFPERR_PARAM );
2558 case 1 : /* unicode */
2560 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2561 return( AFPERR_NOITEM );
2566 case 2 : /* unicode */
2568 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2569 return( AFPERR_NOITEM );
2578 memcpy( rbuf, &id, sizeof( id ));
2579 *rbuflen = sizeof( id );
2583 /* ------------------------------------
2584 variable DID support
2586 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2588 char *ibuf _U_, *rbuf _U_;
2589 int ibuflen _U_, *rbuflen;
2600 /* do nothing as dids are static for the life of the process. */
2604 memcpy(&vid, ibuf, sizeof( vid ));
2605 ibuf += sizeof( vid );
2606 if (( vol = getvolbyvid( vid )) == NULL ) {
2607 return( AFPERR_PARAM );
2610 memcpy( &did, ibuf, sizeof( did ));
2611 ibuf += sizeof( did );
2612 if (( dir = dirlookup( vol, did )) == NULL ) {
2613 return( AFPERR_PARAM );
2616 /* dir_remove -- deletedid */
2622 /* did creation gets done automatically
2623 * there's a pb again with case but move it to cname
2625 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2628 int ibuflen _U_, *rbuflen;
2631 struct dir *parentdir;
2639 memcpy(&vid, ibuf, sizeof(vid));
2640 ibuf += sizeof( vid );
2642 if (NULL == ( vol = getvolbyvid( vid )) ) {
2643 return( AFPERR_PARAM );
2646 memcpy(&did, ibuf, sizeof(did));
2647 ibuf += sizeof(did);
2649 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2653 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2654 return get_afp_errno(AFPERR_PARAM);
2657 if ( *path->m_name != '\0' ) {
2658 return path_error(path, AFPERR_NOOBJ);
2661 if ( !path->st_valid && of_stat(path ) < 0 ) {
2662 return( AFPERR_NOOBJ );
2664 if ( path->st_errno ) {
2665 return( AFPERR_NOOBJ );
2668 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2669 *rbuflen = sizeof(curdir->d_did);