]> wimlib.net Git - wimlib/blob - src/verify.c
Merge branch with pipable WIM support
[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  * Return values:
44  *      WIMLIB_ERR_SUCCESS (0)
45  */
46 int
47 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
48 {
49         struct wim_dentry *dentry;
50
51         /* Check the security ID.  -1 is valid and means "no security
52          * descriptor".  Anything else has to be a valid index into the WIM
53          * image's security descriptors table. */
54         if (inode->i_security_id < -1 ||
55             (inode->i_security_id >= 0 &&
56              inode->i_security_id >= sd->num_entries))
57         {
58                 WARNING("\"%"TS"\" has an invalid security ID (%d)",
59                         inode_first_full_path(inode), inode->i_security_id);
60                 inode->i_security_id = -1;
61         }
62
63         /* Make sure there is only one unnamed data stream. */
64         unsigned num_unnamed_streams = 0;
65         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
66                 const u8 *hash;
67                 hash = inode_stream_hash(inode, i);
68                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
69                         num_unnamed_streams++;
70         }
71         if (num_unnamed_streams > 1) {
72                 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
73                         inode_first_full_path(inode), num_unnamed_streams);
74         }
75
76         /* Files cannot have multiple DOS names, even if they have multiple
77          * names in multiple directories (i.e. hard links).
78          * Source: NTFS-3g authors. */
79         struct wim_dentry *dentry_with_dos_name = NULL;
80         inode_for_each_dentry(dentry, inode) {
81                 if (dentry_has_short_name(dentry)) {
82                         if (dentry_with_dos_name) {
83                                 /* This was previously an error, but if we
84                                  * capture a WIM from UDF on Windows, hard links
85                                  * are supported but DOS names are automatically
86                                  * generated for all names for an inode.  */
87                         #if 0
88                                 ERROR("Hard-linked file has a DOS name at "
89                                       "both `%"TS"' and `%"TS"'",
90                                       dentry_full_path(dentry_with_dos_name),
91                                       dentry_full_path(dentry));
92                                 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
93                         #else
94                                 dentry->dos_name_invalid = 1;
95                         #endif
96                         }
97                         dentry_with_dos_name = dentry;
98                 }
99         }
100         return 0;
101 }
102
103 static int
104 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
105 {
106         if (lte->refcnt != lte->real_refcnt) {
107                 if (wimlib_print_errors) {
108                         WARNING("The following lookup table entry has a reference "
109                                 "count of %u, but", lte->refcnt);
110                         WARNING("We found %u references to it",
111                                 lte->real_refcnt);
112                         print_lookup_table_entry(lte, stderr);
113                 }
114                 lte->refcnt = lte->real_refcnt;
115                 ++*(unsigned long *)ctr;
116         }
117         return 0;
118 }
119
120 static void
121 tally_inode_refcnts(const struct wim_inode *inode,
122                     const struct wim_lookup_table *lookup_table)
123 {
124         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
125                 struct wim_lookup_table_entry *lte;
126                 lte = inode_stream_lte(inode, i, lookup_table);
127                 if (lte)
128                         lte->real_refcnt += inode->i_nlink;
129         }
130 }
131
132
133 static int
134 tally_image_refcnts(WIMStruct *wim)
135 {
136         const struct wim_image_metadata *imd;
137         const struct wim_inode *inode;
138
139         imd = wim_get_current_image_metadata(wim);
140         image_for_each_inode(inode, imd)
141                 tally_inode_refcnts(inode, wim->lookup_table);
142         return 0;
143 }
144
145
146 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
147  * screwed up because some lookup table entries are referenced more times than
148  * their stated reference counts.  So theoretically, if we delete all the
149  * references to a stream and then remove it, it might still be referenced
150  * somewhere else, making a file be missing from the WIM... So, work around this
151  * problem by looking at ALL the images to re-calculate the reference count of
152  * EVERY lookup table entry.  This only absolutely has to be done before an image
153  * is deleted or before an image is mounted read-write. */
154 void
155 wim_recalculate_refcnts(WIMStruct *wim)
156 {
157         unsigned long num_ltes_with_bogus_refcnt = 0;
158
159         for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
160         for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
161         num_ltes_with_bogus_refcnt = 0;
162         for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
163                                &num_ltes_with_bogus_refcnt);
164         if (num_ltes_with_bogus_refcnt != 0) {
165                 WARNING("A total of %lu entries in the WIM's stream "
166                         "lookup table had to have\n"
167                         "          their reference counts fixed.",
168                         num_ltes_with_bogus_refcnt);
169         }
170         wim->refcnts_ok = 1;
171 }