4 * Verify stream reference counts.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
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"
37 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
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",
45 print_lookup_table_entry(lte, stderr);
47 lte->refcnt = lte->real_refcnt;
48 ++*(unsigned long *)ctr;
54 tally_inode_refcnts(const struct wim_inode *inode,
55 const struct wim_lookup_table *lookup_table)
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);
61 lte->real_refcnt += inode->i_nlink;
67 tally_image_refcnts(WIMStruct *wim)
69 const struct wim_image_metadata *imd;
70 const struct wim_inode *inode;
72 imd = wim_get_current_image_metadata(wim);
73 image_for_each_inode(inode, imd)
74 tally_inode_refcnts(inode, wim->lookup_table);
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. */
88 wim_recalculate_refcnts(WIMStruct *wim)
90 unsigned long num_ltes_with_bogus_refcnt = 0;
93 for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
94 ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
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);