Show More
@@ -1,127 +1,132 b'' | |||
|
1 | 1 | # store.py - repository store handling for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2008 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 | from i18n import _ | |
|
8 | 9 | import os, stat, osutil, util |
|
9 | 10 | |
|
10 | 11 | def _buildencodefun(): |
|
11 | 12 | e = '_' |
|
12 | 13 | win_reserved = [ord(x) for x in '\\:*?"<>|'] |
|
13 | 14 | cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ]) |
|
14 | 15 | for x in (range(32) + range(126, 256) + win_reserved): |
|
15 | 16 | cmap[chr(x)] = "~%02x" % x |
|
16 | 17 | for x in range(ord("A"), ord("Z")+1) + [ord(e)]: |
|
17 | 18 | cmap[chr(x)] = e + chr(x).lower() |
|
18 | 19 | dmap = {} |
|
19 | 20 | for k, v in cmap.iteritems(): |
|
20 | 21 | dmap[v] = k |
|
21 | 22 | def decode(s): |
|
22 | 23 | i = 0 |
|
23 | 24 | while i < len(s): |
|
24 | 25 | for l in xrange(1, 4): |
|
25 | 26 | try: |
|
26 | 27 | yield dmap[s[i:i+l]] |
|
27 | 28 | i += l |
|
28 | 29 | break |
|
29 | 30 | except KeyError: |
|
30 | 31 | pass |
|
31 | 32 | else: |
|
32 | 33 | raise KeyError |
|
33 | 34 | return (lambda s: "".join([cmap[c] for c in s]), |
|
34 | 35 | lambda s: "".join(list(decode(s)))) |
|
35 | 36 | |
|
36 | 37 | encodefilename, decodefilename = _buildencodefun() |
|
37 | 38 | |
|
38 | 39 | def _dirwalk(path, recurse): |
|
39 | 40 | '''yields (filename, size)''' |
|
40 | 41 | for e, kind, st in osutil.listdir(path, stat=True): |
|
41 | 42 | pe = os.path.join(path, e) |
|
42 | 43 | if kind == stat.S_IFDIR: |
|
43 | 44 | if recurse: |
|
44 | 45 | for x in _dirwalk(pe, True): |
|
45 | 46 | yield x |
|
46 | 47 | elif kind == stat.S_IFREG: |
|
47 | 48 | yield pe, st.st_size |
|
48 | 49 | |
|
49 | 50 | class _store: |
|
50 | 51 | '''base class for local repository stores''' |
|
51 | 52 | def __init__(self, path): |
|
52 | 53 | self.path = path |
|
53 | 54 | try: |
|
54 | 55 | # files in .hg/ will be created using this mode |
|
55 | 56 | mode = os.stat(self.path).st_mode |
|
56 | 57 | # avoid some useless chmods |
|
57 | 58 | if (0777 & ~util._umask) == (0777 & mode): |
|
58 | 59 | mode = None |
|
59 | 60 | except OSError: |
|
60 | 61 | mode = None |
|
61 | 62 | self.createmode = mode |
|
62 | 63 | |
|
63 | 64 | def join(self, f): |
|
64 | 65 | return os.path.join(self.path, f) |
|
65 | 66 | |
|
66 | 67 | def _revlogfiles(self, relpath='', recurse=False): |
|
67 | 68 | '''yields (filename, size)''' |
|
68 | 69 | if relpath: |
|
69 | 70 | path = os.path.join(self.path, relpath) |
|
70 | 71 | else: |
|
71 | 72 | path = self.path |
|
72 | 73 | if not os.path.isdir(path): |
|
73 | 74 | return |
|
74 | 75 | striplen = len(self.path) + len(os.sep) |
|
75 | 76 | filetypes = ('.d', '.i') |
|
76 | 77 | for f, size in _dirwalk(path, recurse): |
|
77 | 78 | if (len(f) > 2) and f[-2:] in filetypes: |
|
78 | 79 | yield util.pconvert(f[striplen:]), size |
|
79 | 80 | |
|
80 |
def |
|
|
81 | def datafiles(self, reporterror=None): | |
|
81 | 82 | for x in self._revlogfiles('data', True): |
|
82 | 83 | yield x |
|
83 | 84 | |
|
84 | 85 | def walk(self): |
|
85 | 86 | '''yields (direncoded filename, size)''' |
|
86 | 87 | # yield data files first |
|
87 |
for x in self. |
|
|
88 | for x in self.datafiles(): | |
|
88 | 89 | yield x |
|
89 | 90 | # yield manifest before changelog |
|
90 | 91 | meta = util.sort(self._revlogfiles()) |
|
91 | 92 | meta.reverse() |
|
92 | 93 | for x in meta: |
|
93 | 94 | yield x |
|
94 | 95 | |
|
95 | 96 | class directstore(_store): |
|
96 | 97 | def __init__(self, path): |
|
97 | 98 | _store.__init__(self, path) |
|
98 | self.encodefn = lambda x: x | |
|
99 | 99 | self.opener = util.opener(self.path) |
|
100 | 100 | self.opener.createmode = self.createmode |
|
101 | 101 | |
|
102 | 102 | class encodedstore(_store): |
|
103 | 103 | def __init__(self, path): |
|
104 | 104 | _store.__init__(self, os.path.join(path, 'store')) |
|
105 | 105 | self.encodefn = encodefilename |
|
106 | 106 | op = util.opener(self.path) |
|
107 | 107 | op.createmode = self.createmode |
|
108 | 108 | self.opener = lambda f, *args, **kw: op(self.encodefn(f), *args, **kw) |
|
109 | 109 | |
|
110 |
def |
|
|
110 | def datafiles(self, reporterror=None): | |
|
111 | 111 | for f, size in self._revlogfiles('data', True): |
|
112 | yield decodefilename(f), size | |
|
112 | try: | |
|
113 | yield decodefilename(f), size | |
|
114 | except KeyError: | |
|
115 | if not reporterror: | |
|
116 | raise | |
|
117 | reporterror(_("cannot decode filename '%s'") % f) | |
|
113 | 118 | |
|
114 | 119 | def join(self, f): |
|
115 | 120 | return os.path.join(self.path, self.encodefn(f)) |
|
116 | 121 | |
|
117 | 122 | def encodefn(requirements): |
|
118 | 123 | if 'store' not in requirements: |
|
119 | 124 | return lambda x: x |
|
120 | 125 | else: |
|
121 | 126 | return encodefilename |
|
122 | 127 | |
|
123 | 128 | def store(requirements, path): |
|
124 | 129 | if 'store' not in requirements: |
|
125 | 130 | return directstore(path) |
|
126 | 131 | else: |
|
127 | 132 | return encodedstore(path) |
@@ -1,217 +1,233 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 | ||
|
162 | storefiles = {} | |
|
163 | for f, size in repo.store.datafiles(lambda m: err(None, m)): | |
|
164 | if size > 0: | |
|
165 | storefiles[f] = True | |
|
166 | ||
|
161 | 167 | files = util.sort(util.unique(filenodes.keys() + filelinkrevs.keys())) |
|
162 | 168 | for f in files: |
|
163 | 169 | fl = repo.file(f) |
|
170 | ||
|
171 | for ff in fl.files(): | |
|
172 | try: | |
|
173 | del storefiles[ff] | |
|
174 | except KeyError: | |
|
175 | err(0, _("missing revlog!"), ff) | |
|
176 | ||
|
164 | 177 | checklog(fl, f) |
|
165 | 178 | seen = {} |
|
166 | 179 | for i in fl: |
|
167 | 180 | revisions += 1 |
|
168 | 181 | n = fl.node(i) |
|
169 | 182 | lr = checkentry(fl, i, n, seen, filelinkrevs.get(f, []), f) |
|
170 | 183 | if f in filenodes: |
|
171 | 184 | if havemf and n not in filenodes[f]: |
|
172 | 185 | err(lr, _("%s not in manifests") % (short(n)), f) |
|
173 | 186 | else: |
|
174 | 187 | del filenodes[f][n] |
|
175 | 188 | |
|
176 | 189 | # verify contents |
|
177 | 190 | try: |
|
178 | 191 | t = fl.read(n) |
|
179 | 192 | rp = fl.renamed(n) |
|
180 | 193 | if len(t) != fl.size(i): |
|
181 | 194 | if not fl._readmeta(n): # ancient copy? |
|
182 | 195 | err(lr, _("unpacked size is %s, %s expected") % |
|
183 | 196 | (len(t), fl.size(i)), f) |
|
184 | 197 | except Exception, inst: |
|
185 | 198 | exc(lr, _("unpacking %s") % short(n), inst, f) |
|
186 | 199 | |
|
187 | 200 | # check renames |
|
188 | 201 | try: |
|
189 | 202 | if rp: |
|
190 | 203 | fl2 = repo.file(rp[0]) |
|
191 | 204 | if not len(fl2): |
|
192 | 205 | err(lr, _("empty or missing copy source revlog %s:%s") |
|
193 | 206 | % (rp[0], short(rp[1])), f) |
|
194 | 207 | elif rp[1] == nullid: |
|
195 | 208 | warn(lr, _("copy source revision is nullid %s:%s") |
|
196 | 209 | % (rp[0], short(rp[1])), f) |
|
197 | 210 | else: |
|
198 | 211 | rev = fl2.rev(rp[1]) |
|
199 | 212 | except Exception, inst: |
|
200 | 213 | exc(lr, _("checking rename of %s") % short(n), inst, f) |
|
201 | 214 | |
|
202 | 215 | # cross-check |
|
203 | 216 | if f in filenodes: |
|
204 | 217 | fns = [(mf.linkrev(l), n) for n,l in filenodes[f].items()] |
|
205 | 218 | for lr, node in util.sort(fns): |
|
206 | 219 | err(lr, _("%s in manifests not found") % short(node), f) |
|
207 | 220 | |
|
221 | for f in storefiles: | |
|
222 | warn(_("warning: orphan revlog '%s'") % f) | |
|
223 | ||
|
208 | 224 | ui.status(_("%d files, %d changesets, %d total revisions\n") % |
|
209 | 225 | (len(files), len(cl), revisions)) |
|
210 | 226 | if warnings[0]: |
|
211 | 227 | ui.warn(_("%d warnings encountered!\n") % warnings[0]) |
|
212 | 228 | if errors[0]: |
|
213 | 229 | ui.warn(_("%d integrity errors encountered!\n") % errors[0]) |
|
214 | 230 | if badrevs: |
|
215 | 231 | ui.warn(_("(first damaged changeset appears to be %d)\n") |
|
216 | 232 | % min(badrevs)) |
|
217 | 233 | return 1 |
General Comments 0
You need to be logged in to leave comments.
Login now