Skip to content

Commit b6ee670

Browse files
authored
Compatibility with cstruct v4 (#28)
1 parent b8fa810 commit b6ee670

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

dissect/xfs/c_xfs.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import stat
22

3-
from dissect import cstruct
3+
from dissect.cstruct import cstruct
44

55
xfs_def = """
66
typedef uint64 xfs_ino_t; /* <inode> type */
@@ -888,8 +888,7 @@
888888
} xfs_dir2_block_tail_t;
889889
"""
890890

891-
c_xfs = cstruct.cstruct(endian=">")
892-
c_xfs.load(xfs_def)
891+
c_xfs = cstruct(endian=">").load(xfs_def)
893892

894893
FILETYPES = {
895894
0x0: None,

dissect/xfs/xfs.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import BinaryIO, Iterator, Optional, Union
1010
from uuid import UUID
1111

12-
from dissect.cstruct import Instance
1312
from dissect.util import ts
1413
from dissect.util.stream import RangeStream, RunlistStream
1514

@@ -95,7 +94,7 @@ def get_relative_inode(self, agnum: int, inum: int, *args, **kwargs) -> INode:
9594

9695
return self.get_allocation_group(agnum).get_inode(inum, *args, **kwargs)
9796

98-
def walk_agi(self, block: int, agnum: int) -> Iterator[Instance]:
97+
def walk_agi(self, block: int, agnum: int) -> Iterator[c_xfs.xfs_inobt_rec]:
9998
for record in self.walk_small_tree(block, agnum, 16, (c_xfs.XFS_IBT_MAGIC, c_xfs.XFS_IBT_CRC_MAGIC)):
10099
yield c_xfs.xfs_inobt_rec(record)
101100

@@ -119,7 +118,11 @@ def walk_small_tree(
119118
yield from self._walk_small_tree(root, leaf_size, agnum, magic)
120119

121120
def _walk_small_tree(
122-
self, node: Instance, leaf_size: int, agnum: int, magic: Optional[list[int]] = None
121+
self,
122+
node: c_xfs.xfs_btree_sblock | c_xfs.xfs_btree_sblock_crc,
123+
leaf_size: int,
124+
agnum: int,
125+
magic: Optional[list[int]] = None,
123126
) -> Iterator[bytes]:
124127
fh = self.fh
125128
if magic and node.bb_magic not in magic:
@@ -141,7 +144,12 @@ def _walk_small_tree(
141144

142145
yield from self._walk_small_tree(self._sblock_s(fh), leaf_size, agnum, magic)
143146

144-
def _walk_large_tree(self, node: Instance, leaf_size: int, magic: Optional[list[int]] = None) -> Iterator[bytes]:
147+
def _walk_large_tree(
148+
self,
149+
node: c_xfs.xfs_btree_lblock | c_xfs.xfs_btree_lblock_crc,
150+
leaf_size: int,
151+
magic: Optional[list[int]] = None,
152+
) -> Iterator[bytes]:
145153
fh = self.fh
146154
if magic and node.bb_magic not in magic:
147155
magic_values = ", ".join([f"0x{magic_value:x}" for magic_value in magic])
@@ -219,7 +227,7 @@ def walk_extents(self, fsb: int) -> Iterator[tuple[int, int, int, int]]:
219227
block = agnum * self.xfs.sb.sb_agblocks + blknum
220228
yield from self.xfs.walk_extents(block)
221229

222-
def walk_agi(self) -> Iterator[Instance]:
230+
def walk_agi(self) -> Iterator[c_xfs.xfs_inobt_rec]:
223231
yield from self.xfs.walk_agi(self.agi.agi_root, self.num)
224232

225233
def walk_tree(self, fsb: int, magic: Optional[list[int]] = None, small: bool = False):
@@ -260,7 +268,7 @@ def __init__(
260268
def __repr__(self) -> str:
261269
return f"<inode {self.inum} ({self.ag.num}:{self.relative_inum})>"
262270

263-
def _read_inode(self) -> Instance:
271+
def _read_inode(self) -> c_xfs.xfs_dinode:
264272
self.ag.fh.seek(self.relative_inum * self.ag.sb.sb_inodesize)
265273
self._buf = io.BytesIO(self.ag.fh.read(self.ag.sb.sb_inodesize))
266274
inode = c_xfs.xfs_dinode(self._buf)
@@ -271,7 +279,7 @@ def _read_inode(self) -> Instance:
271279
return inode
272280

273281
@property
274-
def inode(self) -> Instance:
282+
def inode(self) -> c_xfs.xfs_dinode:
275283
if not self._inode:
276284
self._inode = self._read_inode()
277285
return self._inode

pyproject.toml

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ classifiers = [
2525
"Topic :: Utilities",
2626
]
2727
dependencies = [
28-
"dissect.cstruct>=3.0.dev,<4.0.dev",
29-
"dissect.util>=3.0.dev,<4.0.dev",
28+
"dissect.cstruct>=4.dev,<5",
29+
"dissect.util>=3,<4",
3030
]
3131
dynamic = ["version"]
3232

@@ -35,6 +35,12 @@ homepage = "https://dissect.tools"
3535
documentation = "https://docs.dissect.tools/en/latest/projects/dissect.xfs"
3636
repository = "https://github.com/fox-it/dissect.xfs"
3737

38+
[project.optional-dependencies]
39+
dev = [
40+
"dissect.cstruct>=4.0.dev,<5.0.dev",
41+
"dissect.util>=3.0.dev,<4.0.dev",
42+
]
43+
3844
[tool.black]
3945
line-length = 120
4046

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ minversion = 4.4.3
1111
requires = virtualenv>=20.16.6
1212

1313
[testenv]
14+
extras = dev
1415
deps =
1516
pytest
1617
pytest-cov

0 commit comments

Comments
 (0)