+/**
+ * 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_READ | 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;
+}
+