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