struct wim_lookup_table_entry;
struct wimfs_fd;
struct wim_inode;
+struct wim_security_data;
/* Size of the struct wim_dentry up to and including the file_name_len. */
#define WIM_DENTRY_DISK_SIZE 102
* (This is not an on-disk field.) */
u8 i_resolved : 1;
- /* %true iff verify_inode() has run on this inode. */
- u8 i_verified : 1;
-
u8 i_visited : 1;
/* Used only in NTFS-mode extraction */
#define inode_first_dentry(inode) \
container_of(inode->i_dentry.next, struct wim_dentry, d_alias)
+#define inode_first_full_path(inode) \
+ dentry_full_path(inode_first_dentry(inode))
+
static inline bool
dentry_is_first_in_inode(const struct wim_dentry *dentry)
{
extern int
dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list);
-extern int
-verify_dentry(struct wim_dentry *dentry, void *wim);
+int
+verify_inode(struct wim_inode *inode, const struct wim_security_data *sd);
#endif
# define ERROR_WITH_ERRNO(format, ...) wimlib_error_with_errno(T(format), ## __VA_ARGS__)
# define WARNING(format, ...) wimlib_warning(T(format), ## __VA_ARGS__)
# define WARNING_WITH_ERRNO(format, ...) wimlib_warning_with_errno(T(format), ## __VA_ARGS__)
+extern bool wimlib_print_errors;
#else /* ENABLE_ERROR_MESSAGES */
+# define wimlib_print_errors 0
# define ERROR(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
# define ERROR_WITH_ERRNO(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
# define WARNING(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
# define ENABLE_ASSERTIONS 1
#endif
+
#ifdef ENABLE_DEBUG
extern void
wimlib_debug(const tchar *file, int line, const char *func,
/* Have any images been deleted? */
u8 deletion_occurred : 1;
- u8 all_images_verified : 1;
+ /* Do we know that all the stream reference counts in the WIM are
+ * correct? If so, this is set to 1 and deletions are safe; otherwise
+ * this is set to 0 and deletions are not safe until reference counts
+ * are recalculated. (This is due to a bug in M$'s software that
+ * generates WIMs with invalid reference counts.) */
+ u8 refcnts_ok : 1;
+
u8 wim_locked : 1;
};
-extern int
-wim_run_full_verifications(WIMStruct *w);
+extern void
+wim_recalculate_refcnts(WIMStruct *wim);
extern int
read_header(const tchar *filename, int in_fd, struct wim_header *hdr);
return 0;
}
-#if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
-static void
+static inline void
print_inode_dentries(const struct wim_inode *inode)
{
struct wim_dentry *dentry;
inode_for_each_dentry(dentry, inode)
tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
}
-#endif
static void
inconsistent_inode(const struct wim_inode *inode)
{
-#ifdef ENABLE_ERROR_MESSAGES
- ERROR("An inconsistent hard link group that cannot be corrected has "
- "been detected");
- ERROR("The dentries are located at the following paths:");
- print_inode_dentries(inode);
-#endif
+ if (wimlib_print_errors) {
+ ERROR("An inconsistent hard link group that cannot be corrected has "
+ "been detected");
+ ERROR("The dentries are located at the following paths:");
+ print_inode_dentries(inode);
+ }
}
static bool
&& (cur_entry->resource_entry.size !=
cur_entry->resource_entry.original_size))
{
- #ifdef ENABLE_ERROR_MESSAGES
- ERROR("Found uncompressed resource with original size "
- "not the same as compressed size");
- ERROR("The lookup table entry for the resource is as follows:");
- print_lookup_table_entry(cur_entry, stderr);
- #endif
+ if (wimlib_print_errors) {
+ ERROR("Found uncompressed resource with original size "
+ "not the same as compressed size");
+ ERROR("The lookup table entry for the resource is as follows:");
+ print_lookup_table_entry(cur_entry, stderr);
+ }
ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
goto out_free_cur_entry;
}
if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
/* Lookup table entry for a metadata resource */
if (cur_entry->refcnt != 1) {
- #ifdef ENABLE_ERROR_MESSAGES
- ERROR("Found metadata resource with refcnt != 1:");
- print_lookup_table_entry(cur_entry, stderr);
- #endif
+ if (wimlib_print_errors) {
+ ERROR("Found metadata resource with refcnt != 1:");
+ print_lookup_table_entry(cur_entry, stderr);
+ }
ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
goto out_free_cur_entry;
}
* metadata resource */
duplicate_entry = __lookup_resource(table, cur_entry->hash);
if (duplicate_entry) {
- #ifdef ENABLE_ERROR_MESSAGES
- ERROR("The WIM lookup table contains two entries with the "
- "same SHA1 message digest!");
- ERROR("The first entry is:");
- print_lookup_table_entry(duplicate_entry, stderr);
- ERROR("The second entry is:");
- print_lookup_table_entry(cur_entry, stderr);
- #endif
+ if (wimlib_print_errors) {
+ ERROR("The WIM lookup table contains two entries with the "
+ "same SHA1 message digest!");
+ ERROR("The first entry is:");
+ print_lookup_table_entry(duplicate_entry, stderr);
+ ERROR("The second entry is:");
+ print_lookup_table_entry(cur_entry, stderr);
+ }
ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
goto out_free_cur_entry;
}
const struct wim_lookup_table_entry *metadata_lte;
u64 metadata_len;
u8 hash[SHA1_HASH_SIZE];
+ struct wim_security_data *security_data;
+ struct wim_inode *inode;
metadata_lte = imd->metadata_lte;
metadata_len = wim_resource_size(metadata_lte);
* and calculate the offset in the metadata resource of the root dentry.
* */
- ret = read_wim_security_data(buf, metadata_len, &imd->security_data);
+ ret = read_wim_security_data(buf, metadata_len, &security_data);
if (ret)
goto out_free_buf;
}
ret = read_dentry(buf, metadata_len,
- imd->security_data->total_length, root);
+ security_data->total_length, root);
if (ret == 0 && root->length == 0) {
WARNING("Metadata resource begins with end-of-directory entry "
if (ret)
goto out_free_dentry_tree;
- if (!wim->all_images_verified) {
- /* Note: verify_dentry() expects to access imd->security_data,
- * so it needs to be set before here. */
- DEBUG("Running miscellaneous verifications on the dentry tree");
- for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
- ret = for_dentry_in_tree(root, verify_dentry, wim);
+
+ DEBUG("Running miscellaneous verifications on the dentry tree");
+ image_for_each_inode(inode, imd) {
+ ret = verify_inode(inode, security_data);
if (ret)
goto out_free_dentry_tree;
}
-
DEBUG("Done reading image metadata");
-
out_success:
imd->root_dentry = root;
+ imd->security_data = security_data;
INIT_LIST_HEAD(&imd->unhashed_streams);
+ ret = 0;
goto out_free_buf;
out_free_dentry_tree:
free_dentry_tree(root, wim->lookup_table);
out_free_security_data:
- free_wim_security_data(imd->security_data);
- imd->security_data = NULL;
+ free_wim_security_data(security_data);
out_free_buf:
FREE(buf);
return ret;
u8 hash[SHA1_HASH_SIZE];
sha1_final(hash, &ctx.sha_ctx);
if (!hashes_equal(hash, lte->hash)) {
- #ifdef ENABLE_ERROR_MESSAGES
- ERROR("Invalid SHA1 message digest "
- "on the following WIM resource:");
- print_lookup_table_entry(lte, stderr);
- if (lte->resource_location == RESOURCE_IN_WIM)
- ERROR("The WIM file appears to be corrupt!");
- #endif
+ if (wimlib_print_errors) {
+ ERROR("Invalid SHA1 message digest "
+ "on the following WIM resource:");
+ print_lookup_table_entry(lte, stderr);
+ if (lte->resource_location == RESOURCE_IN_WIM)
+ ERROR("The WIM file appears to be corrupt!");
+ }
ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
}
}
#endif /* __WIN32__ */
#ifdef ENABLE_ERROR_MESSAGES
-static bool wimlib_print_errors = false;
+bool wimlib_print_errors = false;
#endif
#if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
/*
* verify.c
*
- * Some functions to verify that stuff in the WIM is valid. Of course, not
- * *all* the verifications of the input data are in this file.
+ * Verify WIM inodes and stream reference counts.
*/
/*
#include "wimlib/metadata.h"
#include "wimlib/security.h"
-static int
-verify_inode(struct wim_inode *inode, const WIMStruct *w)
+/*
+ * Verify a WIM inode:
+ *
+ * - Check to make sure the security ID is valid
+ * - Check to make sure there is at most one unnamed stream
+ * - Check to make sure there is at most one DOS name.
+ */
+int
+verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
{
- const struct wim_lookup_table *table = w->lookup_table;
- const struct wim_security_data *sd = wim_const_security_data(w);
- struct wim_dentry *first_dentry = inode_first_dentry(inode);
struct wim_dentry *dentry;
/* Check the security ID. -1 is valid and means "no security
inode->i_security_id >= sd->num_entries))
{
WARNING("\"%"TS"\" has an invalid security ID (%d)",
- dentry_full_path(first_dentry), inode->i_security_id);
+ inode_first_full_path(inode), inode->i_security_id);
inode->i_security_id = -1;
}
- /* Check that lookup table entries for all the inode's stream exist,
- * except if the SHA1 message digest is all 0's, which indicates an
- * empty stream.
- *
- * This check is skipped on split WIMs. */
- if (w->hdr.total_parts == 1 && !inode->i_resolved) {
- for (unsigned i = 0; i <= inode->i_num_ads; i++) {
- struct wim_lookup_table_entry *lte;
- const u8 *hash;
- hash = inode_stream_hash(inode, i);
- lte = __lookup_resource(table, hash);
- if (!lte && !is_zero_hash(hash)) {
- ERROR("Could not find lookup table entry for stream "
- "%u of dentry `%"TS"'",
- i, dentry_full_path(first_dentry));
- return WIMLIB_ERR_INVALID_DENTRY;
- }
- if (lte)
- lte->real_refcnt += inode->i_nlink;
- }
- }
-
/* Make sure there is only one unnamed data stream. */
unsigned num_unnamed_streams = 0;
for (unsigned i = 0; i <= inode->i_num_ads; i++) {
}
if (num_unnamed_streams > 1) {
WARNING("\"%"TS"\" has multiple (%u) un-named streams",
- dentry_full_path(first_dentry), num_unnamed_streams);
+ inode_first_full_path(inode), num_unnamed_streams);
}
/* Files cannot have multiple DOS names, even if they have multiple
dentry_with_dos_name = dentry;
}
}
-
- inode->i_verified = 1;
return 0;
}
-/* Run some miscellaneous verifications on a WIM dentry */
-int
-verify_dentry(struct wim_dentry *dentry, void *wim)
+static int
+lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
{
- int ret;
- WIMStruct *w = wim;
- /* Verify the associated inode, but only one time no matter how many
- * dentries it has (unless we are doing a full verification of the WIM,
- * in which case we need to force the inode to be verified again.) */
- if (!dentry->d_inode->i_verified) {
- ret = verify_inode(dentry->d_inode, w);
- if (ret)
- return ret;
+ if (lte->refcnt != lte->real_refcnt) {
+ if (wimlib_print_errors) {
+ WARNING("The following lookup table entry has a reference "
+ "count of %u, but", lte->refcnt);
+ WARNING("We found %u references to it",
+ lte->real_refcnt);
+ print_lookup_table_entry(lte, stderr);
+ }
+ lte->refcnt = lte->real_refcnt;
+ ++*(unsigned long *)ctr;
}
return 0;
}
-static int
-image_run_full_verifications(WIMStruct *w)
+static void
+tally_inode_refcnts(const struct wim_inode *inode,
+ const struct wim_lookup_table *lookup_table)
{
- struct wim_image_metadata *imd;
- struct wim_inode *inode;
-
- imd = wim_get_current_image_metadata(w);
- image_for_each_inode(inode, imd) {
- inode->i_verified = 0;
- return for_dentry_in_tree(imd->root_dentry, verify_dentry, w);
+ for (unsigned i = 0; i <= inode->i_num_ads; i++) {
+ struct wim_lookup_table_entry *lte;
+ lte = inode_stream_lte(inode, i, lookup_table);
+ if (lte)
+ lte->real_refcnt += inode->i_nlink;
+ }
}
+
static int
-lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
+tally_image_refcnts(WIMStruct *wim)
{
- if (lte->refcnt != lte->real_refcnt) {
- #ifdef ENABLE_ERROR_MESSAGES
- WARNING("The following lookup table entry has a reference "
- "count of %u, but", lte->refcnt);
- WARNING("We found %u references to it",
- lte->real_refcnt);
- print_lookup_table_entry(lte, stderr);
- #endif
- lte->refcnt = lte->real_refcnt;
- ++*(unsigned long *)ctr;
- }
+ const struct wim_image_metadata *imd;
+ const struct wim_inode *inode;
+
+ imd = wim_get_current_image_metadata(wim);
+ image_for_each_inode(inode, imd)
+ tally_inode_refcnts(inode, wim->lookup_table);
return 0;
}
+
/* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
* screwed up because some lookup table entries are referenced more times than
* their stated reference counts. So theoretically, if we delete all the
* problem by looking at ALL the images to re-calculate the reference count of
* EVERY lookup table entry. This only absolutely has to be done before an image
* is deleted or before an image is mounted read-write. */
-int
-wim_run_full_verifications(WIMStruct *w)
+void
+wim_recalculate_refcnts(WIMStruct *wim)
{
- int ret;
-
- for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
-
- w->all_images_verified = 1; /* Set *before* image_run_full_verifications,
- because of check in read_metadata_resource() */
- ret = for_image(w, WIMLIB_ALL_IMAGES, image_run_full_verifications);
- if (ret == 0) {
- unsigned long num_ltes_with_bogus_refcnt = 0;
- for_lookup_table_entry(w->lookup_table, lte_fix_refcnt,
- &num_ltes_with_bogus_refcnt);
- if (num_ltes_with_bogus_refcnt != 0) {
- WARNING("A total of %lu entries in the WIM's stream "
- "lookup table had to have\n"
- " their reference counts fixed.",
- num_ltes_with_bogus_refcnt);
- }
- } else {
- w->all_images_verified = 0;
+ unsigned long num_ltes_with_bogus_refcnt = 0;
+
+ for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
+ for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
+ num_ltes_with_bogus_refcnt = 0;
+ for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
+ &num_ltes_with_bogus_refcnt);
+ if (num_ltes_with_bogus_refcnt != 0) {
+ WARNING("A total of %lu entries in the WIM's stream "
+ "lookup table had to have\n"
+ " their reference counts fixed.",
+ num_ltes_with_bogus_refcnt);
}
- return ret;
+ wim->refcnts_ok = 1;
}
goto out_free;
}
w->lookup_table = table;
+ w->refcnts_ok = 1;
*w_ret = w;
return 0;
out_free:
int ret;
ret = can_modify_wim(wim);
- if (ret == 0 && !wim->all_images_verified)
- ret = wim_run_full_verifications(wim);
- return ret;
+ if (ret)
+ return ret;
+ if (!wim->refcnts_ok)
+ wim_recalculate_refcnts(wim);
+ return 0;
}
/* Frees the memory for the WIMStruct, including all internal memory; also
if (lte->resource_entry.offset +
lte->resource_entry.size > args->end_offset)
{
- #ifdef ENABLE_ERROR_MESSAGES
- ERROR("The following resource is after the XML data:");
- print_lookup_table_entry(lte, stderr);
- #endif
+ if (wimlib_print_errors) {
+ ERROR("The following resource is after the XML data:");
+ print_lookup_table_entry(lte, stderr);
+ }
return WIMLIB_ERR_RESOURCE_ORDER;
}
copy_resource_entry(<e->output_resource_entry,