vfs_fat_fileio1.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. try:
  2. import uerrno
  3. import uos
  4. except ImportError:
  5. print("SKIP")
  6. raise SystemExit
  7. try:
  8. uos.VfsFat
  9. except AttributeError:
  10. print("SKIP")
  11. raise SystemExit
  12. class RAMFS:
  13. SEC_SIZE = 512
  14. def __init__(self, blocks):
  15. self.data = bytearray(blocks * self.SEC_SIZE)
  16. def readblocks(self, n, buf):
  17. #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
  18. for i in range(len(buf)):
  19. buf[i] = self.data[n * self.SEC_SIZE + i]
  20. def writeblocks(self, n, buf):
  21. #print("writeblocks(%s, %x)" % (n, id(buf)))
  22. for i in range(len(buf)):
  23. self.data[n * self.SEC_SIZE + i] = buf[i]
  24. def ioctl(self, op, arg):
  25. #print("ioctl(%d, %r)" % (op, arg))
  26. if op == 4: # BP_IOCTL_SEC_COUNT
  27. return len(self.data) // self.SEC_SIZE
  28. if op == 5: # BP_IOCTL_SEC_SIZE
  29. return self.SEC_SIZE
  30. try:
  31. bdev = RAMFS(50)
  32. except MemoryError:
  33. print("SKIP")
  34. raise SystemExit
  35. uos.VfsFat.mkfs(bdev)
  36. vfs = uos.VfsFat(bdev)
  37. uos.mount(vfs, '/ramdisk')
  38. uos.chdir('/ramdisk')
  39. # file IO
  40. f = open("foo_file.txt", "w")
  41. print(str(f)[:17], str(f)[-1:])
  42. f.write("hello!")
  43. f.flush()
  44. f.close()
  45. f.close() # allowed
  46. try:
  47. f.write("world!")
  48. except OSError as e:
  49. print(e.args[0] == uerrno.EINVAL)
  50. try:
  51. f.read()
  52. except OSError as e:
  53. print(e.args[0] == uerrno.EINVAL)
  54. try:
  55. f.flush()
  56. except OSError as e:
  57. print(e.args[0] == uerrno.EINVAL)
  58. try:
  59. open("foo_file.txt", "x")
  60. except OSError as e:
  61. print(e.args[0] == uerrno.EEXIST)
  62. with open("foo_file.txt", "a") as f:
  63. f.write("world!")
  64. with open("foo_file.txt") as f2:
  65. print(f2.read())
  66. print(f2.tell())
  67. f2.seek(0, 0) # SEEK_SET
  68. print(f2.read(1))
  69. f2.seek(0, 1) # SEEK_CUR
  70. print(f2.read(1))
  71. f2.seek(2, 1) # SEEK_CUR
  72. print(f2.read(1))
  73. f2.seek(-2, 2) # SEEK_END
  74. print(f2.read(1))
  75. # using constructor of FileIO type to open a file
  76. # no longer working with new VFS sub-system
  77. #FileIO = type(f)
  78. #with FileIO("/ramdisk/foo_file.txt") as f:
  79. # print(f.read())
  80. # dirs
  81. vfs.mkdir("foo_dir")
  82. try:
  83. vfs.rmdir("foo_file.txt")
  84. except OSError as e:
  85. print(e.args[0] == 20) # uerrno.ENOTDIR
  86. vfs.remove("foo_file.txt")
  87. print(list(vfs.ilistdir()))
  88. # Here we test that opening a file with the heap locked fails correctly. This
  89. # is a special case because file objects use a finaliser and allocating with a
  90. # finaliser is a different path to normal allocation. It would be better to
  91. # test this in the core tests but there are no core objects that use finaliser.
  92. import micropython
  93. micropython.heap_lock()
  94. try:
  95. vfs.open('x', 'r')
  96. except MemoryError:
  97. print('MemoryError')
  98. micropython.heap_unlock()
  99. # Here we test that the finaliser is actually called during a garbage collection.
  100. import gc
  101. N = 4
  102. for i in range(N):
  103. n = 'x%d' % i
  104. f = vfs.open(n, 'w')
  105. f.write(n)
  106. f = None # release f without closing
  107. [0, 1, 2, 3] # use up Python stack so f is really gone
  108. gc.collect() # should finalise all N files by closing them
  109. for i in range(N):
  110. with vfs.open('x%d' % i, 'r') as f:
  111. print(f.read())