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