lfs.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * The little filesystem
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #ifndef LFS_H
  8. #define LFS_H
  9. #include "lfs_config.h"
  10. #include "lfs_bd.h"
  11. // Data structures
  12. enum lfs_error {
  13. LFS_ERROR_OK = 0,
  14. LFS_ERROR_CORRUPT = -3,
  15. LFS_ERROR_NO_ENTRY = -4,
  16. LFS_ERROR_EXISTS = -5,
  17. LFS_ERROR_NOT_DIR = -6,
  18. LFS_ERROR_INVALID = -7,
  19. LFS_ERROR_NO_SPACE = -8,
  20. };
  21. enum lfs_type {
  22. LFS_TYPE_REG = 1,
  23. LFS_TYPE_DIR = 2,
  24. };
  25. enum lfs_open_flags {
  26. LFS_O_RDONLY = 0,
  27. LFS_O_WRONLY = 1,
  28. LFS_O_RDWR = 2,
  29. LFS_O_CREAT = 0x0040,
  30. LFS_O_EXCL = 0x0080,
  31. LFS_O_TRUNC = 0x0200,
  32. LFS_O_APPEND = 0x0400,
  33. LFS_O_SYNC = 0x1000,
  34. };
  35. typedef struct lfs_entry {
  36. lfs_block_t dir[2];
  37. lfs_off_t off;
  38. struct lfs_disk_entry {
  39. uint16_t type;
  40. uint16_t len;
  41. union {
  42. struct {
  43. lfs_block_t head;
  44. lfs_size_t size;
  45. } file;
  46. lfs_block_t dir[2];
  47. } u;
  48. } d;
  49. } lfs_entry_t;
  50. typedef struct lfs_file {
  51. lfs_block_t head;
  52. lfs_size_t size;
  53. lfs_block_t wblock;
  54. uint32_t windex;
  55. lfs_block_t rblock;
  56. uint32_t rindex;
  57. lfs_off_t roff;
  58. struct lfs_entry entry;
  59. } lfs_file_t;
  60. typedef struct lfs_dir {
  61. lfs_block_t pair[2];
  62. lfs_off_t off;
  63. struct lfs_disk_dir {
  64. uint32_t rev;
  65. lfs_size_t size;
  66. lfs_block_t tail[2];
  67. struct lfs_disk_free {
  68. uint32_t begin;
  69. uint32_t end;
  70. } free;
  71. } d;
  72. } lfs_dir_t;
  73. typedef struct lfs_superblock {
  74. lfs_block_t pair[2];
  75. struct lfs_disk_superblock {
  76. uint32_t rev;
  77. uint32_t size;
  78. lfs_block_t root[2];
  79. char magic[8];
  80. uint32_t block_size;
  81. uint32_t block_count;
  82. } d;
  83. } lfs_superblock_t;
  84. // Little filesystem type
  85. typedef struct lfs {
  86. lfs_bd_t *bd;
  87. const struct lfs_bd_ops *bd_ops;
  88. lfs_block_t cwd[2];
  89. struct lfs_disk_free free;
  90. lfs_size_t read_size; // size of read
  91. lfs_size_t prog_size; // size of program
  92. lfs_size_t block_size; // size of erase (block size)
  93. lfs_size_t block_count; // number of erasable blocks
  94. lfs_size_t words; // number of 32-bit words that can fit in a block
  95. } lfs_t;
  96. // Functions
  97. int lfs_format(lfs_t *lfs, lfs_bd_t *bd, const struct lfs_bd_ops *bd_ops);
  98. int lfs_mount(lfs_t *lfs, lfs_bd_t *bd, const struct lfs_bd_ops *bd_ops);
  99. int lfs_unmount(lfs_t *lfs);
  100. int lfs_mkdir(lfs_t *lfs, const char *path);
  101. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  102. const char *path, int flags);
  103. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  104. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  105. const void *buffer, lfs_size_t size);
  106. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  107. void *buffer, lfs_size_t size);
  108. #endif