lfs.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. };
  16. typedef struct lfs_free {
  17. lfs_disk_struct lfs_disk_free {
  18. lfs_ino_t head;
  19. lfs_word_t ioff;
  20. lfs_word_t icount;
  21. lfs_word_t rev;
  22. } d;
  23. } lfs_free_t;
  24. typedef struct lfs_dir {
  25. lfs_ino_t pair[2];
  26. lfs_disk_struct lfs_disk_dir {
  27. lfs_word_t rev;
  28. lfs_size_t size;
  29. lfs_ino_t tail[2];
  30. lfs_ino_t parent[2];
  31. struct lfs_disk_free free;
  32. } d;
  33. } lfs_dir_t;
  34. typedef struct lfs_entry {
  35. lfs_ino_t dir[2];
  36. lfs_off_t off;
  37. lfs_disk_struct lfs_disk_entry {
  38. uint16_t type;
  39. uint16_t len;
  40. union {
  41. lfs_disk_struct {
  42. lfs_ino_t head;
  43. lfs_size_t size;
  44. char name[LFS_NAME_MAX];
  45. } file;
  46. lfs_disk_struct {
  47. lfs_ino_t dir[2];
  48. char name[LFS_NAME_MAX];
  49. } dir;
  50. lfs_disk_struct {
  51. char magic[4];
  52. uint32_t read_size;
  53. uint32_t write_size;
  54. uint32_t erase_size;
  55. uint32_t erase_count;
  56. } superblock;
  57. } value;
  58. } d;
  59. } lfs_entry_t;
  60. typedef struct lfs_superblock {
  61. lfs_ino_t pair[2];
  62. lfs_disk_struct lfs_disk_superblock {
  63. lfs_word_t rev;
  64. uint32_t size;
  65. lfs_ino_t root[2];
  66. char magic[8];
  67. uint32_t block_size;
  68. uint32_t block_count;
  69. } d;
  70. } lfs_superblock_t;
  71. // Little filesystem type
  72. typedef struct lfs {
  73. lfs_bd_t *bd;
  74. const struct lfs_bd_ops *ops;
  75. lfs_free_t free;
  76. struct lfs_bd_info info;
  77. } lfs_t;
  78. // Functions
  79. lfs_error_t lfs_create(lfs_t *lfs, lfs_bd_t *bd, const struct lfs_bd_ops *bd_ops);
  80. lfs_error_t lfs_format(lfs_t *lfs);
  81. lfs_error_t lfs_mount(lfs_t *lfs);
  82. #endif