lfs.h 3.7 KB

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