##// END OF EJS Templates
verify: avoid exception on missing file revlog...
Henrik Stuart -
r8291:f5c1a909 default
parent child Browse files
Show More
@@ -1,245 +1,255
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, incorporated herein by reference.
7 7
8 8 from node import nullid, short
9 9 from i18n import _
10 10 import revlog, util, error
11 11
12 12 def verify(repo):
13 13 lock = repo.lock()
14 14 try:
15 15 return _verify(repo)
16 16 finally:
17 17 lock.release()
18 18
19 19 def _verify(repo):
20 20 mflinkrevs = {}
21 21 filelinkrevs = {}
22 22 filenodes = {}
23 23 revisions = 0
24 24 badrevs = {}
25 25 errors = [0]
26 26 warnings = [0]
27 27 ui = repo.ui
28 28 cl = repo.changelog
29 29 mf = repo.manifest
30 30
31 31 if not repo.cancopy():
32 32 raise util.Abort(_("cannot verify bundle or remote repos"))
33 33
34 34 def err(linkrev, msg, filename=None):
35 35 if linkrev != None:
36 36 badrevs[linkrev] = True
37 37 else:
38 38 linkrev = '?'
39 39 msg = "%s: %s" % (linkrev, msg)
40 40 if filename:
41 41 msg = "%s@%s" % (filename, msg)
42 42 ui.warn(" " + msg + "\n")
43 43 errors[0] += 1
44 44
45 45 def exc(linkrev, msg, inst, filename=None):
46 46 if isinstance(inst, KeyboardInterrupt):
47 47 ui.warn(_("interrupted"))
48 48 raise
49 49 err(linkrev, "%s: %s" % (msg, inst), filename)
50 50
51 51 def warn(msg):
52 52 ui.warn(msg + "\n")
53 53 warnings[0] += 1
54 54
55 55 def checklog(obj, name):
56 56 if not len(obj) and (havecl or havemf):
57 57 err(0, _("empty or missing %s") % name)
58 58 return
59 59
60 60 d = obj.checksize()
61 61 if d[0]:
62 62 err(None, _("data length off by %d bytes") % d[0], name)
63 63 if d[1]:
64 64 err(None, _("index contains %d extra bytes") % d[1], name)
65 65
66 66 if obj.version != revlog.REVLOGV0:
67 67 if not revlogv1:
68 68 warn(_("warning: `%s' uses revlog format 1") % name)
69 69 elif revlogv1:
70 70 warn(_("warning: `%s' uses revlog format 0") % name)
71 71
72 72 def checkentry(obj, i, node, seen, linkrevs, f):
73 73 lr = obj.linkrev(obj.rev(node))
74 74 if lr < 0 or (havecl and lr not in linkrevs):
75 75 if lr < 0 or lr >= len(cl):
76 76 msg = _("rev %d points to nonexistent changeset %d")
77 77 else:
78 78 msg = _("rev %d points to unexpected changeset %d")
79 79 err(None, msg % (i, lr), f)
80 80 if linkrevs:
81 81 warn(_(" (expected %s)") % " ".join(map(str,linkrevs)))
82 82 lr = None # can't be trusted
83 83
84 84 try:
85 85 p1, p2 = obj.parents(node)
86 86 if p1 not in seen and p1 != nullid:
87 87 err(lr, _("unknown parent 1 %s of %s") %
88 88 (short(p1), short(n)), f)
89 89 if p2 not in seen and p2 != nullid:
90 90 err(lr, _("unknown parent 2 %s of %s") %
91 91 (short(p2), short(p1)), f)
92 92 except Exception, inst:
93 93 exc(lr, _("checking parents of %s") % short(node), inst, f)
94 94
95 95 if node in seen:
96 96 err(lr, _("duplicate revision %d (%d)") % (i, seen[n]), f)
97 97 seen[n] = i
98 98 return lr
99 99
100 100 revlogv1 = cl.version != revlog.REVLOGV0
101 101 if ui.verbose or not revlogv1:
102 102 ui.status(_("repository uses revlog format %d\n") %
103 103 (revlogv1 and 1 or 0))
104 104
105 105 havecl = len(cl) > 0
106 106 havemf = len(mf) > 0
107 107
108 108 ui.status(_("checking changesets\n"))
109 109 seen = {}
110 110 checklog(cl, "changelog")
111 111 for i in repo:
112 112 n = cl.node(i)
113 113 checkentry(cl, i, n, seen, [i], "changelog")
114 114
115 115 try:
116 116 changes = cl.read(n)
117 117 mflinkrevs.setdefault(changes[0], []).append(i)
118 118 for f in changes[3]:
119 119 filelinkrevs.setdefault(f, []).append(i)
120 120 except Exception, inst:
121 121 exc(i, _("unpacking changeset %s") % short(n), inst)
122 122
123 123 ui.status(_("checking manifests\n"))
124 124 seen = {}
125 125 checklog(mf, "manifest")
126 126 for i in mf:
127 127 n = mf.node(i)
128 128 lr = checkentry(mf, i, n, seen, mflinkrevs.get(n, []), "manifest")
129 129 if n in mflinkrevs:
130 130 del mflinkrevs[n]
131 131
132 132 try:
133 133 for f, fn in mf.readdelta(n).iteritems():
134 134 if not f:
135 135 err(lr, _("file without name in manifest"))
136 136 elif f != "/dev/null":
137 137 fns = filenodes.setdefault(f, {})
138 138 if fn not in fns:
139 139 fns[fn] = i
140 140 except Exception, inst:
141 141 exc(lr, _("reading manifest delta %s") % short(n), inst)
142 142
143 143 ui.status(_("crosschecking files in changesets and manifests\n"))
144 144
145 145 if havemf:
146 146 for c,m in sorted([(c, m) for m in mflinkrevs for c in mflinkrevs[m]]):
147 147 err(c, _("changeset refers to unknown manifest %s") % short(m))
148 148 del mflinkrevs
149 149
150 150 for f in sorted(filelinkrevs):
151 151 if f not in filenodes:
152 152 lr = filelinkrevs[f][0]
153 153 err(lr, _("in changeset but not in manifest"), f)
154 154
155 155 if havecl:
156 156 for f in sorted(filenodes):
157 157 if f not in filelinkrevs:
158 158 try:
159 159 fl = repo.file(f)
160 160 lr = min([fl.linkrev(fl.rev(n)) for n in filenodes[f]])
161 161 except:
162 162 lr = None
163 163 err(lr, _("in manifest but not in changeset"), f)
164 164
165 165 ui.status(_("checking files\n"))
166 166
167 167 storefiles = {}
168 168 for f, f2, size in repo.store.datafiles():
169 169 if not f:
170 170 err(None, _("cannot decode filename '%s'") % f2)
171 171 elif size > 0:
172 172 storefiles[f] = True
173 173
174 174 files = sorted(set(filenodes) | set(filelinkrevs))
175 175 for f in files:
176 lr = filelinkrevs[f][0]
176 try:
177 linkrevs = filelinkrevs[f]
178 except KeyError:
179 # in manifest but not in changelog
180 linkrevs = []
181
182 if linkrevs:
183 lr = linkrevs[0]
184 else:
185 lr = None
186
177 187 try:
178 188 fl = repo.file(f)
179 189 except error.RevlogError, e:
180 190 err(lr, _("broken revlog! (%s)") % e, f)
181 191 continue
182 192
183 193 for ff in fl.files():
184 194 try:
185 195 del storefiles[ff]
186 196 except KeyError:
187 197 err(lr, _("missing revlog!"), ff)
188 198
189 199 checklog(fl, f)
190 200 seen = {}
191 201 for i in fl:
192 202 revisions += 1
193 203 n = fl.node(i)
194 lr = checkentry(fl, i, n, seen, filelinkrevs.get(f, []), f)
204 lr = checkentry(fl, i, n, seen, linkrevs, f)
195 205 if f in filenodes:
196 206 if havemf and n not in filenodes[f]:
197 207 err(lr, _("%s not in manifests") % (short(n)), f)
198 208 else:
199 209 del filenodes[f][n]
200 210
201 211 # verify contents
202 212 try:
203 213 t = fl.read(n)
204 214 rp = fl.renamed(n)
205 215 if len(t) != fl.size(i):
206 216 if len(fl.revision(n)) != fl.size(i):
207 217 err(lr, _("unpacked size is %s, %s expected") %
208 218 (len(t), fl.size(i)), f)
209 219 except Exception, inst:
210 220 exc(lr, _("unpacking %s") % short(n), inst, f)
211 221
212 222 # check renames
213 223 try:
214 224 if rp:
215 225 fl2 = repo.file(rp[0])
216 226 if not len(fl2):
217 227 err(lr, _("empty or missing copy source revlog %s:%s")
218 228 % (rp[0], short(rp[1])), f)
219 229 elif rp[1] == nullid:
220 230 warn(_("warning: %s@%s: copy source revision is nullid %s:%s")
221 231 % (f, lr, rp[0], short(rp[1])))
222 232 else:
223 233 fl2.rev(rp[1])
224 234 except Exception, inst:
225 235 exc(lr, _("checking rename of %s") % short(n), inst, f)
226 236
227 237 # cross-check
228 238 if f in filenodes:
229 239 fns = [(mf.linkrev(l), n) for n,l in filenodes[f].iteritems()]
230 240 for lr, node in sorted(fns):
231 241 err(lr, _("%s in manifests not found") % short(node), f)
232 242
233 243 for f in storefiles:
234 244 warn(_("warning: orphan revlog '%s'") % f)
235 245
236 246 ui.status(_("%d files, %d changesets, %d total revisions\n") %
237 247 (len(files), len(cl), revisions))
238 248 if warnings[0]:
239 249 ui.warn(_("%d warnings encountered!\n") % warnings[0])
240 250 if errors[0]:
241 251 ui.warn(_("%d integrity errors encountered!\n") % errors[0])
242 252 if badrevs:
243 253 ui.warn(_("(first damaged changeset appears to be %d)\n")
244 254 % min(badrevs))
245 255 return 1
General Comments 0
You need to be logged in to leave comments. Login now