Skip to content

Commit

Permalink
Apply suggestions code review
Browse files Browse the repository at this point in the history
Read MFT records based on segment numbers
  • Loading branch information
Zawadidone committed Apr 9, 2024
1 parent a01c824 commit 42b2942
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions dissect/ntfs/mft.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@ def get(self, ref: Union[int, str, Instance], root: Optional[MftRecord] = None)
else:
raise TypeError(f"Invalid MFT reference: {ref!r}")

def segments(self) -> Iterator[MftRecord]:
def segments(self, start: int = 0, end: int = -1) -> Iterator[MftRecord]:
"""Yield all valid MFT records, regardless if they're allocated or not."""
record_size = self.ntfs._record_size if self.ntfs else DEFAULT_RECORD_SIZE
mft_size = self.get(FILE_NUMBER_MFT).size()
end = (self.get(FILE_NUMBER_MFT).size() // record_size) if end == -1 else end

for segment in range(mft_size // record_size):
if start > end:
raise Error("The start segment cannot be higher than the end segment number")

for segment in range(start, end + 1):
try:
yield self.get(segment)
except Error:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_mft.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ def test_mft_record() -> None:
record = mft.get(0)
assert record.filename == "$MFT"
assert record.filenames() == ["$MFT"]


def test_mft_records_segment_number(mft_bin: BinaryIO) -> None:
fs = NTFS(mft=mft_bin)

assert fs.mft
assert len(list(fs.mft.segments())) == 37

records = list(fs.mft.segments(0, 4))

assert len(records) == 5
assert records[0].filename == "$MFT"
assert records[4].filename == "$AttrDef"

0 comments on commit 42b2942

Please sign in to comment.