Skip to content

Commit 8007d52

Browse files
Fix qcow2 support for absolute and relative backing_file paths (#65)
Signed-off-by: Andreia Ocanoaia <andreia.ocanoaia@gmail.com> Co-authored-by: Schamper <1254028+Schamper@users.noreply.github.com>
1 parent ae08df8 commit 8007d52

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

dissect/hypervisor/disk/qcow2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _open_data_file(self, data_file: BinaryIO | None, allow_no_data_file: bool =
176176
return data_file
177177

178178
if self.path:
179-
if (data_file_path := self.path.with_name(self.image_data_file)).exists():
179+
if (data_file_path := self.path.parent.joinpath(self.image_data_file)).exists():
180180
return data_file_path.open("rb")
181181

182182
if not allow_no_data_file:
@@ -190,7 +190,7 @@ def _open_backing_file(self, backing_file: BinaryIO | None, allow_no_backing_fil
190190
backing_file_path = None
191191
if backing_file is None:
192192
if self.path:
193-
if (backing_file_path := self.path.with_name(self.auto_backing_file)).exists():
193+
if (backing_file_path := self.path.parent.joinpath(self.auto_backing_file)).exists():
194194
backing_file = backing_file_path.open("rb")
195195
elif not allow_no_backing_file:
196196
raise Error(

tests/disk/test_qcow2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def test_data_file() -> None:
7272
for i in range(255):
7373
assert stream.read(1024 * 1024).strip(bytes([i])) == b"", f"Mismatch at offset {i * 1024 * 1024:#x}"
7474

75+
# Test with absolute path
76+
with patch.object(Path, "open", lambda *args: None), patch.object(Path, "exists", return_value=False):
77+
qcow2.image_data_file = "/absolute/path/to/nothing.qcow2"
78+
with pytest.raises(
79+
Error,
80+
match=r"data-file '(?:[A-Z]:\\+)?[/\\]+absolute[/\\]+path[/\\]+to[/\\]+nothing\.qcow2' not found \(image_data_file = '/absolute/path/to/nothing\.qcow2'\)", # noqa: E501
81+
):
82+
qcow2._open_data_file(None)
83+
7584

7685
def test_backing_file() -> None:
7786
file1 = absolute_path("_data/disk/qcow2/backing-chain-1.qcow2.gz")
@@ -136,6 +145,15 @@ def test_backing_file() -> None:
136145
assert stream.read(1024 * 1024).strip(b"\x00") == b"Something here four"
137146
assert stream.read(1024 * 1024).strip(b"\x00") == b"Something here five"
138147

148+
# Test with absolute path
149+
with patch.object(Path, "open", lambda *args: None), patch.object(Path, "exists", return_value=False):
150+
qcow2.auto_backing_file = "/absolute/path/to/nothing.qcow2"
151+
with pytest.raises(
152+
Error,
153+
match=r"backing-file '(?:[A-Z]:\\+)?[/\\]+absolute[/\\]+path[/\\]+to[/\\]+nothing\.qcow2' not found \(auto_backing_file = '/absolute/path/to/nothing\.qcow2'\)", # noqa: E501
154+
):
155+
qcow2._open_backing_file(None)
156+
139157

140158
def test_snapshot() -> None:
141159
with gzip.open(absolute_path("_data/disk/qcow2/snapshot.qcow2.gz"), "rb") as fh:

0 commit comments

Comments
 (0)