Skip to content

Commit 817dcf9

Browse files
object1337Schamper
andauthored
Fix VMDK when parentFileNameHint is an absolute path (#76)
Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com>
1 parent 6f2d5ab commit 817dcf9

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

dissect/hypervisor/disk/vmdk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,10 @@ def open_parent(path: Path, filename_hint: str) -> VMDK:
535535
hint_path, _, filename = filename_hint.rpartition("/")
536536
filepath = path.joinpath(filename)
537537
if not filepath.exists():
538-
_, _, hint_path_name = hint_path.rpartition("/")
539-
filepath = path.parent.joinpath(hint_path_name).joinpath(filename)
538+
filepath = path.joinpath(filename_hint)
539+
if not filepath.exists():
540+
_, _, hint_path_name = hint_path.rpartition("/")
541+
filepath = path.parent.joinpath(hint_path_name).joinpath(filename)
540542
vmdk = VMDK(filepath)
541543
except Exception as err:
542544
raise IOError(f"Failed to open parent disk with hint {filename_hint} from path {path}: {err}")

tests/disk/test_vmdk.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

33
import gzip
4+
from pathlib import Path
5+
from unittest.mock import MagicMock
46

57
import pytest
68

79
from dissect.hypervisor.disk.c_vmdk import c_vmdk
8-
from dissect.hypervisor.disk.vmdk import VMDK, DiskDescriptor, ExtentDescriptor
10+
from dissect.hypervisor.disk.vmdk import VMDK, DiskDescriptor, ExtentDescriptor, open_parent
911
from tests._util import absolute_path
1012

1113

@@ -203,3 +205,40 @@ def test_vmdk_extent_description(extent_description: str, expected_extents: list
203205

204206
descriptor = DiskDescriptor.parse(extent_description)
205207
assert descriptor.extents == expected_extents
208+
209+
210+
def test_open_parent_all_cases(monkeypatch: pytest.MonkeyPatch) -> None:
211+
"""Test open_parent handles absolute and relative filename_hint paths."""
212+
213+
# Mock Path.exists to simulate file existence
214+
def mock_exists(path: Path) -> bool:
215+
return str(path) in {
216+
"/a/b/c/d.vmdk", # Case: absolute path
217+
"base/relative/hint.vmdk", # Case: full relative path
218+
"base/hint.vmdk", # Case: basename in same dir
219+
"../sibling/hint.vmdk", # Case: fallback to sibling
220+
}
221+
222+
with monkeypatch.context() as m:
223+
m.setattr("pathlib.Path.exists", mock_exists)
224+
225+
# Mock VMDK to avoid real file I/O
226+
mock_vmdk = MagicMock()
227+
m.setattr("dissect.hypervisor.disk.vmdk.VMDK", lambda path: mock_vmdk)
228+
mock_vmdk.path = "mocked-path"
229+
230+
# Case: Absolute path — should use /a/b/c/d.vmdk directly
231+
vmdk = open_parent(Path("base"), "/a/b/c/d.vmdk")
232+
assert str(vmdk.path) == "mocked-path"
233+
234+
# Case: Full relative path — try base/relative/hint.vmdk
235+
vmdk = open_parent(Path("base"), "relative/hint.vmdk")
236+
assert str(vmdk.path) == "mocked-path"
237+
238+
# Case: Basename only — fall back to base/hint.vmdk
239+
vmdk = open_parent(Path("base"), "hint.vmdk")
240+
assert str(vmdk.path) == "mocked-path"
241+
242+
# Case: Fallback to sibling — try ../sibling/hint.vmdk
243+
vmdk = open_parent(Path("base"), "sibling/hint.vmdk")
244+
assert str(vmdk.path) == "mocked-path"

0 commit comments

Comments
 (0)