Skip to content

Commit

Permalink
use unpacker.tell() instead of deprecated write_bytes, fixes borgback…
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Jun 12, 2018
1 parent 37f2c89 commit 2eac60b
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/borg/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,14 @@ 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''

write_offset = self.write_offset
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):
Expand All @@ -149,16 +142,24 @@ 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()
except msgpack.OutOfData:
need_more_data = True
else:
need_more_data = False

start = stream_offset - chunk_begin
length = unpacker.tell() - stream_offset
stream_offset = stream_offset + length # == tell()
msgpacked_bytes += data[start:start+length]

if need_more_data:
# Need more data, feed the next chunk
break

Expand Down

0 comments on commit 2eac60b

Please sign in to comment.