circuitpython/tests/extmod/vfs_fat_more.py

123 lines
2.6 KiB
Python
Raw Normal View History

try:
import uos
except ImportError:
print("SKIP")
raise SystemExit
try:
uos.VfsFat
except 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):
2021-03-15 09:57:36 -04:00
# 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]
return 0
def writeblocks(self, n, buf):
2021-03-15 09:57:36 -04:00
# print("writeblocks(%s, %x)" % (n, id(buf)))
for i in range(len(buf)):
self.data[n * self.SEC_SIZE + i] = buf[i]
return 0
def ioctl(self, op, arg):
2021-03-15 09:57:36 -04:00
# 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:
2021-03-15 09:57:36 -04:00
uos.umount("/")
except OSError:
pass
2021-03-15 09:57:36 -04:00
for path in uos.listdir("/"):
uos.umount("/" + path)
uos.VfsFat.mkfs(bdev)
2021-03-15 09:57:36 -04:00
uos.mount(bdev, "/")
print(uos.getcwd())
2021-03-15 09:57:36 -04:00
f = open("test.txt", "w")
f.write("hello")
f.close()
print(uos.listdir())
2021-03-15 09:57:36 -04:00
print(uos.listdir("/"))
print(uos.stat("")[:-3])
print(uos.stat("/")[:-3])
print(uos.stat("test.txt")[:-3])
print(uos.stat("/test.txt")[:-3])
2021-03-15 09:57:36 -04:00
f = open("/test.txt")
print(f.read())
f.close()
2021-03-15 09:57:36 -04:00
uos.rename("test.txt", "test2.txt")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.rename("test2.txt", "/test3.txt")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.rename("/test3.txt", "test4.txt")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.rename("/test4.txt", "/test5.txt")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.mkdir("dir")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.mkdir("/dir2")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.mkdir("dir/subdir")
print(uos.listdir("dir"))
for exist in ("", "/", "dir", "/dir", "dir/subdir"):
try:
uos.mkdir(exist)
except OSError as er:
2021-03-15 09:57:36 -04:00
print("mkdir OSError", er.args[0] == 17) # EEXIST
2021-03-15 09:57:36 -04:00
uos.chdir("/")
print(uos.stat("test5.txt")[:-3])
uos.VfsFat.mkfs(bdev2)
2021-03-15 09:57:36 -04:00
uos.mount(bdev2, "/sys")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
print(uos.listdir("sys"))
print(uos.listdir("/sys"))
2021-03-15 09:57:36 -04:00
uos.rmdir("dir2")
uos.remove("test5.txt")
print(uos.listdir())
2021-03-15 09:57:36 -04:00
uos.umount("/")
print(uos.getcwd())
print(uos.listdir())
2021-03-15 09:57:36 -04:00
print(uos.listdir("sys"))
# test importing a file from a mounted FS
import sys
2021-03-15 09:57:36 -04:00
sys.path.clear()
2021-03-15 09:57:36 -04:00
sys.path.append("/sys")
with open("sys/test_module.py", "w") as f:
f.write('print("test_module!")')
import test_module