+#ifdef HAVE_LINUX_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, bool is_old_format)
+{
+ const void * const entries_end = entries + entries_size;
+ char name[WIM_XATTR_NAME_MAX + 1];
+
+ for (const void *entry = entries;
+ entry < entries_end;
+ entry = is_old_format ? (const void *)old_xattr_entry_next(entry) :
+ (const void *)xattr_entry_next(entry))
+ {
+ bool valid;
+ u16 name_len;
+ const void *value;
+ u32 value_len;
+ int res;
+
+ if (is_old_format) {
+ valid = old_valid_xattr_entry(entry,
+ entries_end - entry);
+ } else {
+ valid = valid_xattr_entry(entry, entries_end - entry);
+ }
+ if (!valid) {
+ if (!path) {
+ path = unix_build_inode_extraction_path(inode,
+ ctx);
+ }
+ ERROR("\"%s\": extended attribute is corrupt or unsupported",
+ path);
+ return WIMLIB_ERR_INVALID_XATTR;
+ }
+ if (is_old_format) {
+ const struct wimlib_xattr_entry_old *e = entry;
+
+ name_len = le16_to_cpu(e->name_len);
+ memcpy(name, e->name, name_len);
+ value = e->name + name_len;
+ value_len = le32_to_cpu(e->value_len);
+ } else {
+ const struct wim_xattr_entry *e = entry;
+
+ name_len = e->name_len;
+ memcpy(name, e->name, name_len);
+ value = e->name + name_len + 1;
+ value_len = le16_to_cpu(e->value_len);
+ }
+ name[name_len] = '\0';
+
+ 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_linux_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_LINUX_XATTR_SUPPORT */
+