1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-25 17:27:31 +00:00

recover_segment: use mmap(), fixes #2982

This commit is contained in:
Thomas Waldmann 2017-09-01 05:26:27 +02:00
parent 4a22dec340
commit 9fc4d00bf6

View file

@ -1,4 +1,5 @@
import errno
import mmap
import os
import shutil
import struct
@ -1305,13 +1306,15 @@ def iter_objects(self, segment, offset=0, include_data=False, read_data=True):
header = fd.read(self.header_fmt.size)
def recover_segment(self, segment, filename):
logger.info('attempting to recover ' + filename)
if segment in self.fds:
del self.fds[segment]
with open(filename, 'rb') as fd:
# XXX: Rather use mmap, this loads the entire segment (up to 500 MB by default) into memory.
data = memoryview(fd.read())
os.rename(filename, filename + '.beforerecover')
logger.info('attempting to recover ' + filename)
backup_filename = filename + '.beforerecover'
os.rename(filename, backup_filename)
with open(backup_filename, 'rb') as backup_fd:
# note: file must not be 0 size (windows can't create 0 size mapping)
with mmap.mmap(backup_fd.fileno(), 0, access=mmap.ACCESS_READ) as mm:
data = memoryview(mm)
with open(filename, 'wb') as fd:
fd.write(MAGIC)
while len(data) >= self.header_fmt.size:
@ -1324,6 +1327,7 @@ def recover_segment(self, segment, filename):
continue
fd.write(data[:size])
data = data[size:]
data.release()
def read(self, segment, offset, id, read_data=True):
"""