2 * $Id: directory.c,v 1.78 2005-04-30 21:33:41 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
38 #include <sys/param.h>
41 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/logger.h>
48 #include "directory.h"
61 #define SENTINEL (&sentinel)
62 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
63 NULL, NULL, NULL, NULL, NULL, 0, 0,
64 0, 0, NULL, NULL, NULL};
65 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
67 0, 0, NULL, NULL, NULL};
69 /* (from IM: Toolbox Essentials)
70 * dirFinderInfo (DInfo) fields:
72 * frRect 8 folder's window rectangle
74 * frLocation 4 folder's location in window
75 * frView 2 folder's view (default == closedView (256))
77 * extended dirFinderInfo (DXInfo) fields:
78 * frScroll 4 scroll position
79 * frOpenChain: 4 directory ID chain of open folders
80 * frScript: 1 script flag and code
81 * frXFlags: 1 reserved
82 * frComment: 2 comment ID
83 * frPutAway: 4 home directory ID
87 * redid did assignment for directories. now we use red-black trees.
92 const struct vol *vol;
100 afp_errno = AFPERR_PARAM;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
110 /* XXX would be nice to check against curdir but we need to keep its volume */
111 if (vol->curdir && curdir->d_did == did) {
120 afp_errno = AFPERR_NOOBJ;
121 while ( dir != SENTINEL ) {
122 if (dir->d_did == did)
123 return dir->d_m_name ? dir : NULL;
124 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
129 /* ------------------- */
131 int path_isadir(struct path *o_path)
133 return o_path->d_dir != NULL;
135 return o_path->m_name == '\0' || /* we are in a it */
136 !o_path->st_valid || /* in cache but we can't chdir in it */
137 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
142 int get_afp_errno(const int param)
144 if (afp_errno != AFPERR_DID1)
149 /* ------------------- */
151 dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
153 struct dir *dir = NULL;
155 if (cdir->d_did == DIRDID_ROOT_PARENT) {
156 if ( !strcmp(vol->v_dir->d_u_name, name)) {
159 } else if ( cdir->d_child) {
165 hn = hash_lookup(vol->v_hash, &key);
173 /* -----------------------------------------
174 * if did is not in the cache resolve it with cnid
177 * OSX call it with bogus id, ie file ID not folder ID,
178 * and we are really bad in this case.
181 dirlookup( vol, did )
182 const struct vol *vol;
188 static char path[MAXPATHLEN + 1];
191 static char buffer[12 + MAXPATHLEN + 1];
192 int buflen = 12 + MAXPATHLEN + 1;
197 ret = dirsearch(vol, did);
198 if (ret != NULL || afp_errno == AFPERR_PARAM)
201 utf8 = utf8_encoding();
202 maxpath = (utf8)?MAXPATHLEN -7:255;
204 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
205 afp_errno = AFPERR_NOOBJ;
208 ptr = path + MAXPATHLEN;
209 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
210 afp_errno = AFPERR_NOOBJ;
214 pathlen = len; /* no 0 in the last part */
216 strcpy(ptr - len, mpath);
219 ret = dirsearch(vol,id);
224 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
226 NULL == (mpath = utompath(vol, upath, cnid, utf8))
228 afp_errno = AFPERR_NOOBJ;
232 len = strlen(mpath) + 1;
234 if (pathlen > maxpath) {
235 afp_errno = AFPERR_PARAM;
238 strcpy(ptr - len, mpath);
242 /* fill the cache, another place where we know about the path type */
248 temp16 = htons(pathlen);
249 memcpy(ptr, &temp16, sizeof(temp16));
251 temp = htonl(kTextEncodingUTF8);
253 memcpy(ptr, &temp, sizeof(temp));
259 *ptr = (unsigned char)pathlen;
263 /* cname is not efficient */
264 if (cname( vol, ret, &ptr ) == NULL )
267 return dirsearch(vol, did);
270 /* child addition/removal */
271 static void dirchildadd(const struct vol *vol, struct dir *a, struct dir *b)
276 b->d_next = a->d_child;
277 b->d_prev = b->d_next->d_prev;
278 b->d_next->d_prev = b;
279 b->d_prev->d_next = b;
281 if (!hash_alloc_insert(vol->v_hash, b, b)) {
282 LOG(log_error, logtype_afpd, "dirchildadd: can't hash %s", b->d_u_name);
286 static void dirchildremove(struct dir *a,struct dir *b)
289 a->d_child = (b == b->d_next) ? NULL : b->d_next;
290 b->d_next->d_prev = b->d_prev;
291 b->d_prev->d_next = b->d_next;
292 b->d_next = b->d_prev = b;
295 /* --------------------------- */
296 /* rotate the tree to the left */
297 static void dir_leftrotate(vol, dir)
301 struct dir *right = dir->d_right;
303 /* whee. move the right's left tree into dir's right tree */
304 dir->d_right = right->d_left;
305 if (right->d_left != SENTINEL)
306 right->d_left->d_back = dir;
308 if (right != SENTINEL) {
309 right->d_back = dir->d_back;
313 if (!dir->d_back) /* no parent. move the right tree to the top. */
315 else if (dir == dir->d_back->d_left) /* we were on the left */
316 dir->d_back->d_left = right;
318 dir->d_back->d_right = right; /* we were on the right */
320 /* re-insert dir on the left tree */
327 /* rotate the tree to the right */
328 static void dir_rightrotate(vol, dir)
332 struct dir *left = dir->d_left;
334 /* whee. move the left's right tree into dir's left tree */
335 dir->d_left = left->d_right;
336 if (left->d_right != SENTINEL)
337 left->d_right->d_back = dir;
339 if (left != SENTINEL) {
340 left->d_back = dir->d_back;
344 if (!dir->d_back) /* no parent. move the left tree to the top. */
346 else if (dir == dir->d_back->d_right) /* we were on the right */
347 dir->d_back->d_right = left;
349 dir->d_back->d_left = left; /* we were on the left */
351 /* re-insert dir on the right tree */
357 /* recolor after a removal */
358 static struct dir *dir_rmrecolor(vol, dir)
364 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
365 /* are we on the left tree? */
366 if (dir == dir->d_back->d_left) {
367 leaf = dir->d_back->d_right; /* get right side */
368 if (leaf->d_color == DIRTREE_COLOR_RED) {
369 /* we're red. we need to change to black. */
370 leaf->d_color = DIRTREE_COLOR_BLACK;
371 dir->d_back->d_color = DIRTREE_COLOR_RED;
372 dir_leftrotate(vol, dir->d_back);
373 leaf = dir->d_back->d_right;
376 /* right leaf has black end nodes */
377 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
378 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
379 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
380 dir = dir->d_back; /* ascend */
382 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
383 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
384 leaf->d_color = DIRTREE_COLOR_RED;
385 dir_rightrotate(vol, leaf);
386 leaf = dir->d_back->d_right;
388 leaf->d_color = dir->d_back->d_color;
389 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
390 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
391 dir_leftrotate(vol, dir->d_back);
394 } else { /* right tree */
395 leaf = dir->d_back->d_left; /* left tree */
396 if (leaf->d_color == DIRTREE_COLOR_RED) {
397 leaf->d_color = DIRTREE_COLOR_BLACK;
398 dir->d_back->d_color = DIRTREE_COLOR_RED;
399 dir_rightrotate(vol, dir->d_back);
400 leaf = dir->d_back->d_left;
403 /* left leaf has black end nodes */
404 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
405 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
406 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
407 dir = dir->d_back; /* ascend */
409 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
410 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
411 leaf->d_color = DIRTREE_COLOR_RED;
412 dir_leftrotate(vol, leaf);
413 leaf = dir->d_back->d_left;
415 leaf->d_color = dir->d_back->d_color;
416 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
417 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
418 dir_rightrotate(vol, dir->d_back);
423 dir->d_color = DIRTREE_COLOR_BLACK;
429 /* --------------------- */
430 static void dir_hash_del(const struct vol *vol, struct dir *dir)
434 hn = hash_lookup(vol->v_hash, dir);
436 LOG(log_error, logtype_afpd, "dir_hash_del: %s not hashed", dir->d_u_name);
439 hash_delete(vol->v_hash, hn);
443 /* remove the node from the tree. this is just like insertion, but
444 * different. actually, it has to worry about a bunch of things that
445 * insertion doesn't care about. */
446 static void dir_remove( vol, dir )
451 struct ofork *of, *last;
452 struct dir *node, *leaf;
453 #endif /* REMOVE_NODES */
455 if (!dir || (dir == SENTINEL))
458 /* i'm not sure if it really helps to delete stuff. */
459 dir_hash_del(vol, dir);
462 dir->d_m_name = NULL;
463 dir->d_u_name = NULL;
464 dir->d_m_name_ucs2 = NULL;
465 #else /* ! REMOVE_NODES */
467 /* go searching for a node with at most one child */
468 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
472 while (node->d_left != SENTINEL)
477 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
480 leaf->d_back = node->d_back;
483 } else if (node == node->d_back->d_left) { /* left tree */
484 node->d_back->d_left = leaf;
486 node->d_back->d_right = leaf;
489 /* we want to free node, but we also want to free the data in dir.
490 * currently, that's d_name and the directory traversal bits.
491 * we just copy the necessary bits and then fix up all the
492 * various pointers to the directory. needless to say, there are
493 * a bunch of places that store the directory struct. */
495 struct dir save, *tmp;
497 memcpy(&save, dir, sizeof(save));
498 memcpy(dir, node, sizeof(struct dir));
500 /* restore the red-black bits */
501 dir->d_left = save.d_left;
502 dir->d_right = save.d_right;
503 dir->d_back = save.d_back;
504 dir->d_color = save.d_color;
506 if (node == vol->v_dir) {/* we may need to fix up this pointer */
508 rootpar.d_child = vol->v_dir;
510 /* if we aren't the root directory, we have parents and
511 * siblings to worry about */
512 if (dir->d_parent->d_child == node)
513 dir->d_parent->d_child = dir;
514 dir->d_next->d_prev = dir;
515 dir->d_prev->d_next = dir;
518 /* fix up children. */
522 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
525 if (node == curdir) /* another pointer to fixup */
528 /* we also need to fix up oforks. bleah */
529 if ((of = dir->d_ofork)) {
530 last = of->of_d_prev;
533 of = (last == of) ? NULL : of->of_d_next;
537 /* set the node's d_name */
538 node->d_m_name = save.d_m_name;
539 node->d_u_name = save.d_u_name;
540 node->d_m_name_ucs2 = save.d_m_name_ucs2;
543 if (node->d_color == DIRTREE_COLOR_BLACK)
544 dir_rmrecolor(vol, leaf);
546 if (node->d_m_name_ucs2)
547 free(node->d_u_name_ucs2);
548 if (node->d_u_name != node->d_m_name) {
549 free(node->d_u_name);
551 free(node->d_m_name);
553 #endif /* ! REMOVE_NODES */
556 /* ---------------------------------------
557 * remove the node and its childs from the tree
559 * FIXME what about opened forks with refs to it?
560 * it's an afp specs violation because you can't delete
561 * an opened forks. Now afpd doesn't care about forks opened by other
562 * process. It's fixable within afpd if fnctl_lock, doable with smb and
563 * next to impossible for nfs and local filesystem access.
565 static void dir_invalidate( vol, dir )
566 const struct vol *vol;
570 /* v_root can't be deleted */
571 if (movecwd(vol, vol->v_root) < 0) {
572 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
576 dirchildremove(dir->d_parent, dir);
577 dir_remove( vol, dir );
580 /* ------------------------------------ */
581 static struct dir *dir_insert(vol, dir)
582 const struct vol *vol;
588 while (pdir->d_did != dir->d_did ) {
589 if ( pdir->d_did > dir->d_did ) {
590 if ( pdir->d_left == SENTINEL ) {
597 if ( pdir->d_right == SENTINEL ) {
602 pdir = pdir->d_right;
608 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
611 caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
616 static u_int32_t did = 0;
617 static char cname[MAXPATHLEN];
618 static char lname[MAXPATHLEN];
619 ucs2_t u2_path[MAXPATHLEN];
620 ucs2_t u2_dename[MAXPATHLEN];
621 char *tmp, *savepath;
623 if (veto_file(ENUMVETO, path->u_name))
626 savepath = path->u_name;
628 /* very simple cache */
629 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
630 path->u_name = cname;
632 if (of_stat( path ) == 0 ) {
635 /* something changed, we cannot stat ... */
639 if (NULL == ( dp = opendir( "." )) ) {
640 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
645 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
646 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, strlen(path->u_name), u2_path, sizeof(u2_path)) )
647 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
649 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
651 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
652 if (NULL == check_dirent(vol, de->d_name))
655 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, strlen(de->d_name), u2_dename, sizeof(u2_dename)) )
658 if (strcasecmp_w( u2_path, u2_dename) == 0) {
660 strlcpy(cname, de->d_name, sizeof(cname));
661 path->u_name = cname;
663 if (of_stat( path ) == 0 ) {
664 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
665 strlcpy(lname, tmp, sizeof(lname));
678 /* invalidate cache */
679 memset(cname, 0, sizeof(cname));
681 path->u_name = savepath;
683 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
689 * attempt to extend the current dir. tree to include path
690 * as a side-effect, movecwd to that point and return the new dir
693 extenddir( vol, dir, path )
700 if ( path->u_name == NULL) {
701 afp_errno = AFPERR_PARAM;
704 if (of_stat( path ) != 0 ) {
705 if (!(vol->v_flags & AFPVOL_CASEINSEN))
707 else if(caseenumerate(vol, path, dir) != 0)
711 if (!S_ISDIR(path->st.st_mode)) {
715 /* mac name is always with the right encoding (from cname()) */
716 if (( dir = adddir( vol, dir, path)) == NULL ) {
721 if ( movecwd( vol, dir ) < 0 ) {
728 /* -------------------
729 system rmdir with afp error code.
730 ENOENT is not an error.
732 int netatalk_rmdir(const char *name)
734 if (rmdir(name) < 0) {
739 return AFPERR_DIRNEMPT;
742 return AFPERR_ACCESS;
752 /* -------------------------
753 appledouble mkdir afp error code.
755 static int netatalk_mkdir(const char *name)
757 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
760 return( AFPERR_NOOBJ );
762 return( AFPERR_VLOCK );
765 return( AFPERR_ACCESS );
767 return( AFPERR_EXIST );
770 return( AFPERR_DFULL );
772 return( AFPERR_PARAM );
778 /* -------------------
779 system unlink with afp error code.
780 ENOENT is not an error.
782 int netatalk_unlink(const char *name)
784 if (unlink(name) < 0) {
792 return AFPERR_ACCESS;
800 /* ------------------- */
801 static int deletedir(char *dir)
803 char path[MAXPATHLEN + 1];
811 if ((len = strlen(dir)) +2 > sizeof(path))
815 if ((dp = opendir(dir)) == NULL)
821 remain = sizeof(path) -len -1;
822 while ((de = readdir(dp)) && err == AFP_OK) {
823 /* skip this and previous directory */
824 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
827 if (strlen(de->d_name) > remain) {
831 strcpy(path + len, de->d_name);
832 if (stat(path, &st)) {
835 if (S_ISDIR(st.st_mode)) {
836 err = deletedir(path);
838 err = netatalk_unlink(path);
843 /* okay. the directory is empty. delete it. note: we already got rid
846 err = netatalk_rmdir(dir);
851 /* do a recursive copy. */
852 static int copydir(const struct vol *vol, char *src, char *dst)
854 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
863 /* doesn't exist or the path is too long. */
864 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
865 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
866 ((dp = opendir(src)) == NULL))
869 /* try to create the destination directory */
870 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
875 /* set things up to copy */
879 srem = sizeof(spath) - slen -1;
884 drem = sizeof(dpath) - dlen -1;
887 while ((de = readdir(dp))) {
888 /* skip this and previous directory */
889 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
892 if (strlen(de->d_name) > srem) {
896 strcpy(spath + slen, de->d_name);
898 if (stat(spath, &st) == 0) {
899 if (strlen(de->d_name) > drem) {
903 strcpy(dpath + dlen, de->d_name);
905 if (S_ISDIR(st.st_mode)) {
906 if (AFP_OK != (err = copydir(vol, spath, dpath)))
908 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
912 /* keep the same time stamp. */
913 ut.actime = ut.modtime = st.st_mtime;
919 /* keep the same time stamp. */
920 if (stat(src, &st) == 0) {
921 ut.actime = ut.modtime = st.st_mtime;
931 /* --- public functions follow --- */
933 /* NOTE: we start off with at least one node (the root directory). */
934 static struct dir *dirinsert( vol, dir )
940 if ((node = dir_insert(vol, dir)))
943 /* recolor the tree. the current node is red. */
944 dir->d_color = DIRTREE_COLOR_RED;
946 /* parent of this node has to be black. if the parent node
947 * is red, then we have a grandparent. */
948 while ((dir != vol->v_root) &&
949 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
950 /* are we on the left tree? */
951 if (dir->d_back == dir->d_back->d_back->d_left) {
952 node = dir->d_back->d_back->d_right; /* get the right node */
953 if (node->d_color == DIRTREE_COLOR_RED) {
954 /* we're red. we need to change to black. */
955 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
956 node->d_color = DIRTREE_COLOR_BLACK;
957 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
958 dir = dir->d_back->d_back; /* finished. go up. */
960 if (dir == dir->d_back->d_right) {
962 dir_leftrotate(vol, dir);
964 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
965 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
966 dir_rightrotate(vol, dir->d_back->d_back);
969 node = dir->d_back->d_back->d_left;
970 if (node->d_color == DIRTREE_COLOR_RED) {
971 /* we're red. we need to change to black. */
972 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
973 node->d_color = DIRTREE_COLOR_BLACK;
974 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
975 dir = dir->d_back->d_back; /* finished. ascend */
977 if (dir == dir->d_back->d_left) {
979 dir_rightrotate(vol, dir);
981 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
982 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
983 dir_leftrotate(vol, dir->d_back->d_back);
988 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
992 /* ---------------------------- */
994 adddir( vol, dir, path)
999 struct dir *cdir, *edir;
1007 upath = path->u_name;
1009 upathlen = strlen(upath);
1011 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
1015 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
1018 name = path->m_name;
1019 if ((cdir = dirnew(name, upath)) == NULL) {
1020 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
1023 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, path->m_name, strlen(path->m_name), &cdir->d_m_name_ucs2)) {
1024 LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
1025 cdir->d_m_name_ucs2 = NULL;
1030 if ((edir = dirinsert( vol, cdir ))) {
1031 /* it's not possible with LASTDID
1033 - someone else have moved the directory.
1034 - it's a symlink inside the share.
1035 - it's an ID reused, the old directory was deleted but not
1036 the cnid record and the server've reused the inode for
1038 for HASH (we should get ride of HASH)
1039 - someone else have moved the directory.
1040 - it's an ID reused as above
1041 - it's a hash duplicate and we are in big trouble
1043 deleted = (edir->d_m_name == NULL);
1045 dir_hash_del(vol, edir);
1047 edir->d_m_name = cdir->d_m_name;
1048 edir->d_u_name = cdir->d_u_name;
1049 edir->d_m_name_ucs2 = cdir->d_m_name_ucs2;
1052 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
1053 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
1054 hash_alloc_insert(vol->v_hash, cdir, cdir);
1057 /* the old was not in the same folder */
1059 dirchildremove(cdir->d_parent, cdir);
1062 /* parent/child directories */
1063 cdir->d_parent = dir;
1064 dirchildadd(vol, dir, cdir);
1068 /* --- public functions follow --- */
1069 /* free everything down. we don't bother to recolor as this is only
1070 * called to free the entire tree */
1071 void dirfreename(struct dir *dir)
1073 if (dir->d_u_name != dir->d_m_name) {
1074 free(dir->d_u_name);
1076 if (dir->d_m_name_ucs2)
1077 free(dir->d_m_name_ucs2);
1078 free(dir->d_m_name);
1084 if (!dir || (dir == SENTINEL))
1087 if ( dir->d_left != SENTINEL ) {
1088 dirfree( dir->d_left );
1090 if ( dir->d_right != SENTINEL ) {
1091 dirfree( dir->d_right );
1094 if (dir != SENTINEL) {
1100 /* --------------------------------------------
1101 * most of the time mac name and unix name are the same
1103 struct dir *dirnew(const char *m_name, const char *u_name)
1107 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1111 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1116 if (m_name == u_name || !strcmp(m_name, u_name)) {
1117 dir->d_u_name = dir->d_m_name;
1119 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1120 free(dir->d_m_name);
1125 dir->d_m_name_ucs2 = NULL;
1126 dir->d_left = dir->d_right = SENTINEL;
1127 dir->d_next = dir->d_prev = dir;
1131 /* ------------------ */
1132 static hash_val_t hash_fun_dir(const void *key)
1134 const struct dir *k = key;
1136 static unsigned long randbox[] = {
1137 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1138 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1139 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1140 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1143 const unsigned char *str = k->d_u_name;
1147 acc ^= randbox[(*str + acc) & 0xf];
1148 acc = (acc << 1) | (acc >> 31);
1150 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1151 acc = (acc << 2) | (acc >> 30);
1157 /* ---------------- */
1158 static int hash_comp_dir(const void *key1, const void *key2)
1160 const struct dir *k1 = key1;
1161 const struct dir *k2 = key2;
1163 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1166 /* ---------------- */
1170 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1173 /* ------------------ */
1174 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1177 movecwd failed some of dir path are not there anymore.
1178 FIXME Is it true with other errors?
1179 so we remove dir from the cache
1181 if (dir->d_did == DIRDID_ROOT_PARENT)
1183 if (afp_errno == AFPERR_ACCESS) {
1184 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1187 /* FIXME should we set these?, don't need to call stat() after:
1189 ret->st_errno = EACCES;
1191 ret->m_name = dir->d_m_name;
1192 ret->u_name = dir->d_u_name;
1195 } else if (afp_errno == AFPERR_NOOBJ) {
1196 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1199 strcpy(ret->m_name, dir->d_m_name);
1200 if (dir->d_m_name == dir->d_u_name) {
1201 ret->u_name = ret->m_name;
1204 size_t tp = strlen(ret->m_name)+1;
1206 ret->u_name = ret->m_name +tp;
1207 strcpy(ret->u_name, dir->d_u_name);
1209 /* FIXME should we set :
1211 ret->st_errno = ENOENT;
1213 dir_invalidate(vol, dir);
1216 dir_invalidate(vol, dir);
1220 /* -------------------------------------------------- */
1226 stat the file or errno
1229 curdir: filename parent directory
1235 stat the dir or errno
1239 curdir: dir parent directory
1247 curdir: dir parent directory
1254 cname( vol, dir, cpath )
1255 const struct vol *vol;
1259 struct dir *cdir, *scdir=NULL;
1260 static char path[ MAXPATHLEN + 1];
1261 static struct path ret;
1273 afp_errno = AFPERR_NOOBJ;
1274 memset(&ret, 0, sizeof(ret));
1275 switch (ret.m_type = *data) { /* path type */
1278 len = (unsigned char) *data++;
1281 if (afp_version >= 30) {
1287 if (afp_version >= 30) {
1289 memcpy(&hint, data, sizeof(hint));
1291 data += sizeof(hint);
1293 memcpy(&len16, data, sizeof(len16));
1300 /* else it's an error */
1302 afp_errno = AFPERR_PARAM;
1305 *cpath += len + size;
1310 if (movecwd( vol, dir ) < 0 ) {
1311 return invalidate(vol, dir, &ret );
1313 if (*path == '\0') {
1320 if (*data == sep ) {
1324 while (*data == sep && len > 0 ) {
1325 if ( dir->d_parent == NULL ) {
1328 dir = dir->d_parent;
1333 /* would this be faster with strlen + strncpy? */
1335 while ( *data != sep && len > 0 ) {
1337 if (p > &path[ MAXPATHLEN]) {
1338 afp_errno = AFPERR_PARAM;
1344 /* short cut bits by chopping off a trailing \0. this also
1345 makes the traversal happy w/ filenames at the end of the
1352 if ( p == path ) { /* end of the name parameter */
1356 if (afp_version >= 30) {
1361 static char temp[ MAXPATHLEN + 1];
1363 /* not an UTF8 name */
1364 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1365 afp_errno = AFPERR_PARAM;
1370 /* check for OS X mangled filename :( */
1372 t = demangle_osx(vol, path, dir->d_did, &fileid);
1375 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1376 * flags weren't the same
1378 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1379 /* at last got our view of mac name */
1384 if (ret.u_name == NULL) {
1385 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1386 afp_errno = AFPERR_PARAM;
1392 cdir = dir->d_child;
1394 if ( cdir && (vol->v_flags & AFPVOL_CASEINSEN) &&
1395 (size_t)-1 != convert_string_allocate(((ret.m_type == 3)?CH_UTF8_MAC:vol->v_maccharset),
1396 CH_UCS2, path, strlen(path), (char **)&tmpname) )
1399 if (!cdir->d_m_name_ucs2) {
1400 LOG(log_error, logtype_afpd, "cname: no UCS2 name for %s (did %u)!!!", cdir->d_m_name, ntohl(cdir->d_did) );
1401 /* this shouldn't happen !!!! */
1405 if ( strcmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
1408 if ( strcasecmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
1411 cdir = (cdir == dir->d_child->d_prev) ? NULL :cdir->d_next;
1417 cdir = dirsearch_byname(vol, dir, ret.u_name);
1420 if (cdir == NULL && scdir != NULL) {
1422 /* LOG(log_debug, logtype_afpd, "cname: using casediff for %s, (%s = %s)", fullpathname(cdir->d_u_name), cdir->d_m_name, path ); */
1425 if ( cdir == NULL ) {
1427 /* if dir == curdir it always succeed,
1428 even if curdir is deleted.
1429 it's not a pb because it will fail in extenddir
1431 if ( movecwd( vol, dir ) < 0 ) {
1432 /* dir is not valid anymore
1433 we delete dir from the cache and abort.
1435 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1436 afp_errno = AFPERR_NOOBJ;
1439 if (afp_errno == AFPERR_ACCESS)
1441 dir_invalidate(vol, dir);
1444 cdir = extenddir( vol, dir, &ret );
1448 cdir = extenddir( vol, dir, &ret );
1449 } /* if (!extend) */
1451 if ( cdir == NULL ) {
1465 * Move curdir to dir, with a possible chdir()
1467 int movecwd( vol, dir)
1468 const struct vol *vol;
1471 char path[MAXPATHLEN + 1];
1476 if ( dir == curdir ) {
1479 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1480 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1484 p = path + sizeof(path) - 1;
1487 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1490 /* parent directory is deleted */
1491 afp_errno = AFPERR_NOOBJ;
1495 if (p -n -1 < path) {
1496 afp_errno = AFPERR_PARAM;
1503 if ( d != curdir ) {
1504 n = strlen( vol->v_path );
1505 if (p -n -1 < path) {
1506 afp_errno = AFPERR_PARAM;
1511 memcpy( p, vol->v_path, n );
1513 if ( chdir( p ) < 0 ) {
1517 afp_errno = AFPERR_ACCESS;
1520 afp_errno = AFPERR_NOOBJ;
1530 * We can't use unix file's perm to support Apple's inherited protection modes.
1531 * If we aren't the file's owner we can't change its perms when moving it and smb
1532 * nfs,... don't even try.
1534 #define AFP_CHECK_ACCESS
1536 int check_access(char *path, int mode)
1538 #ifdef AFP_CHECK_ACCESS
1546 accessmode(p, &ma, curdir, NULL);
1547 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1549 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1555 /* --------------------- */
1556 int file_access(struct path *path, int mode)
1560 accessmode(path->u_name, &ma, curdir, &path->st);
1561 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1563 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1569 /* --------------------- */
1570 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1572 dir->offcnt = count;
1573 dir->ctime = st->st_ctime;
1574 dir->d_flags &= ~DIRF_CNID;
1577 /* ---------------------
1578 * is our cached offspring count valid?
1581 int diroffcnt(struct dir *dir, struct stat *st)
1583 return st->st_ctime == dir->ctime;
1586 /* ---------------------
1587 * is our cached also for reenumerate id?
1590 int dirreenumerate(struct dir *dir, struct stat *st)
1592 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1595 /* ------------------------------
1597 (name, dir) with curdir:name == dir, from afp_enumerate
1600 int getdirparams(const struct vol *vol,
1601 u_int16_t bitmap, struct path *s_path,
1603 char *buf, int *buflen )
1607 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1608 int bit = 0, isad = 0;
1614 struct stat *st = &s_path->st;
1615 char *upath = s_path->u_name;
1617 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1618 (1 << DIRPBIT_CDATE) |
1619 (1 << DIRPBIT_MDATE) |
1620 (1 << DIRPBIT_BDATE) |
1621 (1 << DIRPBIT_FINFO)))) {
1623 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1624 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1629 if ( dir->d_did == DIRDID_ROOT) {
1630 pdid = DIRDID_ROOT_PARENT;
1631 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1634 pdid = dir->d_parent->d_did;
1638 while ( bitmap != 0 ) {
1639 while (( bitmap & 1 ) == 0 ) {
1647 ad_getattr(&ad, &ashort);
1648 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1649 && strcmp(dir->d_u_name, "..")) {
1650 ashort = htons(ATTRBIT_INVISIBLE);
1653 ashort |= htons(ATTRBIT_SHARED);
1654 memcpy( data, &ashort, sizeof( ashort ));
1655 data += sizeof( ashort );
1659 memcpy( data, &pdid, sizeof( pdid ));
1660 data += sizeof( pdid );
1663 case DIRPBIT_CDATE :
1664 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1665 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1666 memcpy( data, &aint, sizeof( aint ));
1667 data += sizeof( aint );
1670 case DIRPBIT_MDATE :
1671 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1672 memcpy( data, &aint, sizeof( aint ));
1673 data += sizeof( aint );
1676 case DIRPBIT_BDATE :
1677 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1678 aint = AD_DATE_START;
1679 memcpy( data, &aint, sizeof( aint ));
1680 data += sizeof( aint );
1683 case DIRPBIT_FINFO :
1685 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1686 } else { /* no appledouble */
1687 memset( data, 0, 32 );
1688 /* set default view -- this also gets done in ad_open() */
1689 ashort = htons(FINDERINFO_CLOSEDVIEW);
1690 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1692 /* dot files are by default invisible */
1693 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1694 strcmp(dir->d_u_name , "..")) {
1695 ashort = htons(FINDERINFO_INVISIBLE);
1696 memcpy(data + FINDERINFO_FRFLAGOFF,
1697 &ashort, sizeof(ashort));
1703 case DIRPBIT_LNAME :
1704 if (dir->d_m_name) /* root of parent can have a null name */
1707 memset(data, 0, sizeof(u_int16_t));
1708 data += sizeof( u_int16_t );
1711 case DIRPBIT_SNAME :
1712 memset(data, 0, sizeof(u_int16_t));
1713 data += sizeof( u_int16_t );
1717 memcpy( data, &dir->d_did, sizeof( aint ));
1718 data += sizeof( aint );
1721 case DIRPBIT_OFFCNT :
1723 /* this needs to handle current directory access rights */
1724 if (diroffcnt(dir, st)) {
1725 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1727 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1728 setdiroffcnt(dir, st, ret);
1729 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1731 ashort = htons( ashort );
1732 memcpy( data, &ashort, sizeof( ashort ));
1733 data += sizeof( ashort );
1737 aint = htonl(st->st_uid);
1738 memcpy( data, &aint, sizeof( aint ));
1739 data += sizeof( aint );
1743 aint = htonl(st->st_gid);
1744 memcpy( data, &aint, sizeof( aint ));
1745 data += sizeof( aint );
1748 case DIRPBIT_ACCESS :
1749 accessmode( upath, &ma, dir , st);
1751 *data++ = ma.ma_user;
1752 *data++ = ma.ma_world;
1753 *data++ = ma.ma_group;
1754 *data++ = ma.ma_owner;
1757 /* Client has requested the ProDOS information block.
1758 Just pass back the same basic block for all
1759 directories. <shirsch@ibm.net> */
1760 case DIRPBIT_PDINFO :
1761 if (afp_version >= 30) { /* UTF8 name */
1762 utf8 = kTextEncodingUTF8;
1763 if (dir->d_m_name) /* root of parent can have a null name */
1766 memset(data, 0, sizeof(u_int16_t));
1767 data += sizeof( u_int16_t );
1769 memcpy(data, &aint, sizeof( aint ));
1770 data += sizeof( aint );
1772 else { /* ProDOS Info Block */
1775 ashort = htons( 0x0200 );
1776 memcpy( data, &ashort, sizeof( ashort ));
1777 data += sizeof( ashort );
1778 memset( data, 0, sizeof( ashort ));
1779 data += sizeof( ashort );
1783 case DIRPBIT_UNIXPR :
1784 aint = htonl(st->st_uid);
1785 memcpy( data, &aint, sizeof( aint ));
1786 data += sizeof( aint );
1787 aint = htonl(st->st_gid);
1788 memcpy( data, &aint, sizeof( aint ));
1789 data += sizeof( aint );
1792 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1793 memcpy( data, &aint, sizeof( aint ));
1794 data += sizeof( aint );
1796 accessmode( upath, &ma, dir , st);
1798 *data++ = ma.ma_user;
1799 *data++ = ma.ma_world;
1800 *data++ = ma.ma_group;
1801 *data++ = ma.ma_owner;
1806 ad_close( &ad, ADFLAGS_HF );
1808 return( AFPERR_BITMAP );
1814 ashort = htons( data - buf );
1815 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1816 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1818 if ( utf_nameoff ) {
1819 ashort = htons( data - buf );
1820 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1821 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1824 ad_close( &ad, ADFLAGS_HF );
1826 *buflen = data - buf;
1830 /* ----------------------------- */
1831 int path_error(struct path *path, int error)
1833 /* - a dir with access error
1834 * - no error it's a file
1837 if (path_isadir(path))
1839 if (path->st_valid && path->st_errno)
1841 return AFPERR_BADTYPE ;
1844 /* ----------------------------- */
1845 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1847 char *ibuf, *rbuf _U_;
1848 int ibuflen _U_, *rbuflen;
1853 u_int16_t vid, bitmap;
1859 memcpy( &vid, ibuf, sizeof( vid ));
1860 ibuf += sizeof( vid );
1862 if (NULL == ( vol = getvolbyvid( vid )) ) {
1863 return( AFPERR_PARAM );
1866 if (vol->v_flags & AFPVOL_RO)
1867 return AFPERR_VLOCK;
1869 memcpy( &did, ibuf, sizeof( did ));
1870 ibuf += sizeof( int );
1872 if (NULL == ( dir = dirlookup( vol, did )) ) {
1876 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1877 bitmap = ntohs( bitmap );
1878 ibuf += sizeof( bitmap );
1880 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1881 return get_afp_errno(AFPERR_NOOBJ);
1884 if ( *path->m_name != '\0' ) {
1885 rc = path_error(path, AFPERR_NOOBJ);
1886 /* maybe we are trying to set perms back */
1887 if (rc != AFPERR_ACCESS)
1892 * If ibuf is odd, make it even.
1894 if ((u_long)ibuf & 1 ) {
1898 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1899 setvoltime(obj, vol );
1905 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1907 * assume path == '\0' eg. it's a directory in canonical form
1910 struct path Cur_Path = {
1913 ".", /* unix name */
1915 NULL,/* struct dir */
1916 0, /* stat is not set */
1919 /* ------------------ */
1920 static int set_dir_errors(struct path *path, const char *where, int err)
1925 return AFPERR_ACCESS;
1927 return AFPERR_VLOCK;
1929 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1930 return AFPERR_PARAM;
1933 /* ------------------ */
1934 int setdirparams(const struct vol *vol,
1935 struct path *path, u_int16_t d_bitmap, char *buf )
1944 int bit, aint, isad = 1;
1947 u_int16_t ashort, bshort;
1949 int change_mdate = 0;
1950 int change_parent_mdate = 0;
1952 u_int16_t bitmap = d_bitmap;
1953 u_char finder_buf[32];
1955 mode_t mpriv; /* uninitialized, OK 310105 */
1956 u_int16_t upriv_bit = 0;
1959 upath = path->u_name;
1961 while ( bitmap != 0 ) {
1962 while (( bitmap & 1 ) == 0 ) {
1970 memcpy( &ashort, buf, sizeof( ashort ));
1971 buf += sizeof( ashort );
1973 case DIRPBIT_CDATE :
1975 memcpy(&cdate, buf, sizeof(cdate));
1976 buf += sizeof( cdate );
1978 case DIRPBIT_MDATE :
1979 memcpy(&newdate, buf, sizeof(newdate));
1980 buf += sizeof( newdate );
1982 case DIRPBIT_BDATE :
1984 memcpy(&bdate, buf, sizeof(bdate));
1985 buf += sizeof( bdate );
1987 case DIRPBIT_FINFO :
1989 memcpy( finder_buf, buf, 32 );
1992 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1993 change_parent_mdate = 1;
1994 memcpy( &owner, buf, sizeof(owner));
1995 buf += sizeof( owner );
1998 change_parent_mdate = 1;
1999 memcpy( &group, buf, sizeof( group ));
2000 buf += sizeof( group );
2002 case DIRPBIT_ACCESS :
2004 change_parent_mdate = 1;
2005 ma.ma_user = *buf++;
2006 ma.ma_world = *buf++;
2007 ma.ma_group = *buf++;
2008 ma.ma_owner = *buf++;
2009 mpriv = mtoumode( &ma );
2010 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2011 err = set_dir_errors(path, "setdirmode", errno);
2015 /* Ignore what the client thinks we should do to the
2016 ProDOS information block. Skip over the data and
2017 report nothing amiss. <shirsch@ibm.net> */
2018 case DIRPBIT_PDINFO :
2019 if (afp_version < 30) {
2023 err = AFPERR_BITMAP;
2027 case DIRPBIT_UNIXPR :
2028 if (vol_unix_priv(vol)) {
2029 /* Skip UID and GID for now, there seems to be no way to set them from an OSX client anyway */
2030 buf += sizeof( aint );
2031 buf += sizeof( aint );
2034 change_parent_mdate = 1;
2035 memcpy( &upriv, buf, sizeof( upriv ));
2036 buf += sizeof( upriv );
2037 upriv = ntohl (upriv);
2038 if (dir_rx_set(upriv)) {
2039 /* maybe we are trying to set perms back */
2040 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
2042 err = set_dir_errors(path, "setdirmode", errno);
2053 err = AFPERR_BITMAP;
2061 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2063 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2064 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2066 * Check to see what we're trying to set. If it's anything
2067 * but ACCESS, UID, or GID, give an error. If it's any of those
2068 * three, we don't need the ad to be open, so just continue.
2070 * note: we also don't need to worry about mdate. also, be quiet
2071 * if we're using the noadouble option.
2073 if (!vol_noadouble(vol) && (d_bitmap &
2074 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
2075 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
2076 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
2077 return AFPERR_ACCESS;
2083 * Check to see if a create was necessary. If it was, we'll want
2084 * to set our name, etc.
2086 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
2087 ad_setname(&ad, curdir->d_m_name);
2093 while ( bitmap != 0 ) {
2094 while (( bitmap & 1 ) == 0 ) {
2102 ad_getattr(&ad, &bshort);
2103 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
2104 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
2105 change_parent_mdate = 1;
2106 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
2107 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
2111 ad_setattr(&ad, bshort);
2114 case DIRPBIT_CDATE :
2116 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2119 case DIRPBIT_MDATE :
2121 case DIRPBIT_BDATE :
2123 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2126 case DIRPBIT_FINFO :
2128 if ( dir->d_did == DIRDID_ROOT ) {
2130 * Alright, we admit it, this is *really* sick!
2131 * The 4 bytes that we don't copy, when we're dealing
2132 * with the root of a volume, are the directory's
2133 * location information. This eliminates that annoying
2134 * behavior one sees when mounting above another mount
2137 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2138 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2140 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2144 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2145 if ( (dir->d_did == DIRDID_ROOT) &&
2146 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2147 err = set_dir_errors(path, "setdeskowner", errno);
2148 if (isad && err == AFPERR_PARAM) {
2149 err = AFP_OK; /* ???*/
2152 goto setdirparam_done;
2155 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2156 err = set_dir_errors(path, "setdirowner", errno);
2157 goto setdirparam_done;
2162 if (dir->d_did == DIRDID_ROOT)
2163 setdeskowner( -1, ntohl(group) );
2165 #if 0 /* don't error if we can't set the desktop owner. */
2166 err = set_dir_errors(path, "setdeskowner", errno);
2167 if (isad && err == AFPERR_PARAM) {
2168 err = AFP_OK; /* ???*/
2171 goto setdirparam_done;
2175 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2176 err = set_dir_errors(path, "setdirowner", errno);
2177 goto setdirparam_done;
2181 case DIRPBIT_ACCESS :
2182 if (dir->d_did == DIRDID_ROOT) {
2184 if (!dir_rx_set(mpriv)) {
2185 /* we can't remove read and search for owner on volume root */
2186 err = AFPERR_ACCESS;
2187 goto setdirparam_done;
2191 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2192 err = set_dir_errors(path, "setdirmode", errno);
2193 goto setdirparam_done;
2196 case DIRPBIT_PDINFO :
2197 if (afp_version >= 30) {
2198 err = AFPERR_BITMAP;
2199 goto setdirparam_done;
2202 case DIRPBIT_UNIXPR :
2203 if (vol_unix_priv(vol)) {
2204 if (dir->d_did == DIRDID_ROOT) {
2205 setdeskmode( upriv );
2206 if (!dir_rx_set(upriv)) {
2207 /* we can't remove read and search for owner on volume root */
2208 err = AFPERR_ACCESS;
2209 goto setdirparam_done;
2213 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2214 err = set_dir_errors(path, "setdirmode", errno);
2215 goto setdirparam_done;
2221 err = AFPERR_BITMAP;
2222 goto setdirparam_done;
2231 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2232 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2236 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2237 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2242 if (path->st_valid && !path->st_errno) {
2243 struct stat *st = &path->st;
2245 if (dir && dir->d_parent) {
2246 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2249 ad_flush( &ad, ADFLAGS_HF );
2250 ad_close( &ad, ADFLAGS_HF );
2253 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2254 && gettimeofday(&tv, NULL) == 0) {
2255 if (!movecwd(vol, dir->d_parent)) {
2256 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2257 /* be careful with bitmap because now dir is null */
2258 bitmap = 1<<DIRPBIT_MDATE;
2259 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2260 /* should we reset curdir ?*/
2267 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2270 int ibuflen _U_, *rbuflen;
2276 struct path *s_path;
2284 memcpy( &vid, ibuf, sizeof( vid ));
2285 ibuf += sizeof( vid );
2286 if (NULL == ( vol = getvolbyvid( vid )) ) {
2287 return( AFPERR_PARAM );
2290 if (vol->v_flags & AFPVOL_RO)
2291 return AFPERR_VLOCK;
2293 memcpy( &did, ibuf, sizeof( did ));
2294 ibuf += sizeof( did );
2295 if (NULL == ( dir = dirlookup( vol, did )) ) {
2296 return afp_errno; /* was AFPERR_NOOBJ */
2298 /* for concurrent access we need to be sure we are not in the
2299 * folder we want to create...
2303 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2304 return get_afp_errno(AFPERR_PARAM);
2306 /* cname was able to move curdir to it! */
2307 if (*s_path->m_name == '\0')
2308 return AFPERR_EXIST;
2310 upath = s_path->u_name;
2311 if (0 != (err = check_name(vol, upath))) {
2315 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2319 if (of_stat(s_path) < 0) {
2323 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2327 if ( movecwd( vol, dir ) < 0 ) {
2328 return( AFPERR_PARAM );
2331 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2332 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
2333 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
2334 if (vol_noadouble(vol))
2335 goto createdir_done;
2336 return( AFPERR_ACCESS );
2338 ad_setname(&ad, s_path->m_name);
2339 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2341 ad_flush( &ad, ADFLAGS_HF );
2342 ad_close( &ad, ADFLAGS_HF );
2345 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2346 *rbuflen = sizeof( u_int32_t );
2347 setvoltime(obj, vol );
2352 * dst new unix filename (not a pathname)
2353 * newname new mac name
2357 int renamedir(vol, src, dst, dir, newparent, newname)
2358 const struct vol *vol;
2359 char *src, *dst, *newname;
2360 struct dir *dir, *newparent;
2367 /* existence check moved to afp_moveandrename */
2368 if ( unix_rename( src, dst ) < 0 ) {
2371 return( AFPERR_NOOBJ );
2373 return( AFPERR_ACCESS );
2375 return AFPERR_VLOCK;
2377 /* tried to move directory into a subdirectory of itself */
2378 return AFPERR_CANTMOVE;
2380 /* this needs to copy and delete. bleah. that means we have
2381 * to deal with entire directory hierarchies. */
2382 if ((err = copydir(vol, src, dst)) < 0) {
2386 if ((err = deletedir(src)) < 0)
2390 return( AFPERR_PARAM );
2394 vol->vfs->rf_renamedir(vol, src, dst);
2396 len = strlen( newname );
2397 /* rename() succeeded so we need to update our tree even if we can't open
2401 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2403 if (!ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2404 ad_setname(&ad, newname);
2405 ad_flush( &ad, ADFLAGS_HF );
2406 ad_close( &ad, ADFLAGS_HF );
2409 dir_hash_del(vol, dir);
2410 if (dir->d_m_name == dir->d_u_name)
2411 dir->d_u_name = NULL;
2413 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2414 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2415 /* FIXME : fatal ? */
2418 dir->d_m_name = buf;
2419 strcpy( dir->d_m_name, newname );
2421 if (newname == dst) {
2422 free(dir->d_u_name);
2423 dir->d_u_name = dir->d_m_name;
2426 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2427 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2430 dir->d_u_name = buf;
2431 strcpy( dir->d_u_name, dst );
2434 if (dir->d_m_name_ucs2)
2435 free(dir->d_m_name_ucs2);
2437 dir->d_m_name_ucs2 = NULL;
2438 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, strlen(dir->d_m_name), (char**)&dir->d_m_name_ucs2))
2439 dir->d_m_name_ucs2 = NULL;
2441 if (( parent = dir->d_parent ) == NULL ) {
2444 if ( parent == newparent ) {
2445 hash_alloc_insert(vol->v_hash, dir, dir);
2449 /* detach from old parent and add to new one. */
2450 dirchildremove(parent, dir);
2451 dir->d_parent = newparent;
2452 dirchildadd(vol, newparent, dir);
2456 /* delete an empty directory */
2457 int deletecurdir( vol)
2458 const struct vol *vol;
2468 if ( curdir->d_parent == NULL ) {
2469 return( AFPERR_ACCESS );
2474 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2475 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2477 ad_getattr(&ad, &ashort);
2478 ad_close( &ad, ADFLAGS_HF );
2479 if ((ashort & htons(ATTRBIT_NODELETE))) {
2480 return AFPERR_OLOCK;
2483 err = vol->vfs->rf_deletecurdir(vol);
2488 /* now get rid of dangling symlinks */
2489 if ((dp = opendir("."))) {
2490 while ((de = readdir(dp))) {
2491 /* skip this and previous directory */
2492 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2495 /* bail if it's not a symlink */
2496 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2498 return AFPERR_DIRNEMPT;
2501 if ((err = netatalk_unlink(de->d_name))) {
2508 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2513 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2514 dirchildremove(curdir, fdir);
2515 cnid_delete(vol->v_cdb, fdir->d_did);
2516 dir_remove( vol, fdir );
2521 /* inode is used as key for cnid.
2522 * Close the descriptor only after cnid_delete
2530 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2533 int ibuflen _U_, *rbuflen;
2543 sfunc = (unsigned char) *ibuf++;
2544 memcpy( &id, ibuf, sizeof( id ));
2549 if (sfunc == 3 || sfunc == 4) {
2550 if (afp_version < 30) {
2551 return( AFPERR_PARAM );
2558 case 3 :/* unicode */
2559 if (( pw = getpwuid( id )) == NULL ) {
2560 return( AFPERR_NOITEM );
2562 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2563 pw->pw_name, strlen(pw->pw_name), &name);
2567 case 4 : /* unicode */
2568 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2569 return( AFPERR_NOITEM );
2571 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2572 gr->gr_name, strlen(gr->gr_name), &name);
2576 return( AFPERR_PARAM );
2578 len = strlen( name );
2585 u_int16_t tp = htons(len);
2586 memcpy(rbuf, &tp, sizeof(tp));
2595 memcpy( rbuf, name, len );
2603 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2606 int ibuflen _U_, *rbuflen;
2615 sfunc = (unsigned char) *ibuf++;
2619 case 2 : /* unicode */
2620 if (afp_version < 30) {
2621 return( AFPERR_PARAM );
2623 memcpy(&ulen, ibuf, sizeof(ulen));
2629 len = (unsigned char) *ibuf++;
2632 return( AFPERR_PARAM );
2639 case 1 : /* unicode */
2641 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2642 return( AFPERR_NOITEM );
2647 case 2 : /* unicode */
2649 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2650 return( AFPERR_NOITEM );
2659 memcpy( rbuf, &id, sizeof( id ));
2660 *rbuflen = sizeof( id );
2664 /* ------------------------------------
2665 variable DID support
2667 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2669 char *ibuf _U_, *rbuf _U_;
2670 int ibuflen _U_, *rbuflen;
2681 /* do nothing as dids are static for the life of the process. */
2685 memcpy(&vid, ibuf, sizeof( vid ));
2686 ibuf += sizeof( vid );
2687 if (( vol = getvolbyvid( vid )) == NULL ) {
2688 return( AFPERR_PARAM );
2691 memcpy( &did, ibuf, sizeof( did ));
2692 ibuf += sizeof( did );
2693 if (( dir = dirlookup( vol, did )) == NULL ) {
2694 return( AFPERR_PARAM );
2697 /* dir_remove -- deletedid */
2703 /* did creation gets done automatically
2704 * there's a pb again with case but move it to cname
2706 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2709 int ibuflen _U_, *rbuflen;
2712 struct dir *parentdir;
2720 memcpy(&vid, ibuf, sizeof(vid));
2721 ibuf += sizeof( vid );
2723 if (NULL == ( vol = getvolbyvid( vid )) ) {
2724 return( AFPERR_PARAM );
2727 memcpy(&did, ibuf, sizeof(did));
2728 ibuf += sizeof(did);
2730 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2734 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2735 return get_afp_errno(AFPERR_PARAM);
2738 if ( *path->m_name != '\0' ) {
2739 return path_error(path, AFPERR_NOOBJ);
2742 if ( !path->st_valid && of_stat(path ) < 0 ) {
2743 return( AFPERR_NOOBJ );
2745 if ( path->st_errno ) {
2746 return( AFPERR_NOOBJ );
2749 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2750 *rbuflen = sizeof(curdir->d_did);