]> wimlib.net Git - wimlib/blob - src/metadata_resource.c
19723269485d7889c4da5b3fe7f8d4f54771328d
[wimlib] / src / metadata_resource.c
1 /*
2  * metadata_resource.c
3  */
4
5 /*
6  * Copyright (C) 2012 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 the metadata metadata resource from 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  * @fp:         The FILE* for the input WIM file.
37  * @wim_ctype:  The compression type of the WIM file.
38  * @imd:        Pointer to the image metadata structure.  Its `metadata_lte'
39  *              member specifies the lookup table entry for the metadata
40  *              resource.  The rest of the image metadata entry will be filled
41  *              in by this function.
42  *
43  * @return:     Zero on success, nonzero on failure.
44  */
45 int read_metadata_resource(WIMStruct *w, struct image_metadata *imd)
46 {
47         u8 *buf;
48         u32 dentry_offset;
49         int ret;
50         struct dentry *dentry;
51         const struct lookup_table_entry *metadata_lte;
52         u64 metadata_len;
53         struct hlist_head inode_list;
54
55         metadata_lte = imd->metadata_lte;
56         metadata_len = wim_resource_size(metadata_lte);
57
58         DEBUG("Reading metadata resource: length = %"PRIu64", "
59               "offset = %"PRIu64"", metadata_len,
60               metadata_lte->resource_entry.offset);
61
62         /* There is no way the metadata resource could possibly be less than (8
63          * + WIM_DENTRY_DISK_SIZE) bytes, where the 8 is for security data (with
64          * no security descriptors) and WIM_DENTRY_DISK_SIZE is for the root
65          * dentry. */
66         if (metadata_len < 8 + WIM_DENTRY_DISK_SIZE) {
67                 ERROR("Expected at least %u bytes for the metadata resource",
68                       8 + WIM_DENTRY_DISK_SIZE);
69                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
70         }
71
72         if (sizeof(size_t) < 8 && metadata_len > 0xffffffff) {
73                 ERROR("Metadata resource is too large (%"PRIu64" bytes",
74                       metadata_len);
75                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
76         }
77
78         /* Allocate memory for the uncompressed metadata resource. */
79         buf = MALLOC(metadata_len);
80
81         if (!buf) {
82                 ERROR("Failed to allocate %"PRIu64" bytes for uncompressed "
83                       "metadata resource", metadata_len);
84                 return WIMLIB_ERR_NOMEM;
85         }
86
87         /* Read the metadata resource into memory.  (It may be compressed.) */
88         ret = read_full_wim_resource(metadata_lte, buf, 0);
89         if (ret != 0)
90                 goto out_free_buf;
91
92         DEBUG("Finished reading metadata resource into memory.");
93
94         /* The root directory entry starts after security data, aligned on an
95          * 8-byte boundary within the metadata resource.
96          *
97          * The security data starts with a 4-byte integer giving its total
98          * length, so if we round that up to an 8-byte boundary that gives us
99          * the offset of the root dentry.
100          *
101          * Here we read the security data into a wim_security_data structure,
102          * and if successful, go ahead and calculate the offset in the metadata
103          * resource of the root dentry. */
104
105         wimlib_assert(imd->security_data == NULL);
106         ret = read_security_data(buf, metadata_len, &imd->security_data);
107         if (ret != 0)
108                 goto out_free_buf;
109
110         dentry_offset = (imd->security_data->total_length + 7) & ~7;
111
112         if (dentry_offset == 0) {
113                 ERROR("Integer overflow while reading metadata resource");
114                 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
115                 goto out_free_security_data;
116         }
117
118         DEBUG("Reading root dentry");
119
120         /* Allocate memory for the root dentry and read it into memory */
121         dentry = MALLOC(sizeof(struct dentry));
122         if (!dentry) {
123                 ERROR("Failed to allocate %zu bytes for root dentry",
124                       sizeof(struct dentry));
125                 ret = WIMLIB_ERR_NOMEM;
126                 goto out_free_security_data;
127         }
128
129         ret = read_dentry(buf, metadata_len, dentry_offset, dentry);
130
131         /* This is the root dentry, so set its parent to itself. */
132         dentry->parent = dentry;
133
134         if (ret == 0 && dentry->length == 0) {
135                 ERROR("Metadata resource cannot begin with end-of-directory entry!");
136                 ret = WIMLIB_ERR_INVALID_DENTRY;
137         }
138
139         if (ret != 0) {
140                 FREE(dentry);
141                 goto out_free_security_data;
142         }
143
144         inode_add_dentry(dentry, dentry->d_inode);
145
146         /* Now read the entire directory entry tree into memory. */
147         DEBUG("Reading dentry tree");
148         ret = read_dentry_tree(buf, metadata_len, dentry);
149         if (ret != 0)
150                 goto out_free_dentry_tree;
151
152         /* Calculate the full paths in the dentry tree. */
153         DEBUG("Calculating dentry full paths");
154         ret = for_dentry_in_tree(dentry, calculate_dentry_full_path, NULL);
155         if (ret != 0)
156                 goto out_free_dentry_tree;
157
158         /* Build hash table that maps hard link group IDs to dentry sets */
159         ret = dentry_tree_fix_inodes(dentry, &inode_list);
160         if (ret != 0)
161                 goto out_free_dentry_tree;
162
163         if (!w->all_images_verified) {
164                 DEBUG("Running miscellaneous verifications on the dentry tree");
165                 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
166                 ret = for_dentry_in_tree(dentry, verify_dentry, w);
167                 if (ret != 0)
168                         goto out_free_dentry_tree;
169         }
170
171         DEBUG("Done reading image metadata");
172
173         imd->root_dentry = dentry;
174         imd->inode_list  = inode_list;
175         goto out_free_buf;
176 out_free_dentry_tree:
177         free_dentry_tree(dentry, NULL);
178 out_free_security_data:
179         free_security_data(imd->security_data);
180         imd->security_data = NULL;
181 out_free_buf:
182         FREE(buf);
183         return ret;
184 }
185
186 static void recalculate_security_data_length(struct wim_security_data *sd)
187 {
188         u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32);
189         for (u32 i = 0; i < sd->num_entries; i++)
190                 total_length += sd->sizes[i];
191         sd->total_length = total_length;
192 }
193
194 /* Like write_wim_resource(), but the resource is specified by a buffer of
195  * uncompressed data rather a lookup table entry; also writes the SHA1 hash of
196  * the buffer to @hash.  */
197 static int write_wim_resource_from_buffer(const u8 *buf, u64 buf_size,
198                                           FILE *out_fp, int out_ctype,
199                                           struct resource_entry *out_res_entry,
200                                           u8 hash[SHA1_HASH_SIZE])
201 {
202         /* Set up a temporary lookup table entry to provide to
203          * write_wim_resource(). */
204         struct lookup_table_entry lte;
205         int ret;
206         lte.resource_entry.flags         = 0;
207         lte.resource_entry.original_size = buf_size;
208         lte.resource_entry.size          = buf_size;
209         lte.resource_entry.offset        = 0;
210         lte.resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
211         lte.attached_buffer              = (u8*)buf;
212
213         zero_out_hash(lte.hash);
214         ret = write_wim_resource(&lte, out_fp, out_ctype, out_res_entry, 0);
215         if (ret != 0)
216                 return ret;
217         copy_hash(hash, lte.hash);
218         return 0;
219 }
220
221 /* Write the metadata resource for the current WIM image. */
222 int write_metadata_resource(WIMStruct *w)
223 {
224         u8 *buf;
225         u8 *p;
226         int ret;
227         u64 subdir_offset;
228         struct dentry *root;
229         struct lookup_table_entry *lte;
230         u64 metadata_original_size;
231         struct wim_security_data *sd;
232
233         DEBUG("Writing metadata resource for image %d (offset = %"PRIu64")",
234               w->current_image, ftello(w->out_fp));
235
236         root = wim_root_dentry(w);
237         sd = wim_security_data(w);
238
239         /* Offset of first child of the root dentry.  It's equal to:
240          * - The total length of the security data, rounded to the next 8-byte
241          *   boundary,
242          * - plus the total length of the root dentry,
243          * - plus 8 bytes for an end-of-directory entry following the root
244          *   dentry (shouldn't really be needed, but just in case...)
245          */
246         recalculate_security_data_length(sd);
247         subdir_offset = (((u64)sd->total_length + 7) & ~7) +
248                         dentry_correct_total_length(root) + 8;
249
250         /* Calculate the subdirectory offsets for the entire dentry tree. */
251         calculate_subdir_offsets(root, &subdir_offset);
252
253         /* Total length of the metadata resource (uncompressed) */
254         metadata_original_size = subdir_offset;
255
256         /* Allocate a buffer to contain the uncompressed metadata resource */
257         buf = MALLOC(metadata_original_size);
258         if (!buf) {
259                 ERROR("Failed to allocate %"PRIu64" bytes for "
260                       "metadata resource", metadata_original_size);
261                 return WIMLIB_ERR_NOMEM;
262         }
263
264         /* Write the security data into the resource buffer */
265         p = write_security_data(sd, buf);
266
267         /* Write the dentry tree into the resource buffer */
268         p = write_dentry_tree(root, p);
269
270         /* We MUST have exactly filled the buffer; otherwise we calculated its
271          * size incorrectly or wrote the data incorrectly. */
272         wimlib_assert(p - buf == metadata_original_size);
273
274         /* Get the lookup table entry for the metadata resource so we can update
275          * it. */
276         lte = w->image_metadata[w->current_image - 1].metadata_lte;
277
278         /* Write the metadata resource to the output WIM using the proper
279          * compression type.  The lookup table entry for the metadata resource
280          * is updated. */
281         ret = write_wim_resource_from_buffer(buf, metadata_original_size,
282                                              w->out_fp,
283                                              wimlib_get_compression_type(w),
284                                              &lte->output_resource_entry,
285                                              lte->hash);
286         if (ret != 0)
287                 goto out;
288
289         /* It's very likely the SHA1 message digest of the metadata resource
290          * changed, so re-insert the lookup table entry into the lookup table.
291          *
292          * We do not check for other lookup table entries having the same SHA1
293          * message digest.  It's possible for 2 absolutely identical images to
294          * be added, therefore causing 2 identical metadata resources to be in
295          * the WIM.  However, in this case, it's expected for 2 separate lookup
296          * table entries to be created, even though this doesn't make a whole
297          * lot of sense since they will share the same SHA1 message digest.
298          * */
299         lookup_table_unlink(w->lookup_table, lte);
300         lookup_table_insert(w->lookup_table, lte);
301         lte->out_refcnt = 1;
302
303         /* Make sure that the lookup table entry for this metadata resource is
304          * marked with the metadata flag. */
305         lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
306 out:
307         /* All the data has been written to the new WIM; no need for the buffer
308          * anymore */
309         FREE(buf);
310         return ret;
311 }