storage.h 11 KB

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