+ return AFPERR_MISC;
+ }
+ }
+
+ return AFP_OK;
+}
+
+/*
+ * @brief Copy EAs
+ *
+ * @note Supports *at semantics, therfor switches back and forth between sfd and cwd
+ */
+int sys_ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
+{
+ int ret = 0;
+ int cwd = -1;
+ ssize_t size;
+ char *names = NULL, *end_names, *name, *value = NULL;
+ unsigned int setxattr_ENOTSUP = 0;
+
+ if (sfd != -1) {
+ if ((cwd = open(".", O_RDONLY)) == -1) {
+ LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant open cwd: %s",
+ strerror(errno));
+ ret = -1;
+ goto getout;
+ }
+ }
+
+ if (sfd != -1) {
+ if (fchdir(sfd) == -1) {
+ LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
+ strerror(errno));
+ ret = -1;
+ goto getout;
+ }
+ }
+
+ size = sys_listxattr(src, NULL, 0);
+ if (size < 0) {
+ if (errno != ENOSYS && errno != ENOTSUP) {
+ ret = -1;
+ }
+ goto getout;
+ }
+ names = malloc(size+1);
+ if (names == NULL) {
+ ret = -1;
+ goto getout;
+ }
+ size = sys_listxattr(src, names, size);
+ if (size < 0) {
+ ret = -1;
+ goto getout;
+ } else {
+ names[size] = '\0';
+ end_names = names + size;
+ }
+
+ if (sfd != -1) {
+ if (fchdir(cwd) == -1) {
+ LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
+ strerror(errno));
+ ret = -1;
+ goto getout;
+ }
+ }
+
+ for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
+ void *old_value;
+
+ /* check if this attribute shall be preserved */
+ if (!*name)
+ continue;
+
+ if (STRCMP(name, ==, AD_EA_META))
+ continue;
+
+ if (sfd != -1) {
+ if (fchdir(sfd) == -1) {
+ LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to sfd: %s",
+ strerror(errno));
+ ret = -1;
+ goto getout;
+ }
+ }
+
+ size = sys_getxattr (src, name, NULL, 0);
+ if (size < 0) {
+ ret = -1;
+ continue;
+ }
+ value = realloc(old_value = value, size);
+ if (size != 0 && value == NULL) {
+ free(old_value);
+ ret = -1;
+ }
+ size = sys_getxattr(src, name, value, size);
+ if (size < 0) {
+ ret = -1;
+ continue;
+ }
+
+ if (sfd != -1) {
+ if (fchdir(cwd) == -1) {
+ LOG(log_error, logtype_afpd, "sys_ea_copyfile: cant chdir to cwd: %s",
+ strerror(errno));
+ ret = -1;
+ goto getout;
+ }
+ }
+
+ if (sys_setxattr(dst, name, value, size, 0) != 0) {
+ if (errno == ENOTSUP)
+ setxattr_ENOTSUP++;
+ else {
+ if (errno == ENOSYS) {
+ ret = -1;
+ /* no hope of getting any further */
+ break;
+ } else {
+ ret = -1;
+ }
+ }
+ }
+ }
+ if (setxattr_ENOTSUP) {
+ errno = ENOTSUP;
+ ret = -1;
+ }
+
+getout:
+ if (cwd != -1)
+ close(cwd);
+
+ free(value);
+ free(names);
+
+ if (ret == -1) {
+ switch(errno) {
+ case ENOENT:
+ /* no attribute */
+ break;
+ case EACCES:
+ LOG(log_debug, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));