2017-03-10 01:13:58 -05:00
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import os
|
2017-03-10 01:13:58 -05:00
|
|
|
except ImportError:
|
2017-05-13 07:13:24 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-05-13 07:13:24 -04:00
|
|
|
|
2017-03-10 01:13:58 -05:00
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
os.VfsFat
|
2017-03-10 01:13:58 -05:00
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-03-10 01:13:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
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)))
|
2017-03-10 01:13:58 -05:00
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
2018-06-14 21:02:12 -04:00
|
|
|
return 0
|
2017-03-10 01:13:58 -05:00
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("writeblocks(%s, %x)" % (n, id(buf)))
|
2017-03-10 01:13:58 -05:00
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
2018-06-14 21:02:12 -04:00
|
|
|
return 0
|
2017-03-10 01:13:58 -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
|
2017-03-10 01:13:58 -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
|
2017-03-10 01:13:58 -05:00
|
|
|
return self.SEC_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
bdev = RAMFS(50)
|
|
|
|
bdev2 = RAMFS(50)
|
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2017-05-09 01:50:40 -04:00
|
|
|
# first we umount any existing mount points the target may have
|
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
os.umount("/")
|
2017-05-09 01:50:40 -04:00
|
|
|
except OSError:
|
|
|
|
pass
|
2023-08-22 11:15:46 -04:00
|
|
|
for path in os.listdir("/"):
|
|
|
|
os.umount("/" + path)
|
2017-05-09 01:50:40 -04:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.VfsFat.mkfs(bdev)
|
|
|
|
os.mount(bdev, "/")
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
print(os.getcwd())
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
f = open("test.txt", "w")
|
|
|
|
f.write("hello")
|
2017-03-10 01:13:58 -05:00
|
|
|
f.close()
|
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
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])
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
f = open("/test.txt")
|
2017-03-10 01:13:58 -05:00
|
|
|
print(f.read())
|
|
|
|
f.close()
|
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
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"))
|
2021-03-15 09:57:36 -04:00
|
|
|
for exist in ("", "/", "dir", "/dir", "dir/subdir"):
|
2017-03-10 01:13:58 -05:00
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
os.mkdir(exist)
|
2017-03-10 01:13:58 -05:00
|
|
|
except OSError as er:
|
2021-04-22 05:32:21 -04:00
|
|
|
print("mkdir OSError", er.errno == 17) # EEXIST
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.chdir("/")
|
|
|
|
print(os.stat("test5.txt")[:-3])
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.VfsFat.mkfs(bdev2)
|
|
|
|
os.mount(bdev2, "/sys")
|
|
|
|
print(os.listdir())
|
|
|
|
print(os.listdir("sys"))
|
|
|
|
print(os.listdir("/sys"))
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.rmdir("dir2")
|
|
|
|
os.remove("test5.txt")
|
|
|
|
print(os.listdir())
|
2017-03-10 01:13:58 -05:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
os.umount("/")
|
|
|
|
print(os.getcwd())
|
|
|
|
print(os.listdir())
|
|
|
|
print(os.listdir("sys"))
|
2018-06-05 23:22:51 -04:00
|
|
|
|
|
|
|
# test importing a file from a mounted FS
|
|
|
|
import sys
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-06-05 23:22:51 -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:
|
2018-06-05 23:22:51 -04:00
|
|
|
f.write('print("test_module!")')
|
|
|
|
import test_module
|