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