1 /*
2 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2022-09-21
13 * Description: fs适配层代码
14 */
15
16 #define _GNU_SOURCE
17
18 #include <errno.h>
19 #include <stdarg.h>
20 #include <dirent.h>
21 #include <sys/mount.h>
22 #include <sys/statfs.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <sys/uio.h>
26 #include "vfs_config.h"
27 #include "vfs_operations.h"
28 #include "vfs_maps.h"
29 #include "vfs_mount.h"
30
open(const char * path,int oflag,...)31 int open(const char *path, int oflag, ...)
32 {
33 S32 ret;
34 va_list ap;
35 va_start(ap, oflag);
36 ret = OsVfsOpen(path, oflag, ap);
37 va_end(ap);
38 return OsMapToPosixRet(ret);
39 }
40
close(int fd)41 int close(int fd)
42 {
43 S32 ret = OsVfsClose(fd);
44 return OsMapToPosixRet(ret);
45 }
46
read(int fd,void * buf,size_t nbyte)47 ssize_t read(int fd, void *buf, size_t nbyte)
48 {
49 ssize_t ret = OsVfsRead(fd, buf, nbyte);
50 return OsMapToPosixRet(ret);
51 }
52
write(int fd,const void * buf,size_t nbyte)53 ssize_t write(int fd, const void *buf, size_t nbyte)
54 {
55 ssize_t ret = OsVfsWrite(fd, buf, nbyte);
56 return OsMapToPosixRet(ret);
57 }
58
lseek(int fd,off_t offset,int whence)59 off_t lseek(int fd, off_t offset, int whence)
60 {
61 return OsVfsLseek(fd, offset, whence);
62 }
63
stat(const char * path,struct stat * buf)64 int stat(const char *path, struct stat *buf)
65 {
66 S32 ret = OsVfsStat(path, buf);
67 return OsMapToPosixRet(ret);
68 }
69
statfs(const char * path,struct statfs * buf)70 int statfs(const char *path, struct statfs *buf)
71 {
72 S32 ret = OsVfsStatfs(path, buf);
73 return OsMapToPosixRet(ret);
74 }
75
unlink(const char * path)76 int unlink(const char *path)
77 {
78 S32 ret = OsVfsUnlink(path);
79 return OsMapToPosixRet(ret);
80 }
81
rename(const char * oldName,const char * newName)82 int rename(const char *oldName, const char *newName)
83 {
84 S32 ret = OsVfsRename(oldName, newName);
85 return OsMapToPosixRet(ret);
86 }
87
fsync(int fd)88 int fsync(int fd)
89 {
90 S32 ret = OsVfsSync(fd);
91 return OsMapToPosixRet(ret);
92 }
93
opendir(const char * dirName)94 DIR *opendir(const char *dirName)
95 {
96 return OsVfsOpendir(dirName);
97 }
98
readdir(DIR * dir)99 struct dirent *readdir(DIR *dir)
100 {
101 return OsVfsReaddir(dir);
102 }
103
closedir(DIR * dir)104 int closedir(DIR *dir)
105 {
106 S32 ret = OsVfsClosedir(dir);
107 return OsMapToPosixRet(ret);
108 }
109
mkdir(const char * path,mode_t mode)110 int mkdir(const char *path, mode_t mode)
111 {
112 S32 ret = OsVfsMkdir(path, (S32)mode);
113 return OsMapToPosixRet(ret);
114 }
115
rmdir(const char * path)116 int rmdir(const char *path)
117 {
118 S32 ret = OsVfsUnlink(path);
119 return OsMapToPosixRet(ret);
120 }
121
fstat(int fd,struct stat * buf)122 int fstat(int fd, struct stat *buf)
123 {
124 return OsVfsFstat(fd, buf);
125 }
126
fcntl(int fd,int cmd,...)127 int fcntl(int fd, int cmd, ...)
128 {
129 S32 ret;
130 va_list ap;
131 va_start(ap, cmd);
132 ret = OsVfsFcntl(fd, cmd, ap);
133 va_end(ap);
134 return ret;
135 }
136
ioctl(int fd,int request,...)137 int ioctl(int fd, int request, ...)
138 {
139 unsigned long arg;
140 S32 ret;
141 va_list ap;
142 va_start(ap, request);
143 arg = va_arg(ap, unsigned long);
144 va_end(ap);
145 ret = OsVfsIoctl(fd, request, arg);
146 return ret;
147 }
148
readv(int fd,const struct iovec * iovBuf,int iovcnt)149 ssize_t readv(int fd, const struct iovec *iovBuf, int iovcnt)
150 {
151 return OsVfsReadv(fd, iovBuf, iovcnt);
152 }
153
writev(int fd,const struct iovec * iovBuf,int iovcnt)154 ssize_t writev(int fd, const struct iovec *iovBuf, int iovcnt)
155 {
156 return OsVfsWritev(fd, iovBuf, iovcnt);
157 }
158
isatty(int fd)159 int isatty(int fd)
160 {
161 (void)fd;
162 return 0;
163 }
164
access(const char * path,int mode)165 int access(const char *path, int mode)
166 {
167 struct stat st;
168
169 if (stat(path, &st) < 0) {
170 return -1;
171 }
172 if ((st.st_mode & S_IFDIR) || (st.st_mode & S_IFREG)) {
173 return 0;
174 }
175 if ((mode & W_OK) && !(st.st_mode & S_IWRITE)) {
176 return -1;
177 }
178
179 return 0;
180 }
181
ftruncate(int fd,off_t length)182 int ftruncate(int fd, off_t length)
183 {
184 return OsVfsFtruncate(fd, length);
185 }
186
pread(int fd,void * buf,size_t nbyte,off_t offset)187 ssize_t pread(int fd, void *buf, size_t nbyte, off_t offset)
188 {
189 ssize_t ret = OsVfsPread(fd, buf, nbyte, offset);
190 return (ssize_t)OsMapToPosixRet(ret);
191 }
192
pwrite(int fd,const void * buf,size_t nbyte,off_t offset)193 ssize_t pwrite(int fd, const void *buf, size_t nbyte, off_t offset)
194 {
195 ssize_t ret = OsVfsPwrite(fd, buf, nbyte, offset);
196 return OsMapToPosixRet(ret);
197 }
198
mount(const char * source,const char * target,const char * filesystemtype,unsigned long mountflags,const void * data)199 int mount(const char *source, const char *target,
200 const char *filesystemtype, unsigned long mountflags,
201 const void *data)
202 {
203 return OsVfsMount(source, target, filesystemtype, mountflags, data);
204 }
205
umount(const char * target)206 int umount(const char *target)
207 {
208 return OsVfsUmount(target);
209 }
210
umount2(const char * target,int flag)211 int umount2(const char *target, int flag)
212 {
213 return OsVfsUmount2(target, flag);
214 }
215