diff --git a/dissect/hypervisor/disk/qcow2.py b/dissect/hypervisor/disk/qcow2.py index e869f71..70cd647 100644 --- a/dissect/hypervisor/disk/qcow2.py +++ b/dissect/hypervisor/disk/qcow2.py @@ -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: @@ -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( diff --git a/tests/disk/test_qcow2.py b/tests/disk/test_qcow2.py index e7d6e42..6734468 100644 --- a/tests/disk/test_qcow2.py +++ b/tests/disk/test_qcow2.py @@ -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 '(?:[A-Z]:\\+)?[/\\]+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 @@ -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 '(?:[A-Z]:\\+)?[/\\]+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)