2 * $Id: directory.c,v 1.71.2.4.2.1 2003-09-09 16:42:20 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 */
15 #include <atalk/logger.h>
16 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <netatalk/endian.h>
22 #include <atalk/adouble.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
32 #endif /* HAVE_FCNTL_H */
39 #else /* STDC_HEADERS */
43 #endif /* HAVE_STRCHR */
44 char *strchr (), *strrchr ();
46 #define memcpy(d,s,n) bcopy ((s), (d), (n))
47 #define memmove(d,s,n) bcopy ((s), (d), (n))
48 #endif /* ! HAVE_MEMCPY */
49 #endif /* STDC_HEADERS */
51 #include "directory.h"
65 #define SENTINEL (&sentinel)
66 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
67 NULL, NULL, NULL, NULL, NULL, 0, 0,
69 static struct dir rootpar = { SENTINEL, SENTINEL, NULL, 0,
70 NULL, NULL, NULL, NULL, NULL, 0, 0,
73 /* (from IM: Toolbox Essentials)
74 * dirFinderInfo (DInfo) fields:
76 * frRect 8 folder's window rectangle
78 * frLocation 4 folder's location in window
79 * frView 2 folder's view (default == closedView (256))
81 * extended dirFinderInfo (DXInfo) fields:
82 * frScroll 4 scroll position
83 * frOpenChain: 4 directory ID chain of open folders
84 * frScript: 1 script flag and code
85 * frXFlags: 1 reserved
86 * frComment: 2 comment ID
87 * frPutAway: 4 home directory ID
91 * redid did assignment for directories. now we use red-black trees.
96 const struct vol *vol;
102 /* check for 0 did */
104 afp_errno = AFPERR_PARAM;
107 if ( did == DIRDID_ROOT_PARENT ) {
109 rootpar.d_did = DIRDID_ROOT_PARENT;
110 rootpar.d_child = vol->v_dir;
115 afp_errno = AFPERR_NOOBJ;
116 while ( dir != SENTINEL ) {
117 if (dir->d_did == did)
118 return dir->d_m_name ? dir : NULL;
119 dir = (dir->d_did > did) ? dir->d_left : dir->d_right;
124 /* ------------------- */
126 int path_isadir(struct path *o_path)
128 return o_path->dir != NULL;
130 return o_path->m_name == '\0' || /* we are in a it */
131 !o_path->st_valid || /* in cache but we can't chdir in it */
132 (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
137 int get_afp_errno(const int param)
139 if (afp_errno != AFPERR_DID1)
144 /* ------------------- */
146 dirsearch_byname( cdir, name )
152 if (!strcmp(name, "."))
156 if ( strcmp( dir->d_u_name, name ) == 0 ) {
159 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
164 /* -----------------------------------------
165 * if did is not in the cache resolve it with cnid
168 * OSX call it with bogus id, ie file ID not folder ID,
169 * and we are really bad in this case.
172 dirlookup( vol, did )
173 const struct vol *vol;
179 static char path[MAXPATHLEN + 1];
182 static char buffer[12 + MAXPATHLEN + 1];
183 int buflen = 12 + MAXPATHLEN + 1;
188 ret = dirsearch(vol, did);
189 if (ret != NULL || afp_errno == AFPERR_PARAM)
192 utf8 = utf8_encoding();
193 maxpath = (utf8)?MAXPATHLEN -7:255;
195 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen)) ) {
196 afp_errno = AFPERR_NOOBJ;
199 ptr = path + MAXPATHLEN;
200 if (NULL == ( mpath = utompath(vol, upath, did, utf8) ) ) {
201 afp_errno = AFPERR_NOOBJ;
205 pathlen = len; /* no 0 in the last part */
207 strcpy(ptr - len, mpath);
210 ret = dirsearch(vol,id);
215 if ( NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, buflen))
217 NULL == (mpath = utompath(vol, upath, cnid, utf8))
219 afp_errno = AFPERR_NOOBJ;
223 len = strlen(mpath) + 1;
225 if (pathlen > maxpath) {
226 afp_errno = AFPERR_PARAM;
229 strcpy(ptr - len, mpath);
233 /* fill the cache, another place where we know about the path type */
239 temp16 = htons(pathlen);
240 memcpy(ptr, &temp16, sizeof(temp16));
242 temp = htonl(kTextEncodingUTF8);
244 memcpy(ptr, &temp, sizeof(temp));
250 *ptr = (unsigned char)pathlen;
254 /* cname is not efficient */
255 if (cname( vol, ret, &ptr ) == NULL )
258 return dirsearch(vol, did);
261 /* --------------------------- */
262 /* rotate the tree to the left */
263 static void dir_leftrotate(vol, dir)
267 struct dir *right = dir->d_right;
269 /* whee. move the right's left tree into dir's right tree */
270 dir->d_right = right->d_left;
271 if (right->d_left != SENTINEL)
272 right->d_left->d_back = dir;
274 if (right != SENTINEL) {
275 right->d_back = dir->d_back;
279 if (!dir->d_back) /* no parent. move the right tree to the top. */
281 else if (dir == dir->d_back->d_left) /* we were on the left */
282 dir->d_back->d_left = right;
284 dir->d_back->d_right = right; /* we were on the right */
286 /* re-insert dir on the left tree */
293 /* rotate the tree to the right */
294 static void dir_rightrotate(vol, dir)
298 struct dir *left = dir->d_left;
300 /* whee. move the left's right tree into dir's left tree */
301 dir->d_left = left->d_right;
302 if (left->d_right != SENTINEL)
303 left->d_right->d_back = dir;
305 if (left != SENTINEL) {
306 left->d_back = dir->d_back;
310 if (!dir->d_back) /* no parent. move the left tree to the top. */
312 else if (dir == dir->d_back->d_right) /* we were on the right */
313 dir->d_back->d_right = left;
315 dir->d_back->d_left = left; /* we were on the left */
317 /* re-insert dir on the right tree */
323 /* recolor after a removal */
324 static struct dir *dir_rmrecolor(vol, dir)
330 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
331 /* are we on the left tree? */
332 if (dir == dir->d_back->d_left) {
333 leaf = dir->d_back->d_right; /* get right side */
334 if (leaf->d_color == DIRTREE_COLOR_RED) {
335 /* we're red. we need to change to black. */
336 leaf->d_color = DIRTREE_COLOR_BLACK;
337 dir->d_back->d_color = DIRTREE_COLOR_RED;
338 dir_leftrotate(vol, dir->d_back);
339 leaf = dir->d_back->d_right;
342 /* right leaf has black end nodes */
343 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
344 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
345 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
346 dir = dir->d_back; /* ascend */
348 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
349 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
350 leaf->d_color = DIRTREE_COLOR_RED;
351 dir_rightrotate(vol, leaf);
352 leaf = dir->d_back->d_right;
354 leaf->d_color = dir->d_back->d_color;
355 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
356 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
357 dir_leftrotate(vol, dir->d_back);
360 } else { /* right tree */
361 leaf = dir->d_back->d_left; /* left tree */
362 if (leaf->d_color == DIRTREE_COLOR_RED) {
363 leaf->d_color = DIRTREE_COLOR_BLACK;
364 dir->d_back->d_color = DIRTREE_COLOR_RED;
365 dir_rightrotate(vol, dir->d_back);
366 leaf = dir->d_back->d_left;
369 /* left leaf has black end nodes */
370 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
371 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
372 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
373 dir = dir->d_back; /* ascend */
375 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
376 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
377 leaf->d_color = DIRTREE_COLOR_RED;
378 dir_leftrotate(vol, leaf);
379 leaf = dir->d_back->d_left;
381 leaf->d_color = dir->d_back->d_color;
382 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
383 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
384 dir_rightrotate(vol, dir->d_back);
389 dir->d_color = DIRTREE_COLOR_BLACK;
396 /* remove the node from the tree. this is just like insertion, but
397 * different. actually, it has to worry about a bunch of things that
398 * insertion doesn't care about. */
399 static void dir_remove( vol, dir )
404 struct ofork *of, *last;
405 struct dir *node, *leaf;
406 #endif /* REMOVE_NODES */
408 if (!dir || (dir == SENTINEL))
411 /* i'm not sure if it really helps to delete stuff. */
414 dir->d_m_name = NULL;
415 dir->d_u_name = NULL;
416 #else /* ! REMOVE_NODES */
418 /* go searching for a node with at most one child */
419 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
423 while (node->d_left != SENTINEL)
428 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
431 leaf->d_back = node->d_back;
434 } else if (node == node->d_back->d_left) { /* left tree */
435 node->d_back->d_left = leaf;
437 node->d_back->d_right = leaf;
440 /* we want to free node, but we also want to free the data in dir.
441 * currently, that's d_name and the directory traversal bits.
442 * we just copy the necessary bits and then fix up all the
443 * various pointers to the directory. needless to say, there are
444 * a bunch of places that store the directory struct. */
446 struct dir save, *tmp;
448 memcpy(&save, dir, sizeof(save));
449 memcpy(dir, node, sizeof(struct dir));
451 /* restore the red-black bits */
452 dir->d_left = save.d_left;
453 dir->d_right = save.d_right;
454 dir->d_back = save.d_back;
455 dir->d_color = save.d_color;
457 if (node == vol->v_dir) {/* we may need to fix up this pointer */
459 rootpar.d_child = vol->v_dir;
461 /* if we aren't the root directory, we have parents and
462 * siblings to worry about */
463 if (dir->d_parent->d_child == node)
464 dir->d_parent->d_child = dir;
465 dir->d_next->d_prev = dir;
466 dir->d_prev->d_next = dir;
469 /* fix up children. */
473 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
476 if (node == curdir) /* another pointer to fixup */
479 /* we also need to fix up oforks. bleah */
480 if ((of = dir->d_ofork)) {
481 last = of->of_d_prev;
484 of = (last == of) ? NULL : of->of_d_next;
488 /* set the node's d_name */
489 node->d_m_name = save.d_m_name;
490 node->d_u_name = save.d_u_name;
493 if (node->d_color == DIRTREE_COLOR_BLACK)
494 dir_rmrecolor(vol, leaf);
496 if (node->d_u_name != node->d_m_name) {
497 free(node->d_u_name);
499 free(node->d_m_name);
501 #endif /* ! REMOVE_NODES */
504 /* ---------------------------------------
505 * remove the node and its childs from the tree
507 * FIXME what about opened forks with refs to it?
508 * it's an afp specs violation because you can't delete
509 * an opened forks. Now afpd doesn't care about forks opened by other
510 * process. It's fixable within afpd if fnctl_lock, doable with smb and
511 * next to impossible for nfs and local filesystem access.
513 static void dir_invalidate( vol, dir )
514 const struct vol *vol;
518 /* v_root can't be deleted */
519 if (movecwd(vol, vol->v_root) < 0) {
520 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
524 dirchildremove(dir->d_parent, dir);
525 dir_remove( vol, dir );
528 /* ------------------------------------ */
529 static struct dir *dir_insert(vol, dir)
530 const struct vol *vol;
536 while (pdir->d_did != dir->d_did ) {
537 if ( pdir->d_did > dir->d_did ) {
538 if ( pdir->d_left == SENTINEL ) {
545 if ( pdir->d_right == SENTINEL ) {
550 pdir = pdir->d_right;
558 * attempt to extend the current dir. tree to include path
559 * as a side-effect, movecwd to that point and return the new dir
562 extenddir( vol, dir, path )
567 if ( path->u_name == NULL) {
568 path->u_name = mtoupath(vol, path->m_name, utf8_encoding() );
572 if ( path->u_name == NULL) {
573 afp_errno = AFPERR_PARAM;
576 if (of_stat( path ) != 0 ) {
580 if (!S_ISDIR(path->st.st_mode)) {
584 if (( dir = adddir( vol, dir, path)) == NULL ) {
589 if ( movecwd( vol, dir ) < 0 ) {
596 /* -------------------
597 system rmdir with afp error code.
598 ENOENT is not an error.
600 static int netatalk_rmdir(const char *name)
602 if (rmdir(name) < 0) {
607 return AFPERR_DIRNEMPT;
610 return AFPERR_ACCESS;
620 /* -------------------------
621 appledouble mkdir afp error code.
623 static int netatalk_mkdir(const char *name)
625 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
628 return( AFPERR_NOOBJ );
630 return( AFPERR_VLOCK );
633 return( AFPERR_ACCESS );
635 return( AFPERR_EXIST );
638 return( AFPERR_DFULL );
640 return( AFPERR_PARAM );
646 /* -------------------
647 system unlink with afp error code.
648 ENOENT is not an error.
650 int netatalk_unlink(const char *name)
652 if (unlink(name) < 0) {
660 return AFPERR_ACCESS;
668 /* ------------------- */
669 static int deletedir(char *dir)
671 char path[MAXPATHLEN + 1];
679 if ((len = strlen(dir)) +2 > sizeof(path))
683 if ((dp = opendir(dir)) == NULL)
689 remain = sizeof(path) -len -1;
690 while ((de = readdir(dp)) && err == AFP_OK) {
691 /* skip this and previous directory */
692 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
695 if (strlen(de->d_name) > remain) {
699 strcpy(path + len, de->d_name);
700 if (stat(path, &st)) {
703 if (S_ISDIR(st.st_mode)) {
704 err = deletedir(path);
706 err = netatalk_unlink(path);
711 /* okay. the directory is empty. delete it. note: we already got rid
714 err = netatalk_rmdir(dir);
719 /* do a recursive copy. */
720 static int copydir(char *src, char *dst, int noadouble)
722 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
732 /* doesn't exist or the path is too long. */
733 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
734 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
735 ((dp = opendir(src)) == NULL))
738 /* try to create the destination directory */
739 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
744 /* set things up to copy */
748 srem = sizeof(spath) - slen -1;
753 drem = sizeof(dpath) - dlen -1;
756 while ((de = readdir(dp))) {
757 /* skip this and previous directory */
758 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
761 if (strlen(de->d_name) > srem) {
765 strcpy(spath + slen, de->d_name);
767 if (stat(spath, &st) == 0) {
768 if (strlen(de->d_name) > drem) {
772 strcpy(dpath + dlen, de->d_name);
774 if (S_ISDIR(st.st_mode)) {
775 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
777 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
781 /* keep the same time stamp. */
782 ut.actime = ut.modtime = st.st_mtime;
788 /* keep the same time stamp. */
789 if (stat(src, &st) == 0) {
790 ut.actime = ut.modtime = st.st_mtime;
800 /* --- public functions follow --- */
802 /* NOTE: we start off with at least one node (the root directory). */
803 struct dir *dirinsert( vol, dir )
809 if ((node = dir_insert(vol, dir)))
812 /* recolor the tree. the current node is red. */
813 dir->d_color = DIRTREE_COLOR_RED;
815 /* parent of this node has to be black. if the parent node
816 * is red, then we have a grandparent. */
817 while ((dir != vol->v_root) &&
818 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
819 /* are we on the left tree? */
820 if (dir->d_back == dir->d_back->d_back->d_left) {
821 node = dir->d_back->d_back->d_right; /* get the right node */
822 if (node->d_color == DIRTREE_COLOR_RED) {
823 /* we're red. we need to change to black. */
824 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
825 node->d_color = DIRTREE_COLOR_BLACK;
826 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
827 dir = dir->d_back->d_back; /* finished. go up. */
829 if (dir == dir->d_back->d_right) {
831 dir_leftrotate(vol, dir);
833 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
834 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
835 dir_rightrotate(vol, dir->d_back->d_back);
838 node = dir->d_back->d_back->d_left;
839 if (node->d_color == DIRTREE_COLOR_RED) {
840 /* we're red. we need to change to black. */
841 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
842 node->d_color = DIRTREE_COLOR_BLACK;
843 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
844 dir = dir->d_back->d_back; /* finished. ascend */
846 if (dir == dir->d_back->d_left) {
848 dir_rightrotate(vol, dir);
850 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
851 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
852 dir_leftrotate(vol, dir->d_back->d_back);
857 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
861 /* free everything down. we don't bother to recolor as this is only
862 * called to free the entire tree */
863 void dirfreename(struct dir *dir)
865 if (dir->d_u_name != dir->d_m_name) {
874 if (!dir || (dir == SENTINEL))
877 if ( dir->d_left != SENTINEL ) {
878 dirfree( dir->d_left );
880 if ( dir->d_right != SENTINEL ) {
881 dirfree( dir->d_right );
884 if (dir != SENTINEL) {
890 /* --------------------------------------------
891 * most of the time mac name and unix name are the same
893 struct dir *dirnew(const char *m_name, const char *u_name)
897 dir = (struct dir *) calloc(1, sizeof( struct dir ));
901 if ((dir->d_m_name = strdup(m_name)) == NULL) {
906 if (m_name == u_name) {
907 dir->d_u_name = dir->d_m_name;
909 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
915 dir->d_left = dir->d_right = SENTINEL;
916 dir->d_next = dir->d_prev = dir;
920 /* -------------------------------------------------- */
926 stat the file or errno
929 curdir: filename parent directory
935 stat the dir or errno
939 curdir: dir parent directory
947 curdir: dir parent directory
954 cname( vol, dir, cpath )
955 const struct vol *vol;
960 static char path[ MAXPATHLEN + 1];
961 static struct path ret;
972 afp_errno = AFPERR_NOOBJ;
973 switch (ret.m_type = *data) { /* path type */
976 len = (unsigned char) *data++;
981 if (afp_version >= 30) {
983 memcpy(&hint, data, sizeof(hint));
985 data += sizeof(hint);
987 memcpy(&len16, data, sizeof(len16));
994 /* else it's an error */
996 afp_errno = AFPERR_PARAM;
999 *cpath += len + size;
1007 if (movecwd( vol, dir ) < 0 ) {
1009 movecwd failed some of dir path are not there anymore.
1010 FIXME Is it true with other errors?
1011 so we remove dir from the cache
1013 if (dir->d_did == DIRDID_ROOT_PARENT)
1015 if (afp_errno == AFPERR_ACCESS) {
1016 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1019 ret.m_name = dir->d_m_name;
1020 ret.u_name = dir->d_u_name;
1023 } else if (afp_errno == AFPERR_NOOBJ) {
1024 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1027 strcpy(path, dir->d_m_name);
1028 if (dir->d_m_name == dir->d_u_name) {
1032 size_t tp = strlen(path)+1;
1034 strcpy(path +tp, dir->d_u_name);
1035 ret.u_name = path +tp;
1038 dir_invalidate(vol, dir);
1041 dir_invalidate(vol, dir);
1044 if (*path == '\0') {
1049 } /* if (len == 0) */
1051 if (*data == sep ) {
1055 while (*data == sep && len > 0 ) {
1056 if ( dir->d_parent == NULL ) {
1059 dir = dir->d_parent;
1064 /* would this be faster with strlen + strncpy? */
1066 while ( *data != sep && len > 0 ) {
1068 if (p > &path[ MAXPATHLEN]) {
1069 afp_errno = AFPERR_PARAM;
1075 /* short cut bits by chopping off a trailing \0. this also
1076 makes the traversal happy w/ filenames at the end of the
1083 if ( p != path ) { /* we got something */
1086 /* check for mangled filename :( */
1087 if (afp_version >= 30) {
1088 t = demangle(vol, path);
1090 /* should we check id is cdir->d_did ? */
1092 if ( (t = utompath(vol, ret.u_name, 0, utf8_encoding())) ) {
1093 /* at last got our mac name */
1099 cdir = dir->d_child;
1101 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1104 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1107 if ( cdir == NULL ) {
1109 /* if dir == curdir it always succeed,
1110 even if curdir is deleted.
1111 it's not a pb because it will fail in extenddir
1113 if ( movecwd( vol, dir ) < 0 ) {
1114 /* dir is not valid anymore
1115 we delete dir from the cache and abort.
1117 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1118 afp_errno = AFPERR_NOOBJ;
1121 if (afp_errno == AFPERR_ACCESS)
1123 dir_invalidate(vol, dir);
1126 cdir = extenddir( vol, dir, &ret );
1130 cdir = extenddir( vol, dir, &ret );
1131 } /* if (!extend) */
1133 if ( cdir == NULL ) {
1135 if ( len > 0 || !ret.u_name) {
1143 } /* if (p != path) */
1148 * Move curdir to dir, with a possible chdir()
1150 int movecwd( vol, dir)
1151 const struct vol *vol;
1154 char path[MAXPATHLEN + 1];
1159 if ( dir == curdir ) {
1162 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1163 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1167 p = path + sizeof(path) - 1;
1170 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1173 /* parent directory is deleted */
1174 afp_errno = AFPERR_NOOBJ;
1178 if (p -n -1 < path) {
1179 afp_errno = AFPERR_PARAM;
1186 if ( d != curdir ) {
1187 n = strlen( vol->v_path );
1188 if (p -n -1 < path) {
1189 afp_errno = AFPERR_PARAM;
1194 strncpy( p, vol->v_path, n );
1196 if ( chdir( p ) < 0 ) {
1200 afp_errno = AFPERR_ACCESS;
1203 afp_errno = AFPERR_NOOBJ;
1213 * We can't use unix file's perm to support Apple's inherited protection modes.
1214 * If we aren't the file's owner we can't change its perms when moving it and smb
1215 * nfs,... don't even try.
1217 #define AFP_CHECK_ACCESS
1219 int check_access(char *path, int mode)
1221 #ifdef AFP_CHECK_ACCESS
1229 accessmode(p, &ma, curdir, NULL);
1230 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1232 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1238 /* --------------------- */
1239 int file_access(struct path *path, int mode)
1243 accessmode(path->u_name, &ma, curdir, &path->st);
1244 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1246 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1252 /* ------------------------------
1254 (name, dir) with curdir:name == dir, from afp_enumerate
1257 int getdirparams(const struct vol *vol,
1258 u_int16_t bitmap, struct path *s_path,
1260 char *buf, int *buflen )
1264 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1265 int bit = 0, isad = 0;
1270 struct stat *st = &s_path->st;
1271 char *upath = s_path->u_name;
1273 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1274 (1 << DIRPBIT_CDATE) |
1275 (1 << DIRPBIT_MDATE) |
1276 (1 << DIRPBIT_BDATE) |
1277 (1 << DIRPBIT_FINFO)))) {
1279 ad_init(&ad, vol->v_adouble);
1280 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1281 DIRBITS | 0777, &ad)) {
1287 while ( bitmap != 0 ) {
1288 while (( bitmap & 1 ) == 0 ) {
1296 ad_getattr(&ad, &ashort);
1297 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1298 && strcmp(dir->d_u_name, "..")) {
1299 ashort = htons(ATTRBIT_INVISIBLE);
1302 ashort |= htons(ATTRBIT_SHARED);
1303 memcpy( data, &ashort, sizeof( ashort ));
1304 data += sizeof( ashort );
1308 if ( dir->d_did == DIRDID_ROOT) {
1309 aint = DIRDID_ROOT_PARENT;
1310 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1313 aint = dir->d_parent->d_did;
1315 memcpy( data, &aint, sizeof( aint ));
1316 data += sizeof( aint );
1319 case DIRPBIT_CDATE :
1320 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1321 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1322 memcpy( data, &aint, sizeof( aint ));
1323 data += sizeof( aint );
1326 case DIRPBIT_MDATE :
1327 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1328 memcpy( data, &aint, sizeof( aint ));
1329 data += sizeof( aint );
1332 case DIRPBIT_BDATE :
1333 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1334 aint = AD_DATE_START;
1335 memcpy( data, &aint, sizeof( aint ));
1336 data += sizeof( aint );
1339 case DIRPBIT_FINFO :
1341 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1342 } else { /* no appledouble */
1343 memset( data, 0, 32 );
1344 /* set default view -- this also gets done in ad_open() */
1345 ashort = htons(FINDERINFO_CLOSEDVIEW);
1346 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1348 /* dot files are by default invisible */
1349 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1350 strcmp(dir->d_u_name , "..")) {
1351 ashort = htons(FINDERINFO_INVISIBLE);
1352 memcpy(data + FINDERINFO_FRFLAGOFF,
1353 &ashort, sizeof(ashort));
1359 case DIRPBIT_LNAME :
1360 if (dir->d_m_name) /* root of parent can have a null name */
1363 memset(data, 0, sizeof(u_int16_t));
1364 data += sizeof( u_int16_t );
1367 case DIRPBIT_SNAME :
1368 memset(data, 0, sizeof(u_int16_t));
1369 data += sizeof( u_int16_t );
1373 memcpy( data, &dir->d_did, sizeof( aint ));
1374 data += sizeof( aint );
1377 case DIRPBIT_OFFCNT :
1379 /* this needs to handle current directory access rights */
1380 if (st->st_ctime == dir->ctime) {
1381 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1383 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1385 dir->ctime = st->st_ctime;
1386 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1388 ashort = htons( ashort );
1389 memcpy( data, &ashort, sizeof( ashort ));
1390 data += sizeof( ashort );
1394 aint = htonl(st->st_uid);
1395 memcpy( data, &aint, sizeof( aint ));
1396 data += sizeof( aint );
1400 aint = htonl(st->st_gid);
1401 memcpy( data, &aint, sizeof( aint ));
1402 data += sizeof( aint );
1405 case DIRPBIT_ACCESS :
1406 accessmode( upath, &ma, dir , st);
1408 *data++ = ma.ma_user;
1409 *data++ = ma.ma_world;
1410 *data++ = ma.ma_group;
1411 *data++ = ma.ma_owner;
1414 /* Client has requested the ProDOS information block.
1415 Just pass back the same basic block for all
1416 directories. <shirsch@ibm.net> */
1417 case DIRPBIT_PDINFO :
1418 if (afp_version >= 30) { /* UTF8 name */
1419 utf8 = kTextEncodingUTF8;
1420 if (dir->d_m_name) /* root of parent can have a null name */
1423 memset(data, 0, sizeof(u_int16_t));
1424 data += sizeof( u_int16_t );
1426 memcpy(data, &aint, sizeof( aint ));
1427 data += sizeof( aint );
1429 else { /* ProDOS Info Block */
1432 ashort = htons( 0x0200 );
1433 memcpy( data, &ashort, sizeof( ashort ));
1434 data += sizeof( ashort );
1435 memset( data, 0, sizeof( ashort ));
1436 data += sizeof( ashort );
1440 case DIRPBIT_UNIXPR :
1441 aint = htonl(st->st_uid);
1442 memcpy( data, &aint, sizeof( aint ));
1443 data += sizeof( aint );
1444 aint = htonl(st->st_gid);
1445 memcpy( data, &aint, sizeof( aint ));
1446 data += sizeof( aint );
1449 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1450 memcpy( data, &aint, sizeof( aint ));
1451 data += sizeof( aint );
1453 accessmode( upath, &ma, dir , st);
1455 *data++ = ma.ma_user;
1456 *data++ = ma.ma_world;
1457 *data++ = ma.ma_group;
1458 *data++ = ma.ma_owner;
1464 ad_close( &ad, ADFLAGS_HF );
1466 return( AFPERR_BITMAP );
1472 ashort = htons( data - buf );
1473 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1474 data = set_name(vol, data, dir->d_m_name, dir->d_did, 0);
1476 if ( utf_nameoff ) {
1477 ashort = htons( data - buf );
1478 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1479 data = set_name(vol, data, dir->d_m_name, dir->d_did, utf8);
1482 ad_close( &ad, ADFLAGS_HF );
1484 *buflen = data - buf;
1488 /* ----------------------------- */
1489 int path_error(struct path *path, int error)
1491 /* - a dir with access error
1492 * - no error it's a file
1495 if (path_isadir(path))
1499 return AFPERR_BADTYPE ;
1502 /* ----------------------------- */
1503 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1506 int ibuflen, *rbuflen;
1511 u_int16_t vid, bitmap;
1517 memcpy( &vid, ibuf, sizeof( vid ));
1518 ibuf += sizeof( vid );
1520 if (NULL == ( vol = getvolbyvid( vid )) ) {
1521 return( AFPERR_PARAM );
1524 if (vol->v_flags & AFPVOL_RO)
1525 return AFPERR_VLOCK;
1527 memcpy( &did, ibuf, sizeof( did ));
1528 ibuf += sizeof( int );
1530 if (NULL == ( dir = dirlookup( vol, did )) ) {
1534 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1535 bitmap = ntohs( bitmap );
1536 ibuf += sizeof( bitmap );
1538 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1539 return get_afp_errno(AFPERR_NOOBJ);
1542 if ( *path->m_name != '\0' ) {
1543 return path_error(path, AFPERR_NOOBJ);
1547 * If ibuf is odd, make it even.
1549 if ((u_long)ibuf & 1 ) {
1553 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1554 setvoltime(obj, vol );
1560 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1562 * assume path == '\0' eg. it's a directory in canonical form
1565 struct path Cur_Path = {
1568 ".", /* unix name */
1569 0, /* stat is not set */
1573 int setdirparams(const struct vol *vol,
1574 struct path *path, u_int16_t bitmap, char *buf )
1582 int bit = 0, aint, isad = 1;
1583 u_int16_t ashort, bshort;
1585 int change_mdate = 0;
1586 int change_parent_mdate = 0;
1589 upath = path->u_name;
1590 ad_init(&ad, vol->v_adouble);
1592 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1593 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1595 * Check to see what we're trying to set. If it's anything
1596 * but ACCESS, UID, or GID, give an error. If it's any of those
1597 * three, we don't need the ad to be open, so just continue.
1599 * note: we also don't need to worry about mdate. also, be quiet
1600 * if we're using the noadouble option.
1602 if (!vol_noadouble(vol) && (bitmap &
1603 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1604 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1605 return AFPERR_ACCESS;
1611 * Check to see if a create was necessary. If it was, we'll want
1612 * to set our name, etc.
1614 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1615 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1616 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1617 ad_getentrylen( &ad, ADEID_NAME ));
1621 while ( bitmap != 0 ) {
1622 while (( bitmap & 1 ) == 0 ) {
1631 memcpy( &ashort, buf, sizeof( ashort ));
1632 ad_getattr(&ad, &bshort);
1633 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1634 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1638 ad_setattr(&ad, bshort);
1639 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1640 change_parent_mdate = 1;
1642 buf += sizeof( ashort );
1645 case DIRPBIT_CDATE :
1648 memcpy(&aint, buf, sizeof(aint));
1649 ad_setdate(&ad, AD_DATE_CREATE, aint);
1651 buf += sizeof( aint );
1654 case DIRPBIT_MDATE :
1655 memcpy(&newdate, buf, sizeof(newdate));
1656 buf += sizeof( newdate );
1659 case DIRPBIT_BDATE :
1662 memcpy(&aint, buf, sizeof(aint));
1663 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1665 buf += sizeof( aint );
1668 case DIRPBIT_FINFO :
1671 * Alright, we admit it, this is *really* sick!
1672 * The 4 bytes that we don't copy, when we're dealing
1673 * with the root of a volume, are the directory's
1674 * location information. This eliminates that annoying
1675 * behavior one sees when mounting above another mount
1679 if ( curdir->d_did == DIRDID_ROOT ) {
1680 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1681 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1683 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1689 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1690 change_parent_mdate = 1;
1691 memcpy( &aint, buf, sizeof(aint));
1692 buf += sizeof( aint );
1693 if ( (curdir->d_did == DIRDID_ROOT) &&
1694 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1698 err = AFPERR_ACCESS;
1699 goto setdirparam_done;
1703 goto setdirparam_done;
1706 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1710 goto setdirparam_done;
1715 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1719 err = AFPERR_ACCESS;
1720 goto setdirparam_done;
1724 goto setdirparam_done;
1727 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1734 change_parent_mdate = 1;
1735 memcpy( &aint, buf, sizeof( aint ));
1736 buf += sizeof( aint );
1737 if (curdir->d_did == DIRDID_ROOT)
1738 setdeskowner( -1, ntohl(aint) );
1740 #if 0 /* don't error if we can't set the desktop owner. */
1744 err = AFPERR_ACCESS;
1745 goto setdirparam_done;
1749 goto setdirparam_done;
1752 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1755 goto setdirparam_done;
1761 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1765 err = AFPERR_ACCESS;
1766 goto setdirparam_done;
1770 goto setdirparam_done;
1773 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1780 case DIRPBIT_ACCESS :
1782 change_parent_mdate = 1;
1783 ma.ma_user = *buf++;
1784 ma.ma_world = *buf++;
1785 ma.ma_group = *buf++;
1786 ma.ma_owner = *buf++;
1788 if (curdir->d_did == DIRDID_ROOT)
1789 setdeskmode(mtoumode( &ma ));
1790 #if 0 /* don't error if we can't set the desktop mode */
1794 err = AFPERR_ACCESS;
1795 goto setdirparam_done;
1798 goto setdirparam_done;
1800 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1804 goto setdirparam_done;
1808 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1809 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1813 err = AFPERR_ACCESS;
1814 goto setdirparam_done;
1817 goto setdirparam_done;
1819 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1822 goto setdirparam_done;
1827 /* Ignore what the client thinks we should do to the
1828 ProDOS information block. Skip over the data and
1829 report nothing amiss. <shirsch@ibm.net> */
1830 case DIRPBIT_PDINFO :
1831 if (afp_version < 30) {
1836 case DIRPBIT_UNIXPR :
1837 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1838 buf += sizeof( aint );
1839 buf += sizeof( aint );
1842 change_parent_mdate = 1;
1843 memcpy( &aint, buf, sizeof( aint ));
1844 buf += sizeof( aint );
1845 aint = ntohl (aint);
1846 if (curdir->d_did == DIRDID_ROOT)
1847 setdeskmode( aint );
1848 #if 0 /* don't error if we can't set the desktop mode */
1852 err = AFPERR_ACCESS;
1853 goto setdirparam_done;
1856 goto setdirparam_done;
1858 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1862 goto setdirparam_done;
1866 if ( setdirunixmode( aint, vol_noadouble(vol),
1867 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1871 err = AFPERR_ACCESS;
1872 goto setdirparam_done;
1875 goto setdirparam_done;
1877 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1880 goto setdirparam_done;
1886 err = AFPERR_BITMAP;
1887 goto setdirparam_done;
1896 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1897 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1901 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1902 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1907 if (path->st_valid && !path->st_errno) {
1908 ad_setid(&ad, &path->st, curdir->d_did, vol->v_stamp);
1911 ad_flush( &ad, ADFLAGS_HF );
1912 ad_close( &ad, ADFLAGS_HF );
1915 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1916 && gettimeofday(&tv, NULL) == 0) {
1917 if (!movecwd(vol, curdir->d_parent)) {
1918 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1919 bitmap = 1<<DIRPBIT_MDATE;
1920 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1921 /* should we reset curdir ?*/
1928 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1931 int ibuflen, *rbuflen;
1937 struct path *s_path;
1945 memcpy( &vid, ibuf, sizeof( vid ));
1946 ibuf += sizeof( vid );
1947 if (NULL == ( vol = getvolbyvid( vid )) ) {
1948 return( AFPERR_PARAM );
1951 if (vol->v_flags & AFPVOL_RO)
1952 return AFPERR_VLOCK;
1954 memcpy( &did, ibuf, sizeof( did ));
1955 ibuf += sizeof( did );
1956 if (NULL == ( dir = dirlookup( vol, did )) ) {
1957 return afp_errno; /* was AFPERR_NOOBJ */
1959 /* for concurrent access we need to be sure we are not in the
1960 * folder we want to create...
1964 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1965 return get_afp_errno(AFPERR_PARAM);
1967 /* cname was able to move curdir to it! */
1968 if (*s_path->m_name == '\0')
1969 return AFPERR_EXIST;
1971 upath = s_path->u_name;
1972 if (0 != (err = check_name(vol, upath))) {
1976 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1980 if (of_stat(s_path) < 0) {
1984 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1988 if ( movecwd( vol, dir ) < 0 ) {
1989 return( AFPERR_PARAM );
1992 ad_init(&ad, vol->v_adouble);
1993 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1994 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1995 if (vol_noadouble(vol))
1996 goto createdir_done;
1997 return( AFPERR_ACCESS );
2000 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
2001 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
2002 ad_getentrylen( &ad, ADEID_NAME ));
2003 ad_setid( &ad, &s_path->st, dir->d_did, vol->v_stamp);
2004 ad_flush( &ad, ADFLAGS_HF );
2005 ad_close( &ad, ADFLAGS_HF );
2008 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2009 *rbuflen = sizeof( u_int32_t );
2010 setvoltime(obj, vol );
2015 * dst new unix filename (not a pathname)
2016 * newname new mac name
2020 int renamedir(src, dst, dir, newparent, newname, noadouble)
2021 char *src, *dst, *newname;
2022 struct dir *dir, *newparent;
2023 const int noadouble;
2030 /* existence check moved to afp_moveandrename */
2031 if ( unix_rename( src, dst ) < 0 ) {
2034 return( AFPERR_NOOBJ );
2036 return( AFPERR_ACCESS );
2038 return AFPERR_VLOCK;
2040 /* tried to move directory into a subdirectory of itself */
2041 return AFPERR_CANTMOVE;
2043 /* this needs to copy and delete. bleah. that means we have
2044 * to deal with entire directory hierarchies. */
2045 if ((err = copydir(src, dst, noadouble)) < 0) {
2049 if ((err = deletedir(src)) < 0)
2053 return( AFPERR_PARAM );
2057 ad_init(&ad, 0); /* FIXME */
2059 len = strlen( newname );
2060 /* rename() succeeded so we need to update our tree even if we can't open
2063 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2064 ad_setentrylen( &ad, ADEID_NAME, len );
2065 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2066 ad_flush( &ad, ADFLAGS_HF );
2067 ad_close( &ad, ADFLAGS_HF );
2070 if (dir->d_m_name == dir->d_u_name)
2071 dir->d_u_name = NULL;
2073 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2074 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2075 /* FIXME : fatal ? */
2078 dir->d_m_name = buf;
2079 strcpy( dir->d_m_name, newname );
2081 if (newname == dst) {
2082 free(dir->d_u_name);
2083 dir->d_u_name = dir->d_m_name;
2086 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2087 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2090 dir->d_u_name = buf;
2091 strcpy( dir->d_u_name, dst );
2094 if (( parent = dir->d_parent ) == NULL ) {
2097 if ( parent == newparent ) {
2101 /* detach from old parent and add to new one. */
2102 dirchildremove(parent, dir);
2103 dir->d_parent = newparent;
2104 dirchildadd(newparent, dir);
2108 #define DOT_APPLEDOUBLE_LEN 13
2109 /* delete an empty directory */
2110 int deletecurdir( vol, path, pathlen )
2111 const struct vol *vol;
2123 if ( curdir->d_parent == NULL ) {
2124 return( AFPERR_ACCESS );
2129 ad_init(&ad, vol->v_adouble);
2130 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2131 DIRBITS | 0777, &ad) == 0 ) {
2133 ad_getattr(&ad, &ashort);
2134 ad_close( &ad, ADFLAGS_HF );
2135 if ((ashort & htons(ATTRBIT_NODELETE))) {
2136 return AFPERR_OLOCK;
2140 /* delete stray .AppleDouble files. this happens to get .Parent files
2142 if ((dp = opendir(".AppleDouble"))) {
2143 strcpy(path, ".AppleDouble/");
2144 while ((de = readdir(dp))) {
2145 /* skip this and previous directory */
2146 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2149 /* bail if the file exists in the current directory.
2150 * note: this will not fail with dangling symlinks */
2151 if (stat(de->d_name, &st) == 0) {
2153 return AFPERR_DIRNEMPT;
2156 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2157 if ((err = netatalk_unlink(path))) {
2165 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2169 /* now get rid of dangling symlinks */
2170 if ((dp = opendir("."))) {
2171 while ((de = readdir(dp))) {
2172 /* skip this and previous directory */
2173 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2176 /* bail if it's not a symlink */
2177 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2179 return AFPERR_DIRNEMPT;
2182 if ((err = netatalk_unlink(de->d_name))) {
2189 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2194 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2195 dirchildremove(curdir, fdir);
2196 cnid_delete(vol->v_cdb, fdir->d_did);
2197 dir_remove( vol, fdir );
2202 /* inode is used as key for cnid.
2203 * Close the descriptor only after cnid_delete
2211 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2214 int ibuflen, *rbuflen;
2224 sfunc = (unsigned char) *ibuf++;
2225 memcpy( &id, ibuf, sizeof( id ));
2230 if (sfunc == 3 || sfunc == 4) {
2231 if (afp_version < 30) {
2232 return( AFPERR_PARAM );
2239 case 3 :/* unicode */
2240 if (( pw = getpwuid( id )) == NULL ) {
2241 return( AFPERR_NOITEM );
2247 case 4 : /* unicode */
2248 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2249 return( AFPERR_NOITEM );
2255 return( AFPERR_PARAM );
2257 len = strlen( name );
2264 u_int16_t tp = htons(len);
2265 memcpy(rbuf, &tp, sizeof(tp));
2274 memcpy( rbuf, name, len );
2280 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2283 int ibuflen, *rbuflen;
2292 sfunc = (unsigned char) *ibuf++;
2296 case 2 : /* unicode */
2297 if (afp_version < 30) {
2298 return( AFPERR_PARAM );
2300 memcpy(&ulen, ibuf, sizeof(ulen));
2306 len = (unsigned char) *ibuf++;
2309 return( AFPERR_PARAM );
2316 case 1 : /* unicode */
2318 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2319 return( AFPERR_NOITEM );
2324 case 2 : /* unicode */
2326 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2327 return( AFPERR_NOITEM );
2336 memcpy( rbuf, &id, sizeof( id ));
2337 *rbuflen = sizeof( id );
2341 /* ------------------------------------
2342 variable DID support
2344 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2347 int ibuflen, *rbuflen;
2358 /* do nothing as dids are static for the life of the process. */
2362 memcpy(&vid, ibuf, sizeof( vid ));
2363 ibuf += sizeof( vid );
2364 if (( vol = getvolbyvid( vid )) == NULL ) {
2365 return( AFPERR_PARAM );
2368 memcpy( &did, ibuf, sizeof( did ));
2369 ibuf += sizeof( did );
2370 if (( dir = dirlookup( vol, did )) == NULL ) {
2371 return( AFPERR_PARAM );
2374 /* dir_remove -- deletedid */
2380 /* did creation gets done automatically
2381 * there's a pb again with case but move it to cname
2383 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2386 int ibuflen, *rbuflen;
2389 struct dir *parentdir;
2397 memcpy(&vid, ibuf, sizeof(vid));
2398 ibuf += sizeof( vid );
2400 if (NULL == ( vol = getvolbyvid( vid )) ) {
2401 return( AFPERR_PARAM );
2404 memcpy(&did, ibuf, sizeof(did));
2405 ibuf += sizeof(did);
2407 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2411 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2412 return get_afp_errno(AFPERR_PARAM);
2415 if ( *path->m_name != '\0' ) {
2416 return path_error(path, AFPERR_NOOBJ);
2419 if ( !path->st_valid && of_stat(path ) < 0 ) {
2420 return( AFPERR_NOOBJ );
2422 if ( path->st_errno ) {
2423 return( AFPERR_NOOBJ );
2426 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2427 *rbuflen = sizeof(curdir->d_did);