1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-23 08:16:54 +00:00

use unpacker.tell() instead of deprecated write_bytes, fixes #3899

This commit is contained in:
Thomas Waldmann 2018-06-13 01:01:15 +02:00
parent 45018489b5
commit a497fe07ab

View file

@ -126,7 +126,7 @@ def iter_archive_items(self, archive_item_ids, filter=None, consider_part_files=
stream_offset = 0
# Offset of the current chunk in the metadata stream
chunk_begin = 0
# Length of the chunk preciding the current chunk
# Length of the chunk preceding the current chunk
last_chunk_length = 0
msgpacked_bytes = b''
@ -134,13 +134,6 @@ def iter_archive_items(self, archive_item_ids, filter=None, consider_part_files=
meta = self.meta
pack_indirect_into = self.indirect_entry_struct.pack_into
def write_bytes(append_msgpacked_bytes):
# XXX: Future versions of msgpack include an Unpacker.tell() method that provides this for free.
nonlocal msgpacked_bytes
nonlocal stream_offset
msgpacked_bytes += append_msgpacked_bytes
stream_offset += len(append_msgpacked_bytes)
for key, (csize, data) in zip(archive_item_ids, self.decrypted_repository.get_many(archive_item_ids)):
# Store the chunk ID in the meta-array
if write_offset + 32 >= len(meta):
@ -149,16 +142,25 @@ def write_bytes(append_msgpacked_bytes):
current_id_offset = write_offset
write_offset += 32
# The chunk boundaries cannot be tracked through write_bytes, because the unpack state machine
# *can* and *will* consume partial items, so calls to write_bytes are unrelated to chunk boundaries.
chunk_begin += last_chunk_length
last_chunk_length = len(data)
unpacker.feed(data)
while True:
try:
item = unpacker.unpack(write_bytes)
item = unpacker.unpack()
need_more_data = False
except msgpack.OutOfData:
need_more_data = True
start = stream_offset - chunk_begin
# tell() is not helpful for the need_more_data case, but we know it is the remainder
# of the data in that case. in the other case, tell() works as expected.
length = (len(data) - start) if need_more_data else (unpacker.tell() - stream_offset)
msgpacked_bytes += data[start:start+length]
stream_offset += length
if need_more_data:
# Need more data, feed the next chunk
break