Skip to content

Commit cd1e0a7

Browse files
xqyjljGuozhanxin
authored andcommitted
✨ feat(components): add statfs, statfs64, fstatfs, fstatfs64 support
1 parent e4bd8e0 commit cd1e0a7

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

components/dfs/src/dfs_posix.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,32 @@ int statfs(const char *path, struct statfs *buf)
520520
}
521521
RTM_EXPORT(statfs);
522522

523+
/**
524+
* this function is a POSIX compliant version, which will return the
525+
* information about a mounted file system.
526+
*
527+
* @param fildes the file description.
528+
* @param buf the buffer to save the returned information.
529+
*
530+
* @return 0 on successful, others on failed.
531+
*/
532+
int fstatfs(int fildes, struct statfs *buf)
533+
{
534+
struct dfs_fd *d;
535+
536+
/* get the fd */
537+
d = fd_get(fildes);
538+
if (d == NULL)
539+
{
540+
rt_set_errno(-EBADF);
541+
542+
return -1;
543+
}
544+
545+
return statfs(d->vnode->fullpath, buf);
546+
}
547+
RTM_EXPORT(fstatfs);
548+
523549
/**
524550
* this function is a POSIX compliant version, which will make a directory
525551
*

components/libc/compilers/common/include/sys/statfs.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* Change Logs:
77
* Date Author Notes
8+
* 2023-03-27 xqyjlj adapt musl
89
*/
910

1011
#ifndef __SYS_STATFS_H__
@@ -16,16 +17,31 @@
1617
extern "C" {
1718
#endif
1819

20+
#ifdef RT_USING_MUSLLIBC
21+
/* this is required for musl <sys/statfs.h> */
22+
#ifndef _POSIX_SOURCE
23+
#define _POSIX_SOURCE
24+
#include_next <sys/statfs.h>
25+
/* limiting influence of _POSIX_SOURCE */
26+
#undef _POSIX_SOURCE
27+
28+
#else /* def _POSIX_SOURCE */
29+
#include_next <sys/statfs.h>
30+
#endif
31+
#else
1932
struct statfs
2033
{
2134
size_t f_bsize; /* block size */
2235
size_t f_blocks; /* total data blocks in file system */
2336
size_t f_bfree; /* free blocks in file system */
37+
size_t f_bavail; /* free blocks available to unprivileged user*/
2438
};
2539

2640
int statfs(const char *path, struct statfs *buf);
2741
int fstatfs(int fd, struct statfs *buf);
2842

43+
#endif
44+
2945
#ifdef __cplusplus
3046
}
3147
#endif

components/lwp/lwp_syscall.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4611,6 +4611,144 @@ sysret_t sys_uname(struct utsname *uts)
46114611
return 0;
46124612
}
46134613

