]> wimlib.net Git - wimlib/blob - src/verify.c
Remove verify_dentry(); separate refcnt recalc. from verify_inode()
[wimlib] / src / verify.c
1 /*
2  * verify.c
3  *
4  * Verify WIM inodes and stream reference counts.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib/dentry.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/security.h"
35
36 /*
37  * Verify a WIM inode:
38  *
39  * - Check to make sure the security ID is valid
40  * - Check to make sure there is at most one unnamed stream
41  * - Check to make sure there is at most one DOS name.
42  */
43 int
44 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
45 {
46         struct wim_dentry *dentry;
47
48         /* Check the security ID.  -1 is valid and means "no security
49          * descriptor".  Anything else has to be a valid index into the WIM
50          * image's security descriptors table. */
51         if (inode->i_security_id < -1 ||
52             (inode->i_security_id >= 0 &&
53              inode->i_security_id >= sd->num_entries))
54         {
55                 WARNING("\"%"TS"\" has an invalid security ID (%d)",
56                         inode_first_full_path(inode), inode->i_security_id);
57                 inode->i_security_id = -1;
58         }
59
60         /* Make sure there is only one unnamed data stream. */
61         unsigned num_unnamed_streams = 0;
62         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
63                 const u8 *hash;
64                 hash = inode_stream_hash(inode, i);
65                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
66                         num_unnamed_streams++;
67         }
68         if (num_unnamed_streams > 1) {
69                 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
70                         inode_first_full_path(inode), num_unnamed_streams);
71         }
72
73         /* Files cannot have multiple DOS names, even if they have multiple
74          * names in multiple directories (i.e. hard links).
75          * Source: NTFS-3g authors. */
76         struct wim_dentry *dentry_with_dos_name = NULL;
77         inode_for_each_dentry(dentry, inode) {
78                 if (dentry_has_short_name(dentry)) {
79                         if (dentry_with_dos_name) {
80                                 /* This was previously an error, but if we
81                                  * capture a WIM from UDF on Windows, hard links
82                                  * are supported but DOS names are automatically
83                                  * generated for all names for an inode.  */
84                         #if 0
85                                 ERROR("Hard-linked file has a DOS name at "
86                                       "both `%"TS"' and `%"TS"'",
87                                       dentry_full_path(dentry_with_dos_name),
88                                       dentry_full_path(dentry));
89                                 return WIMLIB_ERR_INVALID_DENTRY;
90                         #else
91                                 dentry->dos_name_invalid = 1;
92                         #endif
93                         }
94                         dentry_with_dos_name = dentry;
95                 }
96         }
97         return 0;
98 }
99
100 static int
101 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
102 {
103         if (lte->refcnt != lte->real_refcnt) {
104                 if (wimlib_print_errors) {
105                         WARNING("The following lookup table entry has a reference "
106                                 "count of %u, but", lte->refcnt);
107                         WARNING("We found %u references to it",
108                                 lte->real_refcnt);
109                         print_lookup_table_entry(lte, stderr);
110                 }
111                 lte->refcnt = lte->real_refcnt;
112                 ++*(unsigned long *)ctr;
113         }
114         return 0;
115 }
116
117 static void
118 tally_inode_refcnts(const struct wim_inode *inode,
119                     const struct wim_lookup_table *lookup_table)
120 {
121         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
122                 struct wim_lookup_table_entry *lte;
123                 lte = inode_stream_lte(inode, i, lookup_table);
124                 if (lte)
125                         lte->real_refcnt += inode->i_nlink;
126         }
127 }
128
129
130 static int
131 tally_image_refcnts(WIMStruct *wim)
132 {
133         const struct wim_image_metadata *imd;
134         const struct wim_inode *inode;
135
136         imd = wim_get_current_image_metadata(wim);
137         image_for_each_inode(inode, imd)
138                 tally_inode_refcnts(inode, wim->lookup_table);
139         return 0;
140 }
141
142
143 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
144  * screwed up because some lookup table entries are referenced more times than
145  * their stated reference counts.  So theoretically, if we delete all the
146  * references to a stream and then remove it, it might still be referenced
147  * somewhere else, making a file be missing from the WIM... So, work around this
148  * problem by looking at ALL the images to re-calculate the reference count of
149  * EVERY lookup table entry.  This only absolutely has to be done before an image
150  * is deleted or before an image is mounted read-write. */
151 void
152 wim_recalculate_refcnts(WIMStruct *wim)
153 {
154         unsigned long num_ltes_with_bogus_refcnt = 0;
155
156         for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
157         for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
158         num_ltes_with_bogus_refcnt = 0;
159         for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
160                                &num_ltes_with_bogus_refcnt);
161         if (num_ltes_with_bogus_refcnt != 0) {
162                 WARNING("A total of %lu entries in the WIM's stream "
163                         "lookup table had to have\n"
164                         "          their reference counts fixed.",
165                         num_ltes_with_bogus_refcnt);
166         }
167         wim->refcnts_ok = 1;
168 }