2 * $Id: directory.c,v 1.82 2005-09-28 09:43:04 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
7 * 19 jan 2000 implemented red-black trees for directory lookups
13 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
38 #include <sys/param.h>
41 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/cnid.h>
46 #include <atalk/logger.h>
48 #include "directory.h"
61 #define SENTINEL (&sentinel)
62 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
63 NULL, NULL, NULL, NULL, NULL, 0, 0,
64 0, 0, NULL, NULL, NULL};
65 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
66 NULL, NULL, NULL, NULL, NULL, 0, 0,
67 0, 0, NULL, NULL, NULL};
69 /* (from IM: Toolbox Essentials)
70 * dirFinderInfo (DInfo) fields:
72 * frRect 8 folder's window rectangle
74 * frLocation 4 folder's location in window
75 * frView 2 folder's view (default == closedView (256))
77 * extended dirFinderInfo (DXInfo) fields:
78 * frScroll 4 scroll position
79 * frOpenChain: 4 directory ID chain of open folders
80 * frScript: 1 script flag and code
81 * frXFlags: 1 reserved
82 * frComment: 2 comment ID
83 * frPutAway: 4 home directory ID
87 * redid did assignment for directories. now we use red-black trees.
92 const struct vol *vol;
100 afp_errno = AFPERR_PARAM;
103 if ( did == DIRDID_ROOT_PARENT ) {
105 rootpar.d_did = DIRDID_ROOT_PARENT;
106 rootpar.d_child = vol->v_dir;
110 /* XXX would be nice to check against curdir but we need to keep its volume */
111 if (vol->curdir && curdir->d_did == did) {
120 afp_errno = AFPERR_NOOBJ;
121 while ( dir != SENTINEL ) {
122 if (dir->d_did == did)
123 return dir->d_m_name ? dir : NULL;
124 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
129 /* ------------------- */
131 int path_isadir(struct path *o_path)
133 return o_path->d_dir != NULL;
135 return o_path->m_name == '\0' || /* we are in a it */
136 !o_path->st_valid || /* in cache but we can't chdir in it */
137 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
142 int get_afp_errno(const int param)
144 if (afp_errno != AFPERR_DID1)
149 /* ------------------- */
151 dirsearch_byname( const struct vol *vol, struct dir *cdir, char *name)
153 struct dir *dir = NULL;
155 if ((cdir->d_did != DIRDID_ROOT_PARENT) && (cdir->d_child)) {
161 hn = hash_lookup(vol->v_hash, &key);
169 /* -----------------------------------------
170 * if did is not in the cache resolve it with cnid
173 * OSX call it with bogus id, ie file ID not folder ID,
174 * and we are really bad in this case.
177 dirlookup( vol, did )
178 const struct vol *vol;
184 static char path[MAXPATHLEN + 1];
187 static char buffer[12 + MAXPATHLEN + 1];
188 int buflen = 12 + MAXPATHLEN + 1;
193 ret = dirsearch(vol, did);
194 if (ret != NULL || afp_errno == AFPERR_PARAM)
197 utf8 = utf8_encoding();
198 maxpath = (utf8)?MAXPATHLEN -7:255;
200 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
201 afp_errno = AFPERR_NOOBJ;
204 ptr = path + MAXPATHLEN;
205 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
206 afp_errno = AFPERR_NOOBJ;
210 pathlen = len; /* no 0 in the last part */
212 strcpy(ptr - len, mpath);
215 ret = dirsearch(vol,id);
220 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
222 NULL == (mpath = utompath(vol, upath, cnid, utf8))
224 afp_errno = AFPERR_NOOBJ;
228 len = strlen(mpath) + 1;
230 if (pathlen > maxpath) {
231 afp_errno = AFPERR_PARAM;
234 strcpy(ptr - len, mpath);
238 /* fill the cache, another place where we know about the path type */
244 temp16 = htons(pathlen);
245 memcpy(ptr, &temp16, sizeof(temp16));
247 temp = htonl(kTextEncodingUTF8);
249 memcpy(ptr, &temp, sizeof(temp));
255 *ptr = (unsigned char)pathlen;
259 /* cname is not efficient */
260 if (cname( vol, ret, &ptr ) == NULL )
263 return dirsearch(vol, did);
266 /* child addition/removal */
267 static void dirchildadd(const struct vol *vol, struct dir *a, struct dir *b)
272 b->d_next = a->d_child;
273 b->d_prev = b->d_next->d_prev;
274 b->d_next->d_prev = b;
275 b->d_prev->d_next = b;
277 if (!hash_alloc_insert(vol->v_hash, b, b)) {
278 LOG(log_error, logtype_afpd, "dirchildadd: can't hash %s", b->d_u_name);
282 static void dirchildremove(struct dir *a,struct dir *b)
285 a->d_child = (b == b->d_next) ? NULL : b->d_next;
286 b->d_next->d_prev = b->d_prev;
287 b->d_prev->d_next = b->d_next;
288 b->d_next = b->d_prev = b;
291 /* --------------------------- */
292 /* rotate the tree to the left */
293 static void dir_leftrotate(vol, dir)
297 struct dir *right = dir->d_right;
299 /* whee. move the right's left tree into dir's right tree */
300 dir->d_right = right->d_left;
301 if (right->d_left != SENTINEL)
302 right->d_left->d_back = dir;
304 if (right != SENTINEL) {
305 right->d_back = dir->d_back;
309 if (!dir->d_back) /* no parent. move the right tree to the top. */
311 else if (dir == dir->d_back->d_left) /* we were on the left */
312 dir->d_back->d_left = right;
314 dir->d_back->d_right = right; /* we were on the right */
316 /* re-insert dir on the left tree */
323 /* rotate the tree to the right */
324 static void dir_rightrotate(vol, dir)
328 struct dir *left = dir->d_left;
330 /* whee. move the left's right tree into dir's left tree */
331 dir->d_left = left->d_right;
332 if (left->d_right != SENTINEL)
333 left->d_right->d_back = dir;
335 if (left != SENTINEL) {
336 left->d_back = dir->d_back;
340 if (!dir->d_back) /* no parent. move the left tree to the top. */
342 else if (dir == dir->d_back->d_right) /* we were on the right */
343 dir->d_back->d_right = left;
345 dir->d_back->d_left = left; /* we were on the left */
347 /* re-insert dir on the right tree */
353 /* recolor after a removal */
354 static struct dir *dir_rmrecolor(vol, dir)
360 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
361 /* are we on the left tree? */
362 if (dir == dir->d_back->d_left) {
363 leaf = dir->d_back->d_right; /* get right side */
364 if (leaf->d_color == DIRTREE_COLOR_RED) {
365 /* we're red. we need to change to black. */
366 leaf->d_color = DIRTREE_COLOR_BLACK;
367 dir->d_back->d_color = DIRTREE_COLOR_RED;
368 dir_leftrotate(vol, dir->d_back);
369 leaf = dir->d_back->d_right;
372 /* right leaf has black end nodes */
373 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
374 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
375 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
376 dir = dir->d_back; /* ascend */
378 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
379 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
380 leaf->d_color = DIRTREE_COLOR_RED;
381 dir_rightrotate(vol, leaf);
382 leaf = dir->d_back->d_right;
384 leaf->d_color = dir->d_back->d_color;
385 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
386 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
387 dir_leftrotate(vol, dir->d_back);
390 } else { /* right tree */
391 leaf = dir->d_back->d_left; /* left tree */
392 if (leaf->d_color == DIRTREE_COLOR_RED) {
393 leaf->d_color = DIRTREE_COLOR_BLACK;
394 dir->d_back->d_color = DIRTREE_COLOR_RED;
395 dir_rightrotate(vol, dir->d_back);
396 leaf = dir->d_back->d_left;
399 /* left leaf has black end nodes */
400 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
401 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
402 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
403 dir = dir->d_back; /* ascend */
405 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
406 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
407 leaf->d_color = DIRTREE_COLOR_RED;
408 dir_leftrotate(vol, leaf);
409 leaf = dir->d_back->d_left;
411 leaf->d_color = dir->d_back->d_color;
412 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
413 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
414 dir_rightrotate(vol, dir->d_back);
419 dir->d_color = DIRTREE_COLOR_BLACK;
425 /* --------------------- */
426 static void dir_hash_del(const struct vol *vol, struct dir *dir)
430 hn = hash_lookup(vol->v_hash, dir);
432 LOG(log_error, logtype_afpd, "dir_hash_del: %s not hashed", dir->d_u_name);
435 hash_delete(vol->v_hash, hn);
439 /* remove the node from the tree. this is just like insertion, but
440 * different. actually, it has to worry about a bunch of things that
441 * insertion doesn't care about. */
442 static void dir_remove( vol, dir )
447 struct ofork *of, *last;
448 struct dir *node, *leaf;
449 #endif /* REMOVE_NODES */
451 if (!dir || (dir == SENTINEL))
454 /* i'm not sure if it really helps to delete stuff. */
455 dir_hash_del(vol, dir);
458 dir->d_m_name = NULL;
459 dir->d_u_name = NULL;
460 dir->d_m_name_ucs2 = NULL;
461 #else /* ! REMOVE_NODES */
463 /* go searching for a node with at most one child */
464 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
468 while (node->d_left != SENTINEL)
473 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
476 leaf->d_back = node->d_back;
479 } else if (node == node->d_back->d_left) { /* left tree */
480 node->d_back->d_left = leaf;
482 node->d_back->d_right = leaf;
485 /* we want to free node, but we also want to free the data in dir.
486 * currently, that's d_name and the directory traversal bits.
487 * we just copy the necessary bits and then fix up all the
488 * various pointers to the directory. needless to say, there are
489 * a bunch of places that store the directory struct. */
491 struct dir save, *tmp;
493 memcpy(&save, dir, sizeof(save));
494 memcpy(dir, node, sizeof(struct dir));
496 /* restore the red-black bits */
497 dir->d_left = save.d_left;
498 dir->d_right = save.d_right;
499 dir->d_back = save.d_back;
500 dir->d_color = save.d_color;
502 if (node == vol->v_dir) {/* we may need to fix up this pointer */
504 rootpar.d_child = vol->v_dir;
506 /* if we aren't the root directory, we have parents and
507 * siblings to worry about */
508 if (dir->d_parent->d_child == node)
509 dir->d_parent->d_child = dir;
510 dir->d_next->d_prev = dir;
511 dir->d_prev->d_next = dir;
514 /* fix up children. */
518 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
521 if (node == curdir) /* another pointer to fixup */
524 /* we also need to fix up oforks. bleah */
525 if ((of = dir->d_ofork)) {
526 last = of->of_d_prev;
529 of = (last == of) ? NULL : of->of_d_next;
533 /* set the node's d_name */
534 node->d_m_name = save.d_m_name;
535 node->d_u_name = save.d_u_name;
536 node->d_m_name_ucs2 = save.d_m_name_ucs2;
539 if (node->d_color == DIRTREE_COLOR_BLACK)
540 dir_rmrecolor(vol, leaf);
542 if (node->d_m_name_ucs2)
543 free(node->d_u_name_ucs2);
544 if (node->d_u_name != node->d_m_name) {
545 free(node->d_u_name);
547 free(node->d_m_name);
549 #endif /* ! REMOVE_NODES */
552 /* ---------------------------------------
553 * remove the node and its childs from the tree
555 * FIXME what about opened forks with refs to it?
556 * it's an afp specs violation because you can't delete
557 * an opened forks. Now afpd doesn't care about forks opened by other
558 * process. It's fixable within afpd if fnctl_lock, doable with smb and
559 * next to impossible for nfs and local filesystem access.
561 static void dir_invalidate( vol, dir )
562 const struct vol *vol;
566 /* v_root can't be deleted */
567 if (movecwd(vol, vol->v_root) < 0) {
568 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
572 dirchildremove(dir->d_parent, dir);
573 dir_remove( vol, dir );
576 /* ------------------------------------ */
577 static struct dir *dir_insert(vol, dir)
578 const struct vol *vol;
584 while (pdir->d_did != dir->d_did ) {
585 if ( pdir->d_did > dir->d_did ) {
586 if ( pdir->d_left == SENTINEL ) {
593 if ( pdir->d_right == SENTINEL ) {
598 pdir = pdir->d_right;
604 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
607 caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
612 static u_int32_t did = 0;
613 static char cname[MAXPATHLEN];
614 static char lname[MAXPATHLEN];
615 ucs2_t u2_path[MAXPATHLEN];
616 ucs2_t u2_dename[MAXPATHLEN];
617 char *tmp, *savepath;
619 if (veto_file(ENUMVETO, path->u_name))
622 savepath = path->u_name;
624 /* very simple cache */
625 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
626 path->u_name = cname;
628 if (of_stat( path ) == 0 ) {
631 /* something changed, we cannot stat ... */
635 if (NULL == ( dp = opendir( "." )) ) {
636 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
641 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
642 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, strlen(path->u_name), u2_path, sizeof(u2_path)) )
643 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
645 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
647 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
648 if (NULL == check_dirent(vol, de->d_name))
651 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, strlen(de->d_name), u2_dename, sizeof(u2_dename)) )
654 if (strcasecmp_w( u2_path, u2_dename) == 0) {
656 strlcpy(cname, de->d_name, sizeof(cname));
657 path->u_name = cname;
659 if (of_stat( path ) == 0 ) {
660 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
661 strlcpy(lname, tmp, sizeof(lname));
674 /* invalidate cache */
675 memset(cname, 0, sizeof(cname));
677 path->u_name = savepath;
679 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
685 * attempt to extend the current dir. tree to include path
686 * as a side-effect, movecwd to that point and return the new dir
689 extenddir( vol, dir, path )
696 if ( path->u_name == NULL) {
697 afp_errno = AFPERR_PARAM;
700 if (of_stat( path ) != 0 ) {
701 if (!(vol->v_flags & AFPVOL_CASEINSEN))
703 else if(caseenumerate(vol, path, dir) != 0)
707 if (!S_ISDIR(path->st.st_mode)) {
711 /* mac name is always with the right encoding (from cname()) */
712 if (( dir = adddir( vol, dir, path)) == NULL ) {
717 if ( movecwd( vol, dir ) < 0 ) {
724 /* -------------------
725 system rmdir with afp error code.
726 ENOENT is not an error.
728 int netatalk_rmdir(const char *name)
730 if (rmdir(name) < 0) {
735 return AFPERR_DIRNEMPT;
738 return AFPERR_ACCESS;
748 /* -------------------------
749 appledouble mkdir afp error code.
751 static int netatalk_mkdir(const char *name)
753 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
756 return( AFPERR_NOOBJ );
758 return( AFPERR_VLOCK );
761 return( AFPERR_ACCESS );
763 return( AFPERR_EXIST );
766 return( AFPERR_DFULL );
768 return( AFPERR_PARAM );
774 /* -------------------
775 system unlink with afp error code.
776 ENOENT is not an error.
778 int netatalk_unlink(const char *name)
780 if (unlink(name) < 0) {
788 return AFPERR_ACCESS;
796 /* ------------------- */
797 static int deletedir(char *dir)
799 char path[MAXPATHLEN + 1];
807 if ((len = strlen(dir)) +2 > sizeof(path))
811 if ((dp = opendir(dir)) == NULL)
817 remain = sizeof(path) -len -1;
818 while ((de = readdir(dp)) && err == AFP_OK) {
819 /* skip this and previous directory */
820 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
823 if (strlen(de->d_name) > remain) {
827 strcpy(path + len, de->d_name);
828 if (stat(path, &st)) {
831 if (S_ISDIR(st.st_mode)) {
832 err = deletedir(path);
834 err = netatalk_unlink(path);
839 /* okay. the directory is empty. delete it. note: we already got rid
842 err = netatalk_rmdir(dir);
847 /* do a recursive copy. */
848 static int copydir(const struct vol *vol, char *src, char *dst)
850 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
859 /* doesn't exist or the path is too long. */
860 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
861 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
862 ((dp = opendir(src)) == NULL))
865 /* try to create the destination directory */
866 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
871 /* set things up to copy */
875 srem = sizeof(spath) - slen -1;
880 drem = sizeof(dpath) - dlen -1;
883 while ((de = readdir(dp))) {
884 /* skip this and previous directory */
885 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
888 if (strlen(de->d_name) > srem) {
892 strcpy(spath + slen, de->d_name);
894 if (stat(spath, &st) == 0) {
895 if (strlen(de->d_name) > drem) {
899 strcpy(dpath + dlen, de->d_name);
901 if (S_ISDIR(st.st_mode)) {
902 if (AFP_OK != (err = copydir(vol, spath, dpath)))
904 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
908 /* keep the same time stamp. */
909 ut.actime = ut.modtime = st.st_mtime;
915 /* keep the same time stamp. */
916 if (stat(src, &st) == 0) {
917 ut.actime = ut.modtime = st.st_mtime;
927 /* --- public functions follow --- */
929 /* NOTE: we start off with at least one node (the root directory). */
930 static struct dir *dirinsert( vol, dir )
936 if ((node = dir_insert(vol, dir)))
939 /* recolor the tree. the current node is red. */
940 dir->d_color = DIRTREE_COLOR_RED;
942 /* parent of this node has to be black. if the parent node
943 * is red, then we have a grandparent. */
944 while ((dir != vol->v_root) &&
945 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
946 /* are we on the left tree? */
947 if (dir->d_back == dir->d_back->d_back->d_left) {
948 node = dir->d_back->d_back->d_right; /* get the right node */
949 if (node->d_color == DIRTREE_COLOR_RED) {
950 /* we're red. we need to change to black. */
951 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
952 node->d_color = DIRTREE_COLOR_BLACK;
953 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
954 dir = dir->d_back->d_back; /* finished. go up. */
956 if (dir == dir->d_back->d_right) {
958 dir_leftrotate(vol, dir);
960 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
961 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
962 dir_rightrotate(vol, dir->d_back->d_back);
965 node = dir->d_back->d_back->d_left;
966 if (node->d_color == DIRTREE_COLOR_RED) {
967 /* we're red. we need to change to black. */
968 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
969 node->d_color = DIRTREE_COLOR_BLACK;
970 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
971 dir = dir->d_back->d_back; /* finished. ascend */
973 if (dir == dir->d_back->d_left) {
975 dir_rightrotate(vol, dir);
977 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
978 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
979 dir_leftrotate(vol, dir->d_back->d_back);
984 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
988 /* ---------------------------- */
990 adddir( vol, dir, path)
995 struct dir *cdir, *edir;
1003 upath = path->u_name;
1005 upathlen = strlen(upath);
1007 id = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
1011 if (!path->m_name && !(path->m_name = utompath(vol, upath, id , utf8_encoding()))) {
1014 name = path->m_name;
1015 if ((cdir = dirnew(name, upath)) == NULL) {
1016 LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
1019 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)) {
1020 LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
1021 cdir->d_m_name_ucs2 = NULL;
1026 if ((edir = dirinsert( vol, cdir ))) {
1027 /* it's not possible with LASTDID
1029 - someone else have moved the directory.
1030 - it's a symlink inside the share.
1031 - it's an ID reused, the old directory was deleted but not
1032 the cnid record and the server've reused the inode for
1034 for HASH (we should get ride of HASH)
1035 - someone else have moved the directory.
1036 - it's an ID reused as above
1037 - it's a hash duplicate and we are in big trouble
1039 deleted = (edir->d_m_name == NULL);
1041 dir_hash_del(vol, edir);
1043 edir->d_m_name = cdir->d_m_name;
1044 edir->d_u_name = cdir->d_u_name;
1045 edir->d_m_name_ucs2 = cdir->d_m_name_ucs2;
1048 LOG(log_error, logtype_afpd, "adddir: insert %s", edir->d_m_name);
1049 if (!cdir->d_parent || (cdir->d_parent == dir && !deleted)) {
1050 hash_alloc_insert(vol->v_hash, cdir, cdir);
1053 /* the old was not in the same folder */
1055 dirchildremove(cdir->d_parent, cdir);
1058 /* parent/child directories */
1059 cdir->d_parent = dir;
1060 dirchildadd(vol, dir, cdir);
1064 /* --- public functions follow --- */
1065 /* free everything down. we don't bother to recolor as this is only
1066 * called to free the entire tree */
1067 void dirfreename(struct dir *dir)
1069 if (dir->d_u_name != dir->d_m_name) {
1070 free(dir->d_u_name);
1072 if (dir->d_m_name_ucs2)
1073 free(dir->d_m_name_ucs2);
1074 free(dir->d_m_name);
1080 if (!dir || (dir == SENTINEL))
1083 if ( dir->d_left != SENTINEL ) {
1084 dirfree( dir->d_left );
1086 if ( dir->d_right != SENTINEL ) {
1087 dirfree( dir->d_right );
1090 if (dir != SENTINEL) {
1096 /* --------------------------------------------
1097 * most of the time mac name and unix name are the same
1099 struct dir *dirnew(const char *m_name, const char *u_name)
1103 dir = (struct dir *) calloc(1, sizeof( struct dir ));
1107 if ((dir->d_m_name = strdup(m_name)) == NULL) {
1112 if (m_name == u_name || !strcmp(m_name, u_name)) {
1113 dir->d_u_name = dir->d_m_name;
1115 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
1116 free(dir->d_m_name);
1121 dir->d_m_name_ucs2 = NULL;
1122 dir->d_left = dir->d_right = SENTINEL;
1123 dir->d_next = dir->d_prev = dir;
1127 /* ------------------ */
1128 static hash_val_t hash_fun_dir(const void *key)
1130 const struct dir *k = key;
1132 static unsigned long randbox[] = {
1133 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
1134 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
1135 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
1136 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
1139 const unsigned char *str = k->d_u_name;
1143 acc ^= randbox[(*str + acc) & 0xf];
1144 acc = (acc << 1) | (acc >> 31);
1146 acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
1147 acc = (acc << 2) | (acc >> 30);
1153 /* ---------------- */
1154 static int hash_comp_dir(const void *key1, const void *key2)
1156 const struct dir *k1 = key1;
1157 const struct dir *k2 = key2;
1159 return !(k1->d_parent->d_did == k2->d_parent->d_did && !strcmp(k1->d_u_name, k2->d_u_name));
1162 /* ---------------- */
1166 return hash_create(HASHCOUNT_T_MAX, hash_comp_dir, hash_fun_dir);
1169 /* ------------------ */
1170 static struct path *invalidate (const struct vol *vol, struct dir *dir, struct path *ret)
1173 movecwd failed some of dir path are not there anymore.
1174 FIXME Is it true with other errors?
1175 so we remove dir from the cache
1177 if (dir->d_did == DIRDID_ROOT_PARENT)
1179 if (afp_errno == AFPERR_ACCESS) {
1180 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1183 /* FIXME should we set these?, don't need to call stat() after:
1185 ret->st_errno = EACCES;
1187 ret->m_name = dir->d_m_name;
1188 ret->u_name = dir->d_u_name;
1191 } else if (afp_errno == AFPERR_NOOBJ) {
1192 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1195 strcpy(ret->m_name, dir->d_m_name);
1196 if (dir->d_m_name == dir->d_u_name) {
1197 ret->u_name = ret->m_name;
1200 size_t tp = strlen(ret->m_name)+1;
1202 ret->u_name = ret->m_name +tp;
1203 strcpy(ret->u_name, dir->d_u_name);
1205 /* FIXME should we set :
1207 ret->st_errno = ENOENT;
1209 dir_invalidate(vol, dir);
1212 dir_invalidate(vol, dir);
1216 /* -------------------------------------------------- */
1222 stat the file or errno
1225 curdir: filename parent directory
1231 stat the dir or errno
1235 curdir: dir parent directory
1243 curdir: dir parent directory
1250 cname( vol, dir, cpath )
1251 const struct vol *vol;
1255 struct dir *cdir, *scdir=NULL;
1256 static char path[ MAXPATHLEN + 1];
1257 static struct path ret;
1269 afp_errno = AFPERR_NOOBJ;
1270 memset(&ret, 0, sizeof(ret));
1271 switch (ret.m_type = *data) { /* path type */
1274 len = (unsigned char) *data++;
1277 if (afp_version >= 30) {
1283 if (afp_version >= 30) {
1285 memcpy(&hint, data, sizeof(hint));
1287 data += sizeof(hint);
1289 memcpy(&len16, data, sizeof(len16));
1296 /* else it's an error */
1298 afp_errno = AFPERR_PARAM;
1301 *cpath += len + size;
1306 if (movecwd( vol, dir ) < 0 ) {
1307 return invalidate(vol, dir, &ret );
1309 if (*path == '\0') {
1316 if (*data == sep ) {
1320 while (*data == sep && len > 0 ) {
1321 if ( dir->d_parent == NULL ) {
1324 dir = dir->d_parent;
1329 /* would this be faster with strlen + strncpy? */
1331 while ( *data != sep && len > 0 ) {
1333 if (p > &path[ MAXPATHLEN]) {
1334 afp_errno = AFPERR_PARAM;
1340 /* short cut bits by chopping off a trailing \0. this also
1341 makes the traversal happy w/ filenames at the end of the
1348 if ( p == path ) { /* end of the name parameter */
1352 if (afp_version >= 30) {
1357 static char temp[ MAXPATHLEN + 1];
1359 /* not an UTF8 name */
1360 if (mtoUTF8(vol, path, strlen(path), temp, MAXPATHLEN) == (size_t)-1) {
1361 afp_errno = AFPERR_PARAM;
1366 /* check for OS X mangled filename :( */
1368 t = demangle_osx(vol, path, dir->d_did, &fileid);
1371 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
1372 * flags weren't the same
1374 if ( (t = utompath(vol, ret.u_name, fileid, utf8_encoding())) ) {
1375 /* at last got our view of mac name */
1380 if (ret.u_name == NULL) {
1381 if (!(ret.u_name = mtoupath(vol, ret.m_name, dir->d_did, utf8_encoding()))) {
1382 afp_errno = AFPERR_PARAM;
1388 cdir = dir->d_child;
1390 if ( cdir && (vol->v_flags & AFPVOL_CASEINSEN) &&
1391 (size_t)-1 != convert_string_allocate(((ret.m_type == 3)?CH_UTF8_MAC:vol->v_maccharset),
1392 CH_UCS2, path, strlen(path), (char **)&tmpname) )
1395 if (!cdir->d_m_name_ucs2) {
1396 LOG(log_error, logtype_afpd, "cname: no UCS2 name for %s (did %u)!!!", cdir->d_m_name, ntohl(cdir->d_did) );
1397 /* this shouldn't happen !!!! */
1401 if ( strcmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
1404 if ( strcasecmp_w( cdir->d_m_name_ucs2, tmpname ) == 0 ) {
1407 cdir = (cdir == dir->d_child->d_prev) ? NULL :cdir->d_next;
1413 if (dir->d_did == DIRDID_ROOT_PARENT) {
1414 /* root parent has only one child and d_m_name is *NOT* utm (d_u_name)
1415 * d_m_name is the Mac volume name
1416 * d_u_name is the volume unix directory name
1420 if (!strcmp(vol->v_dir->d_m_name, ret.m_name)) {
1425 cdir = dirsearch_byname(vol, dir, ret.u_name);
1429 if (cdir == NULL && scdir != NULL) {
1431 /* LOG(log_debug, logtype_afpd, "cname: using casediff for %s, (%s = %s)", fullpathname(cdir->d_u_name), cdir->d_m_name, path ); */
1434 if ( cdir == NULL ) {
1436 /* if dir == curdir it always succeed,
1437 even if curdir is deleted.
1438 it's not a pb because it will fail in extenddir
1440 if ( movecwd( vol, dir ) < 0 ) {
1441 /* dir is not valid anymore
1442 we delete dir from the cache and abort.
1444 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1445 afp_errno = AFPERR_NOOBJ;
1448 if (afp_errno == AFPERR_ACCESS)
1450 dir_invalidate(vol, dir);
1453 cdir = extenddir( vol, dir, &ret );
1457 cdir = extenddir( vol, dir, &ret );
1458 } /* if (!extend) */
1460 if ( cdir == NULL ) {
1474 * Move curdir to dir, with a possible chdir()
1476 int movecwd( vol, dir)
1477 const struct vol *vol;
1480 char path[MAXPATHLEN + 1];
1485 if ( dir == curdir ) {
1488 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1489 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1493 p = path + sizeof(path) - 1;
1496 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1499 /* parent directory is deleted */
1500 afp_errno = AFPERR_NOOBJ;
1504 if (p -n -1 < path) {
1505 afp_errno = AFPERR_PARAM;
1512 if ( d != curdir ) {
1513 n = strlen( vol->v_path );
1514 if (p -n -1 < path) {
1515 afp_errno = AFPERR_PARAM;
1520 memcpy( p, vol->v_path, n );
1522 if ( chdir( p ) < 0 ) {
1526 afp_errno = AFPERR_ACCESS;
1529 afp_errno = AFPERR_NOOBJ;
1539 * We can't use unix file's perm to support Apple's inherited protection modes.
1540 * If we aren't the file's owner we can't change its perms when moving it and smb
1541 * nfs,... don't even try.
1543 #define AFP_CHECK_ACCESS
1545 int check_access(char *path, int mode)
1547 #ifdef AFP_CHECK_ACCESS
1555 accessmode(p, &ma, curdir, NULL);
1556 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1558 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1564 /* --------------------- */
1565 int file_access(struct path *path, int mode)
1569 accessmode(path->u_name, &ma, curdir, &path->st);
1570 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1572 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1578 /* --------------------- */
1579 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1581 dir->offcnt = count;
1582 dir->ctime = st->st_ctime;
1583 dir->d_flags &= ~DIRF_CNID;
1586 /* ---------------------
1587 * is our cached offspring count valid?
1590 int diroffcnt(struct dir *dir, struct stat *st)
1592 return st->st_ctime == dir->ctime;
1595 /* ---------------------
1596 * is our cached also for reenumerate id?
1599 int dirreenumerate(struct dir *dir, struct stat *st)
1601 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1604 /* ------------------------------
1606 (name, dir) with curdir:name == dir, from afp_enumerate
1609 int getdirparams(const struct vol *vol,
1610 u_int16_t bitmap, struct path *s_path,
1612 char *buf, int *buflen )
1616 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1617 int bit = 0, isad = 0;
1623 struct stat *st = &s_path->st;
1624 char *upath = s_path->u_name;
1626 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1627 (1 << DIRPBIT_CDATE) |
1628 (1 << DIRPBIT_MDATE) |
1629 (1 << DIRPBIT_BDATE) |
1630 (1 << DIRPBIT_FINFO)))) {
1632 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1633 if ( !ad_metadata( upath, ADFLAGS_DIR, &ad) ) {
1638 if ( dir->d_did == DIRDID_ROOT) {
1639 pdid = DIRDID_ROOT_PARENT;
1640 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1643 pdid = dir->d_parent->d_did;
1647 while ( bitmap != 0 ) {
1648 while (( bitmap & 1 ) == 0 ) {
1656 ad_getattr(&ad, &ashort);
1657 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1658 && strcmp(dir->d_u_name, "..")) {
1659 ashort = htons(ATTRBIT_INVISIBLE);
1662 ashort |= htons(ATTRBIT_SHARED);
1663 memcpy( data, &ashort, sizeof( ashort ));
1664 data += sizeof( ashort );
1668 memcpy( data, &pdid, sizeof( pdid ));
1669 data += sizeof( pdid );
1672 case DIRPBIT_CDATE :
1673 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1674 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1675 memcpy( data, &aint, sizeof( aint ));
1676 data += sizeof( aint );
1679 case DIRPBIT_MDATE :
1680 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1681 memcpy( data, &aint, sizeof( aint ));
1682 data += sizeof( aint );
1685 case DIRPBIT_BDATE :
1686 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1687 aint = AD_DATE_START;
1688 memcpy( data, &aint, sizeof( aint ));
1689 data += sizeof( aint );
1692 case DIRPBIT_FINFO :
1694 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1695 } else { /* no appledouble */
1696 memset( data, 0, 32 );
1697 /* set default view -- this also gets done in ad_open() */
1698 ashort = htons(FINDERINFO_CLOSEDVIEW);
1699 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1701 /* dot files are by default invisible */
1702 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1703 strcmp(dir->d_u_name , "..")) {
1704 ashort = htons(FINDERINFO_INVISIBLE);
1705 memcpy(data + FINDERINFO_FRFLAGOFF,
1706 &ashort, sizeof(ashort));
1712 case DIRPBIT_LNAME :
1713 if (dir->d_m_name) /* root of parent can have a null name */
1716 memset(data, 0, sizeof(u_int16_t));
1717 data += sizeof( u_int16_t );
1720 case DIRPBIT_SNAME :
1721 memset(data, 0, sizeof(u_int16_t));
1722 data += sizeof( u_int16_t );
1726 memcpy( data, &dir->d_did, sizeof( aint ));
1727 data += sizeof( aint );
1730 case DIRPBIT_OFFCNT :
1732 /* this needs to handle current directory access rights */
1733 if (diroffcnt(dir, st)) {
1734 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1736 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1737 setdiroffcnt(dir, st, ret);
1738 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1740 ashort = htons( ashort );
1741 memcpy( data, &ashort, sizeof( ashort ));
1742 data += sizeof( ashort );
1746 aint = htonl(st->st_uid);
1747 memcpy( data, &aint, sizeof( aint ));
1748 data += sizeof( aint );
1752 aint = htonl(st->st_gid);
1753 memcpy( data, &aint, sizeof( aint ));
1754 data += sizeof( aint );
1757 case DIRPBIT_ACCESS :
1758 accessmode( upath, &ma, dir , st);
1760 *data++ = ma.ma_user;
1761 *data++ = ma.ma_world;
1762 *data++ = ma.ma_group;
1763 *data++ = ma.ma_owner;
1766 /* Client has requested the ProDOS information block.
1767 Just pass back the same basic block for all
1768 directories. <shirsch@ibm.net> */
1769 case DIRPBIT_PDINFO :
1770 if (afp_version >= 30) { /* UTF8 name */
1771 utf8 = kTextEncodingUTF8;
1772 if (dir->d_m_name) /* root of parent can have a null name */
1775 memset(data, 0, sizeof(u_int16_t));
1776 data += sizeof( u_int16_t );
1778 memcpy(data, &aint, sizeof( aint ));
1779 data += sizeof( aint );
1781 else { /* ProDOS Info Block */
1784 ashort = htons( 0x0200 );
1785 memcpy( data, &ashort, sizeof( ashort ));
1786 data += sizeof( ashort );
1787 memset( data, 0, sizeof( ashort ));
1788 data += sizeof( ashort );
1792 case DIRPBIT_UNIXPR :
1793 aint = htonl(st->st_uid);
1794 memcpy( data, &aint, sizeof( aint ));
1795 data += sizeof( aint );
1796 aint = htonl(st->st_gid);
1797 memcpy( data, &aint, sizeof( aint ));
1798 data += sizeof( aint );
1801 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1802 memcpy( data, &aint, sizeof( aint ));
1803 data += sizeof( aint );
1805 accessmode( upath, &ma, dir , st);
1807 *data++ = ma.ma_user;
1808 *data++ = ma.ma_world;
1809 *data++ = ma.ma_group;
1810 *data++ = ma.ma_owner;
1815 ad_close_metadata( &ad );
1817 return( AFPERR_BITMAP );
1823 ashort = htons( data - buf );
1824 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1825 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, 0);
1827 if ( utf_nameoff ) {
1828 ashort = htons( data - buf );
1829 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1830 data = set_name(vol, data, pdid, dir->d_m_name, dir->d_did, utf8);
1833 ad_close_metadata( &ad );
1835 *buflen = data - buf;
1839 /* ----------------------------- */
1840 int path_error(struct path *path, int error)
1842 /* - a dir with access error
1843 * - no error it's a file
1846 if (path_isadir(path))
1848 if (path->st_valid && path->st_errno)
1850 return AFPERR_BADTYPE ;
1853 /* ----------------------------- */
1854 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1856 char *ibuf, *rbuf _U_;
1857 int ibuflen _U_, *rbuflen;
1862 u_int16_t vid, bitmap;
1868 memcpy( &vid, ibuf, sizeof( vid ));
1869 ibuf += sizeof( vid );
1871 if (NULL == ( vol = getvolbyvid( vid )) ) {
1872 return( AFPERR_PARAM );
1875 if (vol->v_flags & AFPVOL_RO)
1876 return AFPERR_VLOCK;
1878 memcpy( &did, ibuf, sizeof( did ));
1879 ibuf += sizeof( int );
1881 if (NULL == ( dir = dirlookup( vol, did )) ) {
1885 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1886 bitmap = ntohs( bitmap );
1887 ibuf += sizeof( bitmap );
1889 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1890 return get_afp_errno(AFPERR_NOOBJ);
1893 if ( *path->m_name != '\0' ) {
1894 rc = path_error(path, AFPERR_NOOBJ);
1895 /* maybe we are trying to set perms back */
1896 if (rc != AFPERR_ACCESS)
1901 * If ibuf is odd, make it even.
1903 if ((u_long)ibuf & 1 ) {
1907 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1908 setvoltime(obj, vol );
1914 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1916 * assume path == '\0' eg. it's a directory in canonical form
1919 struct path Cur_Path = {
1922 ".", /* unix name */
1924 NULL,/* struct dir */
1925 0, /* stat is not set */
1928 /* ------------------ */
1929 static int set_dir_errors(struct path *path, const char *where, int err)
1934 return AFPERR_ACCESS;
1936 return AFPERR_VLOCK;
1938 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
1939 return AFPERR_PARAM;
1942 /* ------------------ */
1943 int setdirparams(const struct vol *vol,
1944 struct path *path, u_int16_t d_bitmap, char *buf )
1953 int bit, aint, isad = 1;
1956 u_int16_t ashort, bshort;
1958 int change_mdate = 0;
1959 int change_parent_mdate = 0;
1961 u_int16_t bitmap = d_bitmap;
1962 u_char finder_buf[32];
1965 u_int16_t upriv_bit = 0;
1968 upath = path->u_name;
1970 while ( bitmap != 0 ) {
1971 while (( bitmap & 1 ) == 0 ) {
1979 memcpy( &ashort, buf, sizeof( ashort ));
1980 buf += sizeof( ashort );
1982 case DIRPBIT_CDATE :
1984 memcpy(&cdate, buf, sizeof(cdate));
1985 buf += sizeof( cdate );
1987 case DIRPBIT_MDATE :
1988 memcpy(&newdate, buf, sizeof(newdate));
1989 buf += sizeof( newdate );
1991 case DIRPBIT_BDATE :
1993 memcpy(&bdate, buf, sizeof(bdate));
1994 buf += sizeof( bdate );
1996 case DIRPBIT_FINFO :
1998 memcpy( finder_buf, buf, 32 );
2001 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2002 change_parent_mdate = 1;
2003 memcpy( &owner, buf, sizeof(owner));
2004 buf += sizeof( owner );
2007 change_parent_mdate = 1;
2008 memcpy( &group, buf, sizeof( group ));
2009 buf += sizeof( group );
2011 case DIRPBIT_ACCESS :
2013 change_parent_mdate = 1;
2014 ma.ma_user = *buf++;
2015 ma.ma_world = *buf++;
2016 ma.ma_group = *buf++;
2017 ma.ma_owner = *buf++;
2018 mpriv = mtoumode( &ma ) | vol->v_perm;
2019 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2020 err = set_dir_errors(path, "setdirmode", errno);
2024 /* Ignore what the client thinks we should do to the
2025 ProDOS information block. Skip over the data and
2026 report nothing amiss. <shirsch@ibm.net> */
2027 case DIRPBIT_PDINFO :
2028 if (afp_version < 30) {
2032 err = AFPERR_BITMAP;
2036 case DIRPBIT_UNIXPR :
2037 if (vol_unix_priv(vol)) {
2038 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
2039 buf += sizeof( owner );
2040 memcpy( &group, buf, sizeof( group ));
2041 buf += sizeof( group );
2044 change_parent_mdate = 1;
2045 memcpy( &upriv, buf, sizeof( upriv ));
2046 buf += sizeof( upriv );
2047 upriv = ntohl (upriv) | vol->v_perm;
2048 if (dir_rx_set(upriv)) {
2049 /* maybe we are trying to set perms back */
2050 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
2052 err = set_dir_errors(path, "setdirunixmode", errno);
2063 err = AFPERR_BITMAP;
2071 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2073 if (ad_open_metadata( upath, vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2075 * Check to see what we're trying to set. If it's anything
2076 * but ACCESS, UID, or GID, give an error. If it's any of those
2077 * three, we don't need the ad to be open, so just continue.
2079 * note: we also don't need to worry about mdate. also, be quiet
2080 * if we're using the noadouble option.
2082 if (!vol_noadouble(vol) && (d_bitmap &
2083 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
2084 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
2085 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
2086 return AFPERR_ACCESS;
2092 * Check to see if a create was necessary. If it was, we'll want
2093 * to set our name, etc.
2095 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
2096 ad_setname(&ad, curdir->d_m_name);
2102 while ( bitmap != 0 ) {
2103 while (( bitmap & 1 ) == 0 ) {
2111 ad_getattr(&ad, &bshort);
2112 if ((bshort & htons(ATTRBIT_INVISIBLE)) !=
2113 (ashort & htons(ATTRBIT_INVISIBLE) & htons(ATTRBIT_SETCLR)) )
2114 change_parent_mdate = 1;
2115 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
2116 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
2120 ad_setattr(&ad, bshort);
2123 case DIRPBIT_CDATE :
2125 ad_setdate(&ad, AD_DATE_CREATE, cdate);
2128 case DIRPBIT_MDATE :
2130 case DIRPBIT_BDATE :
2132 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
2135 case DIRPBIT_FINFO :
2137 if ( dir->d_did == DIRDID_ROOT ) {
2139 * Alright, we admit it, this is *really* sick!
2140 * The 4 bytes that we don't copy, when we're dealing
2141 * with the root of a volume, are the directory's
2142 * location information. This eliminates that annoying
2143 * behavior one sees when mounting above another mount
2146 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
2147 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
2149 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
2153 case DIRPBIT_UID : /* What kind of loser mounts as root? */
2154 if ( (dir->d_did == DIRDID_ROOT) &&
2155 (setdeskowner( ntohl(owner), -1 ) < 0)) {
2156 err = set_dir_errors(path, "setdeskowner", errno);
2157 if (isad && err == AFPERR_PARAM) {
2158 err = AFP_OK; /* ???*/
2161 goto setdirparam_done;
2164 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
2165 err = set_dir_errors(path, "setdirowner", errno);
2166 goto setdirparam_done;
2170 if (dir->d_did == DIRDID_ROOT)
2171 setdeskowner( -1, ntohl(group) );
2172 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2173 err = set_dir_errors(path, "setdirowner", errno);
2174 goto setdirparam_done;
2177 case DIRPBIT_ACCESS :
2178 if (dir->d_did == DIRDID_ROOT) {
2180 if (!dir_rx_set(mpriv)) {
2181 /* we can't remove read and search for owner on volume root */
2182 err = AFPERR_ACCESS;
2183 goto setdirparam_done;
2187 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2188 err = set_dir_errors(path, "setdirmode", errno);
2189 goto setdirparam_done;
2192 case DIRPBIT_PDINFO :
2193 if (afp_version >= 30) {
2194 err = AFPERR_BITMAP;
2195 goto setdirparam_done;
2198 case DIRPBIT_UNIXPR :
2199 if (vol_unix_priv(vol)) {
2200 if (dir->d_did == DIRDID_ROOT) {
2201 if (!dir_rx_set(upriv)) {
2202 /* we can't remove read and search for owner on volume root */
2203 err = AFPERR_ACCESS;
2204 goto setdirparam_done;
2206 setdeskowner( -1, ntohl(group) );
2207 setdeskmode( upriv );
2209 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2210 err = set_dir_errors(path, "setdirowner", errno);
2211 goto setdirparam_done;
2214 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2215 err = set_dir_errors(path, "setdirunixmode", errno);
2216 goto setdirparam_done;
2220 err = AFPERR_BITMAP;
2221 goto setdirparam_done;
2225 err = AFPERR_BITMAP;
2226 goto setdirparam_done;
2235 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2236 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2240 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2241 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2246 if (path->st_valid && !path->st_errno) {
2247 struct stat *st = &path->st;
2249 if (dir && dir->d_parent) {
2250 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_parent->d_did, vol->v_stamp);
2253 ad_flush_metadata( &ad);
2254 ad_close_metadata( &ad);
2257 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2258 && gettimeofday(&tv, NULL) == 0) {
2259 if (!movecwd(vol, dir->d_parent)) {
2260 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2261 /* be careful with bitmap because now dir is null */
2262 bitmap = 1<<DIRPBIT_MDATE;
2263 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2264 /* should we reset curdir ?*/
2271 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
2274 int ibuflen _U_, *rbuflen;
2280 struct path *s_path;
2288 memcpy( &vid, ibuf, sizeof( vid ));
2289 ibuf += sizeof( vid );
2290 if (NULL == ( vol = getvolbyvid( vid )) ) {
2291 return( AFPERR_PARAM );
2294 if (vol->v_flags & AFPVOL_RO)
2295 return AFPERR_VLOCK;
2297 memcpy( &did, ibuf, sizeof( did ));
2298 ibuf += sizeof( did );
2299 if (NULL == ( dir = dirlookup( vol, did )) ) {
2300 return afp_errno; /* was AFPERR_NOOBJ */
2302 /* for concurrent access we need to be sure we are not in the
2303 * folder we want to create...
2307 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2308 return get_afp_errno(AFPERR_PARAM);
2310 /* cname was able to move curdir to it! */
2311 if (*s_path->m_name == '\0')
2312 return AFPERR_EXIST;
2314 upath = s_path->u_name;
2315 if (0 != (err = check_name(vol, upath))) {
2319 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2323 if (of_stat(s_path) < 0) {
2327 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
2331 if ( movecwd( vol, dir ) < 0 ) {
2332 return( AFPERR_PARAM );
2335 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2336 if (ad_open_metadata( ".", vol_noadouble(vol)|ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2337 if (vol_noadouble(vol))
2338 goto createdir_done;
2339 return( AFPERR_ACCESS );
2341 ad_setname(&ad, s_path->m_name);
2342 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2344 ad_flush_metadata( &ad);
2345 ad_close_metadata( &ad);
2348 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2349 *rbuflen = sizeof( u_int32_t );
2350 setvoltime(obj, vol );
2355 * dst new unix filename (not a pathname)
2356 * newname new mac name
2360 int renamedir(vol, src, dst, dir, newparent, newname)
2361 const struct vol *vol;
2362 char *src, *dst, *newname;
2363 struct dir *dir, *newparent;
2370 /* existence check moved to afp_moveandrename */
2371 if ( unix_rename( src, dst ) < 0 ) {
2374 return( AFPERR_NOOBJ );
2376 return( AFPERR_ACCESS );
2378 return AFPERR_VLOCK;
2380 /* tried to move directory into a subdirectory of itself */
2381 return AFPERR_CANTMOVE;
2383 /* this needs to copy and delete. bleah. that means we have
2384 * to deal with entire directory hierarchies. */
2385 if ((err = copydir(vol, src, dst)) < 0) {
2389 if ((err = deletedir(src)) < 0)
2393 return( AFPERR_PARAM );
2397 vol->vfs->rf_renamedir(vol, src, dst);
2399 len = strlen( newname );
2400 /* rename() succeeded so we need to update our tree even if we can't open
2404 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2406 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2407 ad_setname(&ad, newname);
2408 ad_flush_metadata( &ad);
2409 ad_close_metadata( &ad);
2412 dir_hash_del(vol, dir);
2413 if (dir->d_m_name == dir->d_u_name)
2414 dir->d_u_name = NULL;
2416 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2417 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2418 /* FIXME : fatal ? */
2421 dir->d_m_name = buf;
2422 strcpy( dir->d_m_name, newname );
2424 if (newname == dst) {
2425 free(dir->d_u_name);
2426 dir->d_u_name = dir->d_m_name;
2429 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2430 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2433 dir->d_u_name = buf;
2434 strcpy( dir->d_u_name, dst );
2437 if (dir->d_m_name_ucs2)
2438 free(dir->d_m_name_ucs2);
2440 dir->d_m_name_ucs2 = NULL;
2441 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))
2442 dir->d_m_name_ucs2 = NULL;
2444 if (( parent = dir->d_parent ) == NULL ) {
2447 if ( parent == newparent ) {
2448 hash_alloc_insert(vol->v_hash, dir, dir);
2452 /* detach from old parent and add to new one. */
2453 dirchildremove(parent, dir);
2454 dir->d_parent = newparent;
2455 dirchildadd(vol, newparent, dir);
2459 /* delete an empty directory */
2460 int deletecurdir( vol)
2461 const struct vol *vol;
2471 if ( curdir->d_parent == NULL ) {
2472 return( AFPERR_ACCESS );
2477 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2478 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2480 ad_getattr(&ad, &ashort);
2481 ad_close( &ad, ADFLAGS_HF );
2482 if ((ashort & htons(ATTRBIT_NODELETE))) {
2483 return AFPERR_OLOCK;
2486 err = vol->vfs->rf_deletecurdir(vol);
2491 /* now get rid of dangling symlinks */
2492 if ((dp = opendir("."))) {
2493 while ((de = readdir(dp))) {
2494 /* skip this and previous directory */
2495 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2498 /* bail if it's not a symlink */
2499 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2501 return AFPERR_DIRNEMPT;
2504 if ((err = netatalk_unlink(de->d_name))) {
2511 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2516 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2517 dirchildremove(curdir, fdir);
2518 cnid_delete(vol->v_cdb, fdir->d_did);
2519 dir_remove( vol, fdir );
2524 /* inode is used as key for cnid.
2525 * Close the descriptor only after cnid_delete
2533 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2536 int ibuflen _U_, *rbuflen;
2546 sfunc = (unsigned char) *ibuf++;
2547 memcpy( &id, ibuf, sizeof( id ));
2552 if (sfunc == 3 || sfunc == 4) {
2553 if (afp_version < 30) {
2554 return( AFPERR_PARAM );
2561 case 3 :/* unicode */
2562 if (( pw = getpwuid( id )) == NULL ) {
2563 return( AFPERR_NOITEM );
2565 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2566 pw->pw_name, strlen(pw->pw_name), &name);
2570 case 4 : /* unicode */
2571 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2572 return( AFPERR_NOITEM );
2574 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2575 gr->gr_name, strlen(gr->gr_name), &name);
2579 return( AFPERR_PARAM );
2581 len = strlen( name );
2588 u_int16_t tp = htons(len);
2589 memcpy(rbuf, &tp, sizeof(tp));
2598 memcpy( rbuf, name, len );
2606 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2609 int ibuflen _U_, *rbuflen;
2618 sfunc = (unsigned char) *ibuf++;
2622 case 2 : /* unicode */
2623 if (afp_version < 30) {
2624 return( AFPERR_PARAM );
2626 memcpy(&ulen, ibuf, sizeof(ulen));
2632 len = (unsigned char) *ibuf++;
2635 return( AFPERR_PARAM );
2642 case 1 : /* unicode */
2644 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2645 return( AFPERR_NOITEM );
2650 case 2 : /* unicode */
2652 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2653 return( AFPERR_NOITEM );
2662 memcpy( rbuf, &id, sizeof( id ));
2663 *rbuflen = sizeof( id );
2667 /* ------------------------------------
2668 variable DID support
2670 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2672 char *ibuf _U_, *rbuf _U_;
2673 int ibuflen _U_, *rbuflen;
2684 /* do nothing as dids are static for the life of the process. */
2688 memcpy(&vid, ibuf, sizeof( vid ));
2689 ibuf += sizeof( vid );
2690 if (( vol = getvolbyvid( vid )) == NULL ) {
2691 return( AFPERR_PARAM );
2694 memcpy( &did, ibuf, sizeof( did ));
2695 ibuf += sizeof( did );
2696 if (( dir = dirlookup( vol, did )) == NULL ) {
2697 return( AFPERR_PARAM );
2700 /* dir_remove -- deletedid */
2706 /* did creation gets done automatically
2707 * there's a pb again with case but move it to cname
2709 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2712 int ibuflen _U_, *rbuflen;
2715 struct dir *parentdir;
2723 memcpy(&vid, ibuf, sizeof(vid));
2724 ibuf += sizeof( vid );
2726 if (NULL == ( vol = getvolbyvid( vid )) ) {
2727 return( AFPERR_PARAM );
2730 memcpy(&did, ibuf, sizeof(did));
2731 ibuf += sizeof(did);
2733 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2737 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2738 return get_afp_errno(AFPERR_PARAM);
2741 if ( *path->m_name != '\0' ) {
2742 return path_error(path, AFPERR_NOOBJ);
2745 if ( !path->st_valid && of_stat(path ) < 0 ) {
2746 return( AFPERR_NOOBJ );
2748 if ( path->st_errno ) {
2749 return( AFPERR_NOOBJ );
2752 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2753 *rbuflen = sizeof(curdir->d_did);