2 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
3 * Copyright (c) 1990,1991 Regents of The University of Michigan.
4 * Copyright (c) 2010 Frank Lahm
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
30 * Part of Netatalk's AppleDouble implementatation
31 * @note We don't use inlines because a good compiler should be
32 * able to optimize all the static funcs below.
33 * @sa include/atalk/adouble.h
38 #endif /* HAVE_CONFIG_H */
41 #include <sys/param.h>
45 #include <atalk/logger.h>
46 #include <atalk/adouble.h>
47 #include <atalk/util.h>
49 #include "ad_private.h"
52 #define MAX(a, b) ((a) < (b) ? (b) : (a))
58 # define AD_SET(a) a = 0
62 * AppleDouble entry default offsets.
63 * The layout looks like this:
65 * this is the v1 layout:
67 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
69 * we need to change it to look like this:
72 * field length (in bytes)
75 * FILEDATESI 16 replaces FILEI
79 * SHORTNAME 12 8.3 new
82 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
83 * and add in the new fields.
86 #define ADEDOFF_MAGIC (0)
87 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
88 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
89 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
91 /* initial lengths of some of the fields */
92 #define ADEDLEN_INIT 0
94 /* make sure we don't redefine ADEDOFF_FILEI */
97 #endif /* ADEDOFF_FILEI */
99 #define ADEDOFF_NAME_V1 (AD_HEADER_LEN + ADEID_NUM_V1*AD_ENTRY_LEN)
100 #define ADEDOFF_COMMENT_V1 (ADEDOFF_NAME_V1 + ADEDLEN_NAME)
101 #define ADEDOFF_FILEI (ADEDOFF_COMMENT_V1 + ADEDLEN_COMMENT)
102 #define ADEDOFF_FINDERI_V1 (ADEDOFF_FILEI + ADEDLEN_FILEI)
103 #define ADEDOFF_RFORK_V1 (ADEDOFF_FINDERI_V1 + ADEDLEN_FINDERI)
105 /* i stick things in a slightly different order than their eid order in
106 * case i ever want to separate RootInfo behaviour from the rest of the
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_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
117 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
118 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
119 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
120 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
122 #define ADEDOFF_FINDERI_OSX (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN)
123 #define ADEDOFF_RFORK_OSX (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI)
125 /* we keep local copies of a bunch of stuff so that we can initialize things
128 /* this is to prevent changing timezones from causing problems with
129 localtime volumes. the screw-up is 30 years. we use a delta of 5
131 #define TIMEWARP_DELTA 157680000
134 uint32_t id, offset, len;
137 /* --------------------------- */
138 static uid_t default_uid = -1;
140 static struct adouble_fops ad_adouble = {
143 &ad_rebuild_adouble_header,
149 static struct adouble_fops ad_adouble_ea = {
152 &ad_rebuild_adouble_header,
155 &ad_header_upgrade_ea,
158 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
159 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
160 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
161 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
162 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
163 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
164 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
165 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
166 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
167 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
168 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
169 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
170 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
171 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
175 /* Using Extended Attributes */
176 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
177 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
178 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
179 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
180 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
181 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
185 #define DISK_EID(ad, a) get_eid(ad, a)
187 static uint32_t get_eid(struct adouble *ad, u_int32_t eid)
192 return ADEID_PRIVDEV;
194 return ADEID_PRIVINO;
196 return ADEID_PRIVSYN;
203 /* ----------------------------------- */
204 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
206 const struct entry *eid;
210 ad->ad_magic = AD_MAGIC;
211 ad->ad_version = ad->ad_flags & 0x0f0000;
212 if (!ad->ad_version) {
213 ad->ad_version = AD_VERSION;
216 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
217 memset(ad->ad_data, 0, sizeof(ad->ad_data));
219 if (ad->ad_flags == AD_VERSION2)
221 else if (ad->ad_flags == AD_VERSION_EA)
222 eid = entry_order_ea;
228 ad->ad_eid[eid->id].ade_off = eid->offset;
229 ad->ad_eid[eid->id].ade_len = eid->len;
233 /* put something sane in the directory finderinfo */
234 if ((adflags & ADFLAGS_DIR)) {
235 /* set default view */
236 ashort = htons(FINDERINFO_CLOSEDVIEW);
237 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
239 /* set default creator/type fields */
240 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
241 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
244 /* make things invisible */
245 if ((ad->ad_options & ADVOL_INVDOTS) && (adflags & ADFLAGS_CREATE) && (*path == '.')) {
246 ashort = htons(ATTRBIT_INVISIBLE);
247 ad_setattr(ad, ashort);
248 ashort = htons(FINDERINFO_INVISIBLE);
249 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
252 if (lstat(path, &st) < 0)
255 /* put something sane in the date fields */
256 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
257 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
258 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
259 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
263 /* update a version 2 adouble resource fork with our private entries */
264 static int ad_update(struct adouble *ad, const char *path)
267 u_int16_t nentries = 0;
268 off_t off, shiftdata=0;
269 const struct entry *eid;
270 static off_t entry_len[ADEID_MAX];
271 static char databuf[ADEID_MAX][256], *buf;
275 /* check to see if we should convert this header. */
276 if (!path || ad->ad_flags != AD_VERSION2)
279 LOG(log_maxdebug, logtype_default, "ad_update: checking whether '%s' needs an upgrade.", path);
281 if (!(ad->ad_md->adf_flags & O_RDWR)) {
282 /* we were unable to open the file read write the last time */
286 if (ad->ad_eid[ADEID_RFORK].ade_off) {
287 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
290 memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof( nentries ));
291 nentries = ntohs( nentries );
293 if ( shiftdata == 0 && nentries == ADEID_NUM_V2)
296 memset(entry_len, 0, sizeof(entry_len));
297 memset(databuf, 0, sizeof(databuf));
299 /* bail if we can't get a lock */
300 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
303 fd = ad->ad_md->adf_fd;
305 if (fstat(fd, &st)) {
309 if (st.st_size > 0x7fffffff) {
310 LOG(log_debug, logtype_default, "ad_update: file '%s' too big for update.", path);
315 off = ad->ad_eid[ADEID_RFORK].ade_off;
316 if (off > st.st_size) {
317 LOG(log_error, logtype_default, "ad_update: invalid resource fork offset. (off: %u)", off);
322 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
323 LOG(log_error, logtype_default, "ad_update: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
328 if ((void *) (buf = (char *)
329 mmap(NULL, st.st_size + shiftdata,
330 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
335 /* last place for failure. */
336 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
337 munmap(buf, st.st_size + shiftdata);
341 /* move the RFORK. this assumes that the RFORK is at the end */
343 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
346 munmap(buf, st.st_size + shiftdata);
348 /* now, fix up our copy of the header */
349 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
351 /* save the header entries */
354 if( ad->ad_eid[eid->id].ade_off != 0) {
355 if ( eid->id > 2 && ad->ad_eid[eid->id].ade_len < 256)
356 memcpy( databuf[eid->id], ad->ad_data +ad->ad_eid[eid->id].ade_off, ad->ad_eid[eid->id].ade_len);
357 entry_len[eid->id] = ad->ad_eid[eid->id].ade_len;
362 memset(ad->ad_data + AD_HEADER_LEN, 0, AD_DATASZ - AD_HEADER_LEN);
364 /* copy the saved entries to the new header */
367 if ( eid->id > 2 && entry_len[eid->id] > 0) {
368 memcpy(ad->ad_data+eid->offset, databuf[eid->id], entry_len[eid->id]);
370 ad->ad_eid[eid->id].ade_off = eid->offset;
371 ad->ad_eid[eid->id].ade_len = entry_len[eid->id];
375 /* rebuild the header and cleanup */
376 LOG(log_debug, logtype_default, "updated AD2 header %s", path);
381 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
386 /* ------------------------------------------
387 FIXME work only if < 2GB
389 static int ad_convert(struct adouble *ad, const char *path)
396 /* use resource fork offset from file */
405 if (!(ad->ad_md->adf_flags & ( O_RDWR))) {
406 /* we were unable to open the file read write the last time */
410 /* check to see if we should convert this header. */
411 toV2 = ad->ad_version == AD_VERSION1 && ad->ad_flags == AD_VERSION2;
412 toV1 = ad->ad_version == AD_VERSION2 && ad->ad_flags == AD_VERSION1;
417 /* convert from v1 to v2. what does this mean?
418 * 1) change FILEI into FILEDATESI
419 * 2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
420 * 3) move FILEI attributes into AFPFILEI
421 * 4) initialize ACCESS field of FILEDATESI.
422 * 5) move the resource fork
425 /* bail if we can't get a lock */
426 if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
429 /* we reuse fd from the resource fork */
430 fd = ad->ad_md->adf_fd;
432 if (ad->ad_eid[ADEID_RFORK].ade_off) {
433 shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
436 shiftdata = ADEDOFF_RFORK_V2 -ADEDOFF_RFORK_V1; /* 136 */
439 if (fstat(fd, &st)) {
443 if (st.st_size > 0x7fffffff -shiftdata) {
444 LOG(log_debug, logtype_default, "ad_v1tov2: file too big.");
449 off = ad->ad_eid[ADEID_RFORK].ade_off;
451 if (off > st.st_size) {
452 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off);
457 if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
458 LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len);
463 if ((void *) (buf = (char *)
464 mmap(NULL, st.st_size + shiftdata,
465 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
470 /* last place for failure. */
472 if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
476 /* move the RFORK. this assumes that the RFORK is at the end */
478 memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
481 munmap(buf, st.st_size + shiftdata);
483 /* now, fix up our copy of the header */
484 memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
486 /* replace FILEI with FILEDATESI */
487 ad_getattr(ad, &attr);
488 ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
489 ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
490 ad->ad_eid[ADEID_FILEI].ade_off = 0;
491 ad->ad_eid[ADEID_FILEI].ade_len = 0;
493 /* add in the new entries */
494 ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
495 ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
496 ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
497 ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
498 ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
499 ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
500 ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
501 ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
503 ad->ad_eid[ADEID_PRIVDEV].ade_off = ADEDOFF_PRIVDEV;
504 ad->ad_eid[ADEID_PRIVDEV].ade_len = ADEDLEN_INIT;
505 ad->ad_eid[ADEID_PRIVINO].ade_off = ADEDOFF_PRIVINO;
506 ad->ad_eid[ADEID_PRIVINO].ade_len = ADEDLEN_INIT;
507 ad->ad_eid[ADEID_PRIVSYN].ade_off = ADEDOFF_PRIVSYN;
508 ad->ad_eid[ADEID_PRIVSYN].ade_len = ADEDLEN_INIT;
509 ad->ad_eid[ADEID_PRIVID].ade_off = ADEDOFF_PRIVID;
510 ad->ad_eid[ADEID_PRIVID].ade_len = ADEDLEN_INIT;
512 /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
513 ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
514 ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
515 ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
516 ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
518 /* switch to dest version */
519 ad->ad_version = (toV2)?AD_VERSION2:AD_VERSION1;
521 /* move our data buffer to make space for the new entries. */
522 memmove(ad->ad_data + ADEDOFF_NAME_V2, ad->ad_data + ADEDOFF_NAME_V1,
523 ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
525 /* now, fill in the space with appropriate stuff. we're
526 operating as a v2 file now. */
527 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
528 memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
529 memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
530 ad_setattr(ad, attr);
531 memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
532 memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
534 /* rebuild the header and cleanup */
539 ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
544 /* -------------------------------------
547 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
549 uint32_t eid, len, off;
552 /* now, read in the entry bits */
553 for (; nentries > 0; nentries-- ) {
554 memcpy(&eid, buf, sizeof( eid ));
555 eid = DISK_EID(ad, ntohl( eid ));
556 buf += sizeof( eid );
557 memcpy(&off, buf, sizeof( off ));
559 buf += sizeof( off );
560 memcpy(&len, buf, sizeof( len ));
562 buf += sizeof( len );
564 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
565 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
566 ad->ad_eid[ eid ].ade_off = off;
567 ad->ad_eid[ eid ].ade_len = len;
568 } else if (!warning) {
570 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
575 /* this reads enough of the header so that we can figure out all of
576 * the entry lengths and offsets. once that's done, we just read/mmap
577 * the rest of the header in.
579 * NOTE: we're assuming that the resource fork is kept at the end of
580 * the file. also, mmapping won't work for the hfs fs until it
581 * understands how to mmap header files. */
582 static int ad_header_read(struct adouble *ad, struct stat *hst)
584 char *buf = ad->ad_data;
588 static int warning = 0;
591 /* read the header */
592 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
595 if (header_len < AD_HEADER_LEN) {
600 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
601 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
603 /* tag broken v1 headers. just assume they're all right.
604 * we detect two cases: null magic/version
605 * byte swapped magic/version
606 * XXX: in the future, you'll need the v1compat flag. */
607 if (!ad->ad_magic && !ad->ad_version) {
609 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
612 ad->ad_magic = AD_MAGIC;
613 ad->ad_version = AD_VERSION1;
615 } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
617 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
622 ad->ad_magic = ntohl( ad->ad_magic );
623 ad->ad_version = ntohl( ad->ad_version );
626 if ((ad->ad_magic != AD_MAGIC) ||
627 ((ad->ad_version != AD_VERSION1) && (ad->ad_version != AD_VERSION2))) {
628 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
633 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
634 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
635 nentries = ntohs( nentries );
637 /* read in all the entry headers. if we have more than the
638 * maximum, just hope that the rfork is specified early on. */
639 len = nentries*AD_ENTRY_LEN;
641 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
642 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
644 buf += AD_HEADER_LEN;
645 if (len > header_len - AD_HEADER_LEN) {
646 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
651 /* figure out all of the entry offsets and lengths. if we aren't
652 * able to read a resource fork entry, bail. */
653 nentries = len / AD_ENTRY_LEN;
654 parse_entries(ad, buf, nentries);
655 if (!ad_getentryoff(ad, ADEID_RFORK)
656 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
658 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
663 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
664 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
671 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
672 return 1; /* fail silently */
675 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
680 static int ad_header_read_ea(struct adouble *ad, struct stat *hst)
685 static int warning = 0;
688 /* read the header */
689 if ((header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 0) {
690 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
694 if (header_len < AD_HEADER_LEN) {
695 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
700 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
701 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
703 ad->ad_magic = ntohl( ad->ad_magic );
704 ad->ad_version = ntohl( ad->ad_version );
706 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION_EA)) {
707 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
712 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
713 nentries = ntohs( nentries );
715 /* Protect against bogus nentries */
716 len = nentries * AD_ENTRY_LEN;
717 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
718 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
719 if (len > header_len - AD_HEADER_LEN) {
720 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
724 nentries = len / AD_ENTRY_LEN;
726 /* Now parse entries */
727 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
730 static int ad_mkrf(char *path)
734 * Probably .AppleDouble doesn't exist, try to mkdir it.
736 if (NULL == ( slash = strrchr( path, '/' )) ) {
741 if ( ad_mkdir( path, 0777 ) < 0 ) {
750 if we are root change path user/ group
751 It can be a native function for BSD cf. FAQ.Q10
752 path: pathname to chown
753 stbuf: parent directory inode
755 use fstat and fchown or lchown with linux?
757 #define EMULATE_SUIDDIR
759 static int ad_chown(const char *path, struct stat *stbuf)
762 #ifdef EMULATE_SUIDDIR
765 if (default_uid != (uid_t)-1) {
766 /* we are root (admin) */
767 id = (default_uid)?default_uid:stbuf->st_uid;
768 ret = lchown( path, id, stbuf->st_gid );
775 return access right and inode of path parent directory
777 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
782 if (ad_stat(path, stbuf) != 0) {
786 *mode &= stbuf->st_mode;
790 /* ----------------- */
791 static int ad_error(struct adouble *ad, int adflags)
794 if ((adflags & ADFLAGS_NOHF)) {
795 /* FIXME double check : set header offset ?*/
798 if ((adflags & ADFLAGS_DF)) {
799 ad_close( ad, ADFLAGS_DF );
805 /* --------------------------- */
806 static int ad_check_size(struct adouble *ad _U_, struct stat *st)
808 if (st->st_size > 0 && st->st_size < AD_DATASZ1)
813 /* --------------------------- */
814 static int ad_check_size_sfm(struct adouble *ad _U_, struct stat *st)
816 if (st->st_size > 0 && st->st_size < AD_SFM_LEN)
821 /* --------------------------- */
822 static int ad_header_upgrade(struct adouble *ad, char *name)
825 if ( (ret = ad_convert(ad, name)) < 0 || (ret = ad_update(ad, name) < 0)) {
831 /* --------------------------- */
832 static int ad_header_upgrade_none(struct adouble *ad _U_, char *name _U_)
837 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
841 struct stat *pst = NULL;
846 if (ad_data_fileno(ad) == -1) {
847 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
849 if ((oflags & O_CREAT)) {
850 st_invalid = ad_mode_st(path, &admode, &st_dir);
851 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
856 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
858 if (ad->ad_data_fork.adf_fd == -1) {
859 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
861 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
863 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
866 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
867 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
869 free(ad->ad_data_fork.adf_syml);
872 ad->ad_data_fork.adf_syml[lsz] = 0;
873 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
877 if ( ad->ad_data_fork.adf_fd == -1 )
880 AD_SET(ad->ad_data_fork.adf_off);
881 ad->ad_data_fork.adf_flags = hoflags;
883 /* just created, set owner if admin (root) */
884 ad_chown(path, &st_dir);
886 adf_lock_init(&ad->ad_data_fork);
888 /* the file is already open... but */
889 if ((oflags & ( O_RDWR | O_WRONLY))
890 /* we want write access */
891 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
892 /* and it was denied the first time */
897 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
898 * already open. Should we check for it? ie
900 * O_TRUNC --> truncate the fork.
901 * idem for ressource fork.
904 ad->ad_data_fork.adf_refcount++;
909 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
913 struct stat *pst = NULL;
918 ad_p = ad->ad_ops->ad_path( path, adflags );
920 hoflags = oflags & ~(O_CREAT | O_EXCL);
921 if (!(adflags & ADFLAGS_RDONLY)) {
922 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
924 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
925 if (ad->ad_md->adf_fd < 0 ) {
926 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
927 hoflags = oflags & ~(O_CREAT | O_EXCL);
928 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
932 if ( ad->ad_md->adf_fd < 0 ) {
933 if (errno == ENOENT && (oflags & O_CREAT) ) {
935 * We're expecting to create a new adouble header file here
936 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
938 LOG(log_debug, logtype_default, "ad_open(\"%s/%s\"): creating adouble file",
942 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
943 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
946 admode = ad_hf_mode(admode);
947 if ((errno == ENOENT)) {
948 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
949 return ad_error(ad, adflags);
952 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
953 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
956 admode = ad_hf_mode(admode);
958 /* retry with O_CREAT */
959 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
960 if ( ad->ad_md->adf_fd < 0 ) {
961 return ad_error(ad, adflags);
963 ad->ad_md->adf_flags = oflags;
964 /* just created, set owner if admin owner (root) */
966 ad_chown(ad_p, &st_dir);
969 return ad_error(ad, adflags);
972 ad->ad_md->adf_flags = hoflags;
973 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
974 /* for 0 length files, treat them as new. */
975 ad->ad_md->adf_flags |= O_TRUNC;
977 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
981 AD_SET(ad->ad_md->adf_off);
983 ad->ad_md->adf_refcount = 1;
984 adf_lock_init(ad->ad_md);
985 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
986 /* This is a new adouble header file, create it */
987 if (new_ad_header(path, ad, adflags) < 0) {
989 /* the file is already deleted, perm, whatever, so return an error */
990 ad_close(ad, adflags);
996 /* Read the adouble header in and parse it.*/
997 if (ad->ad_ops->ad_header_read( ad , pst) < 0
998 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
1000 ad_close( ad, adflags );
1009 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1011 ad->ad_md->adf_fd = -3;
1013 /* Read the adouble header in and parse it.*/
1014 if (ad->ad_ops->ad_header_read(ad, path) != 0) {
1015 /* It doesnt exist, EPERM or another error */
1016 if (errno != ENOENT)
1020 if (new_ad_header(path, ad, adflags) < 0) {
1022 /* the file is already deleted, perm, whatever, so return an error */
1023 ad_close(ad, adflags);
1034 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1038 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
1039 if ((oflags & ( O_RDWR | O_WRONLY))
1040 && !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
1041 if ((adflags & ADFLAGS_HF) && ad->ad_data_fork.adf_refcount)
1042 /* We just have openend the df, so we have to close it now */
1043 ad_close(ad, ADFLAGS_DF);
1048 /* it's not new anymore */
1049 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
1050 ad->ad_md->adf_refcount++;
1054 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1057 switch (ad->ad_version) {
1059 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
1062 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
1072 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1077 /***********************************************************************************
1079 ********************************************************************************* */
1082 * Put the .AppleDouble where it needs to be:
1084 * / a/.AppleDouble/b
1086 * \ b/.AppleDouble/.Parent
1088 * FIXME: should do something for pathname > MAXPATHLEN
1090 char *ad_path( const char *path, int adflags)
1092 static char pathbuf[ MAXPATHLEN + 1];
1096 if ( adflags & ADFLAGS_DIR ) {
1097 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1099 if ( l && l < MAXPATHLEN) {
1102 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1104 if (NULL != ( slash = strrchr( path, '/' )) ) {
1107 /* XXX we must return NULL here and test in the caller */
1110 memcpy( pathbuf, path, l);
1115 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1116 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1122 /* -------------------------
1123 * Support inherited protection modes for AppleDouble files. The supplied
1124 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1125 * and that value is returned.
1128 #define DEFMASK 07700 /* be conservative */
1130 char *ad_dir(const char *path)
1132 static char modebuf[ MAXPATHLEN + 1];
1135 * For a path with directories in it, remove the final component
1136 * (path or subdirectory name) to get the name we want to stat.
1137 * For a path which is just a filename, use "." instead.
1139 slash = strrchr( path, '/' );
1144 if (len >= MAXPATHLEN) {
1145 errno = ENAMETOOLONG;
1146 return NULL; /* can't do it */
1148 memcpy( modebuf, path, len );
1149 modebuf[len] = '\0';
1150 /* is last char a '/' ? */
1151 if (slash[1] == 0) {
1152 slash = modebuf+ len;
1154 while (modebuf < slash && slash[-1] == '/') {
1157 if (modebuf == slash) {
1161 while (modebuf < slash && *slash != '/') {
1164 if (modebuf == slash) {
1167 *slash = '\0'; /* remove pathname component */
1172 modebuf[0] = '.'; /* use current directory */
1177 int ad_setfuid(const uid_t id)
1183 /* ---------------- */
1184 uid_t ad_getfuid(void)
1190 stat path parent directory
1192 int ad_stat(const char *path, struct stat *stbuf)
1199 return lstat( p, stbuf );
1203 return access right of path parent directory
1205 int ad_mode( const char *path, int mode)
1208 ad_mode_st(path, &mode, &stbuf);
1213 * Use mkdir() with mode bits taken from ad_mode().
1215 int ad_mkdir( const char *path, int mode)
1221 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1222 path, mode, getcwdpath());
1224 st_invalid = ad_mode_st(path, &mode, &stbuf);
1225 ret = mkdir( path, mode );
1226 if (ret || st_invalid)
1228 ad_chown(path, &stbuf);
1233 void ad_init(struct adouble *ad, int flags, int options)
1236 ad->ad_flags = flags;
1237 if (flags == AD_VERSION2) {
1238 ad->ad_ops = &ad_adouble;
1239 ad->ad_md = &ad->ad_resource_fork;
1241 else if (flags == AD_VERSION_EA) {
1242 ad->ad_ops = &ad_adouble_ea;
1243 ad->ad_md = &ad->ad_metadata_fork;
1245 LOG(log_error, logtype_default, "ad_init: unknown AD version");
1250 ad->ad_options = options;
1252 ad_data_fileno(ad) = -1;
1253 ad_reso_fileno(ad) = -1;
1254 ad_meta_fileno(ad) = -1;
1256 /* following can be read even if there's no meda data. */
1257 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1262 * Open data-, metadata(header)- or ressource fork
1264 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1267 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1270 * @param path Path to file or directory
1272 * @param adflags ADFLAGS_DF: open data fork \n
1273 * ADFLAGS_RF: open ressource fork \n
1274 * ADFLAGS_HF: open header (metadata) file \n
1275 * ADFLAGS_CREATE: indicate creation \n
1276 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1277 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1278 * ADFLAGS_NOADOUBLE: dont create adouble files if not necessary \n
1279 * ADFLAGS_RDONLY: open read only \n
1280 * ADFLAGS_OPENFORKS: check for open forks from other processes
1282 * @param oflags flags passed through to open syscall: \n
1283 * O_RDONLY: *** FIXME *** \n
1284 * O_RDWR: *** FIXME *** \n
1285 * O_CREAT: create fork \n
1286 * O_EXCL: fail if exists with O_CREAT
1288 * @param mode passed to open with O_CREAT
1289 * @param ad pointer to struct adouble
1291 * @returns 0 on success
1293 * @note It's not possible to open the header file O_RDONLY -- the read
1294 * will fail and return an error. this refcounts things now.\n
1295 * metadata(ressource)-fork only gets created with O_CREAT.
1297 int ad_open(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1300 struct stat st_meta;
1301 struct stat *pst = NULL;
1303 int hoflags, admode;
1304 int st_invalid = -1;
1307 if (ad->ad_inited != AD_INITED) {
1308 ad->ad_inited = AD_INITED;
1309 ad->ad_refcount = 1;
1310 ad->ad_open_forks = 0;
1311 ad->ad_adflags = adflags;
1312 ad->ad_resource_fork.adf_refcount = 0;
1313 ad->ad_data_fork.adf_refcount = 0;
1314 ad->ad_data_fork.adf_syml = 0;
1316 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1317 /* XXX not true if we have a meta data fork ? */
1318 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1319 ad->ad_open_forks |= ATTRBIT_ROPEN;
1322 if ((adflags & ADFLAGS_DF)) {
1323 if (ad_open_df(path, adflags, oflags, mode, ad) != 0)
1327 if ((adflags & ADFLAGS_HF)) {
1328 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0)
1332 if ((adflags & ADFLAGS_RF)) {
1333 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0)
1341 * @brief open metadata, possibly as root
1343 * Return only metadata but try very hard ie at first try as user, then try as root.
1345 * @param name name of file/dir
1346 * @param flags ADFLAGS_DIR: name is a directory \n
1347 * ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
1348 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1350 * @param adp pointer to struct adouble
1352 * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
1353 * a adouble file depends on various other volume options, eg. ADVOL_CACHE
1355 int ad_metadata(const char *name, int flags, struct adouble *adp)
1359 int create = O_RDONLY;
1361 dir = flags & ADFLAGS_DIR;
1363 /* Check if we shall call ad_open with O_CREAT */
1364 if ( (adp->ad_options & ADVOL_CACHE)
1365 && ! (adp->ad_options & ADVOL_NOADOUBLE)
1366 && (flags & ADFLAGS_CREATE) ) {
1367 create = O_CREAT | O_RDWR;
1369 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1372 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1376 /* we are root open read only */
1377 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1379 if ( seteuid(uid) < 0) {
1380 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1386 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1388 we need to check if the file is open by another process.
1389 it's slow so we only do it if we have to:
1391 - we don't already have the answer!
1393 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1399 * @brief openat like wrapper for ad_metadata
1401 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1407 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1413 if (ad_metadata(name, flags, adp) < 0) {
1419 if (fchdir(cwdfd) != 0) {
1420 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1433 int ad_refresh(struct adouble *ad)
1436 if (ad_meta_fileno(ad) < 0)
1439 return ad->ad_ops->ad_header_read(ad, NULL);
1442 int ad_openat(int dirfd, /* dir fd openat like */
1453 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1459 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1465 if (fchdir(cwdfd) != 0) {
1466 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");