filesystem-api-internal.h 6.6 KB

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