2018-06-04 02:25:06 -04:00
|
|
|
# test VFS functionality with a user-defined filesystem
|
|
|
|
# also tests parts of uio.IOBase implementation
|
|
|
|
|
2018-07-19 18:49:23 -04:00
|
|
|
import sys
|
2018-06-04 02:25:06 -04:00
|
|
|
|
|
|
|
try:
|
2018-07-19 18:49:23 -04:00
|
|
|
import uio
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
uio.IOBase
|
|
|
|
import uos
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
uos.mount
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
class UserFile(uio.IOBase):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
self.pos = 0
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def read(self):
|
|
|
|
return self.data
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def readinto(self, buf):
|
|
|
|
n = 0
|
|
|
|
while n < len(buf) and self.pos < len(self.data):
|
|
|
|
buf[n] = self.data[self.pos]
|
|
|
|
n += 1
|
|
|
|
self.pos += 1
|
|
|
|
return n
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def ioctl(self, req, arg):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("ioctl", req, arg)
|
2018-06-04 02:25:06 -04:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class UserFS:
|
|
|
|
def __init__(self, files):
|
|
|
|
self.files = files
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def mount(self, readonly, mksfs):
|
|
|
|
pass
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def umount(self):
|
|
|
|
pass
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def stat(self, path):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("stat", path)
|
2018-06-04 02:25:06 -04:00
|
|
|
if path in self.files:
|
|
|
|
return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
|
|
raise OSError
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-06-04 02:25:06 -04:00
|
|
|
def open(self, path, mode):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("open", path, mode)
|
2018-06-04 02:25:06 -04:00
|
|
|
return UserFile(self.files[path])
|
|
|
|
|
|
|
|
|
|
|
|
# create and mount a user filesystem
|
|
|
|
user_files = {
|
2020-03-22 22:26:08 -04:00
|
|
|
"/data.txt": b"some data in a text file\n",
|
|
|
|
"/usermod1.py": b"print('in usermod1')\nimport usermod2",
|
|
|
|
"/usermod2.py": b"print('in usermod2')",
|
2018-06-04 02:25:06 -04:00
|
|
|
}
|
2020-03-22 22:26:08 -04:00
|
|
|
uos.mount(UserFS(user_files), "/userfs")
|
2018-06-04 02:25:06 -04:00
|
|
|
|
|
|
|
# open and read a file
|
2020-03-22 22:26:08 -04:00
|
|
|
f = open("/userfs/data.txt")
|
2018-06-04 02:25:06 -04:00
|
|
|
print(f.read())
|
|
|
|
|
|
|
|
# import files from the user filesystem
|
2020-03-22 22:26:08 -04:00
|
|
|
sys.path.append("/userfs")
|
2018-06-04 02:25:06 -04:00
|
|
|
import usermod1
|
|
|
|
|
|
|
|
# unmount and undo path addition
|
2020-03-22 22:26:08 -04:00
|
|
|
uos.umount("/userfs")
|
2018-06-04 02:25:06 -04:00
|
|
|
sys.path.pop()
|