2019-10-18 07:25:48 +01:00
|
|
|
# Test for VfsLittle using a RAM device, with mount/umount
|
|
|
|
|
|
|
|
try:
|
2022-08-18 07:57:45 +01:00
|
|
|
import os
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2022-08-18 07:57:45 +01:00
|
|
|
os.VfsLfs1
|
|
|
|
os.VfsLfs2
|
2019-10-18 07:25:48 +01:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
class RAMBlockDevice:
|
|
|
|
ERASE_BLOCK_SIZE = 1024
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
|
|
|
|
|
2020-11-27 10:05:39 +00:00
|
|
|
def readblocks(self, block, buf, off=0):
|
2019-10-18 07:25:48 +01:00
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[addr + i]
|
|
|
|
|
2020-11-27 10:05:39 +00:00
|
|
|
def writeblocks(self, block, buf, off=0):
|
2019-10-18 07:25:48 +01:00
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[addr + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
|
|
|
if op == 4: # block count
|
|
|
|
return len(self.data) // self.ERASE_BLOCK_SIZE
|
|
|
|
if op == 5: # block size
|
|
|
|
return self.ERASE_BLOCK_SIZE
|
|
|
|
if op == 6: # erase block
|
|
|
|
return 0
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2020-11-27 10:05:39 +00:00
|
|
|
def test(vfs_class):
|
2019-10-18 07:25:48 +01:00
|
|
|
print("test", vfs_class)
|
|
|
|
|
2020-11-27 10:05:39 +00:00
|
|
|
bdev = RAMBlockDevice(30)
|
|
|
|
|
|
|
|
# mount bdev unformatted
|
|
|
|
try:
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mount(bdev, "/lfs")
|
2020-11-27 10:05:39 +00:00
|
|
|
except Exception as er:
|
|
|
|
print(repr(er))
|
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
# mkfs
|
|
|
|
vfs_class.mkfs(bdev)
|
|
|
|
|
|
|
|
# construction
|
|
|
|
vfs = vfs_class(bdev)
|
|
|
|
|
|
|
|
# mount
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mount(vfs, "/lfs")
|
2019-10-18 07:25:48 +01:00
|
|
|
|
|
|
|
# import
|
|
|
|
with open("/lfs/lfsmod.py", "w") as f:
|
|
|
|
f.write('print("hello from lfs")\n')
|
|
|
|
import lfsmod
|
|
|
|
|
|
|
|
# import package
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mkdir("/lfs/lfspkg")
|
2019-10-18 07:25:48 +01:00
|
|
|
with open("/lfs/lfspkg/__init__.py", "w") as f:
|
|
|
|
f.write('print("package")\n')
|
|
|
|
import lfspkg
|
|
|
|
|
2020-05-14 12:37:59 +01:00
|
|
|
# chdir and import module from current directory (needs "" in sys.path)
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mkdir("/lfs/subdir")
|
|
|
|
os.chdir("/lfs/subdir")
|
|
|
|
os.rename("/lfs/lfsmod.py", "/lfs/subdir/lfsmod2.py")
|
2020-05-14 12:37:59 +01:00
|
|
|
import lfsmod2
|
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
# umount
|
2022-08-18 07:57:45 +01:00
|
|
|
os.umount("/lfs")
|
2019-10-18 07:25:48 +01:00
|
|
|
|
2020-10-29 00:31:53 +00:00
|
|
|
# mount read-only
|
|
|
|
vfs = vfs_class(bdev)
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mount(vfs, "/lfs", readonly=True)
|
2020-10-29 00:31:53 +00:00
|
|
|
|
|
|
|
# test reading works
|
|
|
|
with open("/lfs/subdir/lfsmod2.py") as f:
|
|
|
|
print("lfsmod2.py:", f.read())
|
|
|
|
|
|
|
|
# test writing fails
|
|
|
|
try:
|
|
|
|
open("/lfs/test_write", "w")
|
|
|
|
except OSError as er:
|
|
|
|
print(repr(er))
|
|
|
|
|
|
|
|
# umount
|
2022-08-18 07:57:45 +01:00
|
|
|
os.umount("/lfs")
|
2020-10-29 00:31:53 +00:00
|
|
|
|
2020-11-27 10:05:39 +00:00
|
|
|
# mount bdev again
|
2022-08-18 07:57:45 +01:00
|
|
|
os.mount(bdev, "/lfs")
|
2020-11-27 10:05:39 +00:00
|
|
|
|
|
|
|
# umount
|
2022-08-18 07:57:45 +01:00
|
|
|
os.umount("/lfs")
|
2020-11-27 10:05:39 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
# clear imported modules
|
2022-08-18 07:57:45 +01:00
|
|
|
sys.modules.clear()
|
2019-10-18 07:25:48 +01:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-18 07:25:48 +01:00
|
|
|
# initialise path
|
2022-08-18 07:57:45 +01:00
|
|
|
import sys
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2022-08-18 07:57:45 +01:00
|
|
|
sys.path.clear()
|
|
|
|
sys.path.append("/lfs")
|
|
|
|
sys.path.append("")
|
2019-10-18 07:25:48 +01:00
|
|
|
|
|
|
|
# run tests
|
2022-08-18 07:57:45 +01:00
|
|
|
test(os.VfsLfs1)
|
|
|
|
test(os.VfsLfs2)
|