Skip to content

Commit 809f272

Browse files
committed
Small improvement
1 parent 5968571 commit 809f272

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

dissect/hypervisor/disk/vdi.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,32 @@
1818
class VDI:
1919
"""VirtualBox Virtual Disk Image (VDI) implementation.
2020
21+
If provided with file-like objects, the caller is responsible for closing them.
22+
When provided with paths, the VDI class will manage the file handles.
23+
2124
Args:
2225
fh: File-like object or path of the VDI file.
23-
parent: Optional file-like object for the parent disk (for differencing disks).
26+
parent: Optional file-like object or path for the parent disk (for differencing disks).
2427
"""
2528

26-
def __init__(self, fh: BinaryIO | Path, parent: BinaryIO | None = None):
29+
def __init__(self, fh: BinaryIO | Path, parent: BinaryIO | Path | None = None):
2730
if isinstance(fh, Path):
2831
self.path = fh
2932
self.fh = self.path.open("rb")
3033
else:
3134
self.path = None
3235
self.fh = fh
3336

34-
self.parent = parent
37+
if isinstance(parent, Path):
38+
self.parent_path = parent
39+
self.parent = self.parent_path.open("rb")
40+
else:
41+
self.parent_path = None
42+
self.parent = parent
3543

3644
self.fh.seek(0)
3745
self.preheader = c_vdi.VDIPREHEADER(self.fh)
3846
if self.preheader.u32Signature != VDI_IMAGE_SIGNATURE:
39-
print(self.preheader)
4047
raise Error(
4148
f"Invalid VDI signature, expected {VDI_IMAGE_SIGNATURE:#08X}, got {self.preheader.u32Signature:#08X}"
4249
)
@@ -95,6 +102,9 @@ def close(self) -> None:
95102
if self.path is not None:
96103
self.fh.close()
97104

105+
if self.parent_path is not None and self.parent is not None:
106+
self.parent.close()
107+
98108

99109
class VDIStream(AlignedStream):
100110
def __init__(self, vdi: VDI):

tests/disk/test_vdi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from dissect.hypervisor.disk.c_vdi import c_vdi
99
from dissect.hypervisor.disk.vdi import VDI
10-
from tests.conftest import absolute_path
10+
from tests._util import absolute_path
1111

1212

1313
def mock_open_gz(self: Path, *args, **kwargs) -> BinaryIO:

0 commit comments

Comments
 (0)