|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import PurePosixPath |
| 4 | +from stat import filemode |
| 5 | +from typing import Iterator, Literal, Optional |
| 6 | + |
| 7 | +from ubireader.ubifs import ubifs, walk |
| 8 | +from ubireader.ubifs.defines import ( |
| 9 | + UBIFS_COMMON_HDR_SZ, |
| 10 | + UBIFS_ITYPE_BLK, |
| 11 | + UBIFS_ITYPE_CHR, |
| 12 | + UBIFS_ITYPE_DIR, |
| 13 | + UBIFS_ITYPE_FIFO, |
| 14 | + UBIFS_ITYPE_LNK, |
| 15 | + UBIFS_ITYPE_REG, |
| 16 | + UBIFS_ITYPE_SOCK, |
| 17 | + UBIFS_NODE_MAGIC, |
| 18 | + UBIFS_ROOT_INO, |
| 19 | + UBIFS_SB_NODE, |
| 20 | + UBIFS_SB_NODE_SZ, |
| 21 | +) |
| 22 | +from ubireader.ubifs.nodes import common_hdr, ino_node, sb_node |
| 23 | +from ubireader.ubifs.output import _process_reg_file |
| 24 | +from ubireader.ubi_io import ubi_file |
| 25 | +from ubireader.utils import guess_leb_size |
| 26 | + |
| 27 | +from reolinkfw.tmpfile import TempFile |
| 28 | + |
| 29 | + |
| 30 | +class File: |
| 31 | + |
| 32 | + def __init__(self, image: UBIFS, nodes: dict, name: str = '', parent: Optional[Directory] = None) -> None: |
| 33 | + self._image = image |
| 34 | + self._nodes = nodes |
| 35 | + self._name = name |
| 36 | + self._parent = parent |
| 37 | + |
| 38 | + @property |
| 39 | + def name(self) -> str: |
| 40 | + return self._name |
| 41 | + |
| 42 | + @property |
| 43 | + def parent(self) -> Optional[Directory]: |
| 44 | + return self._parent |
| 45 | + |
| 46 | + @property |
| 47 | + def path(self) -> PurePosixPath: |
| 48 | + if self._parent is None: |
| 49 | + return PurePosixPath('/') |
| 50 | + return self._parent.path / self._name |
| 51 | + |
| 52 | + @property |
| 53 | + def inode(self) -> ino_node: |
| 54 | + return self._nodes["ino"] |
| 55 | + |
| 56 | + @property |
| 57 | + def mode(self) -> int: |
| 58 | + return self.inode.mode |
| 59 | + |
| 60 | + @property |
| 61 | + def filemode(self) -> str: |
| 62 | + return filemode(self.mode) |
| 63 | + |
| 64 | + @property |
| 65 | + def is_dir(self) -> bool: |
| 66 | + return False |
| 67 | + |
| 68 | + @property |
| 69 | + def is_file(self) -> bool: |
| 70 | + return False |
| 71 | + |
| 72 | + @property |
| 73 | + def is_symlink(self) -> bool: |
| 74 | + return False |
| 75 | + |
| 76 | + |
| 77 | +class DataFile(File): |
| 78 | + |
| 79 | + def read_bytes(self) -> bytes: |
| 80 | + ... |
| 81 | + |
| 82 | + def read_text(self, encoding: str = "utf8", errors: str = "strict") -> str: |
| 83 | + return self.read_bytes().decode(encoding, errors) |
| 84 | + |
| 85 | + |
| 86 | +class RegularFile(DataFile): |
| 87 | + |
| 88 | + @property |
| 89 | + def is_file(self) -> Literal[True]: |
| 90 | + return True |
| 91 | + |
| 92 | + def read_bytes(self) -> bytes: |
| 93 | + return _process_reg_file(self._image._ubifs, self._nodes, None) |
| 94 | + |
| 95 | + |
| 96 | +class Symlink(DataFile): |
| 97 | + |
| 98 | + @property |
| 99 | + def is_symlink(self) -> Literal[True]: |
| 100 | + return True |
| 101 | + |
| 102 | + def read_bytes(self) -> bytes: |
| 103 | + return self.inode.data |
| 104 | + |
| 105 | + def readlink(self) -> PurePosixPath: |
| 106 | + return PurePosixPath(self.read_text()) |
| 107 | + |
| 108 | + |
| 109 | +class Directory(File): |
| 110 | + |
| 111 | + def __init__(self, image: UBIFS, nodes: dict, name: str = '', parent: Optional[Directory] = None) -> None: |
| 112 | + super().__init__(image, nodes, name, parent) |
| 113 | + self._children = {} |
| 114 | + for dent in nodes.get("dent", []): |
| 115 | + cls = filetype[dent.type] |
| 116 | + self._children[dent.name] = cls(image, image.inodes[dent.inum], dent.name, self) |
| 117 | + |
| 118 | + def __iter__(self) -> Iterator[File]: |
| 119 | + yield from self._children.values() |
| 120 | + |
| 121 | + @property |
| 122 | + def is_dir(self) -> Literal[True]: |
| 123 | + return True |
| 124 | + |
| 125 | + @property |
| 126 | + def children(self) -> dict[str, File]: |
| 127 | + return self._children |
| 128 | + |
| 129 | + def select(self, path) -> Optional[File]: |
| 130 | + """Select a file of any kind by path. |
| 131 | +
|
| 132 | + The path can be absolute or relative. |
| 133 | + Special entries `'.'` and `'..'` are supported. |
| 134 | + """ |
| 135 | + path = PurePosixPath(path) |
| 136 | + if str(path) == "..": |
| 137 | + return self.parent if self.parent is not None else self |
| 138 | + if path.root == '/': |
| 139 | + if str(self.path) == '/': |
| 140 | + path = path.relative_to('/') |
| 141 | + else: |
| 142 | + return self._image.root.select(path) |
| 143 | + if str(path) == '.': |
| 144 | + return self |
| 145 | + child, *descendants = path.parts |
| 146 | + if (file := self._children.get(child)) is not None: |
| 147 | + if isinstance(file, Directory) and descendants: |
| 148 | + return file.select(PurePosixPath(*descendants)) |
| 149 | + elif not descendants: |
| 150 | + return file |
| 151 | + return None |
| 152 | + |
| 153 | + def riter(self) -> Iterator[File]: |
| 154 | + yield self |
| 155 | + for file in self: |
| 156 | + if isinstance(file, Directory): |
| 157 | + yield from file.riter() |
| 158 | + else: |
| 159 | + yield file |
| 160 | + |
| 161 | + |
| 162 | +class UBIFS: |
| 163 | + |
| 164 | + def __init__(self, ubifs: ubifs) -> None: |
| 165 | + self._ubifs = ubifs |
| 166 | + self._inodes = {} |
| 167 | + self._bad_blocks = [] |
| 168 | + walk.index(ubifs, ubifs.master_node.root_lnum, ubifs.master_node.root_offs, self._inodes, self._bad_blocks) |
| 169 | + self._root = Directory(self, self._inodes[UBIFS_ROOT_INO]) |
| 170 | + |
| 171 | + def __enter__(self) -> UBIFS: |
| 172 | + return self |
| 173 | + |
| 174 | + def __exit__(self, exc_type, exc_value, traceback) -> None: |
| 175 | + self.close() |
| 176 | + |
| 177 | + def __iter__(self) -> Iterator[File]: |
| 178 | + yield from self._root.riter() |
| 179 | + |
| 180 | + @property |
| 181 | + def inodes(self) -> dict: |
| 182 | + return self._inodes |
| 183 | + |
| 184 | + @property |
| 185 | + def root(self) -> Directory: |
| 186 | + return self._root |
| 187 | + |
| 188 | + def close(self) -> None: |
| 189 | + self._ubifs._file._fhandle.close() |
| 190 | + |
| 191 | + def select(self, path) -> Optional[File]: |
| 192 | + return self._root.select(path) |
| 193 | + |
| 194 | + @classmethod |
| 195 | + def from_file(cls, path) -> UBIFS: |
| 196 | + return cls(ubifs(ubi_file(path, guess_leb_size(path)))) |
| 197 | + |
| 198 | + @classmethod |
| 199 | + def from_bytes(cls, bytes_, offset: int = 0) -> UBIFS: |
| 200 | + chdr = common_hdr(bytes_[offset:offset+UBIFS_COMMON_HDR_SZ]) |
| 201 | + if chdr.magic != int.from_bytes(UBIFS_NODE_MAGIC, "little") or chdr.node_type != UBIFS_SB_NODE: |
| 202 | + raise Exception("Not UBIFS") |
| 203 | + sb_start = offset + UBIFS_COMMON_HDR_SZ |
| 204 | + sb_end = sb_start + UBIFS_SB_NODE_SZ |
| 205 | + sblk = sb_node(bytes_[sb_start:sb_end]) |
| 206 | + tmpfile = TempFile(bytes_[offset:]) |
| 207 | + tmpfile.open() # tmpfile's close() will not be called. |
| 208 | + return cls(ubifs(ubi_file(tmpfile, sblk.leb_size))) |
| 209 | + |
| 210 | + |
| 211 | +filetype = { |
| 212 | + UBIFS_ITYPE_REG: RegularFile, |
| 213 | + UBIFS_ITYPE_DIR: Directory, |
| 214 | + UBIFS_ITYPE_LNK: Symlink, |
| 215 | + UBIFS_ITYPE_BLK: File, |
| 216 | + UBIFS_ITYPE_CHR: File, |
| 217 | + UBIFS_ITYPE_FIFO: File, |
| 218 | + UBIFS_ITYPE_SOCK: File, |
| 219 | +} |
0 commit comments