2021-01-27 21:09:44 -05:00
|
|
|
# Test for VfsLfs using a RAM device, when the first superblock does not exist
|
|
|
|
|
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import os
|
2021-01-27 21:09:44 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.VfsLfs2
|
2021-01-27 21:09:44 -05:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
class RAMBlockDevice:
|
|
|
|
def __init__(self, block_size, data):
|
|
|
|
self.block_size = block_size
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def readblocks(self, block, buf, off):
|
|
|
|
addr = block * self.block_size + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[addr + i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
|
|
|
if op == 4: # block count
|
|
|
|
return len(self.data) // self.block_size
|
|
|
|
if op == 5: # block size
|
|
|
|
return self.block_size
|
|
|
|
if op == 6: # erase block
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
# This is a valid littlefs2 filesystem with a block size of 64 bytes.
|
|
|
|
# The first block (where the first superblock is stored) is fully erased.
|
|
|
|
lfs2_data = b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02\x00\x00\x00\xf0\x0f\xff\xf7littlefs/\xe0\x00\x10\x00\x00\x02\x00@\x00\x00\x00\x04\x00\x00\x00\xff\x00\x00\x00\xff\xff\xff\x7f\xfe\x03\x00\x00p\x1f\xfc\x08\x1b\xb4\x14\xa7\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01\x00\x00\x00\xff\xef\xff\xf7test.txt \x00\x00\x08p\x1f\xfc\x08\x83\xf1u\xba\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
|
|
|
|
|
# Create the block device from the static data (it will be read-only).
|
|
|
|
bdev = RAMBlockDevice(64, lfs2_data)
|
|
|
|
|
|
|
|
# Create the VFS explicitly, no auto-detection is needed for this.
|
2023-08-22 11:15:46 -04:00
|
|
|
vfs = os.VfsLfs2(bdev)
|
2021-01-27 21:09:44 -05:00
|
|
|
print(list(vfs.ilistdir()))
|
|
|
|
|
|
|
|
# Mount the block device directly; this relies on auto-detection.
|
2023-08-22 11:15:46 -04:00
|
|
|
os.mount(bdev, "/userfs")
|
|
|
|
print(os.listdir("/userfs"))
|
2021-01-27 21:09:44 -05:00
|
|
|
|
|
|
|
# Clean up.
|
2023-08-22 11:15:46 -04:00
|
|
|
os.umount("/userfs")
|