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