2017-01-26 23:34:36 -05:00
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import errno
|
|
|
|
import os
|
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:
|
2023-08-22 11:15:46 -04:00
|
|
|
os.VfsFat
|
2016-10-13 05:48:54 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
def readblocks(self, n, buf):
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
|
2016-10-13 05:48:54 -04:00
|
|
|
for i in range(len(buf)):
|
|
|
|
buf[i] = self.data[n * self.SEC_SIZE + i]
|
|
|
|
|
|
|
|
def writeblocks(self, n, buf):
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("writeblocks(%s, %x)" % (n, id(buf)))
|
2016-10-13 05:48:54 -04:00
|
|
|
for i in range(len(buf)):
|
|
|
|
self.data[n * self.SEC_SIZE + i] = buf[i]
|
|
|
|
|
|
|
|
def ioctl(self, op, arg):
|
2021-03-15 09:57:36 -04:00
|
|
|
# print("ioctl(%d, %r)" % (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)
|
2023-10-11 14:21:57 -04:00
|
|
|
os.VfsFat.mkfs(bdev)
|
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
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
vfs = os.VfsFat(bdev)
|
|
|
|
os.mount(vfs, "/ramdisk")
|
|
|
|
os.chdir("/ramdisk")
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.mkdir("foo_dir")
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.EEXIST)
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.remove("foo_dir")
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.EISDIR)
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
vfs.remove("no_file.txt")
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.ENOENT)
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
try:
|
2017-01-24 04:55:05 -05:00
|
|
|
vfs.rename("foo_dir", "/null/file")
|
2016-10-13 05:48:54 -04:00
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.ENOENT)
|
2016-10-13 05:48:54 -04:00
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
# CIRCUITPY-CHANGE: test
|
2022-10-12 16:52:22 -04:00
|
|
|
try:
|
|
|
|
vfs.rename("foo_dir", "foo_dir/inside_itself")
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.EINVAL)
|
2022-10-12 16:52:22 -04:00
|
|
|
|
2016-10-13 05:48:54 -04:00
|
|
|
# file in dir
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("foo_dir/file-in-dir.txt", "w+t") as f:
|
2016-10-13 05:48:54 -04:00
|
|
|
f.write("data in file")
|
|
|
|
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("foo_dir/file-in-dir.txt", "r+b") as f:
|
2016-10-13 05:48:54 -04:00
|
|
|
print(f.read())
|
|
|
|
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("foo_dir/sub_file.txt", "w") as f:
|
2016-10-13 05:48:54 -04:00
|
|
|
f.write("subdir file")
|
|
|
|
|
|
|
|
# directory not empty
|
|
|
|
try:
|
|
|
|
vfs.rmdir("foo_dir")
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print(e.errno == errno.EACCES)
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
# trim full path
|
2017-01-24 04:55:05 -05:00
|
|
|
vfs.rename("foo_dir/file-in-dir.txt", "foo_dir/file.txt")
|
2017-05-05 09:35:49 -04:00
|
|
|
print(list(vfs.ilistdir("foo_dir")))
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
vfs.rename("foo_dir/file.txt", "moved-to-root.txt")
|
2017-05-05 09:35:49 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-10-13 05:48:54 -04:00
|
|
|
|
2016-12-01 23:06:09 -05:00
|
|
|
# check that renaming to existing file will overwrite it
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("temp", "w") as f:
|
2016-12-01 23:06:09 -05:00
|
|
|
f.write("new text")
|
|
|
|
vfs.rename("temp", "moved-to-root.txt")
|
2017-05-05 09:35:49 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2017-01-26 23:34:36 -05:00
|
|
|
with open("moved-to-root.txt") as f:
|
2016-12-01 23:06:09 -05:00
|
|
|
print(f.read())
|
|
|
|
|
2016-10-13 05:48:54 -04:00
|
|
|
# valid removes
|
|
|
|
vfs.remove("foo_dir/sub_file.txt")
|
|
|
|
vfs.rmdir("foo_dir")
|
2017-05-05 09:35:49 -04:00
|
|
|
print(list(vfs.ilistdir()))
|
2016-10-13 05:48:54 -04:00
|
|
|
|
|
|
|
# disk full
|
|
|
|
try:
|
|
|
|
bsize = vfs.statvfs("/ramdisk")[0]
|
|
|
|
free = vfs.statvfs("/ramdisk")[2] + 1
|
2017-01-26 23:34:36 -05:00
|
|
|
f = open("large_file.txt", "wb")
|
2016-10-13 05:48:54 -04:00
|
|
|
f.write(bytearray(bsize * free))
|
|
|
|
except OSError as e:
|
2023-08-22 11:15:46 -04:00
|
|
|
print("ENOSPC:", e.errno == 28) # errno.ENOSPC
|
2021-03-15 21:45:39 -04:00
|
|
|
f.close()
|