]> wimlib.net Git - wimlib/blob - src/verify.c
Use LGPLv3+ for src/*.c
[wimlib] / src / verify.c
1 /*
2  * verify.c
3  *
4  * Verify WIM files.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013, 2014 Eric Biggers
9  *
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
13  * later version.
14  *
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
18  * details.
19  *
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/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
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"
34
35 static int
36 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
37 {
38         if (lte->refcnt != lte->real_refcnt) {
39                 lte->refcnt = lte->real_refcnt;
40                 ++*(unsigned long *)ctr;
41         }
42         return 0;
43 }
44
45 static void
46 tally_inode_refcnts(const struct wim_inode *inode,
47                     const struct wim_lookup_table *lookup_table)
48 {
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);
52                 if (lte)
53                         lte->real_refcnt += inode->i_nlink;
54         }
55 }
56
57
58 static int
59 tally_image_refcnts(WIMStruct *wim)
60 {
61         const struct wim_image_metadata *imd;
62         const struct wim_inode *inode;
63
64         imd = wim_get_current_image_metadata(wim);
65         image_for_each_inode(inode, imd)
66                 tally_inode_refcnts(inode, wim->lookup_table);
67         return 0;
68 }
69
70
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. */
79 int
80 wim_recalculate_refcnts(WIMStruct *wim)
81 {
82         unsigned long num_ltes_with_bogus_refcnt = 0;
83         int ret;
84
85         for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
86         ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
87         if (ret)
88                 return ret;
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);
95         }
96         wim->refcnts_ok = 1;
97         return 0;
98 }
99
100 static int
101 append_lte_to_list(struct wim_lookup_table_entry *lte, void *_list)
102 {
103         list_add(&lte->extraction_list, (struct list_head *)_list);
104         return 0;
105 }
106
107 struct verify_stream_list_ctx {
108         wimlib_progress_func_t progfunc;
109         void *progctx;
110         union wimlib_progress_info *progress;
111         u64 next_progress;
112 };
113
114 static int
115 end_verify_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx)
116 {
117         struct verify_stream_list_ctx *ctx = _ctx;
118         union wimlib_progress_info *progress = ctx->progress;
119
120         if (status)
121                 return status;
122
123         progress->verify_streams.completed_streams++;
124         progress->verify_streams.completed_bytes += lte->size;
125
126         /* Handle rate-limiting of progress messages  */
127
128         if (progress->verify_streams.completed_bytes < ctx->next_progress)
129                 return 0;
130
131         /* Time for another progress message.  */
132
133         status = call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
134                                progress, ctx->progctx);
135         if (status)
136                 return status;
137
138         if (ctx->next_progress == progress->verify_streams.total_bytes) {
139                 ctx->next_progress = ~(uint64_t)0;
140                 return 0;
141         }
142
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;
147
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;
152
153         /* ... But always send a message as soon as we're completely
154          * done.  */
155         if (progress->verify_streams.total_bytes < ctx->next_progress)
156                 ctx->next_progress = progress->verify_streams.total_bytes;
157         return 0;
158 }
159
160 static int
161 verify_image_streams_present(struct wim_image_metadata *imd,
162                              struct wim_lookup_table *lookup_table)
163 {
164         struct wim_inode *inode;
165         int ret;
166
167         image_for_each_inode(inode, imd) {
168                 ret = inode_resolve_streams(inode, lookup_table, false);
169                 if (ret)
170                         return ret;
171         }
172         return 0;
173 }
174
175 /* API function documented in wimlib.h  */
176 WIMLIBAPI int
177 wimlib_verify_wim(WIMStruct *wim, int verify_flags)
178 {
179         int ret;
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,
187         };
188
189         /* Check parameters  */
190
191         if (!wim)
192                 return WIMLIB_ERR_INVALID_PARAM;
193
194         if (verify_flags)
195                 return WIMLIB_ERR_INVALID_PARAM;
196
197         /* Verify the images  */
198
199         if (wim_has_metadata(wim)) {
200
201                 memset(&progress, 0, sizeof(progress));
202                 progress.verify_image.wimfile = wim->filename;
203                 progress.verify_image.total_images = wim->hdr.image_count;
204
205                 for (int i = 1; i <= wim->hdr.image_count; i++) {
206
207                         progress.verify_image.current_image = i;
208
209                         ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_BEGIN_VERIFY_IMAGE,
210                                             &progress, wim->progctx);
211                         if (ret)
212                                 return ret;
213
214                         ret = select_wim_image(wim, i);
215                         if (ret)
216                                 return ret;
217
218                         ret = verify_image_streams_present(wim_get_current_image_metadata(wim),
219                                                            wim->lookup_table);
220                         if (ret)
221                                 return ret;
222
223                         ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_END_VERIFY_IMAGE,
224                                             &progress, wim->progctx);
225                         if (ret)
226                                 return ret;
227                 }
228         } else {
229                 WARNING("\"%"TS"\" does not contain image metadata.  Skipping image verification.",
230                         wim->filename);
231         }
232
233         /* Verify the streams  */
234
235         for_lookup_table_entry(wim->lookup_table, append_lte_to_list, &stream_list);
236
237         memset(&progress, 0, sizeof(progress));
238
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;
243         }
244
245         ctx.progfunc = wim->progfunc;
246         ctx.progctx = wim->progctx;
247         ctx.progress = &progress;
248         ctx.next_progress = 0;
249
250         ret = call_progress(ctx.progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
251                             ctx.progress, ctx.progctx);
252         if (ret)
253                 return ret;
254
255         return read_stream_list(&stream_list,
256                                 offsetof(struct wim_lookup_table_entry,
257                                          extraction_list),
258                                 &cbs, VERIFY_STREAM_HASHES);
259 }