+ int ret = 0;
+
+ if (ad->ad_flags != AD_VERSION_EA)
+ return 0;
+
+ LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s\", adf: 0x%04x, of: 0x%04x)",
+ abspath(path), adflags, oflags);
+
+ if (ad->ad_resource_fork.adf_refcount) {
+ /* already open */
+ ad->ad_resource_fork.adf_flags &= ~( O_TRUNC | O_CREAT ); /* not new anymore */
+ free(ad->ad_resforkbuf);
+ ad->ad_resforkbuf = NULL;
+ }
+
+ if ((ad->ad_rlen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) <= 0)
+ return -1;
+
+ /* Round up and allocate buffer */
+ size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
+ if ((ad->ad_resforkbuf = malloc(roundup)) == NULL)
+ return -1;
+
+ ad->ad_resforkbufsize = roundup;
+
+ /* Read the EA into the buffer */
+ if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_resforkbuf, ad->ad_rlen) == -1)
+ return -1;
+
+ ad->ad_resource_fork.adf_refcount++;
+
+exit:
+ if (ret != 0) {
+ free(ad->ad_resforkbuf);
+ ad->ad_resforkbuf = NULL;
+ }
+
+ return ret;