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