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