2 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
3 * Copyright (c) 1990,1991 Regents of The University of Michigan.
6 * Permission to use, copy, modify, and distribute this software and
7 * its documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appears in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation, and that the name of The University
11 * of Michigan not be used in advertising or publicity pertaining to
12 * distribution of the software without specific, written prior
13 * permission. This software is supplied as is without expressed or
14 * implied warranties of any kind.
16 * Research Systems Unix Group
17 * The University of Michigan
19 * 535 W. William Street
22 * netatalk@itd.umich.edu
28 * Part of Netatalk's AppleDouble implementatation
29 * @note We don't use inlines because a good compiler should be
30 * able to optimize all the static funcs below.
31 * @sa include/atalk/adouble.h
36 #endif /* HAVE_CONFIG_H */
39 #include <sys/param.h>
43 #include <atalk/logger.h>
44 #include <atalk/adouble.h>
45 #include <atalk/util.h>
47 #include "ad_private.h"
50 #define MAX(a, b) ((a) < (b) ? (b) : (a))
54 * AppleDouble entry default offsets.
55 * The layout looks like this:
57 * this is the v1 layout:
59 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
61 * we need to change it to look like this:
64 * field length (in bytes)
67 * FILEDATESI 16 replaces FILEI
71 * SHORTNAME 12 8.3 new
74 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
75 * and add in the new fields.
78 #define ADEDOFF_MAGIC (0)
79 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
80 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
81 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
83 /* initial lengths of some of the fields */
84 #define ADEDLEN_INIT 0
86 /* make sure we don't redefine ADEDOFF_FILEI */
89 #endif /* ADEDOFF_FILEI */
91 #define ADEDOFF_NAME_V1 (AD_HEADER_LEN + ADEID_NUM_V1*AD_ENTRY_LEN)
92 #define ADEDOFF_COMMENT_V1 (ADEDOFF_NAME_V1 + ADEDLEN_NAME)
93 #define ADEDOFF_FILEI (ADEDOFF_COMMENT_V1 + ADEDLEN_COMMENT)
94 #define ADEDOFF_FINDERI_V1 (ADEDOFF_FILEI + ADEDLEN_FILEI)
95 #define ADEDOFF_RFORK_V1 (ADEDOFF_FINDERI_V1 + ADEDLEN_FINDERI)
97 /* i stick things in a slightly different order than their eid order in
98 * case i ever want to separate RootInfo behaviour from the rest of the
100 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
101 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
102 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
103 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
104 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
105 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
106 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
107 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
108 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
109 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
110 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
111 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
112 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
114 #define ADEDOFF_FINDERI_OSX (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN)
115 #define ADEDOFF_RFORK_OSX (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI)
117 /* we keep local copies of a bunch of stuff so that we can initialize things
120 /* this is to prevent changing timezones from causing problems with
121 localtime volumes. the screw-up is 30 years. we use a delta of 5
123 #define TIMEWARP_DELTA 157680000
127 uint32_t id, offset, len;
130 static uint32_t get_eid(struct adouble *ad, u_int32_t eid)
135 return ADEID_PRIVDEV;
137 return ADEID_PRIVINO;
139 return ADEID_PRIVSYN;
146 #define DISK_EID(ad, a) get_eid(ad, a)
148 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
149 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
150 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
151 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
152 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
153 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
154 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
155 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
156 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
157 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
158 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
159 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
160 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
161 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
165 /* OS X adouble finder info and resource fork only */
166 static const struct entry entry_order_osx[ADEID_NUM_OSX + 1] = {
167 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
168 {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
172 /* Using Extended Attributes */
173 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
174 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
175 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
176 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
177 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
178 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
182 /* update a version 2 adouble resource fork with our private entries */
183 static int ad_update(struct adouble *ad, const char *path)
186 u_int16_t nentries = 0;
187 off_t off, shiftdata=0;
188 const struct entry *eid;
189 static off_t entry_len[ADEID_MAX];
190 static char databuf[ADEID_MAX][256], *buf;
194 /* check to see if we should convert this header. */
195 if (!path || ad->ad_flags != AD_VERSION2)
198 LOG(log_maxdebug, logtype_default, "ad_update: checking whether '%s' needs an upgrade.", path);
200 if (!(ad->ad_md->adf_flags & O_RDWR)) {
201 /* we were unable to open the file read write the last time */
205 if (ad->ad_eid[ADEID_RFORK].ade_off) {
206 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
209 memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof( nentries ));
210 nentries = ntohs( nentries );
212 if ( shiftdata == 0 && nentries == ADEID_NUM_V2)
215 memset(entry_len, 0, sizeof(entry_len));
216 memset(databuf, 0, sizeof(databuf));
218 /* bail if we can't get a lock */
219 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
222 fd = ad->ad_md->adf_fd;
224 if (fstat(fd, &st)) {
228 if (st.st_size > 0x7fffffff) {
229 LOG(log_debug, logtype_default, "ad_update: file '%s' too big for update.", path);
234 off = ad->ad_eid[ADEID_RFORK].ade_off;
235 if (off > st.st_size) {
236 LOG(log_error, logtype_default, "ad_update: invalid resource fork offset. (off: %u)", off);
241 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
242 LOG(log_error, logtype_default, "ad_update: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
247 if ((void *) (buf = (char *)
248 mmap(NULL, st.st_size + shiftdata,
249 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
254 /* last place for failure. */
255 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
256 munmap(buf, st.st_size + shiftdata);
260 /* move the RFORK. this assumes that the RFORK is at the end */
262 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
265 munmap(buf, st.st_size + shiftdata);
267 /* now, fix up our copy of the header */
268 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
270 /* save the header entries */
273 if( ad->ad_eid[eid->id].ade_off != 0) {
274 if ( eid->id > 2 && ad->ad_eid[eid->id].ade_len < 256)
275 memcpy( databuf[eid->id], ad->ad_data +ad->ad_eid[eid->id].ade_off, ad->ad_eid[eid->id].ade_len);
276 entry_len[eid->id] = ad->ad_eid[eid->id].ade_len;
281 memset(ad->ad_data + AD_HEADER_LEN, 0, AD_DATASZ - AD_HEADER_LEN);
283 /* copy the saved entries to the new header */
286 if ( eid->id > 2 && entry_len[eid->id] > 0) {
287 memcpy(ad->ad_data+eid->offset, databuf[eid->id], entry_len[eid->id]);
289 ad->ad_eid[eid->id].ade_off = eid->offset;
290 ad->ad_eid[eid->id].ade_len = entry_len[eid->id];
294 /* rebuild the header and cleanup */
295 LOG(log_debug, logtype_default, "updated AD2 header %s", path);
300 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
305 /* ------------------------------------------
306 FIXME work only if < 2GB
308 static int ad_convert(struct adouble *ad, const char *path)
315 /* use resource fork offset from file */
324 if (!(ad->ad_md->adf_flags & ( O_RDWR))) {
325 /* we were unable to open the file read write the last time */
329 /* check to see if we should convert this header. */
330 toV2 = ad->ad_version == AD_VERSION1 && ad->ad_flags == AD_VERSION2;
331 toV1 = ad->ad_version == AD_VERSION2 && ad->ad_flags == AD_VERSION1;
336 /* convert from v1 to v2. what does this mean?
337 * 1) change FILEI into FILEDATESI
338 * 2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
339 * 3) move FILEI attributes into AFPFILEI
340 * 4) initialize ACCESS field of FILEDATESI.
341 * 5) move the resource fork
344 /* bail if we can't get a lock */
345 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
348 /* we reuse fd from the resource fork */
349 fd = ad->ad_md->adf_fd;
351 if (ad->ad_eid[ADEID_RFORK].ade_off) {
352 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
355 shiftdata = ADEDOFF_RFORK_V2 -ADEDOFF_RFORK_V1; /* 136 */
358 if (fstat(fd, &st)) {
362 if (st.st_size > 0x7fffffff -shiftdata) {
363 LOG(log_debug, logtype_default, "ad_v1tov2: file too big.");
368 off = ad->ad_eid[ADEID_RFORK].ade_off;
370 if (off > st.st_size) {
371 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off);
376 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
377 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
382 if ((void *) (buf = (char *)
383 mmap(NULL, st.st_size + shiftdata,
384 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
389 /* last place for failure. */
391 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
395 /* move the RFORK. this assumes that the RFORK is at the end */
397 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
400 munmap(buf, st.st_size + shiftdata);
402 /* now, fix up our copy of the header */
403 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
405 /* replace FILEI with FILEDATESI */
406 ad_getattr(ad, &attr);
407 ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
408 ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
409 ad->ad_eid[ADEID_FILEI].ade_off = 0;
410 ad->ad_eid[ADEID_FILEI].ade_len = 0;
412 /* add in the new entries */
413 ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
414 ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
415 ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
416 ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
417 ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
418 ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
419 ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
420 ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
422 ad->ad_eid[ADEID_PRIVDEV].ade_off = ADEDOFF_PRIVDEV;
423 ad->ad_eid[ADEID_PRIVDEV].ade_len = ADEDLEN_INIT;
424 ad->ad_eid[ADEID_PRIVINO].ade_off = ADEDOFF_PRIVINO;
425 ad->ad_eid[ADEID_PRIVINO].ade_len = ADEDLEN_INIT;
426 ad->ad_eid[ADEID_PRIVSYN].ade_off = ADEDOFF_PRIVSYN;
427 ad->ad_eid[ADEID_PRIVSYN].ade_len = ADEDLEN_INIT;
428 ad->ad_eid[ADEID_PRIVID].ade_off = ADEDOFF_PRIVID;
429 ad->ad_eid[ADEID_PRIVID].ade_len = ADEDLEN_INIT;
431 /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
432 ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
433 ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
434 ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
435 ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
437 /* switch to dest version */
438 ad->ad_version = (toV2)?AD_VERSION2:AD_VERSION1;
440 /* move our data buffer to make space for the new entries. */
441 memmove(ad->ad_data + ADEDOFF_NAME_V2, ad->ad_data + ADEDOFF_NAME_V1,
442 ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
444 /* now, fill in the space with appropriate stuff. we're
445 operating as a v2 file now. */
446 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
447 memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
448 memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
449 ad_setattr(ad, attr);
450 memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
451 memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
453 /* rebuild the header and cleanup */
458 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
463 /* -------------------------------------
466 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
468 uint32_t eid, len, off;
471 /* now, read in the entry bits */
472 for (; nentries > 0; nentries-- ) {
473 memcpy(&eid, buf, sizeof( eid ));
474 eid = DISK_EID(ad, ntohl( eid ));
475 buf += sizeof( eid );
476 memcpy(&off, buf, sizeof( off ));
478 buf += sizeof( off );
479 memcpy(&len, buf, sizeof( len ));
481 buf += sizeof( len );
483 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
484 (off +len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
485 ad->ad_eid[ eid ].ade_off = off;
486 ad->ad_eid[ eid ].ade_len = len;
487 } else if (!warning) {
489 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d",
495 /* this reads enough of the header so that we can figure out all of
496 * the entry lengths and offsets. once that's done, we just read/mmap
497 * the rest of the header in.
499 * NOTE: we're assuming that the resource fork is kept at the end of
500 * the file. also, mmapping won't work for the hfs fs until it
501 * understands how to mmap header files. */
502 static int ad_header_read(struct adouble *ad, struct stat *hst)
504 char *buf = ad->ad_data;
508 static int warning = 0;
511 /* read the header */
512 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
515 if (header_len < AD_HEADER_LEN) {
520 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
521 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
523 /* tag broken v1 headers. just assume they're all right.
524 * we detect two cases: null magic/version
525 * byte swapped magic/version
526 * XXX: in the future, you'll need the v1compat flag.
527 * (ad->ad_flags & ADFLAGS_V1COMPAT) */
528 if (!ad->ad_magic && !ad->ad_version) {
530 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
533 ad->ad_magic = AD_MAGIC;
534 ad->ad_version = AD_VERSION1;
536 } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
538 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
543 ad->ad_magic = ntohl( ad->ad_magic );
544 ad->ad_version = ntohl( ad->ad_version );
547 if ((ad->ad_magic != AD_MAGIC) ||
548 ((ad->ad_version != AD_VERSION1) && (ad->ad_version != AD_VERSION2))) {
549 LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
554 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
555 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
556 nentries = ntohs( nentries );
558 /* read in all the entry headers. if we have more than the
559 * maximum, just hope that the rfork is specified early on. */
560 len = nentries*AD_ENTRY_LEN;
562 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
563 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
565 buf += AD_HEADER_LEN;
566 if (len > header_len - AD_HEADER_LEN) {
567 LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
572 /* figure out all of the entry offsets and lengths. if we aren't
573 * able to read a resource fork entry, bail. */
574 nentries = len / AD_ENTRY_LEN;
575 parse_entries(ad, buf, nentries);
576 if (!ad_getentryoff(ad, ADEID_RFORK)
577 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
579 LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset.");
584 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
585 LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
592 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
593 return 1; /* fail silently */
596 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
598 /* fix up broken dates */
599 if (ad->ad_version == AD_VERSION1) {
602 /* check to see if the ad date is wrong. just see if we have
603 * a modification date in the future. */
604 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
605 (aint > TIMEWARP_DELTA + hst->st_mtime)) {
606 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
607 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
608 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
609 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
610 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
617 static int ad_header_read_ea(struct adouble *ad, struct stat *hst)
619 char *buf = ad->ad_data;
623 static int warning = 0;
626 /* read the header */
627 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
630 if (header_len < AD_HEADER_LEN) {
635 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
636 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
638 /* tag broken v1 headers. just assume they're all right.
639 * we detect two cases: null magic/version
640 * byte swapped magic/version
641 * XXX: in the future, you'll need the v1compat flag.
642 * (ad->ad_flags & ADFLAGS_V1COMPAT) */
643 if (!ad->ad_magic && !ad->ad_version) {
645 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
648 ad->ad_magic = AD_MAGIC;
649 ad->ad_version = AD_VERSION1;
651 } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
653 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
658 ad->ad_magic = ntohl( ad->ad_magic );
659 ad->ad_version = ntohl( ad->ad_version );
662 if ((ad->ad_magic != AD_MAGIC) ||
663 ((ad->ad_version != AD_VERSION1) && (ad->ad_version != AD_VERSION2))) {
664 LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
669 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
670 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
671 nentries = ntohs( nentries );
673 /* read in all the entry headers. if we have more than the
674 * maximum, just hope that the rfork is specified early on. */
675 len = nentries*AD_ENTRY_LEN;
677 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
678 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
680 buf += AD_HEADER_LEN;
681 if (len > header_len - AD_HEADER_LEN) {
682 LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
687 /* figure out all of the entry offsets and lengths. if we aren't
688 * able to read a resource fork entry, bail. */
689 nentries = len / AD_ENTRY_LEN;
690 parse_entries(ad, buf, nentries);
691 if (!ad_getentryoff(ad, ADEID_RFORK)
692 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
694 LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset.");
699 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
700 LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
707 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
708 return 1; /* fail silently */
711 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
713 /* fix up broken dates */
714 if (ad->ad_version == AD_VERSION1) {
717 /* check to see if the ad date is wrong. just see if we have
718 * a modification date in the future. */
719 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
720 (aint > TIMEWARP_DELTA + hst->st_mtime)) {
721 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
722 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
723 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
724 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
725 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
733 * Put the .AppleDouble where it needs to be:
737 * \ b/.AppleDouble/.Parent
739 * FIXME: should do something for pathname > MAXPATHLEN
741 char *ad_path( const char *path, int adflags)
743 static char pathbuf[ MAXPATHLEN + 1];
747 if ( adflags & ADFLAGS_DIR ) {
748 l = strlcpy( pathbuf, path, sizeof(pathbuf));
750 if ( l && l < MAXPATHLEN) {
753 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
755 if (NULL != ( slash = strrchr( path, '/' )) ) {
758 /* XXX we must return NULL here and test in the caller */
761 memcpy( pathbuf, path, l);
766 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
767 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
773 static int ad_mkrf(char *path)
777 * Probably .AppleDouble doesn't exist, try to mkdir it.
779 if (NULL == ( slash = strrchr( path, '/' )) ) {
784 if ( ad_mkdir( path, 0777 ) < 0 ) {
792 /***********************************************************************
793 * OS X adouble scheme funcs
794 ***********************************************************************/
797 * Put the resource fork where it needs to be: ._name
799 char *ad_path_osx(const char *path, int adflags _U_)
801 static char pathbuf[ MAXPATHLEN + 1];
802 char c, *slash, buf[MAXPATHLEN + 1];
804 if (!strcmp(path,".")) {
806 getcwd(buf, MAXPATHLEN);
809 strlcpy(buf, path, MAXPATHLEN +1);
811 if (NULL != ( slash = strrchr( buf, '/' )) ) {
814 strlcpy( pathbuf, buf, MAXPATHLEN +1);
820 strlcat( pathbuf, "._", MAXPATHLEN +1);
821 strlcat( pathbuf, slash, MAXPATHLEN +1);
825 /* -------------------- */
826 static int ad_mkrf_osx(char *path _U_)
831 /* -------------------------
832 * Support inherited protection modes for AppleDouble files. The supplied
833 * mode is ANDed with the parent directory's mask value in lieu of "umask",
834 * and that value is returned.
837 #define DEFMASK 07700 /* be conservative */
839 char *ad_dir(const char *path)
841 static char modebuf[ MAXPATHLEN + 1];
844 * For a path with directories in it, remove the final component
845 * (path or subdirectory name) to get the name we want to stat.
846 * For a path which is just a filename, use "." instead.
848 slash = strrchr( path, '/' );
853 if (len >= MAXPATHLEN) {
854 errno = ENAMETOOLONG;
855 return NULL; /* can't do it */
857 memcpy( modebuf, path, len );
859 /* is last char a '/' ? */
861 slash = modebuf+ len;
863 while (modebuf < slash && slash[-1] == '/') {
866 if (modebuf == slash) {
870 while (modebuf < slash && *slash != '/') {
873 if (modebuf == slash) {
876 *slash = '\0'; /* remove pathname component */
881 modebuf[0] = '.'; /* use current directory */
886 /* ---------------- */
887 static uid_t default_uid = -1;
889 int ad_setfuid(const uid_t id)
895 /* ---------------- */
896 uid_t ad_getfuid(void)
902 return inode of path parent directory
904 int ad_stat(const char *path, struct stat *stbuf)
913 return lstat( p, stbuf );
917 if we are root change path user/ group
918 It can be a native function for BSD cf. FAQ.Q10
919 path: pathname to chown
920 stbuf: parent directory inode
922 use fstat and fchown or lchown with linux?
924 #define EMULATE_SUIDDIR
926 static int ad_chown(const char *path, struct stat *stbuf)
929 #ifdef EMULATE_SUIDDIR
932 if (default_uid != (uid_t)-1) {
933 /* we are root (admin) */
934 id = (default_uid)?default_uid:stbuf->st_uid;
935 ret = lchown( path, id, stbuf->st_gid );
942 return access right and inode of path parent directory
944 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
949 if (ad_stat(path, stbuf) != 0) {
953 *mode &= stbuf->st_mode;
958 return access right of path parent directory
961 ad_mode( const char *path, int mode)
964 ad_mode_st(path, &mode, &stbuf);
969 * Use mkdir() with mode bits taken from ad_mode().
972 ad_mkdir( const char *path, int mode)
978 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
979 path, mode, getcwdpath());
981 st_invalid = ad_mode_st(path, &mode, &stbuf);
982 ret = mkdir( path, mode );
983 if (ret || st_invalid)
985 ad_chown(path, &stbuf);
990 /* ----------------- */
991 static int ad_error(struct adouble *ad, int adflags)
994 if ((adflags & ADFLAGS_NOHF)) {
995 /* FIXME double check : set header offset ?*/
998 if ((adflags & ADFLAGS_DF)) {
999 ad_close( ad, ADFLAGS_DF );
1005 static int new_rfork(const char *path, struct adouble *ad, int adflags);
1010 #define AD_SET(a) a = 0
1013 /* --------------------------- */
1014 static int ad_check_size(struct adouble *ad _U_, struct stat *st)
1016 if (st->st_size > 0 && st->st_size < AD_DATASZ1)
1021 /* --------------------------- */
1022 static int ad_check_size_sfm(struct adouble *ad _U_, struct stat *st)
1024 if (st->st_size > 0 && st->st_size < AD_SFM_LEN)
1029 /* --------------------------- */
1030 static int ad_header_upgrade(struct adouble *ad, char *name)
1033 if ( (ret = ad_convert(ad, name)) < 0 || (ret = ad_update(ad, name) < 0)) {
1039 /* --------------------------- */
1040 static int ad_header_upgrade_none(struct adouble *ad _U_, char *name _U_)
1045 /* --------------------------- */
1046 static struct adouble_fops ad_osx = {
1049 &ad_rebuild_adouble_header,
1055 static struct adouble_fops ad_adouble = {
1058 &ad_rebuild_adouble_header,
1064 static struct adouble_fops ad_adouble_ea = {
1067 &ad_rebuild_adouble_header,
1073 void ad_init(struct adouble *ad, int flags, int options)
1076 ad->ad_flags = flags;
1077 if (flags == AD_VERSION2_OSX) {
1078 ad->ad_ops = &ad_osx;
1079 ad->ad_md = &ad->ad_resource_fork;
1081 else if (flags == AD_VERSION2) {
1082 ad->ad_ops = &ad_adouble;
1083 ad->ad_md = &ad->ad_resource_fork;
1086 ad->ad_ops = &ad_adouble_ea;
1087 ad->ad_md = &ad->ad_metadata_fork;
1089 ad->ad_options = options;
1091 ad_data_fileno(ad) = -1;
1092 ad_reso_fileno(ad) = -1;
1093 ad_meta_fileno(ad) = -1;
1094 /* following can be read even if there's no meda data. */
1095 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1100 * Open data-, metadata(header)- or ressource fork
1102 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1105 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1108 * @param path Path to file or directory
1109 * @param adflags ADFLAGS_DF: open data file/fork\n
1110 * ADFLAGS_HF: open header (metadata) file\n
1111 * ADFLAGS_RF: open ressource fork *** FIXME: not used ?! *** \n
1112 * ADFLAGS_CREATE: indicate creation\n
1113 * ADFLAGS_NOHF: it's not an error if header file couldn't be created\n
1114 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags\n
1115 * ADFLAGS_NOADOUBLE: dont create adouble files if not necessary\n
1116 * ADFLAGS_RDONLY: open read only\n
1117 * ADFLAGS_OPENFORKS: check for open forks from other processes\n
1118 * ADFLAGS_MD: alias for ADFLAGS_HF\n
1119 * ADFLAGS_V1COMPAT: obsolete
1120 * @param oflags flags passed through to open syscall: \n
1121 * O_RDONLY: *** FIXME *** \n
1122 * O_RDWR: *** FIXME *** \n
1123 * O_CREAT: create fork\n
1124 * O_EXCL: fail if exists with O_CREAT
1125 * @param mode passed to open with O_CREAT
1126 * @param ad pointer to struct adouble
1128 * @returns 0 on success
1130 * @note It's not possible to open the header file O_RDONLY -- the read
1131 * will fail and return an error. this refcounts things now.\n
1132 * metadata(ressource)-fork only gets created with O_CREAT.
1134 int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1137 struct stat st_meta;
1138 struct stat *pst = NULL;
1140 int hoflags, admode;
1141 int st_invalid = -1;
1144 if (ad->ad_inited != AD_INITED) {
1145 ad->ad_inited = AD_INITED;
1146 ad->ad_refcount = 1;
1147 ad->ad_open_forks = 0;
1148 ad->ad_adflags = adflags;
1149 ad->ad_resource_fork.adf_refcount = 0;
1150 ad->ad_data_fork.adf_refcount = 0;
1151 ad->ad_data_fork.adf_syml=0;
1154 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1155 /* XXX not true if we have a meta data fork ? */
1156 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1157 ad->ad_open_forks |= ATTRBIT_ROPEN;
1160 if ((adflags & ADFLAGS_DF)) {
1161 if (ad_data_fileno(ad) == -1) {
1162 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1164 if ((oflags & O_CREAT)) {
1165 st_invalid = ad_mode_st(path, &admode, &st_dir);
1166 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1171 ad->ad_data_fork.adf_fd =open( path, hoflags | O_NOFOLLOW, admode );
1173 if (ad->ad_data_fork.adf_fd == -1) {
1174 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1176 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
1178 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
1181 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
1182 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
1184 free(ad->ad_data_fork.adf_syml);
1187 ad->ad_data_fork.adf_syml[lsz] = 0;
1188 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
1192 if ( ad->ad_data_fork.adf_fd == -1 )
1195 AD_SET(ad->ad_data_fork.adf_off);
1196 ad->ad_data_fork.adf_flags = hoflags;
1198 /* just created, set owner if admin (root) */
1199 ad_chown(path, &st_dir);
1201 adf_lock_init(&ad->ad_data_fork);
1204 /* the file is already open... but */
1205 if ((oflags & ( O_RDWR | O_WRONLY)) && /* we want write access */
1206 !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
1212 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
1213 * already open. Should we check for it? ie
1215 * O_TRUNC --> truncate the fork.
1216 * idem for ressource fork.
1219 open_df = ADFLAGS_DF;
1220 ad->ad_data_fork.adf_refcount++;
1223 if (!(adflags & ADFLAGS_HF))
1226 /* ****************************************** */
1228 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
1229 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1230 !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
1232 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
1233 ad_close( ad, open_df );
1239 /* it's not new anymore */
1240 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
1241 ad->ad_md->adf_refcount++;
1245 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1247 ad_p = ad->ad_ops->ad_path( path, adflags );
1249 hoflags = oflags & ~(O_CREAT | O_EXCL);
1250 if (!(adflags & ADFLAGS_RDONLY)) {
1251 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1253 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
1254 if (ad->ad_md->adf_fd < 0 ) {
1255 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1256 hoflags = oflags & ~(O_CREAT | O_EXCL);
1257 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
1261 if ( ad->ad_md->adf_fd < 0 ) {
1262 if (errno == ENOENT && (oflags & O_CREAT) ) {
1264 * We're expecting to create a new adouble header file,
1266 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
1268 LOG(log_debug, logtype_default, "ad_open(\"%s\"): {cwd: \"%s\"} creating adouble file",
1269 ad_p, getcwdpath());
1272 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1273 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1276 admode = ad_hf_mode(admode);
1277 if ((errno == ENOENT) && (ad->ad_flags != AD_VERSION2_OSX)) {
1278 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
1279 return ad_error(ad, adflags);
1282 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1283 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1286 admode = ad_hf_mode(admode);
1288 /* retry with O_CREAT */
1289 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
1290 if ( ad->ad_md->adf_fd < 0 ) {
1291 return ad_error(ad, adflags);
1293 ad->ad_md->adf_flags = oflags;
1294 /* just created, set owner if admin owner (root) */
1296 ad_chown(ad_p, &st_dir);
1300 return ad_error(ad, adflags);
1303 ad->ad_md->adf_flags = hoflags;
1304 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
1305 /* for 0 length files, treat them as new. */
1306 ad->ad_md->adf_flags |= O_TRUNC;
1309 /* we have valid data in st_meta stat structure, reused it
1315 AD_SET(ad->ad_md->adf_off);
1317 ad->ad_md->adf_refcount = 1;
1318 adf_lock_init(ad->ad_md);
1319 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
1321 * This is a new adouble header file. Initialize the structure,
1322 * instead of reading it.
1324 if (new_rfork(path, ad, adflags) < 0) {
1326 /* the file is already deleted, perm, whatever, so return an error*/
1327 ad_close(ad, adflags);
1333 /* Read the adouble header in and parse it.*/
1334 if (ad->ad_ops->ad_header_read( ad , pst) < 0
1335 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0)
1339 ad_close( ad, adflags );
1345 /* ****************************************** */
1346 /* open the resource fork if SFM */
1348 if (ad->ad_flags != AD_VERSION1_SFM) {
1352 if ((adflags & ADFLAGS_DIR)) {
1353 /* no resource fork for directories / volumes XXX it's false! */
1357 /* untrue yet but ad_close will decremente it*/
1358 ad->ad_resource_fork.adf_refcount++;
1360 if (ad_reso_fileno(ad) != -1) { /* the file is already open */
1361 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1362 !(ad->ad_resource_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
1364 ad_close( ad, open_df | ADFLAGS_HF);
1371 ad_p = ad->ad_ops->ad_path( path, ADFLAGS_RF );
1374 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1376 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1380 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1381 ad->ad_resource_fork.adf_fd = open( ad_p, hoflags, admode );
1383 if (ad->ad_resource_fork.adf_fd < 0 ) {
1384 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1386 ad->ad_resource_fork.adf_fd =open( ad_p, hoflags, admode );
1390 if ( ad->ad_resource_fork.adf_fd < 0) {
1393 ad_close( ad, adflags );
1397 adf_lock_init(&ad->ad_resource_fork);
1398 AD_SET(ad->ad_resource_fork.adf_off);
1399 ad->ad_resource_fork.adf_flags = hoflags;
1400 if ((oflags & O_CREAT) && !st_invalid) {
1401 /* just created, set owner if admin (root) */
1402 ad_chown(ad_p, &st_dir);
1404 else if (!fstat(ad->ad_resource_fork.adf_fd, &st_meta)) {
1405 ad->ad_rlen = st_meta.st_size;
1411 * @brief open metadata, possibly as root
1413 * Return only metadata but try very hard ie at first try as user, then try as root.
1415 * @param name name of file/dir
1416 * @param flags ADFLAGS_DIR: name is a directory \n
1417 * ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
1418 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1420 * @param adp pointer to struct adouble
1422 * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
1423 * a adouble file depends on various other volume options, eg. ADVOL_CACHE
1425 int ad_metadata(const char *name, int flags, struct adouble *adp)
1429 int create = O_RDONLY;
1431 dir = flags & ADFLAGS_DIR;
1433 /* Check if we shall call ad_open with O_CREAT */
1434 if ( (adp->ad_options & ADVOL_CACHE)
1435 && ! (adp->ad_options & ADVOL_NOADOUBLE)
1436 && (flags & ADFLAGS_CREATE) ) {
1437 create = O_CREAT | O_RDWR;
1439 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1442 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1446 /* we are root open read only */
1447 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1449 if ( seteuid(uid) < 0) {
1450 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1456 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1458 we need to check if the file is open by another process.
1459 it's slow so we only do it if we have to:
1461 - we don't already have the answer!
1463 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1469 * @brief openat like wrapper for ad_metadata
1471 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1477 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1483 if (ad_metadata(name, flags, adp) < 0) {
1489 if (fchdir(cwdfd) != 0) {
1490 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1503 /* ----------------------------------- */
1504 static int new_rfork(const char *path, struct adouble *ad, int adflags)
1506 const struct entry *eid;
1510 ad->ad_magic = AD_MAGIC;
1511 ad->ad_version = ad->ad_flags & 0x0f0000;
1512 if (!ad->ad_version) {
1513 ad->ad_version = AD_VERSION;
1516 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
1517 memset(ad->ad_data, 0, sizeof(ad->ad_data));
1519 if (ad->ad_flags == AD_VERSION2)
1521 else if (ad->ad_flags == AD_VERSION2_OSX)
1522 eid = entry_order_osx;
1524 eid = entry_order_ea;
1528 ad->ad_eid[eid->id].ade_off = eid->offset;
1529 ad->ad_eid[eid->id].ade_len = eid->len;
1533 /* put something sane in the directory finderinfo */
1534 if ((adflags & ADFLAGS_DIR)) {
1535 /* set default view */
1536 ashort = htons(FINDERINFO_CLOSEDVIEW);
1537 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF,
1538 &ashort, sizeof(ashort));
1540 /* set default creator/type fields */
1541 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
1542 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
1545 /* make things invisible */
1546 if ((ad->ad_options & ADVOL_INVDOTS) && !(adflags & ADFLAGS_CREATE) &&
1547 (*path == '.') && strcmp(path, ".") && strcmp(path, ".."))
1549 ashort = htons(ATTRBIT_INVISIBLE);
1550 ad_setattr(ad, ashort);
1551 ashort = htons(FINDERINFO_INVISIBLE);
1552 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1555 if (lstat(path, &st) < 0) {
1559 /* put something sane in the date fields */
1560 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
1561 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
1562 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
1563 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
1567 /* to do this with mmap, we need the hfs fs to understand how to mmap
1569 int ad_refresh(struct adouble *ad)
1572 if (ad_meta_fileno(ad) < 0)
1575 return ad->ad_ops->ad_header_read(ad, NULL);
1578 int ad_openat(int dirfd, /* dir fd openat like */
1589 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1595 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1601 if (fchdir(cwdfd) != 0) {
1602 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");