]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/directory.c
Back-port from HEAD: Do directory name matching using strcmp(), not
[netatalk.git] / etc / afpd / directory.c
1 /*
2  * $Id: directory.c,v 1.41.2.2 2002-11-07 17:10:02 srittau 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 ( strcmp( 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                         if ( dir->d_did != DIRDID_ROOT_PARENT) 
885                             dir_invalidate(vol, dir);
886                         return( NULL );
887                     }
888                     cdir = extenddir( vol, dir, path );
889                 }
890
891             } else {
892                 cdir = extenddir( vol, dir, path );
893             }
894
895             if ( cdir == NULL ) {
896                 if ( len > 0 ) {
897                     return( NULL );
898                 }
899
900             } else {
901                 dir = cdir;     
902                 *path = '\0';
903             }
904         }
905     }
906 }
907
908 /*
909  * Move curdir to dir, with a possible chdir()
910  */
911 int movecwd( vol, dir)
912 const struct vol        *vol;
913 struct dir      *dir;
914 {
915     char path[MAXPATHLEN + 1];
916     struct dir  *d;
917     char        *p, *u;
918     int         n;
919
920     if ( dir == curdir ) {
921         return( 0 );
922     }
923     if ( dir->d_did == DIRDID_ROOT_PARENT) {
924         return( -1 );
925     }
926
927     p = path + sizeof(path) - 1;
928     *p-- = '\0';
929     *p = '.';
930     for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
931         *--p = '/';
932         u = mtoupath(vol, d->d_name );
933         n = strlen( u );
934         p -= n;
935         strncpy( p, u, n );
936     }
937     if ( d != curdir ) {
938         *--p = '/';
939         n = strlen( vol->v_path );
940         p -= n;
941         strncpy( p, vol->v_path, n );
942     }
943     if ( chdir( p ) < 0 ) {
944         return( -1 );
945     }
946     curdir = dir;
947     return( 0 );
948 }
949
950 int getdirparams(const struct vol *vol,
951                  u_int16_t bitmap,
952                  char *upath, struct dir *dir, struct stat *st,
953                  char *buf, int *buflen )
954 {
955     struct maccess      ma;
956     struct adouble      ad;
957     char                *data, *nameoff = NULL;
958     DIR                 *dp;
959     struct dirent       *de;
960     int                 bit = 0, isad = 1;
961     u_int32_t           aint;
962     u_int16_t           ashort;
963
964     memset(&ad, 0, sizeof(ad));
965
966     if ( ad_open( upath, ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
967                   DIRBITS | 0777, &ad) < 0 ) {
968         isad = 0;
969     }
970
971     data = buf;
972     while ( bitmap != 0 ) {
973         while (( bitmap & 1 ) == 0 ) {
974             bitmap = bitmap>>1;
975             bit++;
976         }
977
978         switch ( bit ) {
979         case DIRPBIT_ATTR :
980             if ( isad ) {
981                 ad_getattr(&ad, &ashort);
982             } else if (*upath == '.' && strcmp(upath, ".") &&
983                        strcmp(upath, "..")) {
984                 ashort = htons(ATTRBIT_INVISIBLE);
985             } else
986                 ashort = 0;
987             ashort |= htons(ATTRBIT_SHARED);
988             memcpy( data, &ashort, sizeof( ashort ));
989             data += sizeof( ashort );
990             break;
991
992         case DIRPBIT_PDID :
993             if ( dir->d_did == DIRDID_ROOT) {
994                 aint = DIRDID_ROOT_PARENT;
995             } else if (dir->d_did == DIRDID_ROOT_PARENT) {
996                 aint = 0;
997             } else {
998                 aint = dir->d_parent->d_did;
999             }
1000             memcpy( data, &aint, sizeof( aint ));
1001             data += sizeof( aint );
1002             break;
1003
1004         case DIRPBIT_CDATE :
1005             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1006                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1007             memcpy( data, &aint, sizeof( aint ));
1008             data += sizeof( aint );
1009             break;
1010
1011         case DIRPBIT_MDATE :
1012             aint = AD_DATE_FROM_UNIX(st->st_mtime);
1013             memcpy( data, &aint, sizeof( aint ));
1014             data += sizeof( aint );
1015             break;
1016
1017         case DIRPBIT_BDATE :
1018             if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1019                 aint = AD_DATE_START;
1020             memcpy( data, &aint, sizeof( aint ));
1021             data += sizeof( aint );
1022             break;
1023
1024         case DIRPBIT_FINFO :
1025             if ( isad ) {
1026                 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1027             } else { /* no appledouble */
1028                 memset( data, 0, 32 );
1029                 /* set default view -- this also gets done in ad_open() */
1030                 ashort = htons(FINDERINFO_CLOSEDVIEW);
1031                 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1032
1033                 /* dot files are by default invisible */
1034                 if (*upath == '.' && strcmp(upath, ".") &&
1035                         strcmp(upath, "..")) {
1036                     ashort = htons(FINDERINFO_INVISIBLE);
1037                     memcpy(data + FINDERINFO_FRFLAGOFF,
1038                            &ashort, sizeof(ashort));
1039                 }
1040             }
1041             data += 32;
1042             break;
1043
1044         case DIRPBIT_LNAME :
1045             if (dir->d_name) /* root of parent can have a null name */
1046                 nameoff = data;
1047             else
1048                 memset(data, 0, sizeof(u_int16_t));
1049             data += sizeof( u_int16_t );
1050             break;
1051
1052         case DIRPBIT_SNAME :
1053             memset(data, 0, sizeof(u_int16_t));
1054             data += sizeof( u_int16_t );
1055             break;
1056
1057         case DIRPBIT_DID :
1058             memcpy( data, &dir->d_did, sizeof( aint ));
1059             data += sizeof( aint );
1060             break;
1061
1062         case DIRPBIT_OFFCNT :
1063             ashort = 0;
1064             /* this needs to handle current directory access rights */
1065             if ((dp = opendir( upath ))) {
1066                 while (( de = readdir( dp )) != NULL ) {
1067                     if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
1068                         continue;
1069
1070                     if (!validupath(vol, de->d_name))
1071                         continue;
1072
1073                     /* check for vetoed filenames */
1074                     if (veto_file(vol->v_veto, de->d_name))
1075                         continue;
1076
1077                     /* now check against too long a filename */
1078                     if (strlen(utompath(vol, de->d_name)) > MACFILELEN)
1079                         continue;
1080
1081                     ashort++;
1082                 }
1083                 closedir( dp );
1084             }
1085             ashort = htons( ashort );
1086             memcpy( data, &ashort, sizeof( ashort ));
1087             data += sizeof( ashort );
1088             break;
1089
1090         case DIRPBIT_UID :
1091             aint = htonl(st->st_uid);
1092             memcpy( data, &aint, sizeof( aint ));
1093             data += sizeof( aint );
1094             break;
1095
1096         case DIRPBIT_GID :
1097             aint = htonl(st->st_gid);
1098             memcpy( data, &aint, sizeof( aint ));
1099             data += sizeof( aint );
1100             break;
1101
1102         case DIRPBIT_ACCESS :
1103             accessmode( upath, &ma, dir , st);
1104
1105             *data++ = ma.ma_user;
1106             *data++ = ma.ma_world;
1107             *data++ = ma.ma_group;
1108             *data++ = ma.ma_owner;
1109             break;
1110
1111             /* Client has requested the ProDOS information block.
1112                Just pass back the same basic block for all
1113                directories. <shirsch@ibm.net> */
1114         case DIRPBIT_PDINFO :                     /* ProDOS Info Block */
1115             *data++ = 0x0f;
1116             *data++ = 0;
1117             ashort = htons( 0x0200 );
1118             memcpy( data, &ashort, sizeof( ashort ));
1119             data += sizeof( ashort );
1120             memset( data, 0, sizeof( ashort ));
1121             data += sizeof( ashort );
1122             break;
1123
1124         default :
1125             if ( isad ) {
1126                 ad_close( &ad, ADFLAGS_HF );
1127             }
1128             return( AFPERR_BITMAP );
1129         }
1130         bitmap = bitmap>>1;
1131         bit++;
1132     }
1133     if ( nameoff ) {
1134         ashort = htons( data - buf );
1135         memcpy( nameoff, &ashort, sizeof( ashort ));
1136
1137         if ((aint = strlen( dir->d_name )) > MACFILELEN)
1138             aint = MACFILELEN;
1139
1140         *data++ = aint;
1141         memcpy( data, dir->d_name, aint );
1142         data += aint;
1143     }
1144     if ( isad ) {
1145         ad_close( &ad, ADFLAGS_HF );
1146     }
1147     *buflen = data - buf;
1148     return( AFP_OK );
1149 }
1150
1151 int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1152 AFPObj      *obj;
1153 char    *ibuf, *rbuf;
1154 int             ibuflen, *rbuflen;
1155 {
1156     struct vol  *vol;
1157     struct dir  *dir;
1158     char        *path;
1159     u_int16_t   vid, bitmap;
1160     u_int32_t   did;
1161     int         rc;
1162
1163     *rbuflen = 0;
1164     ibuf += 2;
1165     memcpy( &vid, ibuf, sizeof( vid ));
1166     ibuf += sizeof( vid );
1167
1168     if (( vol = getvolbyvid( vid )) == NULL ) {
1169         return( AFPERR_PARAM );
1170     }
1171
1172     if (vol->v_flags & AFPVOL_RO)
1173         return AFPERR_VLOCK;
1174
1175     memcpy( &did, ibuf, sizeof( did ));
1176     ibuf += sizeof( int );
1177
1178     if (( dir = dirlookup( vol, did )) == NULL ) {
1179         return( AFPERR_NOOBJ );
1180     }
1181
1182     memcpy( &bitmap, ibuf, sizeof( bitmap ));
1183     bitmap = ntohs( bitmap );
1184     ibuf += sizeof( bitmap );
1185
1186     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1187         return( AFPERR_NOOBJ );
1188     }
1189
1190     if ( *path != '\0' ) {
1191         return( AFPERR_BADTYPE ); /* not a directory */
1192     }
1193
1194     /*
1195      * If ibuf is odd, make it even.
1196      */
1197     if ((u_long)ibuf & 1 ) {
1198         ibuf++;
1199     }
1200
1201     if (( rc = setdirparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
1202         setvoltime(obj, vol );
1203     }
1204     return( rc );
1205 }
1206
1207 /*
1208  * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic  
1209  *
1210  * assume path == '\0' eg. it's a directory in canonical form
1211 */
1212 int setdirparams(const struct vol *vol, 
1213                  char *path, u_int16_t bitmap, char *buf )
1214 {
1215     struct maccess      ma;
1216     struct adouble      ad;
1217     struct utimbuf      ut;
1218     struct timeval      tv;
1219
1220     char                *upath;
1221     int                 bit = 0, aint, isad = 1;
1222     u_int16_t           ashort, bshort;
1223     int                 err = AFP_OK;
1224     int                 change_mdate = 0;
1225     int                 change_parent_mdate = 0;
1226     int                 newdate = 0;
1227
1228     upath = mtoupath(vol, path);
1229     memset(&ad, 0, sizeof(ad));
1230
1231     if (ad_open( upath, vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1232                  O_RDWR|O_CREAT, 0666, &ad) < 0) {
1233         /*
1234          * Check to see what we're trying to set.  If it's anything
1235          * but ACCESS, UID, or GID, give an error.  If it's any of those
1236          * three, we don't need the ad to be open, so just continue.
1237          *
1238          * note: we also don't need to worry about mdate. also, be quiet
1239          *       if we're using the noadouble option.
1240          */
1241         if (!vol_noadouble(vol) && (bitmap &
1242                                     ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1243                                       (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1244             return AFPERR_ACCESS;
1245         }
1246
1247         isad = 0;
1248     } else {
1249         /*
1250          * Check to see if a create was necessary. If it was, we'll want
1251          * to set our name, etc.
1252          */
1253         if ( ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT ) {
1254             ad_setentrylen( &ad, ADEID_NAME, strlen( curdir->d_name ));
1255             memcpy( ad_entry( &ad, ADEID_NAME ), curdir->d_name,
1256                     ad_getentrylen( &ad, ADEID_NAME ));
1257         }
1258     }
1259
1260     while ( bitmap != 0 ) {
1261         while (( bitmap & 1 ) == 0 ) {
1262             bitmap = bitmap>>1;
1263             bit++;
1264         }
1265
1266         switch( bit ) {
1267         case DIRPBIT_ATTR :
1268             change_mdate = 1;
1269             if (isad) {
1270                 memcpy( &ashort, buf, sizeof( ashort ));
1271                 ad_getattr(&ad, &bshort);
1272                 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1273                     bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1274                 } else {
1275                     bshort &= ~ashort;
1276                 }
1277                 ad_setattr(&ad, bshort);
1278                 if ((ashort & htons(ATTRBIT_INVISIBLE)))
1279                    change_parent_mdate = 1;
1280             }
1281             buf += sizeof( ashort );
1282             break;
1283
1284         case DIRPBIT_CDATE :
1285             change_mdate = 1;
1286             if (isad) {
1287                 memcpy(&aint, buf, sizeof(aint));
1288                 ad_setdate(&ad, AD_DATE_CREATE, aint);
1289             }
1290             buf += sizeof( aint );
1291             break;
1292
1293         case DIRPBIT_MDATE :
1294             memcpy(&newdate, buf, sizeof(newdate));
1295             buf += sizeof( newdate );
1296             break;
1297
1298         case DIRPBIT_BDATE :
1299             change_mdate = 1;
1300             if (isad) {
1301                 memcpy(&aint, buf, sizeof(aint));
1302                 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1303             }
1304             buf += sizeof( aint );
1305             break;
1306
1307         case DIRPBIT_FINFO :
1308             change_mdate = 1;
1309             /*
1310              * Alright, we admit it, this is *really* sick!
1311              * The 4 bytes that we don't copy, when we're dealing
1312              * with the root of a volume, are the directory's
1313              * location information. This eliminates that annoying
1314              * behavior one sees when mounting above another mount
1315              * point.
1316              */
1317             if (isad) {
1318                 if (  curdir->d_did == DIRDID_ROOT ) {
1319                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 10 );
1320                     memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, buf + 14, 18 );
1321                 } else {
1322                     memcpy( ad_entry( &ad, ADEID_FINDERI ), buf, 32 );
1323                 }
1324             }
1325             buf += 32;
1326             break;
1327
1328         case DIRPBIT_UID :      /* What kind of loser mounts as root? */
1329             change_parent_mdate = 1;
1330             memcpy( &aint, buf, sizeof(aint));
1331             buf += sizeof( aint );
1332             if ( (curdir->d_did == DIRDID_ROOT) &&
1333                     (setdeskowner( ntohl(aint), -1 ) < 0)) {
1334                 switch ( errno ) {
1335                 case EPERM :
1336                 case EACCES :
1337                     err = AFPERR_ACCESS;
1338                     goto setdirparam_done;
1339                     break;
1340                 case EROFS :
1341                     err = AFPERR_VLOCK;
1342                     goto setdirparam_done;
1343                     break;
1344                 default :
1345                     LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %s",
1346                         strerror(errno) );
1347                     if (!isad) {
1348                         err = AFPERR_PARAM;
1349                         goto setdirparam_done;
1350                     }
1351                     break;
1352                 }
1353             }
1354             if ( setdirowner( ntohl(aint), -1, vol_noadouble(vol) ) < 0 ) {
1355                 switch ( errno ) {
1356                 case EPERM :
1357                 case EACCES :
1358                     err = AFPERR_ACCESS;
1359                     goto setdirparam_done;
1360                     break;
1361                 case EROFS :
1362                     err = AFPERR_VLOCK;
1363                     goto setdirparam_done;
1364                     break;
1365                 default :
1366                     LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1367                         strerror(errno) );
1368                     break;
1369                 }
1370             }
1371             break;
1372         case DIRPBIT_GID :
1373             change_parent_mdate = 1;
1374             memcpy( &aint, buf, sizeof( aint ));
1375             buf += sizeof( aint );
1376             if (curdir->d_did == DIRDID_ROOT)
1377                 setdeskowner( -1, ntohl(aint) );
1378
1379 #if 0       /* don't error if we can't set the desktop owner. */
1380             switch ( errno ) {
1381             case EPERM :
1382             case EACCES :
1383                 err = AFPERR_ACCESS;
1384                 goto setdirparam_done;
1385                 break;
1386             case EROFS :
1387                 err = AFPERR_VLOCK;
1388                 goto setdirparam_done;
1389                 break;
1390             default :
1391                 LOG(log_error, logtype_afpd, "setdirparam: setdeskowner: %m" );
1392                 if (!isad) {
1393                     err = AFPERR_PARAM;
1394                     goto setdirparam_done;
1395                 }
1396                 break;
1397             }
1398 #endif /* 0 */
1399
1400             if ( setdirowner( -1, ntohl(aint), vol_noadouble(vol) ) < 0 ) {
1401                 switch ( errno ) {
1402                 case EPERM :
1403                 case EACCES :
1404                     err = AFPERR_ACCESS;
1405                     goto setdirparam_done;
1406                     break;
1407                 case EROFS :
1408                     err = AFPERR_VLOCK;
1409                     goto setdirparam_done;
1410                     break;
1411                 default :
1412                     LOG(log_error, logtype_afpd, "setdirparam: setdirowner: %s",
1413                         strerror(errno) );
1414                     break;
1415                 }
1416             }
1417             break;
1418
1419         case DIRPBIT_ACCESS :
1420             change_mdate = 1;
1421             change_parent_mdate = 1;
1422             ma.ma_user = *buf++;
1423             ma.ma_world = *buf++;
1424             ma.ma_group = *buf++;
1425             ma.ma_owner = *buf++;
1426
1427             if (curdir->d_did == DIRDID_ROOT)
1428                 setdeskmode(mtoumode( &ma ));
1429 #if 0 /* don't error if we can't set the desktop mode */
1430             switch ( errno ) {
1431             case EPERM :
1432             case EACCES :
1433                 err = AFPERR_ACCESS;
1434                 goto setdirparam_done;
1435             case EROFS :
1436                 err = AFPERR_VLOCK;
1437                 goto setdirparam_done;
1438             default :
1439                 LOG(log_error, logtype_afpd, "setdirparam: setdeskmode: %s",
1440                     strerror(errno) );
1441                 break;
1442                 err = AFPERR_PARAM;
1443                 goto setdirparam_done;
1444             }
1445 #endif /* 0 */
1446
1447             if ( setdirmode( mtoumode( &ma ), vol_noadouble(vol),
1448                          (vol->v_flags & AFPVOL_DROPBOX)) < 0 ) {
1449                 switch ( errno ) {
1450                 case EPERM :
1451                 case EACCES :
1452                     err = AFPERR_ACCESS;
1453                     goto setdirparam_done;
1454                 case EROFS :
1455                     err = AFPERR_VLOCK;
1456                     goto setdirparam_done;
1457                 default :
1458                     LOG(log_error, logtype_afpd, "setdirparam: setdirmode: %s",
1459                         strerror(errno) );
1460                     err = AFPERR_PARAM;
1461                     goto setdirparam_done;
1462                 }
1463             }
1464             break;
1465
1466         /* Ignore what the client thinks we should do to the
1467            ProDOS information block.  Skip over the data and
1468            report nothing amiss. <shirsch@ibm.net> */
1469         case DIRPBIT_PDINFO :
1470             buf += 6;
1471             break;
1472
1473         default :
1474             err = AFPERR_BITMAP;
1475             goto setdirparam_done;
1476             break;
1477         }
1478
1479         bitmap = bitmap>>1;
1480         bit++;
1481     }
1482
1483 setdirparam_done:
1484     if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1485        newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1486     }
1487     if (newdate) {
1488        if (isad)
1489           ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1490        ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1491        utime(upath, &ut);
1492     }
1493
1494     if ( isad ) {
1495         ad_flush( &ad, ADFLAGS_HF );
1496         ad_close( &ad, ADFLAGS_HF );
1497     }
1498
1499     if (change_parent_mdate && curdir->d_did != DIRDID_ROOT
1500             && gettimeofday(&tv, NULL) == 0) {
1501        if (!movecwd(vol, curdir->d_parent)) {
1502            newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1503            bitmap = 1<<DIRPBIT_MDATE;
1504            setdirparams(vol, "", bitmap, (char *)&newdate);
1505            /* should we reset curdir ?*/
1506        }
1507     }
1508
1509     return err;
1510 }
1511
1512 int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
1513 AFPObj      *obj;
1514 char    *ibuf, *rbuf;
1515 int             ibuflen, *rbuflen;
1516 {
1517     struct adouble      ad;
1518     struct stat         st;
1519     struct vol          *vol;
1520     struct dir          *dir;
1521     char                *path, *upath;
1522     u_int32_t           did;
1523     u_int16_t           vid;
1524
1525     *rbuflen = 0;
1526     ibuf += 2;
1527
1528     memcpy( &vid, ibuf, sizeof( vid ));
1529     ibuf += sizeof( vid );
1530     if (( vol = getvolbyvid( vid )) == NULL ) {
1531         return( AFPERR_PARAM );
1532     }
1533
1534     if (vol->v_flags & AFPVOL_RO)
1535         return AFPERR_VLOCK;
1536
1537     memcpy( &did, ibuf, sizeof( did ));
1538     ibuf += sizeof( did );
1539     if (( dir = dirlookup( vol, did )) == NULL ) {
1540         return( AFPERR_NOOBJ );
1541     }
1542
1543     if (( path = cname( vol, dir, &ibuf )) == NULL ) {
1544         switch( errno ) {
1545         case EACCES:
1546             return( AFPERR_ACCESS );
1547         case EEXIST:                            /* FIXME this on is impossible? */
1548             return( AFPERR_EXIST );
1549         default:
1550             return( AFPERR_NOOBJ );
1551         }
1552     }
1553     /* FIXME check done elswhere? cname was able to move curdir to it! */
1554         if (*path == '\0')
1555                 return AFPERR_EXIST;
1556     upath = mtoupath(vol, path);
1557     {
1558     int ret;
1559         if (0 != (ret = check_name(vol, upath))) {
1560             return  ret;
1561         }
1562     }
1563
1564     if ( ad_mkdir( upath, DIRBITS | 0777 ) < 0 ) {
1565         switch ( errno ) {
1566         case ENOENT :
1567             return( AFPERR_NOOBJ );
1568         case EROFS :
1569             return( AFPERR_VLOCK );
1570         case EACCES :
1571             return( AFPERR_ACCESS );
1572         case EEXIST :
1573             return( AFPERR_EXIST );
1574         case ENOSPC :
1575         case EDQUOT :
1576             return( AFPERR_DFULL );
1577         default :
1578             return( AFPERR_PARAM );
1579         }
1580     }
1581
1582     if (stat(upath, &st) < 0) {
1583         return AFPERR_MISC;
1584     }
1585
1586     if ((dir = adddir( vol, curdir, path, strlen( path ), upath,
1587                        strlen(upath), &st)) == NULL) {
1588         return AFPERR_MISC;
1589     }
1590
1591     if ( movecwd( vol, dir ) < 0 ) {
1592         return( AFPERR_PARAM );
1593     }
1594
1595     memset(&ad, 0, sizeof(ad));
1596     if (ad_open( "", vol_noadouble(vol)|ADFLAGS_HF|ADFLAGS_DIR,
1597                  O_RDWR|O_CREAT, 0666, &ad ) < 0)  {
1598         if (vol_noadouble(vol))
1599             goto createdir_done;
1600         return( AFPERR_ACCESS );
1601     }
1602
1603     ad_setentrylen( &ad, ADEID_NAME, strlen( path ));
1604     memcpy( ad_entry( &ad, ADEID_NAME ), path,
1605             ad_getentrylen( &ad, ADEID_NAME ));
1606     ad_flush( &ad, ADFLAGS_HF );
1607     ad_close( &ad, ADFLAGS_HF );
1608
1609 createdir_done:
1610     memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
1611     *rbuflen = sizeof( u_int32_t );
1612     setvoltime(obj, vol );
1613     return( AFP_OK );
1614 }
1615
1616
1617 int renamedir(src, dst, dir, newparent, newname, noadouble)
1618 char    *src, *dst, *newname;
1619 struct dir      *dir, *newparent;
1620 const int noadouble;
1621 {
1622     struct adouble      ad;
1623     struct dir          *parent;
1624     char                *buf;
1625     int                 len, err;
1626
1627     /* existence check moved to afp_moveandrename */
1628     if ( rename( src, dst ) < 0 ) {
1629         switch ( errno ) {
1630         case ENOENT :
1631             return( AFPERR_NOOBJ );
1632         case EACCES :
1633             return( AFPERR_ACCESS );
1634         case EROFS:
1635             return AFPERR_VLOCK;
1636         case EINVAL:
1637             /* tried to move directory into a subdirectory of itself */
1638             return AFPERR_CANTMOVE;
1639         case EXDEV:
1640             /* this needs to copy and delete. bleah. that means we have
1641              * to deal with entire directory hierarchies. */
1642             if ((err = copydir(src, dst, noadouble)) < 0) {
1643                 deletedir(dst);
1644                 return err;
1645             }
1646             if ((err = deletedir(src)) < 0)
1647                 return err;
1648             break;
1649         default :
1650             return( AFPERR_PARAM );
1651         }
1652     }
1653
1654     memset(&ad, 0, sizeof(ad));
1655     if ( ad_open( dst, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 0, &ad) < 0 ) {
1656         switch ( errno ) {
1657         case ENOENT :
1658             if (noadouble) {
1659                 len = strlen(newname);
1660                 goto renamedir_done;
1661             }
1662             return( AFPERR_NOOBJ );
1663         case EACCES :
1664             return( AFPERR_ACCESS );
1665         default :
1666             return( AFPERR_PARAM );
1667         }
1668     }
1669     len = strlen( newname );
1670     ad_setentrylen( &ad, ADEID_NAME, len );
1671     memcpy( ad_entry( &ad, ADEID_NAME ), newname, len );
1672     ad_flush( &ad, ADFLAGS_HF );
1673     ad_close( &ad, ADFLAGS_HF );
1674
1675 renamedir_done:
1676     if ((buf = (char *) realloc( dir->d_name, len + 1 )) == NULL ) {
1677         LOG(log_error, logtype_afpd, "renamedir: realloc: %s", strerror(errno) );
1678         return AFPERR_MISC;
1679     }
1680     dir->d_name = buf;
1681     strcpy( dir->d_name, newname );
1682
1683     if (( parent = dir->d_parent ) == NULL ) {
1684         return( AFP_OK );
1685     }
1686     if ( parent == newparent ) {
1687         return( AFP_OK );
1688     }
1689
1690     /* detach from old parent and add to new one. */
1691     dirchildremove(parent, dir);
1692     dir->d_parent = newparent;
1693     dirchildadd(newparent, dir);
1694     return( AFP_OK );
1695 }
1696
1697 #define DOT_APPLEDOUBLE_LEN 13
1698 /* delete an empty directory */
1699 int deletecurdir( vol, path, pathlen )
1700 const struct vol        *vol;
1701 char *path;
1702 int pathlen;
1703 {
1704     struct dirent *de;
1705     struct stat st;
1706     struct dir  *fdir;
1707     DIR *dp;
1708     struct adouble      ad;
1709     u_int16_t           ashort;
1710
1711     if ( curdir->d_parent == NULL ) {
1712         return( AFPERR_ACCESS );
1713     }
1714
1715     if ( curdir->d_child != NULL ) {
1716         return( AFPERR_DIRNEMPT );
1717     }
1718
1719     fdir = curdir;
1720
1721     memset(&ad, 0, sizeof(ad));
1722     if ( ad_open( ".", ADFLAGS_HF|ADFLAGS_DIR, O_RDONLY,
1723                   DIRBITS | 0777, &ad) == 0 ) {
1724
1725         ad_getattr(&ad, &ashort);
1726         ad_close( &ad, ADFLAGS_HF );
1727         if ((ashort & htons(ATTRBIT_NODELETE))) {
1728             return  AFPERR_OLOCK;
1729         }
1730     }
1731
1732     /* delete stray .AppleDouble files. this happens to get .Parent files
1733        as well. */
1734     if ((dp = opendir(".AppleDouble"))) {
1735         strcpy(path, ".AppleDouble/");
1736         while ((de = readdir(dp))) {
1737             /* skip this and previous directory */
1738             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1739                 continue;
1740
1741             /* bail if the file exists in the current directory.
1742              * note: this will not fail with dangling symlinks */
1743             if (stat(de->d_name, &st) == 0) {
1744                 closedir(dp);
1745                 return AFPERR_DIRNEMPT;
1746             }
1747
1748             strcpy(path + DOT_APPLEDOUBLE_LEN, de->d_name);
1749             if (unlink(path) < 0) {
1750                 closedir(dp);
1751                 switch (errno) {
1752                 case EPERM:
1753                 case EACCES :
1754                     return( AFPERR_ACCESS );
1755                 case EROFS:
1756                     return AFPERR_VLOCK;
1757                 case ENOENT :
1758                     continue;
1759                 default :
1760                     return( AFPERR_PARAM );
1761                 }
1762             }
1763         }
1764         closedir(dp);
1765     }
1766
1767     if ( rmdir( ".AppleDouble" ) < 0 ) {
1768         switch ( errno ) {
1769         case ENOENT :
1770             break;
1771         case ENOTEMPTY :
1772             return( AFPERR_DIRNEMPT );
1773         case EROFS:
1774             return AFPERR_VLOCK;
1775         case EPERM:
1776         case EACCES :
1777             return( AFPERR_ACCESS );
1778         default :
1779             return( AFPERR_PARAM );
1780         }
1781     }
1782
1783     /* now get rid of dangling symlinks */
1784     if ((dp = opendir("."))) {
1785         while ((de = readdir(dp))) {
1786             /* skip this and previous directory */
1787             if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
1788                 continue;
1789
1790             /* bail if it's not a symlink */
1791             if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
1792                 return AFPERR_DIRNEMPT;
1793             }
1794
1795             if (unlink(de->d_name) < 0) {
1796                 switch (errno) {
1797                 case EPERM:
1798                 case EACCES :
1799                     return( AFPERR_ACCESS );
1800                 case EROFS:
1801                     return AFPERR_VLOCK;
1802                 case ENOENT :
1803                     continue;
1804                 default :
1805                     return( AFPERR_PARAM );
1806                 }
1807             }
1808         }
1809         closedir(dp);
1810     }
1811
1812     if ( movecwd( vol, curdir->d_parent ) < 0 ) {
1813         return( AFPERR_NOOBJ );
1814     }
1815
1816     if ( rmdir(mtoupath(vol, fdir->d_name)) < 0 ) {
1817         switch ( errno ) {
1818         case ENOENT :
1819             return( AFPERR_NOOBJ );
1820         case ENOTEMPTY :
1821             return( AFPERR_DIRNEMPT );
1822         case EPERM:
1823         case EACCES :
1824             return( AFPERR_ACCESS );
1825         case EROFS:
1826             return AFPERR_VLOCK;
1827         default :
1828             return( AFPERR_PARAM );
1829         }
1830     }
1831
1832     dirchildremove(curdir, fdir);
1833 #ifdef CNID_DB
1834     cnid_delete(vol->v_db, fdir->d_did);
1835 #endif /* CNID_DB */
1836     dir_remove( vol, fdir );
1837
1838     return( AFP_OK );
1839 }
1840
1841 int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
1842 AFPObj      *obj;
1843 char    *ibuf, *rbuf;
1844 int             ibuflen, *rbuflen;
1845 {
1846     struct passwd       *pw;
1847     struct group        *gr;
1848     char                *name;
1849     u_int32_t           id;
1850     int                 len, sfunc;
1851
1852     ibuf++;
1853     sfunc = (unsigned char) *ibuf++;
1854     memcpy( &id, ibuf, sizeof( id ));
1855
1856     id = ntohl(id);
1857
1858     if ( id != 0 ) {
1859         switch ( sfunc ) {
1860         case 1 :
1861             if (( pw = getpwuid( id )) == NULL ) {
1862                 *rbuflen = 0;
1863                 return( AFPERR_NOITEM );
1864             }
1865             name = pw->pw_name;
1866             break;
1867
1868         case 2 :
1869             if (( gr = (struct group *)getgrgid( id )) == NULL ) {
1870                 *rbuflen = 0;
1871                 return( AFPERR_NOITEM );
1872             }
1873             name = gr->gr_name;
1874             break;
1875
1876         default :
1877             *rbuflen = 0;
1878             return( AFPERR_PARAM );
1879         }
1880
1881         len = strlen( name );
1882
1883     } else {
1884         len = 0;
1885         name = NULL;
1886     }
1887
1888     *rbuf++ = len;
1889     if ( len > 0 ) {
1890         memcpy( rbuf, name, len );
1891     }
1892     *rbuflen = len + 1;
1893     return( AFP_OK );
1894 }
1895
1896 int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
1897 AFPObj      *obj;
1898 char    *ibuf, *rbuf;
1899 int             ibuflen, *rbuflen;
1900 {
1901     struct passwd       *pw;
1902     struct group        *gr;
1903     int                 len, sfunc;
1904     u_int32_t           id;
1905
1906     ibuf++;
1907     sfunc = (unsigned char) *ibuf++;
1908     len = (unsigned char) *ibuf++;
1909     ibuf[ len ] = '\0';
1910
1911     if ( len != 0 ) {
1912         switch ( sfunc ) {
1913         case 3 :
1914             if (( pw = (struct passwd *)getpwnam( ibuf )) == NULL ) {
1915                 *rbuflen = 0;
1916                 return( AFPERR_NOITEM );
1917             }
1918             id = pw->pw_uid;
1919             break;
1920
1921         case 4 :
1922             if (( gr = (struct group *)getgrnam( ibuf )) == NULL ) {
1923                 *rbuflen = 0;
1924                 return( AFPERR_NOITEM );
1925             }
1926             id = gr->gr_gid;
1927             break;
1928         default :
1929             *rbuflen = 0;
1930             return( AFPERR_PARAM );
1931         }
1932     } else {
1933         id = 0;
1934     }
1935     id = htonl(id);
1936     memcpy( rbuf, &id, sizeof( id ));
1937     *rbuflen = sizeof( id );
1938     return( AFP_OK );
1939 }
1940
1941 /* variable DID support */
1942 int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
1943 AFPObj      *obj;
1944 char    *ibuf, *rbuf;
1945 int             ibuflen, *rbuflen;
1946 {
1947 #if 0
1948     struct vol   *vol;
1949     struct dir   *dir;
1950     u_int16_t    vid;
1951     u_int32_t    did;
1952 #endif /* 0 */
1953
1954     *rbuflen = 0;
1955
1956     /* do nothing as dids are static for the life of the process. */
1957 #if 0
1958     ibuf += 2;
1959
1960     memcpy(&vid,  ibuf, sizeof( vid ));
1961     ibuf += sizeof( vid );
1962     if (( vol = getvolbyvid( vid )) == NULL ) {
1963         return( AFPERR_PARAM );
1964     }
1965
1966     memcpy( &did, ibuf, sizeof( did ));
1967     ibuf += sizeof( did );
1968     if (( dir = dirlookup( vol, did )) == NULL ) {
1969         return( AFPERR_PARAM );
1970     }
1971
1972     /* dir_remove -- deletedid */
1973 #endif /* 0 */
1974
1975     return AFP_OK;
1976 }
1977
1978 /* did creation gets done automatically */
1979 int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
1980 AFPObj      *obj;
1981 char    *ibuf, *rbuf;
1982 int             ibuflen, *rbuflen;
1983 {
1984     struct vol          *vol;
1985     struct dir          *dir, *parentdir;
1986     struct stat         st;
1987     char                *path, *upath;
1988     u_int32_t           did;
1989     u_int16_t           vid;
1990
1991     *rbuflen = 0;
1992     ibuf += 2;
1993
1994     memcpy(&vid, ibuf, sizeof(vid));
1995     ibuf += sizeof( vid );
1996
1997     if (( vol = getvolbyvid( vid )) == NULL ) {
1998         return( AFPERR_PARAM );
1999     }
2000
2001     memcpy(&did, ibuf, sizeof(did));
2002     ibuf += sizeof(did);
2003
2004     if (( parentdir = dirlookup( vol, did )) == NULL ) {
2005         return( AFPERR_NOOBJ );
2006     }
2007
2008     if (( path = cname( vol, parentdir, &ibuf )) == NULL ) {
2009         switch( errno ) {
2010         case EACCES:
2011             return( AFPERR_ACCESS );
2012         default:
2013             return( AFPERR_NOOBJ );
2014         }
2015     }
2016
2017     /* see if we already have the directory. */
2018     upath = mtoupath(vol, path);
2019     if ( stat( upath, &st ) < 0 ) {
2020         return( AFPERR_NOOBJ );
2021     }
2022
2023     dir = parentdir->d_child;
2024     while (dir) {
2025         if (strdiacasecmp(dir->d_name, path) == 0) {
2026             memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2027             *rbuflen = sizeof(dir->d_did);
2028             return AFP_OK;
2029         }
2030         dir = (dir == parentdir->d_child->d_prev) ? NULL : dir->d_next;
2031     }
2032
2033     /* we don't already have a did. add one in. */
2034     if ((dir = adddir(vol, parentdir, path, strlen(path),
2035                       upath, strlen(upath), &st)) == NULL) {
2036         return AFPERR_MISC;
2037     }
2038
2039     memcpy(rbuf, &dir->d_did, sizeof(dir->d_did));
2040     *rbuflen = sizeof(dir->d_did);
2041     return AFP_OK;
2042 }