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