]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
Changes made by Michel Jouvin - jouvin@lal.in2p3.fr - 23/11/2000
[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 HAVE_ACCESS
947             accessmode( upath, &ma, dir );
948 #endif HAVE_ACCESS
949 #ifdef AFS      /* If only AFS defined, access() works only for AFS filesystems */ 
950             afsmode( upath, &ma, dir );
951 #endif AFS
952             *data++ = ma.ma_user;
953             *data++ = ma.ma_world;
954             *data++ = ma.ma_group;
955             *data++ = ma.ma_owner;
956             break;
957
958             /* Client has requested the ProDOS information block.
959                Just pass back the same basic block for all
960                directories. <shirsch@ibm.net> */
961         case DIRPBIT_PDINFO :                     /* ProDOS Info Block */
962             *data++ = 0x0f;
963             *data++ = 0;
964             ashort = htons( 0x0200 );
965             memcpy( data, &ashort, sizeof( ashort ));
966             data += sizeof( ashort );
967             memset( data, 0, sizeof( ashort ));
968             data += sizeof( ashort );
969             break;
970
971         default :
972             if ( isad ) {
973               ad_close( &ad, ADFLAGS_HF );
974             }
975             return( AFPERR_BITMAP );
976         }
977         bitmap = bitmap>>1;
978         bit++;
979     }
980     if ( nameoff ) {
981         ashort = htons( data - buf );
982         memcpy( nameoff, &ashort, sizeof( ashort ));
983
984         if ((aint = strlen( dir->d_name )) > MACFILELEN)
985           aint = MACFILELEN;
986           
987         *data++ = aint;
988         memcpy( data, dir->d_name, aint );
989         data += aint;
990     }
991     if ( isad ) {
992         ad_close( &ad, ADFLAGS_HF );
993     }
994     *buflen = data - buf;
995     return( AFP_OK );
996 }
997
998 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
999     AFPObj      *obj;
1000     char        *ibuf, *rbuf;
1001     int         ibuflen, *rbuflen;
1002 {
1003     struct vol  *vol;
1004     struct dir  *dir;
1005     char        *path;
1006     u_int16_t   vid, bitmap;
1007     u_int32_t   did;
1008     int         rc;
1009
1010     *rbuflen = 0;
1011     ibuf += 2;
1012     memcpy( &vid, ibuf, sizeof( vid ));
1013     ibuf += sizeof( vid );
1014
1015     if (( vol = getvolbyvid( vid )) == NULL ) {
1016         return( AFPERR_PARAM );
1017     }
1018
1019     if (vol->v_flags & AFPVOL_RO)
1020         return AFPERR_VLOCK;
1021
1022     memcpy( &did, ibuf, sizeof( did ));
1023     ibuf += sizeof( int );
1024
1025     if (( dir = dirsearch( vol, did )) == NULL ) {
1026         return( AFPERR_NOOBJ );
1027     }
1028
1029     memcpy( &bitmap, ibuf, sizeof( bitmap ));
1030     bitmap = ntohs( bitmap );
1031     ibuf += sizeof( bitmap );
1032
1033     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1034         return( AFPERR_NOOBJ );
1035     }
1036
1037     /*
1038      * If ibuf is odd, make it even.
1039      */
1040     if ((u_long)ibuf & 1 ) {
1041         ibuf++;
1042     }
1043
1044     if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1045         setvoltime(obj, vol );
1046     }
1047     return( rc );
1048 }
1049
1050 int setdirparams(vol, path, bitmap, buf )
1051     const struct vol          *vol;
1052     char                *path, *buf;
1053     u_int16_t           bitmap;
1054 {
1055     struct maccess      ma;
1056     struct adouble      ad;
1057     struct utimbuf      ut;
1058     char                *upath;
1059     int                 bit = 0, aint, isad = 1;
1060     u_int16_t           ashort, bshort;
1061     int                 err = AFP_OK;
1062
1063     upath = mtoupath(vol, path);
1064     memset(&ad, 0, sizeof(ad));
1065     if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR, 
1066                  O_RDWR|O_CREAT, 0666, &ad) < 0) {
1067         /*
1068          * Check to see what we're trying to set.  If it's anything
1069          * but ACCESS, UID, or GID, give an error.  If it's any of those
1070          * three, we don't need the ad to be open, so just continue.
1071          *
1072          * note: we also don't need to worry about mdate. also, be quiet
1073          *       if we're using the noadouble option.
1074          */
1075         if (!vol_noadouble(vol) && (bitmap &
1076                 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1077                   (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO))))
1078           return AFPERR_ACCESS;
1079
1080         isad = 0;
1081     } else {
1082         /*
1083          * Check to see if a create was necessary. If it was, we'll want
1084          * to set our name, etc.
1085          */
1086         if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1087             ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1088             memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name, 
1089                     ad_getentrylen( &ad, ADEID_NAME ));
1090         }
1091     }
1092
1093     while ( bitmap != 0 ) {
1094         while (( bitmap & 1 ) == 0 ) {
1095             bitmap = bitmap>>1;
1096             bit++;
1097         }
1098
1099         switch( bit ) {
1100         case DIRPBIT_ATTR :
1101             if (isad) {
1102               memcpy( &ashort, buf, sizeof( ashort ));
1103               ad_getattr(&ad, &bshort);
1104               if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1105                 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1106               } else {
1107                 bshort &= ~ashort;
1108               }
1109               ad_setattr(&ad, bshort);
1110             }
1111             buf += sizeof( ashort );
1112             break;
1113
1114         case DIRPBIT_CDATE :
1115             if (isad) {
1116               memcpy(&aint, buf, sizeof(aint));
1117               ad_setdate(&ad, AD_DATE_CREATE, aint);
1118             }
1119             buf += sizeof( aint );
1120             break;
1121
1122         case DIRPBIT_MDATE :
1123             memcpy(&aint, buf, sizeof(aint));
1124             if (isad)
1125               ad_setdate(&ad, AD_DATE_MODIFY, aint);
1126             ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
1127             utime(upath, &ut);
1128             buf += sizeof( aint );
1129             break;
1130
1131         case DIRPBIT_BDATE :
1132             if (isad) {
1133               memcpy(&aint, buf, sizeof(aint));
1134               ad_setdate(&ad, AD_DATE_BACKUP, aint);
1135             }
1136             buf += sizeof( aint );
1137             break;
1138
1139         case DIRPBIT_FINFO :
1140             /*
1141              * Alright, we admit it, this is *really* sick!
1142              * The 4 bytes that we don't copy, when we're dealing
1143              * with the root of a volume, are the directory's
1144              * location information. This eliminates that annoying
1145              * behavior one sees when mounting above another mount
1146              * point.
1147              */
1148             if (isad) {
1149               if (  curdir->d_did == DIRDID_ROOT ) {
1150                 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1151                 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1152               } else {
1153                 memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1154               }
1155             } 
1156             buf += 32;
1157             break;
1158
1159         case DIRPBIT_UID :      /* What kind of loser mounts as root? */
1160             memcpy( &aint, buf, sizeof(aint));
1161             buf += sizeof( aint );
1162             if ( (curdir->d_did == DIRDID_ROOT) &&
1163                  (setdeskowner( aint, -1 ) < 0)) {
1164                 switch ( errno ) {
1165                 case EPERM :
1166                 case EACCES :
1167                     err = AFPERR_ACCESS;
1168                     goto setdirparam_done;
1169                     break;
1170                 case EROFS :
1171                     err = AFPERR_VLOCK;
1172                     goto setdirparam_done;
1173                     break;
1174                 default :
1175                     syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1176                     if (!isad) {
1177                     err = AFPERR_PARAM;
1178                     goto setdirparam_done;
1179                     }
1180                     break;
1181                 }
1182             }
1183             if ( setdirowner( aint, -1, vol_noadouble(vol) ) < 0 ) {
1184                 switch ( errno ) {
1185                 case EPERM :
1186                 case EACCES :
1187                     err = AFPERR_ACCESS;
1188                     goto setdirparam_done;
1189                     break;
1190                 case EROFS :
1191                     err = AFPERR_VLOCK;
1192                     goto setdirparam_done;
1193                     break;
1194                 default :
1195                     syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1196                     break;
1197                 }
1198             }
1199             break;
1200
1201         case DIRPBIT_GID :
1202             memcpy( &aint, buf, sizeof( aint ));
1203             buf += sizeof( aint );
1204             if (curdir->d_did == DIRDID_ROOT)
1205               setdeskowner( -1, aint );
1206
1207 #if 0       /* don't error if we can't set the desktop owner. */
1208                 switch ( errno ) {
1209                 case EPERM :
1210                 case EACCES :
1211                     err = AFPERR_ACCESS;
1212                     goto setdirparam_done;
1213                     break;
1214                 case EROFS :
1215                     err = AFPERR_VLOCK;
1216                     goto setdirparam_done;
1217                     break;
1218                 default :
1219                     syslog( LOG_ERR, "setdirparam: setdeskowner: %m" );
1220                     if (!isad) {
1221                       err = AFPERR_PARAM;
1222                       goto setdirparam_done;
1223                     }
1224                     break;
1225                 }
1226 #endif
1227
1228             if ( setdirowner( -1, aint, vol_noadouble(vol) ) < 0 ) {
1229                 switch ( errno ) {
1230                 case EPERM :
1231                 case EACCES :
1232                     err = AFPERR_ACCESS;
1233                     goto setdirparam_done;
1234                     break;
1235                 case EROFS :
1236                     err = AFPERR_VLOCK;
1237                     goto setdirparam_done;
1238                     break;
1239                 default :
1240                     syslog( LOG_ERR, "setdirparam: setdirowner: %m" );
1241                     break;
1242                 }
1243             }
1244             break;
1245
1246         case DIRPBIT_ACCESS :
1247             ma.ma_user = *buf++;
1248             ma.ma_world = *buf++;
1249             ma.ma_group = *buf++;
1250             ma.ma_owner = *buf++;
1251
1252             if (curdir->d_did == DIRDID_ROOT) 
1253                  setdeskmode(mtoumode( &ma ));
1254 #if 0 /* don't error if we can't set the desktop mode */
1255                 switch ( errno ) {
1256                 case EPERM :
1257                 case EACCES :
1258                     err = AFPERR_ACCESS;
1259                     goto setdirparam_done;
1260                 case EROFS :
1261                     err = AFPERR_VLOCK;
1262                     goto setdirparam_done;
1263                 default :
1264                     syslog( LOG_ERR, "setdirparam: setdeskmode: %m" );
1265                     break;
1266                     err = AFPERR_PARAM;
1267                     goto setdirparam_done;
1268                 }
1269             }
1270 #endif
1271
1272             if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol)) < 0 ) {
1273                 switch ( errno ) {
1274                 case EPERM :
1275                 case EACCES :
1276                     err = AFPERR_ACCESS;
1277                     goto setdirparam_done;
1278                 case EROFS :
1279                     err = AFPERR_VLOCK;
1280                     goto setdirparam_done;
1281                 default :
1282                     syslog( LOG_ERR, "setdirparam: setdirmode: %m" );
1283                     err = AFPERR_PARAM;
1284                     goto setdirparam_done;
1285                 }
1286             }
1287             break;
1288             
1289             /* Ignore what the client thinks we should do to the
1290                ProDOS information block.  Skip over the data and
1291                report nothing amiss. <shirsch@ibm.net> */
1292         case DIRPBIT_PDINFO :
1293             buf += 6;
1294             break;
1295
1296         default :
1297             err = AFPERR_BITMAP;
1298             goto setdirparam_done;
1299             break;
1300         }
1301
1302         bitmap = bitmap>>1;
1303         bit++;
1304     }
1305
1306
1307 setdirparam_done:
1308     if ( isad ) {
1309         ad_flush( &ad, ADFLAGS_HF );
1310         ad_close( &ad, ADFLAGS_HF );
1311     }
1312
1313     return err;
1314 }
1315
1316 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1317     AFPObj      *obj;
1318     char        *ibuf, *rbuf;
1319     int         ibuflen, *rbuflen;
1320 {
1321     struct adouble      ad;
1322     struct stat         st;
1323     struct vol          *vol;
1324     struct dir          *dir;
1325     char                *path, *upath;
1326     u_int32_t           did;
1327     u_int16_t           vid;
1328
1329     *rbuflen = 0;
1330     ibuf += 2;
1331
1332     memcpy( &vid, ibuf, sizeof( vid ));
1333     ibuf += sizeof( vid );
1334     if (( vol = getvolbyvid( vid )) == NULL ) {
1335         return( AFPERR_PARAM );
1336     }
1337
1338     if (vol->v_flags & AFPVOL_RO)
1339         return AFPERR_VLOCK;
1340
1341     memcpy( &did, ibuf, sizeof( did ));
1342     ibuf += sizeof( did );
1343     if (( dir = dirsearch( vol, did )) == NULL ) {
1344         return( AFPERR_NOOBJ );
1345     }
1346
1347     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1348         return( AFPERR_NOOBJ );
1349     }
1350
1351     /* check for illegal bits */
1352     if ((vol->v_flags & AFPVOL_MSWINDOWS) && 
1353         strpbrk(path, MSWINDOWS_BADCHARS))
1354         return AFPERR_PARAM;
1355
1356     upath = mtoupath(vol, path);
1357
1358     if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
1359       return AFPERR_PARAM;
1360
1361     if (!validupath(vol, upath))
1362       return AFPERR_EXIST;
1363
1364     if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1365         switch ( errno ) {
1366         case ENOENT :
1367             return( AFPERR_NOOBJ );
1368         case EROFS :
1369             return( AFPERR_VLOCK );
1370         case EACCES :
1371             return( AFPERR_ACCESS );
1372         case EEXIST :
1373             return( AFPERR_EXIST );
1374         case ENOSPC :
1375         case EDQUOT :
1376             return( AFPERR_DFULL );
1377         default :
1378             return( AFPERR_PARAM );
1379         }
1380     }
1381
1382     if (stat(upath, &st) < 0)
1383       return AFPERR_MISC;
1384
1385     if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1386                        strlen(upath), &st)) == NULL)
1387       return AFPERR_MISC;
1388
1389     if ( movecwd( vol, dir ) < 0 ) {
1390         return( AFPERR_PARAM );
1391     }
1392
1393     memset(&ad, 0, sizeof(ad));
1394     if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1395                  O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
1396       if (vol_noadouble(vol))
1397           goto createdir_done;
1398       return( AFPERR_ACCESS );
1399     }
1400     
1401     ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1402     memcpy( ad_entry( &ad, ADEID_NAME ), path, 
1403            ad_getentrylen( &ad, ADEID_NAME ));
1404     ad_flush( &ad, ADFLAGS_HF );
1405     ad_close( &ad, ADFLAGS_HF );
1406
1407 createdir_done:
1408     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1409     *rbuflen = sizeof( u_int32_t );
1410     setvoltime(obj, vol );
1411     return( AFP_OK );
1412 }
1413
1414
1415 int renamedir(src, dst, dir, newparent, newname, noadouble)
1416     char        *src, *dst, *newname;
1417     struct dir  *dir, *newparent;
1418     const int noadouble;
1419 {
1420     struct adouble      ad;
1421     struct dir          *parent;
1422     char                *buf;
1423     int                 len, err;
1424
1425     /* existence check moved to afp_moveandrename */
1426     if ( rename( src, dst ) < 0 ) {
1427         switch ( errno ) {
1428         case ENOENT :
1429             return( AFPERR_NOOBJ );
1430         case EACCES :
1431             return( AFPERR_ACCESS );
1432         case EROFS:
1433             return AFPERR_VLOCK;
1434         case EINVAL: 
1435             /* tried to move directory into a subdirectory of itself */
1436             return AFPERR_CANTMOVE;
1437         case EXDEV:
1438           /* this needs to copy and delete. bleah. that means we have
1439            * to deal with entire directory hierarchies. */
1440             if ((err = copydir(src, dst, noadouble)) < 0) {
1441                deletedir(dst);
1442                return err;
1443             }
1444             if ((err = deletedir(src)) < 0)
1445                return err;
1446             break;
1447         default : 
1448             return( AFPERR_PARAM );
1449         }
1450     }
1451
1452     memset(&ad, 0, sizeof(ad));
1453     if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1454         switch ( errno ) {
1455         case ENOENT :
1456             if (noadouble) {
1457               len = strlen(newname);
1458               goto renamedir_done;
1459             }
1460             return( AFPERR_NOOBJ );
1461         case EACCES :
1462             return( AFPERR_ACCESS );
1463         default : 
1464             return( AFPERR_PARAM );
1465         }
1466     }
1467     len = strlen( newname );
1468     ad_setentrylen( &ad, ADEID_NAME, len );
1469     memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1470     ad_flush( &ad, ADFLAGS_HF );
1471     ad_close( &ad, ADFLAGS_HF );
1472
1473 renamedir_done:
1474     if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1475         syslog( LOG_ERR, "renamedir: realloc: %m" );
1476         return AFPERR_MISC;
1477     }
1478     dir->d_name = buf;
1479     strcpy( dir->d_name, newname );
1480
1481     if (( parent = dir->d_parent ) == NULL ) {
1482         return( AFP_OK );
1483     }
1484     if ( parent == newparent ) {
1485         return( AFP_OK );
1486     }
1487
1488     /* detach from old parent and add to new one. */
1489     dirchildremove(parent, dir);
1490     dir->d_parent = newparent;
1491     dirchildadd(newparent, dir);
1492     return( AFP_OK );
1493 }
1494
1495 #define DOT_APPLEDOUBLE_LEN 13
1496 /* delete an empty directory */
1497 int deletecurdir( vol, path, pathlen )
1498     const struct vol    *vol;
1499     char *path;
1500     int pathlen;
1501 {
1502     struct dirent *de;
1503     struct stat st;
1504     struct dir  *fdir;
1505     DIR *dp;
1506
1507     if ( curdir->d_parent == NULL ) {
1508         return( AFPERR_ACCESS );
1509     }
1510
1511     if ( curdir->d_child != NULL ) {
1512         return( AFPERR_DIRNEMPT );
1513     }
1514
1515     fdir = curdir;
1516
1517     /* delete stray .AppleDouble files. this happens to get .Parent files
1518        as well. */
1519     if ((dp = opendir(".AppleDouble"))) {
1520       strcpy(path, ".AppleDouble/");
1521       while ((de = readdir(dp))) {
1522         /* skip this and previous directory */
1523         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1524           continue;
1525
1526         /* bail if the file exists in the current directory.
1527          * note: this will not fail with dangling symlinks */
1528         if (stat(de->d_name, &st) == 0) {
1529           closedir(dp);
1530           return AFPERR_DIRNEMPT;
1531         }
1532
1533         strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1534         if (unlink(path) < 0) {
1535           closedir(dp);
1536           switch (errno) {
1537           case EPERM:
1538           case EACCES :
1539             return( AFPERR_ACCESS );
1540           case EROFS:
1541             return AFPERR_VLOCK;
1542           case ENOENT :
1543             continue;
1544           default :
1545             return( AFPERR_PARAM );
1546           }
1547         }
1548       }
1549       closedir(dp);
1550     }
1551     
1552     if ( rmdir( ".AppleDouble" ) < 0 ) {
1553         switch ( errno ) {
1554         case ENOENT :
1555             break;
1556         case ENOTEMPTY :
1557             return( AFPERR_DIRNEMPT );
1558         case EROFS:
1559             return AFPERR_VLOCK;
1560         case EPERM:
1561         case EACCES :
1562             return( AFPERR_ACCESS );
1563         default :
1564             return( AFPERR_PARAM );
1565         }
1566     }
1567
1568     /* now get rid of dangling symlinks */
1569     if ((dp = opendir("."))) {
1570       while ((de = readdir(dp))) {
1571         /* skip this and previous directory */
1572         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1573           continue;
1574
1575         /* bail if it's not a symlink */
1576         if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode))
1577           return AFPERR_DIRNEMPT;
1578
1579         if (unlink(de->d_name) < 0) {
1580           switch (errno) {
1581           case EPERM:
1582           case EACCES :
1583             return( AFPERR_ACCESS );
1584           case EROFS:
1585             return AFPERR_VLOCK;
1586           case ENOENT :
1587             continue;
1588           default :
1589             return( AFPERR_PARAM );
1590           }
1591         }
1592       }
1593       closedir(dp);
1594     }
1595
1596     if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1597         return( AFPERR_NOOBJ );
1598     }
1599
1600     if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1601         switch ( errno ) {
1602         case ENOENT :
1603             return( AFPERR_NOOBJ );
1604         case ENOTEMPTY :
1605             return( AFPERR_DIRNEMPT );
1606         case EPERM:
1607         case EACCES :
1608             return( AFPERR_ACCESS );
1609         case EROFS:
1610             return AFPERR_VLOCK;
1611         default : 
1612             return( AFPERR_PARAM );
1613         }
1614     }
1615
1616     dirchildremove(curdir, fdir);
1617 #if AD_VERSION > AD_VERSION1
1618     cnid_delete(vol->v_db, fdir->d_did);
1619 #endif
1620     dir_remove( vol, fdir );
1621
1622     return( AFP_OK );
1623 }
1624
1625 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1626     AFPObj      *obj;
1627     char        *ibuf, *rbuf;
1628     int         ibuflen, *rbuflen;
1629 {
1630     struct passwd       *pw;
1631     struct group        *gr;
1632     char                *name;
1633     u_int32_t           id;
1634     int                 len, sfunc;
1635
1636     ibuf++;
1637     sfunc = (unsigned char) *ibuf++;
1638     memcpy( &id, ibuf, sizeof( id ));
1639
1640     if ( id != 0 ) {
1641         switch ( sfunc ) {
1642         case 1 :
1643             if (( pw = getpwuid( id )) == NULL ) {
1644                 *rbuflen = 0;
1645                 return( AFPERR_NOITEM );
1646             }
1647             name = pw->pw_name;
1648             break;
1649
1650         case 2 :
1651             if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1652                 *rbuflen = 0;
1653                 return( AFPERR_NOITEM );
1654             }
1655             name = gr->gr_name;
1656             break;
1657
1658         default :
1659             *rbuflen = 0;
1660             return( AFPERR_PARAM );
1661         }
1662
1663         len = strlen( name );
1664
1665     } else {
1666         len = 0;
1667         name = NULL;
1668     }
1669
1670     *rbuf++ = len;
1671     if ( len > 0 ) {
1672         memcpy( rbuf, name, len );
1673     }
1674     *rbuflen = len + 1;
1675     return( AFP_OK );
1676 }
1677
1678 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1679     AFPObj      *obj;
1680     char        *ibuf, *rbuf;
1681     int         ibuflen, *rbuflen;
1682 {
1683     struct passwd       *pw;
1684     struct group        *gr;
1685     int                 len, sfunc;
1686     u_int32_t           id;
1687
1688     ibuf++;
1689     sfunc = (unsigned char) *ibuf++;
1690     len = (unsigned char) *ibuf++;
1691     ibuf[ len ] = '\0';
1692
1693     if ( len != 0 ) {
1694         switch ( sfunc ) {
1695         case 3 :
1696             if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1697                 *rbuflen = 0;
1698                 return( AFPERR_NOITEM );
1699             }
1700             id = pw->pw_uid;
1701             break;
1702
1703         case 4 :
1704             if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1705                 *rbuflen = 0;
1706                 return( AFPERR_NOITEM );
1707             }
1708             id = gr->gr_gid;
1709             break;
1710         default :
1711             *rbuflen = 0;
1712             return( AFPERR_PARAM );
1713         }
1714     } else {
1715         id = 0;
1716     }
1717
1718     memcpy( rbuf, &id, sizeof( id ));
1719     *rbuflen = sizeof( id );
1720     return( AFP_OK );
1721 }
1722
1723 /* variable DID support */
1724 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1725     AFPObj      *obj;
1726     char        *ibuf, *rbuf;
1727     int         ibuflen, *rbuflen;
1728 {
1729 #if 0
1730     struct vol   *vol;
1731     struct dir   *dir;
1732     u_int16_t    vid;
1733     u_int32_t    did;
1734 #endif
1735
1736    *rbuflen = 0;
1737
1738   /* do nothing as dids are static for the life of the process. */
1739 #if 0
1740     ibuf += 2;
1741
1742     memcpy(&vid,  ibuf, sizeof( vid ));
1743     ibuf += sizeof( vid );
1744     if (( vol = getvolbyvid( vid )) == NULL ) {
1745         return( AFPERR_PARAM );
1746     }
1747
1748     memcpy( &did, ibuf, sizeof( did ));
1749     ibuf += sizeof( did );
1750     if (( dir = dirsearch( vol, did )) == NULL ) {
1751         return( AFPERR_PARAM );
1752     }
1753
1754     /* dir_remove -- deletedid */
1755 #endif
1756
1757     return AFP_OK;
1758 }
1759
1760 /* did creation gets done automatically */
1761 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1762     AFPObj      *obj;
1763     char        *ibuf, *rbuf;
1764     int         ibuflen, *rbuflen;
1765 {
1766     struct vol          *vol;
1767     struct dir          *dir, *parentdir;
1768     struct stat         st;
1769     char                *path, *upath;
1770     u_int32_t           did;
1771     u_int16_t           vid;
1772
1773     *rbuflen = 0;
1774     ibuf += 2;
1775
1776     memcpy(&vid, ibuf, sizeof(vid));
1777     ibuf += sizeof( vid );
1778
1779     if (( vol = getvolbyvid( vid )) == NULL ) {
1780         return( AFPERR_PARAM );
1781     }
1782
1783     memcpy(&did, ibuf, sizeof(did));
1784     ibuf += sizeof(did);
1785
1786     if (( parentdir = dirsearch( vol, did )) == NULL ) {
1787         return( AFPERR_NOOBJ );
1788     }
1789
1790     if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
1791         return( AFPERR_NOOBJ );
1792     }
1793
1794     /* see if we already have the directory. */
1795     upath = mtoupath(vol, path);
1796     if ( stat( upath, &st ) < 0 ) {
1797         return( AFPERR_NOOBJ );
1798     }
1799
1800     dir = parentdir->d_child;
1801     while (dir) {
1802       if (strdiacasecmp(dir->d_name, path) == 0) {
1803         memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1804         *rbuflen = sizeof(dir->d_did);
1805         return AFP_OK;
1806       }    
1807       dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
1808     }
1809
1810     /* we don't already have a did. add one in. */
1811     if ((dir = adddir(vol, parentdir, path, strlen(path), 
1812                       upath, strlen(upath), &st)) == NULL) 
1813       return AFPERR_MISC;
1814
1815     memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
1816     *rbuflen = sizeof(dir->d_did);
1817     return AFP_OK;
1818 }