Skip to content

Commit

Permalink
Extend stat unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
twiggler committed Oct 7, 2024
1 parent a823a66 commit d74c3b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
9 changes: 3 additions & 6 deletions dissect/target/filesystems/xfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def _detect(fh: BinaryIO) -> bool:
def get(self, path: str) -> FilesystemEntry:
return XfsFilesystemEntry(self, path, self._get_node(path))

@property
def block_size(self) -> int:
return self.xfs.block_size

def _get_node(self, path: str, node: Optional[xfs.INode] = None) -> xfs.INode:
try:
return self.xfs.get(path, node)
Expand Down Expand Up @@ -134,13 +130,14 @@ def lstat(self) -> fsutil.stat_result:
st_info.st_mtime_ns = self.entry.mtime_ns
st_info.st_ctime_ns = self.entry.ctime_ns

st_info.st_blksize = self.fs.block_size
st_info.st_blksize = self.fs.xfs.block_size
# Convert number of filesystem blocks to basic blocks
# Reference: https://github.com/torvalds/linux/blob/e32cde8d2bd7d251a8f9b434143977ddf13dcec6/fs/xfs/xfs_iops.c#L602 # noqa: E501
st_info.st_blocks = self.entry.number_of_blocks * (self.fs.block_size // 512)
st_info.st_blocks = self.entry.nblocks * (self.fs.xfs.block_size // 512)

# XFS has a birth time, since inode version 3 (version 5 of filesystem)
st_info.st_birthtime = self.entry.crtime.timestamp()
st_info.st_birthtime_ns = self.entry.crtime_ns

return st_info

Expand Down
47 changes: 38 additions & 9 deletions tests/filesystems/test_xfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from unittest.mock import Mock, patch

import pytest
from dissect.xfs.xfs import INode

from dissect.target.filesystems.xfs import XfsFilesystem, XfsFilesystemEntry

NANOSECONDS_IN_SECOND = 1_000_000_000


@pytest.fixture
def xfs_fs() -> Iterator[XfsFilesystem]:
Expand All @@ -19,21 +20,49 @@ def xfs_fs() -> Iterator[XfsFilesystem]:

@pytest.fixture
def xfs_fs_entry(xfs_fs: XfsFilesystem) -> Iterator[XfsFilesystemEntry]:
mock_datetime = datetime(2023, 10, 1, 12, 0, 0)
atime = datetime(2024, 10, 1, 12, 0, 0)
mtime = datetime(2024, 10, 2, 12, 0, 0)
ctime = datetime(2024, 10, 3, 12, 0, 0)
crtime = datetime(2024, 10, 4, 12, 0, 0)

dinode = Mock(di_mode=33272, di_nlink=1, di_uid=1000, di_gid=999)
inode = Mock(
spec=INode,
number_of_blocks=10,
nblocks=10,
inode=dinode,
inum=4,
atime=mock_datetime,
mtime=mock_datetime,
ctime=mock_datetime,
crtime=mock_datetime,
size=4594,
atime=atime,
atime_ns=atime.timestamp() * NANOSECONDS_IN_SECOND,
mtime=mtime,
mtime_ns=mtime.timestamp() * NANOSECONDS_IN_SECOND,
ctime=ctime,
ctime_ns=ctime.timestamp() * NANOSECONDS_IN_SECOND,
crtime=crtime,
crtime_ns=crtime.timestamp() * NANOSECONDS_IN_SECOND,
)
entry = XfsFilesystemEntry(xfs_fs, "/some_file", inode)
yield entry


def test_es_filesystems_xfs_stat_blocks(xfs_fs: XfsFilesystem, xfs_fs_entry: XfsFilesystemEntry):
def test_es_filesystems_xfs_stat(xfs_fs: XfsFilesystem, xfs_fs_entry: XfsFilesystemEntry) -> None:
stat = xfs_fs_entry.stat()

entry = xfs_fs_entry.entry
assert stat.st_mode == entry.inode.di_mode
assert stat.st_ino == entry.inum
assert stat.st_dev == id(xfs_fs)
assert stat.st_nlink == entry.inode.di_nlink
assert stat.st_uid == entry.inode.di_uid
assert stat.st_gid == entry.inode.di_gid
assert stat.st_size == entry.size
assert stat.st_atime == entry.atime.timestamp()
assert stat.st_atime_ns == entry.atime_ns
assert stat.st_mtime == entry.mtime.timestamp()
assert stat.st_mtime_ns == entry.mtime_ns
assert stat.st_ctime == entry.ctime.timestamp()
assert stat.st_ctime_ns == entry.ctime_ns
assert stat.st_birthtime == entry.crtime.timestamp()
assert stat.st_birthtime_ns == entry.crtime_ns

assert stat.st_blksize == 4096
assert stat.st_blocks == 10 * 4096 // 512

0 comments on commit d74c3b8

Please sign in to comment.