-
Notifications
You must be signed in to change notification settings - Fork 2
Calculate nlinks #13
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
Calculate nlinks #13
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,9 +43,13 @@ | |
| def __init__(self, fh: BinaryIO): | ||
| self.fh = fh | ||
|
|
||
| # Dict of ino to list of tuples of (inode, offset) | ||
| self._inodes: dict[int, list[tuple[c_jffs2.jffs2_raw_inode, int]]] = {} | ||
| # Dict of pino to dict of dirent names to list of dirents | ||
| self._dirents: dict[int, dict[bytes, list[DirEntry]]] = {} | ||
| self._lost_found: list[list[DirEntry]] = [] | ||
| # Map of nlinks by inum | ||
| self._nlinks_by_inum: dict[int, int] = {} | ||
|
|
||
| if (node := c_jffs2.jffs2_unknown_node(fh)).magic not in JFFS2_MAGIC_NUMBERS: | ||
| raise Error(f"Unknown JFFS2 magic: {node.magic:#x}") | ||
|
|
@@ -55,6 +59,7 @@ | |
| self.root = self.inode(inum=0x1, type=c_jffs2.DT_DIR, parent=None) | ||
|
|
||
| self._scan() | ||
| self._count_nlinks() | ||
| self._garbage_collect() | ||
|
|
||
| def inode(self, inum: int, type: int | None = None, parent: INode | None = None) -> INode: | ||
|
|
@@ -147,6 +152,34 @@ | |
|
|
||
| pos += (totlen + 3) & ~3 | ||
|
|
||
| def _count_nlinks(self) -> None: | ||
| """Count the number of hardlinks for each inode. | ||
| JFFS does not store nlink information in the inode itself, so we have to calculate it. | ||
| """ | ||
| for direntries in self._dirents.values(): | ||
| for versions in direntries.values(): | ||
| if not versions: | ||
| continue | ||
|
|
||
| last_version = versions[-1] | ||
| if last_version.type == c_jffs2.DT_DIR: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note that this code will massively benefit from fox-it/dissect.cstruct#114 |
||
| # Root dir (inum == 1) gets three nlinks | ||
| # (see https://github.com/torvalds/linux/blob/6485cf5ea253d40d507cd71253c9568c5470cd27/fs/jffs2/fs.c#L311) | ||
| self._nlinks_by_inum[last_version.inum] = 3 if last_version.inum == 1 else 2 | ||
|
|
||
| # Now update the parent entry, which might not have an associated nlink yet. | ||
| # The parent directory gets one nlink for each child directory. | ||
| base_nlinks = 3 if last_version.parent_inum == 1 else 2 | ||
| self._nlinks_by_inum[last_version.parent_inum] = ( | ||
| self._nlinks_by_inum.get(last_version.parent_inum, base_nlinks) + 1 | ||
| ) | ||
|
|
||
| elif last_version.type == c_jffs2.DT_REG: | ||
| self._nlinks_by_inum[last_version.inum] = self._nlinks_by_inum.get(last_version.inum, 0) + 1 | ||
| elif last_version.type == c_jffs2.DT_LNK: | ||
| self._nlinks_by_inum[last_version.inum] = 1 | ||
|
|
||
| def _garbage_collect(self) -> None: | ||
| """Collect all found orphaned files and put them in the lost+found folder.""" | ||
| if not self._lost_found: | ||
|
|
@@ -267,6 +300,10 @@ | |
| def gid(self) -> int: | ||
| return self.inode.gid | ||
|
|
||
| @cached_property | ||
| def nlink(self) -> int: | ||
| return self.fs._nlinks_by_inum.get(self.inum, 0) | ||
|
|
||
| def is_dir(self) -> bool: | ||
| return self.type == stat.S_IFDIR | ||
|
|
||
|
|
||
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
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.