2 * $Id: ad_open.c,v 1.20 2002-10-10 20:27:36 jmarcus 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 */
39 #include <atalk/logger.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)
183 /* check to see if we should convert this header. */
184 if (!path || (ad->ad_version != AD_VERSION1))
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.
193 * so, we need 4*12 (entry ids) + 12 (shortname) + 4 (afpfilei) +
194 * 4 (did) + 8 (prodosi) = 76 more bytes. */
196 #define SHIFTDATA (AD_DATASZ2 - AD_DATASZ1)
198 /* bail if we can't get a lock */
199 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0) < 0)
202 if ((fd = open(path, O_RDWR)) < 0)
205 if (fstat(fd, &st) ||
206 ftruncate(fd, st.st_size + SHIFTDATA) < 0) {
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)) ==
218 off = ad->ad_eid[ADEID_RFORK].ade_off;
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);
224 #endif /* USER_MMAPPED_HEADERS */
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);
230 /* now, fix up our copy of the header */
231 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
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;
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;
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;
257 ad->ad_version = AD_VERSION2;
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);
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, st.st_mtime);
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);
272 /* rebuild the header and cleanup */
273 ad_rebuild_header(ad);
274 munmap(buf, st.st_size + SHIFTDATA);
276 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
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)
285 #endif /* USE_MMAPPED_HEADERS */
290 ftruncate(fd, st.st_size);
294 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
298 #endif /* AD_VERSION == AD_VERSION2 */
301 /* read in the entries */
302 static __inline__ void parse_entries(struct adouble *ad, char *buf,
305 u_int32_t eid, len, off;
307 /* now, read in the entry bits */
308 for (; nentries > 0; nentries-- ) {
309 memcpy(&eid, buf, sizeof( eid ));
311 buf += sizeof( eid );
312 memcpy(&off, buf, sizeof( off ));
314 buf += sizeof( off );
315 memcpy(&len, buf, sizeof( len ));
317 buf += sizeof( len );
319 if ( 0 < eid && eid < ADEID_MAX ) {
320 ad->ad_eid[ eid ].ade_off = off;
321 ad->ad_eid[ eid ].ade_len = len;
323 LOG(log_debug, logtype_default, "ad_refresh: nentries %hd eid %d\n",
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.
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)
339 #ifdef USE_MMAPPED_HEADERS
340 char buf[AD_ENTRY_LEN*ADEID_MAX];
341 #else /* USE_MMAPPED_HEADERS */
342 char *buf = ad->ad_data;
343 #endif /* USE_MMAPPED_HEADERS */
346 static int warning = 0;
348 /* read the header */
349 if ( ad->ad_hf.adf_off != 0 ) {
350 if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
353 ad->ad_hf.adf_off = 0;
356 if (read( ad->ad_hf.adf_fd, buf, AD_HEADER_LEN) != AD_HEADER_LEN) {
362 ad->ad_hf.adf_off = AD_HEADER_LEN;
364 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
365 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION,
366 sizeof( ad->ad_version ));
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) {
375 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
378 ad->ad_magic = AD_MAGIC;
379 ad->ad_version = AD_VERSION1;
381 } else if ((ad->ad_magic == AD_MAGIC) &&
382 (ad->ad_version == AD_VERSION1)) {
384 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
389 ad->ad_magic = ntohl( ad->ad_magic );
390 ad->ad_version = ntohl( ad->ad_version );
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 /* AD_VERSION == AD_VERSION2 */
399 LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
403 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
404 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
405 nentries = ntohs( nentries );
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))
413 #else /* USE_MMAPPED_HEADERS */
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 /* USE_MMAPPED_HEADERS */
418 if (read(ad->ad_hf.adf_fd, buf, len) != len) {
421 LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
424 ad->ad_hf.adf_off += len;
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 /* ! USE_MMAPPED_HEADERS */
434 LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset.");
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)
446 #else /* USE_MMAPPED_HEADERS */
448 len = ad_getentryoff(ad, ADEID_RFORK) - ad->ad_hf.adf_off;
449 if (read(ad->ad_hf.adf_fd, buf, len) != len) {
452 LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
455 #endif /* USE_MMAPPED_HEADERS */
457 /* fix up broken dates */
458 if (ad->ad_version == AD_VERSION1) {
462 if (fstat(ad->ad_hf.adf_fd, &st) < 0) {
463 return 1; /* fail silently */
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);
483 * Put the .AppleDouble where it needs to be:
487 * \ b/.AppleDouble/.Parent
490 ad_path( path, adflags )
494 static char pathbuf[ MAXPATHLEN + 1];
495 char c, *slash, buf[MAXPATHLEN + 1];
497 strncpy(buf, path, MAXPATHLEN);
498 if ( adflags & ADFLAGS_DIR ) {
499 strncpy( pathbuf, buf, MAXPATHLEN );
500 if ( *buf != '\0' ) {
501 strcat( pathbuf, "/" );
505 if (( slash = strrchr( buf, '/' )) != NULL ) {
508 strncpy( pathbuf, buf, MAXPATHLEN);
515 strncat( pathbuf, ".AppleDouble/", MAXPATHLEN - strlen(pathbuf));
516 strncat( pathbuf, slash, MAXPATHLEN - strlen(pathbuf));
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.
527 #define DEFMASK 07700 /* be conservative */
533 static char modebuf[ MAXPATHLEN + 1];
536 if ( strlen( path ) >= MAXPATHLEN ) {
537 return NULL; /* can't do it */
541 * For a path with directories in it, remove the final component
542 * (path or subdirectory name) to get the name we want to stat.
543 * For a path which is just a filename, use "." instead.
545 strcpy( modebuf, path );
546 if (( slash = strrchr( modebuf, '/' )) != NULL ) {
547 *slash = '\0'; /* remove pathname component */
549 modebuf[0] = '.'; /* use current directory */
556 ad_mode( path, mode )
564 return( mode ); /* save on syscalls */
568 return( mode & DEFMASK ); /* can't do it */
571 if ( stat( p, &stbuf ) != 0 ) {
572 return( mode & DEFMASK ); /* bail out... can't stat dir? */
575 return( mode & stbuf.st_mode );
579 * Use mkdir() with mode bits taken from ad_mode().
582 ad_mkdir( path, mode )
587 LOG(log_info, logtype_default, "ad_mkdir: Creating directory with mode %d", mode);
589 return mkdir( path, ad_mode( path, mode ) );
594 * It's not possible to open the header file O_RDONLY -- the read
595 * will fail and return an error. this refcounts things now.
597 int ad_open( path, adflags, oflags, mode, ad )
599 int adflags, oflags, mode;
602 const struct entry *eid;
608 if (ad->ad_inited != AD_INITED) {
611 adf_lock_init(&ad->ad_df);
612 adf_lock_init(&ad->ad_hf);
613 #ifdef USE_MMAPPED_HEADERS
614 ad->ad_data = MAP_FAILED;
615 #endif /* USE_MMAPPED_HEADERS */
616 ad->ad_inited = AD_INITED;
620 if (adflags & ADFLAGS_DF) {
621 if (ad_dfileno(ad) == -1) {
622 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
623 admode = ad_mode( path, mode );
624 if (( ad->ad_df.adf_fd = open( path, hoflags, admode )) < 0 ) {
625 if (errno == EACCES && !(oflags & O_RDWR)) {
627 ad->ad_df.adf_fd =open( path, hoflags, admode );
630 if ( ad->ad_df.adf_fd < 0)
632 ad->ad_df.adf_off = 0;
633 ad->ad_df.adf_flags = hoflags;
636 /* the file is already open... but */
637 if ((oflags & ( O_RDWR | O_WRONLY)) && /* we want write access */
638 !(ad->ad_df.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
644 ad->ad_df.adf_refcount++;
647 if (adflags & ADFLAGS_HF) {
648 if (ad_hfileno(ad) == -1) {
649 ad_p = ad_path( path, adflags );
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 );
659 if ( ad->ad_hf.adf_fd < 0 ) {
660 if (errno == ENOENT && (oflags & O_CREAT) ) {
662 * We're expecting to create a new adouble header file,
664 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
666 admode = ad_hf_mode(ad_mode( ad_p, mode ));
668 if (( ad->ad_hf.adf_fd = open( ad_p, oflags,
671 * Probably .AppleDouble doesn't exist, try to
674 if ((errno == ENOENT) &&
675 ((adflags & ADFLAGS_NOADOUBLE) == 0)) {
676 if (( slash = strrchr( ad_p, '/' )) == NULL ) {
677 ad_close( ad, adflags );
682 if ( ad_mkdir( ad_p, 0777 ) < 0 ) {
683 ad_close( ad, adflags );
687 if (( ad->ad_hf.adf_fd =
688 open( ad_p, oflags, ad_mode( ad_p, mode) )) < 0 ) {
689 ad_close( ad, adflags );
693 ad_close( ad, adflags );
697 ad->ad_hf.adf_flags = oflags;
699 ad_close( ad, adflags );
702 } else if ((fstat(ad->ad_hf.adf_fd, &st) == 0) &&
704 /* for 0 length files, treat them as new. */
705 ad->ad_hf.adf_flags = (oflags & ~O_RDONLY) | O_RDWR;
707 ad->ad_hf.adf_flags = hoflags;
709 ad->ad_hf.adf_off = 0;
712 * This is a new adouble header file. Initialize the structure,
713 * instead of reading it.
715 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
716 if ( ad->ad_hf.adf_flags & ( O_TRUNC | O_CREAT )) {
719 ad->ad_magic = AD_MAGIC;
720 ad->ad_version = AD_VERSION;
721 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
723 #ifdef USE_MMAPPED_HEADERS
724 /* truncate the header file and mmap it. */
725 ftruncate(ad->ad_hf.adf_fd, AD_DATASZ);
726 ad->ad_data = mmap(NULL, AD_DATASZ, PROT_READ | PROT_WRITE,
727 MAP_SHARED, ad->ad_hf.adf_fd, 0);
728 if (ad->ad_data == MAP_FAILED) {
729 ad_close(ad, adflags);
732 #else /* USE_MMAPPED_HEADERS */
733 memset(ad->ad_data, 0, sizeof(ad->ad_data));
734 #endif /* USE_MMAPPED_HEADERS */
738 ad->ad_eid[eid->id].ade_off = eid->offset;
739 ad->ad_eid[eid->id].ade_len = eid->len;
743 /* put something sane in the directory finderinfo */
744 if (adflags & ADFLAGS_DIR) {
745 /* set default view */
746 ashort = htons(FINDERINFO_CLOSEDVIEW);
747 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF,
748 &ashort, sizeof(ashort));
750 /* set default creator/type fields */
751 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,
753 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,
757 /* make things invisible */
758 if ((*path == '.') && strcmp(path, ".") && strcmp(path, "..")) {
759 ashort = htons(ATTRBIT_INVISIBLE);
760 ad_setattr(ad, ashort);
761 ashort = htons(FINDERINFO_INVISIBLE);
762 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,
763 &ashort, sizeof(ashort));
766 /*if (gettimeofday(&tv, NULL) < 0) {
767 ad_close(ad, adflags);
771 if (stat(path, &st) < 0) {
772 ad_close(ad, adflags);
776 /* put something sane in the date fields */
777 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
778 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
779 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
780 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
784 * Read the adouble header in and parse it.
786 if ((ad_header_read( ad ) < 0)
787 #if AD_VERSION == AD_VERSION2
788 || (ad_v1tov2(ad, ad_p) < 0)
789 #endif /* AD_VERSION == AD_VERSION2 */
791 ad_close( ad, adflags );
796 else { /* already open */
797 if ((oflags & ( O_RDWR | O_WRONLY)) &&
798 !(ad->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
799 if (adflags & ADFLAGS_DF) {
800 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
801 ad_close( ad, ADFLAGS_DF );
807 ad->ad_hf.adf_refcount++;
813 /* to do this with mmap, we need the hfs fs to understand how to mmap
815 int ad_refresh(struct adouble *ad)
817 #ifdef USE_MMAPPED_HEADERS
819 #endif /* USE_MMAPPED_HEADERS */
821 if (ad->ad_hf.adf_fd < -1)
824 #ifdef USE_MMAPPED_HEADERS
825 if (ad->ad_data == MAP_FAILED)
828 /* re-read the header */
829 off = ad_getentryoff(ad, ADEID_RFORK);
830 memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof(nentries));
831 nentries = ntohs(nentries);
832 parse_entries(ad, ad->ad_data + AD_HEADER_LEN, nentries);
834 /* check to see if something screwy happened */
835 if (!ad_getentryoff(ad, ADEID_RFORK))
838 /* if there's a length discrepancy, remap the header. this shouldn't
839 * really ever happen. */
840 if (off != ad_getentryoff(ad, ADEID_RFORK)) {
841 char *buf = ad->ad_data;
843 ad->ad_data = mmap(NULL, ad_getentryoff(ad, ADEID_RFORK),
844 PROT_READ | PROT_WRITE,
845 (ad_getoflags(ad, ADFLAGS_HF) & O_RDWR) ?
846 MAP_SHARED : MAP_PRIVATE, ad->ad_hf.adf_fd, 0);
847 if (ad->ad_data == MAP_FAILED) {
855 #else /* USE_MMAPPED_HEADERS */
856 return ad_header_read(ad);
857 #endif /* USE_MMAPPED_HEADERS */