dfs_def.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * File : dfs_def.h
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2004-10-01 Beranard The first version.
  13. * 2004-10-14 Beranard Clean up the code.
  14. * 2005-01-22 Beranard Clean up the code, port to MinGW
  15. */
  16. #ifndef __DFS_DEF_H__
  17. #define __DFS_DEF_H__
  18. #include <rtthread.h>
  19. #ifndef __D_FS__
  20. #define __D_FS__
  21. #endif
  22. #define DEVICE_GETGEOME 0
  23. #define DEVICE_GETINFO 1
  24. #define DEVICE_FORMAT 2
  25. #define DEVICE_CLEAN_SECTOR 3
  26. /* File flags */
  27. #define DFS_F_OPEN 0x01000000
  28. #define DFS_F_DIRECTORY 0x02000000
  29. #define DFS_F_EOF 0x04000000
  30. #define DFS_F_ERR 0x08000000
  31. #ifndef DFS_PATH_MAX
  32. #define DFS_PATH_MAX 256
  33. #endif
  34. #ifndef SECTOR_SIZE
  35. #define SECTOR_SIZE 512
  36. #endif
  37. #ifndef DFS_FILESYSTEM_TYPES_MAX
  38. #define DFS_FILESYSTEM_TYPES_MAX 4
  39. #endif
  40. #define DFS_DEBUG_INFO 0x01
  41. #define DFS_DEBUG_WARNING 0x02
  42. #define DFS_DEBUG_ERROR 0x04
  43. #define DFS_DEBUG_LEVEL (DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR)
  44. /* #define DFS_DEBUG */
  45. #ifdef DFS_DEBUG
  46. #define dfs_log(level, x) do { if (level & DFS_DEBUG_LEVEL) \
  47. {rt_kprintf("DFS %s, %d:", __FUNCTION__, __LINE__); rt_kprintf x; \
  48. rt_kprintf ("\n");}}while (0)
  49. #else
  50. #define dfs_log(level, x)
  51. #endif
  52. #if defined(RT_USING_NEWLIB)
  53. #include <string.h>
  54. #include <sys/stat.h> /* used for struct stat */
  55. #include <sys/statfs.h> /* used for struct statfs */
  56. #include <sys/errno.h> /* used for error number */
  57. #include <sys/fcntl.h> /* used for operation flags */
  58. #include <sys/unistd.h> /* used for SEEK_SET/CUR/END */
  59. #include <dirent.h> /* used for struct dirent */
  60. /* Device error codes */
  61. #define DFS_STATUS_OK 0 /* no error */
  62. #define DFS_STATUS_ENOENT ENOENT /* No such file or directory */
  63. #define DFS_STATUS_EIO EIO /* I/O error */
  64. #define DFS_STATUS_ENXIO ENXIO /* No such device or address */
  65. #define DFS_STATUS_EBADF EBADF /* Bad file number */
  66. #define DFS_STATUS_EAGAIN EAGAIN /* Try again */
  67. #define DFS_STATUS_ENOMEM ENOMEM /* no memory */
  68. #define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */
  69. #define DFS_STATUS_EEXIST EEXIST /* File exists */
  70. #define DFS_STATUS_EXDEV EXDEV /* Cross-device link */
  71. #define DFS_STATUS_ENODEV ENODEV /* No such device */
  72. #define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */
  73. #define DFS_STATUS_EISDIR EISDIR /* Is a directory */
  74. #define DFS_STATUS_EINVAL EINVAL /* Invalid argument */
  75. #define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */
  76. #define DFS_STATUS_EROFS EROFS /* Read-only file system */
  77. #define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */
  78. #define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */
  79. /* Operation flags */
  80. #define DFS_O_RDONLY O_RDONLY
  81. #define DFS_O_WRONLY O_WRONLY
  82. #define DFS_O_RDWR O_RDWR
  83. #define DFS_O_ACCMODE O_ACCMODE
  84. #define DFS_O_CREAT O_CREAT
  85. #define DFS_O_EXCL O_EXCL
  86. #define DFS_O_TRUNC O_TRUNC
  87. #define DFS_O_APPEND O_APPEND
  88. #define DFS_O_DIRECTORY O_DIRECTORY
  89. /* Seek flags */
  90. #define DFS_SEEK_SET SEEK_SET
  91. #define DFS_SEEK_CUR SEEK_CUR
  92. #define DFS_SEEK_END SEEK_END
  93. /* Stat codes */
  94. #define DFS_S_IFMT S_IFMT
  95. #define DFS_S_IFSOCK S_IFSOCK
  96. #define DFS_S_IFLNK S_IFLNK
  97. #define DFS_S_IFREG S_IFREG
  98. #define DFS_S_IFBLK S_IFBLK
  99. #define DFS_S_IFDIR S_IFDIR
  100. #define DFS_S_IFCHR S_IFCHR
  101. #define DFS_S_IFIFO S_IFIFO
  102. #define DFS_S_ISUID S_ISUID
  103. #define DFS_S_ISGID S_ISGID
  104. #define DFS_S_ISVTX S_ISVTX
  105. #define DFS_S_ISLNK(m) S_ISLNK(m)
  106. #define DFS_S_ISREG(m) S_ISREG(m)
  107. #define DFS_S_ISDIR(m) S_ISDIR(m)
  108. #define DFS_S_ISCHR(m) S_ISCHR(m)
  109. #define DFS_S_ISBLK(m) S_ISBLK(m)
  110. #define DFS_S_ISFIFO(m) S_ISFIFO(m)
  111. #define DFS_S_ISSOCK(m) S_ISSOCK(m)
  112. #define DFS_S_IRWXU S_IRWXU
  113. #define DFS_S_IRUSR S_IRUSR
  114. #define DFS_S_IWUSR S_IWUSR
  115. #define DFS_S_IXUSR S_IXUSR
  116. #define DFS_S_IRWXG S_IRWXG
  117. #define DFS_S_IRGRP S_IRGRP
  118. #define DFS_S_IWGRP S_IWGRP
  119. #define DFS_S_IXGRP S_IXGRP
  120. #define DFS_S_IRWXO S_IRWXO
  121. #define DFS_S_IROTH S_IROTH
  122. #define DFS_S_IWOTH S_IWOTH
  123. #define DFS_S_IXOTH S_IXOTH
  124. /* Dirent types */
  125. #define DFS_DT_UNKNOWN DT_UNKNOWN
  126. #define DFS_DT_REG DT_REG
  127. #define DFS_DT_DIR DT_DIR
  128. #else
  129. #ifdef RT_USING_MINILIBC
  130. #include <string.h>
  131. #else
  132. typedef long off_t;
  133. typedef int mode_t;
  134. #endif
  135. /* Device error codes */
  136. #define DFS_STATUS_OK 0 /* no error */
  137. #define DFS_STATUS_ENOENT 2 /* No such file or directory */
  138. #define DFS_STATUS_EIO 5 /* I/O error */
  139. #define DFS_STATUS_ENXIO 6 /* No such device or address */
  140. #define DFS_STATUS_EBADF 9 /* Bad file number */
  141. #define DFS_STATUS_EAGAIN 11 /* Try again */
  142. #define DFS_STATUS_ENOMEM 12 /* no memory */
  143. #define DFS_STATUS_EBUSY 16 /* Device or resource busy */
  144. #define DFS_STATUS_EEXIST 17 /* File exists */
  145. #define DFS_STATUS_EXDEV 18 /* Cross-device link */
  146. #define DFS_STATUS_ENODEV 19 /* No such device */
  147. #define DFS_STATUS_ENOTDIR 20 /* Not a directory */
  148. #define DFS_STATUS_EISDIR 21 /* Is a directory */
  149. #define DFS_STATUS_EINVAL 22 /* Invalid argument */
  150. #define DFS_STATUS_ENOSPC 28 /* No space left on device */
  151. #define DFS_STATUS_EROFS 30 /* Read-only file system */
  152. #define DFS_STATUS_ENOSYS 38 /* Function not implemented */
  153. #define DFS_STATUS_ENOTEMPTY 39 /* Directory not empty */
  154. /* Operation flags */
  155. #define DFS_O_RDONLY 0x0000000
  156. #define DFS_O_WRONLY 0x0000001
  157. #define DFS_O_RDWR 0x0000002
  158. #define DFS_O_ACCMODE 0x0000003
  159. #define DFS_O_CREAT 0x0000100
  160. #define DFS_O_EXCL 0x0000200
  161. #define DFS_O_TRUNC 0x0001000
  162. #define DFS_O_APPEND 0x0002000
  163. #define DFS_O_BINARY 0x0008000
  164. #define DFS_O_DIRECTORY 0x0200000
  165. /* File flags */
  166. #define DFS_F_OPEN 0x01000000
  167. #define DFS_F_DIRECTORY 0x02000000
  168. #define DFS_F_EOF 0x04000000
  169. #define DFS_F_ERR 0x08000000
  170. /* Seek flags */
  171. #ifdef __CC_ARM
  172. #include <stdio.h>
  173. #define DFS_SEEK_SET SEEK_SET
  174. #define DFS_SEEK_CUR SEEK_CUR
  175. #define DFS_SEEK_END SEEK_END
  176. #else
  177. #define DFS_SEEK_SET 0
  178. #define DFS_SEEK_CUR 1
  179. #define DFS_SEEK_END 2
  180. #endif
  181. /* Stat codes */
  182. #define DFS_S_IFMT 00170000
  183. #define DFS_S_IFSOCK 0140000
  184. #define DFS_S_IFLNK 0120000
  185. #define DFS_S_IFREG 0100000
  186. #define DFS_S_IFBLK 0060000
  187. #define DFS_S_IFDIR 0040000
  188. #define DFS_S_IFCHR 0020000
  189. #define DFS_S_IFIFO 0010000
  190. #define DFS_S_ISUID 0004000
  191. #define DFS_S_ISGID 0002000
  192. #define DFS_S_ISVTX 0001000
  193. #define DFS_S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK)
  194. #define DFS_S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG)
  195. #define DFS_S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR)
  196. #define DFS_S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR)
  197. #define DFS_S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK)
  198. #define DFS_S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO)
  199. #define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK)
  200. #define DFS_S_IRWXU 00700
  201. #define DFS_S_IRUSR 00400
  202. #define DFS_S_IWUSR 00200
  203. #define DFS_S_IXUSR 00100
  204. #define DFS_S_IRWXG 00070
  205. #define DFS_S_IRGRP 00040
  206. #define DFS_S_IWGRP 00020
  207. #define DFS_S_IXGRP 00010
  208. #define DFS_S_IRWXO 00007
  209. #define DFS_S_IROTH 00004
  210. #define DFS_S_IWOTH 00002
  211. #define DFS_S_IXOTH 00001
  212. struct stat
  213. {
  214. rt_device_t st_dev;
  215. rt_uint16_t st_mode;
  216. rt_uint32_t st_size;
  217. rt_time_t st_mtime;
  218. rt_uint32_t st_blksize;
  219. };
  220. struct statfs
  221. {
  222. rt_size_t f_bsize; /* block size */
  223. rt_size_t f_blocks; /* total data blocks in file system */
  224. rt_size_t f_bfree; /* free blocks in file system */
  225. };
  226. /* File types */
  227. #define FT_REGULAR 0 /* regular file */
  228. #define FT_SOCKET 1 /* socket file */
  229. #define FT_DIRECTORY 2 /* directory */
  230. #define FT_USER 3 /* user defined */
  231. /* Dirent types */
  232. #define DFS_DT_UNKNOWN 0x00
  233. #define DFS_DT_REG 0x01
  234. #define DFS_DT_DIR 0x02
  235. struct dirent
  236. {
  237. rt_uint8_t d_type; /* The type of the file */
  238. rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */
  239. rt_uint16_t d_reclen; /* length of this record */
  240. char d_name[DFS_PATH_MAX]; /* The null-terminated file name */
  241. };
  242. #endif
  243. /* file descriptor */
  244. struct dfs_fd
  245. {
  246. char *path; /* Name (below mount point) */
  247. int type; /* Type (regular or socket) */
  248. int ref_count; /* Descriptor reference count */
  249. struct dfs_filesystem *fs; /* Resident file system */
  250. rt_uint32_t flags; /* Descriptor flags */
  251. rt_size_t size; /* Size in bytes */
  252. rt_off_t pos; /* Current file position */
  253. void *data; /* Specific file system data */
  254. };
  255. #endif