dfs_file.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2005-01-26 Bernard The first version.
  9. * 2023-05-05 Bernard Change to dfs v2.0
  10. */
  11. #ifndef __DFS_FILE_H__
  12. #define __DFS_FILE_H__
  13. #include <dfs.h>
  14. #include <dfs_fs.h>
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. #define STDIN_FILENO 0 /* standard input file descriptor */
  20. #define STDOUT_FILENO 1 /* standard output file descriptor */
  21. #define STDERR_FILENO 2 /* standard error file descriptor */
  22. struct dfs_file;
  23. struct dfs_vnode;
  24. struct dfs_dentry;
  25. struct dfs_attr;
  26. struct rt_pollreq;
  27. struct dirent;
  28. struct lwp_avl_struct;
  29. struct file_lock;
  30. struct dfs_file_ops
  31. {
  32. int (*open)(struct dfs_file *file);
  33. int (*close)(struct dfs_file *file);
  34. int (*ioctl)(struct dfs_file *file, int cmd, void *arg);
  35. ssize_t (*read)(struct dfs_file *file, void *buf, size_t count, off_t *pos);
  36. ssize_t (*write)(struct dfs_file *file, const void *buf, size_t count, off_t *pos);
  37. int (*flush)(struct dfs_file *file);
  38. off_t (*lseek)(struct dfs_file *file, off_t offset, int wherece);
  39. int (*truncate)(struct dfs_file *file, off_t offset);
  40. int (*getdents)(struct dfs_file *file, struct dirent *dirp, uint32_t count);
  41. int (*poll)(struct dfs_file *file, struct rt_pollreq *req);
  42. int (*mmap)(struct dfs_file *file, struct lwp_avl_struct *mmap);
  43. int (*lock)(struct dfs_file *file, struct file_lock *flock);
  44. int (*flock)(struct dfs_file *file, int, struct file_lock *flock);
  45. };
  46. struct dfs_vnode
  47. {
  48. uint32_t flags;
  49. uint32_t mode;
  50. int type; /* node type */
  51. rt_atomic_t ref_count; /* reference count */
  52. struct dfs_mnt *mnt; /* which mounted file system does this vnode belong to */
  53. size_t size;
  54. uint32_t nlink;
  55. const struct dfs_file_ops *fops;
  56. unsigned int uid;
  57. unsigned int gid;
  58. struct timespec atime;
  59. struct timespec mtime;
  60. struct timespec ctime;
  61. void *data; /* private data of this file system */
  62. };
  63. /* file descriptor */
  64. #define DFS_FD_MAGIC 0xfdfd
  65. struct dfs_file
  66. {
  67. uint16_t magic;
  68. uint16_t mode;
  69. uint32_t flags;
  70. rt_atomic_t ref_count;
  71. off_t fpos;
  72. struct rt_mutex pos_lock;
  73. const struct dfs_file_ops *fops;
  74. struct dfs_dentry *dentry; /* dentry of this file */
  75. struct dfs_vnode *vnode; /* vnode of this file */
  76. void *data;
  77. };
  78. /* file is open for reading */
  79. #define FMODE_READ 0x1
  80. /* file is open for writing */
  81. #define FMODE_WRITE 0x2
  82. /* file is seekable */
  83. #define FMODE_LSEEK 0x4
  84. /* file can be accessed using pread */
  85. #define FMODE_PREAD 0x8
  86. /* file can be accessed using pwrite */
  87. #define FMODE_PWRITE 0x10
  88. /* File is opened for execution with sys_execve / sys_uselib */
  89. #define FMODE_EXEC 0x20
  90. /* File is opened with O_NDELAY (only set for block devices) */
  91. #define FMODE_NDELAY 0x40
  92. /* File is opened with O_EXCL (only set for block devices) */
  93. #define FMODE_EXCL 0x80
  94. /* dfs_vnode.c */
  95. int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops);
  96. struct dfs_vnode *dfs_vnode_create(void);
  97. int dfs_vnode_destroy(struct dfs_vnode* vnode);
  98. struct dfs_vnode *dfs_vnode_ref(struct dfs_vnode *vnode);
  99. void dfs_vnode_unref(struct dfs_vnode *vnode);
  100. /*dfs_file.c*/
  101. struct dfs_mmap2_args
  102. {
  103. void *addr;
  104. size_t length;
  105. int prot;
  106. int flags;
  107. off_t pgoffset;
  108. void *ret;
  109. };
  110. void dfs_file_init(struct dfs_file *file);
  111. void dfs_file_deinit(struct dfs_file *file);
  112. int dfs_file_open(struct dfs_file *file, const char *path, int flags, mode_t mode);
  113. int dfs_file_close(struct dfs_file *file);
  114. off_t dfs_file_get_fpos(struct dfs_file *file);
  115. void dfs_file_set_fpos(struct dfs_file *file, off_t fpos);
  116. ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len);
  117. ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len);
  118. off_t generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence);
  119. off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece);
  120. int dfs_file_stat(const char *path, struct stat *buf);
  121. int dfs_file_lstat(const char *path, struct stat *buf);
  122. int dfs_file_setattr(const char *path, struct dfs_attr *attr);
  123. int dfs_file_fstat(struct dfs_file *file, struct stat *buf);
  124. int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args);
  125. int dfs_file_fcntl(int fd, int cmd, unsigned long arg);
  126. int dfs_file_fsync(struct dfs_file *file);
  127. int dfs_file_unlink(const char *path);
  128. int dfs_file_link(const char *oldname, const char *newname);
  129. int dfs_file_symlink(const char *oldname, const char *name);
  130. int dfs_file_readlink(const char *path, char *buf, int bufsize);
  131. int dfs_file_rename(const char *old_file, const char *new_file);
  132. int dfs_file_ftruncate(struct dfs_file *file, off_t length);
  133. int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes);
  134. int dfs_file_mkdir(const char *path, mode_t mode);
  135. int dfs_file_rmdir(const char *pathname);
  136. int dfs_file_isdir(const char *path);
  137. int dfs_file_access(const char *path, mode_t mode);
  138. int dfs_file_chdir(const char *path);
  139. char *dfs_file_getcwd(char *buf, size_t size);
  140. int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2);
  141. /* 0x5254 is just a magic number to make these relatively unique ("RT") */
  142. #define RT_FIOFTRUNCATE 0x52540000U
  143. #define RT_FIOGETADDR 0x52540001U
  144. #define RT_FIOMMAP2 0x52540002U
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif