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