mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-26 09:47:58 +00:00
LoggedIO: deduplicated code, improved checks and error handling in read()
Code shared by read() and iter_objects() was moved into _read(). Compared to read()'s previous state, this improved: - fixed size check to avoid read with negative size - exception handler for struct unpack - checking for short read - more precise exception messages
This commit is contained in:
parent
4474db31d4
commit
6f637bed2f
1 changed files with 33 additions and 27 deletions
|
@ -534,26 +534,9 @@ def iter_objects(self, segment, include_data=False):
|
|||
offset = MAGIC_LEN
|
||||
header = fd.read(self.header_fmt.size)
|
||||
while header:
|
||||
try:
|
||||
crc, size, tag = self.header_fmt.unpack(header)
|
||||
except struct.error as err:
|
||||
raise IntegrityError('Invalid segment entry header [offset {}]: {}'.format(offset, err))
|
||||
if size > MAX_OBJECT_SIZE or size < self.header_fmt.size:
|
||||
raise IntegrityError('Invalid segment entry size [offset {}]'.format(offset))
|
||||
length = size - self.header_fmt.size
|
||||
rest = fd.read(length)
|
||||
if len(rest) != length:
|
||||
raise IntegrityError('Segment entry data short read [offset {}]: expected: {}, got {} bytes'.format(
|
||||
offset, length, len(rest)))
|
||||
if crc32(rest, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
|
||||
raise IntegrityError('Segment entry checksum mismatch [offset {}]'.format(offset))
|
||||
if tag not in (TAG_PUT, TAG_DELETE, TAG_COMMIT):
|
||||
raise IntegrityError('Invalid segment entry tag [offset {}]'.format(offset))
|
||||
key = None
|
||||
if tag in (TAG_PUT, TAG_DELETE):
|
||||
key = rest[:32]
|
||||
size, tag, key, data = self._read(fd, self.header_fmt, header, offset, (TAG_PUT, TAG_DELETE, TAG_COMMIT))
|
||||
if include_data:
|
||||
yield tag, key, offset, rest[32:]
|
||||
yield tag, key, offset, data
|
||||
else:
|
||||
yield tag, key, offset
|
||||
offset += size
|
||||
|
@ -586,16 +569,39 @@ def read(self, segment, offset, id):
|
|||
fd = self.get_fd(segment)
|
||||
fd.seek(offset)
|
||||
header = fd.read(self.put_header_fmt.size)
|
||||
crc, size, tag, key = self.put_header_fmt.unpack(header)
|
||||
if size > MAX_OBJECT_SIZE:
|
||||
raise IntegrityError('Invalid segment object size')
|
||||
data = fd.read(size - self.put_header_fmt.size)
|
||||
if crc32(data, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
|
||||
raise IntegrityError('Segment checksum mismatch')
|
||||
if tag != TAG_PUT or id != key:
|
||||
raise IntegrityError('Invalid segment entry header')
|
||||
size, tag, key, data = self._read(fd, self.put_header_fmt, header, offset, (TAG_PUT, ))
|
||||
if id != key:
|
||||
raise IntegrityError('Invalid segment entry header, is not for wanted id [offset {}]'.format(offset))
|
||||
return data
|
||||
|
||||
def _read(self, fd, fmt, header, offset, acceptable_tags):
|
||||
# some code shared by read() and iter_objects()
|
||||
try:
|
||||
hdr_tuple = fmt.unpack(header)
|
||||
except struct.error as err:
|
||||
raise IntegrityError('Invalid segment entry header [offset {}]: {}'.format(offset, err))
|
||||
if fmt is self.put_header_fmt:
|
||||
crc, size, tag, key = hdr_tuple
|
||||
elif fmt is self.header_fmt:
|
||||
crc, size, tag = hdr_tuple
|
||||
key = None
|
||||
else:
|
||||
raise TypeError("_read called with unsupported format")
|
||||
if size > MAX_OBJECT_SIZE or size < fmt.size:
|
||||
raise IntegrityError('Invalid segment entry size [offset {}]'.format(offset))
|
||||
length = size - fmt.size
|
||||
data = fd.read(length)
|
||||
if len(data) != length:
|
||||
raise IntegrityError('Segment entry data short read [offset {}]: expected: {}, got {} bytes'.format(
|
||||
offset, length, len(data)))
|
||||
if crc32(data, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
|
||||
raise IntegrityError('Segment entry checksum mismatch [offset {}]'.format(offset))
|
||||
if tag not in acceptable_tags:
|
||||
raise IntegrityError('Invalid segment entry header, did not get acceptable tag [offset {}]'.format(offset))
|
||||
if key is None and tag in (TAG_PUT, TAG_DELETE):
|
||||
key, data = data[:32], data[32:]
|
||||
return size, tag, key, data
|
||||
|
||||
def write_put(self, id, data):
|
||||
size = len(data) + self.put_header_fmt.size
|
||||
fd = self.get_write_fd()
|
||||
|
|
Loading…
Reference in a new issue