Skip to content

Commit

Permalink
Compatibility with cstruct v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed May 24, 2024
1 parent 7266125 commit fd30a27
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
5 changes: 2 additions & 3 deletions dissect/extfs/c_ext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stat

from dissect import cstruct
from dissect.cstruct import cstruct

ext_def = """
#define EXT2_SBOFF 1024 // offset to superblock
Expand Down Expand Up @@ -397,8 +397,7 @@
};
"""

c_ext = cstruct.cstruct()
c_ext.load(ext_def)
c_ext = cstruct().load(ext_def)

EXT2 = 2
EXT3 = 3
Expand Down
5 changes: 2 additions & 3 deletions dissect/extfs/c_jdb2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dissect import cstruct
from dissect.cstruct import cstruct

jdb2_def = """
#define JBD2_MAGIC_NUMBER 0xC03B3998
Expand Down Expand Up @@ -87,5 +87,4 @@
};
"""

c_jdb2 = cstruct.cstruct(endian=">")
c_jdb2.load(jdb2_def)
c_jdb2 = cstruct(endian=">").load(jdb2_def)
11 changes: 5 additions & 6 deletions dissect/extfs/extfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import BinaryIO, Iterator, Optional, Union
from uuid import UUID

from dissect.cstruct import Instance
from dissect.util import ts
from dissect.util.stream import RangeStream, RunlistStream

Expand Down Expand Up @@ -154,7 +153,7 @@ def get_inode(

return inode

def _read_group_desc(self, group_num: int) -> Instance:
def _read_group_desc(self, group_num: int) -> c_ext.ext2_group_desc | c_ext.ext4_group_desc:
if group_num >= self.groups_count:
raise Error("Group number exceeds amount of groups")

Expand Down Expand Up @@ -204,7 +203,7 @@ def __init__(
def __repr__(self) -> str:
return f"<inode {self.inum}>"

def _read_inode(self) -> Instance:
def _read_inode(self) -> c_ext.ext4_inode:
block_group_num, index = divmod(self.inum - 1, self.extfs.sb.s_inodes_per_group)
block_group = self.extfs._read_group_desc(block_group_num)

Expand All @@ -218,7 +217,7 @@ def _read_inode(self) -> Instance:
return c_ext.ext4_inode(self.extfs.fh)

@property
def inode(self) -> Instance:
def inode(self) -> c_ext.ext4_inode:
if not self._inode:
self._inode = self._read_inode()
return self._inode
Expand Down Expand Up @@ -456,7 +455,7 @@ def open(self) -> BinaryIO:


class XAttr:
def __init__(self, extfs: ExtFS, inode: INode, entry: Instance, value: bytes):
def __init__(self, extfs: ExtFS, inode: INode, entry: c_ext.ext4_xattr_entry, value: bytes):
self.extfs = extfs
self.inode = inode
self.entry = entry
Expand Down Expand Up @@ -496,7 +495,7 @@ def _parse_indirect(inode: INode, offset: int, num_blocks: int, level: int) -> l
return blocks


def _parse_extents(inode: INode, buf: bytes) -> Iterator[Instance]:
def _parse_extents(inode: INode, buf: bytes) -> Iterator[c_ext.ext4_extent]:
extent_header = c_ext.ext4_extent_header(buf)

if extent_header.eh_magic != 0xF30A:
Expand Down
13 changes: 9 additions & 4 deletions dissect/extfs/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io
from typing import BinaryIO, Iterator, Optional

from dissect.cstruct import Instance
from dissect.util.stream import RangeStream

from dissect.extfs.c_jdb2 import c_jdb2
Expand Down Expand Up @@ -83,7 +82,7 @@ def walk(self) -> Iterator[CommitBlock]:


class DescriptorBlock:
def __init__(self, jdb2: JDB2, header: Instance, block: int):
def __init__(self, jdb2: JDB2, header: c_jdb2.journal_header, block: int):
self.jdb2 = jdb2
self.header = header
self.journal_block = block
Expand All @@ -110,7 +109,9 @@ def tags(self) -> Iterator[DescriptorBlockTag]:


class DescriptorBlockTag:
def __init__(self, descriptor: DescriptorBlock, tag: Instance, journal_block: int):
def __init__(
self, descriptor: DescriptorBlock, tag: c_jdb2.journal_block_tag | c_jdb2.journal_block_tag3, journal_block: int
):
self.descriptor = descriptor
self.tag = tag
self.journal_block = journal_block
Expand All @@ -127,7 +128,11 @@ def open(self) -> BinaryIO:

class CommitBlock:
def __init__(
self, jdb2: JDB2, header: Instance, journal_block: int, descriptors: Optional[list[DescriptorBlock]] = None
self,
jdb2: JDB2,
header: c_jdb2.commit_header,
journal_block: int,
descriptors: Optional[list[DescriptorBlock]] = None,
):
self.jdb2 = jdb2
self.header = header
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 fd30a27

Please sign in to comment.