1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stddef.h>
6 #include <fcntl.h>
7 #include <sys/stat.h>
8 #include <sys/statfs.h>
9 #include <utime.h>
10 #include <aos/vfs.h>
11 
12 #include "internal/common.h"
13 
statfs(const char * path,struct statfs * buf)14 int statfs(const char *path, struct statfs *buf)
15 {
16     int ret;
17     struct aos_statfs statfs_temp;
18 
19     CHECK_POSIX_PARAM(path);
20     CHECK_POSIX_PARAM(buf);
21 
22     ret = aos_statfs(path, &statfs_temp);
23     CHECK_AOS_RET(ret);
24     if (ret == 0) {
25         buf->f_type    = statfs_temp.f_type;
26         buf->f_bsize   = statfs_temp.f_bsize;
27         buf->f_blocks  = statfs_temp.f_blocks;
28         buf->f_bfree   = statfs_temp.f_bfree;
29         buf->f_bavail  = statfs_temp.f_bavail;
30         buf->f_files   = statfs_temp.f_files;
31         buf->f_ffree   = statfs_temp.f_ffree;
32         buf->f_fsid    = statfs_temp.f_fsid;
33         buf->f_namelen = statfs_temp.f_namelen;
34     }
35 
36     return ret;
37 }
38 
remove(const char * path)39 int remove(const char *path)
40 {
41     int ret;
42 
43     CHECK_POSIX_PARAM(path);
44 
45     ret = aos_remove(path);
46     CHECK_AOS_RET(ret);
47 
48     return ret;
49 }
50 
fsync(int fd)51 int fsync(int fd)
52 {
53     int ret;
54 
55     if (fd < 0) {
56         errno = EBADF;
57         return -1;
58     }
59 
60     ret = aos_sync(fd);
61     CHECK_AOS_RET(ret);
62 
63     return ret;
64 }
65 
fdatasync(int fd)66 int fdatasync(int fd)
67 {
68     int ret;
69 
70     if (fd < 0) {
71         errno = EBADF;
72         return -1;
73     }
74 
75     ret = aos_sync(fd);
76     CHECK_AOS_RET(ret);
77 
78     return ret;
79 }
80 
sync(void)81 void sync(void)
82 {
83     aos_allsync();
84 }
85 
access(const char * path,int mode)86 int access(const char *path, int mode)
87 {
88     int ret;
89 
90     CHECK_POSIX_PARAM(path);
91 
92     ret = aos_access(path, mode);
93     CHECK_AOS_RET(ret);
94 
95     return ret;
96 }
97 
98 
getcwd(char * buf,size_t size)99 char *getcwd(char *buf, size_t size)
100 {
101     if (buf == NULL) {
102         return NULL;
103     }
104 
105     return aos_getcwd(buf, size);
106 }
107 
creat(const char * path,mode_t mode)108 int creat(const char *path, mode_t mode)
109 {
110     int ret;
111 
112     CHECK_POSIX_PARAM(path);
113 
114     ret = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
115     CHECK_AOS_RET(ret);
116 
117     return ret;
118 }
119 
pathconf(const char * path,int name)120 long pathconf(const char *path, int name)
121 {
122     long ret;
123 
124     CHECK_POSIX_PARAM(path);
125 
126     ret = aos_pathconf(path, name);
127     CHECK_AOS_RET(ret);
128 
129     return ret;
130 }
131 
fpathconf(int fd,int name)132 long fpathconf(int fd, int name)
133 {
134     long ret;
135 
136     if (fd < 0) {
137         errno = EBADF;
138         return -1;
139     }
140 
141     ret = aos_fpathconf(fd, name);
142     CHECK_AOS_RET(ret);
143 
144     return ret;
145 }
146 
utime(const char * path,const struct utimbuf * buf)147 int utime(const char *path, const struct utimbuf *buf)
148 {
149     int ret;
150     struct aos_utimbuf utimbuf_temp;
151 
152     CHECK_POSIX_PARAM(path);
153 
154     if (buf == NULL) {
155         utimbuf_temp.actime = time(NULL);
156         utimbuf_temp.modtime = utimbuf_temp.actime;
157     } else {
158         utimbuf_temp.actime = buf->actime;
159         utimbuf_temp.modtime = buf->modtime;
160     }
161 
162     ret = aos_utime(path, &utimbuf_temp);
163     CHECK_AOS_RET(ret);
164 
165     return ret;
166 }
167