2 * $Id: directory.c,v 1.76 2003-06-06 20:36:59 srittau 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>
26 #include <atalk/cnid.h>
34 #endif /* HAVE_FCNTL_H */
41 #else /* STDC_HEADERS */
45 #endif /* HAVE_STRCHR */
46 char *strchr (), *strrchr ();
48 #define memcpy(d,s,n) bcopy ((s), (d), (n))
49 #define memmove(d,s,n) bcopy ((s), (d), (n))
50 #endif /* ! HAVE_MEMCPY */
51 #endif /* STDC_HEADERS */
53 #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 /* ------------------- */
125 int get_afp_errno(const int param)
127 if (afp_errno != AFPERR_DID1)
132 /* ------------------- */
134 dirsearch_byname( cdir, name )
140 if (!strcmp(name, "."))
144 if ( strcmp( dir->d_u_name, name ) == 0 ) {
147 dir = (dir == cdir->d_child->d_prev) ? NULL : dir->d_next;
152 /* -----------------------------------------
153 * if did is not in the cache resolve it with cnid
156 * OSX call it with bogus id, ie file ID not folder ID,
157 * and we are really bad in this case.
160 dirlookup( vol, did )
161 const struct vol *vol;
168 static char path[MAXPATHLEN + 1];
171 static char buffer[12 + MAXPATHLEN + 1];
172 int buflen = 12 + MAXPATHLEN + 1;
177 ret = dirsearch(vol, did);
178 if (ret != NULL || afp_errno == AFPERR_PARAM)
181 utf8 = utf8_encoding();
182 maxpath = (utf8)?MAXPATHLEN -7:255;
184 if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) ) {
185 afp_errno = AFPERR_NOOBJ;
188 ptr = path + MAXPATHLEN;
189 if (NULL == ( mpath = utompath(vol, upath, utf8) ) ) {
190 afp_errno = AFPERR_NOOBJ;
194 pathlen = len; /* no 0 in the last part */
196 strcpy(ptr - len, mpath);
199 ret = dirsearch(vol,id);
203 if ( NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen))
205 NULL == (mpath = utompath(vol, upath, utf8))
207 afp_errno = AFPERR_NOOBJ;
211 len = strlen(mpath) + 1;
213 if (pathlen > maxpath) {
214 afp_errno = AFPERR_PARAM;
217 strcpy(ptr - len, mpath);
221 /* fill the cache, another place where we know about the path type */
227 temp16 = htons(pathlen);
228 memcpy(ptr, &temp16, sizeof(temp16));
230 temp = htonl(kTextEncodingUTF8);
232 memcpy(ptr, &temp, sizeof(temp));
238 *ptr = (unsigned char)pathlen;
242 /* cname is not efficient */
243 if (cname( vol, ret, &ptr ) == NULL )
246 return dirsearch(vol, did);
249 /* --------------------------- */
250 /* rotate the tree to the left */
251 static void dir_leftrotate(vol, dir)
255 struct dir *right = dir->d_right;
257 /* whee. move the right's left tree into dir's right tree */
258 dir->d_right = right->d_left;
259 if (right->d_left != SENTINEL)
260 right->d_left->d_back = dir;
262 if (right != SENTINEL) {
263 right->d_back = dir->d_back;
267 if (!dir->d_back) /* no parent. move the right tree to the top. */
269 else if (dir == dir->d_back->d_left) /* we were on the left */
270 dir->d_back->d_left = right;
272 dir->d_back->d_right = right; /* we were on the right */
274 /* re-insert dir on the left tree */
281 /* rotate the tree to the right */
282 static void dir_rightrotate(vol, dir)
286 struct dir *left = dir->d_left;
288 /* whee. move the left's right tree into dir's left tree */
289 dir->d_left = left->d_right;
290 if (left->d_right != SENTINEL)
291 left->d_right->d_back = dir;
293 if (left != SENTINEL) {
294 left->d_back = dir->d_back;
298 if (!dir->d_back) /* no parent. move the left tree to the top. */
300 else if (dir == dir->d_back->d_right) /* we were on the right */
301 dir->d_back->d_right = left;
303 dir->d_back->d_left = left; /* we were on the left */
305 /* re-insert dir on the right tree */
311 /* recolor after a removal */
312 static struct dir *dir_rmrecolor(vol, dir)
318 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
319 /* are we on the left tree? */
320 if (dir == dir->d_back->d_left) {
321 leaf = dir->d_back->d_right; /* get right side */
322 if (leaf->d_color == DIRTREE_COLOR_RED) {
323 /* we're red. we need to change to black. */
324 leaf->d_color = DIRTREE_COLOR_BLACK;
325 dir->d_back->d_color = DIRTREE_COLOR_RED;
326 dir_leftrotate(vol, dir->d_back);
327 leaf = dir->d_back->d_right;
330 /* right leaf has black end nodes */
331 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
332 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
333 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
334 dir = dir->d_back; /* ascend */
336 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
337 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
338 leaf->d_color = DIRTREE_COLOR_RED;
339 dir_rightrotate(vol, leaf);
340 leaf = dir->d_back->d_right;
342 leaf->d_color = dir->d_back->d_color;
343 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
344 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
345 dir_leftrotate(vol, dir->d_back);
348 } else { /* right tree */
349 leaf = dir->d_back->d_left; /* left tree */
350 if (leaf->d_color == DIRTREE_COLOR_RED) {
351 leaf->d_color = DIRTREE_COLOR_BLACK;
352 dir->d_back->d_color = DIRTREE_COLOR_RED;
353 dir_rightrotate(vol, dir->d_back);
354 leaf = dir->d_back->d_left;
357 /* left leaf has black end nodes */
358 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
359 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
360 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
361 dir = dir->d_back; /* ascend */
363 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
364 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
365 leaf->d_color = DIRTREE_COLOR_RED;
366 dir_leftrotate(vol, leaf);
367 leaf = dir->d_back->d_left;
369 leaf->d_color = dir->d_back->d_color;
370 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
371 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
372 dir_rightrotate(vol, dir->d_back);
377 dir->d_color = DIRTREE_COLOR_BLACK;
384 /* remove the node from the tree. this is just like insertion, but
385 * different. actually, it has to worry about a bunch of things that
386 * insertion doesn't care about. */
387 static void dir_remove( vol, dir )
392 struct ofork *of, *last;
393 struct dir *node, *leaf;
394 #endif /* REMOVE_NODES */
396 if (!dir || (dir == SENTINEL))
399 /* i'm not sure if it really helps to delete stuff. */
402 dir->d_m_name = NULL;
403 dir->d_u_name = NULL;
404 #else /* ! REMOVE_NODES */
406 /* go searching for a node with at most one child */
407 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
411 while (node->d_left != SENTINEL)
416 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
419 leaf->d_back = node->d_back;
422 } else if (node == node->d_back->d_left) { /* left tree */
423 node->d_back->d_left = leaf;
425 node->d_back->d_right = leaf;
428 /* we want to free node, but we also want to free the data in dir.
429 * currently, that's d_name and the directory traversal bits.
430 * we just copy the necessary bits and then fix up all the
431 * various pointers to the directory. needless to say, there are
432 * a bunch of places that store the directory struct. */
434 struct dir save, *tmp;
436 memcpy(&save, dir, sizeof(save));
437 memcpy(dir, node, sizeof(struct dir));
439 /* restore the red-black bits */
440 dir->d_left = save.d_left;
441 dir->d_right = save.d_right;
442 dir->d_back = save.d_back;
443 dir->d_color = save.d_color;
445 if (node == vol->v_dir) {/* we may need to fix up this pointer */
447 rootpar.d_child = vol->v_dir;
449 /* if we aren't the root directory, we have parents and
450 * siblings to worry about */
451 if (dir->d_parent->d_child == node)
452 dir->d_parent->d_child = dir;
453 dir->d_next->d_prev = dir;
454 dir->d_prev->d_next = dir;
457 /* fix up children. */
461 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
464 if (node == curdir) /* another pointer to fixup */
467 /* we also need to fix up oforks. bleah */
468 if ((of = dir->d_ofork)) {
469 last = of->of_d_prev;
472 of = (last == of) ? NULL : of->of_d_next;
476 /* set the node's d_name */
477 node->d_m_name = save.d_m_name;
478 node->d_u_name = save.d_u_name;
481 if (node->d_color == DIRTREE_COLOR_BLACK)
482 dir_rmrecolor(vol, leaf);
484 if (node->d_u_name != node->d_m_name) {
485 free(node->d_u_name);
487 free(node->d_m_name);
489 #endif /* ! REMOVE_NODES */
492 /* ---------------------------------------
493 * remove the node and its childs from the tree
495 * FIXME what about opened forks with refs to it?
496 * it's an afp specs violation because you can't delete
497 * an opened forks. Now afpd doesn't care about forks opened by other
498 * process. It's fixable within afpd if fnctl_lock, doable with smb and
499 * next to impossible for nfs and local filesystem access.
501 static void dir_invalidate( vol, dir )
502 const struct vol *vol;
506 /* v_root can't be deleted */
507 if (movecwd(vol, vol->v_root) < 0) {
508 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
512 dirchildremove(dir->d_parent, dir);
513 dir_remove( vol, dir );
516 /* ------------------------------------ */
517 static struct dir *dir_insert(vol, dir)
518 const struct vol *vol;
524 while (pdir->d_did != dir->d_did ) {
525 if ( pdir->d_did > dir->d_did ) {
526 if ( pdir->d_left == SENTINEL ) {
533 if ( pdir->d_right == SENTINEL ) {
538 pdir = pdir->d_right;
546 * attempt to extend the current dir. tree to include path
547 * as a side-effect, movecwd to that point and return the new dir
550 extenddir( vol, dir, path )
555 path->u_name = mtoupath(vol, path->m_name, utf8_encoding() );
558 if ( path->u_name == NULL) {
559 afp_errno = AFPERR_PARAM;
562 if (of_stat( path ) != 0 ) {
566 if (!S_ISDIR(path->st.st_mode)) {
570 if (( dir = adddir( vol, dir, path)) == NULL ) {
575 if ( movecwd( vol, dir ) < 0 ) {
582 /* -------------------
583 system rmdir with afp error code.
584 ENOENT is not an error.
586 static int netatalk_rmdir(const char *name)
588 if (rmdir(name) < 0) {
593 return AFPERR_DIRNEMPT;
596 return AFPERR_ACCESS;
606 /* -------------------------
607 appledouble mkdir afp error code.
609 static int netatalk_mkdir(const char *name)
611 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
614 return( AFPERR_NOOBJ );
616 return( AFPERR_VLOCK );
619 return( AFPERR_ACCESS );
621 return( AFPERR_EXIST );
624 return( AFPERR_DFULL );
626 return( AFPERR_PARAM );
632 /* -------------------
633 system unlink with afp error code.
634 ENOENT is not an error.
636 int netatalk_unlink(const char *name)
638 if (unlink(name) < 0) {
646 return AFPERR_ACCESS;
654 /* ------------------- */
655 static int deletedir(char *dir)
657 char path[MAXPATHLEN + 1];
665 if ((len = strlen(dir)) +2 > sizeof(path))
669 if ((dp = opendir(dir)) == NULL)
675 remain = sizeof(path) -len -1;
676 while ((de = readdir(dp)) && err == AFP_OK) {
677 /* skip this and previous directory */
678 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
681 if (strlen(de->d_name) > remain) {
685 strcpy(path + len, de->d_name);
686 if (stat(path, &st)) {
689 if (S_ISDIR(st.st_mode)) {
690 err = deletedir(path);
692 err = netatalk_unlink(path);
697 /* okay. the directory is empty. delete it. note: we already got rid
700 err = netatalk_rmdir(dir);
705 /* do a recursive copy. */
706 static int copydir(char *src, char *dst, int noadouble)
708 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
718 /* doesn't exist or the path is too long. */
719 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
720 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
721 ((dp = opendir(src)) == NULL))
724 /* try to create the destination directory */
725 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
730 /* set things up to copy */
734 srem = sizeof(spath) - slen -1;
739 drem = sizeof(dpath) - dlen -1;
742 while ((de = readdir(dp))) {
743 /* skip this and previous directory */
744 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
747 if (strlen(de->d_name) > srem) {
751 strcpy(spath + slen, de->d_name);
753 if (stat(spath, &st) == 0) {
754 if (strlen(de->d_name) > drem) {
758 strcpy(dpath + dlen, de->d_name);
760 if (S_ISDIR(st.st_mode)) {
761 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
763 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
767 /* keep the same time stamp. */
768 ut.actime = ut.modtime = st.st_mtime;
774 /* keep the same time stamp. */
775 if (stat(src, &st) == 0) {
776 ut.actime = ut.modtime = st.st_mtime;
786 /* --- public functions follow --- */
788 /* NOTE: we start off with at least one node (the root directory). */
789 struct dir *dirinsert( vol, dir )
795 if ((node = dir_insert(vol, dir)))
798 /* recolor the tree. the current node is red. */
799 dir->d_color = DIRTREE_COLOR_RED;
801 /* parent of this node has to be black. if the parent node
802 * is red, then we have a grandparent. */
803 while ((dir != vol->v_root) &&
804 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
805 /* are we on the left tree? */
806 if (dir->d_back == dir->d_back->d_back->d_left) {
807 node = dir->d_back->d_back->d_right; /* get the right node */
808 if (node->d_color == DIRTREE_COLOR_RED) {
809 /* we're red. we need to change to black. */
810 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
811 node->d_color = DIRTREE_COLOR_BLACK;
812 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
813 dir = dir->d_back->d_back; /* finished. go up. */
815 if (dir == dir->d_back->d_right) {
817 dir_leftrotate(vol, dir);
819 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
820 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
821 dir_rightrotate(vol, dir->d_back->d_back);
824 node = dir->d_back->d_back->d_left;
825 if (node->d_color == DIRTREE_COLOR_RED) {
826 /* we're red. we need to change to black. */
827 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
828 node->d_color = DIRTREE_COLOR_BLACK;
829 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
830 dir = dir->d_back->d_back; /* finished. ascend */
832 if (dir == dir->d_back->d_left) {
834 dir_rightrotate(vol, dir);
836 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
837 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
838 dir_leftrotate(vol, dir->d_back->d_back);
843 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
847 /* free everything down. we don't bother to recolor as this is only
848 * called to free the entire tree */
849 void dirfreename(struct dir *dir)
851 if (dir->d_u_name != dir->d_m_name) {
860 if (!dir || (dir == SENTINEL))
863 if ( dir->d_left != SENTINEL ) {
864 dirfree( dir->d_left );
866 if ( dir->d_right != SENTINEL ) {
867 dirfree( dir->d_right );
870 if (dir != SENTINEL) {
876 /* --------------------------------------------
877 * most of the time mac name and unix name are the same
879 struct dir *dirnew(const char *m_name, const char *u_name)
883 dir = (struct dir *) calloc(1, sizeof( struct dir ));
887 if ((dir->d_m_name = strdup(m_name)) == NULL) {
892 if (m_name == u_name) {
893 dir->d_u_name = dir->d_m_name;
895 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
901 dir->d_left = dir->d_right = SENTINEL;
902 dir->d_next = dir->d_prev = dir;
906 /* -------------------------------------------------- */
912 stat the file or errno
915 curdir: filename parent directory
921 stat the dir or errno
925 curdir: dir parent directory
933 curdir: dir parent directory
940 cname( vol, dir, cpath )
941 const struct vol *vol;
946 static char path[ MAXPATHLEN + 1];
947 static struct path ret;
958 afp_errno = AFPERR_NOOBJ;
959 switch (ret.m_type = *data) { /* path type */
962 len = (unsigned char) *data++;
967 if (afp_version >= 30) {
969 memcpy(&hint, data, sizeof(hint));
971 data += sizeof(hint);
973 memcpy(&len16, data, sizeof(len16));
980 /* else it's an error */
982 afp_errno = AFPERR_PARAM;
985 *cpath += len + size;
993 if (movecwd( vol, dir ) < 0 ) {
995 movecwd failed some of dir path are not there anymore.
996 FIXME Is it true with other errors?
997 so we remove dir from the cache
999 if (dir->d_did == DIRDID_ROOT_PARENT)
1001 if (afp_errno == AFPERR_ACCESS) {
1002 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1005 ret.m_name = dir->d_m_name;
1006 ret.u_name = dir->d_u_name;
1009 } else if (afp_errno == AFPERR_NOOBJ) {
1010 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1013 strcpy(path, dir->d_m_name);
1014 if (dir->d_m_name == dir->d_u_name) {
1018 size_t tp = strlen(path)+1;
1020 strcpy(path +tp, dir->d_u_name);
1021 ret.u_name = path +tp;
1024 dir_invalidate(vol, dir);
1027 dir_invalidate(vol, dir);
1030 if (*path == '\0') {
1037 if (*data == sep ) {
1041 while (*data == sep && len > 0 ) {
1042 if ( dir->d_parent == NULL ) {
1045 dir = dir->d_parent;
1050 /* would this be faster with strlen + strncpy? */
1052 while ( *data != sep && len > 0 ) {
1054 if (p > &path[ MAXPATHLEN]) {
1055 afp_errno = AFPERR_PARAM;
1061 /* short cut bits by chopping off a trailing \0. this also
1062 makes the traversal happy w/ filenames at the end of the
1069 if ( p != path ) { /* we got something */
1071 cdir = dir->d_child;
1073 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1076 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1079 if ( cdir == NULL ) {
1081 /* if dir == curdir it always succeed,
1082 even if curdir is deleted.
1083 it's not a pb because it will fail in extenddir
1085 if ( movecwd( vol, dir ) < 0 ) {
1086 /* dir is not valid anymore
1087 we delete dir from the cache and abort.
1089 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1090 afp_errno = AFPERR_NOOBJ;
1093 if (afp_errno == AFPERR_ACCESS)
1095 dir_invalidate(vol, dir);
1098 cdir = extenddir( vol, dir, &ret );
1102 cdir = extenddir( vol, dir, &ret );
1105 if ( cdir == NULL ) {
1107 if ( len > 0 || !ret.u_name) {
1120 * Move curdir to dir, with a possible chdir()
1122 int movecwd( vol, dir)
1123 const struct vol *vol;
1126 char path[MAXPATHLEN + 1];
1131 if ( dir == curdir ) {
1134 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1135 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1139 p = path + sizeof(path) - 1;
1142 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1145 /* parent directory is deleted */
1146 afp_errno = AFPERR_NOOBJ;
1150 if (p -n -1 < path) {
1151 afp_errno = AFPERR_PARAM;
1158 if ( d != curdir ) {
1159 n = strlen( vol->v_path );
1160 if (p -n -1 < path) {
1161 afp_errno = AFPERR_PARAM;
1166 strncpy( p, vol->v_path, n );
1168 if ( chdir( p ) < 0 ) {
1172 afp_errno = AFPERR_ACCESS;
1175 afp_errno = AFPERR_NOOBJ;
1185 * We can't use unix file's perm to support Apple's inherited protection modes.
1186 * If we aren't the file's owner we can't change its perms when moving it and smb
1187 * nfs,... don't even try.
1189 #define AFP_CHECK_ACCESS
1191 int check_access(char *path, int mode)
1193 #ifdef AFP_CHECK_ACCESS
1201 accessmode(p, &ma, curdir, NULL);
1202 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1204 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1210 /* --------------------- */
1211 int file_access(struct path *path, int mode)
1215 accessmode(path->u_name, &ma, curdir, &path->st);
1216 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1218 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1224 /* ------------------------------
1226 (name, dir) with curdir:name == dir, from afp_enumerate
1229 int getdirparams(const struct vol *vol,
1230 u_int16_t bitmap, struct path *s_path,
1232 char *buf, int *buflen )
1236 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1237 int bit = 0, isad = 0;
1242 struct stat *st = &s_path->st;
1243 char *upath = s_path->u_name;
1245 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1246 (1 << DIRPBIT_CDATE) |
1247 (1 << DIRPBIT_MDATE) |
1248 (1 << DIRPBIT_BDATE) |
1249 (1 << DIRPBIT_FINFO)))) {
1250 memset(&ad, 0, sizeof(ad));
1251 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1252 DIRBITS | 0777, &ad)) {
1258 while ( bitmap != 0 ) {
1259 while (( bitmap & 1 ) == 0 ) {
1267 ad_getattr(&ad, &ashort);
1268 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1269 && strcmp(dir->d_u_name, "..")) {
1270 ashort = htons(ATTRBIT_INVISIBLE);
1273 ashort |= htons(ATTRBIT_SHARED);
1274 memcpy( data, &ashort, sizeof( ashort ));
1275 data += sizeof( ashort );
1279 if ( dir->d_did == DIRDID_ROOT) {
1280 aint = DIRDID_ROOT_PARENT;
1281 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1284 aint = dir->d_parent->d_did;
1286 memcpy( data, &aint, sizeof( aint ));
1287 data += sizeof( aint );
1290 case DIRPBIT_CDATE :
1291 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1292 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1293 memcpy( data, &aint, sizeof( aint ));
1294 data += sizeof( aint );
1297 case DIRPBIT_MDATE :
1298 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1299 memcpy( data, &aint, sizeof( aint ));
1300 data += sizeof( aint );
1303 case DIRPBIT_BDATE :
1304 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1305 aint = AD_DATE_START;
1306 memcpy( data, &aint, sizeof( aint ));
1307 data += sizeof( aint );
1310 case DIRPBIT_FINFO :
1312 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1313 } else { /* no appledouble */
1314 memset( data, 0, 32 );
1315 /* set default view -- this also gets done in ad_open() */
1316 ashort = htons(FINDERINFO_CLOSEDVIEW);
1317 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1319 /* dot files are by default invisible */
1320 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1321 strcmp(dir->d_u_name , "..")) {
1322 ashort = htons(FINDERINFO_INVISIBLE);
1323 memcpy(data + FINDERINFO_FRFLAGOFF,
1324 &ashort, sizeof(ashort));
1330 case DIRPBIT_LNAME :
1331 if (dir->d_m_name) /* root of parent can have a null name */
1334 memset(data, 0, sizeof(u_int16_t));
1335 data += sizeof( u_int16_t );
1338 case DIRPBIT_SNAME :
1339 memset(data, 0, sizeof(u_int16_t));
1340 data += sizeof( u_int16_t );
1344 memcpy( data, &dir->d_did, sizeof( aint ));
1345 data += sizeof( aint );
1348 case DIRPBIT_OFFCNT :
1350 /* this needs to handle current directory access rights */
1351 if (st->st_ctime == dir->ctime) {
1352 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1354 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1356 dir->ctime = st->st_ctime;
1357 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1359 ashort = htons( ashort );
1360 memcpy( data, &ashort, sizeof( ashort ));
1361 data += sizeof( ashort );
1365 aint = htonl(st->st_uid);
1366 memcpy( data, &aint, sizeof( aint ));
1367 data += sizeof( aint );
1371 aint = htonl(st->st_gid);
1372 memcpy( data, &aint, sizeof( aint ));
1373 data += sizeof( aint );
1376 case DIRPBIT_ACCESS :
1377 accessmode( upath, &ma, dir , st);
1379 *data++ = ma.ma_user;
1380 *data++ = ma.ma_world;
1381 *data++ = ma.ma_group;
1382 *data++ = ma.ma_owner;
1385 /* Client has requested the ProDOS information block.
1386 Just pass back the same basic block for all
1387 directories. <shirsch@ibm.net> */
1388 case DIRPBIT_PDINFO :
1389 if (afp_version >= 30) { /* UTF8 name */
1390 utf8 = kTextEncodingUTF8;
1391 if (dir->d_m_name) /* root of parent can have a null name */
1394 memset(data, 0, sizeof(u_int16_t));
1395 data += sizeof( u_int16_t );
1397 memcpy(data, &aint, sizeof( aint ));
1398 data += sizeof( aint );
1400 else { /* ProDOS Info Block */
1403 ashort = htons( 0x0200 );
1404 memcpy( data, &ashort, sizeof( ashort ));
1405 data += sizeof( ashort );
1406 memset( data, 0, sizeof( ashort ));
1407 data += sizeof( ashort );
1411 case DIRPBIT_UNIXPR :
1412 aint = htonl(st->st_uid);
1413 memcpy( data, &aint, sizeof( aint ));
1414 data += sizeof( aint );
1415 aint = htonl(st->st_gid);
1416 memcpy( data, &aint, sizeof( aint ));
1417 data += sizeof( aint );
1420 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1421 memcpy( data, &aint, sizeof( aint ));
1422 data += sizeof( aint );
1424 accessmode( upath, &ma, dir , st);
1426 *data++ = ma.ma_user;
1427 *data++ = ma.ma_world;
1428 *data++ = ma.ma_group;
1429 *data++ = ma.ma_owner;
1435 ad_close( &ad, ADFLAGS_HF );
1437 return( AFPERR_BITMAP );
1443 ashort = htons( data - buf );
1444 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1445 data = set_name(vol, data, dir->d_m_name, 0);
1447 if ( utf_nameoff ) {
1448 ashort = htons( data - buf );
1449 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1450 data = set_name(vol, data, dir->d_m_name, utf8);
1453 ad_close( &ad, ADFLAGS_HF );
1455 *buflen = data - buf;
1459 /* ----------------------------- */
1460 int path_error(struct path *path, int error)
1462 /* - a dir with access error
1463 * - no error it's a file
1466 if (path_isadir(path))
1470 return AFPERR_BADTYPE ;
1473 /* ----------------------------- */
1474 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1477 int ibuflen, *rbuflen;
1482 u_int16_t vid, bitmap;
1488 memcpy( &vid, ibuf, sizeof( vid ));
1489 ibuf += sizeof( vid );
1491 if (NULL == ( vol = getvolbyvid( vid )) ) {
1492 return( AFPERR_PARAM );
1495 if (vol->v_flags & AFPVOL_RO)
1496 return AFPERR_VLOCK;
1498 memcpy( &did, ibuf, sizeof( did ));
1499 ibuf += sizeof( int );
1501 if (NULL == ( dir = dirlookup( vol, did )) ) {
1505 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1506 bitmap = ntohs( bitmap );
1507 ibuf += sizeof( bitmap );
1509 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1510 return get_afp_errno(AFPERR_NOOBJ);
1513 if ( *path->m_name != '\0' ) {
1514 return path_error(path, AFPERR_NOOBJ);
1518 * If ibuf is odd, make it even.
1520 if ((u_long)ibuf & 1 ) {
1524 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1525 setvoltime(obj, vol );
1531 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1533 * assume path == '\0' eg. it's a directory in canonical form
1536 struct path Cur_Path = {
1539 ".", /* unix name */
1540 0, /* stat is not set */
1544 int setdirparams(const struct vol *vol,
1545 struct path *path, u_int16_t bitmap, char *buf )
1553 int bit = 0, aint, isad = 1;
1554 u_int16_t ashort, bshort;
1556 int change_mdate = 0;
1557 int change_parent_mdate = 0;
1560 upath = path->u_name;
1561 memset(&ad, 0, sizeof(ad));
1563 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1564 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1566 * Check to see what we're trying to set. If it's anything
1567 * but ACCESS, UID, or GID, give an error. If it's any of those
1568 * three, we don't need the ad to be open, so just continue.
1570 * note: we also don't need to worry about mdate. also, be quiet
1571 * if we're using the noadouble option.
1573 if (!vol_noadouble(vol) && (bitmap &
1574 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1575 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1576 return AFPERR_ACCESS;
1582 * Check to see if a create was necessary. If it was, we'll want
1583 * to set our name, etc.
1585 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1586 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1587 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1588 ad_getentrylen( &ad, ADEID_NAME ));
1592 while ( bitmap != 0 ) {
1593 while (( bitmap & 1 ) == 0 ) {
1602 memcpy( &ashort, buf, sizeof( ashort ));
1603 ad_getattr(&ad, &bshort);
1604 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1605 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1609 ad_setattr(&ad, bshort);
1610 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1611 change_parent_mdate = 1;
1613 buf += sizeof( ashort );
1616 case DIRPBIT_CDATE :
1619 memcpy(&aint, buf, sizeof(aint));
1620 ad_setdate(&ad, AD_DATE_CREATE, aint);
1622 buf += sizeof( aint );
1625 case DIRPBIT_MDATE :
1626 memcpy(&newdate, buf, sizeof(newdate));
1627 buf += sizeof( newdate );
1630 case DIRPBIT_BDATE :
1633 memcpy(&aint, buf, sizeof(aint));
1634 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1636 buf += sizeof( aint );
1639 case DIRPBIT_FINFO :
1642 * Alright, we admit it, this is *really* sick!
1643 * The 4 bytes that we don't copy, when we're dealing
1644 * with the root of a volume, are the directory's
1645 * location information. This eliminates that annoying
1646 * behavior one sees when mounting above another mount
1650 if ( curdir->d_did == DIRDID_ROOT ) {
1651 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1652 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1654 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1660 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1661 change_parent_mdate = 1;
1662 memcpy( &aint, buf, sizeof(aint));
1663 buf += sizeof( aint );
1664 if ( (curdir->d_did == DIRDID_ROOT) &&
1665 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1669 err = AFPERR_ACCESS;
1670 goto setdirparam_done;
1674 goto setdirparam_done;
1677 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1681 goto setdirparam_done;
1686 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1690 err = AFPERR_ACCESS;
1691 goto setdirparam_done;
1695 goto setdirparam_done;
1698 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1705 change_parent_mdate = 1;
1706 memcpy( &aint, buf, sizeof( aint ));
1707 buf += sizeof( aint );
1708 if (curdir->d_did == DIRDID_ROOT)
1709 setdeskowner( -1, ntohl(aint) );
1711 #if 0 /* don't error if we can't set the desktop owner. */
1715 err = AFPERR_ACCESS;
1716 goto setdirparam_done;
1720 goto setdirparam_done;
1723 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1726 goto setdirparam_done;
1732 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1736 err = AFPERR_ACCESS;
1737 goto setdirparam_done;
1741 goto setdirparam_done;
1744 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1751 case DIRPBIT_ACCESS :
1753 change_parent_mdate = 1;
1754 ma.ma_user = *buf++;
1755 ma.ma_world = *buf++;
1756 ma.ma_group = *buf++;
1757 ma.ma_owner = *buf++;
1759 if (curdir->d_did == DIRDID_ROOT)
1760 setdeskmode(mtoumode( &ma ));
1761 #if 0 /* don't error if we can't set the desktop mode */
1765 err = AFPERR_ACCESS;
1766 goto setdirparam_done;
1769 goto setdirparam_done;
1771 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1775 goto setdirparam_done;
1779 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1780 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1784 err = AFPERR_ACCESS;
1785 goto setdirparam_done;
1788 goto setdirparam_done;
1790 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1793 goto setdirparam_done;
1798 /* Ignore what the client thinks we should do to the
1799 ProDOS information block. Skip over the data and
1800 report nothing amiss. <shirsch@ibm.net> */
1801 case DIRPBIT_PDINFO :
1802 if (afp_version < 30) {
1807 case DIRPBIT_UNIXPR :
1808 /* Skip UID and GID for now, there seems to be now way to set them from an OSX client anyway */
1809 buf += sizeof( aint );
1810 buf += sizeof( aint );
1813 change_parent_mdate = 1;
1814 memcpy( &aint, buf, sizeof( aint ));
1815 buf += sizeof( aint );
1816 aint = ntohl (aint);
1817 if (curdir->d_did == DIRDID_ROOT)
1818 setdeskmode( aint );
1819 #if 0 /* don't error if we can't set the desktop mode */
1823 err = AFPERR_ACCESS;
1824 goto setdirparam_done;
1827 goto setdirparam_done;
1829 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1833 goto setdirparam_done;
1837 if ( setdirunixmode( aint, vol_noadouble(vol),
1838 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1842 err = AFPERR_ACCESS;
1843 goto setdirparam_done;
1846 goto setdirparam_done;
1848 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1851 goto setdirparam_done;
1857 err = AFPERR_BITMAP;
1858 goto setdirparam_done;
1867 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1868 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1872 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1873 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1878 ad_flush( &ad, ADFLAGS_HF );
1879 ad_close( &ad, ADFLAGS_HF );
1882 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1883 && gettimeofday(&tv, NULL) == 0) {
1884 if (!movecwd(vol, curdir->d_parent)) {
1885 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1886 bitmap = 1<<DIRPBIT_MDATE;
1887 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1888 /* should we reset curdir ?*/
1895 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1898 int ibuflen, *rbuflen;
1904 struct path *s_path;
1912 memcpy( &vid, ibuf, sizeof( vid ));
1913 ibuf += sizeof( vid );
1914 if (NULL == ( vol = getvolbyvid( vid )) ) {
1915 return( AFPERR_PARAM );
1918 if (vol->v_flags & AFPVOL_RO)
1919 return AFPERR_VLOCK;
1921 memcpy( &did, ibuf, sizeof( did ));
1922 ibuf += sizeof( did );
1923 if (NULL == ( dir = dirlookup( vol, did )) ) {
1924 return afp_errno; /* was AFPERR_NOOBJ */
1926 /* for concurrent access we need to be sure we are not in the
1927 * folder we want to create...
1931 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1932 return get_afp_errno(AFPERR_PARAM);
1934 /* cname was able to move curdir to it! */
1935 if (*s_path->m_name == '\0')
1936 return AFPERR_EXIST;
1938 upath = s_path->u_name;
1939 if (0 != (err = check_name(vol, upath))) {
1943 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1947 if (of_stat(s_path) < 0) {
1951 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1955 if ( movecwd( vol, dir ) < 0 ) {
1956 return( AFPERR_PARAM );
1959 memset(&ad, 0, sizeof(ad));
1960 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1961 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1962 if (vol_noadouble(vol))
1963 goto createdir_done;
1964 return( AFPERR_ACCESS );
1967 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1968 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1969 ad_getentrylen( &ad, ADEID_NAME ));
1970 ad_flush( &ad, ADFLAGS_HF );
1971 ad_close( &ad, ADFLAGS_HF );
1974 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1975 *rbuflen = sizeof( u_int32_t );
1976 setvoltime(obj, vol );
1981 * dst new unix filename (not a pathname)
1982 * newname new mac name
1986 int renamedir(src, dst, dir, newparent, newname, noadouble)
1987 char *src, *dst, *newname;
1988 struct dir *dir, *newparent;
1989 const int noadouble;
1996 /* existence check moved to afp_moveandrename */
1997 if ( unix_rename( src, dst ) < 0 ) {
2000 return( AFPERR_NOOBJ );
2002 return( AFPERR_ACCESS );
2004 return AFPERR_VLOCK;
2006 /* tried to move directory into a subdirectory of itself */
2007 return AFPERR_CANTMOVE;
2009 /* this needs to copy and delete. bleah. that means we have
2010 * to deal with entire directory hierarchies. */
2011 if ((err = copydir(src, dst, noadouble)) < 0) {
2015 if ((err = deletedir(src)) < 0)
2019 return( AFPERR_PARAM );
2023 memset(&ad, 0, sizeof(ad));
2024 len = strlen( newname );
2025 /* rename() succeeded so we need to update our tree even if we can't open
2028 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
2029 ad_setentrylen( &ad, ADEID_NAME, len );
2030 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
2031 ad_flush( &ad, ADFLAGS_HF );
2032 ad_close( &ad, ADFLAGS_HF );
2035 if (dir->d_m_name == dir->d_u_name)
2036 dir->d_u_name = NULL;
2038 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
2039 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
2040 /* FIXME : fatal ? */
2043 dir->d_m_name = buf;
2044 strcpy( dir->d_m_name, newname );
2046 if (newname == dst) {
2047 free(dir->d_u_name);
2048 dir->d_u_name = dir->d_m_name;
2051 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
2052 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
2055 dir->d_u_name = buf;
2056 strcpy( dir->d_u_name, dst );
2059 if (( parent = dir->d_parent ) == NULL ) {
2062 if ( parent == newparent ) {
2066 /* detach from old parent and add to new one. */
2067 dirchildremove(parent, dir);
2068 dir->d_parent = newparent;
2069 dirchildadd(newparent, dir);
2073 #define DOT_APPLEDOUBLE_LEN 13
2074 /* delete an empty directory */
2075 int deletecurdir( vol, path, pathlen )
2076 const struct vol *vol;
2088 if ( curdir->d_parent == NULL ) {
2089 return( AFPERR_ACCESS );
2094 memset(&ad, 0, sizeof(ad));
2095 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2096 DIRBITS | 0777, &ad) == 0 ) {
2098 ad_getattr(&ad, &ashort);
2099 ad_close( &ad, ADFLAGS_HF );
2100 if ((ashort & htons(ATTRBIT_NODELETE))) {
2101 return AFPERR_OLOCK;
2105 /* delete stray .AppleDouble files. this happens to get .Parent files
2107 if ((dp = opendir(".AppleDouble"))) {
2108 strcpy(path, ".AppleDouble/");
2109 while ((de = readdir(dp))) {
2110 /* skip this and previous directory */
2111 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2114 /* bail if the file exists in the current directory.
2115 * note: this will not fail with dangling symlinks */
2116 if (stat(de->d_name, &st) == 0) {
2118 return AFPERR_DIRNEMPT;
2121 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2122 if ((err = netatalk_unlink(path))) {
2130 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2134 /* now get rid of dangling symlinks */
2135 if ((dp = opendir("."))) {
2136 while ((de = readdir(dp))) {
2137 /* skip this and previous directory */
2138 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2141 /* bail if it's not a symlink */
2142 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2144 return AFPERR_DIRNEMPT;
2147 if ((err = netatalk_unlink(de->d_name))) {
2154 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2159 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2160 dirchildremove(curdir, fdir);
2162 cnid_delete(vol->v_db, fdir->d_did);
2163 #endif /* CNID_DB */
2164 dir_remove( vol, fdir );
2169 /* inode is used as key for cnid.
2170 * Close the descriptor only after cnid_delete
2178 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2181 int ibuflen, *rbuflen;
2191 sfunc = (unsigned char) *ibuf++;
2192 memcpy( &id, ibuf, sizeof( id ));
2197 if (sfunc == 3 || sfunc == 4) {
2198 if (afp_version < 30) {
2199 return( AFPERR_PARAM );
2206 case 3 :/* unicode */
2207 if (( pw = getpwuid( id )) == NULL ) {
2208 return( AFPERR_NOITEM );
2214 case 4 : /* unicode */
2215 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2216 return( AFPERR_NOITEM );
2222 return( AFPERR_PARAM );
2224 len = strlen( name );
2231 u_int16_t tp = htons(len);
2232 memcpy(rbuf, &tp, sizeof(tp));
2241 memcpy( rbuf, name, len );
2247 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2250 int ibuflen, *rbuflen;
2259 sfunc = (unsigned char) *ibuf++;
2263 case 2 : /* unicode */
2264 if (afp_version < 30) {
2265 return( AFPERR_PARAM );
2267 memcpy(&ulen, ibuf, sizeof(ulen));
2273 len = (unsigned char) *ibuf++;
2276 return( AFPERR_PARAM );
2283 case 1 : /* unicode */
2285 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2286 return( AFPERR_NOITEM );
2291 case 2 : /* unicode */
2293 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2294 return( AFPERR_NOITEM );
2303 memcpy( rbuf, &id, sizeof( id ));
2304 *rbuflen = sizeof( id );
2308 /* ------------------------------------
2309 variable DID support
2311 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2314 int ibuflen, *rbuflen;
2325 /* do nothing as dids are static for the life of the process. */
2329 memcpy(&vid, ibuf, sizeof( vid ));
2330 ibuf += sizeof( vid );
2331 if (( vol = getvolbyvid( vid )) == NULL ) {
2332 return( AFPERR_PARAM );
2335 memcpy( &did, ibuf, sizeof( did ));
2336 ibuf += sizeof( did );
2337 if (( dir = dirlookup( vol, did )) == NULL ) {
2338 return( AFPERR_PARAM );
2341 /* dir_remove -- deletedid */
2347 /* did creation gets done automatically
2348 * there's a pb again with case but move it to cname
2350 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2353 int ibuflen, *rbuflen;
2356 struct dir *parentdir;
2364 memcpy(&vid, ibuf, sizeof(vid));
2365 ibuf += sizeof( vid );
2367 if (NULL == ( vol = getvolbyvid( vid )) ) {
2368 return( AFPERR_PARAM );
2371 memcpy(&did, ibuf, sizeof(did));
2372 ibuf += sizeof(did);
2374 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2378 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2379 return get_afp_errno(AFPERR_PARAM);
2382 if ( *path->m_name != '\0' ) {
2383 return path_error(path, AFPERR_NOOBJ);
2386 if ( !path->st_valid && of_stat(path ) < 0 ) {
2387 return( AFPERR_NOOBJ );
2389 if ( path->st_errno ) {
2390 return( AFPERR_NOOBJ );
2393 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2394 *rbuflen = sizeof(curdir->d_did);