storage.h 9.9 KB

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