2021-05-13 10:33:52 +00:00
|
|
|
try:
|
|
|
|
from trezorio import fatfs, sdcard
|
|
|
|
|
|
|
|
HAVE_SDCARD = True
|
2022-03-01 12:55:58 +00:00
|
|
|
is_present = sdcard.is_present # type: ignore [obscured-by-same-name]
|
|
|
|
capacity = sdcard.capacity # type: ignore [obscured-by-same-name]
|
2021-12-08 09:10:58 +00:00
|
|
|
|
2021-05-13 10:33:52 +00:00
|
|
|
except Exception:
|
|
|
|
HAVE_SDCARD = False
|
2020-02-17 14:44:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
def is_present() -> bool:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def capacity() -> int:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-18 09:48:50 +00:00
|
|
|
from typing import Any, Callable, TypeVar
|
2023-08-15 15:56:09 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
from typing_extensions import ParamSpec
|
2020-02-26 18:25:22 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
P = ParamSpec("P")
|
|
|
|
R = TypeVar("R")
|
2020-02-17 14:44:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FilesystemWrapper:
|
2021-03-18 09:48:50 +00:00
|
|
|
_INSTANCE: "FilesystemWrapper" | None = None
|
2020-02-17 14:44:58 +00:00
|
|
|
|
2020-02-21 14:53:16 +00:00
|
|
|
def __init__(self, mounted: bool) -> None:
|
2021-05-13 10:33:52 +00:00
|
|
|
if not HAVE_SDCARD:
|
|
|
|
raise RuntimeError
|
|
|
|
|
2020-02-17 14:44:58 +00:00
|
|
|
self.mounted = mounted
|
|
|
|
self.counter = 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, mounted: bool = True) -> "FilesystemWrapper":
|
|
|
|
if cls._INSTANCE is None:
|
|
|
|
cls._INSTANCE = cls(mounted=mounted)
|
|
|
|
if cls._INSTANCE.mounted is not mounted:
|
|
|
|
raise RuntimeError # cannot request mounted and non-mounted instance at the same time
|
|
|
|
return cls._INSTANCE
|
|
|
|
|
2020-02-21 14:53:16 +00:00
|
|
|
def _deinit_instance(self) -> None:
|
2020-02-26 18:25:22 +00:00
|
|
|
fatfs.unmount()
|
2020-02-21 14:53:16 +00:00
|
|
|
sdcard.power_off()
|
|
|
|
FilesystemWrapper._INSTANCE = None
|
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
def __enter__(self) -> None:
|
2020-02-21 14:53:16 +00:00
|
|
|
try:
|
|
|
|
if self.counter <= 0:
|
|
|
|
self.counter = 0
|
|
|
|
sdcard.power_on()
|
|
|
|
if self.mounted:
|
2020-02-26 18:25:22 +00:00
|
|
|
fatfs.mount()
|
2020-02-21 14:53:16 +00:00
|
|
|
self.counter += 1
|
|
|
|
except Exception:
|
|
|
|
self._deinit_instance()
|
|
|
|
raise
|
2020-02-17 14:44:58 +00:00
|
|
|
|
|
|
|
def __exit__(self, exc_type: Any, exc_val: Any, tb: Any) -> None:
|
|
|
|
self.counter -= 1
|
|
|
|
if self.counter <= 0:
|
|
|
|
self.counter = 0
|
2020-02-21 14:53:16 +00:00
|
|
|
self._deinit_instance()
|
2020-02-17 14:44:58 +00:00
|
|
|
|
|
|
|
|
2020-02-26 18:25:22 +00:00
|
|
|
def filesystem(mounted: bool = True) -> FilesystemWrapper:
|
2020-02-17 14:44:58 +00:00
|
|
|
return FilesystemWrapper.get_instance(mounted=mounted)
|
|
|
|
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
def with_filesystem(func: Callable[P, R]) -> Callable[P, R]:
|
|
|
|
def wrapped_func(*args: P.args, **kwargs: P.kwargs) -> R:
|
2020-02-26 18:25:22 +00:00
|
|
|
with filesystem():
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
return wrapped_func
|