4 * Support for tagged metadata items that can be appended to WIM directory
9 * Copyright (C) 2014-2016 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib/endianness.h"
30 #include "wimlib/inode.h"
31 #include "wimlib/object_id.h"
32 #include "wimlib/types.h"
33 #include "wimlib/unix_data.h"
35 /* Object ID tag; this is also used by the Microsoft implementation. */
36 #define TAG_OBJECT_ID 0x00000001
38 /* Random number that we'll use for tagging our UNIX data items. */
39 #define TAG_WIMLIB_UNIX_DATA 0x337DD873
41 /* Header that begins each tagged metadata item in the metadata resource */
42 struct tagged_item_header {
44 /* Unique identifier for this item. */
47 /* Size of the data of this tagged item, in bytes. This excludes this
48 * header and should be a multiple of 8. */
51 /* Variable length data */
55 /* Unconfirmed: are all 64 bytes of the object ID always present? Since NTFS-3G
56 * permits shorter object IDs, we'll do the same for now. */
57 #define OBJECT_ID_MIN_LENGTH 16
59 struct object_id_disk {
61 u8 birth_volume_id[16];
62 u8 birth_object_id[16];
66 struct wimlib_unix_data_disk {
73 /* Retrieves the first tagged item with the specified tag and minimum length
74 * from the WIM inode. Returns a pointer to the tagged data, which can be read
75 * and/or modified in place. Or, if no matching tagged item is found, returns
78 inode_get_tagged_item(const struct wim_inode *inode,
79 u32 desired_tag, u32 min_data_len, u32 *actual_len_ret)
81 size_t minlen_with_hdr = sizeof(struct tagged_item_header) + min_data_len;
88 len_remaining = inode->i_extra->size;
89 p = inode->i_extra->data;
91 /* Iterate through the tagged items. */
92 while (len_remaining >= minlen_with_hdr) {
93 struct tagged_item_header *hdr;
97 hdr = (struct tagged_item_header *)p;
98 tag = le32_to_cpu(hdr->tag);
99 len = ALIGN(le32_to_cpu(hdr->length), 8);
101 /* Length overflow? */
102 if (unlikely(len > len_remaining - sizeof(struct tagged_item_header)))
105 /* Matches the item we wanted? */
106 if (tag == desired_tag && len >= min_data_len) {
108 *actual_len_ret = len;
112 len_remaining -= sizeof(struct tagged_item_header) + len;
113 p += sizeof(struct tagged_item_header) + len;
118 /* Adds a tagged item to a WIM inode and returns a pointer to its uninitialized
119 * data, which must be initialized in-place by the caller. */
121 inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len)
125 struct wim_inode_extra *extra;
126 struct tagged_item_header *hdr;
128 /* We prepend the item instead of appending it because it's easier. */
130 itemsize = sizeof(struct tagged_item_header) + ALIGN(len, 8);
133 newsize += inode->i_extra->size;
135 extra = MALLOC(sizeof(struct wim_inode_extra) + newsize);
138 if (inode->i_extra) {
139 memcpy(&extra->data[itemsize], inode->i_extra->data,
140 inode->i_extra->size);
141 FREE(inode->i_extra);
143 extra->size = newsize;
144 inode->i_extra = extra;
146 hdr = (struct tagged_item_header *)extra->data;
147 hdr->tag = cpu_to_le32(tag);
148 hdr->length = cpu_to_le32(len);
149 return memset(hdr->data, 0, ALIGN(len, 8));
152 static inline struct wimlib_unix_data_disk *
153 inode_get_unix_data_disk(const struct wim_inode *inode)
155 return inode_get_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
156 sizeof(struct wimlib_unix_data_disk),
160 static inline struct wimlib_unix_data_disk *
161 inode_add_unix_data_disk(struct wim_inode *inode)
163 return inode_add_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
164 sizeof(struct wimlib_unix_data_disk));
167 /* Returns %true if the specified WIM inode has UNIX data; otherwise %false.
168 * This is a wimlib extension. */
170 inode_has_unix_data(const struct wim_inode *inode)
172 return inode_get_unix_data_disk(inode) != NULL;
175 /* Retrieves UNIX data from the specified WIM inode.
176 * This is a wimlib extension.
178 * Returns %true and fills @unix_data if the inode has UNIX data.
179 * Otherwise returns %false. */
181 inode_get_unix_data(const struct wim_inode *inode,
182 struct wimlib_unix_data *unix_data)
184 const struct wimlib_unix_data_disk *p;
186 p = inode_get_unix_data_disk(inode);
190 unix_data->uid = le32_to_cpu(p->uid);
191 unix_data->gid = le32_to_cpu(p->gid);
192 unix_data->mode = le32_to_cpu(p->mode);
193 unix_data->rdev = le32_to_cpu(p->rdev);
197 /* Sets UNIX data on the specified WIM inode.
198 * This is a wimlib extension.
200 * Callers must specify all members in @unix_data. If the inode does not yet
201 * have UNIX data, it is given these values. Otherwise, only the values that
202 * also have the corresponding flags in @which set are changed.
204 * Returns %true if successful, %false if failed (out of memory). */
206 inode_set_unix_data(struct wim_inode *inode, struct wimlib_unix_data *unix_data,
209 struct wimlib_unix_data_disk *p;
211 p = inode_get_unix_data_disk(inode);
213 p = inode_add_unix_data_disk(inode);
216 which = UNIX_DATA_ALL;
218 if (which & UNIX_DATA_UID)
219 p->uid = cpu_to_le32(unix_data->uid);
220 if (which & UNIX_DATA_GID)
221 p->gid = cpu_to_le32(unix_data->gid);
222 if (which & UNIX_DATA_MODE)
223 p->mode = cpu_to_le32(unix_data->mode);
224 if (which & UNIX_DATA_RDEV)
225 p->rdev = cpu_to_le32(unix_data->rdev);
229 /* Return %true iff the specified inode has an object ID. */
231 inode_has_object_id(const struct wim_inode *inode)
233 return inode_get_object_id(inode, NULL) != NULL;
236 /* Retrieve a pointer to the object ID of the specified inode and write its
237 * length to @len_ret. Return NULL if the inode does not have an object ID. */
239 inode_get_object_id(const struct wim_inode *inode, u32 *len_ret)
241 return inode_get_tagged_item(inode, TAG_OBJECT_ID,
242 OBJECT_ID_MIN_LENGTH, len_ret);
245 /* Set the inode's object ID to the value specified by @object_id and @len.
246 * Assumes the inode didn't already have an object ID set. Returns %true if
247 * successful, %false if failed (out of memory). */
249 inode_set_object_id(struct wim_inode *inode, const void *object_id, u32 len)
253 p = inode_add_tagged_item(inode, TAG_OBJECT_ID, len);
257 memcpy(p, object_id, len);