Skip to content

Commit

Permalink
Align btrfs st_blocks to how linux calculates it
Browse files Browse the repository at this point in the history
  • Loading branch information
Miauwkeru committed Oct 10, 2024
1 parent 6b06c93 commit ddbc901
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 4 additions & 1 deletion dissect/target/filesystems/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ def lstat(self) -> fsutil.stat_result:

# Add block information of the filesystem
st_info.st_blksize = entry.btrfs.sector_size
st_info.st_blocks = math.ceil(entry.size / st_info.st_blksize)

st_info.st_blocks = 0
if not self.is_dir():
st_info.st_blocks = (st_info.st_blksize // 512) * math.ceil(st_info.st_size / st_info.st_blksize)

return st_info
30 changes: 27 additions & 3 deletions tests/filesystems/test_btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@pytest.mark.parametrize(
"sector_size, filesize, expected_blocks",
[
(0x1000, 0x343, 0x1),
(0x1000, 0x1000, 0x1),
(0x1000, 0x1001, 0x2),
(0x1000, 0x343, 0x8),
(0x1000, 0x1000, 0x8),
(0x1000, 0x1001, 0x10),
],
ids=["lower", "equal", "greater"],
)
Expand Down Expand Up @@ -55,3 +55,27 @@ def test_stat_information_file_blocksize(sector_size: int, filesize: int, expect

assert stat_info.st_blksize == sector_size
assert stat_info.st_blocks == expected_blocks


def test_stat_directory() -> None:
"""Using btrfs stat information as a base"""
entry = INode(Mock(), 42, type=c_btrfs.BTRFS_FT_DIR)
entry.btrfs = Mock(sector_size=0x1000)
timestamp = c_btrfs.btrfs_timespec()
entry.inode = c_btrfs.btrfs_inode_item(
mode=0o777,
nlink=0,
uid=1000,
gid=1000,
size=0x1000,
atime=timestamp,
ctime=timestamp,
otime=timestamp,
mtime=timestamp,
)

fs_entry = BtrfsFilesystemEntry(Mock(), "some/path", entry)

stat_info = fs_entry.lstat()

assert stat_info.st_blocks == 0

0 comments on commit ddbc901

Please sign in to comment.