filesystem_api_internal.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include <furi.h>
  3. #include "filesystem_api_defines.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /** File type */
  8. typedef enum {
  9. FileTypeClosed, /**< Closed file */
  10. FileTypeOpenDir, /**< Open dir */
  11. FileTypeOpenFile, /**< Open file */
  12. } FileType;
  13. /** Structure that hold file index and returned api errors */
  14. struct File {
  15. uint32_t file_id; /**< File ID for internal references */
  16. FileType type;
  17. FS_Error error_id; /**< Standart API error from FS_Error enum */
  18. int32_t internal_error_id; /**< Internal API error value */
  19. void* storage;
  20. };
  21. /** File api structure
  22. * @var FS_File_Api::open
  23. * @brief Open file
  24. * @param file pointer to file object, filled by api
  25. * @param path path to file
  26. * @param access_mode access mode from FS_AccessMode
  27. * @param open_mode open mode from FS_OpenMode
  28. * @return success flag
  29. *
  30. * @var FS_File_Api::close
  31. * @brief Close file
  32. * @param file pointer to file object
  33. * @return success flag
  34. *
  35. * @var FS_File_Api::read
  36. * @brief Read bytes from file to buffer
  37. * @param file pointer to file object
  38. * @param buff pointer to buffer for reading
  39. * @param bytes_to_read how many bytes to read, must be smaller or equal to buffer size
  40. * @return how many bytes actually has been read
  41. *
  42. * @var FS_File_Api::write
  43. * @brief Write bytes from buffer to file
  44. * @param file pointer to file object
  45. * @param buff pointer to buffer for writing
  46. * @param bytes_to_read how many bytes to write, must be smaller or equal to buffer size
  47. * @return how many bytes actually has been written
  48. *
  49. * @var FS_File_Api::seek
  50. * @brief Move r/w pointer
  51. * @param file pointer to file object
  52. * @param offset offset to move r/w pointer
  53. * @param from_start set offset from start, or from current position
  54. * @return success flag
  55. *
  56. * @var FS_File_Api::tell
  57. * @brief Get r/w pointer position
  58. * @param file pointer to file object
  59. * @return current r/w pointer position
  60. *
  61. * @var FS_File_Api::truncate
  62. * @brief Truncate file size to current r/w pointer position
  63. * @param file pointer to file object
  64. * @return success flag
  65. *
  66. * @var FS_File_Api::size
  67. * @brief Fet file size
  68. * @param file pointer to file object
  69. * @return file size
  70. *
  71. * @var FS_File_Api::sync
  72. * @brief Write file cache to storage
  73. * @param file pointer to file object
  74. * @return success flag
  75. *
  76. * @var FS_File_Api::eof
  77. * @brief Checks that the r/w pointer is at the end of the file
  78. * @param file pointer to file object
  79. * @return end of file flag
  80. */
  81. typedef struct {
  82. bool (*const open)(
  83. void* context,
  84. File* file,
  85. const char* path,
  86. FS_AccessMode access_mode,
  87. FS_OpenMode open_mode);
  88. bool (*const close)(void* context, File* file);
  89. uint16_t (*read)(void* context, File* file, void* buff, uint16_t bytes_to_read);
  90. uint16_t (*write)(void* context, File* file, const void* buff, uint16_t bytes_to_write);
  91. bool (*const seek)(void* context, File* file, uint32_t offset, bool from_start);
  92. uint64_t (*tell)(void* context, File* file);
  93. bool (*const truncate)(void* context, File* file);
  94. uint64_t (*size)(void* context, File* file);
  95. bool (*const sync)(void* context, File* file);
  96. bool (*const eof)(void* context, File* file);
  97. } FS_File_Api;
  98. /** Dir api structure
  99. * @var FS_Dir_Api::open
  100. * @brief Open directory to get objects from
  101. * @param file pointer to file object, filled by api
  102. * @param path path to directory
  103. * @return success flag
  104. *
  105. * @var FS_Dir_Api::close
  106. * @brief Close directory
  107. * @param file pointer to file object
  108. * @return success flag
  109. *
  110. * @var FS_Dir_Api::read
  111. * @brief Read next object info in directory
  112. * @param file pointer to file object
  113. * @param fileinfo pointer to read FileInfo, can be NULL
  114. * @param name pointer to name buffer, can be NULL
  115. * @param name_length name buffer length
  116. * @return success flag (if next object not exist also returns false and set error_id to FSE_NOT_EXIST)
  117. *
  118. * @var FS_Dir_Api::rewind
  119. * @brief Rewind to first object info in directory
  120. * @param file pointer to file object
  121. * @return success flag
  122. */
  123. typedef struct {
  124. bool (*const open)(void* context, File* file, const char* path);
  125. bool (*const close)(void* context, File* file);
  126. bool (*const read)(
  127. void* context,
  128. File* file,
  129. FileInfo* fileinfo,
  130. char* name,
  131. uint16_t name_length);
  132. bool (*const rewind)(void* context, File* file);
  133. } FS_Dir_Api;
  134. /** Common api structure
  135. * @var FS_Common_Api::stat
  136. * @brief Open directory to get objects from
  137. * @param path path to file/directory
  138. * @param fileinfo pointer to read FileInfo, can be NULL
  139. * @param name pointer to name buffer, can be NULL
  140. * @param name_length name buffer length
  141. * @return FS_Error error info
  142. *
  143. * @var FS_Common_Api::remove
  144. * @brief Remove file/directory from storage,
  145. * directory must be empty,
  146. * file/directory must not be opened,
  147. * file/directory must not have FSF_READ_ONLY flag
  148. * @param path path to file/directory
  149. * @return FS_Error error info
  150. *
  151. * @var FS_Common_Api::mkdir
  152. * @brief Create new directory
  153. * @param path path to new directory
  154. * @return FS_Error error info
  155. *
  156. * @var FS_Common_Api::fs_info
  157. * @brief Get total and free space storage values
  158. * @param fs_path path of fs
  159. * @param total_space pointer to total space value
  160. * @param free_space pointer to free space value
  161. * @return FS_Error error info
  162. */
  163. typedef struct {
  164. FS_Error (*const stat)(void* context, const char* path, FileInfo* fileinfo);
  165. FS_Error (*const remove)(void* context, const char* path);
  166. FS_Error (*const mkdir)(void* context, const char* path);
  167. FS_Error (*const fs_info)(
  168. void* context,
  169. const char* fs_path,
  170. uint64_t* total_space,
  171. uint64_t* free_space);
  172. } FS_Common_Api;
  173. /** Full filesystem api structure */
  174. typedef struct {
  175. const FS_File_Api file;
  176. const FS_Dir_Api dir;
  177. const FS_Common_Api common;
  178. } FS_Api;
  179. #ifdef __cplusplus
  180. }
  181. #endif