]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_open.c
74ead26fb9f3c03a7a8257210382a444222e40ee
[netatalk.git] / libatalk / adouble / ad_open.c
1 /*
2  * $Id: ad_open.c,v 1.24 2002-11-26 18:48:23 didg Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
5  * Copyright (c) 1990,1991 Regents of The University of Michigan.
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify, and distribute this software and
9  * its documentation for any purpose and without fee is hereby granted,
10  * provided that the above copyright notice appears in all copies and
11  * that both that copyright notice and this permission notice appear
12  * in supporting documentation, and that the name of The University
13  * of Michigan not be used in advertising or publicity pertaining to
14  * distribution of the software without specific, written prior
15  * permission. This software is supplied as is without expressed or
16  * implied warranties of any kind.
17  *
18  *      Research Systems Unix Group
19  *      The University of Michigan
20  *      c/o Mike Clark
21  *      535 W. William Street
22  *      Ann Arbor, Michigan
23  *      +1-313-763-0525
24  *      netatalk@itd.umich.edu
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif /* HAVE_CONFIG_H */
30
31 #include <string.h>
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif /* HAVE_FCNTL_H */
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #include <errno.h>
39 #include <atalk/logger.h>
40
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/param.h>
45 #include <sys/mman.h>
46
47 #include <netatalk/endian.h>
48 #include <atalk/adouble.h>
49
50 #include "ad_private.h"
51
52 #ifndef MAX
53 #define MAX(a, b)  ((a) < (b) ? (b) : (a))
54 #endif /* ! MAX */
55
56 /*
57  * AppleDouble entry default offsets.
58  * The layout looks like this:
59  *
60  * this is the v1 layout:
61  *        255     200             16      32              N
62  *      |  NAME |    COMMENT    | FILEI |    FINDERI    | RFORK |
63  *
64  * we need to change it to look like this:
65  *
66  * v2 layout:
67  * field       length (in bytes)
68  * NAME        255
69  * COMMENT     200
70  * FILEDATESI  16     replaces FILEI
71  * FINDERI     32  
72  * DID          4     new
73  * AFPFILEI     4     new
74  * SHORTNAME   12     8.3 new
75  * RFORK        N
76  * 
77  * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
78  * and add in the new fields.
79  *
80  * NOTE: the HFS module will need similar modifications to interact with
81  * afpd correctly.
82  */
83  
84 #define ADEDOFF_MAGIC        (0)
85 #define ADEDOFF_VERSION      (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
86 #define ADEDOFF_FILLER       (ADEDOFF_VERSION + ADEDLEN_VERSION)
87 #define ADEDOFF_NENTRIES     (ADEDOFF_FILLER + ADEDLEN_FILLER)
88
89 /* initial lengths of some of the fields */
90 #define ADEDLEN_INIT     0
91
92 /* make sure we don't redefine ADEDOFF_FILEI */
93 #ifdef ADEDOFF_FILEI
94 #undef ADEDOFF_FILEI
95 #endif /* ADEDOFF_FILEI */
96
97 #define ADEID_NUM_V1         5
98 #define ADEDOFF_NAME_V1      (AD_HEADER_LEN + ADEID_NUM_V1*AD_ENTRY_LEN)
99 #define ADEDOFF_COMMENT_V1   (ADEDOFF_NAME_V1 + ADEDLEN_NAME)
100 #define ADEDOFF_FILEI        (ADEDOFF_COMMENT_V1 + ADEDLEN_COMMENT)
101 #define ADEDOFF_FINDERI_V1   (ADEDOFF_FILEI + ADEDLEN_FILEI)
102 #define ADEDOFF_RFORK_V1     (ADEDOFF_FINDERI_V1 + ADEDLEN_FINDERI)
103
104 /* i stick things in a slightly different order than their eid order in 
105  * case i ever want to separate RootInfo behaviour from the rest of the 
106  * stuff. */
107 #define ADEID_NUM_V2         9
108 #define ADEDOFF_NAME_V2      (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
109 #define ADEDOFF_COMMENT_V2   (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
110 #define ADEDOFF_FILEDATESI   (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
111 #define ADEDOFF_FINDERI_V2   (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
112 #define ADEDOFF_DID          (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
113 #define ADEDOFF_AFPFILEI     (ADEDOFF_DID + ADEDLEN_DID)
114 #define ADEDOFF_SHORTNAME    (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
115 #define ADEDOFF_PRODOSFILEI  (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
116 #define ADEDOFF_RFORK_V2     (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
117
118
119
120 /* we keep local copies of a bunch of stuff so that we can initialize things 
121  * correctly. */
122
123 /* Bits in the finderinfo data. 
124  * see etc/afpd/{directory.c,file.c} for the finderinfo structure
125  * layout. */
126 #define FINDERINFO_CUSTOMICON 0x4
127 #define FINDERINFO_CLOSEDVIEW 0x100
128
129 /* offsets in finderinfo */
130 #define FINDERINFO_FRTYPEOFF   0
131 #define FINDERINFO_FRCREATOFF  4
132 #define FINDERINFO_FRFLAGOFF   8
133 #define FINDERINFO_FRVIEWOFF  14
134
135 /* invisible bit for dot files */
136 #define ATTRBIT_INVISIBLE     (1 << 0)
137 #define FINDERINFO_INVISIBLE  (1 << 14)
138
139 /* this is to prevent changing timezones from causing problems with
140    localtime volumes. the screw-up is 30 years. we use a delta of 5
141    years.  */
142 #define TIMEWARP_DELTA 157680000
143
144
145 struct entry {
146   u_int32_t id, offset, len;
147 };
148
149 #if AD_VERSION == AD_VERSION1 
150 static const struct entry entry_order[] = {
151   {ADEID_NAME, ADEDOFF_NAME_V1, ADEDLEN_INIT},
152   {ADEID_COMMENT, ADEDOFF_COMMENT_V1, ADEDLEN_INIT},
153   {ADEID_FILEI, ADEDOFF_FILEI, ADEDLEN_FILEI},
154   {ADEID_FINDERI, ADEDOFF_FINDERI_V1, ADEDLEN_FINDERI},
155   {ADEID_RFORK, ADEDOFF_RFORK_V1, ADEDLEN_INIT},
156   {0, 0, 0}
157 };
158 #else /* AD_VERSION == AD_VERSION2 */
159 static const struct entry entry_order[] = {
160   {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
161   {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
162   {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
163   {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
164   {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
165   {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
166   {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
167   {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
168   {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
169   {0, 0, 0}
170 };
171 #endif /* AD_VERSION == AD_VERSION2 */
172
173 #if AD_VERSION == AD_VERSION2
174
175
176 static __inline__ int ad_v1tov2(struct adouble *ad, const char *path)
177 {
178   struct stat st;
179   u_int16_t attr;
180   char *buf;
181   int fd, off;
182   
183   /* check to see if we should convert this header. */
184   if (!path || (ad->ad_version != AD_VERSION1))
185     return 0;
186
187   /* convert from v1 to v2. what does this mean?
188    *  1) change FILEI into FILEDATESI
189    *  2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
190    *  3) move FILEI attributes into AFPFILEI
191    *  4) initialize ACCESS field of FILEDATESI.
192    *
193    *  so, we need 4*12 (entry ids) + 12 (shortname) + 4 (afpfilei) +
194    *  4 (did) + 8 (prodosi) = 76 more bytes.  */
195   
196 #define SHIFTDATA (AD_DATASZ2 - AD_DATASZ1)
197
198   /* bail if we can't get a lock */
199   if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0) < 0) 
200     goto bail_err;
201   
202   if ((fd = open(path, O_RDWR)) < 0) 
203     goto bail_lock;
204   
205   if (fstat(fd, &st) ||
206       ftruncate(fd, st.st_size + SHIFTDATA) < 0) {
207     goto bail_open;
208   }
209   
210   /* last place for failure. */
211   if ((void *) (buf = (char *) 
212                 mmap(NULL, st.st_size + SHIFTDATA,
213                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == 
214           MAP_FAILED) {
215     goto bail_truncate;
216   }
217   
218   off = ad->ad_eid[ADEID_RFORK].ade_off;
219
220   /* move the RFORK. this assumes that the RFORK is at the end */
221   memmove(buf + off + SHIFTDATA, buf + off, 
222           ad->ad_eid[ADEID_RFORK].ade_len);
223   
224   /* now, fix up our copy of the header */
225   memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
226   
227   /* replace FILEI with FILEDATESI */
228   ad_getattr(ad, &attr);
229   ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
230   ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
231   ad->ad_eid[ADEID_FILEI].ade_off = 0;
232   ad->ad_eid[ADEID_FILEI].ade_len = 0;
233   
234   /* add in the new entries */
235   ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
236   ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
237   ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
238   ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
239   ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
240   ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
241   ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
242   ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
243   
244   /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
245   ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
246   ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
247   ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
248   ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
249   
250   /* switch to v2 */
251   ad->ad_version = AD_VERSION2;
252   
253   /* move our data buffer to make space for the new entries. */
254   memmove(buf + ADEDOFF_NAME_V2, buf + ADEDOFF_NAME_V1,
255           ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
256   
257   /* now, fill in the space with appropriate stuff. we're
258      operating as a v2 file now. */
259   ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
260   memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
261   memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
262   ad_setattr(ad, attr);
263   memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
264   memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
265   
266   /* rebuild the header and cleanup */
267   ad_rebuild_header(ad);
268   munmap(buf, st.st_size + SHIFTDATA);
269   close(fd);
270   ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
271
272   return 0;
273   
274 bail_truncate:
275   ftruncate(fd, st.st_size);
276 bail_open:
277   close(fd);
278 bail_lock:
279   ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
280 bail_err:
281   return -1;
282 }
283 #endif /* AD_VERSION == AD_VERSION2 */
284
285 #ifdef ATACC
286 mode_t ad_hf_mode (mode_t mode)
287 {
288     /* fnctl lock need write access */
289     if ((mode & S_IRUSR))
290         mode |= S_IWUSR;
291     if ((mode & S_IRGRP))
292         mode |= S_IWGRP;
293     if ((mode & S_IROTH))
294         mode |= S_IWOTH;
295     /* if write mode set add read mode */
296     if ((mode & S_IWUSR))
297         mode |= S_IRUSR;
298     if ((mode & S_IWGRP))
299         mode |= S_IRGRP;
300     if ((mode & S_IWOTH))
301         mode |= S_IROTH;
302
303     return mode;
304 }
305
306 #endif
307
308 /* ------------------------------------- 
309   read in the entries 
310 */
311 static void parse_entries(struct adouble *ad, char *buf,
312                                     u_int16_t nentries)
313 {
314     u_int32_t           eid, len, off;
315
316     /* now, read in the entry bits */
317     for (; nentries > 0; nentries-- ) {
318         memcpy(&eid, buf, sizeof( eid ));
319         eid = ntohl( eid );
320         buf += sizeof( eid );
321         memcpy(&off, buf, sizeof( off ));
322         off = ntohl( off );
323         buf += sizeof( off );
324         memcpy(&len, buf, sizeof( len ));
325         len = ntohl( len );
326         buf += sizeof( len );
327
328         if ( 0 < eid && eid < ADEID_MAX ) {
329             ad->ad_eid[ eid ].ade_off = off;
330             ad->ad_eid[ eid ].ade_len = len;
331         } else {
332             LOG(log_debug, logtype_default, "ad_refresh: nentries %hd  eid %d\n",
333                     nentries, eid );
334         }
335     }
336 }
337
338
339 /* this reads enough of the header so that we can figure out all of
340  * the entry lengths and offsets. once that's done, we just read/mmap
341  * the rest of the header in.
342  *
343  * NOTE: we're assuming that the resource fork is kept at the end of
344  *       the file. also, mmapping won't work for the hfs fs until it
345  *       understands how to mmap header files. */
346 static int ad_header_read(struct adouble *ad, struct stat *hst)
347 {
348     char                *buf = ad->ad_data;
349     u_int16_t           nentries;
350     int                 len;
351     ssize_t             header_len;
352     static int          warning = 0;
353     struct stat         st;
354
355     /* read the header */
356     if ((header_len = adf_pread( &ad->ad_hf, buf, sizeof(ad->ad_data), 0)) < 0) {
357         return -1;
358     }
359     if (header_len < AD_HEADER_LEN) {
360         errno = EIO;
361         return -1;
362     }
363
364     memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
365     memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
366
367     /* tag broken v1 headers. just assume they're all right. 
368      * we detect two cases: null magic/version
369      *                      byte swapped magic/version
370      * XXX: in the future, you'll need the v1compat flag. 
371      * (ad->ad_flags & ADFLAGS_V1COMPAT) */
372     if (!ad->ad_magic && !ad->ad_version) {
373       if (!warning) {
374         LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
375         warning++;
376       }
377       ad->ad_magic = AD_MAGIC;
378       ad->ad_version = AD_VERSION1;
379
380     } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
381       if (!warning) {
382         LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
383         warning++;
384       }
385
386     } else {
387       ad->ad_magic = ntohl( ad->ad_magic );
388       ad->ad_version = ntohl( ad->ad_version );
389     }
390
391     if ((ad->ad_magic != AD_MAGIC) || ((ad->ad_version != AD_VERSION1)
392 #if AD_VERSION == AD_VERSION2
393                                        && (ad->ad_version != AD_VERSION2)
394 #endif /* AD_VERSION == AD_VERSION2 */
395                                        )) {
396       errno = EIO;
397       LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
398       return -1;
399     }
400
401     memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
402     memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
403     nentries = ntohs( nentries );
404
405     /* read in all the entry headers. if we have more than the 
406      * maximum, just hope that the rfork is specified early on. */
407     len = nentries*AD_ENTRY_LEN;
408
409     if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
410       len = sizeof(ad->ad_data) - AD_HEADER_LEN;
411
412     buf += AD_HEADER_LEN;
413     if (len > header_len - AD_HEADER_LEN) {
414         errno = EIO;
415         LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
416         return -1;
417     }
418
419     /* figure out all of the entry offsets and lengths. if we aren't
420      * able to read a resource fork entry, bail. */
421     parse_entries(ad, buf, nentries);
422     if (!ad_getentryoff(ad, ADEID_RFORK)
423         || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
424         ) {
425       LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset."); 
426       return -1;
427     }
428
429     if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
430         errno = EIO;
431         LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
432         return -1;
433     }
434     
435     if (hst == NULL) {
436         hst = &st;
437         if (fstat(ad->ad_hf.adf_fd, &st) < 0) {
438             return 1; /* fail silently */
439          }
440     }
441     ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
442
443     /* fix up broken dates */
444     if (ad->ad_version == AD_VERSION1) {
445       u_int32_t aint;
446       
447       /* check to see if the ad date is wrong. just see if we have
448       * a modification date in the future. */
449       if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
450           (aint > TIMEWARP_DELTA + hst->st_mtime)) {
451         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
452         ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
453         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
454         ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
455         ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
456       }
457     }
458
459     return 0;
460 }
461
462
463 /*
464  * Put the .AppleDouble where it needs to be:
465  *
466  *          /   a/.AppleDouble/b
467  *      a/b
468  *          \   b/.AppleDouble/.Parent
469  */
470 char *
471 ad_path( path, adflags )
472     const char  *path;
473     int         adflags;
474 {
475     static char pathbuf[ MAXPATHLEN + 1];
476     char        c, *slash, buf[MAXPATHLEN + 1];
477
478     strncpy(buf, path, MAXPATHLEN);
479     if ( adflags & ADFLAGS_DIR ) {
480         strncpy( pathbuf, buf, MAXPATHLEN );
481         if ( *buf != '\0' ) {
482             strcat( pathbuf, "/" );
483         }
484         slash = ".Parent";
485     } else {
486         if (( slash = strrchr( buf, '/' )) != NULL ) {
487             c = *++slash;
488             *slash = '\0';
489             strncpy( pathbuf, buf, MAXPATHLEN);
490             *slash = c;
491         } else {
492             pathbuf[ 0 ] = '\0';
493             slash = buf;
494         }
495     }
496     strncat( pathbuf, ".AppleDouble/", MAXPATHLEN - strlen(pathbuf));
497     strncat( pathbuf, slash, MAXPATHLEN - strlen(pathbuf));
498
499     return( pathbuf );
500 }
501
502 /*
503  * Support inherited protection modes for AppleDouble files.  The supplied
504  * mode is ANDed with the parent directory's mask value in lieu of "umask",
505  * and that value is returned.
506  */
507
508 #define DEFMASK 07700   /* be conservative */
509
510 char 
511 *ad_dir(path)
512     const char          *path;
513 {
514     static char         modebuf[ MAXPATHLEN + 1];
515     char                *slash;
516
517     if ( strlen( path ) >= MAXPATHLEN ) {
518         return NULL;  /* can't do it */
519     }
520
521     /*
522      * For a path with directories in it, remove the final component
523      * (path or subdirectory name) to get the name we want to stat.
524      * For a path which is just a filename, use "." instead.
525      */
526     strcpy( modebuf, path );
527     if (( slash = strrchr( modebuf, '/' )) != NULL ) {
528         *slash = '\0';          /* remove pathname component */
529     } else {
530         modebuf[0] = '.';       /* use current directory */
531         modebuf[1] = '\0';
532     }
533     return modebuf;
534 }
535
536 int
537 ad_mode( path, mode )
538     const char          *path;
539     int                 mode;
540 {
541     struct stat         stbuf;
542     char                *p;
543     
544     if ( mode == 0 ) {
545         return( mode );         /* save on syscalls */
546     }
547     p = ad_dir(path);
548     if (!p) {
549         return( mode & DEFMASK );  /* can't do it */
550     }
551
552     if ( stat( p, &stbuf ) != 0 ) {
553         return( mode & DEFMASK );       /* bail out... can't stat dir? */
554     }
555
556     return( mode & stbuf.st_mode );
557 }
558
559 /*
560  * Use mkdir() with mode bits taken from ad_mode().
561  */
562 int
563 ad_mkdir( path, mode )
564     const char          *path;
565     int                 mode;
566 {
567 #ifdef DEBUG
568     LOG(log_info, logtype_default, "ad_mkdir: Creating directory with mode %d", mode);
569 #endif /* DEBUG */
570     return mkdir( path, ad_mode( path, mode ) );
571 }
572
573
574 static int new_rfork(const char *path, struct adouble *ad, int adflags);
575
576 #ifdef  HAVE_PREAD
577 #define AD_SET(a) 
578 #else 
579 #define AD_SET(a) a = 0
580 #endif
581 /*
582  * It's not possible to open the header file O_RDONLY -- the read
583  * will fail and return an error. this refcounts things now. 
584  */
585 int ad_open( path, adflags, oflags, mode, ad )
586     const char          *path;
587     int                 adflags, oflags, mode;
588     struct adouble      *ad;
589 {
590     struct stat         st;
591     char                *slash, *ad_p;
592     int                 hoflags, admode;
593
594     if (ad->ad_inited != AD_INITED) {
595         ad_dfileno(ad) = -1;
596         ad_hfileno(ad) = -1;
597         adf_lock_init(&ad->ad_df);
598         adf_lock_init(&ad->ad_hf);
599         ad->ad_inited = AD_INITED;
600         ad->ad_refcount = 1;
601     }
602
603     if ((adflags & ADFLAGS_DF)) { 
604         if (ad_dfileno(ad) == -1) {
605           hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
606           admode = ad_mode( path, mode ); 
607           if (( ad->ad_df.adf_fd = open( path, hoflags, admode )) < 0 ) {
608              if (errno == EACCES && !(oflags & O_RDWR)) {
609                 hoflags = oflags;
610                 ad->ad_df.adf_fd =open( path, hoflags, admode );
611              }
612           }
613           if ( ad->ad_df.adf_fd < 0)
614                 return -1;
615
616           AD_SET(ad->ad_df.adf_off);
617           ad->ad_df.adf_flags = hoflags;
618         } 
619         else {
620             /* the file is already open... but */
621             if ((oflags & ( O_RDWR | O_WRONLY)) &&             /* we want write access */
622                 !(ad->ad_df.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
623             {
624                  errno = EACCES;
625                  return -1;
626             }
627         }
628         ad->ad_df.adf_refcount++;
629     }
630
631     if (!(adflags & ADFLAGS_HF)) 
632         return 0;
633         
634     if (ad_hfileno(ad) != -1) { /* the file is already open */
635         if ((oflags & ( O_RDWR | O_WRONLY)) &&             
636                 !(ad->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
637             if (adflags & ADFLAGS_DF) {
638                 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
639                 ad_close( ad, ADFLAGS_DF );
640             }
641             errno = EACCES;
642             return -1;
643         }
644         ad_refresh(ad);
645         ad->ad_hf.adf_refcount++;
646         return 0;
647     }
648
649     ad_p = ad_path( path, adflags );
650
651     hoflags = oflags & ~O_CREAT;
652     hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
653     if (( ad->ad_hf.adf_fd = open( ad_p, hoflags, 0 )) < 0 ) {
654         if (errno == EACCES && !(oflags & O_RDWR)) {
655             hoflags = oflags & ~O_CREAT;
656             ad->ad_hf.adf_fd = open( ad_p, hoflags, 0 );
657         }    
658     }
659
660     if ( ad->ad_hf.adf_fd < 0 ) { 
661         if (errno == ENOENT && (oflags & O_CREAT) ) {
662             /*
663              * We're expecting to create a new adouble header file,
664              * here.
665              * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
666              */
667             admode = ad_hf_mode(ad_mode( ad_p, mode )); 
668             errno = 0;
669             if (( ad->ad_hf.adf_fd = open( ad_p, oflags,admode )) < 0 ) {
670                 /*
671                  * Probably .AppleDouble doesn't exist, try to
672                  * mkdir it.
673                  */
674                 if (errno == ENOENT && (adflags & ADFLAGS_NOADOUBLE) == 0) {
675                     if (( slash = strrchr( ad_p, '/' )) == NULL ) {
676                         ad_close( ad, adflags );
677                         return( -1 );
678                     }
679                     *slash = '\0';
680                     errno = 0;
681                     if ( ad_mkdir( ad_p, 0777 ) < 0 ) {
682                         ad_close( ad, adflags );
683                         return( -1 );
684                     }
685                     *slash = '/';
686                     if (( ad->ad_hf.adf_fd = 
687                                    open( ad_p, oflags, ad_mode( ad_p, mode) )) < 0 ) {
688                         ad_close( ad, adflags );
689                         return( -1 );
690                     }
691                 } else {
692                   ad_close( ad, adflags );
693                   return( -1 );
694                 }
695             }
696             ad->ad_hf.adf_flags = oflags;
697         } else {
698             ad_close( ad, adflags );
699             return( -1 );
700         }
701     } else if (fstat(ad->ad_hf.adf_fd, &st) == 0 && st.st_size == 0) {
702         /* for 0 length files, treat them as new. */
703         ad->ad_hf.adf_flags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR | O_TRUNC;
704     } else {
705         ad->ad_hf.adf_flags = hoflags;
706     }
707     AD_SET(ad->ad_hf.adf_off);
708           
709     memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
710     ad->ad_hf.adf_refcount++;
711     if ((ad->ad_hf.adf_flags & ( O_TRUNC | O_CREAT ))) {
712         /*
713          * This is a new adouble header file. Initialize the structure,
714          * instead of reading it.
715         */
716         if (new_rfork(path, ad, adflags) < 0) {
717             ad_close(ad, adflags);
718             return -1;
719         }
720     } else {
721             /* Read the adouble header in and parse it.*/
722         if ((ad_header_read( ad , &st) < 0)
723 #if AD_VERSION == AD_VERSION2
724                 || (ad_v1tov2(ad, ad_p) < 0)
725 #endif /* AD_VERSION == AD_VERSION2 */
726         ) {
727             ad_close( ad, adflags );
728             return( -1 );
729         }
730     }
731     return 0 ;
732 }
733
734 /* ----------------------------------- */
735 static int new_rfork(const char *path, struct adouble *ad, int adflags)
736 {
737 #if 0
738     struct timeval      tv;
739 #endif    
740     const struct entry  *eid;
741     u_int16_t           ashort;
742     struct stat         st;
743
744     ad->ad_magic = AD_MAGIC;
745     ad->ad_version = AD_VERSION;
746
747     memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
748     memset(ad->ad_data, 0, sizeof(ad->ad_data));
749
750     eid = entry_order;
751     while (eid->id) {
752         ad->ad_eid[eid->id].ade_off = eid->offset;
753         ad->ad_eid[eid->id].ade_len = eid->len;
754         eid++;
755     }
756             
757     /* put something sane in the directory finderinfo */
758     if (adflags & ADFLAGS_DIR) {
759         /* set default view */
760         ashort = htons(FINDERINFO_CLOSEDVIEW);
761         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, 
762                      &ashort, sizeof(ashort));
763     } else {
764         /* set default creator/type fields */
765         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"TEXT", 4);
766         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"UNIX", 4);
767     }
768
769     /* make things invisible */
770     if ((*path == '.') && strcmp(path, ".") && strcmp(path, "..")) {
771         ashort = htons(ATTRBIT_INVISIBLE);
772         ad_setattr(ad, ashort);
773         ashort = htons(FINDERINFO_INVISIBLE);
774         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,
775                      &ashort, sizeof(ashort));
776     }
777
778 #if 0
779     if (gettimeofday(&tv, NULL) < 0) {
780         return -1;
781     } 
782 #endif
783     
784     if (stat(path, &st) < 0) {
785         return -1;
786     }
787             
788     /* put something sane in the date fields */
789     ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
790     ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
791     ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
792     ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
793     return 0;
794 }
795
796 /* to do this with mmap, we need the hfs fs to understand how to mmap
797    header files. */
798 int ad_refresh(struct adouble *ad)
799 {
800
801   if (ad->ad_hf.adf_fd < -1)
802     return -1;
803
804   return ad_header_read(ad, NULL);
805 }