lfs.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_IS_DIR = -7,
  19. LFS_ERROR_INVALID = -8,
  20. LFS_ERROR_NO_SPACE = -9,
  21. };
  22. enum lfs_type {
  23. LFS_TYPE_REG = 1,
  24. LFS_TYPE_DIR = 2,
  25. };
  26. enum lfs_open_flags {
  27. LFS_O_RDONLY = 0,
  28. LFS_O_WRONLY = 1,
  29. LFS_O_RDWR = 2,
  30. LFS_O_CREAT = 0x020,
  31. LFS_O_EXCL = 0x040,
  32. LFS_O_TRUNC = 0x080,
  33. LFS_O_APPEND = 0x100,
  34. LFS_O_SYNC = 0x200,
  35. };
  36. struct lfs_config {
  37. lfs_bd_t *bd;
  38. const struct lfs_bd_ops *bd_ops;
  39. lfs_size_t read_size;
  40. lfs_size_t prog_size;
  41. lfs_size_t block_size;
  42. lfs_size_t block_count;
  43. };
  44. struct lfs_info {
  45. uint8_t type;
  46. lfs_size_t size;
  47. char name[LFS_NAME_MAX+1];
  48. };
  49. typedef struct lfs_entry {
  50. lfs_block_t dir[2];
  51. lfs_off_t off;
  52. struct lfs_disk_entry {
  53. uint16_t type;
  54. uint16_t len;
  55. union {
  56. struct {
  57. lfs_block_t head;
  58. lfs_size_t size;
  59. } file;
  60. lfs_block_t dir[2];
  61. } u;
  62. } d;
  63. } lfs_entry_t;
  64. typedef struct lfs_file {
  65. lfs_block_t head;
  66. lfs_size_t size;
  67. lfs_block_t wblock;
  68. uint32_t windex;
  69. lfs_block_t rblock;
  70. uint32_t rindex;
  71. lfs_off_t roff;
  72. struct lfs_entry entry;
  73. } lfs_file_t;
  74. typedef struct lfs_dir {
  75. lfs_block_t pair[2];
  76. lfs_off_t off;
  77. struct lfs_disk_dir {
  78. uint32_t rev;
  79. lfs_size_t size;
  80. lfs_block_t tail[2];
  81. } d;
  82. } lfs_dir_t;
  83. typedef struct lfs_superblock {
  84. lfs_block_t pair[2];
  85. struct lfs_disk_superblock {
  86. uint32_t rev;
  87. uint32_t size;
  88. lfs_block_t root[2];
  89. char magic[8];
  90. uint32_t block_size;
  91. uint32_t block_count;
  92. } d;
  93. } lfs_superblock_t;
  94. // Little filesystem type
  95. typedef struct lfs {
  96. lfs_size_t read_size; // size of read
  97. lfs_size_t prog_size; // size of program
  98. lfs_size_t block_size; // size of erase (block size)
  99. lfs_size_t block_count; // number of erasable blocks
  100. lfs_size_t words; // number of 32-bit words that can fit in a block
  101. lfs_bd_t *bd;
  102. const struct lfs_bd_ops *bd_ops;
  103. lfs_block_t root[2];
  104. lfs_block_t cwd[2];
  105. struct {
  106. lfs_block_t begin;
  107. lfs_block_t end;
  108. } free;
  109. uint32_t lookahead[LFS_CFG_LOOKAHEAD/32];
  110. } lfs_t;
  111. // Functions
  112. int lfs_format(lfs_t *lfs, const struct lfs_config *config);
  113. int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
  114. int lfs_unmount(lfs_t *lfs);
  115. int lfs_remove(lfs_t *lfs, const char *path);
  116. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
  117. int lfs_mkdir(lfs_t *lfs, const char *path);
  118. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
  119. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
  120. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
  121. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  122. const char *path, int flags);
  123. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  124. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  125. const void *buffer, lfs_size_t size);
  126. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  127. void *buffer, lfs_size_t size);
  128. int lfs_deorphan(lfs_t *lfs);
  129. int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
  130. #endif