changeset 43034:294afb982a88

sidedata: add a function to read sidedata from revlog raw text This implement the "reading" part of a `sidedata` flag processor. Differential Revision: https://phab.mercurial-scm.org/D6890
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 04 Sep 2019 00:59:15 +0200
parents 21025a4107d4
children ea83abf95630
files mercurial/error.py mercurial/revlog.py mercurial/revlogutils/sidedata.py
diffstat 3 files changed, 43 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/error.py	Mon Sep 09 14:03:12 2019 +0200
+++ b/mercurial/error.py	Wed Sep 04 00:59:15 2019 +0200
@@ -44,6 +44,13 @@
 class RevlogError(StorageError):
     __bytes__ = _tobytes
 
+class SidedataHashError(RevlogError):
+
+    def __init__(self, key, expected, got):
+        self.sidedatakey = key
+        self.expecteddigest = expected
+        self.actualdigest = got
+
 class FilteredIndexError(IndexError):
     __bytes__ = _tobytes
 
--- a/mercurial/revlog.py	Mon Sep 09 14:03:12 2019 +0200
+++ b/mercurial/revlog.py	Wed Sep 04 00:59:15 2019 +0200
@@ -1675,7 +1675,13 @@
             validatehash = flagutil.processflagsraw(self, rawtext, flags)
             text = rawtext
         else:
-            r = flagutil.processflagsread(self, rawtext, flags)
+            try:
+                r = flagutil.processflagsread(self, rawtext, flags)
+            except error.SidedataHashError as exc:
+                msg = _("integrity check failed on %s:%s sidedata key %d")
+                msg %= (self.indexfile, pycompat.bytestr(rev),
+                        exc.sidedatakey)
+                raise error.RevlogError(msg)
             text, validatehash, sidedata = r
         if validatehash:
             self.checkhash(text, node, rev=rev)
--- a/mercurial/revlogutils/sidedata.py	Mon Sep 09 14:03:12 2019 +0200
+++ b/mercurial/revlogutils/sidedata.py	Wed Sep 04 00:59:15 2019 +0200
@@ -32,3 +32,32 @@
 """
 
 from __future__ import absolute_import
+
+import hashlib
+import struct
+
+from .. import error
+
+SIDEDATA_HEADER = struct.Struct('>H')
+SIDEDATA_ENTRY = struct.Struct('>HL20s')
+
+def sidedatareadprocessor(rl, text):
+    sidedata = {}
+    offset = 0
+    nbentry, = SIDEDATA_HEADER.unpack(text[:SIDEDATA_HEADER.size])
+    offset += SIDEDATA_HEADER.size
+    dataoffset = SIDEDATA_HEADER.size + (SIDEDATA_ENTRY.size * nbentry)
+    for i in range(nbentry):
+        nextoffset = offset + SIDEDATA_ENTRY.size
+        key, size, storeddigest = SIDEDATA_ENTRY.unpack(text[offset:nextoffset])
+        offset = nextoffset
+        # read the data associated with that entry
+        nextdataoffset = dataoffset + size
+        entrytext = text[dataoffset:nextdataoffset]
+        readdigest = hashlib.sha1(entrytext).digest()
+        if storeddigest != readdigest:
+            raise error.SidedataHashError(key, storeddigest, readdigest)
+        sidedata[key] = entrytext
+        dataoffset = nextdataoffset
+    text = text[dataoffset:]
+    return text, True, sidedata