2 * $Id: directory.c,v 1.69 2003-04-20 06:13:40 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>
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 /* ------------------- */
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 == curdir->d_child->d_prev) ? NULL : dir->d_next;
164 /* -----------------------------------------
165 * if did is not in the cache resolve it with cnid
169 dirlookup( vol, did )
170 const struct vol *vol;
177 static char path[MAXPATHLEN + 1];
180 static char buffer[12 + MAXPATHLEN + 1];
181 int buflen = 12 + MAXPATHLEN + 1;
186 ret = dirsearch(vol, did);
187 if (ret != NULL || afp_errno == AFPERR_PARAM)
190 utf8 = utf8_encoding();
191 maxpath = (utf8)?MAXPATHLEN -7:255;
193 if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) ) {
194 afp_errno = AFPERR_NOOBJ;
197 ptr = path + MAXPATHLEN;
198 if (NULL == ( mpath = utompath(vol, upath, utf8) ) ) {
199 afp_errno = AFPERR_NOOBJ;
203 pathlen = len; /* no 0 in the last part */
205 strcpy(ptr - len, mpath);
208 ret = dirsearch(vol,id);
212 if ( NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen))
214 NULL == (mpath = utompath(vol, upath, utf8))
216 afp_errno = AFPERR_NOOBJ;
220 len = strlen(mpath) + 1;
222 if (pathlen > maxpath) {
223 afp_errno = AFPERR_PARAM;
226 strcpy(ptr - len, mpath);
230 /* fill the cache, another place where we know about the path type */
236 temp16 = htons(pathlen);
237 memcpy(ptr, &temp16, sizeof(temp16));
239 temp = htonl(kTextEncodingUTF8);
241 memcpy(ptr, &temp, sizeof(temp));
247 *ptr = (unsigned char)pathlen;
251 /* cname is not efficient */
252 if (cname( vol, ret, &ptr ) == NULL )
255 return dirsearch(vol, did);
258 /* --------------------------- */
259 /* rotate the tree to the left */
260 static void dir_leftrotate(vol, dir)
264 struct dir *right = dir->d_right;
266 /* whee. move the right's left tree into dir's right tree */
267 dir->d_right = right->d_left;
268 if (right->d_left != SENTINEL)
269 right->d_left->d_back = dir;
271 if (right != SENTINEL) {
272 right->d_back = dir->d_back;
276 if (!dir->d_back) /* no parent. move the right tree to the top. */
278 else if (dir == dir->d_back->d_left) /* we were on the left */
279 dir->d_back->d_left = right;
281 dir->d_back->d_right = right; /* we were on the right */
283 /* re-insert dir on the left tree */
290 /* rotate the tree to the right */
291 static void dir_rightrotate(vol, dir)
295 struct dir *left = dir->d_left;
297 /* whee. move the left's right tree into dir's left tree */
298 dir->d_left = left->d_right;
299 if (left->d_right != SENTINEL)
300 left->d_right->d_back = dir;
302 if (left != SENTINEL) {
303 left->d_back = dir->d_back;
307 if (!dir->d_back) /* no parent. move the left tree to the top. */
309 else if (dir == dir->d_back->d_right) /* we were on the right */
310 dir->d_back->d_right = left;
312 dir->d_back->d_left = left; /* we were on the left */
314 /* re-insert dir on the right tree */
320 /* recolor after a removal */
321 static struct dir *dir_rmrecolor(vol, dir)
327 while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
328 /* are we on the left tree? */
329 if (dir == dir->d_back->d_left) {
330 leaf = dir->d_back->d_right; /* get right side */
331 if (leaf->d_color == DIRTREE_COLOR_RED) {
332 /* we're red. we need to change to black. */
333 leaf->d_color = DIRTREE_COLOR_BLACK;
334 dir->d_back->d_color = DIRTREE_COLOR_RED;
335 dir_leftrotate(vol, dir->d_back);
336 leaf = dir->d_back->d_right;
339 /* right leaf has black end nodes */
340 if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
341 (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
342 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
343 dir = dir->d_back; /* ascend */
345 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
346 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
347 leaf->d_color = DIRTREE_COLOR_RED;
348 dir_rightrotate(vol, leaf);
349 leaf = dir->d_back->d_right;
351 leaf->d_color = dir->d_back->d_color;
352 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
353 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
354 dir_leftrotate(vol, dir->d_back);
357 } else { /* right tree */
358 leaf = dir->d_back->d_left; /* left tree */
359 if (leaf->d_color == DIRTREE_COLOR_RED) {
360 leaf->d_color = DIRTREE_COLOR_BLACK;
361 dir->d_back->d_color = DIRTREE_COLOR_RED;
362 dir_rightrotate(vol, dir->d_back);
363 leaf = dir->d_back->d_left;
366 /* left leaf has black end nodes */
367 if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
368 (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
369 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
370 dir = dir->d_back; /* ascend */
372 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
373 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
374 leaf->d_color = DIRTREE_COLOR_RED;
375 dir_leftrotate(vol, leaf);
376 leaf = dir->d_back->d_left;
378 leaf->d_color = dir->d_back->d_color;
379 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
380 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
381 dir_rightrotate(vol, dir->d_back);
386 dir->d_color = DIRTREE_COLOR_BLACK;
393 /* remove the node from the tree. this is just like insertion, but
394 * different. actually, it has to worry about a bunch of things that
395 * insertion doesn't care about. */
396 static void dir_remove( vol, dir )
401 struct ofork *of, *last;
402 struct dir *node, *leaf;
403 #endif /* REMOVE_NODES */
405 if (!dir || (dir == SENTINEL))
408 /* i'm not sure if it really helps to delete stuff. */
411 dir->d_m_name = NULL;
412 dir->d_u_name = NULL;
413 #else /* ! REMOVE_NODES */
415 /* go searching for a node with at most one child */
416 if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
420 while (node->d_left != SENTINEL)
425 leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
428 leaf->d_back = node->d_back;
431 } else if (node == node->d_back->d_left) { /* left tree */
432 node->d_back->d_left = leaf;
434 node->d_back->d_right = leaf;
437 /* we want to free node, but we also want to free the data in dir.
438 * currently, that's d_name and the directory traversal bits.
439 * we just copy the necessary bits and then fix up all the
440 * various pointers to the directory. needless to say, there are
441 * a bunch of places that store the directory struct. */
443 struct dir save, *tmp;
445 memcpy(&save, dir, sizeof(save));
446 memcpy(dir, node, sizeof(struct dir));
448 /* restore the red-black bits */
449 dir->d_left = save.d_left;
450 dir->d_right = save.d_right;
451 dir->d_back = save.d_back;
452 dir->d_color = save.d_color;
454 if (node == vol->v_dir) {/* we may need to fix up this pointer */
456 rootpar.d_child = vol->v_dir;
458 /* if we aren't the root directory, we have parents and
459 * siblings to worry about */
460 if (dir->d_parent->d_child == node)
461 dir->d_parent->d_child = dir;
462 dir->d_next->d_prev = dir;
463 dir->d_prev->d_next = dir;
466 /* fix up children. */
470 tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
473 if (node == curdir) /* another pointer to fixup */
476 /* we also need to fix up oforks. bleah */
477 if ((of = dir->d_ofork)) {
478 last = of->of_d_prev;
481 of = (last == of) ? NULL : of->of_d_next;
485 /* set the node's d_name */
486 node->d_m_name = save.d_m_name;
487 node->d_u_name = save.d_u_name;
490 if (node->d_color == DIRTREE_COLOR_BLACK)
491 dir_rmrecolor(vol, leaf);
493 if (node->d_u_name != node->d_m_name) {
494 free(node->d_u_name);
496 free(node->d_m_name);
498 #endif /* ! REMOVE_NODES */
501 /* ---------------------------------------
502 * remove the node and its childs from the tree
504 * FIXME what about opened forks with refs to it?
505 * it's an afp specs violation because you can't delete
506 * an opened forks. Now afpd doesn't care about forks opened by other
507 * process. It's fixable within afpd if fnctl_lock, doable with smb and
508 * next to impossible for nfs and local filesystem access.
510 static void dir_invalidate( vol, dir )
511 const struct vol *vol;
515 /* v_root can't be deleted */
516 if (movecwd(vol, vol->v_root) < 0) {
517 LOG(log_error, logtype_afpd, "cname can't chdir to : %s", vol->v_root);
521 dirchildremove(dir->d_parent, dir);
522 dir_remove( vol, dir );
525 /* ------------------------------------ */
526 static struct dir *dir_insert(vol, dir)
527 const struct vol *vol;
533 while (pdir->d_did != dir->d_did ) {
534 if ( pdir->d_did > dir->d_did ) {
535 if ( pdir->d_left == SENTINEL ) {
542 if ( pdir->d_right == SENTINEL ) {
547 pdir = pdir->d_right;
555 * attempt to extend the current dir. tree to include path
556 * as a side-effect, movecwd to that point and return the new dir
559 extenddir( vol, dir, path )
564 path->u_name = mtoupath(vol, path->m_name, utf8_encoding() );
567 if ( path->u_name == NULL) {
568 afp_errno = AFPERR_PARAM;
571 if (of_stat( path ) != 0 ) {
575 if (!S_ISDIR(path->st.st_mode)) {
579 if (( dir = adddir( vol, dir, path)) == NULL ) {
584 if ( movecwd( vol, dir ) < 0 ) {
591 /* -------------------
592 system rmdir with afp error code.
593 ENOENT is not an error.
595 static int netatalk_rmdir(const char *name)
597 if (rmdir(name) < 0) {
602 return AFPERR_DIRNEMPT;
605 return AFPERR_ACCESS;
615 /* -------------------------
616 appledouble mkdir afp error code.
618 static int netatalk_mkdir(const char *name)
620 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
623 return( AFPERR_NOOBJ );
625 return( AFPERR_VLOCK );
628 return( AFPERR_ACCESS );
630 return( AFPERR_EXIST );
633 return( AFPERR_DFULL );
635 return( AFPERR_PARAM );
641 /* -------------------
642 system unlink with afp error code.
643 ENOENT is not an error.
645 int netatalk_unlink(const char *name)
647 if (unlink(name) < 0) {
655 return AFPERR_ACCESS;
663 /* ------------------- */
664 static int deletedir(char *dir)
666 char path[MAXPATHLEN + 1];
674 if ((len = strlen(dir)) +2 > sizeof(path))
678 if ((dp = opendir(dir)) == NULL)
684 remain = sizeof(path) -len -1;
685 while ((de = readdir(dp)) && err == AFP_OK) {
686 /* skip this and previous directory */
687 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
690 if (strlen(de->d_name) > remain) {
694 strcpy(path + len, de->d_name);
695 if (stat(path, &st)) {
698 if (S_ISDIR(st.st_mode)) {
699 err = deletedir(path);
701 err = netatalk_unlink(path);
706 /* okay. the directory is empty. delete it. note: we already got rid
709 err = netatalk_rmdir(dir);
714 /* do a recursive copy. */
715 static int copydir(char *src, char *dst, int noadouble)
717 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
727 /* doesn't exist or the path is too long. */
728 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
729 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
730 ((dp = opendir(src)) == NULL))
733 /* try to create the destination directory */
734 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
739 /* set things up to copy */
743 srem = sizeof(spath) - slen -1;
748 drem = sizeof(dpath) - dlen -1;
751 while ((de = readdir(dp))) {
752 /* skip this and previous directory */
753 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
756 if (strlen(de->d_name) > srem) {
760 strcpy(spath + slen, de->d_name);
762 if (stat(spath, &st) == 0) {
763 if (strlen(de->d_name) > drem) {
767 strcpy(dpath + dlen, de->d_name);
769 if (S_ISDIR(st.st_mode)) {
770 if (AFP_OK != (err = copydir(spath, dpath, noadouble)))
772 } else if (AFP_OK != (err = copyfile(spath, dpath, NULL, noadouble))) {
776 /* keep the same time stamp. */
777 ut.actime = ut.modtime = st.st_mtime;
783 /* keep the same time stamp. */
784 if (stat(src, &st) == 0) {
785 ut.actime = ut.modtime = st.st_mtime;
795 /* --- public functions follow --- */
797 /* NOTE: we start off with at least one node (the root directory). */
798 struct dir *dirinsert( vol, dir )
804 if ((node = dir_insert(vol, dir)))
807 /* recolor the tree. the current node is red. */
808 dir->d_color = DIRTREE_COLOR_RED;
810 /* parent of this node has to be black. if the parent node
811 * is red, then we have a grandparent. */
812 while ((dir != vol->v_root) &&
813 (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
814 /* are we on the left tree? */
815 if (dir->d_back == dir->d_back->d_back->d_left) {
816 node = dir->d_back->d_back->d_right; /* get the right node */
817 if (node->d_color == DIRTREE_COLOR_RED) {
818 /* we're red. we need to change to black. */
819 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
820 node->d_color = DIRTREE_COLOR_BLACK;
821 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
822 dir = dir->d_back->d_back; /* finished. go up. */
824 if (dir == dir->d_back->d_right) {
826 dir_leftrotate(vol, dir);
828 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
829 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
830 dir_rightrotate(vol, dir->d_back->d_back);
833 node = dir->d_back->d_back->d_left;
834 if (node->d_color == DIRTREE_COLOR_RED) {
835 /* we're red. we need to change to black. */
836 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
837 node->d_color = DIRTREE_COLOR_BLACK;
838 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
839 dir = dir->d_back->d_back; /* finished. ascend */
841 if (dir == dir->d_back->d_left) {
843 dir_rightrotate(vol, dir);
845 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
846 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
847 dir_leftrotate(vol, dir->d_back->d_back);
852 vol->v_root->d_color = DIRTREE_COLOR_BLACK;
856 /* free everything down. we don't bother to recolor as this is only
857 * called to free the entire tree */
858 void dirfreename(struct dir *dir)
860 if (dir->d_u_name != dir->d_m_name) {
869 if (!dir || (dir == SENTINEL))
872 if ( dir->d_left != SENTINEL ) {
873 dirfree( dir->d_left );
875 if ( dir->d_right != SENTINEL ) {
876 dirfree( dir->d_right );
879 if (dir != SENTINEL) {
885 /* --------------------------------------------
886 * most of the time mac name and unix name are the same
888 struct dir *dirnew(const char *m_name, const char *u_name)
892 dir = (struct dir *) calloc(1, sizeof( struct dir ));
896 if ((dir->d_m_name = strdup(m_name)) == NULL) {
901 if (m_name == u_name) {
902 dir->d_u_name = dir->d_m_name;
904 else if ((dir->d_u_name = strdup(u_name)) == NULL) {
910 dir->d_left = dir->d_right = SENTINEL;
911 dir->d_next = dir->d_prev = dir;
915 /* -------------------------------------------------- */
921 stat the file or errno
924 curdir: filename parent directory
930 stat the dir or errno
934 curdir: dir parent directory
942 curdir: dir parent directory
949 cname( vol, dir, cpath )
950 const struct vol *vol;
955 static char path[ MAXPATHLEN + 1];
956 static struct path ret;
967 afp_errno = AFPERR_NOOBJ;
968 switch (ret.m_type = *data) { /* path type */
971 len = (unsigned char) *data++;
976 if (afp_version >= 30) {
978 memcpy(&hint, data, sizeof(hint));
980 data += sizeof(hint);
982 memcpy(&len16, data, sizeof(len16));
989 /* else it's an error */
991 afp_errno = AFPERR_PARAM;
994 *cpath += len + size;
1002 if (movecwd( vol, dir ) < 0 ) {
1004 movecwd failed some of dir path are not there anymore.
1005 FIXME Is it true with other errors?
1006 so we remove dir from the cache
1008 if (dir->d_did == DIRDID_ROOT_PARENT)
1010 if (afp_errno == AFPERR_ACCESS) {
1011 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1014 ret.m_name = dir->d_m_name;
1015 ret.u_name = dir->d_u_name;
1018 } else if (afp_errno == AFPERR_NOOBJ) {
1019 if ( movecwd( vol, dir->d_parent ) < 0 ) {
1022 strcpy(path, dir->d_m_name);
1023 if (dir->d_m_name == dir->d_u_name) {
1027 size_t tp = strlen(path)+1;
1029 strcpy(path +tp, dir->d_u_name);
1030 ret.u_name = path +tp;
1033 dir_invalidate(vol, dir);
1036 dir_invalidate(vol, dir);
1039 if (*path == '\0') {
1046 if (*data == sep ) {
1050 while (*data == sep && len > 0 ) {
1051 if ( dir->d_parent == NULL ) {
1054 dir = dir->d_parent;
1059 /* would this be faster with strlen + strncpy? */
1061 while ( *data != sep && len > 0 ) {
1063 if (p > &path[ MAXPATHLEN]) {
1064 afp_errno = AFPERR_PARAM;
1070 /* short cut bits by chopping off a trailing \0. this also
1071 makes the traversal happy w/ filenames at the end of the
1078 if ( p != path ) { /* we got something */
1080 cdir = dir->d_child;
1082 if ( strcmp( cdir->d_m_name, path ) == 0 ) {
1085 cdir = (cdir == dir->d_child->d_prev) ? NULL :
1088 if ( cdir == NULL ) {
1090 /* if dir == curdir it always succeed,
1091 even if curdir is deleted.
1092 it's not a pb because it will fail in extenddir
1094 if ( movecwd( vol, dir ) < 0 ) {
1095 /* dir is not valid anymore
1096 we delete dir from the cache and abort.
1098 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1099 afp_errno = AFPERR_NOOBJ;
1102 if (afp_errno == AFPERR_ACCESS)
1104 dir_invalidate(vol, dir);
1107 cdir = extenddir( vol, dir, &ret );
1111 cdir = extenddir( vol, dir, &ret );
1114 if ( cdir == NULL ) {
1116 if ( len > 0 || !ret.u_name) {
1129 * Move curdir to dir, with a possible chdir()
1131 int movecwd( vol, dir)
1132 const struct vol *vol;
1135 char path[MAXPATHLEN + 1];
1140 if ( dir == curdir ) {
1143 if ( dir->d_did == DIRDID_ROOT_PARENT) {
1144 afp_errno = AFPERR_DID1; /* AFPERR_PARAM;*/
1148 p = path + sizeof(path) - 1;
1151 for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
1154 /* parent directory is deleted */
1155 afp_errno = AFPERR_NOOBJ;
1159 if (p -n -1 < path) {
1160 afp_errno = AFPERR_PARAM;
1167 if ( d != curdir ) {
1168 n = strlen( vol->v_path );
1169 if (p -n -1 < path) {
1170 afp_errno = AFPERR_PARAM;
1175 strncpy( p, vol->v_path, n );
1177 if ( chdir( p ) < 0 ) {
1181 afp_errno = AFPERR_ACCESS;
1184 afp_errno = AFPERR_NOOBJ;
1194 * We can't use unix file's perm to support Apple's inherited protection modes.
1195 * If we aren't the file's owner we can't change its perms when moving it and smb
1196 * nfs,... don't even try.
1198 #define AFP_CHECK_ACCESS
1200 int check_access(char *path, int mode)
1202 #ifdef AFP_CHECK_ACCESS
1210 accessmode(p, &ma, curdir, NULL);
1211 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1213 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1219 /* ------------------------------
1221 (name, dir) with curdir:name == dir, from afp_enumerate
1224 int getdirparams(const struct vol *vol,
1225 u_int16_t bitmap, struct path *s_path,
1227 char *buf, int *buflen )
1231 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1232 int bit = 0, isad = 0;
1237 struct stat *st = &s_path->st;
1238 char *upath = s_path->u_name;
1240 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1241 (1 << DIRPBIT_CDATE) |
1242 (1 << DIRPBIT_MDATE) |
1243 (1 << DIRPBIT_BDATE) |
1244 (1 << DIRPBIT_FINFO)))) {
1245 memset(&ad, 0, sizeof(ad));
1246 if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1247 DIRBITS | 0777, &ad)) {
1253 while ( bitmap != 0 ) {
1254 while (( bitmap & 1 ) == 0 ) {
1262 ad_getattr(&ad, &ashort);
1263 } else if (*dir->d_u_name == '.' && strcmp(dir->d_u_name, ".")
1264 && strcmp(dir->d_u_name, "..")) {
1265 ashort = htons(ATTRBIT_INVISIBLE);
1268 ashort |= htons(ATTRBIT_SHARED);
1269 memcpy( data, &ashort, sizeof( ashort ));
1270 data += sizeof( ashort );
1274 if ( dir->d_did == DIRDID_ROOT) {
1275 aint = DIRDID_ROOT_PARENT;
1276 } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1279 aint = dir->d_parent->d_did;
1281 memcpy( data, &aint, sizeof( aint ));
1282 data += sizeof( aint );
1285 case DIRPBIT_CDATE :
1286 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1287 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1288 memcpy( data, &aint, sizeof( aint ));
1289 data += sizeof( aint );
1292 case DIRPBIT_MDATE :
1293 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1294 memcpy( data, &aint, sizeof( aint ));
1295 data += sizeof( aint );
1298 case DIRPBIT_BDATE :
1299 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1300 aint = AD_DATE_START;
1301 memcpy( data, &aint, sizeof( aint ));
1302 data += sizeof( aint );
1305 case DIRPBIT_FINFO :
1307 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1308 } else { /* no appledouble */
1309 memset( data, 0, 32 );
1310 /* set default view -- this also gets done in ad_open() */
1311 ashort = htons(FINDERINFO_CLOSEDVIEW);
1312 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1314 /* dot files are by default invisible */
1315 if (*dir->d_u_name == '.' && strcmp(dir->d_u_name , ".") &&
1316 strcmp(dir->d_u_name , "..")) {
1317 ashort = htons(FINDERINFO_INVISIBLE);
1318 memcpy(data + FINDERINFO_FRFLAGOFF,
1319 &ashort, sizeof(ashort));
1325 case DIRPBIT_LNAME :
1326 if (dir->d_m_name) /* root of parent can have a null name */
1329 memset(data, 0, sizeof(u_int16_t));
1330 data += sizeof( u_int16_t );
1333 case DIRPBIT_SNAME :
1334 memset(data, 0, sizeof(u_int16_t));
1335 data += sizeof( u_int16_t );
1339 memcpy( data, &dir->d_did, sizeof( aint ));
1340 data += sizeof( aint );
1343 case DIRPBIT_OFFCNT :
1345 /* this needs to handle current directory access rights */
1346 if (st->st_ctime == dir->ctime) {
1347 ashort = dir->offcnt;
1349 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1351 dir->offcnt = ashort;
1352 dir->ctime = st->st_ctime;
1354 ashort = htons( ashort );
1355 memcpy( data, &ashort, sizeof( ashort ));
1356 data += sizeof( ashort );
1360 aint = htonl(st->st_uid);
1361 memcpy( data, &aint, sizeof( aint ));
1362 data += sizeof( aint );
1366 aint = htonl(st->st_gid);
1367 memcpy( data, &aint, sizeof( aint ));
1368 data += sizeof( aint );
1371 case DIRPBIT_ACCESS :
1372 accessmode( upath, &ma, dir , st);
1374 *data++ = ma.ma_user;
1375 *data++ = ma.ma_world;
1376 *data++ = ma.ma_group;
1377 *data++ = ma.ma_owner;
1380 /* Client has requested the ProDOS information block.
1381 Just pass back the same basic block for all
1382 directories. <shirsch@ibm.net> */
1383 case DIRPBIT_PDINFO :
1384 if (afp_version >= 30) { /* UTF8 name */
1385 utf8 = kTextEncodingUTF8;
1386 if (dir->d_m_name) /* root of parent can have a null name */
1389 memset(data, 0, sizeof(u_int16_t));
1390 data += sizeof( u_int16_t );
1392 memcpy(data, &aint, sizeof( aint ));
1393 data += sizeof( aint );
1395 else { /* ProDOS Info Block */
1398 ashort = htons( 0x0200 );
1399 memcpy( data, &ashort, sizeof( ashort ));
1400 data += sizeof( ashort );
1401 memset( data, 0, sizeof( ashort ));
1402 data += sizeof( ashort );
1408 ad_close( &ad, ADFLAGS_HF );
1410 return( AFPERR_BITMAP );
1416 ashort = htons( data - buf );
1417 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1418 data = set_name(vol, data, dir->d_m_name, 0);
1420 if ( utf_nameoff ) {
1421 ashort = htons( data - buf );
1422 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1423 data = set_name(vol, data, dir->d_m_name, utf8);
1426 ad_close( &ad, ADFLAGS_HF );
1428 *buflen = data - buf;
1432 /* ----------------------------- */
1433 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1436 int ibuflen, *rbuflen;
1441 u_int16_t vid, bitmap;
1447 memcpy( &vid, ibuf, sizeof( vid ));
1448 ibuf += sizeof( vid );
1450 if (NULL == ( vol = getvolbyvid( vid )) ) {
1451 return( AFPERR_PARAM );
1454 if (vol->v_flags & AFPVOL_RO)
1455 return AFPERR_VLOCK;
1457 memcpy( &did, ibuf, sizeof( did ));
1458 ibuf += sizeof( int );
1460 if (NULL == ( dir = dirlookup( vol, did )) ) {
1464 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1465 bitmap = ntohs( bitmap );
1466 ibuf += sizeof( bitmap );
1468 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1469 return get_afp_errno(AFPERR_NOOBJ);
1472 /* FIXME access error or not a file */
1473 if ( *path->m_name != '\0' ) {
1474 return (path_isadir( path))? afp_errno:AFPERR_BADTYPE ;
1478 * If ibuf is odd, make it even.
1480 if ((u_long)ibuf & 1 ) {
1484 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1485 setvoltime(obj, vol );
1491 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1493 * assume path == '\0' eg. it's a directory in canonical form
1496 struct path Cur_Path = {
1499 ".", /* unix name */
1500 0, /* stat is not set */
1504 int setdirparams(const struct vol *vol,
1505 struct path *path, u_int16_t bitmap, char *buf )
1513 int bit = 0, aint, isad = 1;
1514 u_int16_t ashort, bshort;
1516 int change_mdate = 0;
1517 int change_parent_mdate = 0;
1520 upath = path->u_name;
1521 memset(&ad, 0, sizeof(ad));
1523 if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1524 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1526 * Check to see what we're trying to set. If it's anything
1527 * but ACCESS, UID, or GID, give an error. If it's any of those
1528 * three, we don't need the ad to be open, so just continue.
1530 * note: we also don't need to worry about mdate. also, be quiet
1531 * if we're using the noadouble option.
1533 if (!vol_noadouble(vol) && (bitmap &
1534 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1535 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1536 return AFPERR_ACCESS;
1542 * Check to see if a create was necessary. If it was, we'll want
1543 * to set our name, etc.
1545 if ( ad_get_HF_flags( &ad ) & O_CREAT ) {
1546 ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1547 memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1548 ad_getentrylen( &ad, ADEID_NAME ));
1552 while ( bitmap != 0 ) {
1553 while (( bitmap & 1 ) == 0 ) {
1562 memcpy( &ashort, buf, sizeof( ashort ));
1563 ad_getattr(&ad, &bshort);
1564 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1565 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1569 ad_setattr(&ad, bshort);
1570 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1571 change_parent_mdate = 1;
1573 buf += sizeof( ashort );
1576 case DIRPBIT_CDATE :
1579 memcpy(&aint, buf, sizeof(aint));
1580 ad_setdate(&ad, AD_DATE_CREATE, aint);
1582 buf += sizeof( aint );
1585 case DIRPBIT_MDATE :
1586 memcpy(&newdate, buf, sizeof(newdate));
1587 buf += sizeof( newdate );
1590 case DIRPBIT_BDATE :
1593 memcpy(&aint, buf, sizeof(aint));
1594 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1596 buf += sizeof( aint );
1599 case DIRPBIT_FINFO :
1602 * Alright, we admit it, this is *really* sick!
1603 * The 4 bytes that we don't copy, when we're dealing
1604 * with the root of a volume, are the directory's
1605 * location information. This eliminates that annoying
1606 * behavior one sees when mounting above another mount
1610 if ( curdir->d_did == DIRDID_ROOT ) {
1611 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1612 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1614 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1620 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1621 change_parent_mdate = 1;
1622 memcpy( &aint, buf, sizeof(aint));
1623 buf += sizeof( aint );
1624 if ( (curdir->d_did == DIRDID_ROOT) &&
1625 (setdeskowner( ntohl(aint), -1 ) < 0)) {
1629 err = AFPERR_ACCESS;
1630 goto setdirparam_done;
1634 goto setdirparam_done;
1637 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1641 goto setdirparam_done;
1646 if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1650 err = AFPERR_ACCESS;
1651 goto setdirparam_done;
1655 goto setdirparam_done;
1658 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1665 change_parent_mdate = 1;
1666 memcpy( &aint, buf, sizeof( aint ));
1667 buf += sizeof( aint );
1668 if (curdir->d_did == DIRDID_ROOT)
1669 setdeskowner( -1, ntohl(aint) );
1671 #if 0 /* don't error if we can't set the desktop owner. */
1675 err = AFPERR_ACCESS;
1676 goto setdirparam_done;
1680 goto setdirparam_done;
1683 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1686 goto setdirparam_done;
1692 if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1696 err = AFPERR_ACCESS;
1697 goto setdirparam_done;
1701 goto setdirparam_done;
1704 LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1711 case DIRPBIT_ACCESS :
1713 change_parent_mdate = 1;
1714 ma.ma_user = *buf++;
1715 ma.ma_world = *buf++;
1716 ma.ma_group = *buf++;
1717 ma.ma_owner = *buf++;
1719 if (curdir->d_did == DIRDID_ROOT)
1720 setdeskmode(mtoumode( &ma ));
1721 #if 0 /* don't error if we can't set the desktop mode */
1725 err = AFPERR_ACCESS;
1726 goto setdirparam_done;
1729 goto setdirparam_done;
1731 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1735 goto setdirparam_done;
1739 if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1740 (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1744 err = AFPERR_ACCESS;
1745 goto setdirparam_done;
1748 goto setdirparam_done;
1750 LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1753 goto setdirparam_done;
1758 /* Ignore what the client thinks we should do to the
1759 ProDOS information block. Skip over the data and
1760 report nothing amiss. <shirsch@ibm.net> */
1761 case DIRPBIT_PDINFO :
1762 if (afp_version < 30) {
1767 err = AFPERR_BITMAP;
1768 goto setdirparam_done;
1777 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1778 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1782 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1783 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1788 ad_flush( &ad, ADFLAGS_HF );
1789 ad_close( &ad, ADFLAGS_HF );
1792 if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1793 && gettimeofday(&tv, NULL) == 0) {
1794 if (!movecwd(vol, curdir->d_parent)) {
1795 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1796 bitmap = 1<<DIRPBIT_MDATE;
1797 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1798 /* should we reset curdir ?*/
1805 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1808 int ibuflen, *rbuflen;
1814 struct path *s_path;
1822 memcpy( &vid, ibuf, sizeof( vid ));
1823 ibuf += sizeof( vid );
1824 if (NULL == ( vol = getvolbyvid( vid )) ) {
1825 return( AFPERR_PARAM );
1828 if (vol->v_flags & AFPVOL_RO)
1829 return AFPERR_VLOCK;
1831 memcpy( &did, ibuf, sizeof( did ));
1832 ibuf += sizeof( did );
1833 if (NULL == ( dir = dirlookup( vol, did )) ) {
1834 return afp_errno; /* was AFPERR_NOOBJ */
1836 /* for concurrent access we need to be sure we are not in the
1837 * folder we want to create...
1841 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1842 return get_afp_errno(AFPERR_PARAM);
1844 /* cname was able to move curdir to it! */
1845 if (*s_path->m_name == '\0')
1846 return AFPERR_EXIST;
1848 upath = s_path->u_name;
1849 if (0 != (err = check_name(vol, upath))) {
1853 if (AFP_OK != (err = netatalk_mkdir( upath))) {
1857 if (of_stat(s_path) < 0) {
1861 if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1865 if ( movecwd( vol, dir ) < 0 ) {
1866 return( AFPERR_PARAM );
1869 memset(&ad, 0, sizeof(ad));
1870 if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1871 O_RDWR|O_CREAT, 0666, &ad ) < 0) {
1872 if (vol_noadouble(vol))
1873 goto createdir_done;
1874 return( AFPERR_ACCESS );
1877 ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1878 memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1879 ad_getentrylen( &ad, ADEID_NAME ));
1880 ad_flush( &ad, ADFLAGS_HF );
1881 ad_close( &ad, ADFLAGS_HF );
1884 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1885 *rbuflen = sizeof( u_int32_t );
1886 setvoltime(obj, vol );
1891 * dst new unix filename (not a pathname)
1892 * newname new mac name
1896 int renamedir(src, dst, dir, newparent, newname, noadouble)
1897 char *src, *dst, *newname;
1898 struct dir *dir, *newparent;
1899 const int noadouble;
1906 /* existence check moved to afp_moveandrename */
1907 if ( unix_rename( src, dst ) < 0 ) {
1910 return( AFPERR_NOOBJ );
1912 return( AFPERR_ACCESS );
1914 return AFPERR_VLOCK;
1916 /* tried to move directory into a subdirectory of itself */
1917 return AFPERR_CANTMOVE;
1919 /* this needs to copy and delete. bleah. that means we have
1920 * to deal with entire directory hierarchies. */
1921 if ((err = copydir(src, dst, noadouble)) < 0) {
1925 if ((err = deletedir(src)) < 0)
1929 return( AFPERR_PARAM );
1933 memset(&ad, 0, sizeof(ad));
1934 len = strlen( newname );
1935 /* rename() succeeded so we need to update our tree even if we can't open
1938 if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
1939 ad_setentrylen( &ad, ADEID_NAME, len );
1940 memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1941 ad_flush( &ad, ADFLAGS_HF );
1942 ad_close( &ad, ADFLAGS_HF );
1945 if (dir->d_m_name == dir->d_u_name)
1946 dir->d_u_name = NULL;
1948 if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
1949 LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
1950 /* FIXME : fatal ? */
1953 dir->d_m_name = buf;
1954 strcpy( dir->d_m_name, newname );
1956 if (newname == dst) {
1957 free(dir->d_u_name);
1958 dir->d_u_name = dir->d_m_name;
1961 if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
1962 LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
1965 dir->d_u_name = buf;
1966 strcpy( dir->d_u_name, dst );
1969 if (( parent = dir->d_parent ) == NULL ) {
1972 if ( parent == newparent ) {
1976 /* detach from old parent and add to new one. */
1977 dirchildremove(parent, dir);
1978 dir->d_parent = newparent;
1979 dirchildadd(newparent, dir);
1983 #define DOT_APPLEDOUBLE_LEN 13
1984 /* delete an empty directory */
1985 int deletecurdir( vol, path, pathlen )
1986 const struct vol *vol;
1998 if ( curdir->d_parent == NULL ) {
1999 return( AFPERR_ACCESS );
2004 memset(&ad, 0, sizeof(ad));
2005 if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
2006 DIRBITS | 0777, &ad) == 0 ) {
2008 ad_getattr(&ad, &ashort);
2009 ad_close( &ad, ADFLAGS_HF );
2010 if ((ashort & htons(ATTRBIT_NODELETE))) {
2011 return AFPERR_OLOCK;
2015 /* delete stray .AppleDouble files. this happens to get .Parent files
2017 if ((dp = opendir(".AppleDouble"))) {
2018 strcpy(path, ".AppleDouble/");
2019 while ((de = readdir(dp))) {
2020 /* skip this and previous directory */
2021 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2024 /* bail if the file exists in the current directory.
2025 * note: this will not fail with dangling symlinks */
2026 if (stat(de->d_name, &st) == 0) {
2028 return AFPERR_DIRNEMPT;
2031 strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
2032 if ((err = netatalk_unlink(path))) {
2040 if ( (err = netatalk_rmdir( ".AppleDouble" )) ) {
2044 /* now get rid of dangling symlinks */
2045 if ((dp = opendir("."))) {
2046 while ((de = readdir(dp))) {
2047 /* skip this and previous directory */
2048 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2051 /* bail if it's not a symlink */
2052 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2054 return AFPERR_DIRNEMPT;
2057 if ((err = netatalk_unlink(de->d_name))) {
2064 if ( movecwd( vol, curdir->d_parent ) < 0 ) {
2069 if ( !(err = netatalk_rmdir(fdir->d_u_name))) {
2070 dirchildremove(curdir, fdir);
2072 cnid_delete(vol->v_db, fdir->d_did);
2073 #endif /* CNID_DB */
2074 dir_remove( vol, fdir );
2079 /* inode is used as key for cnid.
2080 * Close the descriptor only after cnid_delete
2088 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
2091 int ibuflen, *rbuflen;
2101 sfunc = (unsigned char) *ibuf++;
2102 memcpy( &id, ibuf, sizeof( id ));
2110 case 3 :/* unicode */
2111 if (( pw = getpwuid( id )) == NULL ) {
2112 return( AFPERR_NOITEM );
2118 case 4 : /* unicode */
2119 if (( gr = (struct group *)getgrgid( id )) == NULL ) {
2120 return( AFPERR_NOITEM );
2126 return( AFPERR_PARAM );
2131 if (afp_version < 30) {
2132 return( AFPERR_PARAM );
2135 /* map to unicode */
2138 len = strlen( name );
2145 u_int16_t tp = htons(len);
2146 memcpy(rbuf, &tp, sizeof(tp));
2155 memcpy( rbuf, name, len );
2161 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2164 int ibuflen, *rbuflen;
2173 sfunc = (unsigned char) *ibuf++;
2176 case 2 : /* unicode */
2177 memcpy(&ulen, ibuf, sizeof(ulen));
2183 len = (unsigned char) *ibuf++;
2187 return( AFPERR_PARAM );
2194 case 1 : /* unicode */
2196 if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
2198 return( AFPERR_NOITEM );
2203 case 2 : /* unicode */
2205 if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
2207 return( AFPERR_NOITEM );
2216 memcpy( rbuf, &id, sizeof( id ));
2217 *rbuflen = sizeof( id );
2221 /* ------------------------------------
2222 variable DID support
2224 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2227 int ibuflen, *rbuflen;
2238 /* do nothing as dids are static for the life of the process. */
2242 memcpy(&vid, ibuf, sizeof( vid ));
2243 ibuf += sizeof( vid );
2244 if (( vol = getvolbyvid( vid )) == NULL ) {
2245 return( AFPERR_PARAM );
2248 memcpy( &did, ibuf, sizeof( did ));
2249 ibuf += sizeof( did );
2250 if (( dir = dirlookup( vol, did )) == NULL ) {
2251 return( AFPERR_PARAM );
2254 /* dir_remove -- deletedid */
2260 /* did creation gets done automatically
2261 * there's a pb again with case but move it to cname
2263 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2266 int ibuflen, *rbuflen;
2269 struct dir *parentdir;
2277 memcpy(&vid, ibuf, sizeof(vid));
2278 ibuf += sizeof( vid );
2280 if (NULL == ( vol = getvolbyvid( vid )) ) {
2281 return( AFPERR_PARAM );
2284 memcpy(&did, ibuf, sizeof(did));
2285 ibuf += sizeof(did);
2287 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2291 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2292 return get_afp_errno(AFPERR_PARAM);
2295 if ( *path->m_name != '\0' ) {
2296 return (path_isadir(path))? afp_errno:AFPERR_BADTYPE ;
2299 if ( !path->st_valid && of_stat(path ) < 0 ) {
2300 return( AFPERR_NOOBJ );
2302 if ( path->st_errno ) {
2303 return( AFPERR_NOOBJ );
2306 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2307 *rbuflen = sizeof(curdir->d_did);