4 * Support for tagged metadata items that can be appended to WIM directory
9 * Copyright (C) 2014 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/types.h"
32 #include "wimlib/unix_data.h"
34 /* Used by the Microsoft implementation. */
35 #define TAG_OBJECT_ID 0x00000001
37 /* Random number that we'll use for tagging our UNIX data items. */
38 #define TAG_WIMLIB_UNIX_DATA 0x337DD873
40 /* Header that begins each tagged metadata item in the metadata resource */
41 struct tagged_item_header {
43 /* Unique identifier for this item. */
46 /* Size of the data of this tagged item, in bytes. This excludes this
47 * header and should be a multiple of 8. */
50 /* Variable length data */
54 struct object_id_disk {
56 u8 birth_volume_id[16];
57 u8 birth_object_id[16];
61 struct wimlib_unix_data_disk {
68 /* Retrieves the first tagged item with the specified tag and minimum length
69 * from the WIM inode. Returns a pointer to the tagged data, which can be read
70 * and/or modified in place. Or, if no matching tagged item is found, returns
73 inode_get_tagged_item(const struct wim_inode *inode,
74 u32 desired_tag, u32 min_data_len)
76 size_t minlen_with_hdr = sizeof(struct tagged_item_header) + min_data_len;
77 size_t len_remaining = inode->i_extra_size;
78 u8 *p = inode->i_extra;
80 /* Iterate through the tagged items. */
81 while (len_remaining >= minlen_with_hdr) {
82 struct tagged_item_header *hdr;
86 hdr = (struct tagged_item_header *)p;
87 tag = le32_to_cpu(hdr->tag);
88 len = ALIGN(le32_to_cpu(hdr->length), 8);
90 /* Length overflow? */
91 if (unlikely(len > len_remaining - sizeof(struct tagged_item_header)))
94 /* Matches the item we wanted? */
95 if (tag == desired_tag && len >= min_data_len)
98 len_remaining -= sizeof(struct tagged_item_header) + len;
99 p += sizeof(struct tagged_item_header) + len;
104 /* Adds a tagged item to a WIM inode and returns a pointer to its uninitialized
105 * data, which must be initialized in-place by the caller. */
107 inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len)
112 struct tagged_item_header *hdr;
114 /* We prepend the item instead of appending it because it's easier. */
116 itemsize = sizeof(struct tagged_item_header) + ALIGN(len, 8);
117 newsize = itemsize + inode->i_extra_size;
119 buf = MALLOC(newsize);
123 if (inode->i_extra_size) {
124 memcpy(buf + itemsize, inode->i_extra, inode->i_extra_size);
125 FREE(inode->i_extra);
127 inode->i_extra = buf;
128 inode->i_extra_size = newsize;
130 hdr = (struct tagged_item_header *)buf;
131 hdr->tag = cpu_to_le32(tag);
132 hdr->length = cpu_to_le32(len);
133 return memset(hdr->data, 0, ALIGN(len, 8));
136 static inline struct wimlib_unix_data_disk *
137 inode_get_unix_data_disk(const struct wim_inode *inode)
139 return inode_get_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
140 sizeof(struct wimlib_unix_data_disk));
143 static inline struct wimlib_unix_data_disk *
144 inode_add_unix_data_disk(struct wim_inode *inode)
146 return inode_add_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
147 sizeof(struct wimlib_unix_data_disk));
150 /* Returns %true if the specified WIM inode has UNIX data; otherwise %false.
151 * This is a wimlib extension. */
153 inode_has_unix_data(const struct wim_inode *inode)
155 return inode_get_unix_data_disk(inode) != NULL;
158 /* Retrieves UNIX data from the specified WIM inode.
159 * This is a wimlib extension.
161 * Returns %true and fills @unix_data if the inode has UNIX data.
162 * Otherwise returns %false. */
164 inode_get_unix_data(const struct wim_inode *inode,
165 struct wimlib_unix_data *unix_data)
167 const struct wimlib_unix_data_disk *p;
169 p = inode_get_unix_data_disk(inode);
173 unix_data->uid = le32_to_cpu(p->uid);
174 unix_data->gid = le32_to_cpu(p->gid);
175 unix_data->mode = le32_to_cpu(p->mode);
176 unix_data->rdev = le32_to_cpu(p->rdev);
180 /* Sets UNIX data on the specified WIM inode.
181 * This is a wimlib extension.
183 * Callers must specify all members in @unix_data. If the inode does not yet
184 * have UNIX data, it is given these values. Otherwise, only the values that
185 * also have the corresponding flags in @which set are changed.
187 * Returns %true if successful, %false if failed (out of memory). */
189 inode_set_unix_data(struct wim_inode *inode, struct wimlib_unix_data *unix_data,
192 struct wimlib_unix_data_disk *p;
194 p = inode_get_unix_data_disk(inode);
196 p = inode_add_unix_data_disk(inode);
199 which = UNIX_DATA_ALL;
201 if (which & UNIX_DATA_UID)
202 p->uid = cpu_to_le32(unix_data->uid);
203 if (which & UNIX_DATA_GID)
204 p->gid = cpu_to_le32(unix_data->gid);
205 if (which & UNIX_DATA_MODE)
206 p->mode = cpu_to_le32(unix_data->mode);
207 if (which & UNIX_DATA_RDEV)
208 p->rdev = cpu_to_le32(unix_data->rdev);