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