1# Test for VfsFat using a RAM device, mtime feature 2 3try: 4 import utime, uos 5 6 utime.time 7 utime.sleep 8 uos.VfsFat 9except (ImportError, AttributeError): 10 print("SKIP") 11 raise SystemExit 12 13 14class RAMBlockDevice: 15 ERASE_BLOCK_SIZE = 512 16 17 def __init__(self, blocks): 18 self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE) 19 20 def readblocks(self, block, buf): 21 addr = block * self.ERASE_BLOCK_SIZE 22 for i in range(len(buf)): 23 buf[i] = self.data[addr + i] 24 25 def writeblocks(self, block, buf): 26 addr = block * self.ERASE_BLOCK_SIZE 27 for i in range(len(buf)): 28 self.data[addr + i] = buf[i] 29 30 def ioctl(self, op, arg): 31 if op == 4: # block count 32 return len(self.data) // self.ERASE_BLOCK_SIZE 33 if op == 5: # block size 34 return self.ERASE_BLOCK_SIZE 35 36 37def test(bdev, vfs_class): 38 print("test", vfs_class) 39 40 # Initial format of block device. 41 vfs_class.mkfs(bdev) 42 43 # construction 44 vfs = vfs_class(bdev) 45 46 # Create an empty file, should have a timestamp. 47 current_time = int(utime.time()) 48 vfs.open("test1", "wt").close() 49 50 # Wait 2 seconds so mtime will increase (FAT has 2 second resolution). 51 utime.sleep(2) 52 53 # Create another empty file, should have a timestamp. 54 vfs.open("test2", "wt").close() 55 56 # Stat the files and check mtime is non-zero. 57 stat1 = vfs.stat("test1") 58 stat2 = vfs.stat("test2") 59 print(stat1[8] != 0, stat2[8] != 0) 60 61 # Check that test1 has mtime which matches time.time() at point of creation. 62 # TODO this currently fails on the unix port because FAT stores timestamps 63 # in localtime and stat() is UTC based. 64 # print(current_time - 1 <= stat1[8] <= current_time + 1) 65 66 # Check that test1 is older than test2. 67 print(stat1[8] < stat2[8]) 68 69 # Unmount. 70 vfs.umount() 71 72 73bdev = RAMBlockDevice(50) 74test(bdev, uos.VfsFat) 75