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