File: vfs_lfs.py

package info (click to toggle)
giac 1.9.0.35%2Bdfsg2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,448 kB
  • sloc: cpp: 394,793; ansic: 200,527; python: 30,548; javascript: 28,735; makefile: 8,328; yacc: 2,690; lex: 2,458; sh: 623; perl: 314; lisp: 216; asm: 62; java: 41; xml: 36; sed: 16; csh: 7; pascal: 6
file content (107 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Test for VfsLittle using a RAM device

try:
    import uos
    uos.VfsLfs1
    uos.VfsLfs2
except (ImportError, AttributeError):
    print("SKIP")
    raise SystemExit

class RAMBlockDevice:
    ERASE_BLOCK_SIZE = 1024

    def __init__(self, blocks):
        self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)

    def readblocks(self, block, buf, off):
        addr = block * self.ERASE_BLOCK_SIZE + off
        for i in range(len(buf)):
            buf[i] = self.data[addr + i]

    def writeblocks(self, block, buf, off):
        addr = block * self.ERASE_BLOCK_SIZE + off
        for i in range(len(buf)):
            self.data[addr + i] = buf[i]

    def ioctl(self, op, arg):
        if op == 4: # block count
            return len(self.data) // self.ERASE_BLOCK_SIZE
        if op == 5: # block size
            return self.ERASE_BLOCK_SIZE
        if op == 6: # erase block
            return 0

def test(bdev, vfs_class):
    print('test', vfs_class)

    # mkfs
    vfs_class.mkfs(bdev)

    # construction
    vfs = vfs_class(bdev)

    # statvfs
    print(vfs.statvfs('/'))

    # open, write close
    f = vfs.open('test', 'w')
    f.write('littlefs')
    f.close()

    # statvfs after creating a file
    print(vfs.statvfs('/'))

    # ilistdir
    print(list(vfs.ilistdir()))
    print(list(vfs.ilistdir('/')))
    print(list(vfs.ilistdir(b'/')))

    # mkdir, rmdir
    vfs.mkdir('testdir')
    print(list(vfs.ilistdir()))
    print(list(vfs.ilistdir('testdir')))
    vfs.rmdir('testdir')
    print(list(vfs.ilistdir()))
    vfs.mkdir('testdir')

    # stat a file
    print(vfs.stat('test'))

    # stat a dir (size seems to vary on LFS2 so don't print that)
    print(vfs.stat('testdir')[:6])

    # read
    with vfs.open('test', 'r') as f:
        print(f.read())

    # create large file
    with vfs.open('testbig', 'w') as f:
        data = 'large012' * 32 * 16
        print('data length:', len(data))
        for i in range(4):
            print('write', i)
            f.write(data)

    # stat after creating large file
    print(vfs.statvfs('/'))

    # rename
    vfs.rename('testbig', 'testbig2')
    print(list(vfs.ilistdir()))

    # remove
    vfs.remove('testbig2')
    print(list(vfs.ilistdir()))

    # getcwd, chdir
    print(vfs.getcwd())
    vfs.chdir('/testdir')
    print(vfs.getcwd())
    vfs.chdir('/')
    print(vfs.getcwd())
    vfs.rmdir('testdir')

bdev = RAMBlockDevice(30)
test(bdev, uos.VfsLfs1)
test(bdev, uos.VfsLfs2)