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-02-22     Bernard      The first version.
9  * 2023-05-05     Bernard      change to dfs v2.0
10  */
11 
12 #ifndef __DFS_H__
13 #define __DFS_H__
14 
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "../../libc/compilers/common/include/dirent.h"
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/statfs.h>
23 #include <sys/time.h>
24 #include <sys/errno.h>
25 #include <rtatomic.h>
26 #include <rtdevice.h>
27 
28 #ifndef ATTR_MODE_SET
29 #define ATTR_MODE_SET	(1 << 6)
30 #endif
31 
32 #ifndef ATTR_ATIME_SET
33 #define ATTR_ATIME_SET	(1 << 7)
34 #endif
35 
36 #ifndef ATTR_MTIME_SET
37 #define ATTR_MTIME_SET	(1 << 8)
38 #endif
39 
40 #ifndef ATTR_UID_SET
41 #define ATTR_UID_SET	(1 << 9)
42 #endif
43 
44 #ifndef ATTR_GID_SET
45 #define ATTR_GID_SET	(1 << 10)
46 #endif
47 
48 #ifndef AT_SYMLINK_NOFOLLOW
49 #define AT_SYMLINK_NOFOLLOW 0x100
50 #endif
51 
52 #ifndef UTIME_NOW
53 #define UTIME_NOW  0x3fffffff
54 #endif
55 
56 #ifndef UTIME_OMIT
57 #define UTIME_OMIT 0x3ffffffe
58 #endif
59 
60 #ifndef DFS_FD_MAX
61 #define DFS_FD_MAX              16
62 #endif
63 
64 /*
65  * skip stdin/stdout/stderr normally
66  */
67 #ifndef DFS_STDIO_OFFSET
68 #define DFS_STDIO_OFFSET        3
69 #endif
70 
71 #ifndef DFS_PATH_MAX
72 #define DFS_PATH_MAX            4096
73 #endif
74 
75 #ifndef SECTOR_SIZE
76 #define SECTOR_SIZE             512
77 #endif
78 
79 #define DFS_FS_FLAG_DEFAULT     0x00    /* default flag */
80 #define DFS_FS_FLAG_FULLPATH    0x01    /* set full path to underlaying file system */
81 
82 /* File flags */
83 #define DFS_F_FREAD     0x01
84 #define DFS_F_FWRITE    0x02
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
dfs_fflags(int oflags)90 rt_inline int dfs_fflags(int oflags)
91 {
92     int rw = oflags & O_ACCMODE;
93 
94     oflags &= ~O_ACCMODE;
95     return (rw + 1) | oflags;
96 }
97 
dfs_oflags(int fflags)98 rt_inline int dfs_oflags(int fflags)
99 {
100     int rw = fflags & (DFS_F_FREAD | DFS_F_FWRITE);
101 
102     fflags &= ~(DFS_F_FREAD | DFS_F_FWRITE);
103     return (rw - 1) | fflags;
104 }
105 
106 struct dfs_fdtable
107 {
108     uint32_t maxfd;
109     struct dfs_file **fds;
110 };
111 
112 /* Initialization of dfs */
113 int dfs_init(void);
114 
115 char *dfs_normalize_path(const char *directory, const char *filename);
116 const char *dfs_subdir(const char *directory, const char *filename);
117 
118 rt_err_t dfs_lock(void);
119 void dfs_unlock(void);
120 
121 rt_err_t dfs_file_lock(void);
122 void dfs_file_unlock(void);
123 
124 int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src);
125 int dfs_fdtable_drop_fd(struct dfs_fdtable *fdtab, int fd);
126 
127 #ifdef DFS_USING_POSIX
128 /* FD APIs */
129 int fdt_fd_new(struct dfs_fdtable *fdt);
130 struct dfs_file *fdt_get_file(struct dfs_fdtable* fdt, int fd);
131 void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
132 int fd_new(void);
133 int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file);
134 struct dfs_file *fd_get(int fd);
135 void fd_release(int fd);
136 
137 void fd_init(struct dfs_file *fd);
138 
139 struct dfs_fdtable *dfs_fdtable_get(void);
140 struct dfs_fdtable *dfs_fdtable_get_global(void);
141 int dfs_dup(int oldfd, int startfd);
142 #endif /* DFS_USING_POSIX */
143 
144 struct dfs_file* dfs_file_create(void);
145 void dfs_file_destroy(struct dfs_file *file);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif
152