storage.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. #include <furi.h>
  3. #include "filesystem-api-defines.h"
  4. #include "storage-sd-api.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. typedef struct Storage Storage;
  9. /** Allocates and initializes a file descriptor
  10. * @return File*
  11. */
  12. File* storage_file_alloc(Storage* storage);
  13. /** Frees the file descriptor. Closes the file if it was open.
  14. */
  15. void storage_file_free(File* file);
  16. /******************* File Functions *******************/
  17. /** Opens an existing file or create a new one.
  18. * @param file pointer to file object.
  19. * @param path path to file
  20. * @param access_mode access mode from FS_AccessMode
  21. * @param open_mode open mode from FS_OpenMode
  22. * @return success flag. You need to close the file even if the open operation failed.
  23. */
  24. bool storage_file_open(
  25. File* file,
  26. const char* path,
  27. FS_AccessMode access_mode,
  28. FS_OpenMode open_mode);
  29. /** Close the file.
  30. * @param file pointer to a file object, the file object will be freed.
  31. * @return success flag
  32. */
  33. bool storage_file_close(File* file);
  34. /** Tells if the file is open
  35. * @param file pointer to a file object
  36. * @return bool true if file is open
  37. */
  38. bool storage_file_is_open(File* file);
  39. /** Reads bytes from a file into a buffer
  40. * @param file pointer to file object.
  41. * @param buff pointer to a buffer, for reading
  42. * @param bytes_to_read how many bytes to read. Must be less than or equal to the size of the buffer.
  43. * @return uint16_t how many bytes were actually readed
  44. */
  45. uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read);
  46. /** Writes bytes from a buffer to a file
  47. * @param file pointer to file object.
  48. * @param buff pointer to buffer, for writing
  49. * @param bytes_to_write how many bytes to write. Must be less than or equal to the size of the buffer.
  50. * @return uint16_t how many bytes were actually written
  51. */
  52. uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write);
  53. /** Moves the r/w pointer
  54. * @param file pointer to file object.
  55. * @param offset offset to move the r/w pointer
  56. * @param from_start set an offset from the start or from the current position
  57. * @return success flag
  58. */
  59. bool storage_file_seek(File* file, uint32_t offset, bool from_start);
  60. /** Gets the position of the r/w pointer
  61. * @param file pointer to file object.
  62. * @return uint64_t position of the r/w pointer
  63. */
  64. uint64_t storage_file_tell(File* file);
  65. /** Truncates the file size to the current position of the r/w pointer
  66. * @param file pointer to file object.
  67. * @return bool success flag
  68. */
  69. bool storage_file_truncate(File* file);
  70. /** Gets the size of the file
  71. * @param file pointer to file object.
  72. * @return uint64_t size of the file
  73. */
  74. uint64_t storage_file_size(File* file);
  75. /** Writes file cache to storage
  76. * @param file pointer to file object.
  77. * @return bool success flag
  78. */
  79. bool storage_file_sync(File* file);
  80. /** Checks that the r/w pointer is at the end of the file
  81. * @param file pointer to file object.
  82. * @return bool success flag
  83. */
  84. bool storage_file_eof(File* file);
  85. /******************* Dir Functions *******************/
  86. /** Opens a directory to get objects from it
  87. * @param app pointer to the api
  88. * @param file pointer to file object.
  89. * @param path path to directory
  90. * @return bool success flag. You need to close the directory even if the open operation failed.
  91. */
  92. bool storage_dir_open(File* file, const char* path);
  93. /** Close the directory. Also free file handle structure and point it to the NULL.
  94. * @param file pointer to a file object.
  95. * @return bool success flag
  96. */
  97. bool storage_dir_close(File* file);
  98. /** Reads the next object in the directory
  99. * @param file pointer to file object.
  100. * @param fileinfo pointer to the readed FileInfo, may be NULL
  101. * @param name pointer to name buffer, may be NULL
  102. * @param name_length name buffer length
  103. * @return success flag (if the next object does not exist, it also returns false and sets the file error id to FSE_NOT_EXIST)
  104. */
  105. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
  106. /** Rewinds the read pointer to first item in the directory
  107. * @param file pointer to file object.
  108. * @return bool success flag
  109. */
  110. bool storage_dir_rewind(File* file);
  111. /******************* Common Functions *******************/
  112. /** Retrieves information about a file/directory
  113. * @param app pointer to the api
  114. * @param path path to file/directory
  115. * @param fileinfo pointer to the readed FileInfo, may be NULL
  116. * @return FS_Error operation result
  117. */
  118. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo);
  119. /** Removes a file/directory from the repository, the directory must be empty and the file/directory must not be open
  120. * @param app pointer to the api
  121. * @param path
  122. * @return FS_Error operation result
  123. */
  124. FS_Error storage_common_remove(Storage* storage, const char* path);
  125. /** Renames file/directory, file/directory must not be open
  126. * @param app pointer to the api
  127. * @param old_path old path
  128. * @param new_path new path
  129. * @return FS_Error operation result
  130. */
  131. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path);
  132. /** Copy file, file must not be open
  133. * @param app pointer to the api
  134. * @param old_path old path
  135. * @param new_path new path
  136. * @return FS_Error operation result
  137. */
  138. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path);
  139. /** Creates a directory
  140. * @param app pointer to the api
  141. * @param path directory path
  142. * @return FS_Error operation result
  143. */
  144. FS_Error storage_common_mkdir(Storage* storage, const char* path);
  145. /** Gets general information about the storage
  146. * @param app pointer to the api
  147. * @param fs_path the path to the storage of interest
  148. * @param total_space pointer to total space record, will be filled
  149. * @param free_space pointer to free space record, will be filled
  150. * @return FS_Error operation result
  151. */
  152. FS_Error storage_common_fs_info(
  153. Storage* storage,
  154. const char* fs_path,
  155. uint64_t* total_space,
  156. uint64_t* free_space);
  157. /******************* Error Functions *******************/
  158. /** Retrieves the error text from the error id
  159. * @param error_id error id
  160. * @return const char* error text
  161. */
  162. const char* storage_error_get_desc(FS_Error error_id);
  163. /** Retrieves the error id from the file object
  164. * @param file pointer to file object. Pointer must not point to NULL. YOU CANNOT RETREIVE THE ERROR ID IF THE FILE HAS BEEN CLOSED
  165. * @return FS_Error error id
  166. */
  167. FS_Error storage_file_get_error(File* file);
  168. /** Retrieves the error text from the file object
  169. * @param file pointer to file object. Pointer must not point to NULL. YOU CANNOT RETREIVE THE ERROR TEXT IF THE FILE HAS BEEN CLOSED
  170. * @return const char* error text
  171. */
  172. const char* storage_file_get_error_desc(File* file);
  173. /******************* SD Card Functions *******************/
  174. /** Formats SD Card
  175. * @param api pointer to the api
  176. * @return FS_Error operation result
  177. */
  178. FS_Error storage_sd_format(Storage* api);
  179. /** Will unmount the SD card
  180. * @param api pointer to the api
  181. * @return FS_Error operation result
  182. */
  183. FS_Error storage_sd_unmount(Storage* api);
  184. /** Retrieves SD card information
  185. * @param api pointer to the api
  186. * @param info pointer to the info
  187. * @return FS_Error operation result
  188. */
  189. FS_Error storage_sd_info(Storage* api, SDInfo* info);
  190. /** Retrieves SD card status
  191. * @param api pointer to the api
  192. * @return FS_Error operation result
  193. */
  194. FS_Error storage_sd_status(Storage* api);
  195. #ifdef __cplusplus
  196. }
  197. #endif