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