storage.h 13 KB

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