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