(and fix an off-by-one error in MutableDiskShare._write_share_data).
Signed-off-by: Daira Hopwood <david-sarah@jacaranda.org>
seekpos = self.DATA_OFFSET + offset
precondition(seekpos >= self._total_size, offset=offset, seekpos=seekpos, total_size=self._total_size)
if offset + len(data) > self._allocated_data_length:
- raise DataTooLargeError(self._allocated_data_length, offset, len(data))
+ raise DataTooLargeError(self._shnum, self._allocated_data_length, offset, len(data))
self._set_size(self._total_size + len(data))
return self._store_or_buffer( (seekpos, data, 0) )
length = len(data)
precondition(offset >= 0, offset=offset)
if offset + length > self.MAX_SIZE:
- raise DataTooLargeError()
+ raise DataTooLargeError(self._shnum, self.MAX_SIZE, offset, length)
if new_length is not None and new_length < offset + length:
length = max(0, new_length - offset)
length = len(data)
precondition(offset >= 0, offset)
if self._allocated_data_length is not None and offset+length > self._allocated_data_length:
- raise DataTooLargeError(self._allocated_data_length, offset, length)
+ raise DataTooLargeError(self._shnum, self._allocated_data_length, offset, length)
+
f = open(self._home, 'rb+')
try:
real_offset = self.DATA_OFFSET + offset
def _write_share_data(self, f, offset, data):
length = len(data)
precondition(offset >= 0, offset=offset)
- precondition(offset + length < self.MAX_SIZE, offset=offset, length=length)
+ if offset + length > self.MAX_SIZE:
+ raise DataTooLargeError(self._shnum, self.MAX_SIZE, offset, length)
data_length = self._read_data_length(f)
if offset+length >= data_length:
- # They are expanding their data size.
-
- if offset+length > self.MAX_SIZE:
- raise DataTooLargeError()
-
- # Their data now fits in the current container. We must write
+ # They are expanding their data size. We must write
# their new data and modify the recorded data size.
# Fill any newly exposed empty space with 0's.
for (offset, data) in datav:
precondition(offset >= 0, offset=offset)
if offset + len(data) > self.MAX_SIZE:
- raise DataTooLargeError()
+ raise DataTooLargeError(self._shnum, self.MAX_SIZE, offset, len(data))
f = open(self._home, 'rb+')
try:
class DataTooLargeError(Exception):
- pass
+ def __init__(self, shnum, allocated_data_length, offset, length):
+ self.shnum = shnum
+ self.allocated_data_length = allocated_data_length
+ self.offset = offset
+ self.length = length
+
+ def __str__(self):
+ return ("attempted write to shnum %d of %d bytes at offset %d exceeds allocated data length of %d bytes"
+ % (self.__class__.__name__, self.shnum, self.length, self.offset, self.allocated_data_length))
class CorruptStoredShareError(Exception):