]> wimlib.net Git - wimlib/blob - src/verify.c
Refactor some of the dentry, inode, and lookup table code
[wimlib] / src / verify.c
1 /*
2  * verify.c
3  *
4  * Verify 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 static int
37 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
38 {
39         if (lte->refcnt != lte->real_refcnt) {
40                 if (wimlib_print_errors) {
41                         WARNING("The following lookup table entry has a reference "
42                                 "count of %u, but", lte->refcnt);
43                         WARNING("We found %u references to it",
44                                 lte->real_refcnt);
45                         print_lookup_table_entry(lte, stderr);
46                 }
47                 lte->refcnt = lte->real_refcnt;
48                 ++*(unsigned long *)ctr;
49         }
50         return 0;
51 }
52
53 static void
54 tally_inode_refcnts(const struct wim_inode *inode,
55                     const struct wim_lookup_table *lookup_table)
56 {
57         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
58                 struct wim_lookup_table_entry *lte;
59                 lte = inode_stream_lte(inode, i, lookup_table);
60                 if (lte)
61                         lte->real_refcnt += inode->i_nlink;
62         }
63 }
64
65
66 static int
67 tally_image_refcnts(WIMStruct *wim)
68 {
69         const struct wim_image_metadata *imd;
70         const struct wim_inode *inode;
71
72         imd = wim_get_current_image_metadata(wim);
73         image_for_each_inode(inode, imd)
74                 tally_inode_refcnts(inode, wim->lookup_table);
75         return 0;
76 }
77
78
79 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
80  * screwed up because some lookup table entries are referenced more times than
81  * their stated reference counts.  So theoretically, if we delete all the
82  * references to a stream and then remove it, it might still be referenced
83  * somewhere else, making a file be missing from the WIM... So, work around this
84  * problem by looking at ALL the images to re-calculate the reference count of
85  * EVERY lookup table entry.  This only absolutely has to be done before an image
86  * is deleted or before an image is mounted read-write. */
87 int
88 wim_recalculate_refcnts(WIMStruct *wim)
89 {
90         unsigned long num_ltes_with_bogus_refcnt = 0;
91         int ret;
92
93         for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
94         ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
95         if (ret)
96                 return ret;
97         num_ltes_with_bogus_refcnt = 0;
98         for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
99                                &num_ltes_with_bogus_refcnt);
100         if (num_ltes_with_bogus_refcnt != 0) {
101                 WARNING("A total of %lu entries in the WIM's stream "
102                         "lookup table had to have\n"
103                         "          their reference counts fixed.",
104                         num_ltes_with_bogus_refcnt);
105         }
106         wim->refcnts_ok = 1;
107         return 0;
108 }