lfs.h 1.9 KB

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