Skip to content

Commit 9275b30

Browse files
committed
Replace is_* functions by an enum
1 parent 7826986 commit 9275b30

File tree

3 files changed

+37
-55
lines changed

3 files changed

+37
-55
lines changed

reolinkfw/__init__.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import posixpath
44
import re
5+
from enum import Enum
56
from pathlib import Path
67
from typing import Optional
78
from urllib.parse import parse_qsl, urlparse
@@ -12,19 +13,15 @@
1213
from lxml.html import document_fromstring
1314
from pakler import PAK, is_pak_file
1415
from pycramfs import Cramfs
16+
from pycramfs.const import MAGIC_BYTES as CRAMFS_MAGIC
1517
from PySquashfsImage import SquashFsImage
18+
from PySquashfsImage.const import SQUASHFS_MAGIC
19+
from ubireader.ubi.defines import UBI_EC_HDR_MAGIC as UBI_MAGIC
1620
from ubireader.ubifs import ubifs, walk
21+
from ubireader.ubifs.defines import UBIFS_NODE_MAGIC as UBIFS_MAGIC
1722
from ubireader.ubifs.output import _process_reg_file
1823

19-
from reolinkfw.util import (
20-
DummyLEB,
21-
get_fs_from_ubi,
22-
is_cramfs,
23-
is_squashfs,
24-
is_ubi,
25-
is_ubifs,
26-
sha256_pak
27-
)
24+
from reolinkfw.util import DummyLEB, get_fs_from_ubi, sha256_pak
2825

2926
__version__ = "1.1.0"
3027

@@ -35,6 +32,20 @@
3532
FS_SECTIONS = ROOTFS_SECTIONS + ["app"]
3633

3734

35+
class FileSystem(Enum):
36+
CRAMFS = CRAMFS_MAGIC
37+
SQUASHFS = SQUASHFS_MAGIC.to_bytes(4, "little")
38+
UBI = UBI_MAGIC # Not a file system
39+
UBIFS = UBIFS_MAGIC
40+
41+
@classmethod
42+
def from_magic(cls, key, default=None):
43+
try:
44+
return cls(key)
45+
except ValueError:
46+
return default
47+
48+
3849
async def download(url):
3950
"""Return resource as bytes.
4051
@@ -111,12 +122,12 @@ def get_files_from_ubifs(binbytes):
111122

112123
def get_files_from_ubi(binbytes):
113124
fsbytes = get_fs_from_ubi(binbytes)
114-
if is_ubifs(fsbytes):
125+
fs = FileSystem.from_magic(fsbytes[:4])
126+
if fs == FileSystem.UBIFS:
115127
return get_files_from_ubifs(fsbytes)
116-
elif is_squashfs(fsbytes):
128+
elif fs == FileSystem.SQUASHFS:
117129
return get_files_from_squashfs(fsbytes)
118-
else:
119-
raise Exception("unknown file system in UBI")
130+
raise Exception("Unknown file system in UBI")
120131

121132

122133
def get_files_from_cramfs(binbytes):
@@ -144,13 +155,12 @@ async def get_info_from_pak(pak: PAK):
144155
binbytes = await asyncio.to_thread(extract_fs, pak)
145156
if isinstance(binbytes, str):
146157
return {"error": binbytes, "sha256": ha}
147-
if is_cramfs(binbytes):
148-
func = get_files_from_cramfs
149-
elif is_ubi(binbytes):
150-
func = get_files_from_ubi
151-
elif is_squashfs(binbytes):
152-
func = get_files_from_squashfs
153-
else:
158+
func = {
159+
FileSystem.CRAMFS: get_files_from_cramfs,
160+
FileSystem.UBI: get_files_from_ubi,
161+
FileSystem.SQUASHFS: get_files_from_squashfs,
162+
}.get(FileSystem.from_magic(binbytes[:4]))
163+
if func is None:
154164
return {"error": "Unrecognized image type", "sha256": ha}
155165
files = await asyncio.to_thread(func, binbytes)
156166
info = get_info_from_files(files)

reolinkfw/extract.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,25 @@
1010
from ubireader.ubifs import ubifs
1111
from ubireader.ubifs.output import extract_files as extract_ubifs
1212

13-
from reolinkfw import FS_SECTIONS, ROOTFS_SECTIONS
14-
from reolinkfw.util import (
15-
DummyLEB,
16-
get_fs_from_ubi,
17-
is_cramfs,
18-
is_squashfs,
19-
is_ubi,
20-
is_ubifs
21-
)
13+
from reolinkfw import FS_SECTIONS, ROOTFS_SECTIONS, FileSystem
14+
from reolinkfw.util import DummyLEB, get_fs_from_ubi
2215

2316

2417
def extract_file_system(fs_bytes, dest: Path = None):
2518
dest = (Path.cwd() / "reolink_fs") if dest is None else dest
2619
dest.mkdir(parents=True, exist_ok=True)
27-
if is_ubi(fs_bytes):
20+
fs = FileSystem.from_magic(fs_bytes[:4])
21+
if fs == FileSystem.UBI:
2822
extract_file_system(get_fs_from_ubi(fs_bytes), dest)
29-
elif is_ubifs(fs_bytes):
23+
elif fs == FileSystem.UBIFS:
3024
with DummyLEB.from_bytes(fs_bytes) as leb:
3125
with redirect_stdout(StringIO()):
3226
# If files already exist they are not written again.
3327
extract_ubifs(ubifs(leb), dest)
34-
elif is_squashfs(fs_bytes):
28+
elif fs == FileSystem.SQUASHFS:
3529
with SquashFsImage.from_bytes(fs_bytes) as image:
3630
extract_squashfs(image.root, dest, True)
37-
elif is_cramfs(fs_bytes):
31+
elif fs == FileSystem.CRAMFS:
3832
with Cramfs.from_bytes(fs_bytes) as image:
3933
extract_cramfs(image.rootdir, dest, True)
4034
else:

reolinkfw/util.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44
from functools import partial
55

66
from pakler import PAK
7-
from pycramfs.const import MAGIC_BYTES as CRAMFS_MAGIC
8-
from PySquashfsImage.const import SQUASHFS_MAGIC
97
from ubireader.ubi import ubi
10-
from ubireader.ubi.defines import UBI_EC_HDR_MAGIC as UBI_MAGIC
118
from ubireader.ubi_io import ubi_file, leb_virtual_file
12-
from ubireader.ubifs.defines import UBIFS_NODE_MAGIC as UBIFS_MAGIC
139
from ubireader.utils import guess_peb_size
1410

1511
from reolinkfw.tmpfile import TempFile
1612

17-
SQUASHFS_MAGIC = SQUASHFS_MAGIC.to_bytes(4, "little")
18-
1913

2014
class DummyLEB:
2115
"""A class that emulates ubireader's `leb_virtual_file`."""
@@ -70,22 +64,6 @@ def get_fs_from_ubi(binbytes):
7064
return b''.join(leb_virtual_file(ubi_obj, vol_blocks).reader())
7165

7266

73-
def is_ubi(bytes_):
74-
return bytes_[:4] == UBI_MAGIC
75-
76-
77-
def is_squashfs(bytes_):
78-
return bytes_[:4] == SQUASHFS_MAGIC
79-
80-
81-
def is_cramfs(bytes_):
82-
return bytes_[:4] == CRAMFS_MAGIC
83-
84-
85-
def is_ubifs(bytes_):
86-
return bytes_[:4] == UBIFS_MAGIC
87-
88-
8967
def sha256_pak(pak: PAK) -> str:
9068
sha = hashlib.sha256()
9169
pak._fd.seek(0)

0 commit comments

Comments
 (0)