Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions dissect/hypervisor/disk/qcow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _open_data_file(self, data_file: BinaryIO | None, allow_no_data_file: bool =
return data_file

if self.path:
if (data_file_path := self.path.with_name(self.image_data_file)).exists():
if (data_file_path := self.path.parent.joinpath(self.image_data_file)).exists():
return data_file_path.open("rb")

if not allow_no_data_file:
Expand All @@ -188,7 +188,7 @@ def _open_backing_file(self, backing_file: BinaryIO | None, allow_no_backing_fil
backing_file_path = None
if backing_file is None:
if self.path:
if (backing_file_path := self.path.with_name(self.auto_backing_file)).exists():
if (backing_file_path := self.path.parent.joinpath(self.auto_backing_file)).exists():
backing_file = backing_file_path.open("rb")
elif not allow_no_backing_file:
raise Error(
Expand Down
18 changes: 18 additions & 0 deletions tests/disk/test_qcow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def test_data_file(data_file_qcow2: Path) -> None:
for i in range(255):
assert stream.read(1024 * 1024).strip(bytes([i])) == b"", f"Mismatch at offset {i * 1024 * 1024:#x}"

# Test with absolute path
with patch.object(Path, "open", lambda *args: None), patch.object(Path, "exists", return_value=False):
qcow2.image_data_file = "/absolute/path/to/nothing.qcow2"
with pytest.raises(
Error,
match=r"data-file '/absolute/path/to/nothing.qcow2' not found \(image_data_file = '/absolute/path/to/nothing.qcow2'\)", # noqa: E501
):
qcow2._open_data_file(None)


def test_backing_file(backing_chain_qcow2: tuple[Path, Path, Path]) -> None:
file1, file2, file3 = backing_chain_qcow2
Expand Down Expand Up @@ -123,6 +132,15 @@ def test_backing_file(backing_chain_qcow2: tuple[Path, Path, Path]) -> None:
assert stream.read(1024 * 1024).strip(b"\x00") == b"Something here four"
assert stream.read(1024 * 1024).strip(b"\x00") == b"Something here five"

# Test with absolute path
with patch.object(Path, "open", lambda *args: None), patch.object(Path, "exists", return_value=False):
qcow2.auto_backing_file = "/absolute/path/to/nothing.qcow2"
with pytest.raises(
Error,
match=r"backing-file '/absolute/path/to/nothing.qcow2' not found \(auto_backing_file = '/absolute/path/to/nothing.qcow2'\)", # noqa: E501
):
qcow2._open_backing_file(None)


def test_snapshot(snapshot_qcow2: BinaryIO) -> None:
qcow2 = QCow2(snapshot_qcow2)
Expand Down
Loading