1# Test for VfsLittle using a RAM device 2 3try: 4 import uos 5 6 uos.VfsLfs1 7 uos.VfsLfs2 8except (ImportError, AttributeError): 9 print("SKIP") 10 raise SystemExit 11 12 13class RAMBlockDevice: 14 ERASE_BLOCK_SIZE = 1024 15 16 def __init__(self, blocks): 17 self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) 18 19 def readblocks(self, block, buf, off): 20 addr = block * self.ERASE_BLOCK_SIZE + off 21 for i in range(len(buf)): 22 buf[i] = self.data[addr + i] 23 24 def writeblocks(self, block, buf, off): 25 addr = block * self.ERASE_BLOCK_SIZE + off 26 for i in range(len(buf)): 27 self.data[addr + i] = buf[i] 28 29 def ioctl(self, op, arg): 30 if op == 4: # block count 31 return len(self.data) // self.ERASE_BLOCK_SIZE 32 if op == 5: # block size 33 return self.ERASE_BLOCK_SIZE 34 if op == 6: # erase block 35 return 0 36 37 38def print_stat(st, print_size=True): 39 # don't print times (just check that they have the correct type) 40 print(st[:6], st[6] if print_size else -1, type(st[7]), type(st[8]), type(st[9])) 41 42 43def test(bdev, vfs_class): 44 print("test", vfs_class) 45 46 # mkfs 47 vfs_class.mkfs(bdev) 48 49 # construction 50 vfs = vfs_class(bdev) 51 52 # statvfs 53 print(vfs.statvfs("/")) 54 55 # open, write close 56 f = vfs.open("test", "w") 57 f.write("littlefs") 58 f.close() 59 60 # statvfs after creating a file 61 print(vfs.statvfs("/")) 62 63 # ilistdir 64 print(list(vfs.ilistdir())) 65 print(list(vfs.ilistdir("/"))) 66 print(list(vfs.ilistdir(b"/"))) 67 68 # mkdir, rmdir 69 vfs.mkdir("testdir") 70 print(list(vfs.ilistdir())) 71 print(sorted(list(vfs.ilistdir("testdir")))) 72 vfs.rmdir("testdir") 73 print(list(vfs.ilistdir())) 74 vfs.mkdir("testdir") 75 76 # stat a file 77 print_stat(vfs.stat("test")) 78 79 # stat a dir (size seems to vary on LFS2 so don't print that) 80 print_stat(vfs.stat("testdir"), False) 81 82 # read 83 with vfs.open("test", "r") as f: 84 print(f.read()) 85 86 # create large file 87 with vfs.open("testbig", "w") as f: 88 data = "large012" * 32 * 16 89 print("data length:", len(data)) 90 for i in range(4): 91 print("write", i) 92 f.write(data) 93 94 # stat after creating large file 95 print(vfs.statvfs("/")) 96 97 # rename 98 vfs.rename("testbig", "testbig2") 99 print(sorted(list(vfs.ilistdir()))) 100 vfs.chdir("testdir") 101 vfs.rename("/testbig2", "testbig2") 102 print(sorted(list(vfs.ilistdir()))) 103 vfs.rename("testbig2", "/testbig2") 104 vfs.chdir("/") 105 print(sorted(list(vfs.ilistdir()))) 106 107 # remove 108 vfs.remove("testbig2") 109 print(sorted(list(vfs.ilistdir()))) 110 111 # getcwd, chdir 112 vfs.mkdir("/testdir2") 113 vfs.mkdir("/testdir/subdir") 114 print(vfs.getcwd()) 115 vfs.chdir("/testdir") 116 print(vfs.getcwd()) 117 118 # create file in directory to make sure paths are relative 119 vfs.open("test2", "w").close() 120 print_stat(vfs.stat("test2")) 121 print_stat(vfs.stat("/testdir/test2")) 122 vfs.remove("test2") 123 124 # chdir back to root and remove testdir 125 vfs.chdir("/") 126 print(vfs.getcwd()) 127 vfs.chdir("testdir") 128 print(vfs.getcwd()) 129 vfs.chdir("..") 130 print(vfs.getcwd()) 131 vfs.chdir("testdir/subdir") 132 print(vfs.getcwd()) 133 vfs.chdir("../..") 134 print(vfs.getcwd()) 135 vfs.chdir("/./testdir2") 136 print(vfs.getcwd()) 137 vfs.chdir("../testdir") 138 print(vfs.getcwd()) 139 vfs.chdir("../..") 140 print(vfs.getcwd()) 141 vfs.chdir(".//testdir") 142 print(vfs.getcwd()) 143 vfs.chdir("subdir/./") 144 print(vfs.getcwd()) 145 vfs.chdir("/") 146 print(vfs.getcwd()) 147 vfs.rmdir("testdir/subdir") 148 vfs.rmdir("testdir") 149 vfs.rmdir("testdir2") 150 151 152bdev = RAMBlockDevice(30) 153test(bdev, uos.VfsLfs1) 154test(bdev, uos.VfsLfs2) 155