dfs_def.h 11 KB

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