Skip to content

Commit

Permalink
CStruct4 compatibility (DIS-2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecinestpasunepipe committed May 24, 2024
1 parent ee2928a commit eda13e1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dissect/ntfs/c_ntfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@
INDEX_ENTRY_END = 0x02


def segment_reference(reference: cstruct.Instance) -> int:
def segment_reference(reference: cstruct.Structure) -> int:
"""Helper to calculate the complete segment number from a cstruct MFT segment reference.
Args:
Expand Down
10 changes: 5 additions & 5 deletions dissect/ntfs/mft.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from operator import itemgetter
from typing import TYPE_CHECKING, Any, BinaryIO, Iterator, Optional, Union

from dissect.cstruct import Instance
from dissect.cstruct import Structure

from dissect.ntfs.attr import Attribute, AttributeHeader
from dissect.ntfs.c_ntfs import (
Expand Down Expand Up @@ -91,11 +91,11 @@ def _get_path(self, path: str, root: Optional[MftRecord] = None) -> MftRecord:

return node

def get(self, ref: Union[int, str, Instance], root: Optional[MftRecord] = None) -> MftRecord:
def get(self, ref: Union[int, str, Structure], root: Optional[MftRecord] = None) -> MftRecord:
"""Retrieve an MFT record using a variety of methods.
Supported references are:
- ``_MFT_SEGMENT_REFERENCE`` cstruct instance
- ``_MFT_SEGMENT_REFERENCE`` cstruct structure
- integer segment number
- string file path
Expand All @@ -106,7 +106,7 @@ def get(self, ref: Union[int, str, Instance], root: Optional[MftRecord] = None)
Raises:
TypeError: If the reference is of an unsupported type.
"""
if isinstance(ref, Instance) and ref._type == c_ntfs._MFT_SEGMENT_REFERENCE:
if isinstance(ref, Structure) and ref._type == c_ntfs._MFT_SEGMENT_REFERENCE:
ref = segment_reference(ref)

if isinstance(ref, int):
Expand Down Expand Up @@ -153,7 +153,7 @@ def __init__(self):
self.segment: Optional[int] = None
self.offset: Optional[int] = None
self.data: Optional[bytes] = None
self.header: Optional[Instance] = None
self.header: Optional[Structure] = None

def __repr__(self) -> str:
return f"<MftRecord {self.segment}#{self.header.SequenceNumber}>"
Expand Down
4 changes: 2 additions & 2 deletions dissect/ntfs/secure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import BinaryIO, Iterator
from uuid import UUID

from dissect.cstruct import Instance
from dissect.cstruct import Structure
from dissect.util.sid import read_sid

from dissect.ntfs.c_ntfs import ACE_OBJECT_FLAGS, ACE_TYPE, c_ntfs
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(self, record: MftRecord = None, sds: BinaryIO = None):
if not hasattr(self.sds, "size"):
self.sds.size = self.sds.seek(0, io.SEEK_END)

def _iter_entries(self, offset: int = 0) -> Iterator[Instance]:
def _iter_entries(self, offset: int = 0) -> Iterator[Structure]:
"""Iterate over all SDS entries, optionally starting from a specific offset.
Args:
Expand Down
8 changes: 4 additions & 4 deletions dissect/ntfs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import UserDict
from typing import TYPE_CHECKING, Any, BinaryIO, Optional, Union

from dissect.cstruct import EnumInstance, Instance
from dissect.cstruct import Enum, Structure
from dissect.util.stream import RunlistStream

from dissect.ntfs.c_ntfs import (
Expand Down Expand Up @@ -44,12 +44,12 @@ def __getattr__(self, attr: str) -> AttributeCollection:
return super().__getattribute__(attr)

def __getitem__(self, item: Union[ATTRIBUTE_TYPE_CODE, int]) -> AttributeCollection:
if isinstance(item, EnumInstance):
if isinstance(item, Enum):
item = item.value
return self.data.get(item, AttributeCollection())

def __contains__(self, key: Union[ATTRIBUTE_TYPE_CODE, int]) -> bool:
if isinstance(key, EnumInstance):
if isinstance(key, Enum):
key = key.value
return super().__contains__(key)

Expand Down Expand Up @@ -234,7 +234,7 @@ def ensure_volume(ntfs: NTFS) -> None:
raise VolumeNotAvailableError()


def get_full_path(mft: Mft, name: str, parent: Instance, seen: set[str] = None) -> str:
def get_full_path(mft: Mft, name: str, parent: Structure, seen: set[str] = None) -> str:
"""Walk up parent file references to construct a full path.
Args:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ classifiers = [
"Topic :: Utilities",
]
dependencies = [
"dissect.cstruct>=3.0.dev,<4.0.dev",
"dissect.util>=3.0.dev,<4.0.dev",
"dissect.cstruct>3,<5",
"dissect.util>2,<4",
]
dynamic = ["version"]

Expand Down
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ deps =
pytest
pytest-cov
coverage
# Unfortunately, tox does not allow separate installation flags for the project
# dependencies and the test dependencies. When running tox, we want to install the
# project dependencies with the --pre flag, so that we get the latest version of all
# dependencies. We do the installation step ourselves for this reason.
skip_install = true
commands_pre =
pip install --pre -e .
commands =
pytest --basetemp="{envtmpdir}" {posargs:--color=yes --cov=dissect --cov-report=term-missing -v tests}
coverage report
Expand Down

0 comments on commit eda13e1

Please sign in to comment.