1# Test for VfsLfs using a RAM device, when the first superblock does not exist
2
3try:
4    import uos
5
6    uos.VfsLfs2
7except (ImportError, AttributeError):
8    print("SKIP")
9    raise SystemExit
10
11
12class RAMBlockDevice:
13    def __init__(self, block_size, data):
14        self.block_size = block_size
15        self.data = data
16
17    def readblocks(self, block, buf, off):
18        addr = block * self.block_size + off
19        for i in range(len(buf)):
20            buf[i] = self.data[addr + i]
21
22    def ioctl(self, op, arg):
23        if op == 4:  # block count
24            return len(self.data) // self.block_size
25        if op == 5:  # block size
26            return self.block_size
27        if op == 6:  # erase block
28            return 0
29
30
31# This is a valid littlefs2 filesystem with a block size of 64 bytes.
32# The first block (where the first superblock is stored) is fully erased.
33lfs2_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"
34
35# Create the block device from the static data (it will be read-only).
36bdev = RAMBlockDevice(64, lfs2_data)
37
38# Create the VFS explicitly, no auto-detection is needed for this.
39vfs = uos.VfsLfs2(bdev)
40print(list(vfs.ilistdir()))
41
42# Mount the block device directly; this relies on auto-detection.
43uos.mount(bdev, "/userfs")
44print(uos.listdir("/userfs"))
45
46# Clean up.
47uos.umount("/userfs")
48