From eae9a10fe4c59bb639a9b557ac9d0b0a1f8d55fe Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 12 Feb 2026 12:40:35 -0700 Subject: [PATCH] Fix #14 --- ext4/block.py | 3 +++ test.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ext4/block.py b/ext4/block.py index a63b533..ead3472 100644 --- a/ext4/block.py +++ b/ext4/block.py @@ -106,6 +106,9 @@ def peek(self, size: int = 0) -> bytes: if self.cursor >= len(self): return b"" + if self.cursor + size >= len(self): + size = len(self) - self.cursor + start_index = self.cursor // self.block_size end_index = (self.cursor + size - 1) // self.block_size start_offset = self.cursor % self.block_size diff --git a/test.py b/test.py index b68cf6b..124691e 100644 --- a/test.py +++ b/test.py @@ -5,6 +5,7 @@ import ext4 from typing import cast +from typing import Callable FAILED = False @@ -26,7 +27,7 @@ def test_path_tuple(path: str | bytes, expected: tuple[bytes, ...]): print(e) -def _assert(source: str): +def _assert(source: str, debug: Callable[[], str] | None = None): global FAILED print(f"check {source}: ", end="") if eval(source): @@ -35,6 +36,8 @@ def _assert(source: str): FAILED = True print("fail") + if debug is not None: + print(f" {debug()}") test_path_tuple("/", tuple()) @@ -91,8 +94,15 @@ def _assert(source: str): attrs = {k: v for k, v in inode.xattrs} for j in range(1, 21): _assert(f'attrs["user.name{j}"] == b"value{i}_{j}"') + data = inode.open().read() _assert(f'data == b"hello world{i}\\n"') + inode = cast(ext4.File, volume.inode_at("/test1.txt")) + b = inode.open() + data = b"hello world1\n" + for x in range(1, 15): + _assert(f"b.peek({x}) == {data[:x]}", lambda: b.peek(x)) + if FAILED: sys.exit(1)