Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions dissect/hypervisor/descriptor/c_hyperv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dissect.cstruct import cstruct

hyperv_def = """
Expand Down
28 changes: 13 additions & 15 deletions dissect/hypervisor/descriptor/hyperv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from __future__ import annotations

import struct
from collections.abc import ItemsView, KeysView, ValuesView
from typing import BinaryIO, Optional, Union
from typing import TYPE_CHECKING, BinaryIO

from dissect.util.stream import RangeStream

Expand All @@ -16,6 +15,9 @@
)
from dissect.hypervisor.exceptions import InvalidSignature

if TYPE_CHECKING:
from collections.abc import ItemsView, KeysView, ValuesView

Check warning on line 19 in dissect/hypervisor/descriptor/hyperv.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/hyperv.py#L19

Added line #L19 was not covered by tests


class HyperVFile:
"""HyperVFile implementation.
Expand Down Expand Up @@ -278,7 +280,7 @@
return f"<HyperVStorageKeyTableEntry type={self.type} size={self.size}>"

@property
def parent(self) -> Optional[HyperVStorageKeyTableEntry]:
def parent(self) -> HyperVStorageKeyTableEntry | None:
"""Return the entry parent, if there is any.

Requires that all key tables are loaded.
Expand Down Expand Up @@ -333,8 +335,8 @@
file_object = self.get_file_object()
# This memoryview has no purpose, only do it so the return value type is consistent
return memoryview(file_object.read(size))
else:
return self.raw[self.header.data_offset :]

return self.raw[self.header.data_offset :]

@property
def key(self) -> str:
Expand All @@ -343,7 +345,7 @@
return self.raw.tobytes()[: self.header.data_offset - 1].decode("utf-8")

@property
def value(self) -> Union[int, bytes, str]:
def value(self) -> int | bytes | str:
"""Return a Python native value for this entry."""
data = self.data

Expand All @@ -369,6 +371,8 @@
if self.type == KeyDataType.Bool:
return struct.unpack("<I", data[:4])[0] != 0

raise TypeError(f"Unknown data type: {self.type}")

Check warning on line 374 in dissect/hypervisor/descriptor/hyperv.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/hyperv.py#L374

Added line #L374 was not covered by tests

@property
def data_size(self) -> int:
"""Return the total amount of data bytes, including the key name.
Expand Down Expand Up @@ -427,10 +431,7 @@

obj = {}
for key, child in self.children.items():
if child.type == KeyDataType.Node:
value = child.as_dict()
else:
value = child.value
value = child.as_dict() if child.type == KeyDataType.Node else child.value

obj[key] = value

Expand Down Expand Up @@ -466,13 +467,10 @@
if n is not None and n < -1:
raise ValueError("invalid number of bytes to read")

if n == -1:
read_length = self.size
else:
read_length = min(n, self.size)
read_length = self.size if n == -1 else min(n, self.size)

Check warning on line 470 in dissect/hypervisor/descriptor/hyperv.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/hyperv.py#L470

Added line #L470 was not covered by tests

self.file.fh.seek(self.offset)
return self.file.fh.read(read_length)

def open(self, size: Optional[int] = None) -> BinaryIO:
def open(self, size: int | None = None) -> BinaryIO:
return RangeStream(self.file.fh, self.offset, size or self.size)
14 changes: 9 additions & 5 deletions dissect/hypervisor/descriptor/ovf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from typing import Iterator, TextIO
from xml.etree.ElementTree import Element
from __future__ import annotations

from typing import TYPE_CHECKING, Final, TextIO

from defusedxml import ElementTree

if TYPE_CHECKING:
from collections.abc import Iterator
from xml.etree.ElementTree import Element

Check warning on line 9 in dissect/hypervisor/descriptor/ovf.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/ovf.py#L8-L9

Added lines #L8 - L9 were not covered by tests


class OVF:
NS = {
NS: Final[dict[str, str]] = {
"ovf": "http://schemas.dmtf.org/ovf/envelope/1",
"rasd": "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
}
Expand Down Expand Up @@ -34,8 +39,7 @@
for disk in self.xml.findall(self.DISK_DRIVE_XPATH, self.NS):
resource = disk.find("{{{rasd}}}HostResource".format(**self.NS))
xpath = resource.text
if xpath.startswith("ovf:"):
xpath = xpath[4:]
xpath = xpath.removeprefix("ovf:")

if xpath.startswith("/disk/"):
disk_ref = xpath.split("/")[-1]
Expand Down
11 changes: 8 additions & 3 deletions dissect/hypervisor/descriptor/pvs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from typing import IO, Iterator
from xml.etree.ElementTree import Element
from __future__ import annotations

from typing import TYPE_CHECKING, TextIO

from defusedxml import ElementTree

if TYPE_CHECKING:
from collections.abc import Iterator
from xml.etree.ElementTree import Element

Check warning on line 9 in dissect/hypervisor/descriptor/pvs.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/pvs.py#L8-L9

Added lines #L8 - L9 were not covered by tests


class PVS:
"""Parallels VM settings file.
Expand All @@ -11,7 +16,7 @@
fh: The file-like object to a PVS file.
"""

def __init__(self, fh: IO):
def __init__(self, fh: TextIO):
self._xml: Element = ElementTree.fromstring(fh.read())

def disks(self) -> Iterator[str]:
Expand Down
11 changes: 8 additions & 3 deletions dissect/hypervisor/descriptor/vbox.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from typing import IO, Iterator
from xml.etree.ElementTree import Element
from __future__ import annotations

from typing import TYPE_CHECKING, TextIO

from defusedxml import ElementTree

if TYPE_CHECKING:
from collections.abc import Iterator
from xml.etree.ElementTree import Element

Check warning on line 9 in dissect/hypervisor/descriptor/vbox.py

View check run for this annotation

Codecov / codecov/patch

dissect/hypervisor/descriptor/vbox.py#L8-L9

Added lines #L8 - L9 were not covered by tests


class VBox:
VBOX_XML_NAMESPACE = "{http://www.virtualbox.org/}"

def __init__(self, fh: IO):
def __init__(self, fh: TextIO):
self._xml: Element = ElementTree.fromstring(fh.read())

def disks(self) -> Iterator[str]:
Expand Down
Loading
Loading