4 * Some functions to verify that stuff in the WIM is valid. Of course, not
5 * *all* the verifications of the input data are in this file.
9 * Copyright (C) 2012 Eric Biggers
11 * wimlib - Library for working with WIM files
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
31 #include "lookup_table.h"
33 static inline struct dentry *inode_first_dentry(struct inode *inode)
35 wimlib_assert(inode->dentry_list.next != &inode->dentry_list);
36 return container_of(inode->dentry_list.next, struct dentry,
40 static int verify_inode(struct inode *inode, const WIMStruct *w)
42 const struct lookup_table *table = w->lookup_table;
43 const struct wim_security_data *sd = wim_const_security_data(w);
44 const struct dentry *first_dentry = inode_first_dentry(inode);
45 int ret = WIMLIB_ERR_INVALID_DENTRY;
47 /* Check the security ID */
48 if (inode->security_id < -1) {
49 ERROR("Dentry `%s' has an invalid security ID (%d)",
50 first_dentry->full_path_utf8, inode->security_id);
53 if (inode->security_id >= sd->num_entries) {
54 ERROR("Dentry `%s' has an invalid security ID (%d) "
55 "(there are only %u entries in the security table)",
56 first_dentry->full_path_utf8, inode->security_id,
61 /* Check that lookup table entries for all the resources exist, except
62 * if the SHA1 message digest is all 0's, which indicates there is
63 * intentionally no resource there. */
64 if (w->hdr.total_parts == 1) {
65 for (unsigned i = 0; i <= inode->num_ads; i++) {
66 struct lookup_table_entry *lte;
68 hash = inode_stream_hash_unresolved(inode, i);
69 lte = __lookup_resource(table, hash);
70 if (!lte && !is_zero_hash(hash)) {
71 ERROR("Could not find lookup table entry for stream "
72 "%u of dentry `%s'", i, first_dentry->full_path_utf8);
76 lte->real_refcnt += inode->link_count;
78 /* The following is now done when required by
79 * wim_run_full_verifications(). */
82 if (lte && !w->full_verification_in_progress &&
83 lte->real_refcnt > lte->refcnt)
85 #ifdef ENABLE_ERROR_MESSAGES
86 WARNING("The following lookup table entry "
87 "has a reference count of %u, but",
89 WARNING("We found %u references to it",
91 WARNING("(One dentry referencing it is at `%s')",
92 first_dentry->full_path_utf8);
94 print_lookup_table_entry(lte);
96 /* Guess what! install.wim for Windows 8
97 * contains many streams referenced by more
98 * dentries than the refcnt stated in the lookup
99 * table entry. So we will need to handle this
100 * case and not just make it be an error... I'm
101 * just setting the reference count to the
102 * number of references we found.
103 * (Unfortunately, even after doing this, the
104 * reference count could be too low if it's also
105 * referenced in other WIM images) */
108 lte->refcnt = lte->real_refcnt;
109 WARNING("Fixing reference count");
118 /* Make sure there is only one un-named stream. */
119 unsigned num_unnamed_streams = 0;
120 for (unsigned i = 0; i <= inode->num_ads; i++) {
122 hash = inode_stream_hash_unresolved(inode, i);
123 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
124 num_unnamed_streams++;
126 if (num_unnamed_streams > 1) {
127 ERROR("Dentry `%s' has multiple (%u) un-named streams",
128 first_dentry->full_path_utf8, num_unnamed_streams);
131 inode->verified = true;
137 /* Run some miscellaneous verifications on a WIM dentry */
138 int verify_dentry(struct dentry *dentry, void *wim)
142 if (!dentry->d_inode->verified) {
143 ret = verify_inode(dentry->d_inode, wim);
148 /* Cannot have a short name but no long name */
149 if (dentry->short_name_len && !dentry->file_name_len) {
150 ERROR("Dentry `%s' has a short name but no long name",
151 dentry->full_path_utf8);
152 return WIMLIB_ERR_INVALID_DENTRY;
155 /* Make sure root dentry is unnamed */
156 if (dentry_is_root(dentry)) {
157 if (dentry->file_name_len) {
158 ERROR("The root dentry is named `%s', but it must "
159 "be unnamed", dentry->file_name_utf8);
160 return WIMLIB_ERR_INVALID_DENTRY;
165 /* Check timestamps */
166 if (inode->last_access_time < inode->creation_time ||
167 inode->last_write_time < inode->creation_time) {
168 WARNING("Dentry `%s' was created after it was last accessed or "
169 "written to", dentry->full_path_utf8);
176 static int image_run_full_verifications(WIMStruct *w)
178 return for_dentry_in_tree(wim_root_dentry(w), verify_dentry, w);
181 static int lte_fix_refcnt(struct lookup_table_entry *lte, void *ctr)
183 if (lte->refcnt != lte->real_refcnt) {
184 WARNING("The following lookup table entry has a reference "
185 "count of %u, but", lte->refcnt);
186 WARNING("We found %u references to it",
188 print_lookup_table_entry(lte);
189 lte->refcnt = lte->real_refcnt;
190 ++*(unsigned long *)ctr;
195 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
196 * screwed up because some lookup table entries are referenced more times than
197 * their stated reference counts. So theoretically, if we delete all the
198 * references to a stream and then remove it, it might still be referenced
199 * somewhere else, making a file be missing from the WIM... So, work around this
200 * problem by looking at ALL the images to re-calculate the reference count of
201 * EVERY lookup table entry. This only absolutely has to be done before an image
202 * is deleted or before an image is mounted read-write. */
203 int wim_run_full_verifications(WIMStruct *w)
207 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
208 w->all_images_verified = 1;
209 w->full_verification_in_progress = 1;
210 ret = for_image(w, WIMLIB_ALL_IMAGES, image_run_full_verifications);
211 w->full_verification_in_progress = 0;
213 unsigned long num_ltes_with_bogus_refcnt = 0;
214 for (int i = 0; i < w->hdr.image_count; i++)
215 w->image_metadata[i].metadata_lte->real_refcnt++;
216 for_lookup_table_entry(w->lookup_table, lte_fix_refcnt,
217 &num_ltes_with_bogus_refcnt);
218 if (num_ltes_with_bogus_refcnt != 0) {
219 WARNING("A total of %lu entries in the WIM's stream "
220 "lookup table had to have\n"
221 " their reference counts fixed.",
222 num_ltes_with_bogus_refcnt);
225 w->all_images_verified = 0;
231 * Sanity checks to make sure a set of WIMs correctly correspond to a spanned
238 * All parts of the set other than part 1.
240 * @num_additional_swms:
241 * Number of WIMStructs in @additional_swms. Or, the total number of parts
242 * in the set minus 1.
245 * 0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
247 int verify_swm_set(WIMStruct *w, WIMStruct **additional_swms,
248 unsigned num_additional_swms)
250 unsigned total_parts = w->hdr.total_parts;
254 if (total_parts != num_additional_swms + 1) {
255 ERROR("`%s' says there are %u parts in the spanned set, "
256 "but %s%u part%s provided",
257 w->filename, total_parts,
258 (num_additional_swms + 1 < total_parts) ? "only " : "",
259 num_additional_swms + 1,
260 (num_additional_swms) ? "s were" : " was");
261 return WIMLIB_ERR_SPLIT_INVALID;
263 if (w->hdr.part_number != 1) {
264 ERROR("WIM `%s' is not the first part of the split WIM.",
266 return WIMLIB_ERR_SPLIT_INVALID;
268 for (unsigned i = 0; i < num_additional_swms; i++) {
269 if (additional_swms[i]->hdr.total_parts != total_parts) {
270 ERROR("WIM `%s' says there are %u parts in the spanned set, "
271 "but %u parts were provided",
272 additional_swms[i]->filename,
273 additional_swms[i]->hdr.total_parts,
275 return WIMLIB_ERR_SPLIT_INVALID;
279 /* keep track of ctype and guid just to make sure they are the same for
281 ctype = wimlib_get_compression_type(w);
284 WIMStruct *parts_to_swms[num_additional_swms];
285 ZERO_ARRAY(parts_to_swms);
286 for (unsigned i = 0; i < num_additional_swms; i++) {
288 WIMStruct *swm = additional_swms[i];
290 if (wimlib_get_compression_type(swm) != ctype) {
291 ERROR("The split WIMs do not all have the same "
293 return WIMLIB_ERR_SPLIT_INVALID;
295 if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
296 ERROR("The split WIMs do not all have the same "
298 return WIMLIB_ERR_SPLIT_INVALID;
300 if (swm->hdr.part_number == 1) {
301 ERROR("WIMs `%s' and `%s' both are marked as the "
302 "first WIM in the spanned set",
303 w->filename, swm->filename);
304 return WIMLIB_ERR_SPLIT_INVALID;
306 if (swm->hdr.part_number == 0 ||
307 swm->hdr.part_number > total_parts)
309 ERROR("WIM `%s' says it is part %u in the spanned set, "
310 "but the part number must be in the range "
312 swm->filename, swm->hdr.part_number, total_parts);
313 return WIMLIB_ERR_SPLIT_INVALID;
315 if (parts_to_swms[swm->hdr.part_number - 2])
317 ERROR("`%s' and `%s' are both marked as part %u of %u "
318 "in the spanned set",
319 parts_to_swms[swm->hdr.part_number - 2]->filename,
321 swm->hdr.part_number,
323 return WIMLIB_ERR_SPLIT_INVALID;
325 parts_to_swms[swm->hdr.part_number - 2] = swm;