8 * Copyright (C) 2012, 2013, 2014 Eric Biggers
10 * This file is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option) any
15 * This file is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/dentry.h"
29 #include "wimlib/error.h"
30 #include "wimlib/lookup_table.h"
31 #include "wimlib/metadata.h"
32 #include "wimlib/progress.h"
33 #include "wimlib/security.h"
36 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
38 if (lte->refcnt != lte->real_refcnt) {
39 lte->refcnt = lte->real_refcnt;
40 ++*(unsigned long *)ctr;
46 tally_inode_refcnts(const struct wim_inode *inode,
47 const struct wim_lookup_table *lookup_table)
49 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
50 struct wim_lookup_table_entry *lte;
51 lte = inode_stream_lte(inode, i, lookup_table);
53 lte->real_refcnt += inode->i_nlink;
59 tally_image_refcnts(WIMStruct *wim)
61 const struct wim_image_metadata *imd;
62 const struct wim_inode *inode;
64 imd = wim_get_current_image_metadata(wim);
65 image_for_each_inode(inode, imd)
66 tally_inode_refcnts(inode, wim->lookup_table);
71 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
72 * screwed up because some lookup table entries are referenced more times than
73 * their stated reference counts. So theoretically, if we delete all the
74 * references to a stream and then remove it, it might still be referenced
75 * somewhere else, making a file be missing from the WIM... So, work around this
76 * problem by looking at ALL the images to re-calculate the reference count of
77 * EVERY lookup table entry. This only absolutely has to be done before an image
78 * is deleted or before an image is mounted read-write. */
80 wim_recalculate_refcnts(WIMStruct *wim)
82 unsigned long num_ltes_with_bogus_refcnt = 0;
85 for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
86 ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
89 num_ltes_with_bogus_refcnt = 0;
90 for_lookup_table_entry(wim->lookup_table, lte_fix_refcnt,
91 &num_ltes_with_bogus_refcnt);
92 if (num_ltes_with_bogus_refcnt != 0) {
93 WARNING("%lu stream(s) had incorrect reference count.",
94 num_ltes_with_bogus_refcnt);
101 append_lte_to_list(struct wim_lookup_table_entry *lte, void *_list)
103 list_add(<e->extraction_list, (struct list_head *)_list);
107 struct verify_stream_list_ctx {
108 wimlib_progress_func_t progfunc;
110 union wimlib_progress_info *progress;
115 end_verify_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx)
117 struct verify_stream_list_ctx *ctx = _ctx;
118 union wimlib_progress_info *progress = ctx->progress;
123 progress->verify_streams.completed_streams++;
124 progress->verify_streams.completed_bytes += lte->size;
126 /* Handle rate-limiting of progress messages */
128 if (progress->verify_streams.completed_bytes < ctx->next_progress)
131 /* Time for another progress message. */
133 status = call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
134 progress, ctx->progctx);
138 if (ctx->next_progress == progress->verify_streams.total_bytes) {
139 ctx->next_progress = ~(uint64_t)0;
143 /* Send new message as soon as another 1/128 of the total has
144 * been verified. (Arbitrary number.) */
145 ctx->next_progress = progress->verify_streams.completed_bytes +
146 progress->verify_streams.total_bytes / 128;
148 /* ... Unless that would be more than 5000000 bytes, in which case send
149 * the next after the next 5000000 bytes. (Another arbitrary number.) */
150 if (progress->verify_streams.completed_bytes + 5000000 < ctx->next_progress)
151 ctx->next_progress = progress->verify_streams.completed_bytes + 5000000;
153 /* ... But always send a message as soon as we're completely
155 if (progress->verify_streams.total_bytes < ctx->next_progress)
156 ctx->next_progress = progress->verify_streams.total_bytes;
161 verify_image_streams_present(struct wim_image_metadata *imd,
162 struct wim_lookup_table *lookup_table)
164 struct wim_inode *inode;
167 image_for_each_inode(inode, imd) {
168 ret = inode_resolve_streams(inode, lookup_table, false);
175 /* API function documented in wimlib.h */
177 wimlib_verify_wim(WIMStruct *wim, int verify_flags)
180 LIST_HEAD(stream_list);
181 union wimlib_progress_info progress;
182 struct verify_stream_list_ctx ctx;
183 struct wim_lookup_table_entry *lte;
184 struct read_stream_list_callbacks cbs = {
185 .end_stream = end_verify_stream,
186 .end_stream_ctx = &ctx,
189 /* Check parameters */
192 return WIMLIB_ERR_INVALID_PARAM;
195 return WIMLIB_ERR_INVALID_PARAM;
197 /* Verify the images */
199 if (wim_has_metadata(wim)) {
201 memset(&progress, 0, sizeof(progress));
202 progress.verify_image.wimfile = wim->filename;
203 progress.verify_image.total_images = wim->hdr.image_count;
205 for (int i = 1; i <= wim->hdr.image_count; i++) {
207 progress.verify_image.current_image = i;
209 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_BEGIN_VERIFY_IMAGE,
210 &progress, wim->progctx);
214 ret = select_wim_image(wim, i);
218 ret = verify_image_streams_present(wim_get_current_image_metadata(wim),
223 ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_END_VERIFY_IMAGE,
224 &progress, wim->progctx);
229 WARNING("\"%"TS"\" does not contain image metadata. Skipping image verification.",
233 /* Verify the streams */
235 for_lookup_table_entry(wim->lookup_table, append_lte_to_list, &stream_list);
237 memset(&progress, 0, sizeof(progress));
239 progress.verify_streams.wimfile = wim->filename;
240 list_for_each_entry(lte, &stream_list, extraction_list) {
241 progress.verify_streams.total_streams++;
242 progress.verify_streams.total_bytes += lte->size;
245 ctx.progfunc = wim->progfunc;
246 ctx.progctx = wim->progctx;
247 ctx.progress = &progress;
248 ctx.next_progress = 0;
250 ret = call_progress(ctx.progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
251 ctx.progress, ctx.progctx);
255 return read_stream_list(&stream_list,
256 offsetof(struct wim_lookup_table_entry,
258 &cbs, VERIFY_STREAM_HASHES);