6 * Copyright (C) 2012, 2013 Eric Biggers
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
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
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/.
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"
35 * Reads and parses a metadata resource for an image in the WIM file.
38 * Pointer to the WIMStruct for the WIM file.
41 * Pointer to the image metadata structure for the image whose metadata
42 * resource we are reading. Its `metadata_lte' member specifies the lookup
43 * table entry for the metadata resource. The rest of the image metadata
44 * entry will be filled in by this function.
47 * WIMLIB_ERR_SUCCESS (0)
48 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
51 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
52 * WIMLIB_ERR_DECOMPRESSION
55 read_metadata_resource(WIMStruct *wim, struct wim_image_metadata *imd)
57 const struct wim_lookup_table_entry *metadata_lte;
60 struct wim_security_data *sd;
61 struct wim_dentry *root;
62 struct wim_inode *inode;
64 metadata_lte = imd->metadata_lte;
66 DEBUG("Reading metadata resource (size=%"PRIu64").", metadata_lte->size);
68 /* Read the metadata resource into memory. (It may be compressed.) */
69 ret = read_full_stream_into_alloc_buf(metadata_lte, &buf);
73 /* Checksum the metadata resource. */
74 if (!metadata_lte->dont_check_metadata_hash) {
75 u8 hash[SHA1_HASH_SIZE];
77 sha1_buffer(buf, metadata_lte->size, hash);
78 if (!hashes_equal(metadata_lte->hash, hash)) {
79 ERROR("Metadata resource is corrupted "
80 "(invalid SHA-1 message digest)!");
81 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
86 /* Parse the metadata resource.
88 * Notes: The metadata resource consists of the security data, followed
89 * by the directory entry for the root directory, followed by all the
90 * other directory entries in the filesystem. The subdir_offset field
91 * of each directory entry gives the start of its child entries from the
92 * beginning of the metadata resource. An end-of-directory is signaled
93 * by a directory entry of length '0', really of length 8, because
94 * that's how long the 'length' field is. */
96 ret = read_wim_security_data(buf, metadata_lte->size, &sd);
100 ret = read_dentry_tree(buf, metadata_lte->size, sd->total_length, &root);
102 goto out_free_security_data;
104 /* We have everything we need from the buffer now. */
108 /* Calculate and validate inodes. */
110 ret = dentry_tree_fix_inodes(root, &imd->inode_list);
112 goto out_free_dentry_tree;
114 image_for_each_inode(inode, imd)
115 check_inode(inode, sd);
117 /* Success; fill in the image_metadata structure. */
118 imd->root_dentry = root;
119 imd->security_data = sd;
120 INIT_LIST_HEAD(&imd->unhashed_streams);
121 DEBUG("Done parsing metadata resource.");
124 out_free_dentry_tree:
125 free_dentry_tree(root, NULL);
126 out_free_security_data:
127 free_wim_security_data(sd);
134 recalculate_security_data_length(struct wim_security_data *sd)
136 u32 total_length = sizeof(u64) * sd->num_entries + 2 * sizeof(u32);
137 for (u32 i = 0; i < sd->num_entries; i++)
138 total_length += sd->sizes[i];
139 sd->total_length = (total_length + 7) & ~7;
143 prepare_metadata_resource(WIMStruct *wim, int image,
144 u8 **buf_ret, size_t *len_ret)
150 struct wim_dentry *root;
152 struct wim_security_data *sd;
153 struct wim_image_metadata *imd;
155 DEBUG("Preparing metadata resource for image %d", image);
157 ret = select_wim_image(wim, image);
161 imd = wim->image_metadata[image - 1];
163 root = imd->root_dentry;
164 sd = imd->security_data;
167 /* Empty image; create a dummy root. */
168 ret = new_filler_directory(&root);
171 imd->root_dentry = root;
174 /* Offset of first child of the root dentry. It's equal to:
175 * - The total length of the security data, rounded to the next 8-byte
177 * - plus the total length of the root dentry,
178 * - plus 8 bytes for an end-of-directory entry following the root
179 * dentry (shouldn't really be needed, but just in case...)
181 recalculate_security_data_length(sd);
182 subdir_offset = (((u64)sd->total_length + 7) & ~7) +
183 dentry_out_total_length(root) + 8;
185 /* Calculate the subdirectory offsets for the entire dentry tree. */
186 calculate_subdir_offsets(root, &subdir_offset);
188 /* Total length of the metadata resource (uncompressed). */
191 /* Allocate a buffer to contain the uncompressed metadata resource. */
193 if (likely(len == subdir_offset))
196 ERROR("Failed to allocate %"PRIu64" bytes for "
197 "metadata resource", subdir_offset);
198 return WIMLIB_ERR_NOMEM;
201 /* Write the security data into the resource buffer. */
202 p = write_wim_security_data(sd, buf);
204 /* Write the dentry tree into the resource buffer. */
205 p = write_dentry_tree(root, p);
207 /* We MUST have exactly filled the buffer; otherwise we calculated its
208 * size incorrectly or wrote the data incorrectly. */
209 wimlib_assert(p - buf == len);
217 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags)
222 struct wim_image_metadata *imd;
224 ret = prepare_metadata_resource(wim, image, &buf, &len);
228 imd = wim->image_metadata[image - 1];
230 /* Write the metadata resource to the output WIM using the proper
231 * compression type, in the process updating the lookup table entry for
232 * the metadata resource. */
233 ret = write_wim_resource_from_buffer(buf, len, WIM_RESHDR_FLAG_METADATA,
235 wim->out_compression_type,
237 &imd->metadata_lte->out_reshdr,
238 imd->metadata_lte->hash,
239 write_resource_flags);
241 /* Original checksum was overridden; set a flag so it isn't used. */
242 imd->metadata_lte->dont_check_metadata_hash = 1;