+#ifdef HAVE_XATTR_SUPPORT
+/* Apply extended attributes to a file */
+static int
+apply_linux_xattrs(int fd, const struct wim_inode *inode,
+ const char *path, struct unix_apply_ctx *ctx,
+ const void *entries, size_t entries_size)
+{
+ const void * const entries_end = entries + entries_size;
+ char name[XATTR_NAME_MAX + 1];
+
+ for (const struct wimlib_xattr_entry *entry = entries;
+ (void *)entry < entries_end; entry = xattr_entry_next(entry))
+ {
+ u16 name_len;
+ const void *value;
+ u32 value_len;
+ int res;
+
+ if (!valid_xattr_entry(entry, entries_end - (void *)entry)) {
+ if (!path) {
+ path = unix_build_inode_extraction_path(inode,
+ ctx);
+ }
+ ERROR("\"%s\": extended attribute is corrupt", path);
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+ name_len = le16_to_cpu(entry->name_len);
+ memcpy(name, entry->name, name_len);
+ name[name_len] = '\0';
+
+ value = entry->name + name_len;
+ value_len = le32_to_cpu(entry->value_len);
+
+ if (fd >= 0)
+ res = fsetxattr(fd, name, value, value_len, 0);
+ else
+ res = lsetxattr(path, name, value, value_len, 0);
+
+ if (unlikely(res != 0)) {
+ if (!path) {
+ path = unix_build_inode_extraction_path(inode,
+ ctx);
+ }
+ if (is_security_xattr(name) &&
+ (ctx->common.extract_flags &
+ WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
+ {
+ ERROR_WITH_ERRNO("\"%s\": unable to set extended attribute \"%s\"",
+ path, name);
+ return WIMLIB_ERR_SET_XATTR;
+ }
+ WARNING_WITH_ERRNO("\"%s\": unable to set extended attribute \"%s\"",
+ path, name);
+ }
+ }
+ return 0;
+}
+#endif /* HAVE_XATTR_SUPPORT */
+