]> wimlib.net Git - wimlib/blob - src/metadata_resource.c
metadata_resource.c
[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         struct inode_table inode_tab;
52         const struct lookup_table_entry *metadata_lte;
53         u64 metadata_len;
54         struct hlist_head inode_list;
55
56         metadata_lte = imd->metadata_lte;
57         metadata_len = wim_resource_size(metadata_lte);
58
59         DEBUG("Reading metadata resource: length = %"PRIu64", "
60               "offset = %"PRIu64"", metadata_len,
61               metadata_lte->resource_entry.offset);
62
63         /* There is no way the metadata resource could possibly be less than (8
64          * + WIM_DENTRY_DISK_SIZE) bytes, where the 8 is for security data (with
65          * no security descriptors) and WIM_DENTRY_DISK_SIZE is for the root
66          * dentry. */
67         if (metadata_len < 8 + WIM_DENTRY_DISK_SIZE) {
68                 ERROR("Expected at least %u bytes for the metadata resource",
69                       8 + WIM_DENTRY_DISK_SIZE);
70                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
71         }
72
73         if (sizeof(size_t) < 8 && metadata_len > 0xffffffff) {
74                 ERROR("Metadata resource is too large (%"PRIu64" bytes",
75                       metadata_len);
76                 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
77         }
78
79         /* Allocate memory for the uncompressed metadata resource. */
80         buf = MALLOC(metadata_len);
81
82         if (!buf) {
83                 ERROR("Failed to allocate %"PRIu64" bytes for uncompressed "
84                       "metadata resource", metadata_len);
85                 return WIMLIB_ERR_NOMEM;
86         }
87
88         /* Read the metadata resource into memory.  (It may be compressed.) */
89         ret = read_full_wim_resource(metadata_lte, buf, 0);
90         if (ret != 0)
91                 goto out_free_buf;
92
93         DEBUG("Finished reading metadata resource into memory.");
94
95         /* The root directory entry starts after security data, aligned on an
96          * 8-byte boundary within the metadata resource.
97          *
98          * The security data starts with a 4-byte integer giving its total
99          * length, so if we round that up to an 8-byte boundary that gives us
100          * the offset of the root dentry.
101          *
102          * Here we read the security data into a wim_security_data structure,
103          * and if successful, go ahead and calculate the offset in the metadata
104          * resource of the root dentry. */
105
106         wimlib_assert(imd->security_data == NULL);
107         ret = read_security_data(buf, metadata_len, &imd->security_data);
108         if (ret != 0)
109                 goto out_free_buf;
110
111         dentry_offset = (imd->security_data->total_length + 7) & ~7;
112
113         if (dentry_offset == 0) {
114                 ERROR("Integer overflow while reading metadata resource");
115                 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
116                 goto out_free_security_data;
117         }
118
119         DEBUG("Reading root dentry");
120
121         /* Allocate memory for the root dentry and read it into memory */
122         dentry = MALLOC(sizeof(struct dentry));
123         if (!dentry) {
124                 ERROR("Failed to allocate %zu bytes for root dentry",
125                       sizeof(struct dentry));
126                 ret = WIMLIB_ERR_NOMEM;
127                 goto out_free_security_data;
128         }
129
130         ret = read_dentry(buf, metadata_len, dentry_offset, dentry);
131
132         /* This is the root dentry, so set its parent to itself. */
133         dentry->parent = dentry;
134
135         if (ret == 0 && dentry->length == 0) {
136                 ERROR("Metadata resource cannot begin with end-of-directory entry!");
137                 ret = WIMLIB_ERR_INVALID_DENTRY;
138         }
139
140         if (ret != 0) {
141                 FREE(dentry);
142                 goto out_free_security_data;
143         }
144
145         inode_add_dentry(dentry, dentry->d_inode);
146
147         /* Now read the entire directory entry tree into memory. */
148         DEBUG("Reading dentry tree");
149         ret = read_dentry_tree(buf, metadata_len, dentry);
150         if (ret != 0)
151                 goto out_free_dentry_tree;
152
153         /* Calculate the full paths in the dentry tree. */
154         DEBUG("Calculating dentry full paths");
155         ret = for_dentry_in_tree(dentry, calculate_dentry_full_path, NULL);
156         if (ret != 0)
157                 goto out_free_dentry_tree;
158
159         /* Build hash table that maps hard link group IDs to dentry sets */
160         DEBUG("Building link group table");
161         ret = init_inode_table(&inode_tab, 9001);
162         if (ret != 0)
163                 goto out_free_dentry_tree;
164
165         for_dentry_in_tree(dentry, inode_table_insert, &inode_tab);
166
167         DEBUG("Fixing inconsistencies in the hard link groups");
168         ret = fix_inodes(&inode_tab, &inode_list);
169         destroy_inode_table(&inode_tab);
170         if (ret != 0)
171                 goto out_free_dentry_tree;
172
173         if (!w->all_images_verified) {
174                 DEBUG("Running miscellaneous verifications on the dentry tree");
175                 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
176                 ret = for_dentry_in_tree(dentry, verify_dentry, w);
177                 if (ret != 0)
178                         goto out_free_dentry_tree;
179         }
180
181         DEBUG("Done reading image metadata");
182
183         imd->root_dentry = dentry;
184         imd->inode_list  = inode_list;
185         goto out_free_buf;
186 out_free_dentry_tree:
187         free_dentry_tree(dentry, NULL);
188 out_free_security_data:
189         free_security_data(imd->security_data);
190         imd->security_data = NULL;
191 out_free_buf:
192         FREE(buf);
193         return ret;
194 }
195
196 static void recalculate_security_data_length(struct wim_security_data *sd)
197 {
198         u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32);
199         for (u32 i = 0; i < sd->num_entries; i++)
200                 total_length += sd->sizes[i];
201         sd->total_length = total_length;
202 }
203
204 /* Like write_wim_resource(), but the resource is specified by a buffer of
205  * uncompressed data rather a lookup table entry; also writes the SHA1 hash of
206  * the buffer to @hash.  */
207 static int write_wim_resource_from_buffer(const u8 *buf, u64 buf_size,
208                                           FILE *out_fp, int out_ctype,
209                                           struct resource_entry *out_res_entry,
210                                           u8 hash[SHA1_HASH_SIZE])
211 {
212         /* Set up a temporary lookup table entry to provide to
213          * write_wim_resource(). */
214         struct lookup_table_entry lte;
215         int ret;
216         lte.resource_entry.flags         = 0;
217         lte.resource_entry.original_size = buf_size;
218         lte.resource_entry.size          = buf_size;
219         lte.resource_entry.offset        = 0;
220         lte.resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
221         lte.attached_buffer              = (u8*)buf;
222
223         zero_out_hash(lte.hash);
224         ret = write_wim_resource(&lte, out_fp, out_ctype, out_res_entry, 0);
225         if (ret != 0)
226                 return ret;
227         copy_hash(hash, lte.hash);
228         return 0;
229 }
230
231 /* Write the metadata resource for the current WIM image. */
232 int write_metadata_resource(WIMStruct *w)
233 {
234         u8 *buf;
235         u8 *p;
236         int ret;
237         u64 subdir_offset;
238         struct dentry *root;
239         struct lookup_table_entry *lte;
240         u64 metadata_original_size;
241         struct wim_security_data *sd;
242
243         DEBUG("Writing metadata resource for image %d (offset = %"PRIu64")",
244               w->current_image, ftello(w->out_fp));
245
246         root = wim_root_dentry(w);
247         sd = wim_security_data(w);
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 = w->image_metadata[w->current_image - 1].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_fp,
293                                              wimlib_get_compression_type(w),
294                                              &lte->output_resource_entry,
295                                              lte->hash);
296         if (ret != 0)
297                 goto out;
298
299         /* It's very likely the SHA1 message digest of the metadata resource
300          * changed, so re-insert the lookup table entry into the lookup table.
301          *
302          * We do not check for other lookup table entries having the same SHA1
303          * message digest.  It's possible for 2 absolutely identical images to
304          * be added, therefore causing 2 identical metadata resources to be in
305          * the WIM.  However, in this case, it's expected for 2 separate lookup
306          * table entries to be created, even though this doesn't make a whole
307          * lot of sense since they will share the same SHA1 message digest.
308          * */
309         lookup_table_unlink(w->lookup_table, lte);
310         lookup_table_insert(w->lookup_table, lte);
311         lte->out_refcnt = 1;
312
313         /* Make sure that the lookup table entry for this metadata resource is
314          * marked with the metadata flag. */
315         lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
316 out:
317         /* All the data has been written to the new WIM; no need for the buffer
318          * anymore */
319         FREE(buf);
320         return ret;
321 }