From: Philipp Gesang Date: Thu, 23 Mar 2017 10:48:59 +0000 (+0100) Subject: extend open_index() API for info file handling X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=b825fbef0ccf3c7ca94367385f4214dd4e9b76ed;p=python-delta-tar extend open_index() API for info file handling In fact, backup_python’s “info file” is just another “index file” to deltatar. Conceptually they’re quite different though especially regarding encryption. To allow requesting an info flavored index file, add a parameter to communicate with the crypto layer. --- diff --git a/deltatar/deltatar.py b/deltatar/deltatar.py index dd6f584..b04afc4 100644 --- a/deltatar/deltatar.py +++ b/deltatar/deltatar.py @@ -18,6 +18,8 @@ # Author: Eduardo Robles Elvira +I2N_XXX_ENCRYPTION_VERSION = 0 + import logging import datetime import binascii @@ -476,11 +478,16 @@ class DeltaTar(object): return path[len(prefix):] return path - def open_index(self, path, mode='r'): + def open_index(self, path, mode='r', kind="index"): ''' Given the specified configuration, opens the index for reading or writing. It transparently handles if the index is encrypted and/or - compressed, returning a file object reading to use. + compressed, returning a file object ready to use. + + :param kind: Role of file. The only special value is “info” + which sets the appropriate counter in the crypto + layer. + :type kind: str ''' filemode = None @@ -493,11 +500,15 @@ class DeltaTar(object): encver = None if 'aes' in self.index_mode: - encver = 1 + encver = I2N_XXX_ENCRYPTION_VERSION + counter = None + if kind == "info": # fixed counter + counter = crypto.AES_GCM_IV_CNT_INFOFILE return tarfile._Stream(name=path, mode=mode, comptype=comptype, - bufsize=tarfile.RECORDSIZE, fileobj=None, - encver=encver, password=self.password) + bufsize=tarfile.RECORDSIZE, fileobj=None, + encver=encver, enccounter=counter, + password=self.password) def create_full_backup(self, source_path, backup_path, max_volume_size=None, extra_data=dict()): diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index ef50e1d..61fcc9f 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -381,7 +381,8 @@ class _Stream: remainder = -1 # track size in encrypted entries def __init__(self, name, mode, comptype, fileobj, bufsize, - concat_stream=False, encver=None, password=None, + concat_stream=False, + encver=None, enccounter=None, password=None, nacl=None, compresslevel=9): """Construct a _Stream object. """ @@ -419,6 +420,8 @@ class _Stream: self.encryption = None self.lasthdr = None + enccounter = enccounter or crypto.AES_GCM_IV_CNT_DATA + try: if comptype == "gz": try: @@ -450,7 +453,7 @@ class _Stream: try: enc = crypto.Encrypt (password, I2N_XXX_ENCRYPTION_VERSION, nacl, - counter=crypto.AES_GCM_IV_CNT_DATA) + counter=enccounter) except ValueError as exn: raise InvalidEncryptionError \ ("ctor failed crypto.Encrypt(, “%s”, %r)" @@ -519,7 +522,7 @@ class _Stream: try: enc = crypto.Encrypt (password, I2N_XXX_ENCRYPTION_VERSION, nacl, - counter=crypto.AES_GCM_IV_CNT_DATA) + counter=enccounter) except ValueError as exn: raise InvalidEncryptionError \ ("ctor failed crypto.Encrypt(, “%s”, %r)"