Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 22963:56e04741bbf1
util: add a file handle wrapper class that does hash digest validation
It is going to be used for the remote-changegroup feature in bundle2.
author | Mike Hommey <mh@glandium.org> |
---|---|
date | Thu, 16 Oct 2014 17:03:21 +0900 |
parents | 4d58f4083148 |
children | a0e0aa12b672 |
comparison
equal
deleted
inserted
replaced
22962:4d58f4083148 | 22963:56e04741bbf1 |
---|---|
181 for k in DIGESTS_BY_STRENGTH: | 181 for k in DIGESTS_BY_STRENGTH: |
182 if k in supported: | 182 if k in supported: |
183 return k | 183 return k |
184 return None | 184 return None |
185 | 185 |
186 class digestchecker(object): | |
187 """file handle wrapper that additionally checks content against a given | |
188 size and digests. | |
189 | |
190 d = digestchecker(fh, size, {'md5': '...'}) | |
191 | |
192 When multiple digests are given, all of them are validated. | |
193 """ | |
194 | |
195 def __init__(self, fh, size, digests): | |
196 self._fh = fh | |
197 self._size = size | |
198 self._got = 0 | |
199 self._digests = dict(digests) | |
200 self._digester = digester(self._digests.keys()) | |
201 | |
202 def read(self, length=-1): | |
203 content = self._fh.read(length) | |
204 self._digester.update(content) | |
205 self._got += len(content) | |
206 return content | |
207 | |
208 def validate(self): | |
209 if self._size != self._got: | |
210 raise Abort(_('size mismatch: expected %d, got %d') % | |
211 (self._size, self._got)) | |
212 for k, v in self._digests.items(): | |
213 if v != self._digester[k]: | |
214 raise Abort(_('%s mismatch: expected %s, got %s') % | |
215 (k, v, self._digester[k])) | |
216 | |
186 try: | 217 try: |
187 buffer = buffer | 218 buffer = buffer |
188 except NameError: | 219 except NameError: |
189 if sys.version_info[0] < 3: | 220 if sys.version_info[0] < 3: |
190 def buffer(sliceable, offset=0): | 221 def buffer(sliceable, offset=0): |