-
Notifications
You must be signed in to change notification settings - Fork 89
feat(handler): add erofs filesystem & decompression handler #1163
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 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
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,131 @@ | ||
{ | ||
lib, | ||
python3, | ||
fetchFromGitHub, | ||
makeWrapper, | ||
e2fsprogs-nofortify, | ||
erofs-utils, | ||
jefferson, | ||
lz4, | ||
lziprecover, | ||
lzop, | ||
p7zip, | ||
nix-filter, | ||
sasquatch, | ||
sasquatch-v4be, | ||
simg2img, | ||
ubi_reader, | ||
unar, | ||
zstd, | ||
versionCheckHook, | ||
rustPlatform, | ||
}: | ||
|
||
let | ||
# These dependencies are only added to PATH | ||
runtimeDeps = [ | ||
e2fsprogs-nofortify | ||
erofs-utils | ||
jefferson | ||
lziprecover | ||
lzop | ||
p7zip | ||
sasquatch | ||
sasquatch-v4be | ||
ubi_reader | ||
simg2img | ||
unar | ||
zstd | ||
lz4 | ||
]; | ||
pyproject_toml = (builtins.fromTOML (builtins.readFile ./pyproject.toml)); | ||
version = pyproject_toml.project.version; | ||
in | ||
python3.pkgs.buildPythonApplication rec { | ||
pname = "unblob"; | ||
pyproject = true; | ||
disabled = python3.pkgs.pythonOlder "3.9"; | ||
inherit version; | ||
src = nix-filter { | ||
root = ./.; | ||
include = [ | ||
"Cargo.lock" | ||
"Cargo.toml" | ||
"pyproject.toml" | ||
"python" | ||
"rust" | ||
"tests" | ||
"README.md" | ||
]; | ||
}; | ||
|
||
strictDeps = true; | ||
|
||
build-system = with python3.pkgs; [ poetry-core ]; | ||
|
||
dependencies = with python3.pkgs; [ | ||
arpy | ||
attrs | ||
click | ||
cryptography | ||
dissect-cstruct | ||
lark | ||
lief.py | ||
python3.pkgs.lz4 # shadowed by pkgs.lz4 | ||
plotext | ||
pluggy | ||
pyfatfs | ||
pyperscan | ||
python-magic | ||
pyzstd | ||
rarfile | ||
rich | ||
structlog | ||
treelib | ||
unblob-native | ||
]; | ||
|
||
cargoDeps = rustPlatform.importCargoLock { | ||
lockFile = ./Cargo.lock; | ||
}; | ||
|
||
nativeBuildInputs = with rustPlatform; [ | ||
cargoSetupHook | ||
maturinBuildHook | ||
makeWrapper | ||
]; | ||
|
||
# These are runtime-only CLI dependencies, which are used through | ||
# their CLI interface | ||
pythonRemoveDeps = [ | ||
"jefferson" | ||
"ubi-reader" | ||
]; | ||
|
||
pythonImportsCheck = [ "unblob" ]; | ||
|
||
makeWrapperArgs = [ | ||
"--prefix PATH : ${lib.makeBinPath runtimeDeps}" | ||
]; | ||
|
||
nativeCheckInputs = | ||
with python3.pkgs; | ||
[ | ||
pytestCheckHook | ||
pytest-cov | ||
versionCheckHook | ||
] | ||
++ runtimeDeps; | ||
|
||
versionCheckProgramArg = "--version"; | ||
|
||
pytestFlagsArray = [ | ||
"--no-cov" | ||
]; | ||
|
||
passthru = { | ||
# helpful to easily add these to a nix-shell environment | ||
inherit runtimeDeps; | ||
}; | ||
|
||
} |
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,76 @@ | ||
import io | ||
from typing import Optional | ||
|
||
from unblob.extractors import Command | ||
from unblob.file_utils import ( | ||
Endian, | ||
InvalidInputFormat, | ||
snull, | ||
) | ||
from unblob.models import ( | ||
File, | ||
HexString, | ||
StructHandler, | ||
ValidChunk, | ||
) | ||
|
||
C_DEFINITIONS = r""" | ||
typedef struct erofs_handler{ | ||
uint32_t magic; | ||
uint32_t crc32c; | ||
uint32_t feature_compact; | ||
uint8_t block_size_bs; | ||
uint8_t sb_extslots; | ||
uint16_t root_nid; | ||
uint64_t inos; | ||
uint64_t build_time; | ||
uint32_t build_time_nsec; | ||
uint32_t block_count; | ||
uint32_t meta_blkaddr; | ||
uint32_t xattr_blkaddr; | ||
uint8_t uuid[16]; | ||
char volume_name[16]; | ||
uint32_t feature_incompact; | ||
char reserved[44]; | ||
} erofs_handler_t; | ||
""" | ||
|
||
SUPERBLOCK_OFFSET = 0x400 | ||
|
||
|
||
class EROFSHandler(StructHandler): | ||
NAME = "erofs" | ||
PATTERNS = [HexString("e2 e1 f5 e0")] # Magic in little endian | ||
HEADER_STRUCT = "erofs_handler_t" | ||
C_DEFINITIONS = C_DEFINITIONS | ||
EXTRACTOR = Command( | ||
"fsck.erofs", | ||
"--no-preserve", | ||
"--extract={outdir}", | ||
"{inpath}", | ||
) | ||
PATTERN_MATCH_OFFSET = -SUPERBLOCK_OFFSET | ||
|
||
def is_valid_header(self, header) -> bool: | ||
try: | ||
snull(header.volume_name).decode("utf-8") | ||
except UnicodeDecodeError: | ||
return False | ||
return ( | ||
header.block_count >= 1 | ||
and header.build_time > 0 | ||
and header.build_time_nsec > 0 | ||
and header.block_size_bs >= 9 | ||
) | ||
|
||
def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: | ||
file.seek(start_offset + SUPERBLOCK_OFFSET, io.SEEK_SET) | ||
header = self.parse_header(file, Endian.LITTLE) | ||
if not self.is_valid_header(header): | ||
raise InvalidInputFormat("Invalid erofs header.") | ||
|
||
end_offset = (1 << header.block_size_bs) * header.block_count | ||
return ValidChunk( | ||
start_offset=start_offset, | ||
end_offset=end_offset, | ||
) |
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
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/android/erofs/__input__/foo.erofs.img
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/android/erofs/__input__/foo_padding.erofs.img
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/android/erofs/__output__/foo.erofs.img_extract/bar.txt
Git LFS file not shown
Binary file added
BIN
+127 Bytes
tests/integration/filesystem/android/erofs/__output__/foo.erofs.img_extract/notes/apple.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
...egration/filesystem/android/erofs/__output__/foo_padding.erofs.img_extract/0-1024.padding
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...gration/filesystem/android/erofs/__output__/foo_padding.erofs.img_extract/1024-4096.erofs
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...em/android/erofs/__output__/foo_padding.erofs.img_extract/1024-4096.erofs_extract/bar.txt
Git LFS file not shown
Binary file added
BIN
+127 Bytes
...id/erofs/__output__/foo_padding.erofs.img_extract/1024-4096.erofs_extract/notes/apple.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
...ation/filesystem/android/erofs/__output__/foo_padding.erofs.img_extract/4096-6144.padding
Git LFS file not shown
Oops, something went wrong.
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.