Files
t-deckard/deck/fs.py
T

102 lines
2.1 KiB
Python
Raw Normal View History

2025-12-22 09:05:35 +02:00
# deck.fs: FS interaction helpers (WIP)
# Created by Luxferre in 2025, released into public domain
2025-12-22 15:16:27 +02:00
import os
2025-12-22 09:05:35 +02:00
MCU_STORAGE = False
try:
2025-12-22 23:54:41 +02:00
import storage, sdcardio, board
2025-12-22 09:05:35 +02:00
MCU_STORAGE = True
except:
pass
def unlock_rootfs():
if MCU_STORAGE:
storage.remount("/", readonly=False)
def lock_rootfs():
if MCU_STORAGE:
storage.remount("/", readonly=True)
2025-12-22 15:16:27 +02:00
2025-12-22 23:54:41 +02:00
def mount_sd(path='/sd', readonly=False):
sd_ok = True
if MCU_STORAGE:
try:
sdcard = sdcardio.SDCard(board.SPI(), board.SDCARD_CS)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, path, readonly=readonly)
sd_ok = True
except:
sd_ok = False
return sd_ok
def umount_sd(path='/sd'):
sd_ok = True
if MCU_STORAGE:
try:
storage.umount(path)
sd_ok = True
except:
sd_ok = False
return sd_ok
2025-12-22 16:06:01 +02:00
# TODO: switch to buffered copying to avoid RAM overflow
def copy(p1, p2):
with open(p1, 'rb') as f1:
content = f1.read()
with open(p2, 'wb') as f2:
f2.write(content)
cp = copy # shortcut export
2025-12-22 15:16:27 +02:00
# Some shortcut export bindings to corresponding os functions
sep = pathsep = os.sep
cwd = pwd = os.getcwd
ls = listdir = os.listdir
md = mkdir = os.mkdir
cd = chdir = os.chdir
rd = rmdir = os.rmdir
rm = remove = os.remove
mv = rename = move = os.rename
try:
sync = os.sync
except:
def sync(): pass
env = getenv = os.getenv
rnd = urandom = os.urandom
# adapted statistics methods
# for a file
def stat(path):
st_mode, _, _, _, _, _, \
st_size, st_atime, st_mtime, st_ctime = os.stat(path)
return {
'mode': st_mode,
'size': st_size,
'atime': st_atime,
'mtime': st_mtime,
'ctime': st_ctime
}
# for a filesystem
def statvfs(path):
f_bsize, f_frsize, f_frags, f_bfree, f_bavail, f_files, \
f_ffree, f_favail, f_flag, f_namemax = os.statvfs(path)
return {
'block_size': f_bsize,
'fragment_size': f_frsize,
'fragments': f_frags,
'blocks_free': f_bfree,
'blocks_avail': f_bavail,
'inodes': f_files,
'inodes_free': f_ffree,
'inodes_avail': f_favail,
'mount_flags': f_flag,
'max_name_len': f_namemax
}
du = stat # shortcut export
df = statvfs # shortcut export