aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/gen/statvfs.c
blob: fe9462bd307be0495c9da477660c16c035ccae84 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright 2002 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that both the above copyright notice and this
 * permission notice appear in all copies, that both the above
 * copyright notice and this permission notice appear in all
 * supporting documentation, and that the name of M.I.T. not be used
 * in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  M.I.T. makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

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

#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "un-namespace.h"

static int	sfs2svfs(const struct statfs *from, struct statvfs *to);

int
fstatvfs(int fd, struct statvfs *result)
{
	struct statfs sfs;
	int rv;
	long pcval;

	rv = _fstatfs(fd, &sfs);
	if (rv != 0)
		return (rv);

	rv = sfs2svfs(&sfs, result);
	if (rv != 0)
		return (rv);

	/*
	 * Whether pathconf's -1 return means error or unlimited does not
	 * make any difference in this best-effort implementation.
	 */
	pcval = _fpathconf(fd, _PC_NAME_MAX);
	if (pcval == -1)
		result->f_namemax = ~0UL;
	else
		result->f_namemax = (unsigned long)pcval;
	return (0);
}

int
statvfs(const char * __restrict path, struct statvfs * __restrict result)
{
	struct statfs sfs;
	int rv;
	long pcval;

	rv = statfs(path, &sfs);
	if (rv != 0)
		return (rv);

	sfs2svfs(&sfs, result);

	/*
	 * Whether pathconf's -1 return means error or unlimited does not
	 * make any difference in this best-effort implementation.
	 */
	pcval = pathconf(path, _PC_NAME_MAX);
	if (pcval == -1)
		result->f_namemax = ~0UL;
	else
		result->f_namemax = (unsigned long)pcval;
	return (0);
}

static int
sfs2svfs(const struct statfs *from, struct statvfs *to)
{
	static const struct statvfs zvfs;

	*to = zvfs;

	if (from->f_flags & MNT_RDONLY)
		to->f_flag |= ST_RDONLY;
	if (from->f_flags & MNT_NOSUID)
		to->f_flag |= ST_NOSUID;

	/* XXX should we clamp negative values? */
#define COPY(field) \
	do { \
		to->field = from->field; \
		if (from->field != to->field) { \
			errno = EOVERFLOW; \
			return (-1); \
		} \
	} while(0)

	COPY(f_bavail);
	COPY(f_bfree);
	COPY(f_blocks);
	COPY(f_ffree);
	COPY(f_files);
	to->f_bsize = from->f_iosize;
	to->f_frsize = from->f_bsize;
	to->f_favail = to->f_ffree;
	return (0);
}
								 
#ifdef MAIN
#include <err.h>
#include <stdint.h>
#include <stdio.h>

int
main(int argc, char **argv)
{
	struct statvfs buf;

	if (statvfs(argv[1], &buf) < 0)
		err(1, "statvfs");

#define SHOW(field) \
	printf(#field ": %ju\n", (uintmax_t)buf.field)

	SHOW(f_bavail);
	SHOW(f_bfree);
	SHOW(f_blocks);
	SHOW(f_favail);
	SHOW(f_ffree);
	SHOW(f_files);
	SHOW(f_bsize);
	SHOW(f_frsize);
	SHOW(f_namemax);
	printf("f_flag: %lx\n", (unsigned long)buf.f_flag);

	return 0;
}

#endif /* MAIN */