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