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 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
46 struct wim_dentry *dentry;
48 /* Check the security ID. -1 is valid and means "no security
49 * descriptor". Anything else has to be a valid index into the WIM
50 * image's security descriptors table. */
51 if (inode->i_security_id < -1 ||
52 (inode->i_security_id >= 0 &&
53 inode->i_security_id >= sd->num_entries))
55 WARNING("\"%"TS"\" has an invalid security ID (%d)",
56 inode_first_full_path(inode), inode->i_security_id);
57 inode->i_security_id = -1;
60 /* Make sure there is only one unnamed data stream. */
61 unsigned num_unnamed_streams = 0;
62 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
64 hash = inode_stream_hash(inode, i);
65 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
66 num_unnamed_streams++;
68 if (num_unnamed_streams > 1) {
69 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
70 inode_first_full_path(inode), num_unnamed_streams);
73 /* Files cannot have multiple DOS names, even if they have multiple
74 * names in multiple directories (i.e. hard links).
75 * Source: NTFS-3g authors. */
76 struct wim_dentry *dentry_with_dos_name = NULL;
77 inode_for_each_dentry(dentry, inode) {
78 if (dentry_has_short_name(dentry)) {
79 if (dentry_with_dos_name) {
80 /* This was previously an error, but if we
81 * capture a WIM from UDF on Windows, hard links
82 * are supported but DOS names are automatically
83 * generated for all names for an inode. */
85 ERROR("Hard-linked file has a DOS name at "
86 "both `%"TS"' and `%"TS"'",
87 dentry_full_path(dentry_with_dos_name),
88 dentry_full_path(dentry));
89 return WIMLIB_ERR_INVALID_DENTRY;
91 dentry->dos_name_invalid = 1;
94 dentry_with_dos_name = dentry;
101 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
103 if (lte->refcnt != lte->real_refcnt) {
104 if (wimlib_print_errors) {
105 WARNING("The following lookup table entry has a reference "
106 "count of %u, but", lte->refcnt);
107 WARNING("We found %u references to it",
109 print_lookup_table_entry(lte, stderr);
111 lte->refcnt = lte->real_refcnt;
112 ++*(unsigned long *)ctr;
118 tally_inode_refcnts(const struct wim_inode *inode,
119 const struct wim_lookup_table *lookup_table)
121 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
122 struct wim_lookup_table_entry *lte;
123 lte = inode_stream_lte(inode, i, lookup_table);
125 lte->real_refcnt += inode->i_nlink;
131 tally_image_refcnts(WIMStruct *wim)
133 const struct wim_image_metadata *imd;
134 const struct wim_inode *inode;
136 imd = wim_get_current_image_metadata(wim);
137 image_for_each_inode(inode, imd)
138 tally_inode_refcnts(inode, wim->lookup_table);
143 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
144 * screwed up because some lookup table entries are referenced more times than
145 * their stated reference counts. So theoretically, if we delete all the
146 * references to a stream and then remove it, it might still be referenced
147 * somewhere else, making a file be missing from the WIM... So, work around this
148 * problem by looking at ALL the images to re-calculate the reference count of
149 * EVERY lookup table entry. This only absolutely has to be done before an image
150 * is deleted or before an image is mounted read-write. */
152 wim_recalculate_refcnts(WIMStruct *wim)
154 unsigned long num_ltes_with_bogus_refcnt = 0;
156 for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
157 for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
158 num_ltes_with_bogus_refcnt = 0;
159 for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
160 &num_ltes_with_bogus_refcnt);
161 if (num_ltes_with_bogus_refcnt != 0) {
162 WARNING("A total of %lu entries in the WIM's stream "
163 "lookup table had to have\n"
164 " their reference counts fixed.",
165 num_ltes_with_bogus_refcnt);