storage.h 8.8 KB

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