filesystem-api.h 10 KB

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