xref: /freebsd/lib/libc/gen/statvfs.c (revision 315ee00f)
1 /*
2  * Copyright 2002 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "namespace.h"
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/statvfs.h>
35 
36 #include <errno.h>
37 #include <limits.h>
38 #include <unistd.h>
39 #include "un-namespace.h"
40 
41 static int	sfs2svfs(const struct statfs *from, struct statvfs *to);
42 
43 int
44 fstatvfs(int fd, struct statvfs *result)
45 {
46 	struct statfs sfs;
47 	int rv;
48 	long pcval;
49 
50 	rv = _fstatfs(fd, &sfs);
51 	if (rv != 0)
52 		return (rv);
53 
54 	rv = sfs2svfs(&sfs, result);
55 	if (rv != 0)
56 		return (rv);
57 
58 	/*
59 	 * Whether pathconf's -1 return means error or unlimited does not
60 	 * make any difference in this best-effort implementation.
61 	 */
62 	pcval = _fpathconf(fd, _PC_NAME_MAX);
63 	if (pcval == -1)
64 		result->f_namemax = ~0UL;
65 	else
66 		result->f_namemax = (unsigned long)pcval;
67 	return (0);
68 }
69 
70 int
71 statvfs(const char * __restrict path, struct statvfs * __restrict result)
72 {
73 	struct statfs sfs;
74 	int rv;
75 	long pcval;
76 
77 	rv = statfs(path, &sfs);
78 	if (rv != 0)
79 		return (rv);
80 
81 	sfs2svfs(&sfs, result);
82 
83 	/*
84 	 * Whether pathconf's -1 return means error or unlimited does not
85 	 * make any difference in this best-effort implementation.
86 	 */
87 	pcval = pathconf(path, _PC_NAME_MAX);
88 	if (pcval == -1)
89 		result->f_namemax = ~0UL;
90 	else
91 		result->f_namemax = (unsigned long)pcval;
92 	return (0);
93 }
94 
95 static int
96 sfs2svfs(const struct statfs *from, struct statvfs *to)
97 {
98 	static const struct statvfs zvfs;
99 
100 	*to = zvfs;
101 
102 	if (from->f_flags & MNT_RDONLY)
103 		to->f_flag |= ST_RDONLY;
104 	if (from->f_flags & MNT_NOSUID)
105 		to->f_flag |= ST_NOSUID;
106 
107 	/* XXX should we clamp negative values? */
108 #define COPY(field) \
109 	do { \
110 		to->field = from->field; \
111 		if (from->field != to->field) { \
112 			errno = EOVERFLOW; \
113 			return (-1); \
114 		} \
115 	} while(0)
116 
117 	COPY(f_bavail);
118 	COPY(f_bfree);
119 	COPY(f_blocks);
120 	COPY(f_ffree);
121 	COPY(f_files);
122 	to->f_bsize = from->f_iosize;
123 	to->f_frsize = from->f_bsize;
124 	to->f_favail = to->f_ffree;
125 	return (0);
126 }
127 
128 #ifdef MAIN
129 #include <err.h>
130 #include <stdint.h>
131 #include <stdio.h>
132 
133 int
134 main(int argc, char **argv)
135 {
136 	struct statvfs buf;
137 
138 	if (statvfs(argv[1], &buf) < 0)
139 		err(1, "statvfs");
140 
141 #define SHOW(field) \
142 	printf(#field ": %ju\n", (uintmax_t)buf.field)
143 
144 	SHOW(f_bavail);
145 	SHOW(f_bfree);
146 	SHOW(f_blocks);
147 	SHOW(f_favail);
148 	SHOW(f_ffree);
149 	SHOW(f_files);
150 	SHOW(f_bsize);
151 	SHOW(f_frsize);
152 	SHOW(f_namemax);
153 	printf("f_flag: %lx\n", (unsigned long)buf.f_flag);
154 
155 	return 0;
156 }
157 
158 #endif /* MAIN */
159