+/*!
+ * Copy an EA from one file to another
+ *
+ * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
+ */
+int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
+{
+ EC_INIT;
+ int sfd = -1;
+ int dfd = -1;
+ size_t easize;
+ char *eabuf = NULL;
+
+#ifdef HAVE_ATFUNCS
+ if (dirfd == -1)
+ dirfd = AT_FDCWD;
+ EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
+#else
+ EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
+#endif
+ EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
+
+ if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
+ EC_NULL_LOG( eabuf = malloc(easize));
+ EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
+ EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
+ }
+
+EC_CLEANUP:
+ if (sfd != -1)
+ close(sfd);
+ if (dfd != -1)
+ close(dfd);
+ free(eabuf);
+ EC_EXIT;
+}
+