vfs.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 _VFS_H_
  13. #define _VFS_H_
  14. #include <fcntl.h>
  15. #include <sys/stat.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * the maximum absolute file path including mount point, terminate \0
  21. */
  22. #define VFS_PATH_MAX (192)
  23. #define DT_UNKOWN 0 ///< unknown type
  24. #define DT_DIR 4 ///< directory
  25. #define DT_REG 8 ///< regular file
  26. /**
  27. * read only flag used in \p vfs_remount
  28. */
  29. #define MS_RDONLY 1
  30. /**
  31. * DIR data structure
  32. */
  33. typedef struct
  34. {
  35. int16_t fs_index; ///< internal fs index
  36. int16_t _reserved; ///< reserved
  37. } DIR;
  38. /**
  39. * dirent data structure
  40. */
  41. struct dirent
  42. {
  43. int d_ino; ///< inode number, file system implementation can use it for any purpose
  44. unsigned char d_type; ///< type of file
  45. char d_name[256]; ///< file name
  46. };
  47. struct statvfs
  48. {
  49. unsigned long f_bsize; /* file system block size */
  50. unsigned long f_frsize; /* fragment size */
  51. unsigned long f_blocks; /* size of fs in f_frsize units */
  52. unsigned long f_bfree; /* free blocks in fs */
  53. unsigned long f_bavail; /* free blocks avail to non-superuser */
  54. unsigned long f_files; /* total file nodes in file system */
  55. unsigned long f_ffree; /* free file nodes in fs */
  56. unsigned long f_favail; /* avail file nodes in fs */
  57. unsigned long f_fsid; /* file system id */
  58. unsigned long f_flag; /* mount flags */
  59. unsigned long f_namemax; /* maximum length of filenames */
  60. };
  61. /**
  62. * unmount file system by file path
  63. *
  64. * \p path must be absolute path, starting with '/'. It can be mount point
  65. * or arbitrary file or directory under the mount point.
  66. *
  67. * \param [in] path file path
  68. * \return
  69. * - 0 on success
  70. * - -1 on error
  71. * - EINVAL: invalid parameter
  72. * - ENOENT: there are no mount points
  73. */
  74. int vfs_umount(const char *path);
  75. /**
  76. * remount file system with flags
  77. *
  78. * The only supported flags are \p MS_READONLY.
  79. *
  80. * \p path must be a mount point.
  81. *
  82. * \param [in] path mount point path
  83. * \param [in] flags file system mount flags
  84. * \return
  85. * - 0 on success
  86. * - -1 on error
  87. * - ENOENT: \p path is not a mount point
  88. * - ENOSYS: the file system doesn't support remount
  89. * - others: refer to file system implementation
  90. */
  91. int vfs_remount(const char *path, unsigned flags);
  92. /**
  93. * open and possibly create a file
  94. *
  95. * It is the same as open a file with O_WRONLY, O_CREAT, O_TRUNC.
  96. *
  97. * Refer to man (2) creat.
  98. *
  99. * \param [in] path file path
  100. * \param [in] mode file mode, may be ignored by file system
  101. * implementation
  102. * \return
  103. * - file descriptor on success on success
  104. * - -1 on error. Refer to file system implementation for errno.
  105. */
  106. int vfs_creat(const char *path, mode_t mode);
  107. /**
  108. * open and possibly create a file
  109. *
  110. * Refer to man (2) open
  111. *
  112. * \param [in] path file path
  113. * \param [in] flags O_CREAT, O_ACCMODE, O_TRUNC
  114. * \param [in] mode file mode, may be ignored by file system
  115. * implementation
  116. * \return
  117. * - file descriptor on success on success
  118. * - -1 on error. Refer to file system implementation for errno.
  119. */
  120. int vfs_open(const char *path, int flags, ...);
  121. /**
  122. * close a file descriptor
  123. *
  124. * Refer to man (2) close
  125. *
  126. * \param [in] fd file descriptor
  127. * \return
  128. * - 0 on success
  129. * - -1 on error. Refer to file system implementation for errno.
  130. */
  131. int vfs_close(int fd);
  132. /**
  133. * read from a file descriptor
  134. *
  135. * Refer to man (2) read
  136. *
  137. * \param [in] fd file descriptor
  138. * \param [out] buf buf for read
  139. * \param [in] count byte count
  140. * \return
  141. * - the number of bytes read on success
  142. * - -1 on error. Refer to file system implementation for errno.
  143. */
  144. ssize_t vfs_read(int fd, void *buf, size_t count);
  145. /**
  146. * write to a file descriptor
  147. *
  148. * Refer to man (2) write
  149. *
  150. * \param [in] fd file descriptor
  151. * \param [in] buf buf for write
  152. * \param [in] count byte count
  153. * \return
  154. * - the number of bytes written on success
  155. * - -1 on error. Refer to file system implementation for errno.
  156. */
  157. ssize_t vfs_write(int fd, const void *buf, size_t count);
  158. /**
  159. * reposition read/write file offset
  160. *
  161. * Refer to man (2) lseek
  162. *
  163. * \param [in] fd file descriptor
  164. * \param [in] offset offset according to the directive whence
  165. * \param [in] whence SEEK_SET, SEEK_CUR, SEEK_END
  166. * \return
  167. * - offset location as measured in bytes from the beginning of
  168. * the file on success.
  169. * - -1 on error. Refer to file system implementation for errno.
  170. */
  171. long vfs_lseek(int fd, long offset, int whence);
  172. /**
  173. * get file status
  174. *
  175. * Refer to man (2) fstat
  176. *
  177. * \param [in] fd file descriptor
  178. * \param [out] st file status
  179. * \return
  180. * - 0 on success
  181. * - -1 on error. Refer to file system implementation for errno.
  182. */
  183. int vfs_fstat(int fd, struct stat *st);
  184. /**
  185. * get file status
  186. *
  187. * Refer to man (2) stat
  188. *
  189. * \param [in] path file path
  190. * \param [out] st file status
  191. * \return
  192. * - 0 on success
  193. * - -1 on error. Refer to file system implementation for errno.
  194. */
  195. int vfs_stat(const char *path, struct stat *st);
  196. /**
  197. * truncate a file to a specified length
  198. *
  199. * Refer to man (2) truncate
  200. *
  201. * \param [in] path file path
  202. * \param [in] length file length
  203. * \return
  204. * - 0 on success
  205. * - -1 on error. Refer to file system implementation for errno.
  206. */
  207. int vfs_truncate(const char *path, long length);
  208. /**
  209. * truncate a file to a specified length
  210. *
  211. * Refer to man (2) ftruncate
  212. *
  213. * \param [in] fd file descriptor
  214. * \param [in] length file length
  215. * \return
  216. * - 0 on success
  217. * - -1 on error. Refer to file system implementation for errno.
  218. */
  219. int vfs_ftruncate(int fd, long length);
  220. /**
  221. * truncate a file to a specified length
  222. *
  223. * Refer to man (2) link. It is just a placeholder, and no file systems
  224. * support it now.
  225. *
  226. * \param [in] oldpath old file path
  227. * \param [in] newpath new file path
  228. * \return
  229. * - -1 on error, errno is ENOSYS
  230. */
  231. int vfs_link(const char *oldpath, const char *newpath);
  232. /**
  233. * delete a name and possibly the file it refers to
  234. *
  235. * Refer to man (2) unlink
  236. *
  237. * \param [in] pathname file path name
  238. * \return
  239. * - 0 on success
  240. * - -1 on error. Refer to file system implementation for errno.
  241. */
  242. int vfs_unlink(const char *pathname);
  243. /**
  244. * change the name or location of a file
  245. *
  246. * Refer to man (2) rename
  247. *
  248. * \p oldpath and \p newpath must be on the same mounted file system.
  249. *
  250. * \param [in] oldpath old file path
  251. * \param [in] newpath new file path
  252. * \return
  253. * - 0 on success
  254. * - -1 on error. Refer to file system implementation for errno.
  255. * - EXDEV: oldpath and newpath are not on the same mounted
  256. * file system.
  257. */
  258. int vfs_rename(const char *oldpath, const char *newpath);
  259. /**
  260. * synchronize a file's in-core state with storage device
  261. *
  262. * Refer to man (2) fsync
  263. *
  264. * \param [in] fd file descriptor
  265. * \return
  266. * - 0 on success
  267. * - -1 on error. Refer to file system implementation for errno.
  268. */
  269. int vfs_fsync(int fd);
  270. /**
  271. * manipulate file descriptor
  272. *
  273. * Refer to man (2) fcntl. It is just a placeholder, and no file systems
  274. * support it now.
  275. *
  276. * \param [in] fd file descriptor
  277. * \param [in] cmd command
  278. * \return
  279. * - -1 on error, errno is ENOSYS
  280. */
  281. int vfs_fcntl(int fd, int cmd, ...);
  282. /**
  283. * control device
  284. *
  285. * Refer to man (2) ioctl. It is just a placeholder, and no file systems
  286. * support it now.
  287. *
  288. * \param [in] fd file descriptor
  289. * \param [in] cmd command
  290. * \return
  291. * - -1 on error, errno is ENOSYS
  292. */
  293. int vfs_ioctl(int fd, unsigned long request, ...);
  294. /**
  295. * open a directory
  296. *
  297. * Refer to man (3) opendir
  298. *
  299. * \param [in] name directory name
  300. * \return
  301. * - 0 on success
  302. * - -1 on error. Refer to file system implementation for errno.
  303. */
  304. DIR *vfs_opendir(const char *name);
  305. /**
  306. * read a directory
  307. *
  308. * Refer to man (3) readdir
  309. *
  310. * \param [in] dirp directory pointer returned by \p vfs_opendir
  311. * \return
  312. * - 0 on success
  313. * - -1 on error. Refer to file system implementation for errno.
  314. */
  315. struct dirent *vfs_readdir(DIR *dirp);
  316. /**
  317. * read a directory
  318. *
  319. * Refer to man (3) readdir_r
  320. *
  321. * \param [in] dirp directory pointer returned by \p vfs_opendir
  322. * \param [out] entry dirent buffer
  323. * \param [out] result pointer to dirent buffer
  324. * \return
  325. * - 0 on success
  326. * - -1 on error. Refer to file system implementation for errno.
  327. */
  328. int vfs_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
  329. /**
  330. * return current location in directory stream
  331. *
  332. * Refer to man (3) telldir
  333. *
  334. * \param [in] dirp directory pointer returned by \p vfs_opendir
  335. * \return
  336. * - 0 on success
  337. * - -1 on error. Refer to file system implementation for errno.
  338. */
  339. long vfs_telldir(DIR *pdir);
  340. /**
  341. * set the position of the next readdir() call in the directory stream
  342. *
  343. * Refer to man (3) seekdir
  344. *
  345. * \param [in] dirp directory pointer returned by \p vfs_opendir
  346. * \param [in] loc location returned by \p vfs_telldir
  347. * \return
  348. * - 0 on success
  349. * - -1 on error. Refer to file system implementation for errno.
  350. */
  351. void vfs_seekdir(DIR *pdir, long loc);
  352. /**
  353. * reset directory stream
  354. *
  355. * Refer to man (3) rewinddir
  356. *
  357. * \param [in] dirp directory pointer returned by \p vfs_opendir
  358. * \return
  359. * - 0 on success
  360. * - -1 on error. Refer to file system implementation for errno.
  361. */
  362. void vfs_rewinddir(DIR *pdir);
  363. /**
  364. * close a directory
  365. *
  366. * Refer to man (3) closedir
  367. *
  368. * \param [in] dirp directory pointer returned by \p vfs_opendir
  369. * \return
  370. * - 0 on success
  371. * - -1 on error. Refer to file system implementation for errno.
  372. */
  373. int vfs_closedir(DIR *pdir);
  374. /**
  375. * create a directory
  376. *
  377. * Refer to man (2) mkdir
  378. *
  379. * \param [in] pathname directory path name
  380. * \param [in] mode create mode
  381. * \return
  382. * - 0 on success
  383. * - -1 on error. Refer to file system implementation for errno.
  384. */
  385. int vfs_mkdir(const char *pathname, mode_t mode);
  386. /**
  387. * delete a directory
  388. *
  389. * Refer to man (2) rmdir
  390. *
  391. * \param [in] pathname directory path name
  392. * \return
  393. * - 0 on success
  394. * - -1 on error. Refer to file system implementation for errno.
  395. */
  396. int vfs_rmdir(const char *pathname);
  397. /**
  398. * change working directory
  399. *
  400. * Refer to man (2) chdir
  401. *
  402. * NOTE: in RTOS system, working directory is a global concept. It is
  403. * dangerous to change working directory. Don't call \p vfs_chdir
  404. * unless it is absolutely needed, and you really know what you are
  405. * doing.
  406. *
  407. * \param [in] path directory path name
  408. * \return
  409. * - 0 on success
  410. * - -1 on error
  411. */
  412. int vfs_chdir(const char *path);
  413. /**
  414. * get current working directory
  415. *
  416. * Refer to man (3) getcwd
  417. *
  418. * \param [out] buf buffer for result
  419. * \param [in] size buffer size
  420. * \return
  421. * - current working directory on success
  422. * - NULL on error
  423. */
  424. char *vfs_getcwd(char *buf, size_t size);
  425. /**
  426. * return the canonicalized absolute pathname
  427. *
  428. * Refer to man (3) realpath
  429. *
  430. * \param [in] path file path
  431. * \param [in] resolved_path buffer for canonicalized absolute pathname
  432. * \return
  433. * - \p resolved_path on success
  434. * - NULL on error
  435. */
  436. char *vfs_realpath(const char *path, char *resolved_path);
  437. /**
  438. * get filesystem statistics
  439. *
  440. * Refer to man (3) statvfs
  441. *
  442. * \param [in] path file path
  443. * \param [in] buf filesystem statistics
  444. * \return
  445. * - 0 on success
  446. * - -1 on error
  447. */
  448. int vfs_statvfs(const char *path, struct statvfs *buf);
  449. /**
  450. * get filesystem statistics
  451. *
  452. * Refer to man (3) fstatvfs
  453. *
  454. * \param [in] fd file descriptor
  455. * \param [in] buf filesystem statistics
  456. * \return
  457. * - 0 on success
  458. * - -1 on error
  459. */
  460. int vfs_fstatvfs(int fd, struct statvfs *buf);
  461. /**
  462. * get mounted file system count
  463. *
  464. * \return mounted file system count
  465. */
  466. int vfs_mount_count(void);
  467. /**
  468. * get mounted file system mount points
  469. *
  470. * \param [in] mp buffer for mount points
  471. * \param [in] count buffer count
  472. * \return count of get mount points
  473. */
  474. int vfs_mount_points(char **mp, size_t count);
  475. /**
  476. * helper to get canonicalized absolute pathname
  477. *
  478. * It is similar to \p vfs_realpath, just with specified base path name.
  479. *
  480. * \param [in] base_path base path name
  481. * \param [in] path path related to base_path
  482. * \param [in] resolved_path buffer for canonicalized absolute pathname
  483. * \return
  484. * - \p resolved_path on success
  485. * - NULL on error
  486. */
  487. char *vfs_resolve_path(const char *base_path, const char *path, char *resolved_path);
  488. /**
  489. * create directory with all parents
  490. *
  491. * \param [in] path path of directory to be created
  492. * \param [in] mode create mode
  493. * \return
  494. * - 0 on succes
  495. * - -1 on error
  496. */
  497. int vfs_mkpath(const char *path, mode_t mode);
  498. /**
  499. * create directory with all parents of a file
  500. *
  501. * \param [in] path path of file
  502. * \param [in] mode create mode
  503. * \return
  504. * - 0 on succes
  505. * - -1 on error
  506. */
  507. int vfs_mkfilepath(const char *path, mode_t mode);
  508. /**
  509. * helper to get file size
  510. *
  511. * It calls \p vfs_stat to get file status, and return file size only.
  512. *
  513. * \param [in] path file path
  514. * \return
  515. * - file size on success
  516. * - -1 on error
  517. */
  518. ssize_t vfs_file_size(const char *path);
  519. /**
  520. * helper to read file
  521. *
  522. * It calls \p vfs_open, \p vfs_read and \p vfs_close to read bytes from file.
  523. *
  524. * \param [in] path file path
  525. * \param [in] buf buf for read
  526. * \param [in] count byte count
  527. * \return
  528. * - the number of bytes read on success
  529. * - -1 on error
  530. */
  531. ssize_t vfs_file_read(const char *path, void *buf, size_t count);
  532. /**
  533. * helper to write file
  534. *
  535. * It calls \p vfs_open, \p vfs_write and \p vfs_close to write bytes to file.
  536. *
  537. * \param [in] path file path
  538. * \param [in] buf buf for write
  539. * \param [in] count byte count
  540. * \return
  541. * - the number of bytes written on success
  542. * - -1 on error
  543. */
  544. ssize_t vfs_file_write(const char *path, const void *buf, size_t count);
  545. /**
  546. * initial check for safe file
  547. *
  548. * *safe file* is just a normal file, just write is special designed for power
  549. * failure safe write. That is, when there is power failure during file write,
  550. * the file on file system will either be the original content, or the new
  551. * content.
  552. *
  553. * It is needed to write the whole content in one call. Partial write is not
  554. * supported.
  555. *
  556. * When there is power failure during write, there may exist garbages on file
  557. * system. \p vfs_sfile_init will clean up the garbages.
  558. *
  559. * \param [in] path safe file path
  560. * \return
  561. * - 0 on success
  562. * - -1 if the safe file isn't exist or invalid
  563. */
  564. int vfs_sfile_init(const char *path);
  565. /**
  566. * get safe file size
  567. *
  568. * It is the same as \p vfs_file_size.
  569. *
  570. * \param [in] path file path
  571. * \return
  572. * - file size on success
  573. * - -1 on error
  574. */
  575. ssize_t vfs_sfile_size(const char *path);
  576. /**
  577. * read safe file
  578. *
  579. * \param [in] path file path
  580. * \param [in] buf buf for read
  581. * \param [in] count byte count
  582. * \return
  583. * - the number of bytes read on success
  584. * - -1 on error
  585. */
  586. ssize_t vfs_sfile_read(const char *path, void *buf, size_t count);
  587. /**
  588. * write safe file
  589. *
  590. * \param [in] path file path
  591. * \param [in] buf buf for write
  592. * \param [in] count byte count
  593. * \return
  594. * - the number of bytes written on success
  595. * - -1 on error
  596. */
  597. ssize_t vfs_sfile_write(const char *path, const void *buf, size_t count);
  598. /**
  599. * delete files and subdirectories under a directory
  600. *
  601. * \param [in] path directory path
  602. * \return
  603. * - the number of bytes read on success
  604. * - -1 on error
  605. */
  606. int vfs_rmchildren(const char *path);
  607. /**
  608. * delete directory and children under the directory
  609. *
  610. * \param [in] path directory path
  611. * \return
  612. * - the number of bytes read on success
  613. * - -1 on error
  614. */
  615. int vfs_rmdir_recursive(const char *path);
  616. /**
  617. * umount all mounted file system
  618. *
  619. * It should only be called in FDL now. And in application, the
  620. * implementation is empty.
  621. */
  622. void vfs_umount_all(void);
  623. /**
  624. * fs handle of a path
  625. *
  626. * It is for debug only, get the file system implementation handler.
  627. *
  628. * \param [in] path file path
  629. * \return
  630. * - the file system implementation handler
  631. */
  632. void *vfs_fs_handle(const char *path);
  633. /**
  634. * fs handle of mount point
  635. *
  636. * It can be used to determine whether a mount point is mounted.
  637. *
  638. * \param [in] path mount point
  639. * \return
  640. * - the file system implementation handler
  641. */
  642. void *vfs_mount_handle(const char *path);
  643. /**
  644. * recursive total file size of a directory
  645. *
  646. * \param [in] path directory name
  647. * \return
  648. * - recursive total file size of a directory
  649. * - -1 on error, invalid parameter or out of memory
  650. */
  651. int64_t vfs_dir_total_size(const char *path);
  652. typedef struct
  653. {
  654. ssize_t (*stdin_read)(void *dst, size_t size);
  655. ssize_t (*stdout_write)(const void *data, size_t size);
  656. ssize_t (*stderr_write)(const void *data, size_t size);
  657. } vfs_stdio_callback_t;
  658. void vfs_set_stdio_callback(const vfs_stdio_callback_t *cb);
  659. #ifdef __cplusplus
  660. }
  661. #endif
  662. #endif // H