]> wimlib.net Git - wimlib/blob - src/verify.c
Add wimlib_verify_wim()
[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 part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
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"
36
37 static int
38 lte_fix_refcnt(struct wim_lookup_table_entry *lte, void *ctr)
39 {
40         if (lte->refcnt != lte->real_refcnt) {
41                 lte->refcnt = lte->real_refcnt;
42                 ++*(unsigned long *)ctr;
43         }
44         return 0;
45 }
46
47 static void
48 tally_inode_refcnts(const struct wim_inode *inode,
49                     const struct wim_lookup_table *lookup_table)
50 {
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);
54                 if (lte)
55                         lte->real_refcnt += inode->i_nlink;
56         }
57 }
58
59
60 static int
61 tally_image_refcnts(WIMStruct *wim)
62 {
63         const struct wim_image_metadata *imd;
64         const struct wim_inode *inode;
65
66         imd = wim_get_current_image_metadata(wim);
67         image_for_each_inode(inode, imd)
68                 tally_inode_refcnts(inode, wim->lookup_table);
69         return 0;
70 }
71
72
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. */
81 int
82 wim_recalculate_refcnts(WIMStruct *wim)
83 {
84         unsigned long num_ltes_with_bogus_refcnt = 0;
85         int ret;
86
87         for_lookup_table_entry(wim->lookup_table, lte_zero_real_refcnt, NULL);
88         ret = for_image(wim, WIMLIB_ALL_IMAGES, tally_image_refcnts);
89         if (ret)
90                 return ret;
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);
97         }
98         wim->refcnts_ok = 1;
99         return 0;
100 }
101
102 static int
103 append_lte_to_list(struct wim_lookup_table_entry *lte, void *_list)
104 {
105         list_add(&lte->extraction_list, (struct list_head *)_list);
106         return 0;
107 }
108
109 struct verify_stream_list_ctx {
110         wimlib_progress_func_t progfunc;
111         void *progctx;
112         union wimlib_progress_info *progress;
113 };
114
115 static int
116 end_verify_stream(struct wim_lookup_table_entry *lte, int status, void *_ctx)
117 {
118         struct verify_stream_list_ctx *ctx = _ctx;
119
120         if (status)
121                 return status;
122
123         ctx->progress->verify_streams.completed_streams++;
124         ctx->progress->verify_streams.completed_bytes += lte->size;
125
126         return call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
127                              ctx->progress, ctx->progctx);
128 }
129
130 static int
131 verify_image_streams_present(struct wim_image_metadata *imd,
132                              struct wim_lookup_table *lookup_table)
133 {
134         struct wim_inode *inode;
135         int ret;
136
137         image_for_each_inode(inode, imd) {
138                 ret = inode_resolve_streams(inode, lookup_table, false);
139                 if (ret)
140                         return ret;
141         }
142         return 0;
143 }
144
145 /* API function documented in wimlib.h  */
146 WIMLIBAPI int
147 wimlib_verify_wim(WIMStruct *wim, int verify_flags)
148 {
149         int ret;
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,
157         };
158
159         /* Check parameters  */
160
161         if (!wim)
162                 return WIMLIB_ERR_INVALID_PARAM;
163
164         if (verify_flags)
165                 return WIMLIB_ERR_INVALID_PARAM;
166
167         /* Verify the images  */
168
169         if (wim_has_metadata(wim)) {
170
171                 memset(&progress, 0, sizeof(progress));
172                 progress.verify_image.wimfile = wim->filename;
173                 progress.verify_image.total_images = wim->hdr.image_count;
174
175                 for (int i = 1; i <= wim->hdr.image_count; i++) {
176
177                         progress.verify_image.current_image = i;
178
179                         ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_BEGIN_VERIFY_IMAGE,
180                                             &progress, wim->progctx);
181                         if (ret)
182                                 return ret;
183
184                         ret = select_wim_image(wim, i);
185                         if (ret)
186                                 return ret;
187
188                         ret = verify_image_streams_present(wim_get_current_image_metadata(wim),
189                                                            wim->lookup_table);
190                         if (ret)
191                                 return ret;
192
193                         ret = call_progress(wim->progfunc, WIMLIB_PROGRESS_MSG_END_VERIFY_IMAGE,
194                                             &progress, wim->progctx);
195                         if (ret)
196                                 return ret;
197                 }
198         } else {
199                 WARNING("\"%"TS"\" does not contain image metadata.  Skipping image verification.",
200                         wim->filename);
201         }
202
203         /* Verify the streams  */
204
205         for_lookup_table_entry(wim->lookup_table, append_lte_to_list, &stream_list);
206
207         memset(&progress, 0, sizeof(progress));
208
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;
213         }
214
215         ctx.progfunc = wim->progfunc;
216         ctx.progctx = wim->progctx;
217         ctx.progress = &progress;
218
219         ret = call_progress(ctx.progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS,
220                             ctx.progress, ctx.progctx);
221         if (ret)
222                 return ret;
223
224         return read_stream_list(&stream_list,
225                                 offsetof(struct wim_lookup_table_entry,
226                                          extraction_list),
227                                 &cbs, VERIFY_STREAM_HASHES);
228 }