Skip to content

Commit 13b37af

Browse files
committed
Clean up inode flag checking
1 parent ab23778 commit 13b37af

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

ext4/inode.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,13 @@ def validate(self) -> None:
326326
if self.tree is not None:
327327
self.tree.validate()
328328

329+
def has_flag(self, flag: EXT4_FL | int) -> bool:
330+
i_flags = assert_cast(self.i_flags, EXT4_FL) # pyright: ignore[reportAny]
331+
return (i_flags & flag) != 0
332+
329333
@property
330334
def is_inline(self) -> bool:
331-
i_flags = assert_cast(self.i_flags, EXT4_FL) # pyright: ignore[reportAny]
332-
return (i_flags & EXT4_FL.EXTENTS) == 0
335+
return not self.has_flag(EXT4_FL.EXTENTS)
333336

334337
@property
335338
def extents(self) -> list[Extent]:
@@ -450,18 +453,15 @@ def has_filetype(self) -> bool:
450453

451454
@property
452455
def is_htree(self) -> bool:
453-
i_flags = assert_cast(self.i_flags, EXT4_FL) # pyright: ignore[reportAny]
454-
return i_flags & EXT4_FL.INDEX != 0
456+
return self.has_flag(EXT4_FL.INDEX)
455457

456458
@property
457459
def is_casefolded(self) -> bool:
458-
i_flags = assert_cast(self.i_flags, EXT4_FL) # pyright: ignore[reportAny]
459-
return i_flags & EXT4_FL.CASEFOLD != 0
460+
return self.has_flag(EXT4_FL.CASEFOLD)
460461

461462
@property
462463
def is_encrypted(self) -> bool:
463-
i_flags = assert_cast(self.i_flags, EXT4_FL) # pyright: ignore[reportAny]
464-
return i_flags & EXT4_FL.ENCRYPT != 0
464+
return self.has_flag(EXT4_FL.ENCRYPT)
465465

466466
@property
467467
def hash_in_dirent(self) -> bool:

ext4/struct.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def field_type(cls, name: str) -> SimpleCData | None:
8888
def read_from_volume(self):
8989
_ = self.volume.seek(self.offset)
9090
data = self.volume.read(sizeof(self))
91+
if len(data) != sizeof(self):
92+
raise OSError(
93+
f"Short read for {type(self).__name__} at offset {self.offset}"
94+
)
95+
9196
_ = memmove(addressof(self), data, sizeof(self))
9297

9398
@property

ext4/volume.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def __init__(
9595
table_offset = (self.superblock.offset // block_size + 1) * block_size
9696
s_inodes_count = assert_cast(self.superblock.s_inodes_count, int) # pyright: ignore[reportAny]
9797
s_inodes_per_group = assert_cast(self.superblock.s_inodes_per_group, int) # pyright: ignore[reportAny]
98-
for index in range(0, s_inodes_count // s_inodes_per_group):
98+
for index in range(
99+
0,
100+
(s_inodes_count + s_inodes_per_group - 1) // s_inodes_per_group,
101+
):
99102
descriptor = BlockDescriptor(
100103
self,
101104
table_offset + (index * self.superblock.desc_size),

ext4/xattr.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ def __iter__(self) -> Generator[tuple[str, bytes], None, None]:
6262
i = 0
6363
while i < self.data_size:
6464
entry = ExtendedAttributeEntry(self.inode, offset + i, self.data_size - i)
65-
e_name_len = assert_cast(entry.e_name_len, int) # pyright: ignore[reportAny]
66-
e_name_index = assert_cast(entry.e_name_index, int) # pyright: ignore[reportAny]
67-
e_value_offs = assert_cast(entry.e_value_offs, int) # pyright: ignore[reportAny]
65+
e_name_len = assert_cast(entry.e_name_len, int)
66+
e_name_index = assert_cast(entry.e_name_index, int)
67+
e_value_offs = assert_cast(entry.e_value_offs, int)
6868
if (e_name_len | e_name_index | e_value_offs | entry.value_inum) == 0:
6969
break
7070

7171
value: bytes
72-
e_value_size = assert_cast(entry.e_value_size, int) # pyright: ignore[reportAny]
72+
e_value_size = assert_cast(entry.e_value_size, int)
7373
if entry.value_inum != 0:
7474
inode = self.volume.inodes[entry.value_inum]
75-
i_flags = assert_cast(inode.i_flags, EXT4_FL) # pyright: ignore[reportAny]
76-
if (i_flags & EXT4_FL.EA_INODE) != 0:
75+
if not inode.has_flag(EXT4_FL.EA_INODE):
7776
message = f"Inode {inode.i_no:d} is not marked as large extended attribute value"
7877
if not self.volume.ignore_flags:
7978
raise ExtendedAttributeError(message)
79+
8080
warnings.warn(message, RuntimeWarning)
8181

8282
# TODO determine if e_value_size or i_size are required to limit results?

0 commit comments

Comments
 (0)