extmod/vfs_reader: Add file ioctl to set read buffer size.

Can be used to speed up importing a file from a vfs based filesystem.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This commit is contained in:
Andrew Leech
2023-10-31 15:14:05 +11:00
committed by Damien George
parent dff293840e
commit 4cf741062b
4 changed files with 69 additions and 16 deletions

View File

@@ -16,6 +16,8 @@ except (ImportError, AttributeError):
class UserFile(io.IOBase):
buffer_size = 16
def __init__(self, mode, data):
assert isinstance(data, bytes)
self.is_text = mode.find("b") == -1
@@ -39,7 +41,11 @@ class UserFile(io.IOBase):
def ioctl(self, req, arg):
print("ioctl", req, arg)
return 0
if req == 4: # MP_STREAM_CLOSE
return 0
if req == 11: # MP_STREAM_GET_BUFFER_SIZE
return UserFile.buffer_size
return -1
class UserFS:
@@ -70,6 +76,8 @@ user_files = {
"/usermod2.py": b"print('in usermod2')",
"/usermod3.py": b"syntax error",
"/usermod4.mpy": b"syntax error",
"/usermod5.py": b"print('in usermod5')",
"/usermod6.py": b"print('in usermod6')",
}
os.mount(UserFS(user_files), "/userfs")
@@ -93,6 +101,14 @@ try:
except ValueError:
print("ValueError in usermod4")
# Test an import with largest buffer size
UserFile.buffer_size = 255
import usermod5
# Test an import with over-size buffer size (should be safely limited internally)
UserFile.buffer_size = 1024
import usermod6
# unmount and undo path addition
os.umount("/userfs")
sys.path.pop()

View File

@@ -3,21 +3,37 @@ some data in a text file
stat /usermod1
stat /usermod1.py
open /usermod1.py rb
ioctl 11 0
ioctl 4 0
in usermod1
stat /usermod2
stat /usermod2.py
open /usermod2.py rb
ioctl 11 0
ioctl 4 0
in usermod2
stat /usermod3
stat /usermod3.py
open /usermod3.py rb
ioctl 11 0
ioctl 4 0
SyntaxError in usermod3
stat /usermod4
stat /usermod4.py
stat /usermod4.mpy
open /usermod4.mpy rb
ioctl 11 0
ioctl 4 0
ValueError in usermod4
stat /usermod5
stat /usermod5.py
open /usermod5.py rb
ioctl 11 0
ioctl 4 0
in usermod5
stat /usermod6
stat /usermod6.py
open /usermod6.py rb
ioctl 11 0
ioctl 4 0
in usermod6