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