vfs_fat_oldproto.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_OLD:
  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 sync(self):
  25. pass
  26. def count(self):
  27. return len(self.data) // self.SEC_SIZE
  28. try:
  29. bdev = RAMFS_OLD(50)
  30. except MemoryError:
  31. print("SKIP")
  32. raise SystemExit
  33. uos.VfsFat.mkfs(bdev)
  34. vfs = uos.VfsFat(bdev)
  35. uos.mount(vfs, "/ramdisk")
  36. # file io
  37. with vfs.open("file.txt", "w") as f:
  38. f.write("hello!")
  39. print(list(vfs.ilistdir()))
  40. with vfs.open("file.txt", "r") as f:
  41. print(f.read())
  42. vfs.remove("file.txt")
  43. print(list(vfs.ilistdir()))