2017-01-27 04:34:36 +00:00
|
|
|
try:
|
2017-04-02 22:14:57 +01:00
|
|
|
import uerrno
|
2018-06-06 04:24:09 +01:00
|
|
|
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
|
|
|
|
|
|
|
# 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))
|
2017-12-19 22:47:07 +00:00
|
|
|
f2.seek(2, 1) # SEEK_CUR
|
|
|
|
print(f2.read(1))
|
2016-10-13 10:48:54 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
vfs.remove("foo_file.txt")
|
2017-05-05 14:35:49 +01:00
|
|
|
print(list(vfs.ilistdir()))
|
2018-02-26 02:36:13 +00:00
|
|
|
|
|
|
|
# Here we test that opening a file with the heap locked fails correctly. This
|
|
|
|
# is a special case because file objects use a finaliser and allocating with a
|
|
|
|
# finaliser is a different path to normal allocation. It would be better to
|
|
|
|
# test this in the core tests but there are no core objects that use finaliser.
|
|
|
|
import micropython
|
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
|
|
|
vfs.open('x', 'r')
|
|
|
|
except MemoryError:
|
|
|
|
print('MemoryError')
|
|
|
|
micropython.heap_unlock()
|
2018-02-28 04:27:51 +00:00
|
|
|
|
|
|
|
# Here we test that the finaliser is actually called during a garbage collection.
|
|
|
|
import gc
|
|
|
|
N = 4
|
|
|
|
for i in range(N):
|
|
|
|
n = 'x%d' % i
|
|
|
|
f = vfs.open(n, 'w')
|
|
|
|
f.write(n)
|
|
|
|
f = None # release f without closing
|
|
|
|
[0, 1, 2, 3] # use up Python stack so f is really gone
|
|
|
|
gc.collect() # should finalise all N files by closing them
|
|
|
|
for i in range(N):
|
|
|
|
with vfs.open('x%d' % i, 'r') as f:
|
|
|
|
print(f.read())
|