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 */
40 #include <atalk/adouble.h>
41 #include <sys/param.h>
42 #include <atalk/logger.h>
44 #include <atalk/util.h>
47 #include "ad_private.h"
51 #define MAX(a, b) ((a) < (b) ? (b) : (a))
55 * AppleDouble entry default offsets.
56 * The layout looks like this:
58 * this is the v1 layout:
60 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
62 * we need to change it to look like this:
65 * field length (in bytes)
68 * FILEDATESI 16 replaces FILEI
72 * SHORTNAME 12 8.3 new
75 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
76 * and add in the new fields.
78 * NOTE: the HFS module will need similar modifications to interact with
82 #define ADEDOFF_MAGIC (0)
83 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
84 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
85 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
87 /* initial lengths of some of the fields */
88 #define ADEDLEN_INIT 0
90 /* make sure we don't redefine ADEDOFF_FILEI */
93 #endif /* ADEDOFF_FILEI */
95 #define ADEDOFF_NAME_V1 (AD_HEADER_LEN + ADEID_NUM_V1*AD_ENTRY_LEN)
96 #define ADEDOFF_COMMENT_V1 (ADEDOFF_NAME_V1 + ADEDLEN_NAME)
97 #define ADEDOFF_FILEI (ADEDOFF_COMMENT_V1 + ADEDLEN_COMMENT)
98 #define ADEDOFF_FINDERI_V1 (ADEDOFF_FILEI + ADEDLEN_FILEI)
99 #define ADEDOFF_RFORK_V1 (ADEDOFF_FINDERI_V1 + ADEDLEN_FINDERI)
101 /* i stick things in a slightly different order than their eid order in
102 * case i ever want to separate RootInfo behaviour from the rest of the
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_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
113 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
114 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
115 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
117 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
119 #define ADEID_NUM_OSX 2
120 #define ADEDOFF_FINDERI_OSX (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN)
121 #define ADEDOFF_RFORK_OSX (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI)
123 /* we keep local copies of a bunch of stuff so that we can initialize things
126 /* this is to prevent changing timezones from causing problems with
127 localtime volumes. the screw-up is 30 years. we use a delta of 5
129 #define TIMEWARP_DELTA 157680000
133 u_int32_t id, offset, len;
136 static const struct entry entry_order1[ADEID_NUM_V1 +1] = {
137 {ADEID_NAME, ADEDOFF_NAME_V1, ADEDLEN_INIT}, /* 3 */
138 {ADEID_COMMENT, ADEDOFF_COMMENT_V1, ADEDLEN_INIT}, /* 4 */
139 {ADEID_FILEI, ADEDOFF_FILEI, ADEDLEN_FILEI}, /* 7 */
140 {ADEID_FINDERI, ADEDOFF_FINDERI_V1, ADEDLEN_FINDERI}, /* 9 */
141 {ADEID_RFORK, ADEDOFF_RFORK_V1, ADEDLEN_INIT}, /* 2 */
145 #if AD_VERSION == AD_VERSION1
146 #define DISK_EID(ad, a) (a)
148 #else /* AD_VERSION == AD_VERSION2 */
150 static u_int32_t get_eid(struct adouble *ad, u_int32_t eid)
154 if (ad->ad_version == AD_VERSION1)
157 return ADEID_PRIVDEV;
159 return ADEID_PRIVINO;
161 return ADEID_PRIVSYN;
168 #define DISK_EID(ad, a) get_eid(ad, a)
170 static const struct entry entry_order2[ADEID_NUM_V2 +1] = {
171 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
172 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
173 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
174 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
175 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
176 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
177 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
178 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
179 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
180 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
181 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
182 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
183 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
188 /* OS X adouble finder info and resource fork only
190 static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
191 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
192 {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
197 #define ADEID_NUM_SFM 3
198 static const struct entry entry_order_sfm[ADEID_NUM_SFM +1] = {
199 {ADEID_FINDERI, 16, ADEDLEN_FINDERI}, /* 9 */
200 {ADEID_SFMRESERVE2, 16+32, 6}, /* 21 */
201 {ADEID_FILEI, 60, ADEDLEN_FILEI}, /* 7 */
206 #endif /* AD_VERSION == AD_VERSION2 */
208 #if AD_VERSION == AD_VERSION2
210 /* update a version 2 adouble resource fork with our private entries */
211 static int ad_update(struct adouble *ad, const char *path)
214 u_int16_t nentries = 0;
215 off_t off, shiftdata=0;
216 const struct entry *eid;
217 static off_t entry_len[ADEID_MAX];
218 static char databuf[ADEID_MAX][256], *buf;
222 /* check to see if we should convert this header. */
223 if (!path || ad->ad_flags != AD_VERSION2)
226 LOG(log_maxdebug, logtype_default, "ad_update: checking whether '%s' needs an upgrade.", path);
228 if (!(ad->ad_md->adf_flags & O_RDWR)) {
229 /* we were unable to open the file read write the last time */
233 if (ad->ad_eid[ADEID_RFORK].ade_off) {
234 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
237 memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof( nentries ));
238 nentries = ntohs( nentries );
240 if ( shiftdata == 0 && nentries == ADEID_NUM_V2)
243 memset(entry_len, 0, sizeof(entry_len));
244 memset(databuf, 0, sizeof(databuf));
246 /* bail if we can't get a lock */
247 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
250 fd = ad->ad_md->adf_fd;
252 if (fstat(fd, &st)) {
256 if (st.st_size > 0x7fffffff) {
257 LOG(log_debug, logtype_default, "ad_update: file '%s' too big for update.", path);
262 off = ad->ad_eid[ADEID_RFORK].ade_off;
263 if (off > st.st_size) {
264 LOG(log_error, logtype_default, "ad_update: invalid resource fork offset. (off: %u)", off);
269 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
270 LOG(log_error, logtype_default, "ad_update: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
275 if ((void *) (buf = (char *)
276 mmap(NULL, st.st_size + shiftdata,
277 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
282 /* last place for failure. */
283 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
284 munmap(buf, st.st_size + shiftdata);
288 /* move the RFORK. this assumes that the RFORK is at the end */
290 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
293 munmap(buf, st.st_size + shiftdata);
295 /* now, fix up our copy of the header */
296 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
298 /* save the header entries */
301 if( ad->ad_eid[eid->id].ade_off != 0) {
302 if ( eid->id > 2 && ad->ad_eid[eid->id].ade_len < 256)
303 memcpy( databuf[eid->id], ad->ad_data +ad->ad_eid[eid->id].ade_off, ad->ad_eid[eid->id].ade_len);
304 entry_len[eid->id] = ad->ad_eid[eid->id].ade_len;
309 memset(ad->ad_data + AD_HEADER_LEN, 0, AD_DATASZ - AD_HEADER_LEN);
311 /* copy the saved entries to the new header */
314 if ( eid->id > 2 && entry_len[eid->id] > 0) {
315 memcpy(ad->ad_data+eid->offset, databuf[eid->id], entry_len[eid->id]);
317 ad->ad_eid[eid->id].ade_off = eid->offset;
318 ad->ad_eid[eid->id].ade_len = entry_len[eid->id];
322 /* rebuild the header and cleanup */
323 LOG(log_debug, logtype_default, "updated AD2 header %s", path);
328 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
333 /* ------------------------------------------
334 FIXME work only if < 2GB
336 static int ad_convert(struct adouble *ad, const char *path)
343 /* use resource fork offset from file */
352 if (!(ad->ad_md->adf_flags & ( O_RDWR))) {
353 /* we were unable to open the file read write the last time */
357 /* check to see if we should convert this header. */
358 toV2 = ad->ad_version == AD_VERSION1 && ad->ad_flags == AD_VERSION2;
359 toV1 = ad->ad_version == AD_VERSION2 && ad->ad_flags == AD_VERSION1;
364 /* convert from v1 to v2. what does this mean?
365 * 1) change FILEI into FILEDATESI
366 * 2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
367 * 3) move FILEI attributes into AFPFILEI
368 * 4) initialize ACCESS field of FILEDATESI.
369 * 5) move the resource fork
372 /* bail if we can't get a lock */
373 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
376 /* we reuse fd from the resource fork */
377 fd = ad->ad_md->adf_fd;
379 if (ad->ad_eid[ADEID_RFORK].ade_off) {
380 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
383 shiftdata = ADEDOFF_RFORK_V2 -ADEDOFF_RFORK_V1; /* 136 */
386 if (fstat(fd, &st)) {
390 if (st.st_size > 0x7fffffff -shiftdata) {
391 LOG(log_debug, logtype_default, "ad_v1tov2: file too big.");
396 off = ad->ad_eid[ADEID_RFORK].ade_off;
398 if (off > st.st_size) {
399 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off);
404 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
405 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
410 if ((void *) (buf = (char *)
411 mmap(NULL, st.st_size + shiftdata,
412 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
417 /* last place for failure. */
419 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
423 /* move the RFORK. this assumes that the RFORK is at the end */
425 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
428 munmap(buf, st.st_size + shiftdata);
430 /* now, fix up our copy of the header */
431 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
433 /* replace FILEI with FILEDATESI */
434 ad_getattr(ad, &attr);
435 ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
436 ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
437 ad->ad_eid[ADEID_FILEI].ade_off = 0;
438 ad->ad_eid[ADEID_FILEI].ade_len = 0;
440 /* add in the new entries */
441 ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
442 ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
443 ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
444 ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
445 ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
446 ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
447 ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
448 ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
450 ad->ad_eid[ADEID_PRIVDEV].ade_off = ADEDOFF_PRIVDEV;
451 ad->ad_eid[ADEID_PRIVDEV].ade_len = ADEDLEN_INIT;
452 ad->ad_eid[ADEID_PRIVINO].ade_off = ADEDOFF_PRIVINO;
453 ad->ad_eid[ADEID_PRIVINO].ade_len = ADEDLEN_INIT;
454 ad->ad_eid[ADEID_PRIVSYN].ade_off = ADEDOFF_PRIVSYN;
455 ad->ad_eid[ADEID_PRIVSYN].ade_len = ADEDLEN_INIT;
456 ad->ad_eid[ADEID_PRIVID].ade_off = ADEDOFF_PRIVID;
457 ad->ad_eid[ADEID_PRIVID].ade_len = ADEDLEN_INIT;
459 /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
460 ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
461 ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
462 ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
463 ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
465 /* switch to dest version */
466 ad->ad_version = (toV2)?AD_VERSION2:AD_VERSION1;
468 /* move our data buffer to make space for the new entries. */
469 memmove(ad->ad_data + ADEDOFF_NAME_V2, ad->ad_data + ADEDOFF_NAME_V1,
470 ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
472 /* now, fill in the space with appropriate stuff. we're
473 operating as a v2 file now. */
474 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
475 memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
476 memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
477 ad_setattr(ad, attr);
478 memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
479 memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
481 /* rebuild the header and cleanup */
486 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
490 #endif /* AD_VERSION == AD_VERSION2 */
492 /* -------------------------------------
495 static void parse_entries(struct adouble *ad, char *buf,
498 u_int32_t eid, len, off;
501 /* now, read in the entry bits */
502 for (; nentries > 0; nentries-- ) {
503 memcpy(&eid, buf, sizeof( eid ));
504 eid = DISK_EID(ad, ntohl( eid ));
505 buf += sizeof( eid );
506 memcpy(&off, buf, sizeof( off ));
508 buf += sizeof( off );
509 memcpy(&len, buf, sizeof( len ));
511 buf += sizeof( len );
513 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
514 (off +len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
515 ad->ad_eid[ eid ].ade_off = off;
516 ad->ad_eid[ eid ].ade_len = len;
517 } else if (!warning) {
519 LOG(log_debug, logtype_default, "ad_refresh: nentries %hd eid %d",
526 /* this reads enough of the header so that we can figure out all of
527 * the entry lengths and offsets. once that's done, we just read/mmap
528 * the rest of the header in.
530 * NOTE: we're assuming that the resource fork is kept at the end of
531 * the file. also, mmapping won't work for the hfs fs until it
532 * understands how to mmap header files. */
533 static int ad_header_read(struct adouble *ad, struct stat *hst)
535 char *buf = ad->ad_data;
539 static int warning = 0;
542 /* read the header */
543 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
546 if (header_len < AD_HEADER_LEN) {
551 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
552 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
554 /* tag broken v1 headers. just assume they're all right.
555 * we detect two cases: null magic/version
556 * byte swapped magic/version
557 * XXX: in the future, you'll need the v1compat flag.
558 * (ad->ad_flags & ADFLAGS_V1COMPAT) */
559 if (!ad->ad_magic && !ad->ad_version) {
561 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
564 ad->ad_magic = AD_MAGIC;
565 ad->ad_version = AD_VERSION1;
567 } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
569 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
574 ad->ad_magic = ntohl( ad->ad_magic );
575 ad->ad_version = ntohl( ad->ad_version );
578 if ((ad->ad_magic != AD_MAGIC) || ((ad->ad_version != AD_VERSION1)
579 #if AD_VERSION == AD_VERSION2
580 && (ad->ad_version != AD_VERSION2)
581 #endif /* AD_VERSION == AD_VERSION2 */
583 LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
588 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
589 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
590 nentries = ntohs( nentries );
592 /* read in all the entry headers. if we have more than the
593 * maximum, just hope that the rfork is specified early on. */
594 len = nentries*AD_ENTRY_LEN;
596 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
597 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
599 buf += AD_HEADER_LEN;
600 if (len > header_len - AD_HEADER_LEN) {
601 LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
606 /* figure out all of the entry offsets and lengths. if we aren't
607 * able to read a resource fork entry, bail. */
608 nentries = len / AD_ENTRY_LEN;
609 parse_entries(ad, buf, nentries);
610 if (!ad_getentryoff(ad, ADEID_RFORK)
611 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
613 LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset.");
618 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
619 LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
626 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
627 return 1; /* fail silently */
630 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
632 /* fix up broken dates */
633 if (ad->ad_version == AD_VERSION1) {
636 /* check to see if the ad date is wrong. just see if we have
637 * a modification date in the future. */
638 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
639 (aint > TIMEWARP_DELTA + hst->st_mtime)) {
640 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
641 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
642 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
643 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
644 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
651 /* ---------------------------
656 byte afpi_Signature[4]; /* Must be 0x00504641 */
657 byte afpi_Version[4]; /* Must be 0x00010000 */
658 byte afpi_Reserved1[4];
659 byte afpi_BackupTime[4]; /* Backup time for the file/dir */
660 byte finderinfo[32]; /* Finder info */
661 byte afpi_ProDosInfo[6]; /* ProDos Info */
662 byte afpi_Reserved2[6];
666 static int ad_header_sfm_read(struct adouble *ad, struct stat *hst)
668 char *buf = ad->ad_data;
669 const struct entry *eid;
673 /* read the header */
674 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
677 if (header_len != AD_SFM_LEN) {
682 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
683 memcpy(&ad->ad_version, buf + 4, sizeof( ad->ad_version ));
685 /* FIXME in the great Microsoft tradition they aren't in network order */
687 if (ad->ad_magic == SFM_MAGIC && ad->ad_version == AD_VERSION1) {
688 static int warning = 0;
690 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
695 ad->ad_magic = ntohl( ad->ad_magic );
696 ad->ad_version = ntohl( ad->ad_version );
699 if ((ad->ad_magic != SFM_MAGIC) || ((ad->ad_version != AD_VERSION1) )) {
701 LOG(log_debug, logtype_default, "ad_header_sfm_read: can't parse AppleDouble header.");
705 /* reinit adouble table */
706 eid = entry_order_sfm;
708 ad->ad_eid[eid->id].ade_off = eid->offset;
709 ad->ad_eid[eid->id].ade_len = eid->len;
713 /* steal some prodos for attribute */
717 memcpy(&attribute, buf + 48 +4, sizeof(attribute));
718 ad_setattr(ad, attribute );
721 if (ad->ad_resource_fork.adf_fd != -1) {
722 /* we have a resource fork use it rather than the metadata */
723 if (fstat(ad->ad_resource_fork.adf_fd, &st) < 0) {
729 ad->ad_rlen = st.st_size;
732 else if (hst == NULL) {
734 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
735 return 1; /* fail silently */
739 /* fix up broken dates */
740 if (ad->ad_version == AD_VERSION1) {
743 /* check to see if the ad date is wrong. just see if we have
744 * a modification date in the future. */
745 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
746 (aint > TIMEWARP_DELTA + hst->st_mtime)) {
747 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
748 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
749 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
750 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
751 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
758 /* ---------------------------------------
759 * Put the .AppleDouble where it needs to be:
763 * \ b/.AppleDouble/.Parent
765 * FIXME: should do something for pathname > MAXPATHLEN
768 ad_path( const char *path, int adflags)
770 static char pathbuf[ MAXPATHLEN + 1];
774 if ( adflags & ADFLAGS_DIR ) {
775 l = strlcpy( pathbuf, path, sizeof(pathbuf));
777 if ( l && l < MAXPATHLEN) {
780 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
782 if (NULL != ( slash = strrchr( path, '/' )) ) {
785 /* XXX we must return NULL here and test in the caller */
788 memcpy( pathbuf, path, l);
793 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
794 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
800 /* -------------------- */
801 static int ad_mkrf(char *path)
805 * Probably .AppleDouble doesn't exist, try to mkdir it.
807 if (NULL == ( slash = strrchr( path, '/' )) ) {
812 if ( ad_mkdir( path, 0777 ) < 0 ) {
819 /* ---------------------------------------
820 * Put the resource fork where it needs to be:
824 ad_path_osx(const char *path, int adflags _U_)
826 static char pathbuf[ MAXPATHLEN + 1];
827 char c, *slash, buf[MAXPATHLEN + 1];
829 if (!strcmp(path,".")) {
831 getcwd(buf, MAXPATHLEN);
834 strlcpy(buf, path, MAXPATHLEN +1);
836 if (NULL != ( slash = strrchr( buf, '/' )) ) {
839 strlcpy( pathbuf, buf, MAXPATHLEN +1);
845 strlcat( pathbuf, "._", MAXPATHLEN +1);
846 strlcat( pathbuf, slash, MAXPATHLEN +1);
849 /* -------------------- */
850 static int ad_mkrf_osx(char *path _U_)
855 /* ---------------------------------------
856 * Put the .AppleDouble where it needs to be:
858 * / a/.AppleDouble/b/AFP_AfpInfo
860 * \ b/.AppleDouble/.Parent/AFP_AfpInfo
864 ad_path_sfm( const char *path, int adflags)
866 static char pathbuf[ MAXPATHLEN + 1];
867 char c, *slash, buf[MAXPATHLEN + 1];
870 l = strlcpy(buf, path, MAXPATHLEN +1);
871 if ( adflags & ADFLAGS_DIR ) {
872 strcpy( pathbuf, buf);
873 if ( *buf != '\0' && l < MAXPATHLEN) {
879 if (NULL != ( slash = strrchr( buf, '/' )) ) {
882 strcpy( pathbuf, buf);
889 strlcat( pathbuf, ".AppleDouble/", MAXPATHLEN +1);
890 strlcat( pathbuf, slash, MAXPATHLEN +1);
892 if ((adflags == ADFLAGS_RF)) {
893 strlcat( pathbuf, "/AFP_Resource", MAXPATHLEN +1);
896 strlcat( pathbuf, "/AFP_AfpInfo", MAXPATHLEN +1);
901 /* -------------------- */
902 static int ad_mkrf_sfm(char *path)
906 * Probably .AppleDouble doesn't exist, try to mkdir it.
908 if (NULL == ( slash = strrchr( path, '/' )) ) {
913 if ( ad_mkdir( path, 0777 ) < 0 ) {
914 if ( errno == ENOENT ) {
917 if (NULL == ( slash1 = strrchr( path, '/' )) )
921 if ( ad_mkdir( path, 0777 ) < 0 )
924 if ( ad_mkdir( path, 0777 ) < 0 )
934 /* -------------------------
935 * Support inherited protection modes for AppleDouble files. The supplied
936 * mode is ANDed with the parent directory's mask value in lieu of "umask",
937 * and that value is returned.
940 #define DEFMASK 07700 /* be conservative */
943 *ad_dir(const char *path)
945 static char modebuf[ MAXPATHLEN + 1];
948 * For a path with directories in it, remove the final component
949 * (path or subdirectory name) to get the name we want to stat.
950 * For a path which is just a filename, use "." instead.
952 slash = strrchr( path, '/' );
957 if (len >= MAXPATHLEN) {
958 errno = ENAMETOOLONG;
959 return NULL; /* can't do it */
961 memcpy( modebuf, path, len );
963 /* is last char a '/' ? */
965 slash = modebuf+ len;
967 while (modebuf < slash && slash[-1] == '/') {
970 if (modebuf == slash) {
974 while (modebuf < slash && *slash != '/') {
977 if (modebuf == slash) {
980 *slash = '\0'; /* remove pathname component */
985 modebuf[0] = '.'; /* use current directory */
990 /* ---------------- */
991 static uid_t default_uid = -1;
993 int ad_setfuid(const uid_t id)
999 /* ---------------- */
1000 uid_t ad_getfuid(void)
1006 return inode of path parent directory
1008 int ad_stat(const char *path, struct stat *stbuf)
1017 return stat(p, stbuf);
1021 if we are root change path user/ group
1022 It can be a native function for BSD cf. FAQ.Q10
1023 path: pathname to chown
1024 stbuf: parent directory inode
1026 use fstat and fchown or lchown with linux?
1028 #define EMULATE_SUIDDIR
1030 static int ad_chown(const char *path, struct stat *stbuf)
1033 #ifdef EMULATE_SUIDDIR
1036 if (default_uid != (uid_t)-1) {
1037 /* we are root (admin) */
1038 id = (default_uid)?default_uid:stbuf->st_uid;
1039 ret = chown(path, id, stbuf->st_gid);
1046 return access right and inode of path parent directory
1048 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
1053 if (ad_stat(path, stbuf) != 0) {
1057 *mode &= stbuf->st_mode;
1062 return access right of path parent directory
1065 ad_mode( const char *path, int mode)
1068 ad_mode_st(path, &mode, &stbuf);
1073 * Use mkdir() with mode bits taken from ad_mode().
1076 ad_mkdir( const char *path, int mode)
1082 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1083 path, mode, getcwdpath());
1085 st_invalid = ad_mode_st(path, &mode, &stbuf);
1086 ret = mkdir( path, mode );
1087 if (ret || st_invalid)
1089 ad_chown(path, &stbuf);
1094 /* ----------------- */
1095 static int ad_error(struct adouble *ad, int adflags)
1098 if ((adflags & ADFLAGS_NOHF)) {
1099 /* FIXME double check : set header offset ?*/
1102 if ((adflags & ADFLAGS_DF)) {
1103 ad_close( ad, ADFLAGS_DF );
1109 static int new_rfork(const char *path, struct adouble *ad, int adflags);
1114 #define AD_SET(a) a = 0
1117 /* --------------------------- */
1118 static int ad_check_size(struct adouble *ad _U_, struct stat *st)
1120 if (st->st_size > 0 && st->st_size < AD_DATASZ1)
1125 /* --------------------------- */
1126 static int ad_check_size_sfm(struct adouble *ad _U_, struct stat *st)
1128 if (st->st_size > 0 && st->st_size < AD_SFM_LEN)
1133 /* --------------------------- */
1134 static int ad_header_upgrade(struct adouble *ad, char *name)
1136 #if AD_VERSION == AD_VERSION2
1138 if ( (ret = ad_convert(ad, name)) < 0 || (ret = ad_update(ad, name) < 0)) {
1145 /* --------------------------- */
1146 static int ad_header_upgrade_none(struct adouble *ad _U_, char *name _U_)
1151 /* --------------------------- */
1152 static struct adouble_fops ad_osx = {
1155 &ad_rebuild_adouble_header,
1162 static struct adouble_fops ad_sfm = {
1165 &ad_rebuild_sfm_header,
1168 &ad_header_sfm_read,
1169 &ad_header_upgrade_none,
1172 static struct adouble_fops ad_adouble = {
1175 &ad_rebuild_adouble_header,
1183 void ad_init(struct adouble *ad, int flags, int options)
1186 ad->ad_flags = flags;
1187 if (flags == AD_VERSION2_OSX) {
1188 ad->ad_ops = &ad_osx;
1189 ad->ad_md = &ad->ad_resource_fork;
1191 else if (flags == AD_VERSION1_SFM) {
1192 ad->ad_ops = &ad_sfm;
1193 ad->ad_md = &ad->ad_metadata_fork;
1196 ad->ad_ops = &ad_adouble;
1197 ad->ad_md = &ad->ad_resource_fork;
1199 ad->ad_options = options;
1201 ad_data_fileno(ad) = -1;
1202 ad_reso_fileno(ad) = -1;
1203 ad_meta_fileno(ad) = -1;
1204 /* following can be read even if there's no
1207 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1212 * Open data-, metadata(header)- or ressource fork
1214 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1217 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1220 * @param path Path to file or directory
1222 * @param adflags ADFLAGS_DF: open data file/fork\n
1223 * ADFLAGS_HF: open header (metadata) file\n
1224 * ADFLAGS_RF: open ressource fork *** FIXME: not used ?! *** \n
1225 * ADFLAGS_CREATE: indicate creation\n
1226 * ADFLAGS_NOHF: it's not an error if header file couldn't be created\n
1227 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags\n
1228 * ADFLAGS_NOADOUBLE: dont create adouble files if not necessary\n
1229 * ADFLAGS_RDONLY: open read only\n
1230 * ADFLAGS_OPENFORKS: check for open forks from other processes\n
1231 * ADFLAGS_MD: alias for ADFLAGS_HF\n
1232 * ADFLAGS_V1COMPAT: obsolete
1234 * @param oflags flags passed through to open syscall: \n
1235 * O_RDONLY: *** FIXME *** \n
1236 * O_RDWR: *** FIXME *** \n
1237 * O_CREAT: create fork\n
1238 * O_EXCL: fail if exists with O_CREAT
1240 * @param mode passed to open with O_CREAT
1242 * @param ad pointer to struct adouble
1244 * @returns 0 on success
1246 * @note It's not possible to open the header file O_RDONLY -- the read
1247 * will fail and return an error. this refcounts things now.\n
1248 * metadata(ressource)-fork only gets created with O_CREAT.
1250 int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1253 struct stat st_meta;
1254 struct stat *pst = NULL;
1256 int hoflags, admode;
1257 int st_invalid = -1;
1260 if (ad->ad_inited != AD_INITED) {
1261 ad->ad_inited = AD_INITED;
1262 ad->ad_refcount = 1;
1263 ad->ad_open_forks = 0;
1264 ad->ad_adflags = adflags;
1265 ad->ad_resource_fork.adf_refcount = 0;
1266 ad->ad_data_fork.adf_refcount = 0;
1267 ad->ad_data_fork.adf_syml=0;
1270 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1271 /* XXX not true if we have a meta data fork ? */
1272 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1273 ad->ad_open_forks |= ATTRBIT_ROPEN;
1276 if ((adflags & ADFLAGS_DF)) {
1277 if (ad_data_fileno(ad) == -1) {
1278 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1280 if ((oflags & O_CREAT)) {
1281 st_invalid = ad_mode_st(path, &admode, &st_dir);
1282 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1287 ad->ad_data_fork.adf_fd = open(path, hoflags | ad_get_syml_opt(ad), admode);
1289 if (ad->ad_data_fork.adf_fd == -1) {
1290 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1292 ad->ad_data_fork.adf_fd = open( path, hoflags | ad_get_syml_opt(ad), admode );
1294 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
1297 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
1298 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
1300 free(ad->ad_data_fork.adf_syml);
1303 ad->ad_data_fork.adf_syml[lsz] = 0;
1304 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
1308 if ( ad->ad_data_fork.adf_fd == -1 )
1311 AD_SET(ad->ad_data_fork.adf_off);
1312 ad->ad_data_fork.adf_flags = hoflags;
1314 /* just created, set owner if admin (root) */
1315 ad_chown(path, &st_dir);
1317 adf_lock_init(&ad->ad_data_fork);
1320 /* the file is already open... but */
1321 if ((oflags & ( O_RDWR | O_WRONLY)) && /* we want write access */
1322 !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
1328 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
1329 * already open. Should we check for it? ie
1331 * O_TRUNC --> truncate the fork.
1332 * idem for ressource fork.
1335 open_df = ADFLAGS_DF;
1336 ad->ad_data_fork.adf_refcount++;
1339 if (!(adflags & ADFLAGS_HF))
1342 /* ****************************************** */
1344 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
1345 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1346 !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
1348 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
1349 ad_close( ad, open_df );
1355 /* it's not new anymore */
1356 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
1357 ad->ad_md->adf_refcount++;
1361 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1363 ad_p = ad->ad_ops->ad_path( path, adflags );
1365 hoflags = oflags & ~(O_CREAT | O_EXCL);
1366 if (!(adflags & ADFLAGS_RDONLY)) {
1367 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1369 ad->ad_md->adf_fd = open(ad_p, hoflags | ad_get_syml_opt(ad), 0);
1370 if (ad->ad_md->adf_fd < 0 ) {
1371 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1372 hoflags = oflags & ~(O_CREAT | O_EXCL);
1373 ad->ad_md->adf_fd = open(ad_p, hoflags | ad_get_syml_opt(ad), 0);
1377 if ( ad->ad_md->adf_fd < 0 ) {
1378 if (errno == ENOENT && (oflags & O_CREAT) ) {
1380 * We're expecting to create a new adouble header file,
1382 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
1384 LOG(log_debug, logtype_default, "ad_open(\"%s\"): {cwd: \"%s\"} creating adouble file",
1385 ad_p, getcwdpath());
1388 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1389 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1392 admode = ad_hf_mode(admode);
1393 if ((errno == ENOENT) && (ad->ad_flags != AD_VERSION2_OSX)) {
1394 if (ad->ad_ops->ad_mkrf(ad_p) < 0) {
1395 return ad_error(ad, adflags);
1398 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1399 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1402 admode = ad_hf_mode(admode);
1404 /* retry with O_CREAT */
1405 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
1406 if ( ad->ad_md->adf_fd < 0 ) {
1407 return ad_error(ad, adflags);
1409 ad->ad_md->adf_flags = oflags;
1410 /* just created, set owner if admin owner (root) */
1412 ad_chown(ad_p, &st_dir);
1416 return ad_error(ad, adflags);
1419 ad->ad_md->adf_flags = hoflags;
1420 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
1421 /* for 0 length files, treat them as new. */
1422 ad->ad_md->adf_flags |= O_TRUNC;
1425 /* we have valid data in st_meta stat structure, reused it
1431 AD_SET(ad->ad_md->adf_off);
1433 ad->ad_md->adf_refcount = 1;
1434 adf_lock_init(ad->ad_md);
1435 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
1437 * This is a new adouble header file. Initialize the structure,
1438 * instead of reading it.
1440 if (new_rfork(path, ad, adflags) < 0) {
1442 /* the file is already deleted, perm, whatever, so return an error*/
1443 ad_close(ad, adflags);
1449 /* Read the adouble header in and parse it.*/
1450 if (ad->ad_ops->ad_header_read( ad , pst) < 0
1451 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0)
1455 ad_close( ad, adflags );
1461 /* ****************************************** */
1462 /* open the resource fork if SFM */
1464 if (ad->ad_flags != AD_VERSION1_SFM) {
1468 if ((adflags & ADFLAGS_DIR)) {
1469 /* no resource fork for directories / volumes XXX it's false! */
1473 /* untrue yet but ad_close will decremente it*/
1474 ad->ad_resource_fork.adf_refcount++;
1476 if (ad_reso_fileno(ad) != -1) { /* the file is already open */
1477 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1478 !(ad->ad_resource_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
1480 ad_close( ad, open_df | ADFLAGS_HF);
1487 ad_p = ad->ad_ops->ad_path( path, ADFLAGS_RF );
1490 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1492 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1496 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1497 ad->ad_resource_fork.adf_fd = open( ad_p, hoflags, admode );
1499 if (ad->ad_resource_fork.adf_fd < 0 ) {
1500 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1502 ad->ad_resource_fork.adf_fd =open( ad_p, hoflags, admode );
1506 if ( ad->ad_resource_fork.adf_fd < 0) {
1509 ad_close( ad, adflags );
1513 adf_lock_init(&ad->ad_resource_fork);
1514 AD_SET(ad->ad_resource_fork.adf_off);
1515 ad->ad_resource_fork.adf_flags = hoflags;
1516 if ((oflags & O_CREAT) && !st_invalid) {
1517 /* just created, set owner if admin (root) */
1518 ad_chown(ad_p, &st_dir);
1520 else if (!fstat(ad->ad_resource_fork.adf_fd, &st_meta)) {
1521 ad->ad_rlen = st_meta.st_size;
1527 * @brief open metadata, possibly as root
1529 * Return only metadata but try very hard ie at first try as user, then try as root.
1531 * @param name name of file/dir
1532 * @param flags ADFLAGS_DIR: name is a directory \n
1533 * ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
1534 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1536 * @param adp pointer to struct adouble
1538 * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
1539 * a adouble file depends on various other volume options, eg. ADVOL_CACHE
1541 int ad_metadata(const char *name, int flags, struct adouble *adp)
1545 int create = O_RDONLY;
1547 dir = flags & ADFLAGS_DIR;
1549 /* Check if we shall call ad_open with O_CREAT */
1550 if ( (adp->ad_options & ADVOL_CACHE)
1551 && ! (adp->ad_options & ADVOL_NOADOUBLE)
1552 && (flags & ADFLAGS_CREATE) ) {
1553 create = O_CREAT | O_RDWR;
1555 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1558 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1562 /* we are root open read only */
1563 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1565 if ( seteuid(uid) < 0) {
1566 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1572 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1574 we need to check if the file is open by another process.
1575 it's slow so we only do it if we have to:
1577 - we don't already have the answer!
1579 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1585 * @brief openat like wrapper for ad_metadata
1587 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1593 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1599 if (ad_metadata(name, flags, adp) < 0) {
1605 if (fchdir(cwdfd) != 0) {
1606 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1619 /* ----------------------------------- */
1620 static int new_rfork(const char *path, struct adouble *ad, int adflags)
1622 const struct entry *eid;
1626 ad->ad_magic = AD_MAGIC;
1627 ad->ad_version = ad->ad_flags & 0x0f0000;
1628 if (!ad->ad_version) {
1629 ad->ad_version = AD_VERSION;
1632 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
1633 memset(ad->ad_data, 0, sizeof(ad->ad_data));
1635 #if AD_VERSION == AD_VERSION2
1636 if (ad->ad_flags == AD_VERSION2)
1638 else if (ad->ad_flags == AD_VERSION2_OSX)
1639 eid = entry_order_osx;
1640 else if (ad->ad_flags == AD_VERSION1_SFM) {
1641 ad->ad_magic = SFM_MAGIC;
1642 eid = entry_order_sfm;
1649 ad->ad_eid[eid->id].ade_off = eid->offset;
1650 ad->ad_eid[eid->id].ade_len = eid->len;
1654 /* put something sane in the directory finderinfo */
1655 if ((adflags & ADFLAGS_DIR)) {
1656 /* set default view */
1657 ashort = htons(FINDERINFO_CLOSEDVIEW);
1658 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF,
1659 &ashort, sizeof(ashort));
1661 /* set default creator/type fields */
1662 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
1663 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
1666 /* make things invisible */
1667 if ((ad->ad_options & ADVOL_INVDOTS) && !(adflags & ADFLAGS_CREATE) &&
1668 (*path == '.') && strcmp(path, ".") && strcmp(path, ".."))
1670 ashort = htons(ATTRBIT_INVISIBLE);
1671 ad_setattr(ad, ashort);
1672 ashort = htons(FINDERINFO_INVISIBLE);
1673 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1676 if (ostat(path, &st, ad_get_syml_opt(ad)) < 0) {
1680 /* put something sane in the date fields */
1681 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
1682 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
1683 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
1684 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
1688 /* to do this with mmap, we need the hfs fs to understand how to mmap
1690 int ad_refresh(struct adouble *ad)
1693 if (ad_meta_fileno(ad) < 0)
1696 return ad->ad_ops->ad_header_read(ad, NULL);
1699 int ad_openat(int dirfd, /* dir fd openat like */
1710 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1716 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1722 if (fchdir(cwdfd) != 0) {
1723 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");