summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/vfs_fat_more.py
blob: 1d755f9c55c0bb8b54f75b55d74f0271a24b97e1 (plain) (blame)
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
108
109
110
111
112
113
114
115
try:
    import os, vfs

    vfs.VfsFat
except (ImportError, AttributeError):
    print("SKIP")
    raise SystemExit


class RAMFS:
    SEC_SIZE = 512

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

    def readblocks(self, n, buf):
        # print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
        for i in range(len(buf)):
            buf[i] = self.data[n * self.SEC_SIZE + i]

    def writeblocks(self, n, buf):
        # print("writeblocks(%s, %x)" % (n, id(buf)))
        for i in range(len(buf)):
            self.data[n * self.SEC_SIZE + i] = buf[i]

    def ioctl(self, op, arg):
        # print("ioctl(%d, %r)" % (op, arg))
        if op == 4:  # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
            return len(self.data) // self.SEC_SIZE
        if op == 5:  # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
            return self.SEC_SIZE


try:
    bdev = RAMFS(50)
    bdev2 = RAMFS(50)
except MemoryError:
    print("SKIP")
    raise SystemExit

# first we umount any existing mount points the target may have
try:
    vfs.umount("/")
except OSError:
    pass
for path in os.listdir("/"):
    vfs.umount("/" + path)

vfs.VfsFat.mkfs(bdev)
vfs.mount(bdev, "/")

print(os.getcwd())

f = open("test.txt", "w")
f.write("hello")
f.close()

print(os.listdir())
print(os.listdir("/"))
print(os.stat("")[:-3])
print(os.stat("/")[:-3])
print(os.stat("test.txt")[:-3])
print(os.stat("/test.txt")[:-3])

f = open("/test.txt")
print(f.read())
f.close()

os.rename("test.txt", "test2.txt")
print(os.listdir())
os.rename("test2.txt", "/test3.txt")
print(os.listdir())
os.rename("/test3.txt", "test4.txt")
print(os.listdir())
os.rename("/test4.txt", "/test5.txt")
print(os.listdir())

os.mkdir("dir")
print(os.listdir())
os.mkdir("/dir2")
print(os.listdir())
os.mkdir("dir/subdir")
print(os.listdir("dir"))
for exist in ("", "/", "dir", "/dir", "dir/subdir"):
    try:
        os.mkdir(exist)
    except OSError as er:
        print("mkdir OSError", er.errno == 17)  # EEXIST

os.chdir("/")
print(os.stat("test5.txt")[:-3])

vfs.VfsFat.mkfs(bdev2)
vfs.mount(bdev2, "/sys")
print(os.listdir())
print(os.listdir("sys"))
print(os.listdir("/sys"))

os.rmdir("dir2")
os.remove("test5.txt")
print(os.listdir())

vfs.umount("/")
print(os.getcwd())
print(os.listdir())
print(os.listdir("sys"))

# test importing a file from a mounted FS
import sys

sys.path.clear()
sys.path.append("/sys")
with open("sys/test_module.py", "w") as f:
    f.write('print("test_module!")')
import test_module