|
| 1 | +"""Legacy RepoObj1 — Borg 1.x repository object format. |
| 2 | +
|
| 3 | +Moved from borg.repoobj as part of the legacy code separation. |
| 4 | +""" |
| 5 | + |
| 6 | +from ..constants import * # NOQA |
| 7 | +from ..helpers import workarounds |
| 8 | +from ..compress import Compressor, get_compressor |
| 9 | + |
| 10 | +# Workaround for lost passphrase or key in "authenticated" or "authenticated-blake2" mode |
| 11 | +AUTHENTICATED_NO_KEY = "authenticated_no_key" in workarounds |
| 12 | + |
| 13 | + |
| 14 | +class RepoObj1: # legacy |
| 15 | + @classmethod |
| 16 | + def extract_crypted_data(cls, data: bytes) -> bytes: |
| 17 | + # used for crypto type detection |
| 18 | + return data |
| 19 | + |
| 20 | + def __init__(self, key): |
| 21 | + self.key = key |
| 22 | + self.compressor = get_compressor("lz4", legacy_mode=True) |
| 23 | + |
| 24 | + def id_hash(self, data: bytes) -> bytes: |
| 25 | + return self.key.id_hash(data) |
| 26 | + |
| 27 | + def format( |
| 28 | + self, |
| 29 | + id: bytes, |
| 30 | + meta: dict, |
| 31 | + data: bytes, |
| 32 | + compress: bool = True, |
| 33 | + size: int = None, |
| 34 | + ctype: int = None, |
| 35 | + clevel: int = None, |
| 36 | + ro_type: str = None, |
| 37 | + ) -> bytes: |
| 38 | + assert isinstance(id, bytes) |
| 39 | + assert meta == {} |
| 40 | + assert isinstance(data, (bytes, memoryview)) |
| 41 | + assert ro_type is not None |
| 42 | + assert compress or size is not None and ctype is not None and clevel is not None |
| 43 | + if compress: |
| 44 | + assert size is None or size == len(data) |
| 45 | + meta, data_compressed = self.compressor.compress(meta, data) |
| 46 | + else: |
| 47 | + assert isinstance(size, int) |
| 48 | + data_compressed = data # is already compressed, must include type/level bytes |
| 49 | + data_encrypted = self.key.encrypt(id, data_compressed) |
| 50 | + return data_encrypted |
| 51 | + |
| 52 | + def parse_meta(self, id: bytes, cdata: bytes) -> dict: |
| 53 | + raise NotImplementedError("parse_meta is not available for RepoObj1") |
| 54 | + |
| 55 | + def parse( |
| 56 | + self, id: bytes, cdata: bytes, decompress: bool = True, want_compressed: bool = False, ro_type: str = None |
| 57 | + ) -> tuple[dict, bytes]: |
| 58 | + assert not (not decompress and not want_compressed), "invalid parameter combination!" |
| 59 | + assert isinstance(id, bytes) |
| 60 | + assert isinstance(cdata, bytes) |
| 61 | + assert ro_type is not None |
| 62 | + data_compressed = self.key.decrypt(id, cdata) |
| 63 | + compressor_cls, compression_level = Compressor.detect(data_compressed[:2]) |
| 64 | + compressor = compressor_cls(level=compression_level, legacy_mode=True) |
| 65 | + meta_compressed = {} |
| 66 | + meta_compressed["ctype"] = compressor.ID |
| 67 | + meta_compressed["clevel"] = compressor.level |
| 68 | + meta_compressed["csize"] = len(data_compressed) |
| 69 | + if decompress: |
| 70 | + meta, data = compressor.decompress(None, data_compressed) |
| 71 | + if not AUTHENTICATED_NO_KEY: |
| 72 | + self.key.assert_id(id, data) |
| 73 | + else: |
| 74 | + meta, data = None, None |
| 75 | + return meta_compressed if want_compressed else meta, data_compressed if want_compressed else data |
0 commit comments