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