1# Test for VfsLfs using a RAM device, mtime feature
2
3try:
4    import utime, uos
5
6    utime.time
7    utime.sleep
8    uos.VfsLfs2
9except (ImportError, AttributeError):
10    print("SKIP")
11    raise SystemExit
12
13
14class RAMBlockDevice:
15    ERASE_BLOCK_SIZE = 1024
16
17    def __init__(self, blocks):
18        self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
19
20    def readblocks(self, block, buf, off):
21        addr = block * self.ERASE_BLOCK_SIZE + off
22        for i in range(len(buf)):
23            buf[i] = self.data[addr + i]
24
25    def writeblocks(self, block, buf, off):
26        addr = block * self.ERASE_BLOCK_SIZE + off
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        if op == 6:  # erase block
36            return 0
37
38
39def test(bdev, vfs_class):
40    print("test", vfs_class)
41
42    # Initial format of block device.
43    vfs_class.mkfs(bdev)
44
45    # construction
46    print("mtime=True")
47    vfs = vfs_class(bdev, mtime=True)
48
49    # Create an empty file, should have a timestamp.
50    current_time = int(utime.time())
51    vfs.open("test1", "wt").close()
52
53    # Wait 1 second so mtime will increase by at least 1.
54    utime.sleep(1)
55
56    # Create another empty file, should have a timestamp.
57    vfs.open("test2", "wt").close()
58
59    # Stat the files and check mtime is non-zero.
60    stat1 = vfs.stat("test1")
61    stat2 = vfs.stat("test2")
62    print(stat1[8] != 0, stat2[8] != 0)
63
64    # Check that test1 has mtime which matches time.time() at point of creation.
65    print(current_time <= stat1[8] <= current_time + 1)
66
67    # Check that test1 is older than test2.
68    print(stat1[8] < stat2[8])
69
70    # Wait 1 second so mtime will increase by at least 1.
71    utime.sleep(1)
72
73    # Open test1 for reading and ensure mtime did not change.
74    vfs.open("test1", "rt").close()
75    print(vfs.stat("test1") == stat1)
76
77    # Open test1 for writing and ensure mtime increased from the previous value.
78    vfs.open("test1", "wt").close()
79    stat1_old = stat1
80    stat1 = vfs.stat("test1")
81    print(stat1_old[8] < stat1[8])
82
83    # Unmount.
84    vfs.umount()
85
86    # Check that remounting with mtime=False can read the timestamps.
87    print("mtime=False")
88    vfs = vfs_class(bdev, mtime=False)
89    print(vfs.stat("test1") == stat1)
90    print(vfs.stat("test2") == stat2)
91    f = vfs.open("test1", "wt")
92    f.close()
93    print(vfs.stat("test1") == stat1)
94    vfs.umount()
95
96    # Check that remounting with mtime=True still has the timestamps.
97    print("mtime=True")
98    vfs = vfs_class(bdev, mtime=True)
99    print(vfs.stat("test1") == stat1)
100    print(vfs.stat("test2") == stat2)
101    vfs.umount()
102
103
104bdev = RAMBlockDevice(30)
105test(bdev, uos.VfsLfs2)
106