# sidedata.py - Logic around store extra data alongside revlog revisions # # Copyright 2019 Pierre-Yves David sidedata (repeated N times): normal raw text: This is a simple and effective format. It should be enought to experiment with the concept. """ from __future__ import absolute_import import hashlib import struct from .. import error SIDEDATA_HEADER = struct.Struct('>H') SIDEDATA_ENTRY = struct.Struct('>HL20s') def sidedatawriteprocessor(rl, text, sidedata): sidedata = list(sidedata.items()) sidedata.sort() rawtext = [SIDEDATA_HEADER.pack(len(sidedata))] for key, value in sidedata: digest = hashlib.sha1(value).digest() rawtext.append(SIDEDATA_ENTRY.pack(key, len(value), digest)) for key, value in sidedata: rawtext.append(value) rawtext.append(bytes(text)) return ''.join(rawtext), False 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 def sidedatarawprocessor(rl, text): # side data modifies rawtext and prevent rawtext hash validation return False processors = ( sidedatareadprocessor, sidedatawriteprocessor, sidedatarawprocessor, )