8 * Copyright (C) 2012, 2013, 2014 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/progress.h"
35 #include "wimlib/security.h"
38 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
40 if (lte->refcnt != lte->real_refcnt) {
41 lte->refcnt = lte->real_refcnt;
42 ++*(unsigned long *)ctr;
48 tally_inode_refcnts(const struct wim_inode *inode,
49 const struct wim_lookup_table *lookup_table)
51 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
52 struct wim_lookup_table_entry *lte;
53 lte = inode_stream_lte(inode, i, lookup_table);
55 lte->real_refcnt += inode->i_nlink;
61 tally_image_refcnts(WIMStruct *wim)
63 const struct wim_image_metadata *imd;
64 const struct wim_inode *inode;
66 imd = wim_get_current_image_metadata(wim);
67 image_for_each_inode(inode, imd)
68 tally_inode_refcnts(inode, wim->lookup_table);
73 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
74 * screwed up because some lookup table entries are referenced more times than
75 * their stated reference counts. So theoretically, if we delete all the
76 * references to a stream and then remove it, it might still be referenced
77 * somewhere else, making a file be missing from the WIM... So, work around this
78 * problem by looking at ALL the images to re-calculate the reference count of
79 * EVERY lookup table entry. This only absolutely has to be done before an image
80 * is deleted or before an image is mounted read-write. */
82 wim_recalculate_refcnts(WIMStruct *wim)
84 unsigned long num_ltes_with_bogus_refcnt = 0;
87 for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
88 ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
91 num_ltes_with_bogus_refcnt = 0;
92 for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
93 &num_ltes_with_bogus_refcnt);
94 if (num_ltes_with_bogus_refcnt != 0) {
95 WARNING("%lu stream(s) had incorrect reference count.",
96 num_ltes_with_bogus_refcnt);
103 append_lte_to_list(struct wim_lookup_table_entry *lte, void *_list)
105 list_add(<e->extraction_list, (struct list_head *)_list);
109 struct verify_stream_list_ctx {
110 wimlib_progress_func_t progfunc;
112 union wimlib_progress_info *progress;
116 end_verify_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx)
118 struct verify_stream_list_ctx *ctx = _ctx;
123 ctx->progress->verify_streams.completed_streams++;
124 ctx->progress->verify_streams.completed_bytes += lte->size;
126 return call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
127 ctx->progress, ctx->progctx);
131 verify_image_streams_present(struct wim_image_metadata *imd,
132 struct wim_lookup_table *lookup_table)
134 struct wim_inode *inode;
137 image_for_each_inode(inode, imd) {
138 ret = inode_resolve_streams(inode, lookup_table, false);
145 /* API function documented in wimlib.h */
147 wimlib_verify_wim(WIMStruct *wim, int verify_flags)
150 LIST_HEAD(stream_list);
151 union wimlib_progress_info progress;
152 struct verify_stream_list_ctx ctx;
153 struct wim_lookup_table_entry *lte;
154 struct read_stream_list_callbacks cbs = {
155 .end_stream = end_verify_stream,
156 .end_stream_ctx = &ctx,
159 /* Check parameters */
162 return WIMLIB_ERR_INVALID_PARAM;
165 return WIMLIB_ERR_INVALID_PARAM;
167 /* Verify the images */
169 if (wim_has_metadata(wim)) {
171 memset(&progress, 0, sizeof(progress));
172 progress.verify_image.wimfile = wim->filename;
173 progress.verify_image.total_images = wim->hdr.image_count;
175 for (int i = 1; i <= wim->hdr.image_count; i++) {
177 progress.verify_image.current_image = i;
179 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_BEGIN_VERIFY_IMAGE,
180 &progress, wim->progctx);
184 ret = select_wim_image(wim, i);
188 ret = verify_image_streams_present(wim_get_current_image_metadata(wim),
193 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_END_VERIFY_IMAGE,
194 &progress, wim->progctx);
199 WARNING("\"%"TS"\" does not contain image metadata. Skipping image verification.",
203 /* Verify the streams */
205 for_lookup_table_entry(wim->lookup_table, append_lte_to_list, &stream_list);
207 memset(&progress, 0, sizeof(progress));
209 progress.verify_streams.wimfile = wim->filename;
210 list_for_each_entry(lte, &stream_list, extraction_list) {
211 progress.verify_streams.total_streams++;
212 progress.verify_streams.total_bytes += lte->size;
215 ctx.progfunc = wim->progfunc;
216 ctx.progctx = wim->progctx;
217 ctx.progress = &progress;
219 ret = call_progress(ctx.progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
220 ctx.progress, ctx.progctx);
224 return read_stream_list(&stream_list,
225 offsetof(struct wim_lookup_table_entry,
227 &cbs, VERIFY_STREAM_HASHES);