len = ntohl( len );
buf += sizeof( len );
- if (eid
- && eid < ADEID_MAX
- && off < sizeof(ad->ad_data)
- && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
- ad->ad_eid[ eid ].ade_off = off;
- ad->ad_eid[ eid ].ade_len = len;
- } else if (ret == 0) {
- ret = -11;
- LOG(log_warning, logtype_ad, "parse_entries: bogus eid: %d", eid);
+ ad->ad_eid[eid].ade_off = off;
+ ad->ad_eid[eid].ade_len = len;
+
+ if (!eid
+ || eid > ADEID_MAX
+ || off >= sizeof(ad->ad_data)
+ || ((eid != ADEID_RFORK) && (off + len > sizeof(ad->ad_data))))
+ {
+ ret = -1;
+ LOG(log_warning, logtype_ad, "parse_entries: bogus eid: %u, off: %u, len: %u",
+ (uint)eid, (uint)off, (uint)len);
}
}
if (parse_entries(ad, buf, nentries) != 0) {
LOG(log_warning, logtype_ad, "ad_header_read(%s): malformed AppleDouble",
path ? fullpathname(path) : "");
+ errno = EIO;
+ return -1;
}
if (!ad_getentryoff(ad, ADEID_RFORK)
|| (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
return 0;
}
+/**
+ * Convert from Apple's ._ file to Netatalk
+ *
+ * Apple's AppleDouble may contain a FinderInfo entry longer then 32 bytes
+ * containing packed xattrs. Netatalk can't deal with that, so we
+ * simply discard the packed xattrs.
+ *
+ * As we call ad_open() which might result in a recursion, just to be sure
+ * use static variable in_conversion to check for that.
+ *
+ * Returns -1 in case an error occured, 0 if no conversion was done, 1 otherwise
+ **/
+static int ad_convert_osx(const char *path, struct adouble *ad)
+{
+ EC_INIT;
+ static bool in_conversion = false;
+ char *map;
+ int finderlen = ad_getentrylen(ad, ADEID_FINDERI);
+ ssize_t origlen;
+
+ if (in_conversion || finderlen == ADEDLEN_FINDERI)
+ return 0;
+ in_conversion = true;
+
+ LOG(log_debug, logtype_ad, "Converting OS X AppleDouble %s, FinderInfo length: %d",
+ fullpathname(path), finderlen);
+
+ origlen = ad_getentryoff(ad, ADEID_RFORK) + ad_getentrylen(ad, ADEID_RFORK);
+
+ map = mmap(NULL, origlen, PROT_WRITE, MAP_SHARED, ad_reso_fileno(ad), 0);
+ if (map == MAP_FAILED) {
+ LOG(log_error, logtype_ad, "mmap AppleDouble: %s\n", strerror(errno));
+ EC_FAIL;
+ }
+
+ memmove(map + ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI,
+ map + ad_getentryoff(ad, ADEID_RFORK),
+ ad_getentrylen(ad, ADEID_RFORK));
+
+ ad_setentrylen(ad, ADEID_FINDERI, ADEDLEN_FINDERI);
+ ad->ad_rlen = ad_getentrylen(ad, ADEID_RFORK);
+ ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(ad, ADEID_FINDERI) + ADEDLEN_FINDERI);
+
+ EC_ZERO_LOG( ftruncate(ad_reso_fileno(ad),
+ ad_getentryoff(ad, ADEID_RFORK)
+ + ad_getentrylen(ad, ADEID_RFORK)) );
+
+ (void)ad_rebuild_adouble_header_osx(ad, map);
+ munmap(map, origlen);
+
+ /* Create a metadata EA if one doesn't exit */
+ if (strlen(path) < 3)
+ EC_EXIT_STATUS(0);
+ struct adouble adea;
+ ad_init_old(&adea, AD_VERSION_EA, ad->ad_options);
+
+ if (ad_open(&adea, path + 2, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) < 0) {
+ LOG(log_error, logtype_ad, "create metadata: %s\n", strerror(errno));
+ EC_FAIL;
+ }
+ if (adea.ad_mdp->adf_flags & O_CREAT) {
+ memcpy(ad_entry(&adea, ADEID_FINDERI),
+ ad_entry(ad, ADEID_FINDERI),
+ ADEDLEN_FINDERI);
+ ad_flush(&adea);
+ }
+ ad_close(&adea, ADFLAGS_HF);
+
+EC_CLEANUP:
+ in_conversion = false;
+ if (ret != 0)
+ return -1;
+ return 1;
+}
+
/* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
static int ad_header_read_osx(const char *path, struct adouble *ad, const struct stat *hst)
{
int len;
ssize_t header_len;
struct stat st;
+ int retry_read = 0;
+reread:
+ LOG(log_debug, logtype_ad, "ad_header_read_osx: %s", path ? fullpathname(path) : "");
+ ad_init_old(&adosx, AD_VERSION_EA, ad->ad_options);
memset(buf, 0, sizeof(adosx.ad_data));
+ adosx.ad_rfp->adf_fd = ad_reso_fileno(ad);
/* read the header */
EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
path ? fullpathname(path) : "");
}
+ if (ad_getentrylen(&adosx, ADEID_FINDERI) != ADEDLEN_FINDERI) {
+ LOG(log_warning, logtype_ad, "Convert OS X to Netatalk AppleDouble: %s",
+ path ? fullpathname(path) : "");
+
+ if (retry_read > 0) {
+ LOG(log_error, logtype_ad, "ad_header_read_osx: %s, giving up", path ? fullpathname(path) : "");
+ errno = EIO;
+ EC_FAIL;
+ }
+ retry_read++;
+ if (ad_convert_osx(path, &adosx) == 1) {
+ goto reread;
+ }
+ errno = EIO;
+ EC_FAIL;
+ }
+
if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
|| ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
|| ad_getentryoff(&adosx, ADEID_RFORK) > header_len
if (parse_entries(ad, buf + AD_HEADER_LEN, nentries)) {
LOG(log_warning, logtype_ad, "ad_header_read(%s): malformed AppleDouble",
path ? fullpathname(path) : "");
+ errno = EINVAL;
+ EC_FAIL;
}
if (nentries != ADEID_NUM_EA
#ifdef HAVE_EAFD
return 0;
#else
- return ADEDOFF_RFORK_OSX;
+ return ad->ad_eid[eid].ade_off;
#endif
default:
return ad->ad_eid[eid].ade_off;