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