Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fs.md 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Using the filesystem
  2. The Collapse OS filesystem (CFS) is a very simple FS that aims at implementation
  3. simplicity first. It is not efficient or featureful, but allows you to get
  4. play around with the concept of files so that you can conveniently run programs
  5. targeting named blocks of data with in storage.
  6. The filesystem sits on a block device and there can only be one active
  7. filesystem at once.
  8. Files are represented by adjacent blocks of `0x100` bytes with `0x20` bytes of
  9. metadata on the first block. That metadata tells the location of the next block
  10. which allows for block iteration.
  11. To create a file, you must allocate blocks to it and these blocks can't be
  12. grown (you have to delete the file and re-allocate it). When allocating new
  13. files, Collapse OS tries to reuse blocks from deleted files if it can.
  14. Once "mounted" (turned on with `fson`), you can list files, allocate new files
  15. with `fnew`, mark files as deleted with `fdel` and, more importantly, open files
  16. with `fopen`.
  17. Opened files are accessed a independent block devices. It's the glue code that
  18. decides how many file handles we'll support and to which block device ID each
  19. file handle will be assigned.
  20. For example, you could have a system with three block devices, one for ACIA and
  21. one for a SD card and one for a file handle. You would mount the filesystem on
  22. block device `1` (the SD card), then open a file on handle `0` with `fopen 0
  23. filename`. You would then do `bsel 2` to select your third block device which
  24. is mapped to the file you've just opened.
  25. ## Trying it in the emulator
  26. The shell emulator in `tools/emul/shell` is geared for filesystem usage. If you
  27. look at `shell_.asm`, you'll see that there are 4 block devices: one for
  28. console, one for fake storage (`fsdev`) and two file handles (we call them
  29. `stdout` and `stdin`, but both are read/write in this context).
  30. The fake device `fsdev` is hooked to the host system through the `cfspack`
  31. utility. Then the emulated shell is started, it checks for the existence of a
  32. `cfsin` directory and, if it exists, it packs its content into a CFS blob and
  33. shoves it into its `fsdev` storage.
  34. To, to try it out, do this:
  35. $ mkdir cfsin
  36. $ echo "Hello!" > cfsin/foo
  37. $ echo "Goodbye!" > cfsin/bar
  38. $ ./shell
  39. The shell, upon startup, automatically calls `fson` targeting block device `1`,
  40. so it's ready to use:
  41. > fls
  42. foo
  43. bar
  44. > fopen 0 foo
  45. > bsel 2
  46. > getb
  47. > puth a
  48. 65
  49. > getb
  50. > puth a
  51. 6C
  52. > getb
  53. > puth a
  54. 6C
  55. > getb
  56. > puth a
  57. 6F
  58. > getb
  59. > puth a
  60. 21
  61. > fdel bar
  62. > fls
  63. foo
  64. >