]> wimlib.net Git - wimlib/blob - src/metadata_resource.c
Remove unused 'wim' argument to read_metadata_resource()
[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 free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include "wimlib/dentry.h"
27 #include "wimlib/error.h"
28 #include "wimlib/lookup_table.h"
29 #include "wimlib/metadata.h"
30 #include "wimlib/resource.h"
31 #include "wimlib/security.h"
32 #include "wimlib/write.h"
33
34 /*
35  * Reads and parses a metadata resource for an image in the WIM file.
36  *
37  * @imd:
38  *      Pointer to the image metadata structure for the image whose metadata
39  *      resource we are reading.  Its `metadata_lte' member specifies the lookup
40  *      table entry for the metadata resource.  The rest of the image metadata
41  *      entry will be filled in by this function.
42  *
43  * Return values:
44  *      WIMLIB_ERR_SUCCESS (0)
45  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
46  *      WIMLIB_ERR_NOMEM
47  *      WIMLIB_ERR_READ
48  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE
49  *      WIMLIB_ERR_DECOMPRESSION
50  */
51 int
52 read_metadata_resource(struct wim_image_metadata *imd)
53 {
54         const struct wim_lookup_table_entry *metadata_lte;
55         void *buf;
56         int ret;
57         struct wim_security_data *sd;
58         struct wim_dentry *root;
59         struct wim_inode *inode;
60
61         metadata_lte = imd->metadata_lte;
62
63         DEBUG("Reading metadata resource (size=%"PRIu64").", metadata_lte->size);
64
65         /* Read the metadata resource into memory.  (It may be compressed.)  */
66         ret = read_full_stream_into_alloc_buf(metadata_lte, &buf);
67         if (ret)
68                 return ret;
69
70         /* Checksum the metadata resource.  */
71         if (!metadata_lte->dont_check_metadata_hash) {
72                 u8 hash[SHA1_HASH_SIZE];
73
74                 sha1_buffer(buf, metadata_lte->size, hash);
75                 if (!hashes_equal(metadata_lte->hash, hash)) {
76                         ERROR("Metadata resource is corrupted "
77                               "(invalid SHA-1 message digest)!");
78                         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
79                         goto out_free_buf;
80                 }
81         }
82
83         /* Parse the metadata resource.
84          *
85          * Notes: The metadata resource consists of the security data, followed
86          * by the directory entry for the root directory, followed by all the
87          * other directory entries in the filesystem.  The subdir_offset field
88          * of each directory entry gives the start of its child entries from the
89          * beginning of the metadata resource.  An end-of-directory is signaled
90          * by a directory entry of length '0', really of length 8, because
91          * that's how long the 'length' field is.  */
92
93         ret = read_wim_security_data(buf, metadata_lte->size, &sd);
94         if (ret)
95                 goto out_free_buf;
96
97         ret = read_dentry_tree(buf, metadata_lte->size, sd->total_length, &root);
98         if (ret)
99                 goto out_free_security_data;
100
101         /* We have everything we need from the buffer now.  */
102         FREE(buf);
103         buf = NULL;
104
105         /* Calculate and validate inodes.  */
106
107         ret = dentry_tree_fix_inodes(root, &imd->inode_list);
108         if (ret)
109                 goto out_free_dentry_tree;
110
111         image_for_each_inode(inode, imd)
112                 check_inode(inode, sd);
113
114         /* Success; fill in the image_metadata structure.  */
115         imd->root_dentry = root;
116         imd->security_data = sd;
117         INIT_LIST_HEAD(&imd->unhashed_streams);
118         DEBUG("Done parsing metadata resource.");
119         return 0;
120
121 out_free_dentry_tree:
122         free_dentry_tree(root, NULL);
123 out_free_security_data:
124         free_wim_security_data(sd);
125 out_free_buf:
126         FREE(buf);
127         return ret;
128 }
129
130 static void
131 recalculate_security_data_length(struct wim_security_data *sd)
132 {
133         u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32);
134         for (u32 i = 0; i < sd->num_entries; i++)
135                 total_length += sd->sizes[i];
136         sd->total_length = (total_length + 7) & ~7;
137 }
138
139 static int
140 prepare_metadata_resource(WIMStruct *wim, int image,
141                           u8 **buf_ret, size_t *len_ret)
142 {
143         u8 *buf;
144         u8 *p;
145         int ret;
146         u64 subdir_offset;
147         struct wim_dentry *root;
148         size_t len;
149         struct wim_security_data *sd;
150         struct wim_image_metadata *imd;
151
152         DEBUG("Preparing metadata resource for image %d", image);
153
154         ret = select_wim_image(wim, image);
155         if (ret)
156                 return ret;
157
158         imd = wim->image_metadata[image - 1];
159
160         root = imd->root_dentry;
161         sd = imd->security_data;
162
163         if (!root) {
164                 /* Empty image; create a dummy root.  */
165                 ret = new_filler_directory(&root);
166                 if (ret)
167                         return ret;
168                 imd->root_dentry = root;
169         }
170
171         /* Offset of first child of the root dentry.  It's equal to:
172          * - The total length of the security data, rounded to the next 8-byte
173          *   boundary,
174          * - plus the total length of the root dentry,
175          * - plus 8 bytes for an end-of-directory entry following the root
176          *   dentry (shouldn't really be needed, but just in case...)
177          */
178         recalculate_security_data_length(sd);
179         subdir_offset = (((u64)sd->total_length + 7) & ~7) +
180                         dentry_out_total_length(root) + 8;
181
182         /* Calculate the subdirectory offsets for the entire dentry tree.  */
183         calculate_subdir_offsets(root, &subdir_offset);
184
185         /* Total length of the metadata resource (uncompressed).  */
186         len = subdir_offset;
187
188         /* Allocate a buffer to contain the uncompressed metadata resource.  */
189         buf = NULL;
190         if (likely(len == subdir_offset))
191                 buf = MALLOC(len);
192         if (!buf) {
193                 ERROR("Failed to allocate %"PRIu64" bytes for "
194                       "metadata resource", subdir_offset);
195                 return WIMLIB_ERR_NOMEM;
196         }
197
198         /* Write the security data into the resource buffer.  */
199         p = write_wim_security_data(sd, buf);
200
201         /* Write the dentry tree into the resource buffer.  */
202         p = write_dentry_tree(root, p);
203
204         /* We MUST have exactly filled the buffer; otherwise we calculated its
205          * size incorrectly or wrote the data incorrectly.  */
206         wimlib_assert(p - buf == len);
207
208         *buf_ret = buf;
209         *len_ret = len;
210         return 0;
211 }
212
213 int
214 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags)
215 {
216         int ret;
217         u8 *buf;
218         size_t len;
219         struct wim_image_metadata *imd;
220
221         ret = prepare_metadata_resource(wim, image, &buf, &len);
222         if (ret)
223                 return ret;
224
225         imd = wim->image_metadata[image - 1];
226
227         /* Write the metadata resource to the output WIM using the proper
228          * compression type, in the process updating the lookup table entry for
229          * the metadata resource.  */
230         ret = write_wim_resource_from_buffer(buf, len, WIM_RESHDR_FLAG_METADATA,
231                                              &wim->out_fd,
232                                              wim->out_compression_type,
233                                              wim->out_chunk_size,
234                                              &imd->metadata_lte->out_reshdr,
235                                              imd->metadata_lte->hash,
236                                              write_resource_flags);
237
238         /* Original checksum was overridden; set a flag so it isn't used.  */
239         imd->metadata_lte->dont_check_metadata_hash = 1;
240
241         FREE(buf);
242         return ret;
243 }