]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
added dropbox as a volume option when DROPKLUDGE is compiled in
[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),
1301                              (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1302                 switch ( errno ) {
1303                 case EPERM :
1304                 case EACCES :
1305                     err = AFPERR_ACCESS;
1306                     goto setdirparam_done;
1307                 case EROFS :
1308                     err = AFPERR_VLOCK;
1309                     goto setdirparam_done;
1310                 default :
1311                     syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1312                     err = AFPERR_PARAM;
1313                     goto setdirparam_done;
1314                 }
1315             }
1316             break;
1317             
1318             /* Ignore what the client thinks we should do to the
1319                ProDOS information block.  Skip over the data and
1320                report nothing amiss. <shirsch@ibm.net> */
1321         case DIRPBIT_PDINFO :
1322             buf += 6;
1323             break;
1324
1325         default :
1326             err = AFPERR_BITMAP;
1327             goto setdirparam_done;
1328             break;
1329         }
1330
1331         bitmap = bitmap>>1;
1332         bit++;
1333     }
1334
1335
1336 setdirparam_done:
1337     if ( isad ) {
1338         ad_flush( &ad, ADFLAGS_HF );
1339         ad_close( &ad, ADFLAGS_HF );
1340     }
1341
1342 #ifdef FORCE_UIDGID
1343         restore_uidgid ( uidgid );
1344 #endif FORCE_UIDGID
1345     return err;
1346 }
1347
1348 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1349     AFPObj      *obj;
1350     char        *ibuf, *rbuf;
1351     int         ibuflen, *rbuflen;
1352 {
1353     struct adouble      ad;
1354     struct stat         st;
1355     struct vol          *vol;
1356     struct dir          *dir;
1357     char                *path, *upath;
1358     u_int32_t           did;
1359     u_int16_t           vid;
1360 #ifdef FORCE_UIDGID
1361         uidgidset               *uidgid;
1362 #endif FORCE_UIDGID
1363
1364     *rbuflen = 0;
1365     ibuf += 2;
1366
1367     memcpy( &vid, ibuf, sizeof( vid ));
1368     ibuf += sizeof( vid );
1369     if (( vol = getvolbyvid( vid )) == NULL ) {
1370         return( AFPERR_PARAM );
1371     }
1372
1373     if (vol->v_flags & AFPVOL_RO)
1374         return AFPERR_VLOCK;
1375
1376     memcpy( &did, ibuf, sizeof( did ));
1377     ibuf += sizeof( did );
1378     if (( dir = dirsearch( vol, did )) == NULL ) {
1379         return( AFPERR_NOOBJ );
1380     }
1381
1382     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1383         return( AFPERR_NOOBJ );
1384     }
1385
1386     /* check for illegal bits */
1387     if ((vol->v_flags & AFPVOL_MSWINDOWS) && 
1388         strpbrk(path, MSWINDOWS_BADCHARS))
1389         return AFPERR_PARAM;
1390
1391     upath = mtoupath(vol, path);
1392
1393     if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1394       return AFPERR_PARAM;
1395
1396     if (!validupath(vol, upath))
1397       return AFPERR_EXIST;
1398
1399 #ifdef FORCE_UIDGID
1400         save_uidgid ( uidgid );
1401         set_uidgid  ( vol );
1402 #endif FORCE_UIDGID
1403
1404     if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1405 #ifdef FORCE_UIDGID
1406         restore_uidgid ( uidgid );
1407 #endif FORCE_UIDGID
1408         switch ( errno ) {
1409         case ENOENT :
1410             return( AFPERR_NOOBJ );
1411         case EROFS :
1412             return( AFPERR_VLOCK );
1413         case EACCES :
1414             return( AFPERR_ACCESS );
1415         case EEXIST :
1416             return( AFPERR_EXIST );
1417         case ENOSPC :
1418         case EDQUOT :
1419             return( AFPERR_DFULL );
1420         default :
1421             return( AFPERR_PARAM );
1422         }
1423     }
1424
1425     if (stat(upath, &st) < 0) {
1426 #ifdef FORCE_UIDGID
1427           restore_uidgid ( uidgid );
1428 #endif FORCE_UIDGID
1429       return AFPERR_MISC;
1430         }
1431
1432     if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1433                        strlen(upath), &st)) == NULL) {
1434 #ifdef FORCE_UIDGID
1435           restore_uidgid ( uidgid );
1436 #endif FORCE_UIDGID
1437       return AFPERR_MISC;
1438         }
1439
1440     if ( movecwd( vol, dir ) < 0 ) {
1441 #ifdef FORCE_UIDGID
1442           restore_uidgid ( uidgid );
1443 #endif FORCE_UIDGID
1444         return( AFPERR_PARAM );
1445     }
1446
1447     memset(&ad, 0, sizeof(ad));
1448     if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1449                  O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
1450       if (vol_noadouble(vol))
1451           goto createdir_done;
1452 #ifdef FORCE_UIDGID
1453           restore_uidgid ( uidgid );
1454 #endif FORCE_UIDGID
1455       return( AFPERR_ACCESS );
1456     }
1457     
1458     ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1459     memcpy( ad_entry( &ad, ADEID_NAME ), path, 
1460            ad_getentrylen( &ad, ADEID_NAME ));
1461     ad_flush( &ad, ADFLAGS_HF );
1462     ad_close( &ad, ADFLAGS_HF );
1463
1464 createdir_done:
1465     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1466     *rbuflen = sizeof( u_int32_t );
1467     setvoltime(obj, vol );
1468 #ifdef FORCE_UIDGID
1469         restore_uidgid ( uidgid );
1470 #endif FORCE_UIDGID
1471     return( AFP_OK );
1472 }
1473
1474
1475 int renamedir(src, dst, dir, newparent, newname, noadouble)
1476     char        *src, *dst, *newname;
1477     struct dir  *dir, *newparent;
1478     const int noadouble;
1479 {
1480     struct adouble      ad;
1481     struct dir          *parent;
1482     char                *buf;
1483     int                 len, err;
1484
1485     /* existence check moved to afp_moveandrename */
1486     if ( rename( src, dst ) < 0 ) {
1487         switch ( errno ) {
1488         case ENOENT :
1489             return( AFPERR_NOOBJ );
1490         case EACCES :
1491             return( AFPERR_ACCESS );
1492         case EROFS:
1493             return AFPERR_VLOCK;
1494         case EINVAL: 
1495             /* tried to move directory into a subdirectory of itself */
1496             return AFPERR_CANTMOVE;
1497         case EXDEV:
1498           /* this needs to copy and delete. bleah. that means we have
1499            * to deal with entire directory hierarchies. */
1500             if ((err = copydir(src, dst, noadouble)) < 0) {
1501                deletedir(dst);
1502                return err;
1503             }
1504             if ((err = deletedir(src)) < 0)
1505                return err;
1506             break;
1507         default : 
1508             return( AFPERR_PARAM );
1509         }
1510     }
1511
1512     memset(&ad, 0, sizeof(ad));
1513     if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1514         switch ( errno ) {
1515         case ENOENT :
1516             if (noadouble) {
1517               len = strlen(newname);
1518               goto renamedir_done;
1519             }
1520             return( AFPERR_NOOBJ );
1521         case EACCES :
1522             return( AFPERR_ACCESS );
1523         default : 
1524             return( AFPERR_PARAM );
1525         }
1526     }
1527     len = strlen( newname );
1528     ad_setentrylen( &ad, ADEID_NAME, len );
1529     memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1530     ad_flush( &ad, ADFLAGS_HF );
1531     ad_close( &ad, ADFLAGS_HF );
1532
1533 renamedir_done:
1534     if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1535         syslog( LOG_ERR, "renamedir: realloc: %m" );
1536         return AFPERR_MISC;
1537     }
1538     dir->d_name = buf;
1539     strcpy( dir->d_name, newname );
1540
1541     if (( parent = dir->d_parent ) == NULL ) {
1542         return( AFP_OK );
1543     }
1544     if ( parent == newparent ) {
1545         return( AFP_OK );
1546     }
1547
1548     /* detach from old parent and add to new one. */
1549     dirchildremove(parent, dir);
1550     dir->d_parent = newparent;
1551     dirchildadd(newparent, dir);
1552     return( AFP_OK );
1553 }
1554
1555 #define DOT_APPLEDOUBLE_LEN 13
1556 /* delete an empty directory */
1557 int deletecurdir( vol, path, pathlen )
1558     const struct vol    *vol;
1559     char *path;
1560     int pathlen;
1561 {
1562     struct dirent *de;
1563     struct stat st;
1564     struct dir  *fdir;
1565     DIR *dp;
1566 #ifdef FORCE_UIDGID
1567         uidgidset               *uidgid;
1568 #endif FORCE_UIDGID
1569
1570     if ( curdir->d_parent == NULL ) {
1571         return( AFPERR_ACCESS );
1572     }
1573
1574     if ( curdir->d_child != NULL ) {
1575         return( AFPERR_DIRNEMPT );
1576     }
1577
1578     fdir = curdir;
1579
1580 #ifdef FORCE_UIDGID
1581         save_uidgid ( uidgid );
1582         set_uidgid  ( vol );
1583 #endif FORCE_UIDGID
1584
1585     /* delete stray .AppleDouble files. this happens to get .Parent files
1586        as well. */
1587     if ((dp = opendir(".AppleDouble"))) {
1588       strcpy(path, ".AppleDouble/");
1589       while ((de = readdir(dp))) {
1590         /* skip this and previous directory */
1591         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1592           continue;
1593
1594         /* bail if the file exists in the current directory.
1595          * note: this will not fail with dangling symlinks */
1596         if (stat(de->d_name, &st) == 0) {
1597           closedir(dp);
1598 #ifdef FORCE_UIDGID
1599         restore_uidgid ( uidgid );
1600 #endif FORCE_UIDGID
1601           return AFPERR_DIRNEMPT;
1602         }
1603
1604         strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1605         if (unlink(path) < 0) {
1606           closedir(dp);
1607           switch (errno) {
1608           case EPERM:
1609           case EACCES :
1610 #ifdef FORCE_UIDGID
1611                 restore_uidgid ( uidgid );
1612 #endif FORCE_UIDGID
1613             return( AFPERR_ACCESS );
1614           case EROFS:
1615 #ifdef FORCE_UIDGID
1616                 restore_uidgid ( uidgid );
1617 #endif FORCE_UIDGID
1618             return AFPERR_VLOCK;
1619           case ENOENT :
1620             continue;
1621           default :
1622 #ifdef FORCE_UIDGID
1623                 restore_uidgid ( uidgid );
1624 #endif FORCE_UIDGID
1625             return( AFPERR_PARAM );
1626           }
1627         }
1628       }
1629       closedir(dp);
1630     }
1631     
1632     if ( rmdir( ".AppleDouble" ) < 0 ) {
1633         switch ( errno ) {
1634         case ENOENT :
1635             break;
1636         case ENOTEMPTY :
1637 #ifdef FORCE_UIDGID
1638                 restore_uidgid ( uidgid );
1639 #endif FORCE_UIDGID
1640             return( AFPERR_DIRNEMPT );
1641         case EROFS:
1642 #ifdef FORCE_UIDGID
1643                 restore_uidgid ( uidgid );
1644 #endif FORCE_UIDGID
1645             return AFPERR_VLOCK;
1646         case EPERM:
1647         case EACCES :
1648 #ifdef FORCE_UIDGID
1649                 restore_uidgid ( uidgid );
1650 #endif FORCE_UIDGID
1651             return( AFPERR_ACCESS );
1652         default :
1653 #ifdef FORCE_UIDGID
1654                 restore_uidgid ( uidgid );
1655 #endif FORCE_UIDGID
1656             return( AFPERR_PARAM );
1657         }
1658     }
1659
1660     /* now get rid of dangling symlinks */
1661     if ((dp = opendir("."))) {
1662       while ((de = readdir(dp))) {
1663         /* skip this and previous directory */
1664         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1665           continue;
1666
1667         /* bail if it's not a symlink */
1668         if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1669 #ifdef FORCE_UIDGID
1670                 restore_uidgid ( uidgid );
1671 #endif FORCE_UIDGID
1672           return AFPERR_DIRNEMPT;
1673         }
1674
1675         if (unlink(de->d_name) < 0) {
1676           switch (errno) {
1677           case EPERM:
1678           case EACCES :
1679 #ifdef FORCE_UIDGID
1680                 restore_uidgid ( uidgid );
1681 #endif FORCE_UIDGID
1682             return( AFPERR_ACCESS );
1683           case EROFS:
1684 #ifdef FORCE_UIDGID
1685                 restore_uidgid ( uidgid );
1686 #endif FORCE_UIDGID
1687             return AFPERR_VLOCK;
1688           case ENOENT :
1689             continue;
1690           default :
1691 #ifdef FORCE_UIDGID
1692                 restore_uidgid ( uidgid );
1693 #endif FORCE_UIDGID
1694             return( AFPERR_PARAM );
1695           }
1696         }
1697       }
1698       closedir(dp);
1699     }
1700
1701     if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1702 #ifdef FORCE_UIDGID
1703                 restore_uidgid ( uidgid );
1704 #endif FORCE_UIDGID
1705         return( AFPERR_NOOBJ );
1706     }
1707
1708     if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1709         switch ( errno ) {
1710         case ENOENT :
1711 #ifdef FORCE_UIDGID
1712                 restore_uidgid ( uidgid );
1713 #endif FORCE_UIDGID
1714             return( AFPERR_NOOBJ );
1715         case ENOTEMPTY :
1716 #ifdef FORCE_UIDGID
1717                 restore_uidgid ( uidgid );
1718 #endif FORCE_UIDGID
1719             return( AFPERR_DIRNEMPT );
1720         case EPERM:
1721         case EACCES :
1722 #ifdef FORCE_UIDGID
1723                 restore_uidgid ( uidgid );
1724 #endif FORCE_UIDGID
1725             return( AFPERR_ACCESS );
1726         case EROFS:
1727 #ifdef FORCE_UIDGID
1728                 restore_uidgid ( uidgid );
1729 #endif FORCE_UIDGID
1730             return AFPERR_VLOCK;
1731         default : 
1732 #ifdef FORCE_UIDGID
1733                 restore_uidgid ( uidgid );
1734 #endif FORCE_UIDGID
1735             return( AFPERR_PARAM );
1736         }
1737     }
1738
1739     dirchildremove(curdir, fdir);
1740 #if AD_VERSION > AD_VERSION1
1741     cnid_delete(vol->v_db, fdir->d_did);
1742 #endif
1743     dir_remove( vol, fdir );
1744
1745 #ifdef FORCE_UIDGID
1746         restore_uidgid ( uidgid );
1747 #endif FORCE_UIDGID
1748     return( AFP_OK );
1749 }
1750
1751 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1752     AFPObj      *obj;
1753     char        *ibuf, *rbuf;
1754     int         ibuflen, *rbuflen;
1755 {
1756     struct passwd       *pw;
1757     struct group        *gr;
1758     char                *name;
1759     u_int32_t           id;
1760     int                 len, sfunc;
1761
1762     ibuf++;
1763     sfunc = (unsigned char) *ibuf++;
1764     memcpy( &id, ibuf, sizeof( id ));
1765
1766     if ( id != 0 ) {
1767         switch ( sfunc ) {
1768         case 1 :
1769             if (( pw = getpwuid( id )) == NULL ) {
1770                 *rbuflen = 0;
1771                 return( AFPERR_NOITEM );
1772             }
1773             name = pw->pw_name;
1774             break;
1775
1776         case 2 :
1777             if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1778                 *rbuflen = 0;
1779                 return( AFPERR_NOITEM );
1780             }
1781             name = gr->gr_name;
1782             break;
1783
1784         default :
1785             *rbuflen = 0;
1786             return( AFPERR_PARAM );
1787         }
1788
1789         len = strlen( name );
1790
1791     } else {
1792         len = 0;
1793         name = NULL;
1794     }
1795
1796     *rbuf++ = len;
1797     if ( len > 0 ) {
1798         memcpy( rbuf, name, len );
1799     }
1800     *rbuflen = len + 1;
1801     return( AFP_OK );
1802 }
1803
1804 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1805     AFPObj      *obj;
1806     char        *ibuf, *rbuf;
1807     int         ibuflen, *rbuflen;
1808 {
1809     struct passwd       *pw;
1810     struct group        *gr;
1811     int                 len, sfunc;
1812     u_int32_t           id;
1813
1814     ibuf++;
1815     sfunc = (unsigned char) *ibuf++;
1816     len = (unsigned char) *ibuf++;
1817     ibuf[ len ] = '\0';
1818
1819     if ( len != 0 ) {
1820         switch ( sfunc ) {
1821         case 3 :
1822             if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1823                 *rbuflen = 0;
1824                 return( AFPERR_NOITEM );
1825             }
1826             id = pw->pw_uid;
1827             break;
1828
1829         case 4 :
1830             if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1831                 *rbuflen = 0;
1832                 return( AFPERR_NOITEM );
1833             }
1834             id = gr->gr_gid;
1835             break;
1836         default :
1837             *rbuflen = 0;
1838             return( AFPERR_PARAM );
1839         }
1840     } else {
1841         id = 0;
1842     }
1843
1844     memcpy( rbuf, &id, sizeof( id ));
1845     *rbuflen = sizeof( id );
1846     return( AFP_OK );
1847 }
1848
1849 /* variable DID support */
1850 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1851     AFPObj      *obj;
1852     char        *ibuf, *rbuf;
1853     int         ibuflen, *rbuflen;
1854 {
1855 #if 0
1856     struct vol   *vol;
1857     struct dir   *dir;
1858     u_int16_t    vid;
1859     u_int32_t    did;
1860 #endif
1861
1862    *rbuflen = 0;
1863
1864   /* do nothing as dids are static for the life of the process. */
1865 #if 0
1866     ibuf += 2;
1867
1868     memcpy(&vid,  ibuf, sizeof( vid ));
1869     ibuf += sizeof( vid );
1870     if (( vol = getvolbyvid( vid )) == NULL ) {
1871         return( AFPERR_PARAM );
1872     }
1873
1874     memcpy( &did, ibuf, sizeof( did ));
1875     ibuf += sizeof( did );
1876     if (( dir = dirsearch( vol, did )) == NULL ) {
1877         return( AFPERR_PARAM );
1878     }
1879
1880     /* dir_remove -- deletedid */
1881 #endif
1882
1883     return AFP_OK;
1884 }
1885
1886 /* did creation gets done automatically */
1887 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1888     AFPObj      *obj;
1889     char        *ibuf, *rbuf;
1890     int         ibuflen, *rbuflen;
1891 {
1892     struct vol          *vol;
1893     struct dir          *dir, *parentdir;
1894     struct stat         st;
1895     char                *path, *upath;
1896     u_int32_t           did;
1897     u_int16_t           vid;
1898 #ifdef FORCE_UIDGID
1899         uidgidset               *uidgid;
1900 #endif FORCE_UIDGID
1901
1902     *rbuflen = 0;
1903     ibuf += 2;
1904
1905     memcpy(&vid, ibuf, sizeof(vid));
1906     ibuf += sizeof( vid );
1907
1908     if (( vol = getvolbyvid( vid )) == NULL ) {
1909         return( AFPERR_PARAM );
1910     }
1911
1912     memcpy(&did, ibuf, sizeof(did));
1913     ibuf += sizeof(did);
1914
1915     if (( parentdir = dirsearch( vol, did )) == NULL ) {
1916         return( AFPERR_NOOBJ );
1917     }
1918
1919     if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1920         return( AFPERR_NOOBJ );
1921     }
1922
1923     /* see if we already have the directory. */
1924     upath = mtoupath(vol, path);
1925     if ( stat( upath, &st ) < 0 ) {
1926         return( AFPERR_NOOBJ );
1927     }
1928
1929     dir = parentdir->d_child;
1930     while (dir) {
1931       if (strdiacasecmp(dir->d_name, path) == 0) {
1932         memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1933         *rbuflen = sizeof(dir->d_did);
1934         return AFP_OK;
1935       }    
1936       dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1937     }
1938
1939 #ifdef FORCE_UIDGID
1940         save_uidgid ( uidgid );
1941         set_uidgid  ( vol );
1942 #endif FORCE_UIDGID
1943
1944     /* we don't already have a did. add one in. */
1945     if ((dir = adddir(vol, parentdir, path, strlen(path), 
1946                       upath, strlen(upath), &st)) == NULL) {
1947 #ifdef FORCE_UIDGID
1948           restore_uidgid ( uidgid );
1949 #endif FORCE_UIDGID
1950       return AFPERR_MISC;
1951         }
1952
1953     memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1954     *rbuflen = sizeof(dir->d_did);
1955 #ifdef FORCE_UIDGID
1956         restore_uidgid ( uidgid );
1957 #endif FORCE_UIDGID
1958     return AFP_OK;
1959 }