2017-01-27 04:34:36 +00:00
|
|
|
try:
|
2017-04-02 22:14:57 +01:00
|
|
|
import uerrno
|
|
|
|
try:
|
|
|
|
import uos_vfs as uos
|
|
|
|
open = uos.vfs_open
|
|
|
|
except ImportError:
|
|
|
|
import uos
|
2017-01-27 04:34:36 +00:00
|
|
|
except ImportError:
|
2017-04-02 22:14:57 +01:00
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2017-04-02 22:14:57 +01:00
|
|
|
|
2016-10-13 10:48:54 +01:00
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir("foo_dir")))
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
vfs.rename("foo_dir/file.txt", "moved-to-root.txt")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-10-13 10:48:54 +01:00
|
|
|
|
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")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
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.rmdir("foo_dir")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
# 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
|