4 * Support for tagged metadata items that can be appended to WIM directory
9 * Copyright (C) 2014 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/endianness.h"
32 #include "wimlib/inode.h"
33 #include "wimlib/types.h"
34 #include "wimlib/unix_data.h"
36 /* Used by the Microsoft implementation. */
37 #define TAG_OBJECT_ID 0x00000001
39 /* Random number that we'll use for tagging our UNIX data items. */
40 #define TAG_WIMLIB_UNIX_DATA 0x337DD873
42 /* Header that begins each tagged metadata item in the metadata resource */
43 struct tagged_item_header {
45 /* Unique identifier for this item. */
48 /* Size of the data of this tagged item, in bytes. This excludes this
49 * header and should be a multiple of 8.
51 * (Actually, the MS implementation seems to round this up to an 8 byte
52 * boundary when calculating the offset to the next tagged item, but
53 * uses this length unmodified when validating the item. We might as
54 * well do the same.) */
57 /* Variable length data */
61 struct object_id_disk {
63 u8 birth_volume_id[16];
64 u8 birth_object_id[16];
68 struct wimlib_unix_data_disk {
75 /* Retrieves the first tagged item with the specified tag and minimum length
76 * from the WIM inode. Returns a pointer to the tagged data, which can be read
77 * and/or modified in place. Or, if no matching tagged item is found, returns
80 inode_get_tagged_item(const struct wim_inode *inode,
81 u32 desired_tag, u32 min_data_len)
83 size_t minlen_with_hdr = sizeof(struct tagged_item_header) + min_data_len;
84 size_t len_remaining = inode->i_extra_size;
85 u8 *p = inode->i_extra;
87 /* Iterate through the tagged items. */
88 while (len_remaining >= minlen_with_hdr) {
89 struct tagged_item_header *hdr;
93 hdr = (struct tagged_item_header *)p;
94 tag = le32_to_cpu(hdr->tag);
95 len = le32_to_cpu(hdr->length);
97 if (tag == desired_tag && len >= min_data_len)
100 len = (len + 7) & ~7;
101 if (len_remaining <= sizeof(struct tagged_item_header) + len)
103 len_remaining -= sizeof(struct tagged_item_header) + len;
104 p += sizeof(struct tagged_item_header) + len;
109 /* Adds a tagged item to a WIM inode and returns a pointer to its uninitialized
110 * data, which must be initialized in-place by the caller. */
112 inode_add_tagged_item(struct wim_inode *inode, u32 tag, u32 len)
117 struct tagged_item_header *hdr;
119 /* We prepend the item instead of appending it because it's easier. */
121 itemsize = sizeof(struct tagged_item_header) + len;
122 newsize = itemsize + inode->i_extra_size;
124 buf = MALLOC(newsize);
128 if (inode->i_extra_size) {
129 memcpy(buf + itemsize, inode->i_extra, inode->i_extra_size);
130 FREE(inode->i_extra);
132 inode->i_extra = buf;
133 inode->i_extra_size = newsize;
135 hdr = (struct tagged_item_header *)buf;
136 hdr->tag = cpu_to_le32(tag);
137 hdr->length = cpu_to_le32(len);
141 static inline struct wimlib_unix_data_disk *
142 inode_get_unix_data_disk(const struct wim_inode *inode)
144 return inode_get_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
145 sizeof(struct wimlib_unix_data_disk));
148 static inline struct wimlib_unix_data_disk *
149 inode_add_unix_data_disk(struct wim_inode *inode)
151 return inode_add_tagged_item(inode, TAG_WIMLIB_UNIX_DATA,
152 sizeof(struct wimlib_unix_data_disk));
155 /* Returns %true if the specified WIM inode has UNIX data; otherwise %false.
156 * This is a wimlib extension. */
158 inode_has_unix_data(const struct wim_inode *inode)
160 return inode_get_unix_data_disk(inode) != NULL;
163 /* Retrieves UNIX data from the specified WIM inode.
164 * This is a wimlib extension.
166 * Returns %true and fills @unix_data if the inode has UNIX data.
167 * Otherwise returns %false. */
169 inode_get_unix_data(const struct wim_inode *inode,
170 struct wimlib_unix_data *unix_data)
172 const struct wimlib_unix_data_disk *p;
174 p = inode_get_unix_data_disk(inode);
178 unix_data->uid = le32_to_cpu(p->uid);
179 unix_data->gid = le32_to_cpu(p->gid);
180 unix_data->mode = le32_to_cpu(p->mode);
181 unix_data->rdev = le32_to_cpu(p->rdev);
185 /* Sets UNIX data on the specified WIM inode.
186 * This is a wimlib extension.
188 * Callers must specify all members in @unix_data. If the inode does not yet
189 * have UNIX data, it is given these values. Otherwise, only the values that
190 * also have the corresponding flags in @which set are changed.
192 * Returns %true if successful, %false if failed (out of memory). */
194 inode_set_unix_data(struct wim_inode *inode, struct wimlib_unix_data *unix_data,
197 struct wimlib_unix_data_disk *p;
199 p = inode_get_unix_data_disk(inode);
201 p = inode_add_unix_data_disk(inode);
204 which = UNIX_DATA_ALL;
206 if (which & UNIX_DATA_UID)
207 p->uid = cpu_to_le32(unix_data->uid);
208 if (which & UNIX_DATA_GID)
209 p->gid = cpu_to_le32(unix_data->gid);
210 if (which & UNIX_DATA_MODE)
211 p->mode = cpu_to_le32(unix_data->mode);
212 if (which & UNIX_DATA_RDEV)
213 p->rdev = cpu_to_le32(unix_data->rdev);