verify.py
301 lines
| 10.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / verify.py
|
r2778 | # verify.py - repository integrity checking for Mercurial | ||
# | ||||
|
r4635 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> | ||
|
r2778 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2778 | |||
|
r6211 | from node import nullid, short | ||
|
r3891 | from i18n import _ | ||
|
r9690 | import os | ||
|
r7832 | import revlog, util, error | ||
|
r2778 | |||
def verify(repo): | ||||
|
r4915 | lock = repo.lock() | ||
try: | ||||
return _verify(repo) | ||||
finally: | ||||
|
r8109 | lock.release() | ||
|
r4915 | |||
def _verify(repo): | ||||
|
r6752 | mflinkrevs = {} | ||
|
r2778 | filelinkrevs = {} | ||
filenodes = {} | ||||
|
r6752 | revisions = 0 | ||
|
r8466 | badrevs = set() | ||
|
r2778 | errors = [0] | ||
warnings = [0] | ||||
|
r6751 | ui = repo.ui | ||
cl = repo.changelog | ||||
mf = repo.manifest | ||||
|
r9657 | lrugetctx = util.lrucachefunc(repo.changectx) | ||
|
r2778 | |||
|
r7141 | if not repo.cancopy(): | ||
raise util.Abort(_("cannot verify bundle or remote repos")) | ||||
|
r5313 | def err(linkrev, msg, filename=None): | ||
if linkrev != None: | ||||
|
r8466 | badrevs.add(linkrev) | ||
|
r5313 | else: | ||
|
r6752 | linkrev = '?' | ||
|
r5313 | msg = "%s: %s" % (linkrev, msg) | ||
if filename: | ||||
msg = "%s@%s" % (filename, msg) | ||||
|
r6751 | ui.warn(" " + msg + "\n") | ||
|
r2778 | errors[0] += 1 | ||
|
r6752 | def exc(linkrev, msg, inst, filename=None): | ||
if isinstance(inst, KeyboardInterrupt): | ||||
ui.warn(_("interrupted")) | ||||
raise | ||||
|
r11752 | if not str(inst): | ||
inst = repr(inst) | ||||
|
r6752 | err(linkrev, "%s: %s" % (msg, inst), filename) | ||
|
r2778 | def warn(msg): | ||
|
r6751 | ui.warn(msg + "\n") | ||
|
r2778 | warnings[0] += 1 | ||
|
r8292 | def checklog(obj, name, linkrev): | ||
|
r6752 | if not len(obj) and (havecl or havemf): | ||
|
r8292 | err(linkrev, _("empty or missing %s") % name) | ||
|
r6752 | return | ||
|
r2778 | d = obj.checksize() | ||
if d[0]: | ||||
|
r5313 | err(None, _("data length off by %d bytes") % d[0], name) | ||
|
r2778 | if d[1]: | ||
|
r5313 | err(None, _("index contains %d extra bytes") % d[1], name) | ||
|
r2778 | |||
if obj.version != revlog.REVLOGV0: | ||||
if not revlogv1: | ||||
warn(_("warning: `%s' uses revlog format 1") % name) | ||||
elif revlogv1: | ||||
warn(_("warning: `%s' uses revlog format 0") % name) | ||||
|
r6752 | def checkentry(obj, i, node, seen, linkrevs, f): | ||
|
r7361 | lr = obj.linkrev(obj.rev(node)) | ||
|
r6752 | if lr < 0 or (havecl and lr not in linkrevs): | ||
if lr < 0 or lr >= len(cl): | ||||
|
r7926 | msg = _("rev %d points to nonexistent changeset %d") | ||
else: | ||||
msg = _("rev %d points to unexpected changeset %d") | ||||
err(None, msg % (i, lr), f) | ||||
|
r6752 | if linkrevs: | ||
|
r9657 | if f and len(linkrevs) > 1: | ||
try: | ||||
# attempt to filter down to real linkrevs | ||||
linkrevs = [l for l in linkrevs | ||||
if lrugetctx(l)[f].filenode() == node] | ||||
except: | ||||
pass | ||||
|
r9198 | warn(_(" (expected %s)") % " ".join(map(str, linkrevs))) | ||
|
r6752 | lr = None # can't be trusted | ||
try: | ||||
p1, p2 = obj.parents(node) | ||||
if p1 not in seen and p1 != nullid: | ||||
err(lr, _("unknown parent 1 %s of %s") % | ||||
(short(p1), short(n)), f) | ||||
if p2 not in seen and p2 != nullid: | ||||
err(lr, _("unknown parent 2 %s of %s") % | ||||
(short(p2), short(p1)), f) | ||||
except Exception, inst: | ||||
exc(lr, _("checking parents of %s") % short(node), inst, f) | ||||
if node in seen: | ||||
err(lr, _("duplicate revision %d (%d)") % (i, seen[n]), f) | ||||
seen[n] = i | ||||
return lr | ||||
|
r9690 | if os.path.exists(repo.sjoin("journal")): | ||
ui.warn(_("abandoned transaction found - run hg recover\n")) | ||||
|
r6751 | revlogv1 = cl.version != revlog.REVLOGV0 | ||
if ui.verbose or not revlogv1: | ||||
ui.status(_("repository uses revlog format %d\n") % | ||||
|
r2778 | (revlogv1 and 1 or 0)) | ||
|
r6752 | havecl = len(cl) > 0 | ||
havemf = len(mf) > 0 | ||||
|
r6751 | ui.status(_("checking changesets\n")) | ||
|
r6752 | seen = {} | ||
|
r8292 | checklog(cl, "changelog", 0) | ||
|
r10433 | total = len(repo) | ||
|
r6750 | for i in repo: | ||
|
r12745 | ui.progress(_('checking'), i, total=total, unit=_('changesets')) | ||
|
r6751 | n = cl.node(i) | ||
|
r6752 | checkentry(cl, i, n, seen, [i], "changelog") | ||
|
r2778 | |||
try: | ||||
|
r6751 | changes = cl.read(n) | ||
|
r6752 | mflinkrevs.setdefault(changes[0], []).append(i) | ||
for f in changes[3]: | ||||
filelinkrevs.setdefault(f, []).append(i) | ||||
|
r2778 | except Exception, inst: | ||
|
r6752 | exc(i, _("unpacking changeset %s") % short(n), inst) | ||
|
r12745 | ui.progress(_('checking'), None) | ||
|
r2778 | |||
|
r6752 | ui.status(_("checking manifests\n")) | ||
|
r2778 | seen = {} | ||
|
r8292 | checklog(mf, "manifest", 0) | ||
|
r10433 | total = len(mf) | ||
|
r6751 | for i in mf: | ||
|
r12745 | ui.progress(_('checking'), i, total=total, unit=_('manifests')) | ||
|
r6751 | n = mf.node(i) | ||
|
r6752 | lr = checkentry(mf, i, n, seen, mflinkrevs.get(n, []), "manifest") | ||
if n in mflinkrevs: | ||||
del mflinkrevs[n] | ||||
|
r8394 | else: | ||
err(lr, _("%s not in changesets") % short(n), "manifest") | ||||
|
r2778 | |||
try: | ||||
|
r6751 | for f, fn in mf.readdelta(n).iteritems(): | ||
|
r6752 | if not f: | ||
err(lr, _("file without name in manifest")) | ||||
elif f != "/dev/null": | ||||
|
r9657 | filenodes.setdefault(f, {}).setdefault(fn, lr) | ||
|
r2778 | except Exception, inst: | ||
|
r6752 | exc(lr, _("reading manifest delta %s") % short(n), inst) | ||
|
r12745 | ui.progress(_('checking'), None) | ||
|
r2778 | |||
|
r6751 | ui.status(_("crosschecking files in changesets and manifests\n")) | ||
|
r2778 | |||
|
r10433 | total = len(mflinkrevs) + len(filelinkrevs) + len(filenodes) | ||
count = 0 | ||||
|
r6752 | if havemf: | ||
|
r10282 | for c, m in sorted([(c, m) for m in mflinkrevs | ||
for c in mflinkrevs[m]]): | ||||
|
r10433 | count += 1 | ||
|
r10698 | ui.progress(_('crosschecking'), count, total=total) | ||
|
r5541 | err(c, _("changeset refers to unknown manifest %s") % short(m)) | ||
|
r9033 | mflinkrevs = None # del is bad here due to scope issues | ||
|
r2778 | |||
|
r8209 | for f in sorted(filelinkrevs): | ||
|
r10433 | count += 1 | ||
|
r10698 | ui.progress(_('crosschecking'), count, total=total) | ||
|
r5541 | if f not in filenodes: | ||
lr = filelinkrevs[f][0] | ||||
err(lr, _("in changeset but not in manifest"), f) | ||||
|
r2778 | |||
|
r6752 | if havecl: | ||
|
r8209 | for f in sorted(filenodes): | ||
|
r10433 | count += 1 | ||
|
r10698 | ui.progress(_('crosschecking'), count, total=total) | ||
|
r6752 | if f not in filelinkrevs: | ||
try: | ||||
|
r7361 | fl = repo.file(f) | ||
lr = min([fl.linkrev(fl.rev(n)) for n in filenodes[f]]) | ||||
|
r6752 | except: | ||
lr = None | ||||
err(lr, _("in manifest but not in changeset"), f) | ||||
|
r10698 | ui.progress(_('crosschecking'), None) | ||
|
r10433 | |||
|
r6751 | ui.status(_("checking files\n")) | ||
|
r6892 | |||
|
r8466 | storefiles = set() | ||
|
r6900 | for f, f2, size in repo.store.datafiles(): | ||
if not f: | ||||
err(None, _("cannot decode filename '%s'") % f2) | ||||
|
r12170 | elif size > 0 or not revlogv1: | ||
|
r8466 | storefiles.add(f) | ||
|
r6892 | |||
|
r8209 | files = sorted(set(filenodes) | set(filelinkrevs)) | ||
|
r10433 | total = len(files) | ||
for i, f in enumerate(files): | ||||
|
r10698 | ui.progress(_('checking'), i, item=f, total=total) | ||
|
r8291 | try: | ||
linkrevs = filelinkrevs[f] | ||||
except KeyError: | ||||
# in manifest but not in changelog | ||||
linkrevs = [] | ||||
if linkrevs: | ||||
lr = linkrevs[0] | ||||
else: | ||||
lr = None | ||||
|
r7832 | try: | ||
fl = repo.file(f) | ||||
except error.RevlogError, e: | ||||
|
r7833 | err(lr, _("broken revlog! (%s)") % e, f) | ||
|
r7832 | continue | ||
|
r6892 | |||
|
r6900 | for ff in fl.files(): | ||
|
r6892 | try: | ||
|
r8466 | storefiles.remove(ff) | ||
|
r6892 | except KeyError: | ||
|
r7833 | err(lr, _("missing revlog!"), ff) | ||
|
r6892 | |||
|
r8292 | checklog(fl, f, lr) | ||
|
r5313 | seen = {} | ||
|
r11756 | rp = None | ||
|
r6750 | for i in fl: | ||
|
r2778 | revisions += 1 | ||
n = fl.node(i) | ||||
|
r8291 | lr = checkentry(fl, i, n, seen, linkrevs, f) | ||
|
r5541 | if f in filenodes: | ||
if havemf and n not in filenodes[f]: | ||||
|
r6752 | err(lr, _("%s not in manifests") % (short(n)), f) | ||
|
r5541 | else: | ||
del filenodes[f][n] | ||||
|
r2778 | |||
# verify contents | ||||
try: | ||||
|
r11753 | l = len(fl.read(n)) | ||
|
r6752 | rp = fl.renamed(n) | ||
|
r11753 | if l != fl.size(i): | ||
|
r7675 | if len(fl.revision(n)) != fl.size(i): | ||
|
r6752 | err(lr, _("unpacked size is %s, %s expected") % | ||
|
r11753 | (l, fl.size(i)), f) | ||
|
r2778 | except Exception, inst: | ||
|
r6752 | exc(lr, _("unpacking %s") % short(n), inst, f) | ||
|
r2778 | |||
|
r3744 | # check renames | ||
try: | ||||
if rp: | ||||
|
r9545 | if lr is not None and ui.verbose: | ||
ctx = lrugetctx(lr) | ||||
found = False | ||||
for pctx in ctx.parents(): | ||||
if rp[0] in pctx: | ||||
found = True | ||||
break | ||||
if not found: | ||||
warn(_("warning: copy source of '%s' not" | ||||
" in parents of %s") % (f, ctx)) | ||||
|
r3744 | fl2 = repo.file(rp[0]) | ||
|
r6750 | if not len(fl2): | ||
|
r6752 | err(lr, _("empty or missing copy source revlog %s:%s") | ||
|
r6534 | % (rp[0], short(rp[1])), f) | ||
elif rp[1] == nullid: | ||||
|
r8993 | ui.note(_("warning: %s@%s: copy source" | ||
" revision is nullid %s:%s\n") | ||||
|
r7004 | % (f, lr, rp[0], short(rp[1]))) | ||
|
r6534 | else: | ||
|
r7874 | fl2.rev(rp[1]) | ||
|
r3744 | except Exception, inst: | ||
|
r6752 | exc(lr, _("checking rename of %s") % short(n), inst, f) | ||
|
r3744 | |||
|
r2778 | # cross-check | ||
|
r5541 | if f in filenodes: | ||
|
r10282 | fns = [(lr, n) for n, lr in filenodes[f].iteritems()] | ||
|
r8209 | for lr, node in sorted(fns): | ||
|
r5541 | err(lr, _("%s in manifests not found") % short(node), f) | ||
|
r10698 | ui.progress(_('checking'), None) | ||
|
r2778 | |||
|
r6892 | for f in storefiles: | ||
warn(_("warning: orphan revlog '%s'") % f) | ||||
|
r6751 | ui.status(_("%d files, %d changesets, %d total revisions\n") % | ||
|
r6752 | (len(files), len(cl), revisions)) | ||
|
r2778 | if warnings[0]: | ||
|
r6751 | ui.warn(_("%d warnings encountered!\n") % warnings[0]) | ||
|
r2778 | if errors[0]: | ||
|
r6751 | ui.warn(_("%d integrity errors encountered!\n") % errors[0]) | ||
|
r6752 | if badrevs: | ||
|
r6751 | ui.warn(_("(first damaged changeset appears to be %d)\n") | ||
|
r6752 | % min(badrevs)) | ||
|
r2778 | return 1 | ||