2017-01-27 04:34:36 +00:00
|
|
|
try:
|
2022-08-18 06:57:45 +00:00
|
|
|
import errno
|
|
|
|
import os
|
2017-01-27 04:34:36 +00:00
|
|
|
except ImportError:
|
2017-04-02 21:14:57 +00:00
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2017-04-02 21:14:57 +00:00
|
|
|
|
2016-10-13 09:48:54 +00:00
|
|
|
try:
|
2022-08-18 06:57:45 +00:00
|
|
|
os.VfsFat
|
2016-10-13 09:48:54 +00:00
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RAMFS:
|
|
|
|
SEC_SIZE = 512
|
|
|
|
|
|
|
|
def __init__(self, blocks):
|
|
|
|
self.data = bytearray(blocks * self.SEC_SIZE)
|
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
2016-10-13 09:48:54 +00:00
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("writeblocks(%s, %x)" % (n, id(buf)))
|
2016-10-13 09:48:54 +00:00
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2020-03-23 02:26:08 +00:00
|
|
|
# print("ioctl(%d, %r)" % (op, arg))
|
2019-10-29 01:25:30 +00:00
|
|
|
if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT
|
2016-10-13 09:48:54 +00:00
|
|
|
return len(self.data) // self.SEC_SIZE
|
2019-10-29 01:25:30 +00:00
|
|
|
if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE
|
2016-10-13 09:48:54 +00:00
|
|
|
return self.SEC_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2017-01-24 09:55:05 +00:00
|
|
|
bdev = RAMFS(50)
|
2022-08-18 06:57:45 +00:00
|
|
|
os.VfsFat.mkfs(bdev)
|
2016-10-13 09:48:54 +00:00
|
|
|
except MemoryError:
|
|
|
|
print("SKIP")
|
2017-06-10 17:14:16 +00:00
|
|
|
raise SystemExit
|
2016-10-13 09:48:54 +00:00
|
|
|
|
2022-08-18 06:57:45 +00:00
|
|
|
vfs = os.VfsFat(bdev)
|
|
|
|
os.mount(vfs, "/ramdisk")
|
|
|
|
os.chdir("/ramdisk")
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
# file IO
|
2017-01-27 04:34:36 +00:00
|
|
|
f = open("foo_file.txt", "w")
|
2016-10-13 09:48:54 +00:00
|
|
|
print(str(f)[:17], str(f)[-1:])
|
|
|
|
f.write("hello!")
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
2020-03-23 02:26:08 +00:00
|
|
|
f.close() # allowed
|
2016-10-13 09:48:54 +00:00
|
|
|
try:
|
|
|
|
f.write("world!")
|
|
|
|
except OSError as e:
|
2022-08-18 06:57:45 +00:00
|
|
|
print(e.errno == errno.EINVAL)
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
f.read()
|
|
|
|
except OSError as e:
|
2022-08-18 06:57:45 +00:00
|
|
|
print(e.errno == errno.EINVAL)
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
f.flush()
|
|
|
|
except OSError as e:
|
2022-08-18 06:57:45 +00:00
|
|
|
print(e.errno == errno.EINVAL)
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
try:
|
2017-01-27 04:34:36 +00:00
|
|
|
open("foo_file.txt", "x")
|
2016-10-13 09:48:54 +00:00
|
|
|
except OSError as e:
|
2022-08-18 06:57:45 +00:00
|
|
|
print(e.errno == errno.EEXIST)
|
2016-10-13 09:48:54 +00:00
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_file.txt", "a") as f:
|
2016-10-13 09:48:54 +00:00
|
|
|
f.write("world!")
|
|
|
|
|
2017-01-27 04:34:36 +00:00
|
|
|
with open("foo_file.txt") as f2:
|
2016-10-13 09:48:54 +00:00
|
|
|
print(f2.read())
|
|
|
|
print(f2.tell())
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
f2.seek(0, 0) # SEEK_SET
|
2016-10-13 09:48:54 +00:00
|
|
|
print(f2.read(1))
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
f2.seek(0, 1) # SEEK_CUR
|
2016-10-13 09:48:54 +00:00
|
|
|
print(f2.read(1))
|
2020-03-23 02:26:08 +00:00
|
|
|
f2.seek(2, 1) # SEEK_CUR
|
2017-12-19 22:47:07 +00:00
|
|
|
print(f2.read(1))
|
2016-10-13 09:48:54 +00:00
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
f2.seek(-2, 2) # SEEK_END
|
2016-10-13 09:48:54 +00:00
|
|
|
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
|
2020-03-23 02:26:08 +00:00
|
|
|
# FileIO = type(f)
|
|
|
|
# with FileIO("/ramdisk/foo_file.txt") as f:
|
2017-01-27 04:34:36 +00:00
|
|
|
# print(f.read())
|
2016-12-02 04:37:58 +00:00
|
|
|
|
2016-10-13 09:48:54 +00:00
|
|
|
# dirs
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.rmdir("foo_file.txt")
|
|
|
|
except OSError as e:
|
2022-08-18 06:57:45 +00:00
|
|
|
print(e.errno == 20) # errno.ENOTDIR
|
2016-10-13 09:48:54 +00:00
|
|
|
|
|
|
|
vfs.remove("foo_file.txt")
|
2017-05-05 13:35:49 +00:00
|
|
|
print(list(vfs.ilistdir()))
|