]> wimlib.net Git - wimlib/blob - src/verify.c
Minor cleanups
[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 Eric Biggers
10  *
11  * wimlib - Library for working with WIM files
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
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)
18  * any later version.
19  *
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
23  * details.
24  *
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/.
27  */
28
29 #include "wimlib_internal.h"
30 #include "dentry.h"
31 #include "lookup_table.h"
32
33 static inline struct dentry *inode_first_dentry(struct inode *inode)
34 {
35         wimlib_assert(inode->dentry_list.next != &inode->dentry_list);
36         return container_of(inode->dentry_list.next, struct dentry,
37                             inode_dentry_list);
38 }
39
40 static int verify_inode(struct inode *inode, const WIMStruct *w)
41 {
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;
46
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);
51                 goto out;
52         }
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,
57                         sd->num_entries);
58                 goto out;
59         }
60
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;
67                         const u8 *hash;
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);
73                                 goto out;
74                         }
75                         if (lte)
76                                 lte->real_refcnt += inode->link_count;
77
78                         /* The following is now done when required by
79                          * wim_run_full_verifications(). */
80
81                 #if 0
82                         if (lte && !w->full_verification_in_progress &&
83                             lte->real_refcnt > lte->refcnt)
84                         {
85                         #ifdef ENABLE_ERROR_MESSAGES
86                                 WARNING("The following lookup table entry "
87                                         "has a reference count of %u, but",
88                                         lte->refcnt);
89                                 WARNING("We found %u references to it",
90                                         lte->real_refcnt);
91                                 WARNING("(One dentry referencing it is at `%s')",
92                                          first_dentry->full_path_utf8);
93
94                                 print_lookup_table_entry(lte);
95                         #endif
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) */
106
107                         #if 1
108                                 lte->refcnt = lte->real_refcnt;
109                                 WARNING("Fixing reference count");
110                         #else
111                                 goto out;
112                         #endif
113                         }
114                 #endif
115                 }
116         }
117
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++) {
121                 const u8 *hash;
122                 hash = inode_stream_hash_unresolved(inode, i);
123                 if (!inode_stream_name_len(inode, i) && !is_zero_hash(hash))
124                         num_unnamed_streams++;
125         }
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);
129                 goto out;
130         }
131         inode->verified = true;
132         ret = 0;
133 out:
134         return ret;
135 }
136
137 /* Run some miscellaneous verifications on a WIM dentry */
138 int verify_dentry(struct dentry *dentry, void *wim)
139 {
140         int ret;
141
142         if (!dentry->d_inode->verified) {
143                 ret = verify_inode(dentry->d_inode, wim);
144                 if (ret != 0)
145                         return ret;
146         }
147
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;
153         }
154
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;
161                 }
162         }
163
164 #if 0
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);
170         }
171 #endif
172
173         return 0;
174 }
175
176 static int image_run_full_verifications(WIMStruct *w)
177 {
178         return for_dentry_in_tree(wim_root_dentry(w), verify_dentry, w);
179 }
180
181 static int lte_fix_refcnt(struct lookup_table_entry *lte, void *ctr)
182 {
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",
187                         lte->real_refcnt);
188                 print_lookup_table_entry(lte);
189                 lte->refcnt = lte->real_refcnt;
190                 ++*(unsigned long *)ctr;
191         }
192         return 0;
193 }
194
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)
204 {
205         int ret;
206
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;
212         if (ret == 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);
223                 }
224         } else {
225                 w->all_images_verified = 0;
226         }
227         return ret;
228 }
229
230 /*
231  * Sanity checks to make sure a set of WIMs correctly correspond to a spanned
232  * set.
233  *
234  * @w:
235  *      Part 1 of the set.
236  *
237  * @additional_swms:
238  *      All parts of the set other than part 1.
239  *
240  * @num_additional_swms:
241  *      Number of WIMStructs in @additional_swms.  Or, the total number of parts
242  *      in the set minus 1.
243  *
244  * @return:
245  *      0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
246  */
247 int verify_swm_set(WIMStruct *w, WIMStruct **additional_swms,
248                    unsigned num_additional_swms)
249 {
250         unsigned total_parts = w->hdr.total_parts;
251         int ctype;
252         const u8 *guid;
253
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;
262         }
263         if (w->hdr.part_number != 1) {
264                 ERROR("WIM `%s' is not the first part of the split WIM.",
265                       w->filename);
266                 return WIMLIB_ERR_SPLIT_INVALID;
267         }
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,
274                               total_parts);
275                         return WIMLIB_ERR_SPLIT_INVALID;
276                 }
277         }
278
279         /* keep track of ctype and guid just to make sure they are the same for
280          * all the WIMs. */
281         ctype = wimlib_get_compression_type(w);
282         guid = w->hdr.guid;
283
284         WIMStruct *parts_to_swms[num_additional_swms];
285         ZERO_ARRAY(parts_to_swms);
286         for (unsigned i = 0; i < num_additional_swms; i++) {
287
288                 WIMStruct *swm = additional_swms[i];
289
290                 if (wimlib_get_compression_type(swm) != ctype) {
291                         ERROR("The split WIMs do not all have the same "
292                               "compression type");
293                         return WIMLIB_ERR_SPLIT_INVALID;
294                 }
295                 if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
296                         ERROR("The split WIMs do not all have the same "
297                               "GUID");
298                         return WIMLIB_ERR_SPLIT_INVALID;
299                 }
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;
305                 }
306                 if (swm->hdr.part_number == 0 ||
307                     swm->hdr.part_number > total_parts)
308                 {
309                         ERROR("WIM `%s' says it is part %u in the spanned set, "
310                               "but the part number must be in the range "
311                               "[1, %u]",
312                               swm->filename, swm->hdr.part_number, total_parts);
313                         return WIMLIB_ERR_SPLIT_INVALID;
314                 }
315                 if (parts_to_swms[swm->hdr.part_number - 2])
316                 {
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,
320                               swm->filename,
321                               swm->hdr.part_number,
322                               total_parts);
323                         return WIMLIB_ERR_SPLIT_INVALID;
324                 } else {
325                         parts_to_swms[swm->hdr.part_number - 2] = swm;
326                 }
327         }
328         return 0;
329 }
330