storage.h 11 KB

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