]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
44d40daf1143c9cc4c4acb9ff21d53f18ebe4beb
[netatalk.git] / etc / afpd / directory.c
1 /*
2  * $Id: directory.c,v 1.57 2003-01-21 09:58:58 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  *
7  * 19 jan 2000 implemented red-black trees for directory lookups
8  * (asun@cobalt.com).
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <atalk/logger.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <errno.h>
19 #include <sys/time.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 #ifdef CNID_DB
26 #include <atalk/cnid.h>
27 #endif /* CNID_DB */
28 #include <utime.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif /* HAVE_FCNTL_H */
35 #include <grp.h>
36 #include <pwd.h>
37
38 /* STDC check */
39 #if STDC_HEADERS
40 #include <string.h>
41 #else /* STDC_HEADERS */
42 #ifndef HAVE_STRCHR
43 #define strchr index
44 #define strrchr index
45 #endif /* HAVE_STRCHR */
46 char *strchr (), *strrchr ();
47 #ifndef HAVE_MEMCPY
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 */
52
53 #include "directory.h"
54 #include "desktop.h"
55 #include "volume.h"
56 #include "fork.h"
57 #include "file.h"
58 #include "filedir.h"
59 #include "globals.h"
60 #include "unix.h"
61
62 struct dir      *curdir;
63 int             afp_errno;
64
65 #define SENTINEL (&sentinel)
66 static struct dir sentinel = { SENTINEL, SENTINEL, NULL, DIRTREE_COLOR_BLACK,
67                                  NULL, NULL, NULL, NULL, NULL, 0, 0, 
68                                  0, 0, NULL, NULL};
69 static struct dir rootpar  = { SENTINEL, SENTINEL, NULL, 0,
70                                  NULL, NULL, NULL, NULL, NULL, 0, 0, 
71                                  0, 0, NULL, NULL};
72
73 /* (from IM: Toolbox Essentials)
74  * dirFinderInfo (DInfo) fields:
75  * field        bytes
76  * frRect       8    folder's window rectangle
77  * frFlags      2    flags
78  * frLocation   4    folder's location in window
79  * frView       2    folder's view (default == closedView (256))
80  *
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
88  */
89
90 /*
91  * redid did assignment for directories. now we use red-black trees.
92  * how exciting.
93  */
94 struct dir *
95             dirsearch( vol, did )
96             const struct vol    *vol;
97 u_int32_t       did;
98 {
99     struct dir  *dir;
100
101
102     /* check for 0 did */
103     if (!did) {
104         afp_errno = AFPERR_PARAM;
105         return NULL;
106     }
107     if ( did == DIRDID_ROOT_PARENT ) {
108         if (!rootpar.d_did)
109             rootpar.d_did = DIRDID_ROOT_PARENT;
110         rootpar.d_child = vol->v_dir;
111         return( &rootpar );
112     }
113
114     dir = vol->v_root;
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;
120     }
121     return NULL;
122 }
123
124 /* -----------------------------------------
125  * if did is not in the cache resolve it with cnid 
126  * 
127  */
128 struct dir *
129             dirlookup( vol, did )
130             const struct vol    *vol;
131 u_int32_t       did;
132 {
133 #ifdef CNID_DB
134     struct dir *ret;
135     char                *upath;
136     u_int32_t   id;
137     static char         path[MAXPATHLEN + 1];
138     int len;
139     int pathlen;
140     char *ptr;
141     static char buffer[12 + MAXPATHLEN + 1];
142     int buflen = 12 + MAXPATHLEN + 1;
143     char *mpath;
144     
145     ret = dirsearch(vol, did);
146     if (ret != NULL)
147         return ret;
148
149     id = did;
150     if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) ) {
151         afp_errno = AFPERR_NOOBJ;
152         return NULL;
153     }
154     ptr = path + MAXPATHLEN;
155     mpath = utompath(vol, upath);
156     len = strlen(mpath);
157     pathlen = len;          /* no 0 in the last part */
158     len++;
159     strcpy(ptr - len, mpath);
160     ptr -= len;
161     while (1) {
162         ret = dirsearch(vol,id);
163         if (ret != NULL) {
164             break;
165         }
166         if ((upath = cnid_resolve(vol->v_db, &id, buffer, buflen)) == NULL) {
167             afp_errno = AFPERR_NOOBJ;
168             return NULL;
169         }
170         mpath = utompath(vol, upath);
171         len = strlen(mpath) + 1;
172         pathlen += len;
173         if (pathlen > 255) {
174             afp_errno = AFPERR_PARAM;
175             return NULL;
176         }
177         strcpy(ptr - len, mpath);
178         ptr -= len;
179     }
180     /* fill the cache */
181     ptr--;
182     *ptr = (unsigned char)pathlen;
183     ptr--;
184     *ptr = 2;
185     /* cname is not efficient */
186     if (cname( vol, ret, &ptr ) == NULL )
187         return NULL;
188 #endif
189     return dirsearch(vol, did);
190 }
191
192 /* --------------------------- */
193 /* rotate the tree to the left */
194 static void dir_leftrotate(vol, dir)
195 struct vol *vol;
196 struct dir *dir;
197 {
198     struct dir *right = dir->d_right;
199
200     /* whee. move the right's left tree into dir's right tree */
201     dir->d_right = right->d_left;
202     if (right->d_left != SENTINEL)
203         right->d_left->d_back = dir;
204
205     if (right != SENTINEL) {
206         right->d_back = dir->d_back;
207         right->d_left = dir;
208     }
209
210     if (!dir->d_back) /* no parent. move the right tree to the top. */
211         vol->v_root = right;
212     else if (dir == dir->d_back->d_left) /* we were on the left */
213         dir->d_back->d_left = right;
214     else
215         dir->d_back->d_right = right; /* we were on the right */
216
217     /* re-insert dir on the left tree */
218     if (dir != SENTINEL)
219         dir->d_back = right;
220 }
221
222
223
224 /* rotate the tree to the right */
225 static void dir_rightrotate(vol, dir)
226 struct vol *vol;
227 struct dir *dir;
228 {
229     struct dir *left = dir->d_left;
230
231     /* whee. move the left's right tree into dir's left tree */
232     dir->d_left = left->d_right;
233     if (left->d_right != SENTINEL)
234         left->d_right->d_back = dir;
235
236     if (left != SENTINEL) {
237         left->d_back = dir->d_back;
238         left->d_right = dir;
239     }
240
241     if (!dir->d_back) /* no parent. move the left tree to the top. */
242         vol->v_root = left;
243     else if (dir == dir->d_back->d_right) /* we were on the right */
244         dir->d_back->d_right = left;
245     else
246         dir->d_back->d_left = left; /* we were on the left */
247
248     /* re-insert dir on the right tree */
249     if (dir != SENTINEL)
250         dir->d_back = left;
251 }
252
253 #if 0
254 /* recolor after a removal */
255 static struct dir *dir_rmrecolor(vol, dir)
256             struct vol *vol;
257 struct dir *dir;
258 {
259     struct dir *leaf;
260
261     while ((dir != vol->v_root) && (dir->d_color == DIRTREE_COLOR_BLACK)) {
262         /* are we on the left tree? */
263         if (dir == dir->d_back->d_left) {
264             leaf = dir->d_back->d_right; /* get right side */
265             if (leaf->d_color == DIRTREE_COLOR_RED) {
266                 /* we're red. we need to change to black. */
267                 leaf->d_color = DIRTREE_COLOR_BLACK;
268                 dir->d_back->d_color = DIRTREE_COLOR_RED;
269                 dir_leftrotate(vol, dir->d_back);
270                 leaf = dir->d_back->d_right;
271             }
272
273             /* right leaf has black end nodes */
274             if ((leaf->d_left->d_color == DIRTREE_COLOR_BLACK) &&
275                     (leaf->d_right->d_color = DIRTREE_COLOR_BLACK)) {
276                 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
277                 dir = dir->d_back; /* ascend */
278             } else {
279                 if (leaf->d_right->d_color == DIRTREE_COLOR_BLACK) {
280                     leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
281                     leaf->d_color = DIRTREE_COLOR_RED;
282                     dir_rightrotate(vol, leaf);
283                     leaf = dir->d_back->d_right;
284                 }
285                 leaf->d_color = dir->d_back->d_color;
286                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
287                 leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
288                 dir_leftrotate(vol, dir->d_back);
289                 dir = vol->v_root;
290             }
291         } else { /* right tree */
292             leaf = dir->d_back->d_left; /* left tree */
293             if (leaf->d_color == DIRTREE_COLOR_RED) {
294                 leaf->d_color = DIRTREE_COLOR_BLACK;
295                 dir->d_back->d_color = DIRTREE_COLOR_RED;
296                 dir_rightrotate(vol, dir->d_back);
297                 leaf = dir->d_back->d_left;
298             }
299
300             /* left leaf has black end nodes */
301             if ((leaf->d_right->d_color == DIRTREE_COLOR_BLACK) &&
302                     (leaf->d_left->d_color = DIRTREE_COLOR_BLACK)) {
303                 leaf->d_color = DIRTREE_COLOR_RED; /* recolor leaf as red */
304                 dir = dir->d_back; /* ascend */
305             } else {
306                 if (leaf->d_left->d_color == DIRTREE_COLOR_BLACK) {
307                     leaf->d_right->d_color = DIRTREE_COLOR_BLACK;
308                     leaf->d_color = DIRTREE_COLOR_RED;
309                     dir_leftrotate(vol, leaf);
310                     leaf = dir->d_back->d_left;
311                 }
312                 leaf->d_color = dir->d_back->d_color;
313                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
314                 leaf->d_left->d_color = DIRTREE_COLOR_BLACK;
315                 dir_rightrotate(vol, dir->d_back);
316                 dir = vol->v_root;
317             }
318         }
319     }
320     dir->d_color = DIRTREE_COLOR_BLACK;
321
322     return dir;
323 }
324 #endif /* 0 */
325
326
327 /* remove the node from the tree. this is just like insertion, but
328  * different. actually, it has to worry about a bunch of things that
329  * insertion doesn't care about. */
330 static void dir_remove( vol, dir )
331 struct vol      *vol;
332 struct dir      *dir;
333 {
334 #ifdef REMOVE_NODES
335     struct ofork *of, *last;
336     struct dir *node, *leaf;
337 #endif /* REMOVE_NODES */
338
339     if (!dir || (dir == SENTINEL))
340         return;
341
342     /* i'm not sure if it really helps to delete stuff. */
343 #ifndef REMOVE_NODES 
344     if (dir->d_u_name != dir->d_m_name) {
345         free(dir->d_u_name);
346     }
347     free(dir->d_m_name);
348     dir->d_m_name = NULL;
349     dir->d_u_name = NULL;
350 #else /* ! REMOVE_NODES */
351
352     /* go searching for a node with at most one child */
353     if ((dir->d_left == SENTINEL) || (dir->d_right == SENTINEL)) {
354         node = dir;
355     } else {
356         node = dir->d_right;
357         while (node->d_left != SENTINEL)
358             node = node->d_left;
359     }
360
361     /* get that child */
362     leaf = (node->d_left != SENTINEL) ? node->d_left : node->d_right;
363
364     /* detach node */
365     leaf->d_back = node->d_back;
366     if (!node->d_back) {
367         vol->v_root = leaf;
368     } else if (node == node->d_back->d_left) { /* left tree */
369         node->d_back->d_left = leaf;
370     } else {
371         node->d_back->d_right = leaf;
372     }
373
374     /* we want to free node, but we also want to free the data in dir.
375     * currently, that's d_name and the directory traversal bits.
376     * we just copy the necessary bits and then fix up all the
377     * various pointers to the directory. needless to say, there are
378     * a bunch of places that store the directory struct. */
379     if (node != dir) {
380         struct dir save, *tmp;
381
382         memcpy(&save, dir, sizeof(save));
383         memcpy(dir, node, sizeof(struct dir));
384
385         /* restore the red-black bits */
386         dir->d_left = save.d_left;
387         dir->d_right = save.d_right;
388         dir->d_back = save.d_back;
389         dir->d_color = save.d_color;
390
391         if (node == vol->v_dir) {/* we may need to fix up this pointer */
392             vol->v_dir = dir;
393             rootpar.d_child = vol->v_dir;
394         } else {
395             /* if we aren't the root directory, we have parents and
396             * siblings to worry about */
397             if (dir->d_parent->d_child == node)
398                 dir->d_parent->d_child = dir;
399             dir->d_next->d_prev = dir;
400             dir->d_prev->d_next = dir;
401         }
402
403         /* fix up children. */
404         tmp = dir->d_child;
405         while (tmp) {
406             tmp->d_parent = dir;
407             tmp = (tmp == dir->d_child->d_prev) ? NULL : tmp->d_next;
408         }
409
410         if (node == curdir) /* another pointer to fixup */
411             curdir = dir;
412
413         /* we also need to fix up oforks. bleah */
414         if ((of = dir->d_ofork)) {
415             last = of->of_d_prev;
416             while (of) {
417                 of->of_dir = dir;
418                 of = (last == of) ? NULL : of->of_d_next;
419             }
420         }
421
422         /* set the node's d_name */
423         node->d_m_name = save.d_m_name;
424         node->d_u_name = save.d_u_name;
425     }
426
427     if (node->d_color == DIRTREE_COLOR_BLACK)
428         dir_rmrecolor(vol, leaf);
429
430     if (node->d_u_name != node->d_m_name) {
431         free(node->d_u_name);
432     }
433     free(node->d_m_name);
434     free(node);
435 #endif /* ! REMOVE_NODES */
436 }
437
438 /* ---------------------------------------
439  * remove the node and its childs from the tree
440  *
441  * FIXME what about opened forks with refs to it?
442  * it's an afp specs violation because you can't delete
443  * an opened forks. Now afpd doesn't care about forks opened by other 
444  * process. It's fixable within afpd if fnctl_lock, doable with smb and
445  * next to impossible for nfs and local filesystem access.
446  */
447 static void dir_invalidate( vol, dir )
448 const struct vol *vol;
449 struct dir *dir;
450 {
451         if (curdir == dir) {
452             /* v_root can't be deleted */
453                 if (movecwd(vol, vol->v_root) < 0) 
454                         printf("Yuup cant change dir to v_root\n");
455         }
456         /* FIXME */
457     dirchildremove(dir->d_parent, dir);
458         dir_remove( vol, dir );
459 }
460
461 /* ------------------------------------ */
462 static struct dir *dir_insert(vol, dir)
463             const struct vol *vol;
464 struct dir *dir;
465 {
466     struct dir  *pdir;
467
468     pdir = vol->v_root;
469     while (pdir->d_did != dir->d_did ) {
470         if ( pdir->d_did > dir->d_did ) {
471             if ( pdir->d_left == SENTINEL ) {
472                 pdir->d_left = dir;
473                 dir->d_back = pdir;
474                 return NULL;
475             }
476             pdir = pdir->d_left;
477         } else {
478             if ( pdir->d_right == SENTINEL ) {
479                 pdir->d_right = dir;
480                 dir->d_back = pdir;
481                 return NULL;
482             }
483             pdir = pdir->d_right;
484         }
485     }
486     return pdir;
487 }
488
489
490 /*
491  * attempt to extend the current dir. tree to include path
492  * as a side-effect, movecwd to that point and return the new dir
493  */
494 static struct dir *
495             extenddir( vol, dir, path )
496 struct vol      *vol;
497 struct dir      *dir;
498 struct path *path;
499 {
500     char        *p;
501
502     path->u_name = p = mtoupath(vol, path->m_name );
503     if ( of_stat( path ) != 0 ) {
504         return( NULL );
505     }
506
507     if (!S_ISDIR(path->st.st_mode)) {
508         return( NULL );
509     }
510
511     if (( dir = adddir( vol, dir, path)) == NULL ) {
512         return( NULL );
513     }
514
515     if ( movecwd( vol, dir ) < 0 ) {
516         return( NULL );
517     }
518
519     return( dir );
520 }
521
522 static int deletedir(char *dir)
523 {
524     char path[MAXPATHLEN + 1];
525     DIR *dp;
526     struct dirent       *de;
527     struct stat st;
528     size_t len;
529     int err;
530
531     if ((len = strlen(dir)) > sizeof(path))
532         return AFPERR_PARAM;
533
534     /* already gone */
535     if ((dp = opendir(dir)) == NULL)
536         return AFP_OK;
537
538     strcpy(path, dir);
539     strcat(path, "/");
540     len++;
541     while ((de = readdir(dp))) {
542         /* skip this and previous directory */
543         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
544             continue;
545
546         strncpy(path + len, de->d_name, sizeof(path) - len);
547         if (stat(path, &st) == 0) {
548             if (S_ISDIR(st.st_mode)) {
549                 if ((err = deletedir(path)) < 0) {
550                     closedir(dp);
551                     return err;
552                 }
553             } else if (unlink(path) < 0) {
554                 switch (errno) {
555                 case ENOENT :
556                     continue; /* somebody went and deleted it behind our backs. */
557                 case EROFS:
558                     err = AFPERR_VLOCK;
559                     break;
560                 case EPERM:
561                 case EACCES :
562                     err = AFPERR_ACCESS;
563                     break;
564                 default :
565                     err = AFPERR_PARAM;
566                 }
567                 closedir(dp);
568                 return err;
569             }
570         }
571     }
572     closedir(dp);
573
574     /* okay. the directory is empty. delete it. note: we already got rid
575        of .AppleDouble.  */
576     if (rmdir(dir) < 0) {
577         switch ( errno ) {
578         case ENOENT :
579             break;
580         case ENOTEMPTY : /* should never happen */
581             return( AFPERR_DIRNEMPT );
582         case EPERM:
583         case EACCES :
584             return( AFPERR_ACCESS );
585         case EROFS:
586             return AFPERR_VLOCK;
587         default :
588             return( AFPERR_PARAM );
589         }
590     }
591     return AFP_OK;
592 }
593
594 /* do a recursive copy. */
595 static int copydir(char *src, char *dst, int noadouble)
596 {
597     char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
598     DIR *dp;
599     struct dirent       *de;
600     struct stat st;
601     struct utimbuf      ut;
602     size_t slen, dlen;
603     int err;
604
605     /* doesn't exist or the path is too long. */
606     if (((slen = strlen(src)) > sizeof(spath) - 2) ||
607             ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
608             ((dp = opendir(src)) == NULL))
609         return AFPERR_PARAM;
610
611     /* try to create the destination directory */
612     if (ad_mkdir(dst, DIRBITS | 0777) < 0) {
613         closedir(dp);
614         switch ( errno ) {
615         case ENOENT :
616             return( AFPERR_NOOBJ );
617         case EROFS :
618             return( AFPERR_VLOCK );
619         case EPERM:
620         case EACCES :
621             return( AFPERR_ACCESS );
622         case EEXIST :
623             return( AFPERR_EXIST );
624         case ENOSPC :
625         case EDQUOT :
626             return( AFPERR_DFULL );
627         default :
628             return( AFPERR_PARAM );
629         }
630     }
631
632     /* set things up to copy */
633     strcpy(spath, src);
634     strcat(spath, "/");
635     slen++;
636     strcpy(dpath, dst);
637     strcat(dpath, "/");
638     dlen++;
639     err = AFP_OK;
640     while ((de = readdir(dp))) {
641         /* skip this and previous directory */
642         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
643             continue;
644
645         strncpy(spath + slen, de->d_name, sizeof(spath) - slen);
646         if (stat(spath, &st) == 0) {
647             strncpy(dpath + dlen, de->d_name, sizeof(dpath) - dlen);
648
649             if (S_ISDIR(st.st_mode)) {
650                 if ((err = copydir(spath, dpath, noadouble)) < 0)
651                     goto copydir_done;
652             } else if ((err = copyfile(spath, dpath, NULL, noadouble)) < 0) {
653                 goto copydir_done;
654
655             } else {
656                 /* keep the same time stamp. */
657                 ut.actime = ut.modtime = st.st_mtime;
658                 utime(dpath, &ut);
659             }
660         }
661     }
662
663     /* keep the same time stamp. */
664     if (stat(src, &st) == 0) {
665         ut.actime = ut.modtime = st.st_mtime;
666         utime(dst, &ut);
667     }
668
669 copydir_done:
670     closedir(dp);
671     return err;
672 }
673
674
675 /* --- public functions follow --- */
676
677 /* NOTE: we start off with at least one node (the root directory). */
678 struct dir *dirinsert( vol, dir )
679             struct vol  *vol;
680 struct dir      *dir;
681 {
682     struct dir *node;
683
684     if ((node = dir_insert(vol, dir)))
685         return node;
686
687     /* recolor the tree. the current node is red. */
688     dir->d_color = DIRTREE_COLOR_RED;
689
690     /* parent of this node has to be black. if the parent node
691     * is red, then we have a grandparent. */
692     while ((dir != vol->v_root) &&
693             (dir->d_back->d_color == DIRTREE_COLOR_RED)) {
694         /* are we on the left tree? */
695         if (dir->d_back == dir->d_back->d_back->d_left) {
696             node = dir->d_back->d_back->d_right;  /* get the right node */
697             if (node->d_color == DIRTREE_COLOR_RED) {
698                 /* we're red. we need to change to black. */
699                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
700                 node->d_color = DIRTREE_COLOR_BLACK;
701                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
702                 dir = dir->d_back->d_back; /* finished. go up. */
703             } else {
704                 if (dir == dir->d_back->d_right) {
705                     dir = dir->d_back;
706                     dir_leftrotate(vol, dir);
707                 }
708                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
709                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
710                 dir_rightrotate(vol, dir->d_back->d_back);
711             }
712         } else {
713             node = dir->d_back->d_back->d_left;
714             if (node->d_color == DIRTREE_COLOR_RED) {
715                 /* we're red. we need to change to black. */
716                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
717                 node->d_color = DIRTREE_COLOR_BLACK;
718                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
719                 dir = dir->d_back->d_back; /* finished. ascend */
720             } else {
721                 if (dir == dir->d_back->d_left) {
722                     dir = dir->d_back;
723                     dir_rightrotate(vol, dir);
724                 }
725                 dir->d_back->d_color = DIRTREE_COLOR_BLACK;
726                 dir->d_back->d_back->d_color = DIRTREE_COLOR_RED;
727                 dir_leftrotate(vol, dir->d_back->d_back);
728             }
729         }
730     }
731
732     vol->v_root->d_color = DIRTREE_COLOR_BLACK;
733     return NULL;
734 }
735
736 /* free everything down. we don't bother to recolor as this is only
737  * called to free the entire tree */
738 void dirfree( dir )
739 struct dir      *dir;
740 {
741     if (!dir || (dir == SENTINEL))
742         return;
743
744     if ( dir->d_left != SENTINEL ) {
745         dirfree( dir->d_left );
746     }
747     if ( dir->d_right != SENTINEL ) {
748         dirfree( dir->d_right );
749     }
750
751     if (dir != SENTINEL) {
752         if (dir->d_u_name != dir->d_m_name) {
753             free(dir->d_u_name);
754         }
755         free(dir->d_m_name);
756         free( dir );
757     }
758 }
759
760 /* --------------------------------------------
761  * most of the time mac name and unix name are the same 
762 */
763 struct dir *dirnew(const char *m_name, const char *u_name)
764 {
765     struct dir *dir;
766
767     dir = (struct dir *) calloc(1, sizeof( struct dir ));
768     if (!dir)
769         return NULL;
770
771     if ((dir->d_m_name = strdup(m_name)) == NULL) {
772         free(dir);
773         return NULL;
774     }
775
776     if (m_name == u_name) {
777         dir->d_u_name = dir->d_m_name;
778     }
779     else if ((dir->d_u_name = strdup(u_name)) == NULL) {
780         free(dir->d_m_name);
781         free(dir);
782         return NULL;
783     }
784
785     dir->d_left = dir->d_right = SENTINEL;
786     dir->d_next = dir->d_prev = dir;
787     return dir;
788 }
789
790 /* -------------------------------------------------- */
791 /* XXX: this needs to be changed to handle path types 
792  return
793  if it's a filename:
794       in extenddir:
795          compute unix name
796          stat the file
797       return mac name
798  if it's a dirname:
799       not in the cache
800           in extenddir
801               compute unix name
802               stat the dir
803       in the cache 
804
805   u_name can be
806   XXXX u_name can be an alias on m_name if the m_name is
807   a valid unix name.
808   
809 */
810 struct path *
811 cname( vol, dir, cpath )
812 const struct vol        *vol;
813 struct dir      *dir;
814 char    **cpath;
815 {
816     struct dir             *cdir;
817     static char            path[ MAXPATHLEN + 1];
818     static struct path ret;
819
820     char                *data, *p;
821     int                 extend = 0;
822     int                 len;
823     u_int32_t   hint;
824     u_int16_t   len16;
825     int         size = 0;
826     char        sep;
827         
828     data = *cpath;
829     afp_errno = AFPERR_NOOBJ;
830     switch (ret.m_type = *data) { /* path type */
831     case 2:
832        data++;
833        len = (unsigned char) *data++;
834        size = 2;
835        sep = 0;
836        break;
837     case 3:
838        if (afp_version >= 30) {
839            data++;
840            memcpy(&hint, data, sizeof(hint));
841            hint = ntohl(hint);
842            data += sizeof(hint);
843            
844            memcpy(&len16, data, sizeof(len16));
845            len = ntohs(len16);
846            data += 2;
847            size = 7;
848            sep = 0; /* '/';*/
849            break;
850         }
851         /* else it's an error */
852     default:
853         afp_errno = AFPERR_PARAM;
854         return( NULL );
855     
856     }
857     *cpath += len + size;
858     *path = '\0';
859     ret.m_name = path;
860     ret.st_errno = 0;
861     ret.st_valid = 0;
862     for ( ;; ) {
863         if ( len == 0 ) {
864             if ( !extend && movecwd( vol, dir ) < 0 ) {
865                 /* it's tricky:
866                    movecwd failed some of dir path are not there anymore.
867                    FIXME Is it true with other errors?
868                    so we remove dir from the cache 
869                 */
870                 if (dir->d_did == DIRDID_ROOT_PARENT) 
871                     return NULL;
872                 if (afp_errno == AFPERR_ACCESS)
873                     return NULL;                
874
875                 dir_invalidate(vol, dir);
876                 return NULL;
877             }
878             if (*path == '\0') {
879                ret.u_name = ".";
880             }               
881             return &ret;
882         }
883
884         if (*data == sep ) {
885             data++;
886             len--;
887         }
888         while (*data == sep && len > 0 ) {
889             if ( dir->d_parent == NULL ) {
890                 return NULL;
891             }
892             dir = dir->d_parent;
893             data++;
894             len--;
895         }
896
897         /* would this be faster with strlen + strncpy? */
898         p = path;
899         while ( *data != sep && len > 0 ) {
900             *p++ = *data++;
901             len--;
902         }
903
904         /* short cut bits by chopping off a trailing \0. this also
905                   makes the traversal happy w/ filenames at the end of the
906                   cname. */
907         if (len == 1)
908             len--;
909
910         *p = '\0';
911
912         if ( p != path ) { /* we got something */
913             if ( !extend ) {
914                 cdir = dir->d_child;
915                 while (cdir) {
916                     if ( strcmp( cdir->d_m_name, path ) == 0 ) {
917                         break;
918                     }
919                     cdir = (cdir == dir->d_child->d_prev) ? NULL :
920                            cdir->d_next;
921                 }
922                 if ( cdir == NULL ) {
923                     ++extend;
924                     /* if dir == curdir it always succeed,
925                        even if curdir is deleted. 
926                        it's not a pb because it will fail in extenddir
927                     */
928                     if ( movecwd( vol, dir ) < 0 ) {
929                         /* dir is not valid anymore 
930                            we delete dir from the cache and abort.
931                         */
932                         if ( dir->d_did != DIRDID_ROOT_PARENT && 
933                               (afp_errno != AFPERR_ACCESS)) {
934                             dir_invalidate(vol, dir);
935                         }
936                         return NULL;
937                     }
938                     cdir = extenddir( vol, dir, &ret );
939                 }
940
941             } else {
942                 cdir = extenddir( vol, dir, &ret );
943             }
944
945             if ( cdir == NULL ) {
946
947                 if ( len > 0 ) {
948                     return NULL;
949                 }
950
951             } else {
952                 dir = cdir;     
953                 *path = '\0';
954             }
955         }
956     }
957 }
958
959 /*
960  * Move curdir to dir, with a possible chdir()
961  */
962 int movecwd( vol, dir)
963 const struct vol        *vol;
964 struct dir      *dir;
965 {
966     char path[MAXPATHLEN + 1];
967     struct dir  *d;
968     char        *p, *u;
969     int         n;
970
971     if ( dir == curdir ) {
972         return( 0 );
973     }
974     if ( dir->d_did == DIRDID_ROOT_PARENT) {
975         afp_errno = AFPERR_PARAM;
976         return( -1 );
977     }
978
979     p = path + sizeof(path) - 1;
980     *p-- = '\0';
981     *p = '.';
982     for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
983         u = d->d_u_name;
984         n = strlen( u );
985         if (p -n -1 < path) {
986             afp_errno = AFPERR_PARAM;
987             return -1;
988         }
989         *--p = '/';
990         p -= n;
991         strncpy( p, u, n );
992     }
993     if ( d != curdir ) {
994         n = strlen( vol->v_path );
995         if (p -n -1 < path) {
996             afp_errno = AFPERR_PARAM;
997             return -1;
998         }
999         *--p = '/';
1000         p -= n;
1001         strncpy( p, vol->v_path, n );
1002     }
1003     if ( chdir( p ) < 0 ) {
1004         switch (errno) {
1005         case EACCES:
1006         case EPERM:
1007             afp_errno = AFPERR_ACCESS;
1008             break;
1009         default:
1010             afp_errno = AFPERR_NOOBJ;
1011         
1012         }
1013         return( -1 );
1014     }
1015     curdir = dir;
1016     return( 0 );
1017 }
1018
1019 /*
1020  * We can't use unix file's perm to support Apple's inherited protection modes.
1021  * If we aren't the file's owner we can't change its perms when moving it and smb
1022  * nfs,... don't even try.
1023 */
1024 #define AFP_CHECK_ACCESS 
1025
1026 int check_access(char *path, int mode)
1027 {
1028 #ifdef AFP_CHECK_ACCESS
1029 struct maccess ma;
1030 char *p;
1031
1032     p = ad_dir(path);
1033     if (!p)
1034        return -1;
1035
1036     accessmode(p, &ma, curdir, NULL);
1037     if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1038         return -1;
1039     if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1040         return -1;
1041 #endif
1042     return 0;
1043 }
1044
1045 /* ------------------------------ 
1046    (".", curdir)
1047    (name, dir) with curdir:name == dir, from afp_enumerate
1048 */
1049
1050 int getdirparams(const struct vol *vol,
1051                  u_int16_t bitmap, struct path *s_path,
1052                  struct dir *dir, 
1053                  char *buf, int *buflen )
1054 {
1055     struct maccess      ma;
1056     struct adouble      ad;
1057     char                *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1058     int                 bit = 0, isad = 0;
1059     u_int32_t           aint;
1060     u_int16_t           ashort;
1061     int                 ret;
1062     u_int32_t           utf8 = 0;
1063     struct stat *st = &s_path->st;
1064     char *upath = s_path->u_name;
1065     
1066     if ((bitmap & ((1 << DIRPBIT_ATTR)  |
1067                    (1 << DIRPBIT_CDATE) |
1068                    (1 << DIRPBIT_MDATE) |
1069                    (1 << DIRPBIT_BDATE) |
1070                    (1 << DIRPBIT_FINFO)))) {
1071         memset(&ad, 0, sizeof(ad));
1072         if ( !ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1073                   DIRBITS | 0777, &ad)) {
1074             isad = 1;
1075         }
1076     }
1077
1078     data = buf;
1079     while ( bitmap != 0 ) {
1080         while (( bitmap & 1 ) == 0 ) {
1081             bitmap = bitmap>>1;
1082             bit++;
1083         }
1084
1085         switch ( bit ) {
1086         case DIRPBIT_ATTR :
1087             if ( isad ) {
1088                 ad_getattr(&ad, &ashort);
1089             } else if (*upath == '.' && strcmp(upath, ".") &&
1090                        strcmp(upath, "..")) {
1091                 ashort = htons(ATTRBIT_INVISIBLE);
1092             } else
1093                 ashort = 0;
1094             ashort |= htons(ATTRBIT_SHARED);
1095             memcpy( data, &ashort, sizeof( ashort ));
1096             data += sizeof( ashort );
1097             break;
1098
1099         case DIRPBIT_PDID :
1100             if ( dir->d_did == DIRDID_ROOT) {
1101                 aint = DIRDID_ROOT_PARENT;
1102             } else if (dir->d_did == DIRDID_ROOT_PARENT) {
1103                 aint = 0;
1104             } else {
1105                 aint = dir->d_parent->d_did;
1106             }
1107             memcpy( data, &aint, sizeof( aint ));
1108             data += sizeof( aint );
1109             break;
1110
1111         case DIRPBIT_CDATE :
1112             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1113                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1114             memcpy( data, &aint, sizeof( aint ));
1115             data += sizeof( aint );
1116             break;
1117
1118         case DIRPBIT_MDATE :
1119             aint = AD_DATE_FROM_UNIX(st->st_mtime);
1120             memcpy( data, &aint, sizeof( aint ));
1121             data += sizeof( aint );
1122             break;
1123
1124         case DIRPBIT_BDATE :
1125             if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1126                 aint = AD_DATE_START;
1127             memcpy( data, &aint, sizeof( aint ));
1128             data += sizeof( aint );
1129             break;
1130
1131         case DIRPBIT_FINFO :
1132             if ( isad ) {
1133                 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1134             } else { /* no appledouble */
1135                 memset( data, 0, 32 );
1136                 /* set default view -- this also gets done in ad_open() */
1137                 ashort = htons(FINDERINFO_CLOSEDVIEW);
1138                 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1139
1140                 /* dot files are by default invisible */
1141                 if (*upath == '.' && strcmp(upath, ".") &&
1142                         strcmp(upath, "..")) {
1143                     ashort = htons(FINDERINFO_INVISIBLE);
1144                     memcpy(data + FINDERINFO_FRFLAGOFF,
1145                            &ashort, sizeof(ashort));
1146                 }
1147             }
1148             data += 32;
1149             break;
1150
1151         case DIRPBIT_LNAME :
1152             if (dir->d_m_name) /* root of parent can have a null name */
1153                 l_nameoff = data;
1154             else
1155                 memset(data, 0, sizeof(u_int16_t));
1156             data += sizeof( u_int16_t );
1157             break;
1158
1159         case DIRPBIT_SNAME :
1160             memset(data, 0, sizeof(u_int16_t));
1161             data += sizeof( u_int16_t );
1162             break;
1163
1164         case DIRPBIT_DID :
1165             memcpy( data, &dir->d_did, sizeof( aint ));
1166             data += sizeof( aint );
1167             break;
1168
1169         case DIRPBIT_OFFCNT :
1170             ashort = 0;
1171             /* this needs to handle current directory access rights */
1172             if (st->st_ctime == dir->ctime) {
1173                ashort = dir->offcnt;
1174             }
1175             else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1176                 ashort = ret;
1177                 dir->offcnt = ashort;
1178                 dir->ctime = st->st_ctime;
1179             }
1180             ashort = htons( ashort );
1181             memcpy( data, &ashort, sizeof( ashort ));
1182             data += sizeof( ashort );
1183             break;
1184
1185         case DIRPBIT_UID :
1186             aint = htonl(st->st_uid);
1187             memcpy( data, &aint, sizeof( aint ));
1188             data += sizeof( aint );
1189             break;
1190
1191         case DIRPBIT_GID :
1192             aint = htonl(st->st_gid);
1193             memcpy( data, &aint, sizeof( aint ));
1194             data += sizeof( aint );
1195             break;
1196
1197         case DIRPBIT_ACCESS :
1198             accessmode( upath, &ma, dir , st);
1199
1200             *data++ = ma.ma_user;
1201             *data++ = ma.ma_world;
1202             *data++ = ma.ma_group;
1203             *data++ = ma.ma_owner;
1204             break;
1205
1206             /* Client has requested the ProDOS information block.
1207                Just pass back the same basic block for all
1208                directories. <shirsch@ibm.net> */
1209         case DIRPBIT_PDINFO :                     
1210             if (afp_version >= 30) { /* UTF8 name */
1211                 utf8 = kTextEncodingUTF8;
1212                 if (dir->d_m_name) /* root of parent can have a null name */
1213                     utf_nameoff = data;
1214                 else
1215                     memset(data, 0, sizeof(u_int16_t));
1216                 data += sizeof( u_int16_t );
1217                 aint = 0;
1218                 memcpy(data, &aint, sizeof( aint ));
1219                 data += sizeof( aint );
1220             }
1221             else { /* ProDOS Info Block */
1222                 *data++ = 0x0f;
1223                 *data++ = 0;
1224                 ashort = htons( 0x0200 );
1225                 memcpy( data, &ashort, sizeof( ashort ));
1226                 data += sizeof( ashort );
1227                 memset( data, 0, sizeof( ashort ));
1228                 data += sizeof( ashort );
1229             }
1230             break;
1231
1232         default :
1233             if ( isad ) {
1234                 ad_close( &ad, ADFLAGS_HF );
1235             }
1236             return( AFPERR_BITMAP );
1237         }
1238         bitmap = bitmap>>1;
1239         bit++;
1240     }
1241     if ( l_nameoff ) {
1242         ashort = htons( data - buf );
1243         memcpy( l_nameoff, &ashort, sizeof( ashort ));
1244         data = set_name(data, dir->d_m_name, 0);
1245     }
1246     if ( utf_nameoff ) {
1247         ashort = htons( data - buf );
1248         memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1249         data = set_name(data, dir->d_m_name, utf8);
1250     }
1251     if ( isad ) {
1252         ad_close( &ad, ADFLAGS_HF );
1253     }
1254     *buflen = data - buf;
1255     return( AFP_OK );
1256 }
1257
1258 /* ----------------------------- */
1259 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1260 AFPObj      *obj;
1261 char    *ibuf, *rbuf;
1262 int             ibuflen, *rbuflen;
1263 {
1264     struct vol  *vol;
1265     struct dir  *dir;
1266     struct path *path;
1267     u_int16_t   vid, bitmap;
1268     u_int32_t   did;
1269     int         rc;
1270
1271     *rbuflen = 0;
1272     ibuf += 2;
1273     memcpy( &vid, ibuf, sizeof( vid ));
1274     ibuf += sizeof( vid );
1275
1276     if (NULL == ( vol = getvolbyvid( vid )) ) {
1277         return( AFPERR_PARAM );
1278     }
1279
1280     if (vol->v_flags & AFPVOL_RO)
1281         return AFPERR_VLOCK;
1282
1283     memcpy( &did, ibuf, sizeof( did ));
1284     ibuf += sizeof( int );
1285
1286     if (NULL == ( dir = dirlookup( vol, did )) ) {
1287         return afp_errno;
1288     }
1289
1290     memcpy( &bitmap, ibuf, sizeof( bitmap ));
1291     bitmap = ntohs( bitmap );
1292     ibuf += sizeof( bitmap );
1293
1294     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1295         return afp_errno;
1296     }
1297
1298     if ( *path->m_name != '\0' ) {
1299         return( AFPERR_BADTYPE ); /* not a directory */
1300     }
1301
1302     /*
1303      * If ibuf is odd, make it even.
1304      */
1305     if ((u_long)ibuf & 1 ) {
1306         ibuf++;
1307     }
1308
1309     if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1310         setvoltime(obj, vol );
1311     }
1312     return( rc );
1313 }
1314
1315 /*
1316  * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic  
1317  *
1318  * assume path == '\0' eg. it's a directory in canonical form
1319 */
1320
1321 struct path Cur_Path = {
1322     0,
1323     "",  /* mac name */
1324     ".", /* unix name */
1325     0,  /* stat is not set */
1326     0,  /* */
1327 };
1328
1329 int setdirparams(const struct vol *vol, 
1330                  struct path *path, u_int16_t bitmap, char *buf )
1331 {
1332     struct maccess      ma;
1333     struct adouble      ad;
1334     struct utimbuf      ut;
1335     struct timeval      tv;
1336
1337     char                *upath;
1338     int                 bit = 0, aint, isad = 1;
1339     u_int16_t           ashort, bshort;
1340     int                 err = AFP_OK;
1341     int                 change_mdate = 0;
1342     int                 change_parent_mdate = 0;
1343     int                 newdate = 0;
1344
1345     upath = path->u_name;
1346     memset(&ad, 0, sizeof(ad));
1347
1348     if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1349                  O_RDWR|O_CREAT, 0666, &ad) < 0) {
1350         /*
1351          * Check to see what we're trying to set.  If it's anything
1352          * but ACCESS, UID, or GID, give an error.  If it's any of those
1353          * three, we don't need the ad to be open, so just continue.
1354          *
1355          * note: we also don't need to worry about mdate. also, be quiet
1356          *       if we're using the noadouble option.
1357          */
1358         if (!vol_noadouble(vol) && (bitmap &
1359                                     ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1360                                       (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1361             return AFPERR_ACCESS;
1362         }
1363
1364         isad = 0;
1365     } else {
1366         /*
1367          * Check to see if a create was necessary. If it was, we'll want
1368          * to set our name, etc.
1369          */
1370         if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1371             ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_m_name ));
1372             memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_m_name,
1373                     ad_getentrylen( &ad, ADEID_NAME ));
1374         }
1375     }
1376
1377     while ( bitmap != 0 ) {
1378         while (( bitmap & 1 ) == 0 ) {
1379             bitmap = bitmap>>1;
1380             bit++;
1381         }
1382
1383         switch( bit ) {
1384         case DIRPBIT_ATTR :
1385             change_mdate = 1;
1386             if (isad) {
1387                 memcpy( &ashort, buf, sizeof( ashort ));
1388                 ad_getattr(&ad, &bshort);
1389                 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1390                     bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1391                 } else {
1392                     bshort &= ~ashort;
1393                 }
1394                 ad_setattr(&ad, bshort);
1395                 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1396                    change_parent_mdate = 1;
1397             }
1398             buf += sizeof( ashort );
1399             break;
1400
1401         case DIRPBIT_CDATE :
1402             change_mdate = 1;
1403             if (isad) {
1404                 memcpy(&aint, buf, sizeof(aint));
1405                 ad_setdate(&ad, AD_DATE_CREATE, aint);
1406             }
1407             buf += sizeof( aint );
1408             break;
1409
1410         case DIRPBIT_MDATE :
1411             memcpy(&newdate, buf, sizeof(newdate));
1412             buf += sizeof( newdate );
1413             break;
1414
1415         case DIRPBIT_BDATE :
1416             change_mdate = 1;
1417             if (isad) {
1418                 memcpy(&aint, buf, sizeof(aint));
1419                 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1420             }
1421             buf += sizeof( aint );
1422             break;
1423
1424         case DIRPBIT_FINFO :
1425             change_mdate = 1;
1426             /*
1427              * Alright, we admit it, this is *really* sick!
1428              * The 4 bytes that we don't copy, when we're dealing
1429              * with the root of a volume, are the directory's
1430              * location information. This eliminates that annoying
1431              * behavior one sees when mounting above another mount
1432              * point.
1433              */
1434             if (isad) {
1435                 if (  curdir->d_did == DIRDID_ROOT ) {
1436                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1437                     memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1438                 } else {
1439                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1440                 }
1441             }
1442             buf += 32;
1443             break;
1444
1445         case DIRPBIT_UID :      /* What kind of loser mounts as root? */
1446             change_parent_mdate = 1;
1447             memcpy( &aint, buf, sizeof(aint));
1448             buf += sizeof( aint );
1449             if ( (curdir->d_did == DIRDID_ROOT) &&
1450                     (setdeskowner( ntohl(aint), -1 ) < 0)) {
1451                 switch ( errno ) {
1452                 case EPERM :
1453                 case EACCES :
1454                     err = AFPERR_ACCESS;
1455                     goto setdirparam_done;
1456                     break;
1457                 case EROFS :
1458                     err = AFPERR_VLOCK;
1459                     goto setdirparam_done;
1460                     break;
1461                 default :
1462                     LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1463                         strerror(errno) );
1464                     if (!isad) {
1465                         err = AFPERR_PARAM;
1466                         goto setdirparam_done;
1467                     }
1468                     break;
1469                 }
1470             }
1471             if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1472                 switch ( errno ) {
1473                 case EPERM :
1474                 case EACCES :
1475                     err = AFPERR_ACCESS;
1476                     goto setdirparam_done;
1477                     break;
1478                 case EROFS :
1479                     err = AFPERR_VLOCK;
1480                     goto setdirparam_done;
1481                     break;
1482                 default :
1483                     LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1484                         strerror(errno) );
1485                     break;
1486                 }
1487             }
1488             break;
1489         case DIRPBIT_GID :
1490             change_parent_mdate = 1;
1491             memcpy( &aint, buf, sizeof( aint ));
1492             buf += sizeof( aint );
1493             if (curdir->d_did == DIRDID_ROOT)
1494                 setdeskowner( -1, ntohl(aint) );
1495
1496 #if 0       /* don't error if we can't set the desktop owner. */
1497             switch ( errno ) {
1498             case EPERM :
1499             case EACCES :
1500                 err = AFPERR_ACCESS;
1501                 goto setdirparam_done;
1502                 break;
1503             case EROFS :
1504                 err = AFPERR_VLOCK;
1505                 goto setdirparam_done;
1506                 break;
1507             default :
1508                 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1509                 if (!isad) {
1510                     err = AFPERR_PARAM;
1511                     goto setdirparam_done;
1512                 }
1513                 break;
1514             }
1515 #endif /* 0 */
1516
1517             if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1518                 switch ( errno ) {
1519                 case EPERM :
1520                 case EACCES :
1521                     err = AFPERR_ACCESS;
1522                     goto setdirparam_done;
1523                     break;
1524                 case EROFS :
1525                     err = AFPERR_VLOCK;
1526                     goto setdirparam_done;
1527                     break;
1528                 default :
1529                     LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1530                         strerror(errno) );
1531                     break;
1532                 }
1533             }
1534             break;
1535
1536         case DIRPBIT_ACCESS :
1537             change_mdate = 1;
1538             change_parent_mdate = 1;
1539             ma.ma_user = *buf++;
1540             ma.ma_world = *buf++;
1541             ma.ma_group = *buf++;
1542             ma.ma_owner = *buf++;
1543
1544             if (curdir->d_did == DIRDID_ROOT)
1545                 setdeskmode(mtoumode( &ma ));
1546 #if 0 /* don't error if we can't set the desktop mode */
1547             switch ( errno ) {
1548             case EPERM :
1549             case EACCES :
1550                 err = AFPERR_ACCESS;
1551                 goto setdirparam_done;
1552             case EROFS :
1553                 err = AFPERR_VLOCK;
1554                 goto setdirparam_done;
1555             default :
1556                 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1557                     strerror(errno) );
1558                 break;
1559                 err = AFPERR_PARAM;
1560                 goto setdirparam_done;
1561             }
1562 #endif /* 0 */
1563
1564             if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1565                          (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1566                 switch ( errno ) {
1567                 case EPERM :
1568                 case EACCES :
1569                     err = AFPERR_ACCESS;
1570                     goto setdirparam_done;
1571                 case EROFS :
1572                     err = AFPERR_VLOCK;
1573                     goto setdirparam_done;
1574                 default :
1575                     LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1576                         strerror(errno) );
1577                     err = AFPERR_PARAM;
1578                     goto setdirparam_done;
1579                 }
1580             }
1581             break;
1582
1583         /* Ignore what the client thinks we should do to the
1584            ProDOS information block.  Skip over the data and
1585            report nothing amiss. <shirsch@ibm.net> */
1586         case DIRPBIT_PDINFO :
1587             if (afp_version < 30) {
1588                 buf += 6;
1589                 break;
1590             }
1591         default :
1592             err = AFPERR_BITMAP;
1593             goto setdirparam_done;
1594             break;
1595         }
1596
1597         bitmap = bitmap>>1;
1598         bit++;
1599     }
1600
1601 setdirparam_done:
1602     if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1603        newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1604     }
1605     if (newdate) {
1606        if (isad)
1607           ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1608        ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1609        utime(upath, &ut);
1610     }
1611
1612     if ( isad ) {
1613         ad_flush( &ad, ADFLAGS_HF );
1614         ad_close( &ad, ADFLAGS_HF );
1615     }
1616
1617     if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1618             && gettimeofday(&tv, NULL) == 0) {
1619        if (!movecwd(vol, curdir->d_parent)) {
1620            newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1621            bitmap = 1<<DIRPBIT_MDATE;
1622            setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1623            /* should we reset curdir ?*/
1624        }
1625     }
1626
1627     return err;
1628 }
1629
1630 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1631 AFPObj      *obj;
1632 char    *ibuf, *rbuf;
1633 int             ibuflen, *rbuflen;
1634 {
1635     struct adouble      ad;
1636     struct vol          *vol;
1637     struct dir          *dir;
1638     char                *upath;
1639     struct path         *s_path;
1640     u_int32_t           did;
1641     u_int16_t           vid;
1642
1643     *rbuflen = 0;
1644     ibuf += 2;
1645
1646     memcpy( &vid, ibuf, sizeof( vid ));
1647     ibuf += sizeof( vid );
1648     if (NULL == ( vol = getvolbyvid( vid )) ) {
1649         return( AFPERR_PARAM );
1650     }
1651
1652     if (vol->v_flags & AFPVOL_RO)
1653         return AFPERR_VLOCK;
1654
1655     memcpy( &did, ibuf, sizeof( did ));
1656     ibuf += sizeof( did );
1657     if (NULL == ( dir = dirlookup( vol, did )) ) {
1658         return( AFPERR_NOOBJ );
1659     }
1660
1661     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1662         return afp_errno;
1663     }
1664     /* FIXME check done elswhere? cname was able to move curdir to it! */
1665     if (*s_path->m_name == '\0')
1666         return AFPERR_EXIST;
1667
1668     upath = s_path->u_name;
1669     {
1670     int ret;
1671         if (0 != (ret = check_name(vol, upath))) {
1672             return  ret;
1673         }
1674     }
1675
1676     if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1677         switch ( errno ) {
1678         case ENOENT :
1679             return( AFPERR_NOOBJ );
1680         case EROFS :
1681             return( AFPERR_VLOCK );
1682         case EACCES :
1683             return( AFPERR_ACCESS );
1684         case EEXIST :
1685             return( AFPERR_EXIST );
1686         case ENOSPC :
1687         case EDQUOT :
1688             return( AFPERR_DFULL );
1689         default :
1690             return( AFPERR_PARAM );
1691         }
1692     }
1693
1694     if (of_stat(s_path) < 0) {
1695         return AFPERR_MISC;
1696     }
1697     curdir->offcnt++;
1698     if ((dir = adddir( vol, curdir, s_path)) == NULL) {
1699         return AFPERR_MISC;
1700     }
1701
1702     if ( movecwd( vol, dir ) < 0 ) {
1703         return( AFPERR_PARAM );
1704     }
1705
1706     memset(&ad, 0, sizeof(ad));
1707     if (ad_open( ".", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1708                  O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
1709         if (vol_noadouble(vol))
1710             goto createdir_done;
1711         return( AFPERR_ACCESS );
1712     }
1713
1714     ad_setentrylen( &ad, ADEID_NAME, strlen( s_path->m_name ));
1715     memcpy( ad_entry( &ad, ADEID_NAME ), s_path->m_name,
1716             ad_getentrylen( &ad, ADEID_NAME ));
1717     ad_flush( &ad, ADFLAGS_HF );
1718     ad_close( &ad, ADFLAGS_HF );
1719
1720 createdir_done:
1721     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1722     *rbuflen = sizeof( u_int32_t );
1723     setvoltime(obj, vol );
1724     return( AFP_OK );
1725 }
1726
1727 /*
1728  * dst       new unix filename (not a pathname)
1729  * newname   new mac name
1730  * newparent curdir
1731  *
1732 */
1733 int renamedir(src, dst, dir, newparent, newname, noadouble)
1734 char    *src, *dst, *newname;
1735 struct dir      *dir, *newparent;
1736 const int noadouble;
1737 {
1738     struct adouble      ad;
1739     struct dir          *parent;
1740     char                *buf;
1741     int                 len, err;
1742         
1743     /* existence check moved to afp_moveandrename */
1744     if ( unix_rename( src, dst ) < 0 ) {
1745         switch ( errno ) {
1746         case ENOENT :
1747             return( AFPERR_NOOBJ );
1748         case EACCES :
1749             return( AFPERR_ACCESS );
1750         case EROFS:
1751             return AFPERR_VLOCK;
1752         case EINVAL:
1753             /* tried to move directory into a subdirectory of itself */
1754             return AFPERR_CANTMOVE;
1755         case EXDEV:
1756             /* this needs to copy and delete. bleah. that means we have
1757              * to deal with entire directory hierarchies. */
1758             if ((err = copydir(src, dst, noadouble)) < 0) {
1759                 deletedir(dst);
1760                 return err;
1761             }
1762             if ((err = deletedir(src)) < 0)
1763                 return err;
1764             break;
1765         default :
1766             return( AFPERR_PARAM );
1767         }
1768     }
1769
1770     memset(&ad, 0, sizeof(ad));
1771     len = strlen( newname );
1772     /* rename() succeeded so we need to update our tree even if we can't open
1773      * .Parent
1774     */
1775     if ( !ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad)) {
1776         ad_setentrylen( &ad, ADEID_NAME, len );
1777         memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1778         ad_flush( &ad, ADFLAGS_HF );
1779         ad_close( &ad, ADFLAGS_HF );
1780     }
1781     
1782     if (dir->d_m_name == dir->d_u_name)
1783         dir->d_u_name = NULL;
1784
1785     if ((buf = (char *) realloc( dir->d_m_name, len + 1 )) == NULL ) {
1786         LOG(log_error, logtype_afpd, "renamedir: realloc mac name: %s", strerror(errno) );
1787         /* FIXME : fatal ? */
1788         return AFPERR_MISC;
1789     }
1790     dir->d_m_name = buf;
1791     strcpy( dir->d_m_name, newname );
1792
1793     if (newname == dst) {
1794         free(dir->d_u_name);
1795         dir->d_u_name = dir->d_m_name;
1796     }
1797     else {
1798         if ((buf = (char *) realloc( dir->d_u_name, strlen(dst) + 1 )) == NULL ) {
1799             LOG(log_error, logtype_afpd, "renamedir: realloc unix name: %s", strerror(errno) );
1800             return AFPERR_MISC;
1801         }
1802         dir->d_u_name = buf;
1803         strcpy( dir->d_u_name, dst );
1804     }
1805
1806     if (( parent = dir->d_parent ) == NULL ) {
1807         return( AFP_OK );
1808     }
1809     if ( parent == newparent ) {
1810         return( AFP_OK );
1811     }
1812
1813     /* detach from old parent and add to new one. */
1814     dirchildremove(parent, dir);
1815     dir->d_parent = newparent;
1816     dirchildadd(newparent, dir);
1817     return( AFP_OK );
1818 }
1819
1820 #define DOT_APPLEDOUBLE_LEN 13
1821 /* delete an empty directory */
1822 int deletecurdir( vol, path, pathlen )
1823 const struct vol        *vol;
1824 char *path;
1825 int pathlen;
1826 {
1827     struct dirent *de;
1828     struct stat st;
1829     struct dir  *fdir;
1830     DIR *dp;
1831     struct adouble      ad;
1832     u_int16_t           ashort;
1833
1834     if ( curdir->d_parent == NULL ) {
1835         return( AFPERR_ACCESS );
1836     }
1837
1838     if ( curdir->d_child != NULL ) {
1839         return( AFPERR_DIRNEMPT );
1840     }
1841
1842     fdir = curdir;
1843
1844     memset(&ad, 0, sizeof(ad));
1845     if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1846                   DIRBITS | 0777, &ad) == 0 ) {
1847
1848         ad_getattr(&ad, &ashort);
1849         ad_close( &ad, ADFLAGS_HF );
1850         if ((ashort & htons(ATTRBIT_NODELETE))) {
1851             return  AFPERR_OLOCK;
1852         }
1853     }
1854
1855     /* delete stray .AppleDouble files. this happens to get .Parent files
1856        as well. */
1857     if ((dp = opendir(".AppleDouble"))) {
1858         strcpy(path, ".AppleDouble/");
1859         while ((de = readdir(dp))) {
1860             /* skip this and previous directory */
1861             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1862                 continue;
1863
1864             /* bail if the file exists in the current directory.
1865              * note: this will not fail with dangling symlinks */
1866             if (stat(de->d_name, &st) == 0) {
1867                 closedir(dp);
1868                 return AFPERR_DIRNEMPT;
1869             }
1870
1871             strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1872             if (unlink(path) < 0) {
1873                 closedir(dp);
1874                 switch (errno) {
1875                 case EPERM:
1876                 case EACCES :
1877                     return( AFPERR_ACCESS );
1878                 case EROFS:
1879                     return AFPERR_VLOCK;
1880                 case ENOENT :
1881                     continue;
1882                 default :
1883                     return( AFPERR_PARAM );
1884                 }
1885             }
1886         }
1887         closedir(dp);
1888     }
1889
1890     if ( rmdir( ".AppleDouble" ) < 0 ) {
1891         switch ( errno ) {
1892         case ENOENT :
1893             break;
1894         case ENOTEMPTY :
1895             return( AFPERR_DIRNEMPT );
1896         case EROFS:
1897             return AFPERR_VLOCK;
1898         case EPERM:
1899         case EACCES :
1900             return( AFPERR_ACCESS );
1901         default :
1902             return( AFPERR_PARAM );
1903         }
1904     }
1905
1906     /* now get rid of dangling symlinks */
1907     if ((dp = opendir("."))) {
1908         while ((de = readdir(dp))) {
1909             /* skip this and previous directory */
1910             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1911                 continue;
1912
1913             /* bail if it's not a symlink */
1914             if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1915                 return AFPERR_DIRNEMPT;
1916             }
1917
1918             if (unlink(de->d_name) < 0) {
1919                 switch (errno) {
1920                 case EPERM:
1921                 case EACCES :
1922                     return( AFPERR_ACCESS );
1923                 case EROFS:
1924                     return AFPERR_VLOCK;
1925                 case ENOENT :
1926                     continue;
1927                 default :
1928                     return( AFPERR_PARAM );
1929                 }
1930             }
1931         }
1932         closedir(dp);
1933     }
1934
1935     if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1936         return afp_errno;
1937     }
1938
1939     if ( rmdir(fdir->d_u_name) < 0 ) {
1940         switch ( errno ) {
1941         case ENOENT :
1942             return( AFPERR_NOOBJ );
1943         case ENOTEMPTY :
1944             return( AFPERR_DIRNEMPT );
1945         case EPERM:
1946         case EACCES :
1947             return( AFPERR_ACCESS );
1948         case EROFS:
1949             return AFPERR_VLOCK;
1950         default :
1951             return( AFPERR_PARAM );
1952         }
1953     }
1954
1955     dirchildremove(curdir, fdir);
1956 #ifdef CNID_DB
1957     cnid_delete(vol->v_db, fdir->d_did);
1958 #endif /* CNID_DB */
1959     dir_remove( vol, fdir );
1960
1961     return( AFP_OK );
1962 }
1963
1964 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1965 AFPObj      *obj;
1966 char    *ibuf, *rbuf;
1967 int             ibuflen, *rbuflen;
1968 {
1969     struct passwd       *pw;
1970     struct group        *gr;
1971     char                *name;
1972     u_int32_t           id;
1973     int                 len, sfunc;
1974     int         utf8 = 0;
1975     
1976     ibuf++;
1977     sfunc = (unsigned char) *ibuf++;
1978     memcpy( &id, ibuf, sizeof( id ));
1979
1980     id = ntohl(id);
1981     *rbuflen = 0;
1982
1983     if ( id != 0 ) {
1984         switch ( sfunc ) {
1985         case 1 :
1986         case 3 :/* unicode */
1987             if (( pw = getpwuid( id )) == NULL ) {
1988                 return( AFPERR_NOITEM );
1989             }
1990             name = pw->pw_name;
1991             break;
1992
1993         case 2 :
1994         case 4 : /* unicode */
1995             if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1996                 return( AFPERR_NOITEM );
1997             }
1998             name = gr->gr_name;
1999             break;
2000
2001         default :
2002             return( AFPERR_PARAM );
2003         }
2004         switch ( sfunc ) {
2005         case 3:
2006         case 4:
2007             if (afp_version < 30) {
2008                 return( AFPERR_PARAM );
2009             }
2010             utf8 = 1;
2011             /* map to unicode */
2012             break;            
2013         }
2014         len = strlen( name );
2015
2016     } else {
2017         len = 0;
2018         name = NULL;
2019     }
2020     if (utf8) {
2021         u_int16_t tp = htons(len);
2022         memcpy(rbuf, &tp, sizeof(tp));
2023         rbuf += sizeof(tp);
2024         *rbuflen += 2;
2025     }
2026     else {
2027         *rbuf++ = len;
2028         *rbuflen += 1;
2029     }
2030     if ( len > 0 ) {
2031         memcpy( rbuf, name, len );
2032     }
2033     *rbuflen += len;
2034     return( AFP_OK );
2035 }
2036
2037 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
2038 AFPObj      *obj;
2039 char    *ibuf, *rbuf;
2040 int             ibuflen, *rbuflen;
2041 {
2042     struct passwd       *pw;
2043     struct group        *gr;
2044     int             len, sfunc;
2045     u_int32_t       id;
2046     u_int16_t       ulen;
2047
2048     ibuf++;
2049     sfunc = (unsigned char) *ibuf++;
2050     switch ( sfunc ) {
2051     case 1 : 
2052     case 2 : /* unicode */
2053         memcpy(&ulen, ibuf, sizeof(ulen));
2054         len = ntohs(ulen);
2055         ibuf += 2;
2056         break;
2057     case 3 :
2058     case 4 :
2059         len = (unsigned char) *ibuf++;
2060         break;
2061     default :
2062         *rbuflen = 0;
2063         return( AFPERR_PARAM );
2064     }
2065
2066     ibuf[ len ] = '\0';
2067
2068     if ( len != 0 ) {
2069         switch ( sfunc ) {
2070         case 1 : /* unicode */
2071         case 3 :
2072             if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
2073                 *rbuflen = 0;
2074                 return( AFPERR_NOITEM );
2075             }
2076             id = pw->pw_uid;
2077             break;
2078
2079         case 2 : /* unicode */
2080         case 4 :
2081             if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
2082                 *rbuflen = 0;
2083                 return( AFPERR_NOITEM );
2084             }
2085             id = gr->gr_gid;
2086             break;
2087         }
2088     } else {
2089         id = 0;
2090     }
2091     id = htonl(id);
2092     memcpy( rbuf, &id, sizeof( id ));
2093     *rbuflen = sizeof( id );
2094     return( AFP_OK );
2095 }
2096
2097 /* ------------------------------------
2098   variable DID support 
2099 */
2100 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
2101 AFPObj      *obj;
2102 char    *ibuf, *rbuf;
2103 int             ibuflen, *rbuflen;
2104 {
2105 #if 0
2106     struct vol   *vol;
2107     struct dir   *dir;
2108     u_int16_t    vid;
2109     u_int32_t    did;
2110 #endif /* 0 */
2111
2112     *rbuflen = 0;
2113
2114     /* do nothing as dids are static for the life of the process. */
2115 #if 0
2116     ibuf += 2;
2117
2118     memcpy(&vid,  ibuf, sizeof( vid ));
2119     ibuf += sizeof( vid );
2120     if (( vol = getvolbyvid( vid )) == NULL ) {
2121         return( AFPERR_PARAM );
2122     }
2123
2124     memcpy( &did, ibuf, sizeof( did ));
2125     ibuf += sizeof( did );
2126     if (( dir = dirlookup( vol, did )) == NULL ) {
2127         return( AFPERR_PARAM );
2128     }
2129
2130     /* dir_remove -- deletedid */
2131 #endif /* 0 */
2132
2133     return AFP_OK;
2134 }
2135
2136 /* did creation gets done automatically 
2137  * there's a pb again with case but move it to cname
2138 */
2139 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
2140 AFPObj      *obj;
2141 char    *ibuf, *rbuf;
2142 int             ibuflen, *rbuflen;
2143 {
2144     struct vol          *vol;
2145     struct dir          *parentdir;
2146     struct path         *path;
2147     u_int32_t           did;
2148     u_int16_t           vid;
2149
2150     *rbuflen = 0;
2151     ibuf += 2;
2152
2153     memcpy(&vid, ibuf, sizeof(vid));
2154     ibuf += sizeof( vid );
2155
2156     if (NULL == ( vol = getvolbyvid( vid )) ) {
2157         return( AFPERR_PARAM );
2158     }
2159
2160     memcpy(&did, ibuf, sizeof(did));
2161     ibuf += sizeof(did);
2162
2163     if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2164         return afp_errno;
2165     }
2166
2167     if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2168         return afp_errno;
2169     }
2170
2171     if ( *path->m_name != '\0' ) {
2172         return( AFPERR_BADTYPE ); /* not a directory */
2173     }
2174
2175     if ( !path->st_valid && of_stat(path ) < 0 ) {
2176         return( AFPERR_NOOBJ );
2177     }
2178     if ( path->st_errno ) {
2179         return( AFPERR_NOOBJ );
2180     }
2181
2182     memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2183     *rbuflen = sizeof(curdir->d_did);
2184     return AFP_OK;
2185 }