Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bmap): reset internal state before writing #331

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions mtda/storage/helpers/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self, mtda):
self.isloop = False
self.bmapDict = None
self.crtBlockRange = 0
self.writtenBlocks = 0
self.writtenBytes = 0
self.overlap = 0
self.rangeChkSum = None
Expand Down Expand Up @@ -291,6 +290,7 @@ def _get_hasher_by_name(self):
def setBmap(self, bmapDict):
self.bmapDict = bmapDict
self.crtBlockRange = 0
self.writtenBytes = 0
self.overlap = 0
self.rangeChkSum = self._get_hasher_by_name()

Expand Down Expand Up @@ -373,28 +373,28 @@ def _write_with_bmap(self, data):
# remaining bytes in data buffer
remaining = len(data) - offset
while remaining:
if self.writtenBlocks > cur_range["last"]:
writtenBlocks = self.writtenBytes // blksize
if writtenBlocks > cur_range["last"]:
self._validate_and_reset_range()
self.crtBlockRange += 1
cur_range = self.bmapDict["BlockMap"][self.crtBlockRange]

if self.writtenBlocks >= cur_range["first"]:
if writtenBlocks >= cur_range["first"]:
# bmap ranges are inclusive. Exclusive ranges like [from, to)
# are easier to handle, hence add one block
end = min(remaining,
(cur_range["last"] - self.writtenBlocks + 1)
(cur_range["last"] - writtenBlocks + 1)
* blksize - self.overlap)
nbytes = self._write_with_chksum(data[offset:offset + end])
else:
# the range already got incremented, hence we need to iterate
# until the begin of the current range
nbytes = min(remaining,
(cur_range["first"] - self.writtenBlocks)
(cur_range["first"] - writtenBlocks)
* blksize - self.overlap)
self.handle.seek(nbytes, io.SEEK_CUR)
self.overlap -= min(self.overlap, nbytes)
self.writtenBytes += nbytes
self.writtenBlocks = self.writtenBytes // blksize
offset += nbytes
remaining -= nbytes

Expand Down
Loading