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;
117 end_verify_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx)
119 struct verify_stream_list_ctx *ctx = _ctx;
120 union wimlib_progress_info *progress = ctx->progress;
125 progress->verify_streams.completed_streams++;
126 progress->verify_streams.completed_bytes += lte->size;
128 /* Handle rate-limiting of progress messages */
130 if (progress->verify_streams.completed_bytes < ctx->next_progress)
133 /* Time for another progress message. */
135 status = call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
136 progress, ctx->progctx);
140 if (ctx->next_progress == progress->verify_streams.total_bytes) {
141 ctx->next_progress = ~(uint64_t)0;
145 /* Send new message as soon as another 1/128 of the total has
146 * been verified. (Arbitrary number.) */
147 ctx->next_progress = progress->verify_streams.completed_bytes +
148 progress->verify_streams.total_bytes / 128;
150 /* ... Unless that would be more than 5000000 bytes, in which case send
151 * the next after the next 5000000 bytes. (Another arbitrary number.) */
152 if (progress->verify_streams.completed_bytes + 5000000 < ctx->next_progress)
153 ctx->next_progress = progress->verify_streams.completed_bytes + 5000000;
155 /* ... But always send a message as soon as we're completely
157 if (progress->verify_streams.total_bytes < ctx->next_progress)
158 ctx->next_progress = progress->verify_streams.total_bytes;
163 verify_image_streams_present(struct wim_image_metadata *imd,
164 struct wim_lookup_table *lookup_table)
166 struct wim_inode *inode;
169 image_for_each_inode(inode, imd) {
170 ret = inode_resolve_streams(inode, lookup_table, false);
177 /* API function documented in wimlib.h */
179 wimlib_verify_wim(WIMStruct *wim, int verify_flags)
182 LIST_HEAD(stream_list);
183 union wimlib_progress_info progress;
184 struct verify_stream_list_ctx ctx;
185 struct wim_lookup_table_entry *lte;
186 struct read_stream_list_callbacks cbs = {
187 .end_stream = end_verify_stream,
188 .end_stream_ctx = &ctx,
191 /* Check parameters */
194 return WIMLIB_ERR_INVALID_PARAM;
197 return WIMLIB_ERR_INVALID_PARAM;
199 /* Verify the images */
201 if (wim_has_metadata(wim)) {
203 memset(&progress, 0, sizeof(progress));
204 progress.verify_image.wimfile = wim->filename;
205 progress.verify_image.total_images = wim->hdr.image_count;
207 for (int i = 1; i <= wim->hdr.image_count; i++) {
209 progress.verify_image.current_image = i;
211 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_BEGIN_VERIFY_IMAGE,
212 &progress, wim->progctx);
216 ret = select_wim_image(wim, i);
220 ret = verify_image_streams_present(wim_get_current_image_metadata(wim),
225 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_END_VERIFY_IMAGE,
226 &progress, wim->progctx);
231 WARNING("\"%"TS"\" does not contain image metadata. Skipping image verification.",
235 /* Verify the streams */
237 for_lookup_table_entry(wim->lookup_table, append_lte_to_list, &stream_list);
239 memset(&progress, 0, sizeof(progress));
241 progress.verify_streams.wimfile = wim->filename;
242 list_for_each_entry(lte, &stream_list, extraction_list) {
243 progress.verify_streams.total_streams++;
244 progress.verify_streams.total_bytes += lte->size;
247 ctx.progfunc = wim->progfunc;
248 ctx.progctx = wim->progctx;
249 ctx.progress = &progress;
250 ctx.next_progress = 0;
252 ret = call_progress(ctx.progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
253 ctx.progress, ctx.progctx);
257 return read_stream_list(&stream_list,
258 offsetof(struct wim_lookup_table_entry,
260 &cbs, VERIFY_STREAM_HASHES);