2018-06-04 07:25:06 +01:00
|
|
|
# test VFS functionality with a user-defined filesystem
|
|
|
|
# also tests parts of uio.IOBase implementation
|
|
|
|
|
2018-07-19 23:49:23 +01:00
|
|
|
import sys
|
2018-06-04 07:25:06 +01:00
|
|
|
|
|
|
|
try:
|
2018-07-19 23:49:23 +01:00
|
|
|
import uio
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
uio.IOBase
|
|
|
|
import uos
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01: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-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def read(self):
|
|
|
|
return self.data
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01: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-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def ioctl(self, req, arg):
|
2020-03-23 02:26:08 +00:00
|
|
|
print("ioctl", req, arg)
|
2018-06-04 07:25:06 +01:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class UserFS:
|
|
|
|
def __init__(self, files):
|
|
|
|
self.files = files
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def mount(self, readonly, mksfs):
|
|
|
|
pass
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def umount(self):
|
|
|
|
pass
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def stat(self, path):
|
2020-03-23 02:26:08 +00:00
|
|
|
print("stat", path)
|
2018-06-04 07:25:06 +01:00
|
|
|
if path in self.files:
|
|
|
|
return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
|
|
raise OSError
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2018-06-04 07:25:06 +01:00
|
|
|
def open(self, path, mode):
|
2020-03-23 02:26:08 +00:00
|
|
|
print("open", path, mode)
|
2018-06-04 07:25:06 +01:00
|
|
|
return UserFile(self.files[path])
|
|
|
|
|
|
|
|
|
|
|
|
# create and mount a user filesystem
|
|
|
|
user_files = {
|
2020-03-23 02:26:08 +00: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 07:25:06 +01:00
|
|
|
}
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.mount(UserFS(user_files), "/userfs")
|
2018-06-04 07:25:06 +01:00
|
|
|
|
|
|
|
# open and read a file
|
2020-03-23 02:26:08 +00:00
|
|
|
f = open("/userfs/data.txt")
|
2018-06-04 07:25:06 +01:00
|
|
|
print(f.read())
|
|
|
|
|
|
|
|
# import files from the user filesystem
|
2020-03-23 02:26:08 +00:00
|
|
|
sys.path.append("/userfs")
|
2018-06-04 07:25:06 +01:00
|
|
|
import usermod1
|
|
|
|
|
|
|
|
# unmount and undo path addition
|
2020-03-23 02:26:08 +00:00
|
|
|
uos.umount("/userfs")
|
2018-06-04 07:25:06 +01:00
|
|
|
sys.path.pop()
|