2017-01-26 23:34:36 -05:00
|
|
|
try:
|
2017-04-02 17:14:57 -04:00
|
|
|
import uerrno
|
2018-06-05 23:24:09 -04:00
|
|
|
import uos
|
2017-01-26 23:34:36 -05:00
|
|
|
except ImportError:
|
2017-04-02 17:14:57 -04:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-04-02 17:14:57 -04:00
|
|
|
|
2016-10-13 05:48:54 -04:00
|
|
|
try:
|
|
|
|
uos.VfsFat
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
|
|
|
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
# Don't do any allocations in the below functions because they may be called
|
|
|
|
# during a gc_sweep from a finalizer.
|
2016-10-13 05:48:54 -04:00
|
|
|
def readblocks(self, n, buf):
|
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
2018-06-14 21:02:12 -04:00
|
|
|
return 0
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
2018-06-14 21:02:12 -04:00
|
|
|
return 0
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2021-04-23 15:26:42 -04:00
|
|
|
if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
|
2016-10-13 05:48:54 -04:00
|
|
|
return len(self.data) // self.SEC_SIZE
|
2021-04-23 15:26:42 -04:00
|
|
|
if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
|
2016-10-13 05:48:54 -04:00
|
|
|
return self.SEC_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2017-01-24 04:55:05 -05:00
|
|
|
bdev = RAMFS(50)
|
2016-10-13 05:48:54 -04:00
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
uos.VfsFat.mkfs(bdev)
|
2017-01-26 23:34:36 -05:00
|
|
|
vfs = uos.VfsFat(bdev)
|
2021-03-15 09:57:36 -04:00
|
|
|
uos.mount(vfs, "/ramdisk")
|
|
|
|
uos.chdir("/ramdisk")
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
# file IO
|
2017-01-26 23:34:36 -05:00
|
|
|
f = open("foo_file.txt", "w")
|
2016-10-13 05:48:54 -04:00
|
|
|
print(str(f)[:17], str(f)[-1:])
|
|
|
|
f.write("hello!")
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
2021-03-15 09:57:36 -04:00
|
|
|
f.close() # allowed
|
2016-10-13 05:48:54 -04: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-26 23:34:36 -05:00
|
|
|
open("foo_file.txt", "x")
|
2016-10-13 05:48:54 -04:00
|
|
|
except OSError as e:
|
|
|
|
print(e.args[0] == uerrno.EEXIST)
|
|
|
|
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("foo_file.txt", "a") as f:
|
2016-10-13 05:48:54 -04:00
|
|
|
f.write("world!")
|
|
|
|
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("foo_file.txt") as f2:
|
2016-10-13 05:48:54 -04:00
|
|
|
print(f2.read())
|
|
|
|
print(f2.tell())
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
f2.seek(0, 0) # SEEK_SET
|
2016-10-13 05:48:54 -04:00
|
|
|
print(f2.read(1))
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
f2.seek(0, 1) # SEEK_CUR
|
2016-10-13 05:48:54 -04:00
|
|
|
print(f2.read(1))
|
2021-03-15 09:57:36 -04:00
|
|
|
f2.seek(2, 1) # SEEK_CUR
|
2017-12-19 17:47:07 -05:00
|
|
|
print(f2.read(1))
|
2016-10-13 05:48:54 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
f2.seek(-2, 2) # SEEK_END
|
2016-10-13 05:48:54 -04:00
|
|
|
print(f2.read(1))
|
|
|
|
|
2016-12-01 23:37:58 -05:00
|
|
|
# using constructor of FileIO type to open a file
|
2017-01-26 23:34:36 -05:00
|
|
|
# no longer working with new VFS sub-system
|
2021-03-15 09:57:36 -04:00
|
|
|
# FileIO = type(f)
|
|
|
|
# with FileIO("/ramdisk/foo_file.txt") as f:
|
2017-01-26 23:34:36 -05:00
|
|
|
# print(f.read())
|
2016-12-01 23:37:58 -05:00
|
|
|
|
2016-10-13 05:48:54 -04:00
|
|
|
# dirs
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.rmdir("foo_file.txt")
|
|
|
|
except OSError as e:
|
2021-03-15 09:57:36 -04:00
|
|
|
print(e.args[0] == 20) # uerrno.ENOTDIR
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
vfs.remove("foo_file.txt")
|
2017-05-05 09:35:49 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2018-02-25 21:36:13 -05: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
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-02-25 21:36:13 -05:00
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
vfs.open("x", "r")
|
2018-02-25 21:36:13 -05:00
|
|
|
except MemoryError:
|
2021-03-15 09:57:36 -04:00
|
|
|
print("MemoryError")
|
2018-02-25 21:36:13 -05:00
|
|
|
micropython.heap_unlock()
|
2018-02-27 23:27:51 -05:00
|
|
|
|
|
|
|
# Here we test that the finaliser is actually called during a garbage collection.
|
|
|
|
import gc
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-02-27 23:27:51 -05:00
|
|
|
N = 4
|
|
|
|
for i in range(N):
|
2021-03-15 09:57:36 -04:00
|
|
|
n = "x%d" % i
|
|
|
|
f = vfs.open(n, "w")
|
2018-02-27 23:27:51 -05:00
|
|
|
f.write(n)
|
2021-03-15 09:57:36 -04:00
|
|
|
f = None # release f without closing
|
2021-04-23 15:26:42 -04:00
|
|
|
[0, 1, 2, 3, 4, 5] # use up Python stack so f is really gone
|
2021-03-15 09:57:36 -04:00
|
|
|
gc.collect() # should finalise all N files by closing them
|
2018-02-27 23:27:51 -05:00
|
|
|
for i in range(N):
|
2021-03-15 09:57:36 -04:00
|
|
|
with vfs.open("x%d" % i, "r") as f:
|
2018-02-27 23:27:51 -05:00
|
|
|
print(f.read())
|