storage.h 9.2 KB

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