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 lte->refcnt = lte->real_refcnt;
41 ++*(unsigned long *)ctr;
47 tally_inode_refcnts(const struct wim_inode *inode,
48 const struct wim_lookup_table *lookup_table)
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);
54 lte->real_refcnt += inode->i_nlink;
60 tally_image_refcnts(WIMStruct *wim)
62 const struct wim_image_metadata *imd;
63 const struct wim_inode *inode;
65 imd = wim_get_current_image_metadata(wim);
66 image_for_each_inode(inode, imd)
67 tally_inode_refcnts(inode, wim->lookup_table);
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. */
81 wim_recalculate_refcnts(WIMStruct *wim)
83 unsigned long num_ltes_with_bogus_refcnt = 0;
86 for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
87 ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
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);