2019-10-27 09:15:29 +00:00
|
|
|
# Test for behaviour of combined standard and extended block device
|
|
|
|
|
|
|
|
try:
|
|
|
|
import uos
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-27 09:15:29 +00:00
|
|
|
uos.VfsFat
|
|
|
|
uos.VfsLfs2
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-27 09:15:29 +00:00
|
|
|
class RAMBlockDevice:
|
|
|
|
ERASE_BLOCK_SIZE = 512
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
|
|
|
|
|
|
|
|
def readblocks(self, block, buf, off=0):
|
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[addr + i]
|
|
|
|
|
|
|
|
def writeblocks(self, block, buf, off=None):
|
|
|
|
if off is None:
|
|
|
|
# erase, then write
|
|
|
|
off = 0
|
|
|
|
addr = block * self.ERASE_BLOCK_SIZE + off
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[addr + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2020-03-23 02:26:08 +00:00
|
|
|
if op == 4: # block count
|
2019-10-27 09:15:29 +00:00
|
|
|
return len(self.data) // self.ERASE_BLOCK_SIZE
|
2020-03-23 02:26:08 +00:00
|
|
|
if op == 5: # block size
|
2019-10-27 09:15:29 +00:00
|
|
|
return self.ERASE_BLOCK_SIZE
|
2020-03-23 02:26:08 +00:00
|
|
|
if op == 6: # erase block
|
2019-10-27 09:15:29 +00:00
|
|
|
return 0
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-27 09:15:29 +00:00
|
|
|
def test(bdev, vfs_class):
|
2020-03-23 02:26:08 +00:00
|
|
|
print("test", vfs_class)
|
2019-10-27 09:15:29 +00:00
|
|
|
|
|
|
|
# mkfs
|
|
|
|
vfs_class.mkfs(bdev)
|
|
|
|
|
|
|
|
# construction
|
|
|
|
vfs = vfs_class(bdev)
|
|
|
|
|
|
|
|
# statvfs
|
2020-03-23 02:26:08 +00:00
|
|
|
print(vfs.statvfs("/"))
|
2019-10-27 09:15:29 +00:00
|
|
|
|
|
|
|
# open, write close
|
2020-03-23 02:26:08 +00:00
|
|
|
f = vfs.open("test", "w")
|
2019-10-27 09:15:29 +00:00
|
|
|
for i in range(10):
|
2020-03-23 02:26:08 +00:00
|
|
|
f.write("some data")
|
2019-10-27 09:15:29 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
# ilistdir
|
|
|
|
print(list(vfs.ilistdir()))
|
|
|
|
|
|
|
|
# read
|
2020-03-23 02:26:08 +00:00
|
|
|
with vfs.open("test", "r") as f:
|
2019-10-27 09:15:29 +00:00
|
|
|
print(f.read())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2019-10-27 09:15:29 +00:00
|
|
|
try:
|
|
|
|
bdev = RAMBlockDevice(50)
|
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
test(bdev, uos.VfsFat)
|
|
|
|
test(bdev, uos.VfsLfs2)
|