+/* error here means it's not ad ._ adouble:osx file and thus we return 1 */
+int ad_valid_header_osx(const char *path)
+{
+ EC_INIT;
+ int fd = -1;
+ struct adouble adosx;
+ char *buf = &adosx.ad_data[0];
+ ssize_t header_len;
+
+ LOG(log_debug, logtype_ad, "ad_valid_header_osx(\"%s\"): BEGIN", fullpathname(path));
+
+ EC_NEG1( fd = open(path, O_RDONLY) );
+
+ /* read the header */
+ EC_NEG1( header_len = read(fd, buf, AD_DATASZ_OSX) );
+
+ if (header_len < AD_HEADER_LEN)
+ EC_FAIL;
+
+ memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
+ memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
+ adosx.ad_magic = ntohl(adosx.ad_magic);
+ adosx.ad_version = ntohl(adosx.ad_version);
+
+ if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
+ LOG(log_warning, logtype_ad, "ad_valid_header_osx(\"%s\"): not an adouble:osx file", fullpathname(path));
+ EC_FAIL;
+ }
+
+ if (strncmp(buf + ADEDOFF_FILLER, "Mac OS X", strlen("Mac OS X")) == 0)
+ /*
+ * It's a split fork created by OS X, it's not our "own" ._ file
+ * and thus not a valid header in this context.
+ * We allow enumeration and access.
+ */
+ EC_FAIL;
+
+EC_CLEANUP:
+ LOG(log_debug, logtype_ad, "ad_valid_header_osx(\"%s\"): END: %d", fullpathname(path), ret);
+ if (fd != -1)
+ close(fd);
+ if (ret != 0)
+ return 1;
+ 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;
+}
+