circuitpython/tests/extmod/vfs_fat_ramdisk.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.2 KiB
Python
Raw Normal View History

try:
2023-08-22 11:15:46 -04:00
import errno
import os
except ImportError:
2017-02-14 17:56:22 -05:00
print("SKIP")
raise SystemExit
2017-02-14 17:56:22 -05:00
try:
2023-08-22 11:15:46 -04:00
os.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)
os.VfsFat.mkfs(bdev)
except MemoryError:
print("SKIP")
raise SystemExit
print(b"FOO_FILETXT" not in bdev.data)
print(b"hello!" not in bdev.data)
2023-08-22 11:15:46 -04:00
vfs = os.VfsFat(bdev)
os.mount(vfs, "/ramdisk")
2021-03-15 09:57:36 -04:00
vfs.label = "label test"
print("label:", vfs.label)
print("statvfs:", vfs.statvfs("/ramdisk"))
print("getcwd:", vfs.getcwd())
try:
vfs.stat("no_file.txt")
except OSError as e:
2023-08-22 11:15:46 -04:00
print(e.errno == errno.ENOENT)
with vfs.open("foo_file.txt", "w") as f:
f.write("hello!")
print(list(vfs.ilistdir()))
print("stat root:", vfs.stat("/")[:-3]) # timestamps differ across runs
2021-03-15 09:57:36 -04:00
print("stat file:", vfs.stat("foo_file.txt")[:-3]) # timestamps differ across runs
print(b"FOO_FILETXT" in bdev.data)
print(b"hello!" in bdev.data)
vfs.mkdir("foo_dir")
vfs.chdir("foo_dir")
print("getcwd:", vfs.getcwd())
print(list(vfs.ilistdir()))
with vfs.open("sub_file.txt", "w") as f:
f.write("subdir file")
try:
vfs.chdir("sub_file.txt")
except OSError as e:
2023-08-22 11:15:46 -04:00
print(e.errno == errno.ENOENT)
vfs.chdir("..")
print("getcwd:", vfs.getcwd())
2023-08-22 11:15:46 -04:00
os.umount(vfs)
2023-08-22 11:15:46 -04:00
vfs = os.VfsFat(bdev)
print(list(vfs.ilistdir(b"")))
# list a non-existent directory
try:
vfs.ilistdir(b"no_exist")
except OSError as e:
2023-08-22 11:15:46 -04:00
print("ENOENT:", e.errno == errno.ENOENT)