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,
469 u_int32_t eid, len, off;
472 /* now, read in the entry bits */
473 for (; nentries > 0; nentries-- ) {
474 memcpy(&eid, buf, sizeof( eid ));
475 eid = DISK_EID(ad, ntohl( eid ));
476 buf += sizeof( eid );
477 memcpy(&off, buf, sizeof( off ));
479 buf += sizeof( off );
480 memcpy(&len, buf, sizeof( len ));
482 buf += sizeof( len );
484 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
485 (off +len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
486 ad->ad_eid[ eid ].ade_off = off;
487 ad->ad_eid[ eid ].ade_len = len;
488 } else if (!warning) {
490 LOG(log_debug, logtype_default, "ad_refresh: nentries %hd eid %d",
496 /***********************************************************************
497 * adouble v2 scheme funcs
498 ***********************************************************************/
500 /* this reads enough of the header so that we can figure out all of
501 * the entry lengths and offsets. once that's done, we just read/mmap
502 * the rest of the header in.
504 * NOTE: we're assuming that the resource fork is kept at the end of
505 * the file. also, mmapping won't work for the hfs fs until it
506 * understands how to mmap header files. */
507 static int ad_header_read(struct adouble *ad, struct stat *hst)
509 char *buf = ad->ad_data;
513 static int warning = 0;
516 /* read the header */
517 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
520 if (header_len < AD_HEADER_LEN) {
525 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
526 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
528 /* tag broken v1 headers. just assume they're all right.
529 * we detect two cases: null magic/version
530 * byte swapped magic/version
531 * XXX: in the future, you'll need the v1compat flag.
532 * (ad->ad_flags & ADFLAGS_V1COMPAT) */
533 if (!ad->ad_magic && !ad->ad_version) {
535 LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
538 ad->ad_magic = AD_MAGIC;
539 ad->ad_version = AD_VERSION1;
541 } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
543 LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
548 ad->ad_magic = ntohl( ad->ad_magic );
549 ad->ad_version = ntohl( ad->ad_version );
552 if ((ad->ad_magic != AD_MAGIC) ||
553 ((ad->ad_version != AD_VERSION1) && (ad->ad_version != AD_VERSION2))) {
554 LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
559 memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
560 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
561 nentries = ntohs( nentries );
563 /* read in all the entry headers. if we have more than the
564 * maximum, just hope that the rfork is specified early on. */
565 len = nentries*AD_ENTRY_LEN;
567 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
568 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
570 buf += AD_HEADER_LEN;
571 if (len > header_len - AD_HEADER_LEN) {
572 LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
577 /* figure out all of the entry offsets and lengths. if we aren't
578 * able to read a resource fork entry, bail. */
579 nentries = len / AD_ENTRY_LEN;
580 parse_entries(ad, buf, nentries);
581 if (!ad_getentryoff(ad, ADEID_RFORK)
582 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
584 LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset.");
589 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
590 LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
597 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
598 return 1; /* fail silently */
601 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
603 /* fix up broken dates */
604 if (ad->ad_version == AD_VERSION1) {
607 /* check to see if the ad date is wrong. just see if we have
608 * a modification date in the future. */
609 if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
610 (aint > TIMEWARP_DELTA + hst->st_mtime)) {
611 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
612 ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
613 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
614 ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
615 ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
623 * Put the .AppleDouble where it needs to be:
627 * \ b/.AppleDouble/.Parent
629 * FIXME: should do something for pathname > MAXPATHLEN
631 char *ad_path( const char *path, int adflags)
633 static char pathbuf[ MAXPATHLEN + 1];
637 if ( adflags & ADFLAGS_DIR ) {
638 l = strlcpy( pathbuf, path, sizeof(pathbuf));
640 if ( l && l < MAXPATHLEN) {
643 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
645 if (NULL != ( slash = strrchr( path, '/' )) ) {
648 /* XXX we must return NULL here and test in the caller */
651 memcpy( pathbuf, path, l);
656 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
657 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
663 static int ad_mkrf(char *path)
667 * Probably .AppleDouble doesn't exist, try to mkdir it.
669 if (NULL == ( slash = strrchr( path, '/' )) ) {
674 if ( ad_mkdir( path, 0777 ) < 0 ) {
682 /***********************************************************************
683 * OS X adouble scheme funcs
684 ***********************************************************************/
687 * Put the resource fork where it needs to be: ._name
689 char *ad_path_osx(const char *path, int adflags _U_)
691 static char pathbuf[ MAXPATHLEN + 1];
692 char c, *slash, buf[MAXPATHLEN + 1];
694 if (!strcmp(path,".")) {
696 getcwd(buf, MAXPATHLEN);
699 strlcpy(buf, path, MAXPATHLEN +1);
701 if (NULL != ( slash = strrchr( buf, '/' )) ) {
704 strlcpy( pathbuf, buf, MAXPATHLEN +1);
710 strlcat( pathbuf, "._", MAXPATHLEN +1);
711 strlcat( pathbuf, slash, MAXPATHLEN +1);
715 /* -------------------- */
716 static int ad_mkrf_osx(char *path _U_)
721 /* -------------------------
722 * Support inherited protection modes for AppleDouble files. The supplied
723 * mode is ANDed with the parent directory's mask value in lieu of "umask",
724 * and that value is returned.
727 #define DEFMASK 07700 /* be conservative */
729 char *ad_dir(const char *path)
731 static char modebuf[ MAXPATHLEN + 1];
734 * For a path with directories in it, remove the final component
735 * (path or subdirectory name) to get the name we want to stat.
736 * For a path which is just a filename, use "." instead.
738 slash = strrchr( path, '/' );
743 if (len >= MAXPATHLEN) {
744 errno = ENAMETOOLONG;
745 return NULL; /* can't do it */
747 memcpy( modebuf, path, len );
749 /* is last char a '/' ? */
751 slash = modebuf+ len;
753 while (modebuf < slash && slash[-1] == '/') {
756 if (modebuf == slash) {
760 while (modebuf < slash && *slash != '/') {
763 if (modebuf == slash) {
766 *slash = '\0'; /* remove pathname component */
771 modebuf[0] = '.'; /* use current directory */
776 /* ---------------- */
777 static uid_t default_uid = -1;
779 int ad_setfuid(const uid_t id)
785 /* ---------------- */
786 uid_t ad_getfuid(void)
792 return inode of path parent directory
794 int ad_stat(const char *path, struct stat *stbuf)
803 return lstat( p, stbuf );
807 if we are root change path user/ group
808 It can be a native function for BSD cf. FAQ.Q10
809 path: pathname to chown
810 stbuf: parent directory inode
812 use fstat and fchown or lchown with linux?
814 #define EMULATE_SUIDDIR
816 static int ad_chown(const char *path, struct stat *stbuf)
819 #ifdef EMULATE_SUIDDIR
822 if (default_uid != (uid_t)-1) {
823 /* we are root (admin) */
824 id = (default_uid)?default_uid:stbuf->st_uid;
825 ret = lchown( path, id, stbuf->st_gid );
832 return access right and inode of path parent directory
834 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
839 if (ad_stat(path, stbuf) != 0) {
843 *mode &= stbuf->st_mode;
848 return access right of path parent directory
851 ad_mode( const char *path, int mode)
854 ad_mode_st(path, &mode, &stbuf);
859 * Use mkdir() with mode bits taken from ad_mode().
862 ad_mkdir( const char *path, int mode)
868 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
869 path, mode, getcwdpath());
871 st_invalid = ad_mode_st(path, &mode, &stbuf);
872 ret = mkdir( path, mode );
873 if (ret || st_invalid)
875 ad_chown(path, &stbuf);
880 /* ----------------- */
881 static int ad_error(struct adouble *ad, int adflags)
884 if ((adflags & ADFLAGS_NOHF)) {
885 /* FIXME double check : set header offset ?*/
888 if ((adflags & ADFLAGS_DF)) {
889 ad_close( ad, ADFLAGS_DF );
895 static int new_rfork(const char *path, struct adouble *ad, int adflags);
900 #define AD_SET(a) a = 0
903 /* --------------------------- */
904 static int ad_check_size(struct adouble *ad _U_, struct stat *st)
906 if (st->st_size > 0 && st->st_size < AD_DATASZ1)
911 /* --------------------------- */
912 static int ad_check_size_sfm(struct adouble *ad _U_, struct stat *st)
914 if (st->st_size > 0 && st->st_size < AD_SFM_LEN)
919 /* --------------------------- */
920 static int ad_header_upgrade(struct adouble *ad, char *name)
923 if ( (ret = ad_convert(ad, name)) < 0 || (ret = ad_update(ad, name) < 0)) {
929 /* --------------------------- */
930 static int ad_header_upgrade_none(struct adouble *ad _U_, char *name _U_)
935 /* --------------------------- */
936 static struct adouble_fops ad_osx = {
939 &ad_rebuild_adouble_header,
945 static struct adouble_fops ad_adouble = {
948 &ad_rebuild_adouble_header,
954 static struct adouble_fops ad_adouble_ea = {
957 &ad_rebuild_adouble_header,
963 void ad_init(struct adouble *ad, int flags, int options)
966 ad->ad_flags = flags;
967 if (flags == AD_VERSION2_OSX) {
968 ad->ad_ops = &ad_osx;
969 ad->ad_md = &ad->ad_resource_fork;
971 else if (flags == AD_VERSION2) {
972 ad->ad_ops = &ad_adouble;
973 ad->ad_md = &ad->ad_resource_fork;
976 ad->ad_ops = &ad_adouble_ea;
977 ad->ad_md = &ad->ad_metadata_fork;
979 ad->ad_options = options;
981 ad_data_fileno(ad) = -1;
982 ad_reso_fileno(ad) = -1;
983 ad_meta_fileno(ad) = -1;
984 /* following can be read even if there's no meda data. */
985 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
990 * Open data-, metadata(header)- or ressource fork
992 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
995 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
998 * @param path Path to file or directory
999 * @param adflags ADFLAGS_DF: open data file/fork\n
1000 * ADFLAGS_HF: open header (metadata) file\n
1001 * ADFLAGS_RF: open ressource fork *** FIXME: not used ?! *** \n
1002 * ADFLAGS_CREATE: indicate creation\n
1003 * ADFLAGS_NOHF: it's not an error if header file couldn't be created\n
1004 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags\n
1005 * ADFLAGS_NOADOUBLE: dont create adouble files if not necessary\n
1006 * ADFLAGS_RDONLY: open read only\n
1007 * ADFLAGS_OPENFORKS: check for open forks from other processes\n
1008 * ADFLAGS_MD: alias for ADFLAGS_HF\n
1009 * ADFLAGS_V1COMPAT: obsolete
1010 * @param oflags flags passed through to open syscall: \n
1011 * O_RDONLY: *** FIXME *** \n
1012 * O_RDWR: *** FIXME *** \n
1013 * O_CREAT: create fork\n
1014 * O_EXCL: fail if exists with O_CREAT
1015 * @param mode passed to open with O_CREAT
1016 * @param ad pointer to struct adouble
1018 * @returns 0 on success
1020 * @note It's not possible to open the header file O_RDONLY -- the read
1021 * will fail and return an error. this refcounts things now.\n
1022 * metadata(ressource)-fork only gets created with O_CREAT.
1024 int ad_open( const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1027 struct stat st_meta;
1028 struct stat *pst = NULL;
1030 int hoflags, admode;
1031 int st_invalid = -1;
1034 if (ad->ad_inited != AD_INITED) {
1035 ad->ad_inited = AD_INITED;
1036 ad->ad_refcount = 1;
1037 ad->ad_open_forks = 0;
1038 ad->ad_adflags = adflags;
1039 ad->ad_resource_fork.adf_refcount = 0;
1040 ad->ad_data_fork.adf_refcount = 0;
1041 ad->ad_data_fork.adf_syml=0;
1044 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1045 /* XXX not true if we have a meta data fork ? */
1046 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1047 ad->ad_open_forks |= ATTRBIT_ROPEN;
1050 if ((adflags & ADFLAGS_DF)) {
1051 if (ad_data_fileno(ad) == -1) {
1052 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1054 if ((oflags & O_CREAT)) {
1055 st_invalid = ad_mode_st(path, &admode, &st_dir);
1056 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1061 ad->ad_data_fork.adf_fd =open( path, hoflags | O_NOFOLLOW, admode );
1063 if (ad->ad_data_fork.adf_fd == -1) {
1064 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1066 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
1068 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
1071 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
1072 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
1074 free(ad->ad_data_fork.adf_syml);
1077 ad->ad_data_fork.adf_syml[lsz] = 0;
1078 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
1082 if ( ad->ad_data_fork.adf_fd == -1 )
1085 AD_SET(ad->ad_data_fork.adf_off);
1086 ad->ad_data_fork.adf_flags = hoflags;
1088 /* just created, set owner if admin (root) */
1089 ad_chown(path, &st_dir);
1091 adf_lock_init(&ad->ad_data_fork);
1094 /* the file is already open... but */
1095 if ((oflags & ( O_RDWR | O_WRONLY)) && /* we want write access */
1096 !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
1102 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
1103 * already open. Should we check for it? ie
1105 * O_TRUNC --> truncate the fork.
1106 * idem for ressource fork.
1109 open_df = ADFLAGS_DF;
1110 ad->ad_data_fork.adf_refcount++;
1113 if (!(adflags & ADFLAGS_HF))
1116 /* ****************************************** */
1118 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
1119 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1120 !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
1122 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
1123 ad_close( ad, open_df );
1129 /* it's not new anymore */
1130 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
1131 ad->ad_md->adf_refcount++;
1135 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1137 ad_p = ad->ad_ops->ad_path( path, adflags );
1139 hoflags = oflags & ~(O_CREAT | O_EXCL);
1140 if (!(adflags & ADFLAGS_RDONLY)) {
1141 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1143 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
1144 if (ad->ad_md->adf_fd < 0 ) {
1145 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1146 hoflags = oflags & ~(O_CREAT | O_EXCL);
1147 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
1151 if ( ad->ad_md->adf_fd < 0 ) {
1152 if (errno == ENOENT && (oflags & O_CREAT) ) {
1154 * We're expecting to create a new adouble header file,
1156 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
1158 LOG(log_debug, logtype_default, "ad_open(\"%s\"): {cwd: \"%s\"} creating adouble file",
1159 ad_p, getcwdpath());
1162 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1163 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1166 admode = ad_hf_mode(admode);
1167 if ((errno == ENOENT) && (ad->ad_flags != AD_VERSION2_OSX)) {
1168 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
1169 return ad_error(ad, adflags);
1172 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1173 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1176 admode = ad_hf_mode(admode);
1178 /* retry with O_CREAT */
1179 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
1180 if ( ad->ad_md->adf_fd < 0 ) {
1181 return ad_error(ad, adflags);
1183 ad->ad_md->adf_flags = oflags;
1184 /* just created, set owner if admin owner (root) */
1186 ad_chown(ad_p, &st_dir);
1190 return ad_error(ad, adflags);
1193 ad->ad_md->adf_flags = hoflags;
1194 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
1195 /* for 0 length files, treat them as new. */
1196 ad->ad_md->adf_flags |= O_TRUNC;
1199 /* we have valid data in st_meta stat structure, reused it
1205 AD_SET(ad->ad_md->adf_off);
1207 ad->ad_md->adf_refcount = 1;
1208 adf_lock_init(ad->ad_md);
1209 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
1211 * This is a new adouble header file. Initialize the structure,
1212 * instead of reading it.
1214 if (new_rfork(path, ad, adflags) < 0) {
1216 /* the file is already deleted, perm, whatever, so return an error*/
1217 ad_close(ad, adflags);
1223 /* Read the adouble header in and parse it.*/
1224 if (ad->ad_ops->ad_header_read( ad , pst) < 0
1225 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0)
1229 ad_close( ad, adflags );
1235 /* ****************************************** */
1236 /* open the resource fork if SFM */
1238 if (ad->ad_flags != AD_VERSION1_SFM) {
1242 if ((adflags & ADFLAGS_DIR)) {
1243 /* no resource fork for directories / volumes XXX it's false! */
1247 /* untrue yet but ad_close will decremente it*/
1248 ad->ad_resource_fork.adf_refcount++;
1250 if (ad_reso_fileno(ad) != -1) { /* the file is already open */
1251 if ((oflags & ( O_RDWR | O_WRONLY)) &&
1252 !(ad->ad_resource_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
1254 ad_close( ad, open_df | ADFLAGS_HF);
1261 ad_p = ad->ad_ops->ad_path( path, ADFLAGS_RF );
1264 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
1266 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
1270 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1271 ad->ad_resource_fork.adf_fd = open( ad_p, hoflags, admode );
1273 if (ad->ad_resource_fork.adf_fd < 0 ) {
1274 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1276 ad->ad_resource_fork.adf_fd =open( ad_p, hoflags, admode );
1280 if ( ad->ad_resource_fork.adf_fd < 0) {
1283 ad_close( ad, adflags );
1287 adf_lock_init(&ad->ad_resource_fork);
1288 AD_SET(ad->ad_resource_fork.adf_off);
1289 ad->ad_resource_fork.adf_flags = hoflags;
1290 if ((oflags & O_CREAT) && !st_invalid) {
1291 /* just created, set owner if admin (root) */
1292 ad_chown(ad_p, &st_dir);
1294 else if (!fstat(ad->ad_resource_fork.adf_fd, &st_meta)) {
1295 ad->ad_rlen = st_meta.st_size;
1301 * @brief open metadata, possibly as root
1303 * Return only metadata but try very hard ie at first try as user, then try as root.
1305 * @param name name of file/dir
1306 * @param flags ADFLAGS_DIR: name is a directory \n
1307 * ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
1308 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1310 * @param adp pointer to struct adouble
1312 * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
1313 * a adouble file depends on various other volume options, eg. ADVOL_CACHE
1315 int ad_metadata(const char *name, int flags, struct adouble *adp)
1319 int create = O_RDONLY;
1321 dir = flags & ADFLAGS_DIR;
1323 /* Check if we shall call ad_open with O_CREAT */
1324 if ( (adp->ad_options & ADVOL_CACHE)
1325 && ! (adp->ad_options & ADVOL_NOADOUBLE)
1326 && (flags & ADFLAGS_CREATE) ) {
1327 create = O_CREAT | O_RDWR;
1329 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1332 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1336 /* we are root open read only */
1337 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1339 if ( seteuid(uid) < 0) {
1340 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1346 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1348 we need to check if the file is open by another process.
1349 it's slow so we only do it if we have to:
1351 - we don't already have the answer!
1353 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1359 * @brief openat like wrapper for ad_metadata
1361 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1367 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1373 if (ad_metadata(name, flags, adp) < 0) {
1379 if (fchdir(cwdfd) != 0) {
1380 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1393 /* ----------------------------------- */
1394 static int new_rfork(const char *path, struct adouble *ad, int adflags)
1396 const struct entry *eid;
1400 ad->ad_magic = AD_MAGIC;
1401 ad->ad_version = ad->ad_flags & 0x0f0000;
1402 if (!ad->ad_version) {
1403 ad->ad_version = AD_VERSION;
1406 memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
1407 memset(ad->ad_data, 0, sizeof(ad->ad_data));
1409 if (ad->ad_flags == AD_VERSION2)
1411 else if (ad->ad_flags == AD_VERSION2_OSX)
1412 eid = entry_order_osx;
1414 eid = entry_order_ea;
1418 ad->ad_eid[eid->id].ade_off = eid->offset;
1419 ad->ad_eid[eid->id].ade_len = eid->len;
1423 /* put something sane in the directory finderinfo */
1424 if ((adflags & ADFLAGS_DIR)) {
1425 /* set default view */
1426 ashort = htons(FINDERINFO_CLOSEDVIEW);
1427 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF,
1428 &ashort, sizeof(ashort));
1430 /* set default creator/type fields */
1431 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
1432 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
1435 /* make things invisible */
1436 if ((ad->ad_options & ADVOL_INVDOTS) && !(adflags & ADFLAGS_CREATE) &&
1437 (*path == '.') && strcmp(path, ".") && strcmp(path, ".."))
1439 ashort = htons(ATTRBIT_INVISIBLE);
1440 ad_setattr(ad, ashort);
1441 ashort = htons(FINDERINFO_INVISIBLE);
1442 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1445 if (lstat(path, &st) < 0) {
1449 /* put something sane in the date fields */
1450 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
1451 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
1452 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
1453 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
1457 /* to do this with mmap, we need the hfs fs to understand how to mmap
1459 int ad_refresh(struct adouble *ad)
1462 if (ad_meta_fileno(ad) < 0)
1465 return ad->ad_ops->ad_header_read(ad, NULL);
1468 int ad_openat(int dirfd, /* dir fd openat like */
1479 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1485 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1491 if (fchdir(cwdfd) != 0) {
1492 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");