sffs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef __SFFS_H__
  13. #define __SFFS_H__
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <vfs.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct blockDevice;
  22. /**
  23. * opaque data structure of SFFS
  24. */
  25. typedef struct sffsFs sffsFs_t;
  26. /**
  27. * create a SFFS instance
  28. *
  29. * @param pdev pointer of block device
  30. * @param block_cache_count block cache count
  31. * @param read_only whether to mount as read-only
  32. * @return
  33. * - SFFS pointer on success
  34. * - NULL on failure, typical failure is root block mismatch
  35. */
  36. sffsFs_t *sffsMount(struct blockDevice *pdev,
  37. size_t block_cache_count, bool read_only);
  38. /**
  39. * delete the SFFS instance
  40. *
  41. * @param fs SFFS pointer
  42. * @return
  43. * - 0 on success
  44. */
  45. int sffsUnmount(sffsFs_t *fs);
  46. /**
  47. * remount SFFS with flags
  48. *
  49. * The only supported flag is \a MS_RDONLY.
  50. *
  51. * @param fs SFFS pointer
  52. * @param flags remount flags
  53. * @return
  54. * - 0 for success
  55. */
  56. int sffsRemount(sffsFs_t *fs, unsigned flags);
  57. /**
  58. * format SFFS on block device
  59. *
  60. * @param pdev block device pointer
  61. * @return
  62. * - 0 for success
  63. * - -EINVAL: invalid parameter
  64. * - -ENOMEM: out of memory
  65. */
  66. int sffsMakeFs(struct blockDevice *pdev);
  67. /**
  68. * refer to open(2)
  69. *
  70. * It is permitted to open one file multiple times.
  71. *
  72. * It is not supported to open an existed directory.
  73. *
  74. * @param fs SFFS pointer
  75. * @param path path in file system
  76. * @param mode open mode (O_CREAT, O_ACCMODE, O_TRUNC)
  77. * @return
  78. * - file descriptor on success
  79. * - -ENOENT: not exist, and without O_CREAT
  80. * - -EROFS: create on read-only file system
  81. * - -ENOTDIR: parent directory not exist at create
  82. * - -ENOSPC: out of space
  83. * - -ENAMETOOLONG: name is too long
  84. * - -ENOMEM: out of memory
  85. */
  86. int sffsOpen(sffsFs_t *fs, const char *path, int mode);
  87. /**
  88. * refer to close(2)
  89. *
  90. * @param fs SFFS pointer
  91. * @param fd file descriptor
  92. * @return
  93. * - 0 on success
  94. * - -EINVAL: invalid SFFS pointer
  95. * - -EBADF: invalid file descriptor
  96. */
  97. int sffsClose(sffsFs_t *fs, int fd);
  98. /**
  99. * refer to write(2)
  100. *
  101. * On failure, the return value is the negative errno. Written bytes won't
  102. * be returned.
  103. *
  104. * @param fs SFFS pointer
  105. * @param fd file descriptor
  106. * @param data data pointer to be written
  107. * @param size data size to be written
  108. * @return
  109. * - written size on success
  110. * - -EINVAL: invalid SFFS pointer
  111. * - -EBADF: invalid file descriptor
  112. * - -EROFS: write on read-only file system
  113. * - -ENOSPC: out of space
  114. * - -EOVERFLOW: exceed maximum large file size
  115. */
  116. ssize_t sffsWrite(sffsFs_t *fs, int fd, const void *data, size_t size);
  117. /**
  118. * refer to read(2)
  119. *
  120. * When there are no enough size in the file, the read size is returned.
  121. * On other failures, the return value is the negative errno. Read bytes
  122. * won't be returned.
  123. *
  124. * @param fs SFFS pointer
  125. * @param fd file descriptor
  126. * @param data data pointer to be read
  127. * @param size data size to be read
  128. * @return
  129. * - read size on success
  130. * - -EINVAL: invalid SFFS pointer
  131. * - -EBADF: invalid file descriptor
  132. */
  133. ssize_t sffsRead(sffsFs_t *fs, int fd, void *data, size_t size);
  134. /**
  135. * refer to lseek(2)
  136. *
  137. * When the seek position is larger than file size, the position will be
  138. * set to end of file silently.
  139. *
  140. * @param fs SFFS pointer
  141. * @param fd file descriptor
  142. * @param offset seek offset
  143. * @param mode seek mode (SEEK_SET, SEEK_CUR, SEEK_END)
  144. * @return
  145. * - 0 on success
  146. * - -EINVAL: invalid SFFS pointer, or invalid seek mode
  147. * - -EBADF: invalid file descriptor
  148. */
  149. off_t sffsSeek(sffsFs_t *fs, int fd, off_t offset, int mode);
  150. /**
  151. * refer to ftruncate(2)
  152. *
  153. * Support both increase and decrease file size. At increase file size,
  154. * the extended part is undefined.
  155. *
  156. * @param fs SFFS pointer
  157. * @param fd file descriptor
  158. * @param length truncate new length
  159. * @return
  160. * - 0 on success
  161. * - -EINVAL: invalid SFFS pointer
  162. * - -EBADF: invalid file descriptor
  163. * - -EROFS: write on read-only file system
  164. * - -EACCES: file descriptor is opened as read-only
  165. * - -ENOSPC: out of space
  166. * - -EOVERFLOW: exceed maximum large file size
  167. */
  168. int sffsFtruncate(sffsFs_t *fs, int fd, off_t length);
  169. /**
  170. * refer to fstat(2)
  171. *
  172. * @param fs SFFS pointer
  173. * @param fd file descriptor
  174. * @param st output stat pointer
  175. * @return
  176. * - 0 on success
  177. * - -EINVAL: invalid SFFS pointer, or invalid output pointer
  178. * - -EBADF: invalid file descriptor
  179. */
  180. int sffsFstat(sffsFs_t *fs, int fd, struct stat *st);
  181. /**
  182. * refer to unlink(2)
  183. *
  184. * @param fs SFFS pointer
  185. * @param name file path in file system
  186. * @return
  187. * - 0 on success
  188. * - -EINVAL: invalid SFFS pointer
  189. * - -EROFS: write on read-only file system
  190. * - -ENOENT: file not exist
  191. * - -EBUSY: file is opened
  192. */
  193. int sffsUnlink(sffsFs_t *fs, const char *name);
  194. /**
  195. * refer to rename(2)
  196. *
  197. * Support 3 kinds of rename:
  198. * - rename file to an existed directory, but destination file not exist
  199. * - rename file to an existed file
  200. * - rename directory to an existed empty directory
  201. *
  202. * @param fs SFFS pointer
  203. * @param src source file or directory path in file system
  204. * @param dst destination file or directory path in file system
  205. * @return
  206. * - 0 on success
  207. * - -EINVAL: invalid SFFS pointer
  208. * - -EROFS: read-only file system
  209. * - -ENOENT: source not exist
  210. * - -EBUSY: source file or directory is opened
  211. * - -ENOTDIR:
  212. * - source is file, and destination in non-exist directory
  213. * - source is directory, and destination is not existed directory
  214. * - -EISDIR: source is file, and destination is existed directory
  215. * - -ENOTEMPTY: source is directory, and destination is not empty
  216. * - -ENAMETOOLONG: destination name is too long
  217. */
  218. int sffsRename(sffsFs_t *fs, const char *src, const char *dst);
  219. /**
  220. * refer to statvfs(3)
  221. *
  222. * @param fs SFFS pointer
  223. * @param buf output pointer
  224. * @return
  225. * - 0 on success
  226. * - -EINVAL: invalid SFFS pointer, or output pointer
  227. */
  228. int sffsStatVfs(sffsFs_t *fs, struct statvfs *buf);
  229. /**
  230. * refer to sync(2)
  231. *
  232. * @param fs SFFS pointer
  233. * @return
  234. * - 0 on success
  235. * - -EINVAL: invalid SFFS pointer
  236. */
  237. int sffsSync(sffsFs_t *fs);
  238. /**
  239. * refer to mkdir(2)
  240. *
  241. * @param fs SFFS pointer
  242. * @param name directory path in file system
  243. * @return
  244. * - 0 on success
  245. * - -EINVAL: invalid SFFS pointer
  246. * - -EROFS: read-only file system
  247. * - -EEXIST: the path is existed
  248. * - -ENOTDIR: parent of the path not exist
  249. * - -ENOSPC: out of space
  250. * - -ENAMETOOLONG: destination name is too long
  251. */
  252. int sffsMkDir(sffsFs_t *fs, const char *name);
  253. /**
  254. * refer to rmdir(2)
  255. *
  256. * @param fs SFFS pointer
  257. * @param name directory path in file system
  258. * @return
  259. * - 0 on success
  260. * - -EINVAL: invalid SFFS pointer
  261. * - -EROFS: read-only file system
  262. * - -EACCES: can't remove file system root directory
  263. * - -ENOENT: path not exists
  264. * - -ENOTDIR: parent of path not exists
  265. * - -EBUSY: path is opened
  266. * - -ENOTEMPTY: directory is not empty
  267. */
  268. int sffsRmDir(sffsFs_t *fs, const char *name);
  269. /**
  270. * refer to opendir(3)
  271. *
  272. * @param fs SFFS pointer
  273. * @param name directory path in file system
  274. * @return
  275. * - DIR pointer on success
  276. * - NULL on fail
  277. */
  278. DIR *sffsOpenDir(sffsFs_t *fs, const char *name);
  279. /**
  280. * refer to closedir(3)
  281. *
  282. * @param fs SFFS pointer
  283. * @param dir DIR pointer to be closed
  284. * @return
  285. * - 0 on success
  286. * - -EINVAL: invalid SFFS pointer, or DIR pointer
  287. */
  288. int sffsCloseDir(sffsFs_t *fs, DIR *dir);
  289. /**
  290. * refer to readdir_r(3)
  291. *
  292. * At the end of directory, the return value is 0, and \a *result will be
  293. * NULL.
  294. *
  295. * @param fs SFFS pointer
  296. * @param dir DIR pointer to be read
  297. * @param entry output dirent pointer
  298. * @param result output pointer of dirent pointer
  299. * @return
  300. * - 0 on success
  301. * - -EINVAL: invalid SFFS pointer, or DIR pointer, or entry pointer
  302. * - -EBADF: invalid DIR
  303. */
  304. int sffsReadDirR(sffsFs_t *fs, DIR *dir, struct dirent *entry, struct dirent **result);
  305. /**
  306. * refer to readdir(3)
  307. *
  308. * @param fs SFFS pointer
  309. * @param dir DIR pointer to be read
  310. * @return
  311. * - dirent pointer on success
  312. * - NULL on fail, or end of directory
  313. */
  314. struct dirent *sffsReadDir(sffsFs_t *fs, DIR *dir);
  315. /**
  316. * refer to telldir(3)
  317. *
  318. * @param fs SFFS pointer
  319. * @param dir DIR pointer to be read
  320. * @return
  321. * - current location on success
  322. * - -EINVAL: invalid SFFS pointer, or DIR pointer
  323. */
  324. long sffsTellDir(sffsFs_t *fs, DIR *dir);
  325. /**
  326. * refer to seekdir(3)
  327. *
  328. * @param fs SFFS pointer
  329. * @param dir DIR pointer to be read
  330. * @param loc seek location
  331. */
  332. void sffsSeekDir(sffsFs_t *fs, DIR *dir, long loc);
  333. /**
  334. * write all content to a file in safe mode
  335. *
  336. * Comparing to \p sffsFileWrite, when the file already exists, and there
  337. * are power failure during write, either the file content to the new data,
  338. * or the content is kept as original.
  339. *
  340. * Due to the original file won't be deleted before the new data are
  341. * written, there should exist enough free blocks for the whole new data.
  342. *
  343. * @param fs SFFS pointer
  344. * @param path path in file system
  345. * @param data data pointer to be written
  346. * @param size data size to be written
  347. * @return
  348. * - written size on success, it is the same as \p size parameter
  349. * - -EINVAL: invalid SFFS pointer
  350. * - -EROFS: create on read-only file system
  351. * - -ENOTDIR: parent directory not exist at create
  352. * - -EISDIR: path exists, but is directory
  353. * - -ENOSPC: out of space
  354. * - -ENAMETOOLONG: name is too long
  355. * - -EOVERFLOW: exceed maximum large file size
  356. */
  357. ssize_t sffsSfileWrite(sffsFs_t *fs, const char *path, const void *data, size_t size);
  358. /**
  359. * write all content to a file
  360. *
  361. * @param fs SFFS pointer
  362. * @param path path in file system
  363. * @param data data pointer to be written
  364. * @param size data size to be written
  365. * @return
  366. * - written size on success, it is the same as \p size parameter
  367. * - -EINVAL: invalid SFFS pointer
  368. * - -EROFS: create on read-only file system
  369. * - -ENOTDIR: parent directory not exist at create
  370. * - -EISDIR: path exists, but is directory
  371. * - -ENOSPC: out of space
  372. * - -ENAMETOOLONG: name is too long
  373. * - -EOVERFLOW: exceed maximum large file size
  374. */
  375. ssize_t sffsFileWrite(sffsFs_t *fs, const char *path, const void *data, size_t size);
  376. /**
  377. * estimate block count needed for specified file size
  378. *
  379. * @param fs SFFS pointer
  380. * @param size file size to be estimated
  381. * @return
  382. * - needed block count
  383. * - -EOVERFLOW: exceed maximum large file size
  384. */
  385. int sffsFileBlockNeeded(sffsFs_t *fs, size_t size);
  386. /**
  387. * block write count for statistic
  388. *
  389. * @param fs SFFS pointer
  390. * @return
  391. * - block write count
  392. * - -EINVAL: invalid SFFS pointer
  393. */
  394. int sffsBlockWriteCount(sffsFs_t *fs);
  395. /**
  396. * set reserved block count only for sffsSfileWrite
  397. *
  398. * Due to \p sffsSfileWrite will need extra blocks, this can set reserved
  399. * block count, which will only be used during \p sffsSfileWrite.
  400. *
  401. * In most cases, \p sffsSfileWrite is used for important information, and
  402. * failure is unacceptable. With proper reserved blocks, it won't fail due
  403. * to ENOSPC.
  404. *
  405. * Even current free block count is less than \p count, it will success.
  406. *
  407. * Another approach is to create independent partition for important files.
  408. *
  409. * @param fs SFFS pointer
  410. * @param count reserved block count only for sffsSfileWrite
  411. * @return
  412. * - 0 on success
  413. * - -EINVAL on invalid parameter
  414. */
  415. int sffsSetSfileReserveCount(sffsFs_t *fs, size_t count);
  416. #ifdef __cplusplus
  417. }
  418. #endif
  419. #endif //__SFFS_H__