vfs.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef __VFS_H__
  2. #define __VFS_H__
  3. #include "vfs_int.h"
  4. // DEPRECATED, DON'T USE
  5. // Check for fd != 0 instead
  6. #define FS_OPEN_OK 1
  7. // ---------------------------------------------------------------------------
  8. // file functions
  9. //
  10. // vfs_close - close file descriptor and free memory
  11. // fd: file descriptor
  12. // Returns: VFS_RES_OK or negative value in case of error
  13. static inline int32_t vfs_close( int fd ) {
  14. vfs_file *f = (vfs_file *)fd;
  15. return f ? f->fns->close( f ) : VFS_RES_ERR;
  16. }
  17. // vfs_read - read data from file
  18. // fd: file descriptor
  19. // ptr: destination data buffer
  20. // len: requested length
  21. // Returns: Number of bytes read, or VFS_RES_ERR in case of error
  22. static inline int32_t vfs_read( int fd, void *ptr, size_t len ) {
  23. vfs_file *f = (vfs_file *)fd;
  24. return f ? f->fns->read( f, ptr, len ) : VFS_RES_ERR;
  25. }
  26. // vfs_write - write data to file
  27. // fd: file descriptor
  28. // ptr: source data buffer
  29. // len: requested length
  30. // Returns: Number of bytes written, or VFS_RES_ERR in case of error
  31. static inline sint32_t vfs_write( int fd, const void *ptr, size_t len ) {
  32. vfs_file *f = (vfs_file *)fd;
  33. return f ? f->fns->write( f, ptr, len ) : VFS_RES_ERR;
  34. }
  35. int vfs_getc( int fd );
  36. int vfs_ungetc( int c, int fd );
  37. // vfs_lseek - move read/write pointer
  38. // fd: file descriptor
  39. // off: offset
  40. // whence: VFS_SEEK_SET - set pointer to off
  41. // VFS_SEEK_CUR - set pointer to current position + off
  42. // VFS_SEEK_END - set pointer to end of file + off
  43. // Returns: New position, or VFS_RES_ERR in case of error
  44. static inline int32_t vfs_lseek( int fd, sint32_t off, int whence ) {
  45. vfs_file *f = (vfs_file *)fd;
  46. return f ? f->fns->lseek( f, off, whence ) : VFS_RES_ERR;
  47. }
  48. // vfs_eof - test for end-of-file
  49. // fd: file descriptor
  50. // Returns: 0 if not at end, != 0 if end of file
  51. static inline int32_t vfs_eof( int fd ) {
  52. vfs_file *f = (vfs_file *)fd;
  53. return f ? f->fns->eof( f ) : VFS_RES_ERR;
  54. }
  55. // vfs_tell - get read/write position
  56. // fd: file descriptor
  57. // Returns: Current position
  58. static inline int32_t vfs_tell( int fd ) {
  59. vfs_file *f = (vfs_file *)fd;
  60. return f ? f->fns->tell( f ) : VFS_RES_ERR;
  61. }
  62. // vfs_flush - flush write cache to file
  63. // fd: file descriptor
  64. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  65. static inline int32_t vfs_flush( int fd ) {
  66. vfs_file *f = (vfs_file *)fd;
  67. return f ? f->fns->flush( f ) : VFS_RES_ERR;
  68. }
  69. // vfs_size - get current file size
  70. // fd: file descriptor
  71. // Returns: File size
  72. static inline uint32_t vfs_size( int fd ) {
  73. vfs_file *f = (vfs_file *)fd;
  74. return f ? f->fns->size( f ) : 0;
  75. }
  76. // vfs_ferrno - get file system specific errno
  77. // fd: file descriptor
  78. // Returns: errno
  79. int32_t vfs_ferrno( int fd );
  80. // ---------------------------------------------------------------------------
  81. // dir functions
  82. //
  83. // vfs_closedir - close directory descriptor and free memory
  84. // dd: dir descriptor
  85. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  86. static inline int32_t vfs_closedir( vfs_dir *dd ) { return dd->fns->close( dd ); }
  87. // vfs_readdir - read next directory item
  88. // dd: dir descriptor
  89. // buf: pre-allocated stat structure to be filled in
  90. // Returns: VFS_RES_OK if next item found, otherwise VFS_RES_ERR
  91. static inline int32_t vfs_readdir( vfs_dir *dd, struct vfs_stat *buf ) { return dd->fns->readdir( dd, buf ); }
  92. // ---------------------------------------------------------------------------
  93. // volume functions
  94. //
  95. // vfs_umount - unmount logical drive and free memory
  96. // vol: volume object
  97. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  98. static inline int32_t vfs_umount( vfs_vol *vol ) { return vol->fns->umount( vol ); }
  99. // ---------------------------------------------------------------------------
  100. // file system functions
  101. //
  102. // vfs_mount - unmount logical drive
  103. // name: name of logical drive
  104. // num: drive's physical number (eg. SS/CS pin), negative values are ignored
  105. // Returns: Volume object, or NULL in case of error
  106. vfs_vol *vfs_mount( const char *name, int num );
  107. // vfs_open - open file
  108. // name: file name
  109. // mode: open mode
  110. // Returns: File descriptor, or NULL in case of error
  111. int vfs_open( const char *name, const char *mode );
  112. // vfs_opendir - open directory
  113. // name: dir name
  114. // Returns: Directory descriptor, or NULL in case of error
  115. vfs_dir *vfs_opendir( const char *name );
  116. // vfs_stat - stat file or directory
  117. // name: file or directory name
  118. // buf: pre-allocated structure to be filled in
  119. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  120. int32_t vfs_stat( const char *name, struct vfs_stat *buf );
  121. // vfs_remove - remove file or directory
  122. // name: file or directory name
  123. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  124. int32_t vfs_remove( const char *name );
  125. // vfs_rename - rename file or directory
  126. // name: file or directory name
  127. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  128. int32_t vfs_rename( const char *oldname, const char *newname );
  129. // vfs_mkdir - create directory
  130. // name: directory name
  131. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  132. int32_t vfs_mkdir( const char *name );
  133. // vfs_fsinfo - get file system info
  134. // name: logical drive identifier
  135. // total: receives total amount
  136. // used: received used amount
  137. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  138. int32_t vfs_fsinfo( const char *name, uint32_t *total, uint32_t *used );
  139. // vfs_format - format file system
  140. // Returns: 1, or 0 in case of error
  141. int32_t vfs_format( void );
  142. // vfs_chdir - change default directory
  143. // path: new default directory
  144. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  145. int32_t vfs_chdir( const char *path );
  146. // vfs_fscfg - query configuration settings of file system
  147. // phys_addr: pointer to store physical address information
  148. // phys_size: pointer to store physical size information
  149. // Returns: VFS_RES_OK, or VFS_RES_ERR in case of error
  150. int32_t vfs_fscfg( const char *name, uint32_t *phys_addr, uint32_t *phys_size);
  151. // vfs_errno - get file system specific errno
  152. // name: logical drive identifier
  153. // Returns: errno
  154. int32_t vfs_errno( const char *name );
  155. // vfs_clearerr - cleaer file system specific errno
  156. void vfs_clearerr( const char *name );
  157. // vfs_register_rtc_cb - register callback function for RTC query
  158. // cb: pointer to callback function
  159. void vfs_register_rtc_cb( int32_t (*cb)( vfs_time *tm ) );
  160. // vfs_basename - identify basename (incl. extension)
  161. // path: full file system path
  162. // Returns: pointer to basename within path string
  163. const char *vfs_basename( const char *path );
  164. #endif