filesystem-api.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #pragma once
  2. #include <furi.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /**
  7. * @brief Access mode flags
  8. */
  9. typedef enum {
  10. FSAM_READ = (1 << 0), /**< Read access */
  11. FSAM_WRITE = (1 << 1), /**< Write access */
  12. } FS_AccessMode;
  13. /**
  14. * @brief Open mode flags
  15. */
  16. typedef enum {
  17. FSOM_OPEN_EXISTING = 1, /**< Open file, fail if file doesn't exist */
  18. FSOM_OPEN_ALWAYS = 2, /**< Open file. Create new file if not exist */
  19. FSOM_OPEN_APPEND = 4, /**< Open file. Create new file if not exist. Set R/W pointer to EOF */
  20. FSOM_CREATE_NEW = 8, /**< Creates a new file. Fails if the file is exist */
  21. FSOM_CREATE_ALWAYS = 16, /**< Creates a new file. If file exist, truncate to zero size */
  22. } FS_OpenMode;
  23. /**
  24. * @brief API errors enumeration
  25. */
  26. typedef enum {
  27. FSE_OK, /**< No error */
  28. FSE_NOT_READY, /**< FS not ready */
  29. FSE_EXIST, /**< File/Dir alrady exist */
  30. FSE_NOT_EXIST, /**< File/Dir does not exist */
  31. FSE_INVALID_PARAMETER, /**< Invalid API parameter */
  32. FSE_DENIED, /**< Access denied */
  33. FSE_INVALID_NAME, /**< Invalid name/path */
  34. FSE_INTERNAL, /**< Internal error */
  35. FSE_NOT_IMPLEMENTED, /**< Functon not implemented */
  36. } FS_Error;
  37. /**
  38. * @brief FileInfo flags
  39. */
  40. typedef enum {
  41. FSF_READ_ONLY = (1 << 0), /**< Readonly */
  42. FSF_HIDDEN = (1 << 1), /**< Hidden */
  43. FSF_SYSTEM = (1 << 2), /**< System */
  44. FSF_DIRECTORY = (1 << 3), /**< Directory */
  45. FSF_ARCHIVE = (1 << 4), /**< Archive */
  46. } FS_Flags;
  47. /**
  48. * @brief Structure that hold file index and returned api errors
  49. */
  50. typedef struct {
  51. uint32_t file_id; /**< File ID for internal references */
  52. FS_Error error_id; /**< Standart API error from FS_Error enum */
  53. uint32_t internal_error_id; /**< Internal API error value */
  54. } File;
  55. // TODO: solve year 2107 problem
  56. /**
  57. * @brief Structure that hold packed date values
  58. */
  59. typedef struct __attribute__((packed)) {
  60. uint16_t month_day : 5; /**< month day */
  61. uint16_t month : 4; /**< month index */
  62. uint16_t year : 7; /**< year, year + 1980 to get actual value */
  63. } FileDate;
  64. /**
  65. * @brief Structure that hold packed time values
  66. */
  67. typedef struct __attribute__((packed)) {
  68. uint16_t second : 5; /**< second, second * 2 to get actual value */
  69. uint16_t minute : 6; /**< minute */
  70. uint16_t hour : 5; /**< hour */
  71. } FileTime;
  72. /**
  73. * @brief Union of simple date and real value
  74. */
  75. typedef union {
  76. FileDate simple; /**< simple access to date */
  77. uint16_t value; /**< real date value */
  78. } FileDateUnion;
  79. /**
  80. * @brief Union of simple time and real value
  81. */
  82. typedef union {
  83. FileTime simple; /**< simple access to time */
  84. uint16_t value; /**< real time value */
  85. } FileTimeUnion;
  86. /**
  87. * @brief Structure that hold file info
  88. */
  89. typedef struct {
  90. uint8_t flags; /**< flags from FS_Flags enum */
  91. uint64_t size; /**< file size */
  92. FileDateUnion date; /**< file date */
  93. FileTimeUnion time; /**< file time */
  94. } FileInfo;
  95. /** @struct FS_File_Api
  96. * @brief File api structure
  97. *
  98. * @var FS_File_Api::open
  99. * @brief Open file
  100. * @param file pointer to file object, filled by api
  101. * @param path path to file
  102. * @param access_mode access mode from FS_AccessMode
  103. * @param open_mode open mode from FS_OpenMode
  104. * @return success flag
  105. *
  106. * @var FS_File_Api::close
  107. * @brief Close file
  108. * @param file pointer to file object
  109. * @return success flag
  110. *
  111. * @var FS_File_Api::read
  112. * @brief Read bytes from file to buffer
  113. * @param file pointer to file object
  114. * @param buff pointer to buffer for reading
  115. * @param bytes_to_read how many bytes to read, must be smaller or equal to buffer size
  116. * @return how many bytes actually has been readed
  117. *
  118. * @var FS_File_Api::write
  119. * @brief Write bytes from buffer to file
  120. * @param file pointer to file object
  121. * @param buff pointer to buffer for writing
  122. * @param bytes_to_read how many bytes to write, must be smaller or equal to buffer size
  123. * @return how many bytes actually has been writed
  124. *
  125. * @var FS_File_Api::seek
  126. * @brief Move r/w pointer
  127. * @param file pointer to file object
  128. * @param offset offset to move r/w pointer
  129. * @param from_start set offset from start, or from current position
  130. * @return success flag
  131. *
  132. * @var FS_File_Api::tell
  133. * @brief Get r/w pointer position
  134. * @param file pointer to file object
  135. * @return current r/w pointer position
  136. *
  137. * @var FS_File_Api::truncate
  138. * @brief Truncate file size to current r/w pointer position
  139. * @param file pointer to file object
  140. * @return success flag
  141. *
  142. * @var FS_File_Api::size
  143. * @brief Fet file size
  144. * @param file pointer to file object
  145. * @return file size
  146. *
  147. * @var FS_File_Api::sync
  148. * @brief Write file cache to storage
  149. * @param file pointer to file object
  150. * @return success flag
  151. *
  152. * @var FS_File_Api::eof
  153. * @brief Checks that the r/w pointer is at the end of the file
  154. * @param file pointer to file object
  155. * @return end of file flag
  156. */
  157. /**
  158. * @brief File api structure
  159. */
  160. typedef struct {
  161. bool (*open)(File* file, const char* path, FS_AccessMode access_mode, FS_OpenMode open_mode);
  162. bool (*close)(File* file);
  163. uint16_t (*read)(File* file, void* buff, uint16_t bytes_to_read);
  164. uint16_t (*write)(File* file, void* buff, uint16_t bytes_to_write);
  165. bool (*seek)(File* file, uint32_t offset, bool from_start);
  166. uint64_t (*tell)(File* file);
  167. bool (*truncate)(File* file);
  168. uint64_t (*size)(File* file);
  169. bool (*sync)(File* file);
  170. bool (*eof)(File* file);
  171. } FS_File_Api;
  172. /** @struct FS_Dir_Api
  173. * @brief Dir api structure
  174. *
  175. * @var FS_Dir_Api::open
  176. * @brief Open directory to get objects from
  177. * @param file pointer to file object, filled by api
  178. * @param path path to directory
  179. * @return success flag
  180. *
  181. * @var FS_Dir_Api::close
  182. * @brief Close directory
  183. * @param file pointer to file object
  184. * @return success flag
  185. *
  186. * @var FS_Dir_Api::read
  187. * @brief Read next object info in directory
  188. * @param file pointer to file object
  189. * @param fileinfo pointer to readed FileInfo, can be NULL
  190. * @param name pointer to name buffer, can be NULL
  191. * @param name_length name buffer length
  192. * @return success flag (if next object not exist also returns false and set error_id to FSE_NOT_EXIST)
  193. *
  194. * @var FS_Dir_Api::rewind
  195. * @brief Rewind to first object info in directory
  196. * @param file pointer to file object
  197. * @return success flag
  198. */
  199. /**
  200. * @brief Dir api structure
  201. */
  202. typedef struct {
  203. bool (*open)(File* file, const char* path);
  204. bool (*close)(File* file);
  205. bool (*read)(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
  206. bool (*rewind)(File* file);
  207. } FS_Dir_Api;
  208. /** @struct FS_Common_Api
  209. * @brief Common api structure
  210. *
  211. * @var FS_Common_Api::info
  212. * @brief Open directory to get objects from
  213. * @param path path to file/directory
  214. * @param fileinfo pointer to readed FileInfo, can be NULL
  215. * @param name pointer to name buffer, can be NULL
  216. * @param name_length name buffer length
  217. * @return FS_Error error info
  218. *
  219. * @var FS_Common_Api::remove
  220. * @brief Remove file/directory from storage,
  221. * directory must be empty,
  222. * file/directory must not be opened,
  223. * file/directory must not have FSF_READ_ONLY flag
  224. * @param path path to file/directory
  225. * @return FS_Error error info
  226. *
  227. * @var FS_Common_Api::rename
  228. * @brief Rename file/directory,
  229. * file/directory must not be opened
  230. * @param path path to file/directory
  231. * @return FS_Error error info
  232. *
  233. * @var FS_Common_Api::set_attr
  234. * @brief Set attributes of file/directory,
  235. * for example:
  236. * @code
  237. * set "read only" flag and remove "hidden" flag
  238. * set_attr("file.txt", FSF_READ_ONLY, FSF_READ_ONLY | FSF_HIDDEN);
  239. * @endcode
  240. * @param path path to file/directory
  241. * @param attr attribute values consist of FS_Flags
  242. * @param mask attribute mask consist of FS_Flags
  243. * @return FS_Error error info
  244. *
  245. * @var FS_Common_Api::mkdir
  246. * @brief Create new directory
  247. * @param path path to new directory
  248. * @return FS_Error error info
  249. *
  250. * @var FS_Common_Api::set_time
  251. * @brief Set file/directory modification time
  252. * @param path path to file/directory
  253. * @param date modification date
  254. * @param time modification time
  255. * @see FileDateUnion
  256. * @see FileTimeUnion
  257. * @return FS_Error error info
  258. *
  259. * @var FS_Common_Api::get_fs_info
  260. * @brief Get total and free space storage values
  261. * @param total_space pointer to total space value
  262. * @param free_space pointer to free space value
  263. * @return FS_Error error info
  264. */
  265. /**
  266. * @brief Common api structure
  267. */
  268. typedef struct {
  269. FS_Error (*info)(const char* path, FileInfo* fileinfo, char* name, const uint16_t name_length);
  270. FS_Error (*remove)(const char* path);
  271. FS_Error (*rename)(const char* old_path, const char* new_path);
  272. FS_Error (*set_attr)(const char* path, uint8_t attr, uint8_t mask);
  273. FS_Error (*mkdir)(const char* path);
  274. FS_Error (*set_time)(const char* path, FileDateUnion date, FileTimeUnion time);
  275. FS_Error (*get_fs_info)(uint64_t* total_space, uint64_t* free_space);
  276. } FS_Common_Api;
  277. /** @struct FS_Error_Api
  278. * @brief Errors api structure
  279. *
  280. * @var FS_Error_Api::get_desc
  281. * @brief Get error description text
  282. * @param error_id FS_Error error id (for fire/dir functions result can be obtained from File.error_id)
  283. * @return pointer to description text
  284. *
  285. * @var FS_Error_Api::get_internal_desc
  286. * @brief Get internal error description text
  287. * @param internal_error_id error id (for fire/dir functions result can be obtained from File.internal_error_id)
  288. * @return pointer to description text
  289. */
  290. /**
  291. * @brief Errors api structure
  292. */
  293. typedef struct {
  294. const char* (*get_desc)(FS_Error error_id);
  295. const char* (*get_internal_desc)(uint32_t internal_error_id);
  296. } FS_Error_Api;
  297. /**
  298. * @brief Full filesystem api structure
  299. */
  300. typedef struct {
  301. FS_File_Api file;
  302. FS_Dir_Api dir;
  303. FS_Common_Api common;
  304. FS_Error_Api error;
  305. } FS_Api;
  306. #ifdef __cplusplus
  307. }
  308. #endif