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