-
Notifications
You must be signed in to change notification settings - Fork 89
Add JFFS2 support to Dissect #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7c9a94b
add support for jffs targets
JSCU-CNI 4729cde
implement review comments
JSCU-CNI 8c8fe1f
Update dissect/target/filesystems/jffs.py
JSCU-CNI 4704b55
fix scandir
JSCU-CNI 7cd3892
Merge branch 'fox-it:main' into feature/add-jffs-filesystem
JSCU-CNI 80c5706
implement review comments
JSCU-CNI d329bd8
Apply suggestions from code review
JSCU-CNI e5fbc6c
Merge branch 'main' into feature/add-jffs-filesystem
JSCU-CNI 619a9d0
Merge branch 'main' into feature/add-jffs-filesystem
Schamper e393669
Small tweaks
Schamper 557605d
Merge branch 'main' into feature/add-jffs-filesystem
Schamper d4026ba
Rename __fstype__ to __type__
Schamper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| from typing import BinaryIO, Iterator, Optional | ||
|
|
||
| from dissect.jffs import c_jffs2, jffs2 | ||
|
|
||
| from dissect.target.exceptions import ( | ||
| FileNotFoundError, | ||
| FilesystemError, | ||
| IsADirectoryError, | ||
| NotADirectoryError, | ||
| NotASymlinkError, | ||
| ) | ||
| from dissect.target.filesystem import Filesystem, FilesystemEntry | ||
| from dissect.target.helpers import fsutil | ||
|
|
||
|
|
||
| class JFFSFilesystem(Filesystem): | ||
| __fstype__ = "jffs" | ||
|
|
||
| def __init__(self, fh: BinaryIO, *args, **kwargs): | ||
| super().__init__(fh, *args, **kwargs) | ||
| self.jffs2 = jffs2.JFFS2(fh) | ||
|
|
||
| @staticmethod | ||
| def _detect(fh: BinaryIO) -> bool: | ||
| block = fh.read(2) | ||
| magic = int.from_bytes(block[0:2], "little") | ||
|
|
||
| return magic in ( | ||
| c_jffs2.c_jffs2.JFFS2_MAGIC_BITMASK, | ||
| c_jffs2.c_jffs2.JFFS2_OLD_MAGIC_BITMASK, | ||
| ) | ||
|
|
||
| def get(self, path: str) -> FilesystemEntry: | ||
| return JFFSFilesystemEntry(self, path, self._get_node(path)) | ||
|
|
||
| def _get_node(self, path: str, node: Optional[jffs2.INode] = None): | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try: | ||
| return self.jffs2.get(path, node) | ||
| except jffs2.FileNotFoundError as e: | ||
| raise FileNotFoundError(path, cause=e) | ||
| except jffs2.NotADirectoryError as e: | ||
| raise NotADirectoryError(path, cause=e) | ||
| except jffs2.NotASymlinkError as e: | ||
| raise NotASymlinkError(path, cause=e) | ||
| except jffs2.Error as e: | ||
| raise FileNotFoundError(path, cause=e) | ||
|
|
||
|
|
||
| class JFFSFilesystemEntry(FilesystemEntry): | ||
| def _resolve(self) -> FilesystemEntry: | ||
| if self.is_symlink(): | ||
| return self.readlink_ext() | ||
| return self | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def get(self, path: str) -> FilesystemEntry: | ||
| entry_path = fsutil.join(self.path, path, alt_separator=self.fs.alt_separator) | ||
| entry = self.fs._get_node(path, self.entry) | ||
| return JFFSFilesystemEntry(self.fs, entry_path, entry) | ||
|
|
||
| def open(self) -> BinaryIO: | ||
| if self.is_dir(): | ||
| raise IsADirectoryError(self.path) | ||
| return self._resolve().entry.open() | ||
|
|
||
| def iterdir(self) -> Iterator[str]: | ||
| if not self.is_dir(): | ||
| raise NotADirectoryError(self.path) | ||
|
|
||
| if self.is_symlink(): | ||
| for name, entry in self.readlink_ext().iterdir(): | ||
| yield name, entry | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| for name, entry in self.entry.iterdir(): | ||
| yield name, entry | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def scandir(self) -> Iterator[FilesystemEntry]: | ||
| for name, entry in self.iterdir(): | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| entry_path = fsutil.join(self.path, name, alt_separator=self.fs.alt_separator) | ||
| yield JFFSFilesystemEntry(self.fs, entry_path, entry) | ||
|
|
||
| def is_dir(self, follow_symlinks: bool = False) -> bool: | ||
| try: | ||
| return self._resolve().entry.is_dir() | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| except FilesystemError: | ||
| return False | ||
|
|
||
| def is_file(self, follow_symlinks: bool = False) -> bool: | ||
| try: | ||
| return self._resolve().entry.is_file() | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| except FilesystemError: | ||
| return False | ||
|
|
||
| def is_symlink(self) -> bool: | ||
| return self.entry.is_symlink() | ||
|
|
||
| def readlink(self) -> str: | ||
| if not self.is_symlink(): | ||
| raise NotASymlinkError() | ||
|
|
||
| return self.entry.link | ||
|
|
||
| def stat(self, follow_symlinks: bool = False) -> fsutil.stat_result: | ||
| return self._resolve().lstat() | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def lstat(self) -> fsutil.stat_result: | ||
| node = self.entry.inode | ||
|
|
||
| # mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime | ||
| st_info = fsutil.stat_result( | ||
| [ | ||
| self.entry.mode, | ||
| self.entry.inum, | ||
| 0, | ||
JSCU-CNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 0, | ||
Schamper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| node.uid, | ||
| node.gid, | ||
| node.isize, | ||
| self.entry.atime.timestamp(), | ||
| self.entry.mtime.timestamp(), | ||
| self.entry.ctime.timestamp(), | ||
| ] | ||
| ) | ||
|
|
||
| return st_info | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.