summaryrefslogtreecommitdiff
path: root/lib/libc/gen/statvfs.c
blob: f4f7807b3cff3497cf3513d1e2745bf9ec1ac31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*	$OpenBSD: statvfs.c,v 1.1 2008/03/22 19:18:42 otto Exp $	*/

/*
 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/statvfs.h>

static void
cvt_statfs_to_statvfs(const struct statfs *fsp, struct statvfs *vfsp)
{
	vfsp->f_bsize = fsp->f_iosize; /* XXX */
	vfsp->f_frsize = fsp->f_bsize; /* XXX */

	vfsp->f_blocks = fsp->f_blocks;
	vfsp->f_bfree = fsp->f_bfree;
	vfsp->f_bavail = fsp->f_bavail < 0 ? 0 : fsp->f_bavail;

	vfsp->f_files = fsp->f_files;
	vfsp->f_ffree = fsp->f_ffree;
	vfsp->f_favail = fsp->f_favail < 0 ? 0 : fsp->f_favail;

	vfsp->f_fsid = fsp->f_fsid.val[0]; /* XXX */
	vfsp->f_flag = 0;
	if (fsp->f_flags & MNT_RDONLY)
		vfsp->f_flag |= ST_RDONLY;
	if (fsp->f_flags & MNT_NOSUID)
		vfsp->f_flag |= ST_NOSUID;
	vfsp->f_namemax = fsp->f_namemax;
}

int
fstatvfs(int fd, struct statvfs *p)
{
	struct statfs sb;
	int ret;

	ret = fstatfs(fd, &sb);
	if (ret == 0)
		cvt_statfs_to_statvfs(&sb, p);
	return (ret);
}

int
statvfs(const char *path, struct statvfs *p)
{
	struct statfs sb;
	int ret;

	ret = statfs(path, &sb);
	if (ret == 0)
		cvt_statfs_to_statvfs(&sb, p);
	return (ret);
}