]> wimlib.net Git - wimlib/blob - src/verify.c
Refactor SWM code into swm.c
[wimlib] / src / verify.c
1 /*
2  * verify.c
3  *
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.
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
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)
16  * any later version.
17  *
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
21  * details.
22  *
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/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
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"
36
37 static int
38 verify_inode(struct wim_inode *inode, const WIMStruct *w)
39 {
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;
44         int ret = WIMLIB_ERR_INVALID_DENTRY;
45
46         /* Check the security ID.  -1 is valid and means "no security
47          * descriptor".  Anything else has to be a valid index into the WIM
48          * image's security descriptors table. */
49         if (inode->i_security_id < -1) {
50                 ERROR("Dentry `%"TS"' has an invalid security ID (%d)",
51                       dentry_full_path(first_dentry), inode->i_security_id);
52                 goto out;
53         }
54
55         if (inode->i_security_id >= sd->num_entries) {
56                 ERROR("Dentry `%"TS"' has an invalid security ID (%d) "
57                       "(there are only %u entries in the security table)",
58                       dentry_full_path(first_dentry), inode->i_security_id,
59                       sd->num_entries);
60                 goto out;
61         }
62
63         /* Check that lookup table entries for all the inode's stream exist,
64          * except if the SHA1 message digest is all 0's, which indicates an
65          * empty stream.
66          *
67          * This check is skipped on split WIMs. */
68         if (w->hdr.total_parts == 1) {
69                 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
70                         struct wim_lookup_table_entry *lte;
71                         const u8 *hash;
72                         hash = inode_stream_hash_unresolved(inode, i);
73                         lte = __lookup_resource(table, hash);
74                         if (!lte && !is_zero_hash(hash)) {
75                                 ERROR("Could not find lookup table entry for stream "
76                                       "%u of dentry `%"TS"'",
77                                       i, dentry_full_path(first_dentry));
78                                 goto out;
79                         }
80                         if (lte)
81                                 lte->real_refcnt += inode->i_nlink;
82                 }
83         }
84
85         /* Make sure there is only one unnamed data stream. */
86         unsigned num_unnamed_streams = 0;
87         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
88                 const u8 *hash;
89                 hash = inode_stream_hash_unresolved(inode, i);
90                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
91                         num_unnamed_streams++;
92         }
93         if (num_unnamed_streams > 1) {
94                 ERROR("Dentry `%"TS"' has multiple (%u) un-named streams",
95                       dentry_full_path(first_dentry), num_unnamed_streams);
96                 goto out;
97         }
98
99         /* Files cannot have multiple DOS names, even if they have multiple
100          * names in multiple directories (i.e. hard links).
101          * Source: NTFS-3g authors. */
102         struct wim_dentry *dentry_with_dos_name = NULL;
103         inode_for_each_dentry(dentry, inode) {
104                 if (dentry_has_short_name(dentry)) {
105                         if (dentry_with_dos_name) {
106                                 ERROR("Hard-linked file has a DOS name at "
107                                       "both `%"TS"' and `%"TS"'",
108                                       dentry_full_path(dentry_with_dos_name),
109                                       dentry_full_path(dentry));
110                                 goto out;
111                         }
112                         dentry_with_dos_name = dentry;
113                 }
114         }
115
116         /* Directories with multiple links have not been tested. XXX */
117         if (inode->i_nlink > 1 && inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
118                 ERROR("Hard-linked directory `%"TS"' is unsupported",
119                       dentry_full_path(first_dentry));
120                 goto out;
121         }
122
123         inode->i_verified = 1;
124         ret = 0;
125 out:
126         return ret;
127 }
128
129 /* Run some miscellaneous verifications on a WIM dentry */
130 int
131 verify_dentry(struct wim_dentry *dentry, void *wim)
132 {
133         int ret;
134         WIMStruct *w = wim;
135
136         /* Verify the associated inode, but only one time no matter how many
137          * dentries it has (unless we are doing a full verification of the WIM,
138          * in which case we need to force the inode to be verified again.) */
139         if (!dentry->d_inode->i_verified) {
140                 ret = verify_inode(dentry->d_inode, w);
141                 if (ret != 0)
142                         return ret;
143         }
144
145         /* Make sure root dentry is unnamed, while every other dentry has at
146          * least a long name.
147          *
148          * I am assuming that dentries having only a DOS name is illegal; i.e.,
149          * Windows will always combine the Win32 name and DOS name for a file
150          * into a single WIM dentry, even if they are stored separately on NTFS.
151          * (This seems to be the case...) */
152         if (dentry_is_root(dentry)) {
153                 if (dentry_has_long_name(dentry) || dentry_has_short_name(dentry)) {
154                         ERROR("The root dentry has a nonempty name!");
155                         return WIMLIB_ERR_INVALID_DENTRY;
156                 }
157         } else {
158                 if (!dentry_has_long_name(dentry)) {
159                         ERROR("Dentry `%"TS"' has no long name!",
160                               dentry_full_path(dentry));
161                         return WIMLIB_ERR_INVALID_DENTRY;
162                 }
163         }
164
165 #if 0
166         /* Check timestamps */
167         if (inode->i_last_access_time < inode->i_creation_time ||
168             inode->i_last_write_time < inode->i_creation_time) {
169                 WARNING("Dentry `%"TS"' was created after it was last accessed or "
170                         "written to", dentry->full_path);
171         }
172 #endif
173
174         return 0;
175 }
176
177 static int
178 image_run_full_verifications(WIMStruct *w)
179 {
180         struct wim_image_metadata *imd;
181         struct wim_inode *inode;
182
183         imd = wim_get_current_image_metadata(w);
184         image_for_each_inode(inode, imd)
185                 inode->i_verified = 0;
186         return for_dentry_in_tree(imd->root_dentry, verify_dentry, w);
187 }
188
189 static int
190 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
191 {
192         if (lte->refcnt != lte->real_refcnt) {
193         #ifdef ENABLE_ERROR_MESSAGES
194                 WARNING("The following lookup table entry has a reference "
195                         "count of %u, but", lte->refcnt);
196                 WARNING("We found %u references to it",
197                         lte->real_refcnt);
198                 print_lookup_table_entry(lte, stderr);
199         #endif
200                 lte->refcnt = lte->real_refcnt;
201                 ++*(unsigned long *)ctr;
202         }
203         return 0;
204 }
205
206 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
207  * screwed up because some lookup table entries are referenced more times than
208  * their stated reference counts.  So theoretically, if we delete all the
209  * references to a stream and then remove it, it might still be referenced
210  * somewhere else, making a file be missing from the WIM... So, work around this
211  * problem by looking at ALL the images to re-calculate the reference count of
212  * EVERY lookup table entry.  This only absolutely has to be done before an image
213  * is deleted or before an image is mounted read-write. */
214 int
215 wim_run_full_verifications(WIMStruct *w)
216 {
217         int ret;
218
219         for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
220
221         w->all_images_verified = 1; /* Set *before* image_run_full_verifications,
222                                        because of check in read_metadata_resource() */
223         ret = for_image(w, WIMLIB_ALL_IMAGES, image_run_full_verifications);
224         if (ret == 0) {
225                 unsigned long num_ltes_with_bogus_refcnt = 0;
226                 for_lookup_table_entry(w->lookup_table, lte_fix_refcnt,
227                                        &num_ltes_with_bogus_refcnt);
228                 if (num_ltes_with_bogus_refcnt != 0) {
229                         WARNING("A total of %lu entries in the WIM's stream "
230                                 "lookup table had to have\n"
231                                 "          their reference counts fixed.",
232                                 num_ltes_with_bogus_refcnt);
233                 }
234         } else {
235                 w->all_images_verified = 0;
236         }
237         return ret;
238 }