Created
November 8, 2013 14:41
-
-
Save molnarg/7371947 to your computer and use it in GitHub Desktop.
Monkey patching the python cfb library to support overwrite of data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cfb.directory.entry import Entry | |
from cfb.constants import ENDOFCHAIN | |
# Monkey patching Entry | |
def overwrite(self, data): | |
""" | |
Writes `data` to the current directory entry at the current position. If | |
`data` is bigger than the available space in the entry, then the rest of | |
`data` is ignored. It returns the number of bytes written. | |
""" | |
self.source.seek(self._source_position) | |
cursor = 0 | |
size = min(len(data), self.size - self.tell()) | |
while cursor < size: | |
if self.tell() > self.size: | |
break | |
if self._sector_number == ENDOFCHAIN: | |
break | |
to_write = size - cursor | |
to_end = self.sector_size - self._position_in_sector | |
to_do = min(to_write, to_end) | |
self.stream.write(data[cursor:cursor+to_do]) | |
cursor += to_do | |
self._position += to_do | |
self._source_position = self.source.tell() | |
if to_write >= to_end: | |
self._position_in_sector = 0 | |
self._sector_number = self.next_sector(self._sector_number) | |
position = (self._sector_number + int(not self._is_mini)) \ | |
<< self.sector_shift | |
self.stream.seek(position) | |
else: | |
self._position_in_sector += to_do | |
return cursor | |
Entry.write = overwrite |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment