2019-10-18 02:25:48 -04:00
|
|
|
# Test for VfsLittle using a RAM device
|
|
|
|
|
|
|
|
try:
|
|
|
|
import uos
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
uos.VfsLfs1
|
|
|
|
uos.VfsLfs2
|
|
|
|
except (ImportError, AttributeError):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
class RAMBlockDevice:
|
|
|
|
ERASE_BLOCK_SIZE = 1024
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
|
|
|
|
|
|
|
|
def readblocks(self, block, buf, off):
|
|
|
|
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):
|
|
|
|
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-22 22:26:08 -04:00
|
|
|
if op == 4: # block count
|
2019-10-18 02:25:48 -04:00
|
|
|
return len(self.data) // self.ERASE_BLOCK_SIZE
|
2020-03-22 22:26:08 -04:00
|
|
|
if op == 5: # block size
|
2019-10-18 02:25:48 -04:00
|
|
|
return self.ERASE_BLOCK_SIZE
|
2020-03-22 22:26:08 -04:00
|
|
|
if op == 6: # erase block
|
2019-10-18 02:25:48 -04:00
|
|
|
return 0
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
def test(bdev, vfs_class):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("test", vfs_class)
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# mkfs
|
|
|
|
vfs_class.mkfs(bdev)
|
|
|
|
|
|
|
|
# construction
|
|
|
|
vfs = vfs_class(bdev)
|
|
|
|
|
|
|
|
# statvfs
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.statvfs("/"))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# open, write close
|
2020-03-22 22:26:08 -04:00
|
|
|
f = vfs.open("test", "w")
|
|
|
|
f.write("littlefs")
|
2019-10-18 02:25:48 -04:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
# statvfs after creating a file
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.statvfs("/"))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# ilistdir
|
|
|
|
print(list(vfs.ilistdir()))
|
2020-03-22 22:26:08 -04:00
|
|
|
print(list(vfs.ilistdir("/")))
|
|
|
|
print(list(vfs.ilistdir(b"/")))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# mkdir, rmdir
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.mkdir("testdir")
|
2019-10-18 02:25:48 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2020-05-04 09:34:12 -04:00
|
|
|
print(sorted(list(vfs.ilistdir("testdir"))))
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.rmdir("testdir")
|
2019-10-18 02:25:48 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.mkdir("testdir")
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# stat a file
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.stat("test"))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# stat a dir (size seems to vary on LFS2 so don't print that)
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.stat("testdir")[:6])
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# read
|
2020-03-22 22:26:08 -04:00
|
|
|
with vfs.open("test", "r") as f:
|
2019-10-18 02:25:48 -04:00
|
|
|
print(f.read())
|
|
|
|
|
|
|
|
# create large file
|
2020-03-22 22:26:08 -04:00
|
|
|
with vfs.open("testbig", "w") as f:
|
|
|
|
data = "large012" * 32 * 16
|
|
|
|
print("data length:", len(data))
|
2019-10-18 02:25:48 -04:00
|
|
|
for i in range(4):
|
2020-03-22 22:26:08 -04:00
|
|
|
print("write", i)
|
2019-10-18 02:25:48 -04:00
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
# stat after creating large file
|
2020-03-22 22:26:08 -04:00
|
|
|
print(vfs.statvfs("/"))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# rename
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.rename("testbig", "testbig2")
|
2020-05-04 09:34:12 -04:00
|
|
|
print(sorted(list(vfs.ilistdir())))
|
|
|
|
vfs.chdir("testdir")
|
|
|
|
vfs.rename("/testbig2", "testbig2")
|
|
|
|
print(sorted(list(vfs.ilistdir())))
|
|
|
|
vfs.rename("testbig2", "/testbig2")
|
|
|
|
vfs.chdir("/")
|
|
|
|
print(sorted(list(vfs.ilistdir())))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# remove
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.remove("testbig2")
|
2020-05-04 09:34:12 -04:00
|
|
|
print(sorted(list(vfs.ilistdir())))
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
# getcwd, chdir
|
2020-05-03 15:05:08 -04:00
|
|
|
vfs.mkdir("/testdir2")
|
|
|
|
vfs.mkdir("/testdir/subdir")
|
2019-10-18 02:25:48 -04:00
|
|
|
print(vfs.getcwd())
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.chdir("/testdir")
|
2019-10-18 02:25:48 -04:00
|
|
|
print(vfs.getcwd())
|
2020-04-29 12:13:22 -04:00
|
|
|
|
|
|
|
# create file in directory to make sure paths are relative
|
|
|
|
vfs.open("test2", "w").close()
|
|
|
|
print(vfs.stat("test2"))
|
|
|
|
print(vfs.stat("/testdir/test2"))
|
|
|
|
vfs.remove("test2")
|
|
|
|
|
|
|
|
# chdir back to root and remove testdir
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.chdir("/")
|
2019-10-18 02:25:48 -04:00
|
|
|
print(vfs.getcwd())
|
2020-05-03 15:05:08 -04:00
|
|
|
vfs.chdir("testdir")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("..")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("testdir/subdir")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("../..")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("/./testdir2")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("../testdir")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("../..")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir(".//testdir")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("subdir/./")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.chdir("/")
|
|
|
|
print(vfs.getcwd())
|
|
|
|
vfs.rmdir("testdir/subdir")
|
2020-03-22 22:26:08 -04:00
|
|
|
vfs.rmdir("testdir")
|
2020-05-03 15:05:08 -04:00
|
|
|
vfs.rmdir("testdir2")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-10-18 02:25:48 -04:00
|
|
|
|
|
|
|
bdev = RAMBlockDevice(30)
|
|
|
|
test(bdev, uos.VfsLfs1)
|
|
|
|
test(bdev, uos.VfsLfs2)
|