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