]> wimlib.net Git - wimlib/blob - src/metadata_resource.c
Refactor Win32 capture/apply, UNIX apply
[wimlib] / src / metadata_resource.c
1 /*
2  * metadata_resource.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 3 of the License, or (at your option) any later
13  * version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * wimlib; if not, see http://www.gnu.org/licenses/.
21  */
22
23 #include "wimlib_internal.h"
24 #include "dentry.h"
25 #include "lookup_table.h"
26
27 /*
28  * Reads a metadata resource for an image in the WIM file.  The metadata
29  * resource consists of the security data, followed by the directory entry for
30  * the root directory, followed by all the other directory entries in the
31  * filesystem.  The subdir_offset field of each directory entry gives the start
32  * of its child entries from the beginning of the metadata resource.  An
33  * end-of-directory is signaled by a directory entry of length '0', really of
34  * length 8, because that's how long the 'length' field is.
35  *
36  * @w:          Pointer to the WIMStruct for the WIM file.
37  *
38  * @imd:        Pointer to the image metadata structure for the image whose
39  *              metadata resource we are reading.  Its `metadata_lte' member
40  *              specifies the lookup table entry for the metadata resource.  The
41  *              rest of the image metadata entry will be filled in by this
42  *              function.
43  *
44  * Returns:     Zero on success, nonzero on failure.
45  */
46 int
47 read_metadata_resource(WIMStruct *w, struct wim_image_metadata *imd)
48 {
49         u8 *buf;
50         u32 dentry_offset;
51         int ret;
52         struct wim_dentry *dentry;
53         const struct wim_lookup_table_entry *metadata_lte;
54         u64 metadata_len;
55
56         metadata_lte = imd->metadata_lte;
57         metadata_len = wim_resource_size(metadata_lte);
58
59         DEBUG("Reading metadata resource: original_size = %"PRIu64", "
60               "size = %"PRIu64", offset = %"PRIu64"",
61               metadata_lte->resource_entry.original_size,
62               metadata_lte->resource_entry.size,
63               metadata_lte->resource_entry.offset);
64
65         /* There is no way the metadata resource could possibly be less than (8
66          * + WIM_DENTRY_DISK_SIZE) bytes, where the 8 is for security data (with
67          * no security descriptors) and WIM_DENTRY_DISK_SIZE is for the root
68          * dentry. */
69         if (metadata_len < 8 + WIM_DENTRY_DISK_SIZE) {
70                 ERROR("Expected at least %u bytes for the metadata resource",
71                       8 + WIM_DENTRY_DISK_SIZE);
72                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
73         }
74
75         if (sizeof(size_t) < 8 && metadata_len > 0xffffffff) {
76                 ERROR("Metadata resource is too large (%"PRIu64" bytes",
77                       metadata_len);
78                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
79         }
80
81         /* Allocate memory for the uncompressed metadata resource. */
82         buf = MALLOC(metadata_len);
83
84         if (!buf) {
85                 ERROR("Failed to allocate %"PRIu64" bytes for uncompressed "
86                       "metadata resource", metadata_len);
87                 return WIMLIB_ERR_NOMEM;
88         }
89
90         /* Read the metadata resource into memory.  (It may be compressed.) */
91         ret = read_full_resource_into_buf(metadata_lte, buf);
92         if (ret)
93                 goto out_free_buf;
94
95         DEBUG("Finished reading metadata resource into memory.");
96
97         /* The root directory entry starts after security data, aligned on an
98          * 8-byte boundary within the metadata resource.
99          *
100          * The security data starts with a 4-byte integer giving its total
101          * length, so if we round that up to an 8-byte boundary that gives us
102          * the offset of the root dentry.
103          *
104          * Here we read the security data into a wim_security_data structure,
105          * and if successful, go ahead and calculate the offset in the metadata
106          * resource of the root dentry. */
107
108         wimlib_assert(imd->security_data == NULL);
109         ret = read_security_data(buf, metadata_len, &imd->security_data);
110         if (ret)
111                 goto out_free_buf;
112
113         dentry_offset = (imd->security_data->total_length + 7) & ~7;
114
115         if (dentry_offset == 0) {
116                 ERROR("Integer overflow while reading metadata resource");
117                 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
118                 goto out_free_security_data;
119         }
120
121         DEBUG("Reading root dentry");
122
123         /* Allocate memory for the root dentry and read it into memory */
124         dentry = MALLOC(sizeof(struct wim_dentry));
125         if (!dentry) {
126                 ERROR("Failed to allocate %zu bytes for root dentry",
127                       sizeof(struct wim_dentry));
128                 ret = WIMLIB_ERR_NOMEM;
129                 goto out_free_security_data;
130         }
131
132         ret = read_dentry(buf, metadata_len, dentry_offset, dentry);
133
134         /* This is the root dentry, so set its parent to itself. */
135         dentry->parent = dentry;
136
137         if (ret == 0 && dentry->length == 0) {
138                 ERROR("Metadata resource cannot begin with end-of-directory entry!");
139                 ret = WIMLIB_ERR_INVALID_DENTRY;
140         }
141
142         if (ret != 0) {
143                 FREE(dentry);
144                 goto out_free_security_data;
145         }
146
147         inode_add_dentry(dentry, dentry->d_inode);
148
149         /* Now read the entire directory entry tree into memory. */
150         DEBUG("Reading dentry tree");
151         ret = read_dentry_tree(buf, metadata_len, dentry);
152         if (ret)
153                 goto out_free_dentry_tree;
154
155         /* Build hash table that maps hard link group IDs to dentry sets */
156         ret = dentry_tree_fix_inodes(dentry, &imd->inode_list);
157         if (ret)
158                 goto out_free_dentry_tree;
159
160         if (!w->all_images_verified) {
161                 DEBUG("Running miscellaneous verifications on the dentry tree");
162                 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
163                 ret = for_dentry_in_tree(dentry, verify_dentry, w);
164                 if (ret)
165                         goto out_free_dentry_tree;
166         }
167
168         DEBUG("Done reading image metadata");
169
170         imd->root_dentry = dentry;
171         INIT_LIST_HEAD(&imd->unhashed_streams);
172         goto out_free_buf;
173 out_free_dentry_tree:
174         free_dentry_tree(dentry, NULL);
175 out_free_security_data:
176         free_security_data(imd->security_data);
177         imd->security_data = NULL;
178 out_free_buf:
179         FREE(buf);
180         return ret;
181 }
182
183 static void
184 recalculate_security_data_length(struct wim_security_data *sd)
185 {
186         u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32);
187         for (u32 i = 0; i < sd->num_entries; i++)
188                 total_length += sd->sizes[i];
189         sd->total_length = total_length;
190 }
191
192 /* Like write_wim_resource(), but the resource is specified by a buffer of
193  * uncompressed data rather a lookup table entry; also writes the SHA1 hash of
194  * the buffer to @hash.  */
195 static int
196 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
197                                int out_fd, int out_ctype,
198                                struct resource_entry *out_res_entry,
199                                u8 hash[SHA1_HASH_SIZE])
200 {
201         /* Set up a temporary lookup table entry to provide to
202          * write_wim_resource(). */
203         struct wim_lookup_table_entry lte;
204         int ret;
205         lte.resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
206         lte.attached_buffer              = (void*)buf;
207         lte.resource_entry.original_size = buf_size;
208         lte.resource_entry.flags         = 0;
209         lte.unhashed                     = 1;
210         ret = write_wim_resource(&lte, out_fd, out_ctype, out_res_entry, 0);
211         if (ret == 0)
212                 copy_hash(hash, lte.hash);
213         return ret;
214 }
215
216 /* Write the metadata resource for the current WIM image. */
217 int
218 write_metadata_resource(WIMStruct *w)
219 {
220         u8 *buf;
221         u8 *p;
222         int ret;
223         u64 subdir_offset;
224         struct wim_dentry *root;
225         struct wim_lookup_table_entry *lte;
226         u64 metadata_original_size;
227         struct wim_security_data *sd;
228         struct wim_image_metadata *imd;
229
230         wimlib_assert(w->out_fd != -1);
231         wimlib_assert(w->current_image != WIMLIB_NO_IMAGE);
232
233         DEBUG("Writing metadata resource for image %d (offset = %"PRIu64")",
234               w->current_image, filedes_offset(w->out_fd));
235
236         imd = w->image_metadata[w->current_image - 1];
237
238         root = imd->root_dentry;
239         sd = imd->security_data;
240
241         if (!root) {
242                 /* Empty image; create a dummy root. */
243                 ret = new_filler_directory(T(""), &root);
244                 if (ret)
245                         return ret;
246                 imd->root_dentry = root;
247         }
248
249         /* Offset of first child of the root dentry.  It's equal to:
250          * - The total length of the security data, rounded to the next 8-byte
251          *   boundary,
252          * - plus the total length of the root dentry,
253          * - plus 8 bytes for an end-of-directory entry following the root
254          *   dentry (shouldn't really be needed, but just in case...)
255          */
256         recalculate_security_data_length(sd);
257         subdir_offset = (((u64)sd->total_length + 7) & ~7) +
258                         dentry_correct_total_length(root) + 8;
259
260         /* Calculate the subdirectory offsets for the entire dentry tree. */
261         calculate_subdir_offsets(root, &subdir_offset);
262
263         /* Total length of the metadata resource (uncompressed) */
264         metadata_original_size = subdir_offset;
265
266         /* Allocate a buffer to contain the uncompressed metadata resource */
267         buf = MALLOC(metadata_original_size);
268         if (!buf) {
269                 ERROR("Failed to allocate %"PRIu64" bytes for "
270                       "metadata resource", metadata_original_size);
271                 return WIMLIB_ERR_NOMEM;
272         }
273
274         /* Write the security data into the resource buffer */
275         p = write_security_data(sd, buf);
276
277         /* Write the dentry tree into the resource buffer */
278         p = write_dentry_tree(root, p);
279
280         /* We MUST have exactly filled the buffer; otherwise we calculated its
281          * size incorrectly or wrote the data incorrectly. */
282         wimlib_assert(p - buf == metadata_original_size);
283
284         /* Get the lookup table entry for the metadata resource so we can update
285          * it. */
286         lte = wim_get_current_image_metadata(w)->metadata_lte;
287
288         /* Write the metadata resource to the output WIM using the proper
289          * compression type.  The lookup table entry for the metadata resource
290          * is updated. */
291         ret = write_wim_resource_from_buffer(buf, metadata_original_size,
292                                              w->out_fd,
293                                              wimlib_get_compression_type(w),
294                                              &lte->output_resource_entry,
295                                              lte->hash);
296         /* Note that although the SHA1 message digest of the metadata resource
297          * is very likely to have changed, the corresponding lookup table entry
298          * is not actually located in the hash table, so it need not be
299          * re-inserted in the hash table. */
300
301         /* All the data has been written to the new WIM; no need for the buffer
302          * anymore */
303         FREE(buf);
304         return ret;
305 }