Show More
@@ -1,305 +1,303 b'' | |||
|
1 | 1 | # verify.py - repository integrity checking for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from node import nullid, short |
|
9 | 9 | from i18n import _ |
|
10 | 10 | import os |
|
11 | 11 | import revlog, util, error |
|
12 | 12 | |
|
13 | 13 | def verify(repo): |
|
14 | 14 | lock = repo.lock() |
|
15 | 15 | try: |
|
16 | 16 | return _verify(repo) |
|
17 | 17 | finally: |
|
18 | 18 | lock.release() |
|
19 | 19 | |
|
20 | 20 | def _verify(repo): |
|
21 | 21 | mflinkrevs = {} |
|
22 | 22 | filelinkrevs = {} |
|
23 | 23 | filenodes = {} |
|
24 | 24 | revisions = 0 |
|
25 | 25 | badrevs = set() |
|
26 | 26 | errors = [0] |
|
27 | 27 | warnings = [0] |
|
28 | 28 | ui = repo.ui |
|
29 | 29 | cl = repo.changelog |
|
30 | 30 | mf = repo.manifest |
|
31 | 31 | lrugetctx = util.lrucachefunc(repo.changectx) |
|
32 | 32 | |
|
33 | 33 | if not repo.cancopy(): |
|
34 | 34 | raise util.Abort(_("cannot verify bundle or remote repos")) |
|
35 | 35 | |
|
36 | 36 | def err(linkrev, msg, filename=None): |
|
37 | 37 | if linkrev is not None: |
|
38 | 38 | badrevs.add(linkrev) |
|
39 | 39 | else: |
|
40 | 40 | linkrev = '?' |
|
41 | 41 | msg = "%s: %s" % (linkrev, msg) |
|
42 | 42 | if filename: |
|
43 | 43 | msg = "%s@%s" % (filename, msg) |
|
44 | 44 | ui.warn(" " + msg + "\n") |
|
45 | 45 | errors[0] += 1 |
|
46 | 46 | |
|
47 | 47 | def exc(linkrev, msg, inst, filename=None): |
|
48 | 48 | if isinstance(inst, KeyboardInterrupt): |
|
49 | 49 | ui.warn(_("interrupted")) |
|
50 | 50 | raise |
|
51 | 51 | if not str(inst): |
|
52 | 52 | inst = repr(inst) |
|
53 | 53 | err(linkrev, "%s: %s" % (msg, inst), filename) |
|
54 | 54 | |
|
55 | 55 | def warn(msg): |
|
56 | 56 | ui.warn(msg + "\n") |
|
57 | 57 | warnings[0] += 1 |
|
58 | 58 | |
|
59 | 59 | def checklog(obj, name, linkrev): |
|
60 | 60 | if not len(obj) and (havecl or havemf): |
|
61 | 61 | err(linkrev, _("empty or missing %s") % name) |
|
62 | 62 | return |
|
63 | 63 | |
|
64 | 64 | d = obj.checksize() |
|
65 | 65 | if d[0]: |
|
66 | 66 | err(None, _("data length off by %d bytes") % d[0], name) |
|
67 | 67 | if d[1]: |
|
68 | 68 | err(None, _("index contains %d extra bytes") % d[1], name) |
|
69 | 69 | |
|
70 | 70 | if obj.version != revlog.REVLOGV0: |
|
71 | 71 | if not revlogv1: |
|
72 | 72 | warn(_("warning: `%s' uses revlog format 1") % name) |
|
73 | 73 | elif revlogv1: |
|
74 | 74 | warn(_("warning: `%s' uses revlog format 0") % name) |
|
75 | 75 | |
|
76 | 76 | def checkentry(obj, i, node, seen, linkrevs, f): |
|
77 | 77 | lr = obj.linkrev(obj.rev(node)) |
|
78 | 78 | if lr < 0 or (havecl and lr not in linkrevs): |
|
79 | 79 | if lr < 0 or lr >= len(cl): |
|
80 | 80 | msg = _("rev %d points to nonexistent changeset %d") |
|
81 | 81 | else: |
|
82 | 82 | msg = _("rev %d points to unexpected changeset %d") |
|
83 | 83 | err(None, msg % (i, lr), f) |
|
84 | 84 | if linkrevs: |
|
85 | 85 | if f and len(linkrevs) > 1: |
|
86 | 86 | try: |
|
87 | 87 | # attempt to filter down to real linkrevs |
|
88 | 88 | linkrevs = [l for l in linkrevs |
|
89 | 89 | if lrugetctx(l)[f].filenode() == node] |
|
90 | 90 | except: |
|
91 | 91 | pass |
|
92 | 92 | warn(_(" (expected %s)") % " ".join(map(str, linkrevs))) |
|
93 | 93 | lr = None # can't be trusted |
|
94 | 94 | |
|
95 | 95 | try: |
|
96 | 96 | p1, p2 = obj.parents(node) |
|
97 | 97 | if p1 not in seen and p1 != nullid: |
|
98 | 98 | err(lr, _("unknown parent 1 %s of %s") % |
|
99 | 99 | (short(p1), short(n)), f) |
|
100 | 100 | if p2 not in seen and p2 != nullid: |
|
101 | 101 | err(lr, _("unknown parent 2 %s of %s") % |
|
102 | 102 | (short(p2), short(p1)), f) |
|
103 | 103 | except Exception, inst: |
|
104 | 104 | exc(lr, _("checking parents of %s") % short(node), inst, f) |
|
105 | 105 | |
|
106 | 106 | if node in seen: |
|
107 | 107 | err(lr, _("duplicate revision %d (%d)") % (i, seen[n]), f) |
|
108 | 108 | seen[n] = i |
|
109 | 109 | return lr |
|
110 | 110 | |
|
111 | 111 | if os.path.exists(repo.sjoin("journal")): |
|
112 | 112 | ui.warn(_("abandoned transaction found - run hg recover\n")) |
|
113 | 113 | |
|
114 | 114 | revlogv1 = cl.version != revlog.REVLOGV0 |
|
115 | 115 | if ui.verbose or not revlogv1: |
|
116 | 116 | ui.status(_("repository uses revlog format %d\n") % |
|
117 | 117 | (revlogv1 and 1 or 0)) |
|
118 | 118 | |
|
119 | 119 | havecl = len(cl) > 0 |
|
120 | 120 | havemf = len(mf) > 0 |
|
121 | 121 | |
|
122 | 122 | ui.status(_("checking changesets\n")) |
|
123 | 123 | seen = {} |
|
124 | 124 | checklog(cl, "changelog", 0) |
|
125 | 125 | total = len(repo) |
|
126 | 126 | for i in repo: |
|
127 | 127 | ui.progress(_('checking'), i, total=total, unit=_('changesets')) |
|
128 | 128 | n = cl.node(i) |
|
129 | 129 | checkentry(cl, i, n, seen, [i], "changelog") |
|
130 | 130 | |
|
131 | 131 | try: |
|
132 | 132 | changes = cl.read(n) |
|
133 | 133 | mflinkrevs.setdefault(changes[0], []).append(i) |
|
134 | 134 | for f in changes[3]: |
|
135 | 135 | filelinkrevs.setdefault(f, []).append(i) |
|
136 | 136 | except Exception, inst: |
|
137 | 137 | exc(i, _("unpacking changeset %s") % short(n), inst) |
|
138 | 138 | ui.progress(_('checking'), None) |
|
139 | 139 | |
|
140 | 140 | ui.status(_("checking manifests\n")) |
|
141 | 141 | seen = {} |
|
142 | 142 | checklog(mf, "manifest", 0) |
|
143 | 143 | total = len(mf) |
|
144 | 144 | for i in mf: |
|
145 | 145 | ui.progress(_('checking'), i, total=total, unit=_('manifests')) |
|
146 | 146 | n = mf.node(i) |
|
147 | 147 | lr = checkentry(mf, i, n, seen, mflinkrevs.get(n, []), "manifest") |
|
148 | 148 | if n in mflinkrevs: |
|
149 | 149 | del mflinkrevs[n] |
|
150 | 150 | else: |
|
151 | 151 | err(lr, _("%s not in changesets") % short(n), "manifest") |
|
152 | 152 | |
|
153 | 153 | try: |
|
154 | 154 | for f, fn in mf.readdelta(n).iteritems(): |
|
155 | 155 | if not f: |
|
156 | 156 | err(lr, _("file without name in manifest")) |
|
157 | 157 | elif f != "/dev/null": |
|
158 | 158 | filenodes.setdefault(f, {}).setdefault(fn, lr) |
|
159 | 159 | except Exception, inst: |
|
160 | 160 | exc(lr, _("reading manifest delta %s") % short(n), inst) |
|
161 | 161 | ui.progress(_('checking'), None) |
|
162 | 162 | |
|
163 | 163 | ui.status(_("crosschecking files in changesets and manifests\n")) |
|
164 | 164 | |
|
165 | 165 | total = len(mflinkrevs) + len(filelinkrevs) + len(filenodes) |
|
166 | 166 | count = 0 |
|
167 | 167 | if havemf: |
|
168 | 168 | for c, m in sorted([(c, m) for m in mflinkrevs |
|
169 | 169 | for c in mflinkrevs[m]]): |
|
170 | 170 | count += 1 |
|
171 | 171 | if m == nullid: |
|
172 | 172 | continue |
|
173 | 173 | ui.progress(_('crosschecking'), count, total=total) |
|
174 | 174 | err(c, _("changeset refers to unknown manifest %s") % short(m)) |
|
175 | 175 | mflinkrevs = None # del is bad here due to scope issues |
|
176 | 176 | |
|
177 | 177 | for f in sorted(filelinkrevs): |
|
178 | 178 | count += 1 |
|
179 | if m == nullid: | |
|
180 | continue | |
|
181 | 179 | ui.progress(_('crosschecking'), count, total=total) |
|
182 | 180 | if f not in filenodes: |
|
183 | 181 | lr = filelinkrevs[f][0] |
|
184 | 182 | err(lr, _("in changeset but not in manifest"), f) |
|
185 | 183 | |
|
186 | 184 | if havecl: |
|
187 | 185 | for f in sorted(filenodes): |
|
188 | 186 | count += 1 |
|
189 | 187 | ui.progress(_('crosschecking'), count, total=total) |
|
190 | 188 | if f not in filelinkrevs: |
|
191 | 189 | try: |
|
192 | 190 | fl = repo.file(f) |
|
193 | 191 | lr = min([fl.linkrev(fl.rev(n)) for n in filenodes[f]]) |
|
194 | 192 | except: |
|
195 | 193 | lr = None |
|
196 | 194 | err(lr, _("in manifest but not in changeset"), f) |
|
197 | 195 | |
|
198 | 196 | ui.progress(_('crosschecking'), None) |
|
199 | 197 | |
|
200 | 198 | ui.status(_("checking files\n")) |
|
201 | 199 | |
|
202 | 200 | storefiles = set() |
|
203 | 201 | for f, f2, size in repo.store.datafiles(): |
|
204 | 202 | if not f: |
|
205 | 203 | err(None, _("cannot decode filename '%s'") % f2) |
|
206 | 204 | elif size > 0 or not revlogv1: |
|
207 | 205 | storefiles.add(f) |
|
208 | 206 | |
|
209 | 207 | files = sorted(set(filenodes) | set(filelinkrevs)) |
|
210 | 208 | total = len(files) |
|
211 | 209 | for i, f in enumerate(files): |
|
212 | 210 | ui.progress(_('checking'), i, item=f, total=total) |
|
213 | 211 | try: |
|
214 | 212 | linkrevs = filelinkrevs[f] |
|
215 | 213 | except KeyError: |
|
216 | 214 | # in manifest but not in changelog |
|
217 | 215 | linkrevs = [] |
|
218 | 216 | |
|
219 | 217 | if linkrevs: |
|
220 | 218 | lr = linkrevs[0] |
|
221 | 219 | else: |
|
222 | 220 | lr = None |
|
223 | 221 | |
|
224 | 222 | try: |
|
225 | 223 | fl = repo.file(f) |
|
226 | 224 | except error.RevlogError, e: |
|
227 | 225 | err(lr, _("broken revlog! (%s)") % e, f) |
|
228 | 226 | continue |
|
229 | 227 | |
|
230 | 228 | for ff in fl.files(): |
|
231 | 229 | try: |
|
232 | 230 | storefiles.remove(ff) |
|
233 | 231 | except KeyError: |
|
234 | 232 | err(lr, _("missing revlog!"), ff) |
|
235 | 233 | |
|
236 | 234 | checklog(fl, f, lr) |
|
237 | 235 | seen = {} |
|
238 | 236 | rp = None |
|
239 | 237 | for i in fl: |
|
240 | 238 | revisions += 1 |
|
241 | 239 | n = fl.node(i) |
|
242 | 240 | lr = checkentry(fl, i, n, seen, linkrevs, f) |
|
243 | 241 | if f in filenodes: |
|
244 | 242 | if havemf and n not in filenodes[f]: |
|
245 | 243 | err(lr, _("%s not in manifests") % (short(n)), f) |
|
246 | 244 | else: |
|
247 | 245 | del filenodes[f][n] |
|
248 | 246 | |
|
249 | 247 | # verify contents |
|
250 | 248 | try: |
|
251 | 249 | l = len(fl.read(n)) |
|
252 | 250 | rp = fl.renamed(n) |
|
253 | 251 | if l != fl.size(i): |
|
254 | 252 | if len(fl.revision(n)) != fl.size(i): |
|
255 | 253 | err(lr, _("unpacked size is %s, %s expected") % |
|
256 | 254 | (l, fl.size(i)), f) |
|
257 | 255 | except Exception, inst: |
|
258 | 256 | exc(lr, _("unpacking %s") % short(n), inst, f) |
|
259 | 257 | |
|
260 | 258 | # check renames |
|
261 | 259 | try: |
|
262 | 260 | if rp: |
|
263 | 261 | if lr is not None and ui.verbose: |
|
264 | 262 | ctx = lrugetctx(lr) |
|
265 | 263 | found = False |
|
266 | 264 | for pctx in ctx.parents(): |
|
267 | 265 | if rp[0] in pctx: |
|
268 | 266 | found = True |
|
269 | 267 | break |
|
270 | 268 | if not found: |
|
271 | 269 | warn(_("warning: copy source of '%s' not" |
|
272 | 270 | " in parents of %s") % (f, ctx)) |
|
273 | 271 | fl2 = repo.file(rp[0]) |
|
274 | 272 | if not len(fl2): |
|
275 | 273 | err(lr, _("empty or missing copy source revlog %s:%s") |
|
276 | 274 | % (rp[0], short(rp[1])), f) |
|
277 | 275 | elif rp[1] == nullid: |
|
278 | 276 | ui.note(_("warning: %s@%s: copy source" |
|
279 | 277 | " revision is nullid %s:%s\n") |
|
280 | 278 | % (f, lr, rp[0], short(rp[1]))) |
|
281 | 279 | else: |
|
282 | 280 | fl2.rev(rp[1]) |
|
283 | 281 | except Exception, inst: |
|
284 | 282 | exc(lr, _("checking rename of %s") % short(n), inst, f) |
|
285 | 283 | |
|
286 | 284 | # cross-check |
|
287 | 285 | if f in filenodes: |
|
288 | 286 | fns = [(lr, n) for n, lr in filenodes[f].iteritems()] |
|
289 | 287 | for lr, node in sorted(fns): |
|
290 | 288 | err(lr, _("%s in manifests not found") % short(node), f) |
|
291 | 289 | ui.progress(_('checking'), None) |
|
292 | 290 | |
|
293 | 291 | for f in storefiles: |
|
294 | 292 | warn(_("warning: orphan revlog '%s'") % f) |
|
295 | 293 | |
|
296 | 294 | ui.status(_("%d files, %d changesets, %d total revisions\n") % |
|
297 | 295 | (len(files), len(cl), revisions)) |
|
298 | 296 | if warnings[0]: |
|
299 | 297 | ui.warn(_("%d warnings encountered!\n") % warnings[0]) |
|
300 | 298 | if errors[0]: |
|
301 | 299 | ui.warn(_("%d integrity errors encountered!\n") % errors[0]) |
|
302 | 300 | if badrevs: |
|
303 | 301 | ui.warn(_("(first damaged changeset appears to be %d)\n") |
|
304 | 302 | % min(badrevs)) |
|
305 | 303 | return 1 |
General Comments 0
You need to be logged in to leave comments.
Login now