4614+
sysret_t sys_statfs(const char *path, struct statfs *buf)
4615+
{
4616+
int ret = 0;
4617+
int err;
4618+
size_t len;
4619+
size_t copy_len;
4620+
char *copy_path;
4621+
struct statfs statfsbuff = {0};
4622+
4623+
if (!lwp_user_accessable((void *)buf, sizeof(struct statfs)))
4624+
{
4625+
return -EFAULT;
4626+
}
4627+
4628+
len = lwp_user_strlen(path, &err);
4629+
if (err)
4630+
{
4631+
return -EFAULT;
4632+
}
4633+
4634+
copy_path = (char*)rt_malloc(len + 1);
4635+
if (!copy_path)
4636+
{
4637+
return -ENOMEM;
4638+
}
4639+
4640+
copy_len = lwp_get_from_user(copy_path, (void*)path, len);
4641+
if (copy_len == 0)
4642+
{
4643+
rt_free(copy_path);
4644+
return -EFAULT;
4645+
}
4646+
copy_path[copy_len] = '\0';
4647+
4648+
ret = _SYS_WRAP(statfs(copy_path, &statfsbuff));
4649+
rt_free(copy_path);
4650+
4651+
if (ret == 0)
4652+
{
4653+
lwp_put_to_user(buf, &statfsbuff, sizeof statfsbuff);
4654+
}
4655+
4656+
return ret;
4657+
}
4658+
4659+
sysret_t sys_statfs64(const char *path, size_t sz, struct statfs *buf)
4660+
{
4661+
int ret = 0;
4662+
int err;
4663+
size_t len;
4664+
size_t copy_len;
4665+
char *copy_path;
4666+
struct statfs statfsbuff = {0};
4667+
4668+
if (!lwp_user_accessable((void *)buf, sizeof(struct statfs)))
4669+
{
4670+
return -EFAULT;
4671+
}
4672+
4673+
if (sz != sizeof(struct statfs)) {
4674+
return -EINVAL;
4675+
}
4676+
4677+
len = lwp_user_strlen(path, &err);
4678+
if (err)
4679+
{
4680+
return -EFAULT;
4681+
}
4682+
4683+
copy_path = (char*)rt_malloc(len + 1);
4684+
if (!copy_path)
4685+
{
4686+
return -ENOMEM;
4687+
}
4688+
4689+
copy_len = lwp_get_from_user(copy_path, (void*)path, len);
4690+
if (copy_len == 0)
4691+
{
4692+
rt_free(copy_path);
4693+
return -EFAULT;
4694+
}
4695+
copy_path[copy_len] = '\0';
4696+
4697+
ret = _SYS_WRAP(statfs(copy_path, &statfsbuff));
4698+
rt_free(copy_path);
4699+
4700+
if (ret == 0)
4701+
{
4702+
lwp_put_to_user(buf, &statfsbuff, sizeof statfsbuff);
4703+
}
4704+
4705+
return ret;
4706+
}
4707+
4708+
sysret_t sys_fstatfs(int fd, struct statfs *buf)
4709+
{
4710+
int ret = 0;
4711+
struct statfs statfsbuff = {0};
4712+
4713+
if (!lwp_user_accessable((void *)buf, sizeof(struct statfs)))
4714+
{
4715+
return -EFAULT;
4716+
}
4717+
4718+
ret = _SYS_WRAP(fstatfs(fd, &statfsbuff));
4719+
4720+
if (ret == 0)
4721+
{
4722+
lwp_put_to_user(buf, &statfsbuff, sizeof statfsbuff);
4723+
}
4724+
4725+
return ret;
4726+
}
4727+
4728+
sysret_t sys_fstatfs64(int fd, size_t sz, struct statfs *buf)
4729+
{
4730+
int ret = 0;
4731+
struct statfs statfsbuff = {0};
4732+
4733+
if (!lwp_user_accessable((void *)buf, sizeof(struct statfs)))
4734+
{
4735+
return -EFAULT;
4736+
}
4737+
4738+
if (sz != sizeof(struct statfs)) {
4739+
return -EINVAL;
4740+
}
4741+
4742+
ret = _SYS_WRAP(fstatfs(fd, &statfsbuff));
4743+
4744+
if (ret == 0)
4745+
{
4746+
lwp_put_to_user(buf, &statfsbuff, sizeof statfsbuff);
4747+
}
4748+
4749+
return ret;
4750+
}
4751+
46144752
const static struct rt_syscall_def func_table[] =
46154753
{
46164754
SYSCALL_SIGN(sys_exit), /* 01 */
@@ -4822,6 +4960,10 @@ const static struct rt_syscall_def func_table[] =
48224960
SYSCALL_SIGN(sys_mq_close),
48234961
SYSCALL_SIGN(sys_stat), //TODO should be replaced by sys_lstat if symbolic link are implemented
48244962
SYSCALL_SIGN(sys_uname), /* 170 */
4963+
SYSCALL_SIGN(sys_statfs),
4964+
SYSCALL_SIGN(sys_statfs64),
4965+
SYSCALL_SIGN(sys_fstatfs),
4966+
SYSCALL_SIGN(sys_fstatfs64),
48254967
};
48264968

48274969
const void *lwp_get_sys_api(rt_uint32_t number)

0 commit comments

Comments
 (0)