Skip to content

Commit e22a031

Browse files
authored
Fix #14 (#16)
1 parent 4d60e3c commit e22a031

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎ext4/block.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def peek(self, size: int = 0) -> bytes:
106106
if self.cursor >= len(self):
107107
return b""
108108

109+
if self.cursor + size >= len(self):
110+
size = len(self) - self.cursor
111+
109112
start_index = self.cursor // self.block_size
110113
end_index = (self.cursor + size - 1) // self.block_size
111114
start_offset = self.cursor % self.block_size

‎test.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ext4
66

77
from typing import cast
8+
from typing import Callable
89

910
FAILED = False
1011

@@ -26,7 +27,7 @@ def test_path_tuple(path: str | bytes, expected: tuple[bytes, ...]):
2627
print(e)
2728

2829

29-
def _assert(source: str):
30+
def _assert(source: str, debug: Callable[[], str] | None = None):
3031
global FAILED
3132
print(f"check {source}: ", end="")
3233
if eval(source):
@@ -35,6 +36,8 @@ def _assert(source: str):
3536

3637
FAILED = True
3738
print("fail")
39+
if debug is not None:
40+
print(f" {debug()}")
3841

3942

4043
test_path_tuple("/", tuple())
@@ -91,8 +94,15 @@ def _assert(source: str):
9194
attrs = {k: v for k, v in inode.xattrs}
9295
for j in range(1, 21):
9396
_assert(f'attrs["user.name{j}"] == b"value{i}_{j}"')
97+
9498
data = inode.open().read()
9599
_assert(f'data == b"hello world{i}\\n"')
96100

101+
inode = cast(ext4.File, volume.inode_at("/test1.txt"))
102+
b = inode.open()
103+
data = b"hello world1\n"
104+
for x in range(1, 15):
105+
_assert(f"b.peek({x}) == {data[:x]}", lambda: b.peek(x))
106+
97107
if FAILED:
98108
sys.exit(1)

0 commit comments

Comments
 (0)