2016-10-13 10:48:54 +01:00
|
|
|
import sys
|
|
|
|
import uerrno
|
2017-01-27 04:34:36 +00:00
|
|
|
try:
|
|
|
|
import uos_vfs as uos
|
|
|
|
open = uos.vfs_open
|
|
|
|
except ImportError:
|
|
|
|
import uos
|
2016-10-13 10:48:54 +01:00
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
|
|
|
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
|
|
|
#print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
|
|
|
#print("writeblocks(%s, %x)" % (n, id(buf)))
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
|
|
|
#print("ioctl(%d, %r)" % (op, arg))
|
|
|
|
if op == 4: # BP_IOCTL_SEC_COUNT
|
|
|
|
return len(self.data) // self.SEC_SIZE
|
|
|
|
if op == 5: # BP_IOCTL_SEC_SIZE
|
|
|
|
return self.SEC_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2017-01-24 09:55:05 +00:00
|
|
|
bdev = RAMFS(50)
|
2016-10-13 10:48:54 +01:00
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
uos.VfsFat.mkfs(bdev)
|
2017-01-27 04:34:36 +00:00
|
|
|
vfs = uos.VfsFat(bdev)
|
|
|
|
uos.mount(vfs, '/ramdisk')
|
|
|
|
uos.chdir('/ramdisk')
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
# file IO
|
2017-01-27 04:34:36 +00:00
|
|
|
f = open("foo_file.txt", "w")
|
2016-10-13 10:48:54 +01:00
|
|
|
print(str(f)[:17], str(f)[-1:])
|
|
|
|
f.write("hello!")
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
2016-10-24 02:59:20 +01:00
|
|
|
f.close() # allowed
|
2016-10-13 10:48:54 +01:00
|
|
|
try:
|
|
|
|
f.write("world!")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EINVAL)
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.read()
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EINVAL)
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.flush()
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EINVAL)
|
|
|
|
|
|
|
|
try:
|
2017-01-27 04:34:36 +00:00
|
|
|
open("foo_file.txt", "x")
|
2016-10-13 10:48:54 +01:00
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EEXIST)
|
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_file.txt", "a") as f:
|
2016-10-13 10:48:54 +01:00
|
|
|
f.write("world!")
|
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_file.txt") as f2:
|
2016-10-13 10:48:54 +01:00
|
|
|
print(f2.read())
|
|
|
|
print(f2.tell())
|
|
|
|
|
|
|
|
f2.seek(0, 0) # SEEK_SET
|
|
|
|
print(f2.read(1))
|
|
|
|
|
|
|
|
f2.seek(0, 1) # SEEK_CUR
|
|
|
|
print(f2.read(1))
|
|
|
|
try:
|
|
|
|
f2.seek(1, 1) # SEEK_END
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EOPNOTSUPP)
|
|
|
|
|
|
|
|
f2.seek(-2, 2) # SEEK_END
|
|
|
|
print(f2.read(1))
|
|
|
|
|
2016-12-02 04:37:58 +00:00
|
|
|
# using constructor of FileIO type to open a file
|
2017-01-27 04:34:36 +00:00
|
|
|
# no longer working with new VFS sub-system
|
|
|
|
#FileIO = type(f)
|
|
|
|
#with FileIO("/ramdisk/foo_file.txt") as f:
|
|
|
|
# print(f.read())
|
2016-12-02 04:37:58 +00:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
# dirs
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.rmdir("foo_file.txt")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == 20) # uerrno.ENOTDIR
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EEXIST)
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.remove("foo_dir")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EISDIR)
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.remove("no_file.txt")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.ENOENT)
|
|
|
|
|
|
|
|
try:
|
2017-01-24 09:55:05 +00:00
|
|
|
vfs.rename("foo_dir", "/null/file")
|
2016-10-13 10:48:54 +01:00
|
|
|
except OSError as e:
|
2017-01-24 09:55:05 +00:00
|
|
|
print(e.args[0] == uerrno.ENOENT)
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
# file in dir
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_dir/file-in-dir.txt", "w+t") as f:
|
2016-10-13 10:48:54 +01:00
|
|
|
f.write("data in file")
|
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_dir/file-in-dir.txt", "r+b") as f:
|
2016-10-13 10:48:54 +01:00
|
|
|
print(f.read())
|
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_dir/sub_file.txt", "w") as f:
|
2016-10-13 10:48:54 +01:00
|
|
|
f.write("subdir file")
|
|
|
|
|
|
|
|
# directory not empty
|
|
|
|
try:
|
|
|
|
vfs.rmdir("foo_dir")
|
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EACCES)
|
|
|
|
|
|
|
|
# trim full path
|
2017-01-24 09:55:05 +00:00
|
|
|
vfs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt")
|
2016-10-13 10:48:54 +01:00
|
|
|
print(vfs.listdir("foo_dir"))
|
|
|
|
|
|
|
|
vfs.rename("foo_dir/file.txt", "moved-to-root.txt")
|
|
|
|
print(vfs.listdir())
|
|
|
|
|
2016-12-02 04:06:09 +00:00
|
|
|
# check that renaming to existing file will overwrite it
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("temp", "w") as f:
|
2016-12-02 04:06:09 +00:00
|
|
|
f.write("new text")
|
|
|
|
vfs.rename("temp", "moved-to-root.txt")
|
|
|
|
print(vfs.listdir())
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("moved-to-root.txt") as f:
|
2016-12-02 04:06:09 +00:00
|
|
|
print(f.read())
|
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
# valid removes
|
|
|
|
vfs.remove("foo_dir/sub_file.txt")
|
|
|
|
vfs.remove("foo_file.txt")
|
|
|
|
vfs.rmdir("foo_dir")
|
|
|
|
print(vfs.listdir())
|
|
|
|
|
|
|
|
# disk full
|
|
|
|
try:
|
|
|
|
bsize = vfs.statvfs("/ramdisk")[0]
|
|
|
|
free = vfs.statvfs("/ramdisk")[2] + 1
|
2017-01-27 04:34:36 +00:00
|
|
|
f = open("large_file.txt", "wb")
|
2016-10-13 10:48:54 +01:00
|
|
|
f.write(bytearray(bsize * free))
|
|
|
|
except OSError as e:
|
|
|
|
print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC
|