Skip to content

Commit

Permalink
fix: write partition data only
Browse files Browse the repository at this point in the history
Fixes an issue where the partition data would overwrite the entire disk.
  • Loading branch information
swysocki committed Sep 7, 2023
1 parent 1bb9ef2 commit e82041e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gpt_image/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def write_data(self, disk: Disk, data: bytes) -> int:
"""
if len(data) > self.size:
raise ValueError(f"data too large for partition: {len(data)} {self.size}")
with open(str(disk.image_path), "wb") as b:
with open(str(disk.image_path), "r+b") as b:
start = self.first_lba * disk.sector_size
b.seek(start)
b.write(data)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def test_write_data(new_image):
byte_count = len(BYTE_DATA)
b.seek(start)
test_buffer = b.read(byte_count)
assert test_buffer == BYTE_DATA
assert bytes(test_buffer) == BYTE_DATA


def test_read_partition(new_image):
disk = Disk.open(new_image)
part = disk.table.partitions.find("partition1")
part.write_data(disk, BYTE_DATA)
read_data = part.read(disk)
assert read_data == BYTE_DATA
assert read_data[:len(BYTE_DATA)] == BYTE_DATA

0 comments on commit e82041e

Please sign in to comment.