2017-03-10 06:13:58 +00:00
|
|
|
try:
|
2018-06-06 03:24:09 +00:00
|
|
|
import uos
|
2017-03-10 06:13:58 +00:00
|
|
|
except ImportError:
|
2017-05-13 11:13:24 +00:00
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2017-05-13 11:13:24 +00:00
|
|
|
|
2017-03-10 06:13:58 +00:00
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2017-03-10 06:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
|
|
|
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
2017-03-10 06:13:58 +00:00
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("writeblocks(%s, %x)" % (n, id(buf)))
|
2017-03-10 06:13:58 +00:00
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("ioctl(%d, %r)" % (op, arg))
|
2019-10-29 01:25:30 +00:00
|
|
|
if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
|
2017-03-10 06:13:58 +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
|
2017-03-10 06:13:58 +00:00
|
|
|
return self.SEC_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
bdev = RAMFS(50)
|
|
|
|
bdev2 = RAMFS(50)
|
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2017-03-10 06:13:58 +00:00
|
|
|
|
2017-05-09 05:50:40 +00:00
|
|
|
# first we umount any existing mount points the target may have
|
|
|
|
try:
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.umount("/")
|
2017-05-09 05:50:40 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
2020-03-23 02:26:08 +00:00
|
|
|
for path in uos.listdir("/"):
|
|
|
|
uos.umount("/" + path)
|
2017-05-09 05:50:40 +00:00
|
|
|
|
2017-03-10 06:13:58 +00:00
|
|
|
uos.VfsFat.mkfs(bdev)
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mount(bdev, "/")
|
2017-03-10 06:13:58 +00:00
|
|
|
|
|
|
|
print(uos.getcwd())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
f = open("test.txt", "w")
|
|
|
|
f.write("hello")
|
2017-03-10 06:13:58 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uos.listdir("/"))
|
|
|
|
print(uos.stat("")[:-3])
|
|
|
|
print(uos.stat("/")[:-3])
|
|
|
|
print(uos.stat("test.txt")[:-3])
|
|
|
|
print(uos.stat("/test.txt")[:-3])
|
2017-03-10 06:13:58 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
f = open("/test.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(f.read())
|
|
|
|
f.close()
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.rename("test.txt", "test2.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.rename("test2.txt", "/test3.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.rename("/test3.txt", "test4.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.rename("/test4.txt", "/test5.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mkdir("dir")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mkdir("/dir2")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mkdir("dir/subdir")
|
|
|
|
print(uos.listdir("dir"))
|
|
|
|
for exist in ("", "/", "dir", "/dir", "dir/subdir"):
|
2017-03-10 06:13:58 +00:00
|
|
|
try:
|
|
|
|
uos.mkdir(exist)
|
|
|
|
except OSError as er:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("mkdir OSError", er.args[0] == 17) # EEXIST
|
2017-03-10 06:13:58 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.chdir("/")
|
|
|
|
print(uos.stat("test5.txt")[:-3])
|
2017-03-10 06:13:58 +00:00
|
|
|
|
|
|
|
uos.VfsFat.mkfs(bdev2)
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mount(bdev2, "/sys")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uos.listdir("sys"))
|
|
|
|
print(uos.listdir("/sys"))
|
2017-03-10 06:13:58 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.rmdir("dir2")
|
|
|
|
uos.remove("test5.txt")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.listdir())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.umount("/")
|
2017-03-10 06:13:58 +00:00
|
|
|
print(uos.getcwd())
|
|
|
|
print(uos.listdir())
|
2020-03-23 02:26:08 +00:00
|
|
|
print(uos.listdir("sys"))
|
2018-06-06 03:22:51 +00:00
|
|
|
|
|
|
|
# test importing a file from a mounted FS
|
|
|
|
import sys
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-06 03:22:51 +00:00
|
|
|
sys.path.clear()
|
2020-03-23 02:26:08 +00:00
|
|
|
sys.path.append("/sys")
|
|
|
|
with open("sys/test_module.py", "w") as f:
|
2018-06-06 03:22:51 +00:00
|
|
|
f.write('print("test_module!")')
|
|
|
|
import test_module
|