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, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/dentry.h"
32 #include "wimlib/error.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
35 #include "wimlib/security.h"
38 verify_inode(struct wim_inode *inode, const WIMStruct *w)
40 const struct wim_lookup_table *table = w->lookup_table;
41 const struct wim_security_data *sd = wim_const_security_data(w);
42 struct wim_dentry *first_dentry = inode_first_dentry(inode);
43 struct wim_dentry *dentry;
45 /* Check the security ID. -1 is valid and means "no security
46 * descriptor". Anything else has to be a valid index into the WIM
47 * image's security descriptors table. */
48 if (inode->i_security_id < -1) {
49 ERROR("Dentry `%"TS"' has an invalid security ID (%d)",
50 dentry_full_path(first_dentry), inode->i_security_id);
51 return WIMLIB_ERR_INVALID_DENTRY;
54 if (inode->i_security_id >= 0 &&
55 inode->i_security_id >= sd->num_entries)
57 ERROR("Dentry `%"TS"' has an invalid security ID (%d) "
58 "(there are only %u entries in the security table)",
59 dentry_full_path(first_dentry), inode->i_security_id,
61 return WIMLIB_ERR_INVALID_DENTRY;
64 /* Check that lookup table entries for all the inode's stream exist,
65 * except if the SHA1 message digest is all 0's, which indicates an
68 * This check is skipped on split WIMs. */
69 if (w->hdr.total_parts == 1) {
70 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
71 struct wim_lookup_table_entry *lte;
73 hash = inode_stream_hash_unresolved(inode, i);
74 lte = __lookup_resource(table, hash);
75 if (!lte && !is_zero_hash(hash)) {
76 ERROR("Could not find lookup table entry for stream "
77 "%u of dentry `%"TS"'",
78 i, dentry_full_path(first_dentry));
79 return WIMLIB_ERR_INVALID_DENTRY;
82 lte->real_refcnt += inode->i_nlink;
86 /* Make sure there is only one unnamed data stream. */
87 unsigned num_unnamed_streams = 0;
88 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
90 hash = inode_stream_hash_unresolved(inode, i);
91 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
92 num_unnamed_streams++;
94 if (num_unnamed_streams > 1) {
95 ERROR("Dentry `%"TS"' has multiple (%u) un-named streams",
96 dentry_full_path(first_dentry), num_unnamed_streams);
97 return WIMLIB_ERR_INVALID_DENTRY;
100 /* Files cannot have multiple DOS names, even if they have multiple
101 * names in multiple directories (i.e. hard links).
102 * Source: NTFS-3g authors. */
103 struct wim_dentry *dentry_with_dos_name = NULL;
104 inode_for_each_dentry(dentry, inode) {
105 if (dentry_has_short_name(dentry)) {
106 if (dentry_with_dos_name) {
107 /* This was previously an error, but if we
108 * capture a WIM from UDF on Windows, hard links
109 * are supported but DOS names are automatically
110 * generated for all names for an inode. */
112 ERROR("Hard-linked file has a DOS name at "
113 "both `%"TS"' and `%"TS"'",
114 dentry_full_path(dentry_with_dos_name),
115 dentry_full_path(dentry));
116 return WIMLIB_ERR_INVALID_DENTRY;
118 dentry->dos_name_invalid = 1;
121 dentry_with_dos_name = dentry;
125 /* Directories with multiple links have not been tested. XXX */
126 if (inode->i_nlink > 1 && inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
127 ERROR("Hard-linked directory `%"TS"' is unsupported",
128 dentry_full_path(first_dentry));
129 return WIMLIB_ERR_INVALID_DENTRY;
132 inode->i_verified = 1;
136 /* Run some miscellaneous verifications on a WIM dentry */
138 verify_dentry(struct wim_dentry *dentry, void *wim)
143 /* Verify the associated inode, but only one time no matter how many
144 * dentries it has (unless we are doing a full verification of the WIM,
145 * in which case we need to force the inode to be verified again.) */
146 if (!dentry->d_inode->i_verified) {
147 ret = verify_inode(dentry->d_inode, w);
152 /* Make sure root dentry is unnamed, while every other dentry has at
155 * I am assuming that dentries having only a DOS name is illegal; i.e.,
156 * Windows will always combine the Win32 name and DOS name for a file
157 * into a single WIM dentry, even if they are stored separately on NTFS.
158 * (This seems to be the case...) */
159 if (dentry_is_root(dentry)) {
160 if (dentry_has_long_name(dentry) || dentry_has_short_name(dentry)) {
161 ERROR("The root dentry has a nonempty name!");
162 return WIMLIB_ERR_INVALID_DENTRY;
165 if (!dentry_has_long_name(dentry)) {
166 ERROR("Dentry `%"TS"' has no long name!",
167 dentry_full_path(dentry));
168 return WIMLIB_ERR_INVALID_DENTRY;
173 /* Check timestamps */
174 if (inode->i_last_access_time < inode->i_creation_time ||
175 inode->i_last_write_time < inode->i_creation_time) {
176 WARNING("Dentry `%"TS"' was created after it was last accessed or "
177 "written to", dentry->full_path);
185 image_run_full_verifications(WIMStruct *w)
187 struct wim_image_metadata *imd;
188 struct wim_inode *inode;
190 imd = wim_get_current_image_metadata(w);
191 image_for_each_inode(inode, imd)
192 inode->i_verified = 0;
193 return for_dentry_in_tree(imd->root_dentry, verify_dentry, w);
197 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
199 if (lte->refcnt != lte->real_refcnt) {
200 #ifdef ENABLE_ERROR_MESSAGES
201 WARNING("The following lookup table entry has a reference "
202 "count of %u, but", lte->refcnt);
203 WARNING("We found %u references to it",
205 print_lookup_table_entry(lte, stderr);
207 lte->refcnt = lte->real_refcnt;
208 ++*(unsigned long *)ctr;
213 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
214 * screwed up because some lookup table entries are referenced more times than
215 * their stated reference counts. So theoretically, if we delete all the
216 * references to a stream and then remove it, it might still be referenced
217 * somewhere else, making a file be missing from the WIM... So, work around this
218 * problem by looking at ALL the images to re-calculate the reference count of
219 * EVERY lookup table entry. This only absolutely has to be done before an image
220 * is deleted or before an image is mounted read-write. */
222 wim_run_full_verifications(WIMStruct *w)
226 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
228 w->all_images_verified = 1; /* Set *before* image_run_full_verifications,
229 because of check in read_metadata_resource() */
230 ret = for_image(w, WIMLIB_ALL_IMAGES, image_run_full_verifications);
232 unsigned long num_ltes_with_bogus_refcnt = 0;
233 for_lookup_table_entry(w->lookup_table, lte_fix_refcnt,
234 &num_ltes_with_bogus_refcnt);
235 if (num_ltes_with_bogus_refcnt != 0) {
236 WARNING("A total of %lu entries in the WIM's stream "
237 "lookup table had to have\n"
238 " their reference counts fixed.",
239 num_ltes_with_bogus_refcnt);
242 w->all_images_verified = 0;