storage.h 12 KB

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