2016-02-14 17:02:34 +00:00
|
|
|
import sys
|
|
|
|
import uos
|
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
2016-02-14 18:52:38 +00:00
|
|
|
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
2016-02-14 17:02:34 +00:00
|
|
|
def __init__(self, blocks):
|
2016-02-14 18:52:38 +00:00
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
|
|
|
#print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
|
|
|
for i in range(len(buf)):
|
2016-02-14 18:52:38 +00:00
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
|
|
|
#print("writeblocks(%s, %x)" % (n, id(buf)))
|
|
|
|
for i in range(len(buf)):
|
2016-02-14 18:52:38 +00:00
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
2016-02-14 17:02:34 +00:00
|
|
|
|
2016-02-14 18:45:08 +00:00
|
|
|
def ioctl(self, op, arg):
|
|
|
|
#print("ioctl(%d, %r)" % (op, arg))
|
|
|
|
if op == 4: # BP_IOCTL_SEC_COUNT
|
2016-02-14 18:52:38 +00:00
|
|
|
return len(self.data) // self.SEC_SIZE
|
|
|
|
if op == 5: # BP_IOCTL_SEC_SIZE
|
|
|
|
return self.SEC_SIZE
|
2016-02-14 17:02:34 +00:00
|
|
|
|
|
|
|
|
2016-02-15 15:27:57 +00:00
|
|
|
try:
|
|
|
|
bdev = RAMFS(48)
|
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
|
|
|
sys.exit()
|
|
|
|
|
2016-02-14 17:02:34 +00:00
|
|
|
uos.VfsFat.mkfs(bdev)
|
|
|
|
|
|
|
|
assert b"FOO_FILETXT" not in bdev.data
|
|
|
|
assert b"hello!" not in bdev.data
|
|
|
|
|
|
|
|
vfs = uos.VfsFat(bdev, "/ramdisk")
|
|
|
|
|
|
|
|
f = vfs.open("foo_file.txt", "w")
|
|
|
|
f.write("hello!")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
f2 = vfs.open("foo_file.txt")
|
|
|
|
print(f2.read())
|
|
|
|
f2.close()
|
|
|
|
|
|
|
|
assert b"FOO_FILETXT" in bdev.data
|
|
|
|
assert b"hello!" in bdev.data
|
2016-02-28 15:19:12 +00:00
|
|
|
|
|
|
|
assert vfs.listdir() == ['foo_file.txt']
|
2016-02-28 18:30:59 +00:00
|
|
|
|
|
|
|
vfs.remove('foo_file.txt')
|
|
|
|
assert vfs.listdir() == []
|
2016-02-28 22:06:27 +00:00
|
|
|
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
assert vfs.listdir() == ['foo_dir']
|
2016-02-28 23:03:32 +00:00
|
|
|
f = vfs.open("foo_dir/file-in-dir.txt", "w")
|
|
|
|
f.write("data in file")
|
|
|
|
f.close()
|
2016-02-28 22:06:27 +00:00
|
|
|
|
|
|
|
assert vfs.listdir("foo_dir") == ['file-in-dir.txt']
|
2016-02-28 23:24:05 +00:00
|
|
|
|
|
|
|
vfs.rename("foo_dir/file-in-dir.txt", "moved-to-root.txt")
|
|
|
|
assert vfs.listdir() == ['foo_dir', 'moved-to-root.txt']
|