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