4 * Verify WIM inodes and 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"
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.
44 * WIMLIB_ERR_SUCCESS (0)
47 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
49 struct wim_dentry *dentry;
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))
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;
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++) {
67 hash = inode_stream_hash(inode, i);
68 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
69 num_unnamed_streams++;
71 if (num_unnamed_streams > 1) {
72 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
73 inode_first_full_path(inode), num_unnamed_streams);
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. */
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;
94 dentry->dos_name_invalid = 1;
97 dentry_with_dos_name = dentry;
104 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
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",
112 print_lookup_table_entry(lte, stderr);
114 lte->refcnt = lte->real_refcnt;
115 ++*(unsigned long *)ctr;
121 tally_inode_refcnts(const struct wim_inode *inode,
122 const struct wim_lookup_table *lookup_table)
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);
128 lte->real_refcnt += inode->i_nlink;
134 tally_image_refcnts(WIMStruct *wim)
136 const struct wim_image_metadata *imd;
137 const struct wim_inode *inode;
139 imd = wim_get_current_image_metadata(wim);
140 image_for_each_inode(inode, imd)
141 tally_inode_refcnts(inode, wim->lookup_table);
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. */
155 wim_recalculate_refcnts(WIMStruct *wim)
157 unsigned long num_ltes_with_bogus_refcnt = 0;
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);