2 * $Id: ad_open.c,v 1.10.2.1 2002-03-12 16:12:03 srittau Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
5 * Copyright (c) 1990,1991 Regents of The University of Michigan.
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.
18 * Research Systems Unix Group
19 * The University of Michigan
21 * 535 W. William Street
24 * netatalk@itd.umich.edu
29 #endif /* HAVE_CONFIG_H */
34 #endif /* HAVE_FCNTL_H */
37 #endif /* HAVE_UNISTD_H */
42 #include <sys/types.h>
44 #include <sys/param.h>
47 #include <netatalk/endian.h>
48 #include <atalk/adouble.h>
50 #include "ad_private.h"
53 #define MAX(a, b) ((a) < (b) ? (b) : (a))
57 * AppleDouble entry default offsets.
58 * The layout looks like this:
60 * this is the v1 layout:
62 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
64 * we need to change it to look like this:
67 * field length (in bytes)
70 * FILEDATESI 16 replaces FILEI
74 * SHORTNAME 12 8.3 new
77 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
78 * and add in the new fields.
80 * NOTE: the HFS module will need similar modifications to interact with
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)
89 /* initial lengths of some of the fields */
90 #define ADEDLEN_INIT 0
92 /* make sure we don't redefine ADEDOFF_FILEI */
95 #endif /* ADEDOFF_FILEI */
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)
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
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)
120 /* we keep local copies of a bunch of stuff so that we can initialize things
123 /* Bits in the finderinfo data.
124 * see etc/afpd/{directory.c,file.c} for the finderinfo structure
126 #define FINDERINFO_CUSTOMICON 0x4
127 #define FINDERINFO_CLOSEDVIEW 0x100
129 /* offsets in finderinfo */
130 #define FINDERINFO_FRTYPEOFF 0
131 #define FINDERINFO_FRCREATOFF 4
132 #define FINDERINFO_FRFLAGOFF 8
133 #define FINDERINFO_FRVIEWOFF 14
135 /* invisible bit for dot files */
136 #define ATTRBIT_INVISIBLE (1 << 0)
137 #define FINDERINFO_INVISIBLE (1 << 14)
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
142 #define TIMEWARP_DELTA 157680000
146 u_int32_t id, offset, len;
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},
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},
171 #endif /* AD_VERSION == AD_VERSION2 */
173 #if AD_VERSION == AD_VERSION2
176 static __inline__ int ad_v1tov2(struct adouble *ad, const char *path)
184 /* check to see if we should convert this header. */
185 if (!path || (ad->ad_version != AD_VERSION1))
188 /* convert from v1 to v2. what does this mean?
189 * 1) change FILEI into FILEDATESI
190 * 2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
191 * 3) move FILEI attributes into AFPFILEI
192 * 4) initialize ACCESS field of FILEDATESI.
194 * so, we need 4*12 (entry ids) + 12 (shortname) + 4 (afpfilei) +
195 * 4 (did) + 8 (prodosi) = 76 more bytes. */
197 #define SHIFTDATA (AD_DATASZ2 - AD_DATASZ1)
199 /* bail if we can't get a lock */
200 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0) < 0)
203 if ((fd = open(path, O_RDWR)) < 0)
206 if (gettimeofday(&tv, NULL) < 0)
209 if (fstat(fd, &st) ||
210 ftruncate(fd, st.st_size + SHIFTDATA) < 0) {
214 /* last place for failure. */
215 if ((void *) (buf = (char *)
216 mmap(NULL, st.st_size + SHIFTDATA,
217 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
222 off = ad->ad_eid[ADEID_RFORK].ade_off;
224 #ifdef USE_MMAPPED_HEADERS
225 /* okay, unmap our old ad header and point it to our local copy */
226 munmap(ad->ad_data, off);
228 #endif /* USER_MMAPPED_HEADERS */
230 /* move the RFORK. this assumes that the RFORK is at the end */
231 memmove(buf + off + SHIFTDATA, buf + off,
232 ad->ad_eid[ADEID_RFORK].ade_len);
234 /* now, fix up our copy of the header */
235 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
237 /* replace FILEI with FILEDATESI */
238 ad_getattr(ad, &attr);
239 ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
240 ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
241 ad->ad_eid[ADEID_FILEI].ade_off = 0;
242 ad->ad_eid[ADEID_FILEI].ade_len = 0;
244 /* add in the new entries */
245 ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
246 ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
247 ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
248 ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
249 ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
250 ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
251 ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
252 ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
254 /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
255 ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
256 ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
257 ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
258 ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
261 ad->ad_version = AD_VERSION2;
263 /* move our data buffer to make space for the new entries. */
264 memmove(buf + ADEDOFF_NAME_V2, buf + ADEDOFF_NAME_V1,
265 ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
267 /* now, fill in the space with appropriate stuff. we're
268 operating as a v2 file now. */
269 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, tv.tv_sec);
270 memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
271 memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
272 ad_setattr(ad, attr);
273 memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
274 memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
276 /* rebuild the header and cleanup */
277 ad_rebuild_header(ad);
278 munmap(buf, st.st_size + SHIFTDATA);
280 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
282 #ifdef USE_MMAPPED_HEADERS
283 /* now remap our header */
284 ad->ad_data = mmap(NULL, ADEDOFF_RFORK_V2, PROT_READ | PROT_WRITE,
285 (ad_getoflags(ad, ADFLAGS_HF) & O_RDWR) ? MAP_SHARED :
286 MAP_PRIVATE, ad->ad_hf.adf_fd, 0);
287 if (ad->ad_data == MAP_FAILED)
289 #endif /* USE_MMAPPED_HEADERS */
294 ftruncate(fd, st.st_size);
298 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
302 #endif /* AD_VERSION == AD_VERSION2 */
305 /* read in the entries */
306 static __inline__ void parse_entries(struct adouble *ad, char *buf,
309 u_int32_t eid, len, off;
311 /* now, read in the entry bits */
312 for (; nentries > 0; nentries-- ) {
313 memcpy(&eid, buf, sizeof( eid ));
315 buf += sizeof( eid );
316 memcpy(&off, buf, sizeof( off ));
318 buf += sizeof( off );
319 memcpy(&len, buf, sizeof( len ));
321 buf += sizeof( len );
323 if ( 0 < eid && eid < ADEID_MAX ) {
324 ad->ad_eid[ eid ].ade_off = off;
325 ad->ad_eid[ eid ].ade_len = len;
327 syslog( LOG_DEBUG, "ad_refresh: nentries %hd eid %d\n",
334 /* this reads enough of the header so that we can figure out all of
335 * the entry lengths and offsets. once that's done, we just read/mmap
336 * the rest of the header in.
338 * NOTE: we're assuming that the resource fork is kept at the end of
339 * the file. also, mmapping won't work for the hfs fs until it
340 * understands how to mmap header files. */
341 static __inline__ int ad_header_read(struct adouble *ad)
343 #ifdef USE_MMAPPED_HEADERS
344 char buf[AD_ENTRY_LEN*ADEID_MAX];
345 #else /* USE_MMAPPED_HEADERS */
346 char *buf = ad->ad_data;
347 #endif /* USE_MMAPPED_HEADERS */
350 static int warning = 0;
352 /* read the header */
353 if ( ad->ad_hf.adf_off != 0 ) {
354 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
357 ad->ad_hf.adf_off = 0;
360 if (read( ad->ad_hf.adf_fd, buf, AD_HEADER_LEN) != AD_HEADER_LEN) {
366 ad->ad_hf.adf_off = AD_HEADER_LEN;
368 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
369 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION,
370 sizeof( ad->ad_version ));
372 /* tag broken v1 headers. just assume they're all right.
373 * we detect two cases: null magic/version
374 * byte swapped magic/version
375 * XXX: in the future, you'll need the v1compat flag.
376 * (ad->ad_flags & ADFLAGS_V1COMPAT) */
377 if (!ad->ad_magic && !ad->ad_version) {
379 syslog(LOG_DEBUG, "notice: fixing up null v1 magic/version.");
382 ad->ad_magic = AD_MAGIC;
383 ad->ad_version = AD_VERSION1;
385 } else if ((ad->ad_magic == AD_MAGIC) &&
386 (ad->ad_version == AD_VERSION1)) {
388 syslog(LOG_DEBUG, "notice: fixing up byte-swapped v1 magic/version.");
393 ad->ad_magic = ntohl( ad->ad_magic );
394 ad->ad_version = ntohl( ad->ad_version );
397 if ((ad->ad_magic != AD_MAGIC) || ((ad->ad_version != AD_VERSION1)
398 #if AD_VERSION == AD_VERSION2
399 && (ad->ad_version != AD_VERSION2)
400 #endif /* AD_VERSION == AD_VERSION2 */
403 syslog(LOG_DEBUG, "ad_open: can't parse AppleDouble header.");
407 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
408 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
409 nentries = ntohs( nentries );
411 /* read in all the entry headers. if we have more than the
412 * maximum, just hope that the rfork is specified early on. */
413 len = nentries*AD_ENTRY_LEN;
414 #ifdef USE_MMAPPED_HEADERS
415 if (len > sizeof(buf))
417 #else /* USE_MMAPPED_HEADERS */
418 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
419 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
420 buf += AD_HEADER_LEN;
421 #endif /* USE_MMAPPED_HEADERS */
422 if (read(ad->ad_hf.adf_fd, buf, len) != len) {
425 syslog(LOG_DEBUG, "ad_header_read: can't read entry info.");
428 ad->ad_hf.adf_off += len;
430 /* figure out all of the entry offsets and lengths. if we aren't
431 * able to read a resource fork entry, bail. */
432 parse_entries(ad, buf, nentries);
433 if (!ad_getentryoff(ad, ADEID_RFORK)
434 #ifndef USE_MMAPPED_HEADERS
435 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
436 #endif /* ! USE_MMAPPED_HEADERS */
438 syslog(LOG_DEBUG, "ad_header_read: problem with rfork entry offset.");
442 /* read/mmap up to the beginning of the resource fork. */
443 #ifdef USE_MMAPPED_HEADERS
444 ad->ad_data = mmap(NULL, ad_getentryoff(ad, ADEID_RFORK),
445 PROT_READ | PROT_WRITE,
446 (ad_getoflags(ad, ADFLAGS_HF) & O_RDWR) ? MAP_SHARED :
447 MAP_PRIVATE, ad->ad_hf.adf_fd, 0);
448 if (ad->ad_data == MAP_FAILED)
450 #else /* USE_MMAPPED_HEADERS */
452 len = ad_getentryoff(ad, ADEID_RFORK) - ad->ad_hf.adf_off;
453 if (read(ad->ad_hf.adf_fd, buf, len) != len) {
456 syslog(LOG_DEBUG, "ad_header_read: can't read in entries.");
459 #endif /* USE_MMAPPED_HEADERS */
461 /* fix up broken dates */
462 if (ad->ad_version == AD_VERSION1) {
466 if (fstat(ad->ad_hf.adf_fd, &st) < 0) {
467 return 1; /* fail silently */
470 /* check to see if the ad date is wrong. just see if we have
471 * a modification date in the future. */
472 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
473 (aint > TIMEWARP_DELTA + st.st_mtime)) {
474 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
475 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
476 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
477 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
478 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
487 * Put the .AppleDouble where it needs to be:
491 * \ b/.AppleDouble/.Parent
494 ad_path( path, adflags )
498 static char pathbuf[ MAXPATHLEN + 1];
499 char c, *slash, buf[MAXPATHLEN + 1];
501 strncpy(buf, path, MAXPATHLEN);
502 if ( adflags & ADFLAGS_DIR ) {
503 strncpy( pathbuf, buf, MAXPATHLEN );
504 if ( *buf != '\0' ) {
505 strcat( pathbuf, "/" );
509 if (( slash = strrchr( buf, '/' )) != NULL ) {
512 strncpy( pathbuf, buf, MAXPATHLEN);
519 strncat( pathbuf, ".AppleDouble/", MAXPATHLEN - strlen(pathbuf));
520 strncat( pathbuf, slash, MAXPATHLEN - strlen(pathbuf));
526 * Support inherited protection modes for AppleDouble files. The supplied
527 * mode is ANDed with the parent directory's mask value in lieu of "umask",
528 * and that value is returned.
531 #define DEFMASK 07700 /* be conservative */
534 ad_mode( path, mode )
538 static char modebuf[ MAXPATHLEN + 1];
543 return( mode ); /* save on syscalls */
546 if ( strlen( path ) >= MAXPATHLEN ) {
547 return( mode & DEFMASK ); /* can't do it */
551 * For a path with directories in it, remove the final component
552 * (path or subdirectory name) to get the name we want to stat.
553 * For a path which is just a filename, use "." instead.
555 strcpy( modebuf, path );
556 if (( slash = strrchr( modebuf, '/' )) != NULL ) {
557 *slash = '\0'; /* remove pathname component */
559 modebuf[0] = '.'; /* use current directory */
563 if ( stat( modebuf, &stbuf ) != 0 ) {
564 return( mode & DEFMASK ); /* bail out... can't stat dir? */
567 return( mode & stbuf.st_mode );
571 * Use mkdir() with mode bits taken from ad_mode().
574 ad_mkdir( path, mode )
579 syslog (LOG_INFO, "ad_mkdir: Creating directory with mode %d", mode);
581 return mkdir( path, ad_mode( path, mode ) );
586 * It's not possible to open the header file O_RDONLY -- the read
587 * will fail and return an error. this refcounts things now.
589 int ad_open( path, adflags, oflags, mode, ad )
591 int adflags, oflags, mode;
594 const struct entry *eid;
600 if (ad->ad_inited != AD_INITED) {
603 adf_lock_init(&ad->ad_df);
604 adf_lock_init(&ad->ad_hf);
605 #ifdef USE_MMAPPED_HEADERS
606 ad->ad_data = MAP_FAILED;
607 #endif /* USE_MMAPPED_HEADERS */
608 ad->ad_inited = AD_INITED;
612 if (adflags & ADFLAGS_DF) {
613 if (ad_dfileno(ad) == -1) {
614 if (( ad->ad_df.adf_fd =
615 open( path, oflags, ad_mode( path, mode ) )) < 0 ) {
618 ad->ad_df.adf_off = 0;
619 ad->ad_df.adf_flags = oflags;
621 ad->ad_df.adf_refcount++;
624 if (adflags & ADFLAGS_HF) {
625 if (ad_hfileno(ad) == -1) {
626 ad_p = ad_path( path, adflags );
627 admode = ad_mode( ad_p, mode ); /* FIXME? */
629 hoflags = oflags & ~O_CREAT;
630 if (( ad->ad_hf.adf_fd = open( ad_p, hoflags, admode )) < 0 ) {
631 if ( errno == ENOENT && hoflags != oflags ) {
633 * We're expecting to create a new adouble header file,
637 if (( ad->ad_hf.adf_fd = open( ad_p, oflags,
640 * Probably .AppleDouble doesn't exist, try to
643 if ((errno == ENOENT) &&
644 ((adflags & ADFLAGS_NOADOUBLE) == 0)) {
645 if (( slash = strrchr( ad_p, '/' )) == NULL ) {
646 ad_close( ad, adflags );
651 if ( ad_mkdir( ad_p, 0777 ) < 0 ) {
652 ad_close( ad, adflags );
656 if (( ad->ad_hf.adf_fd =
657 open( ad_p, oflags, ad_mode( ad_p, mode) )) < 0 ) {
658 ad_close( ad, adflags );
662 ad_close( ad, adflags );
666 ad->ad_hf.adf_flags = oflags;
668 ad_close( ad, adflags );
671 } else if ((fstat(ad->ad_hf.adf_fd, &st) == 0) &&
673 /* for 0 length files, treat them as new. */
674 ad->ad_hf.adf_flags = oflags;
676 ad->ad_hf.adf_flags = hoflags;
678 ad->ad_hf.adf_off = 0;
681 * This is a new adouble header file. Initialize the structure,
682 * instead of reading it.
684 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
685 if ( ad->ad_hf.adf_flags & ( O_TRUNC | O_CREAT )) {
688 ad->ad_magic = AD_MAGIC;
689 ad->ad_version = AD_VERSION;
690 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
692 #ifdef USE_MMAPPED_HEADERS
693 /* truncate the header file and mmap it. */
694 ftruncate(ad->ad_hf.adf_fd, AD_DATASZ);
695 ad->ad_data = mmap(NULL, AD_DATASZ, PROT_READ | PROT_WRITE,
696 MAP_SHARED, ad->ad_hf.adf_fd, 0);
697 if (ad->ad_data == MAP_FAILED) {
698 ad_close(ad, adflags);
701 #else /* USE_MMAPPED_HEADERS */
702 memset(ad->ad_data, 0, sizeof(ad->ad_data));
703 #endif /* USE_MMAPPED_HEADERS */
707 ad->ad_eid[eid->id].ade_off = eid->offset;
708 ad->ad_eid[eid->id].ade_len = eid->len;
712 /* put something sane in the directory finderinfo */
713 if (adflags & ADFLAGS_DIR) {
714 /* set default view */
715 ashort = htons(FINDERINFO_CLOSEDVIEW);
716 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF,
717 &ashort, sizeof(ashort));
719 /* set default creator/type fields */
720 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,
722 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,
726 /* make things invisible */
727 if ((*path == '.') && strcmp(path, ".") && strcmp(path, "..")) {
728 ashort = htons(ATTRBIT_INVISIBLE);
729 ad_setattr(ad, ashort);
730 ashort = htons(FINDERINFO_INVISIBLE);
731 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,
732 &ashort, sizeof(ashort));
735 if (gettimeofday(&tv, NULL) < 0) {
736 ad_close(ad, adflags);
740 /* put something sane in the date fields */
741 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, tv.tv_sec);
742 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, tv.tv_sec);
743 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, tv.tv_sec);
744 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
748 * Read the adouble header in and parse it.
750 if ((ad_header_read( ad ) < 0)
751 #if AD_VERSION == AD_VERSION2
752 || (ad_v1tov2(ad, ad_p) < 0)
753 #endif /* AD_VERSION == AD_VERSION2 */
755 ad_close( ad, adflags );
760 ad->ad_hf.adf_refcount++;
766 /* to do this with mmap, we need the hfs fs to understand how to mmap
768 int ad_refresh(struct adouble *ad)
770 #ifdef USE_MMAPPED_HEADERS
772 #endif /* USE_MMAPPED_HEADERS */
774 if (ad->ad_hf.adf_fd < -1)
777 #ifdef USE_MMAPPED_HEADERS
778 if (ad->ad_data == MAP_FAILED)
781 /* re-read the header */
782 off = ad_getentryoff(ad, ADEID_RFORK);
783 memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof(nentries));
784 nentries = ntohs(nentries);
785 parse_entries(ad, ad->ad_data + AD_HEADER_LEN, nentries);
787 /* check to see if something screwy happened */
788 if (!ad_getentryoff(ad, ADEID_RFORK))
791 /* if there's a length discrepancy, remap the header. this shouldn't
792 * really ever happen. */
793 if (off != ad_getentryoff(ad, ADEID_RFORK)) {
794 char *buf = ad->ad_data;
796 ad->ad_data = mmap(NULL, ad_getentryoff(ad, ADEID_RFORK),
797 PROT_READ | PROT_WRITE,
798 (ad_getoflags(ad, ADFLAGS_HF) & O_RDWR) ?
799 MAP_SHARED : MAP_PRIVATE, ad->ad_hf.adf_fd, 0);
800 if (ad->ad_data == MAP_FAILED) {
808 #else /* USE_MMAPPED_HEADERS */
809 return ad_header_read(ad);
810 #endif /* USE_MMAPPED_HEADERS */