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