vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / sys / statvfs.c
blob695363fe3b750dc9cdbef208bbb0626881ad42cf
1 /*
2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <sys/statvfs.h>
8 #include <sys/stat.h>
10 #include <fs_info.h>
13 static int
14 fill_statvfs(dev_t device, struct statvfs *statvfs)
16 fs_info info;
17 if (fs_stat_dev(device, &info) < 0)
18 return -1;
20 statvfs->f_frsize = statvfs->f_bsize = info.block_size;
21 statvfs->f_blocks = info.total_blocks;
22 statvfs->f_bavail = statvfs->f_bfree = info.free_blocks;
23 statvfs->f_files = info.total_nodes;
24 statvfs->f_favail = statvfs->f_ffree = info.free_nodes;
25 statvfs->f_fsid = device;
26 statvfs->f_flag = info.flags & B_FS_IS_READONLY ? ST_RDONLY : 0;
27 statvfs->f_namemax = B_FILE_NAME_LENGTH;
29 return 0;
33 // #pragma mark -
36 int
37 statvfs(const char *path, struct statvfs *statvfs)
39 dev_t device = dev_for_path(path);
40 if (device < 0)
41 return -1;
43 return fill_statvfs(device, statvfs);
47 int
48 fstatvfs(int fd, struct statvfs *statvfs)
50 struct stat stat;
51 if (fstat(fd, &stat) < 0)
52 return -1;
54 return fill_statvfs(stat.st_dev, statvfs);