2017-01-27 04:34:36 +00:00
|
|
|
try:
|
2017-02-14 22:56:22 +00:00
|
|
|
import uerrno
|
2018-06-06 04:24:09 +01:00
|
|
|
import uos
|
2017-01-27 04:34:36 +00:00
|
|
|
except ImportError:
|
2017-02-14 22:56:22 +00:00
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2017-02-14 22:56:22 +00:00
|
|
|
|
2016-02-14 17:02:34 +00:00
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
2016-02-14 18:52:38 +00:00
|
|
|
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
2016-02-14 17:02:34 +00:00
|
|
|
def __init__(self, blocks):
|
2016-02-14 18:52:38 +00:00
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
|
|
|
# print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
|
|
|
for i in range(len(buf)):
|
2016-02-14 18:52:38 +00:00
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
|
|
|
# print("writeblocks(%s, %x)" % (n, id(buf)))
|
|
|
|
for i in range(len(buf)):
|
2016-02-14 18:52:38 +00:00
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
2016-02-14 17:02:34 +00:00
|
|
|
|
2016-02-14 18:45:08 +00:00
|
|
|
def ioctl(self, op, arg):
|
|
|
|
# print("ioctl(%d, %r)" % (op, arg))
|
2019-10-29 01:25:30 +00:00
|
|
|
if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
|
2016-02-14 18:52:38 +00:00
|
|
|
return len(self.data) // self.SEC_SIZE
|
2019-10-29 01:25:30 +00:00
|
|
|
if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
|
2016-02-14 18:52:38 +00:00
|
|
|
return self.SEC_SIZE
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
|
2016-02-15 15:27:57 +00:00
|
|
|
try:
|
2017-01-24 09:55:05 +00:00
|
|
|
bdev = RAMFS(50)
|
2016-02-15 15:27:57 +00:00
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2016-02-15 15:27:57 +00:00
|
|
|
|
2016-02-14 17:02:34 +00:00
|
|
|
uos.VfsFat.mkfs(bdev)
|
|
|
|
|
2016-10-12 14:44:32 +01:00
|
|
|
print(b"FOO_FILETXT" not in bdev.data)
|
|
|
|
print(b"hello!" not in bdev.data)
|
2016-02-14 17:02:34 +00:00
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
vfs = uos.VfsFat(bdev)
|
|
|
|
uos.mount(vfs, "/ramdisk")
|
2016-02-28 18:30:59 +00:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
print("statvfs:", vfs.statvfs("/ramdisk"))
|
|
|
|
print("getcwd:", vfs.getcwd())
|
2016-10-07 10:19:04 +01:00
|
|
|
|
|
|
|
try:
|
2016-10-13 10:48:54 +01:00
|
|
|
vfs.stat("no_file.txt")
|
2016-10-07 10:19:04 +01:00
|
|
|
except OSError as e:
|
2021-04-22 10:32:21 +01:00
|
|
|
print(e.errno == uerrno.ENOENT)
|
2016-10-07 10:19:04 +01:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
with vfs.open("foo_file.txt", "w") as f:
|
|
|
|
f.write("hello!")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-02-28 22:06:27 +00:00
|
|
|
|
2020-08-31 06:01:10 +01:00
|
|
|
print("stat root:", vfs.stat("/")[:-3]) # timestamps differ across runs
|
2017-01-27 12:15:09 +00:00
|
|
|
print("stat file:", vfs.stat("foo_file.txt")[:-3]) # timestamps differ across runs
|
2016-02-28 23:24:05 +00:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
print(b"FOO_FILETXT" in bdev.data)
|
|
|
|
print(b"hello!" in bdev.data)
|
2016-05-29 16:54:14 +01:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
vfs.mkdir("foo_dir")
|
2016-05-29 16:54:14 +01:00
|
|
|
vfs.chdir("foo_dir")
|
|
|
|
print("getcwd:", vfs.getcwd())
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-05-29 16:54:14 +01:00
|
|
|
|
|
|
|
with vfs.open("sub_file.txt", "w") as f:
|
2016-10-13 10:48:54 +01:00
|
|
|
f.write("subdir file")
|
2016-05-29 16:54:14 +01:00
|
|
|
|
2016-10-07 10:19:04 +01:00
|
|
|
try:
|
|
|
|
vfs.chdir("sub_file.txt")
|
|
|
|
except OSError as e:
|
2021-04-22 10:32:21 +01:00
|
|
|
print(e.errno == uerrno.ENOENT)
|
2016-10-07 10:19:04 +01:00
|
|
|
|
2016-05-29 16:54:14 +01:00
|
|
|
vfs.chdir("..")
|
|
|
|
print("getcwd:", vfs.getcwd())
|
2016-08-26 14:22:53 +01:00
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
uos.umount(vfs)
|
2016-08-26 14:22:53 +01:00
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
vfs = uos.VfsFat(bdev)
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir(b"")))
|
2017-05-13 10:10:15 +01:00
|
|
|
|
|
|
|
# list a non-existent directory
|
|
|
|
try:
|
|
|
|
vfs.ilistdir(b"no_exist")
|
|
|
|
except OSError as e:
|
2021-04-22 10:32:21 +01:00
|
|
|
print("ENOENT:", e.errno == uerrno.ENOENT)
|