1 #ifndef _WIMLIB_XATTR_H
2 #define _WIMLIB_XATTR_H
6 #include "wimlib/endianness.h"
7 #include "wimlib/tagged_items.h"
8 #include "wimlib/util.h"
10 #undef HAVE_LINUX_XATTR_SUPPORT
11 #if defined(HAVE_SYS_XATTR_H) && \
12 defined(HAVE_LLISTXATTR) && defined(HAVE_LGETXATTR) && \
13 defined(HAVE_FSETXATTR) && defined(HAVE_LSETXATTR)
14 # define HAVE_LINUX_XATTR_SUPPORT 1
17 #define WIM_XATTR_NAME_MAX 255
18 #define WIM_XATTR_SIZE_MAX 65535
21 * On-disk format of each extended attribute (xattr, or EA) entry in a metadata
22 * item tagged with TAG_XATTRS. This is the preferred xattr format, since it is
23 * also used by WIMGAPI and DISM starting in Windows 10 version 1607.
25 struct wim_xattr_entry {
27 /* length of xattr value in bytes */
30 /* length of xattr name in bytes, excluding the null terminator */
33 /* flags: 0 or 0x80 (FILE_NEED_EA) */
36 /* followed by the xattr name *with* a null terminator */
39 /* followed by the xattr value */
42 /* no padding at end! */
46 xattr_entry_size(const struct wim_xattr_entry *entry)
48 STATIC_ASSERT(sizeof(*entry) == 4);
50 return sizeof(*entry) + entry->name_len + 1 +
51 le16_to_cpu(entry->value_len);
54 /* minimum is a 1-byte name (plus null terminator) and an empty value */
55 #define XATTR_ENTRY_MIN_SIZE (sizeof(struct wim_xattr_entry) + 2)
57 static inline struct wim_xattr_entry *
58 xattr_entry_next(const struct wim_xattr_entry *entry)
60 return (void *)entry + xattr_entry_size(entry);
64 valid_xattr_entry(const struct wim_xattr_entry *entry, size_t avail)
66 if (avail < sizeof(*entry))
69 return entry->name_len > 0 && entry->name_len <= WIM_XATTR_NAME_MAX &&
70 le16_to_cpu(entry->value_len) <= WIM_XATTR_SIZE_MAX &&
71 avail >= xattr_entry_size(entry) &&
72 memchr(entry->name, '\0', entry->name_len) == NULL &&
73 entry->name[entry->name_len] == '\0';
77 * On-disk format of each extended attribute entry in a metadata item tagged
78 * with TAG_WIMLIB_LINUX_XATTRS. This is a deprecated format which wimlib
79 * v1.11-v1.12 used to store extended attributes on Linux (predating the Windows
80 * xattr support in both WIMGAPI and wimlib). Now we use TAG_XATTRS for both
81 * Windows and Linux xattrs.
83 struct wimlib_xattr_entry_old {
85 /* length of xattr name in bytes, excluding a null terminator */
88 /* reserved, must be 0 */
91 /* length of xattr value in bytes */
94 /* followed by the xattr name *without* a null terminator */
97 /* followed by the xattr value */
100 /* then zero-padded to a 4-byte boundary */
101 } _aligned_attribute(4);
104 old_xattr_entry_size(const struct wimlib_xattr_entry_old *entry)
106 STATIC_ASSERT(sizeof(*entry) == 8);
108 return ALIGN(sizeof(*entry) + le16_to_cpu(entry->name_len) +
109 le32_to_cpu(entry->value_len), 4);
112 /* minimum is a 1-byte name and an empty value */
113 #define OLD_XATTR_ENTRY_MIN_SIZE \
114 (ALIGN(sizeof(struct wimlib_xattr_entry_old) + 1, 4))
116 static inline struct wimlib_xattr_entry_old *
117 old_xattr_entry_next(const struct wimlib_xattr_entry_old *entry)
119 return (void *)entry + old_xattr_entry_size(entry);
123 old_valid_xattr_entry(const struct wimlib_xattr_entry_old *entry, size_t avail)
127 if (avail < sizeof(*entry))
130 name_len = le16_to_cpu(entry->name_len);
131 return name_len > 0 && name_len <= WIM_XATTR_NAME_MAX &&
132 le32_to_cpu(entry->value_len) <= WIM_XATTR_SIZE_MAX &&
133 avail >= old_xattr_entry_size(entry) &&
134 memchr(entry->name, '\0', name_len) == NULL;
137 /* Is the xattr of the specified name security-related on Linux? */
139 is_linux_security_xattr(const char *name)
141 #define XATTR_SECURITY_PREFIX "security."
142 #define XATTR_SYSTEM_PREFIX "system."
143 #define XATTR_POSIX_ACL_ACCESS "posix_acl_access"
144 #define XATTR_NAME_POSIX_ACL_ACCESS XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_ACCESS
145 #define XATTR_POSIX_ACL_DEFAULT "posix_acl_default"
146 #define XATTR_NAME_POSIX_ACL_DEFAULT XATTR_SYSTEM_PREFIX XATTR_POSIX_ACL_DEFAULT
148 return !strncmp(name, XATTR_SECURITY_PREFIX,
149 sizeof(XATTR_SECURITY_PREFIX) - 1) ||
150 !strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) ||
151 !strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT);
154 static inline const void *
155 inode_get_xattrs(const struct wim_inode *inode, u32 *len_ret)
157 return inode_get_tagged_item(inode, TAG_XATTRS,
158 XATTR_ENTRY_MIN_SIZE, len_ret);
161 static inline const void *
162 inode_get_xattrs_old(const struct wim_inode *inode, u32 *len_ret)
164 return inode_get_tagged_item(inode, TAG_WIMLIB_LINUX_XATTRS,
165 OLD_XATTR_ENTRY_MIN_SIZE, len_ret);
168 static inline const void *
169 inode_get_linux_xattrs(const struct wim_inode *inode, u32 *len_ret,
170 bool *is_old_format_ret)
174 entries = inode_get_xattrs(inode, len_ret);
176 *is_old_format_ret = false;
179 entries = inode_get_xattrs_old(inode, len_ret);
181 *is_old_format_ret = true;
188 inode_has_xattrs(const struct wim_inode *inode)
190 return inode_get_xattrs(inode, NULL) != NULL ||
191 inode_get_xattrs_old(inode, NULL) != NULL;
195 inode_set_xattrs(struct wim_inode *inode, const void *entries, u32 len)
197 return inode_set_tagged_item(inode, TAG_XATTRS, entries, len);
200 #endif /* _WIMLIB_XATTR_H */