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

Improve dm-thin behavior of empty blocks #32

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
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
48 changes: 3 additions & 45 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
tests/data/dm/dm-thin-data.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/dm/dm-thin-metadata.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-linear-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-linear-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid0-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid0-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid0-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid1-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid1-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid4-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid4-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid4-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid5-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid5-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid5-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid6-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid6-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid6-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid6-4.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid10-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-raid10-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-90-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-90-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-90-raid1-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/md/md-90-raid1-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid0-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid0-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid0-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid0-raid1-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid0-raid1-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid1-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid1-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid4-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid4-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid5-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid5-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid5-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid6-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid6-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid6-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid6-4.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid10-1.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid10-2.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid10-3.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/ddf-raid10-4.bin.gz filter=lfs diff=lfs merge=lfs -text
tests/data/dm/* filter=lfs diff=lfs merge=lfs -text
tests/data/md/* filter=lfs diff=lfs merge=lfs -text
tests/data/ddf/* filter=lfs diff=lfs merge=lfs -text
tests/data/lvm/* filter=lfs diff=lfs merge=lfs -text
4 changes: 4 additions & 0 deletions dissect/volume/dm/thin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def _read(self, offset: int, length: int) -> bytes:
while length > 0:
block_info = data_mapping.lookup([self.device_id, block])
if block_info is None:
remaining = length
if self.size is None or (remaining := self.size - offset) > 0:
return b"\x00" * min(length, remaining)

break

block_time = int.from_bytes(block_info, "little")
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def dm_thin() -> Iterator[list[BinaryIO]]:
yield from open_files_gz(["data/dm/dm-thin-metadata.bin.gz", "data/dm/dm-thin-data.bin.gz"])


@pytest.fixture
def dm_thin_empty() -> Iterator[list[BinaryIO]]:
yield from open_files_gz(["data/dm/dm-thin-empty-metadata.bin.gz", "data/dm/dm-thin-empty-data.bin.gz"])


@pytest.fixture
def md_linear() -> Iterator[list[BinaryIO]]:
yield from open_files_gz(["data/md/md-linear-1.bin.gz", "data/md/md-linear-2.bin.gz"])
Expand Down
3 changes: 3 additions & 0 deletions tests/data/dm/dm-thin-empty-data.bin.gz
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/data/dm/dm-thin-empty-metadata.bin.gz
Git LFS file not shown
22 changes: 21 additions & 1 deletion tests/test_dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,24 @@ def test_dm_thin(dm_thin: list[BinaryIO]) -> None:

thin_no_size = pool.open(0)
thin_no_size.seek((1024 * 1024 * 2) - 512)
assert len(thin_no_size.read(1024)) == 512
assert len(thin_no_size.read(1024)) == 1024


def test_dm_thin_empty(dm_thin_empty: list[BinaryIO]) -> None:
metadata_fh, data_fh = dm_thin_empty
pool = ThinPool(metadata_fh, data_fh)

dev = pool.open(0)

assert dev.read(512) == b"\x00" * 512

dev.seek(512 * 1024)
assert dev.read(512) != b"\x00" * 512

# Far beyond the file boundary
dev.seek(2**64)
assert dev.read(512) == b"\x00" * 512

dev = pool.open(0, 512)
assert dev.read(512) == b"\x00" * 512
assert dev.read(512) == b""