]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/gen/statvfs.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libc / gen / statvfs.c
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 __FBSDID("$FreeBSD$");
32
33 #include "namespace.h"
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/statvfs.h>
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <unistd.h>
41 #include "un-namespace.h"
42
43 static int      sfs2svfs(const struct statfs *from, struct statvfs *to);
44
45 int
46 fstatvfs(int fd, struct statvfs *result)
47 {
48         struct statfs sfs;
49         int rv;
50         long pcval;
51
52         rv = _fstatfs(fd, &sfs);
53         if (rv != 0)
54                 return (rv);
55
56         rv = sfs2svfs(&sfs, result);
57         if (rv != 0)
58                 return (rv);
59
60         /*
61          * Whether pathconf's -1 return means error or unlimited does not
62          * make any difference in this best-effort implementation.
63          */
64         pcval = _fpathconf(fd, _PC_NAME_MAX);
65         if (pcval == -1)
66                 result->f_namemax = ~0UL;
67         else
68                 result->f_namemax = (unsigned long)pcval;
69         return (0);
70 }
71
72 int
73 statvfs(const char * __restrict path, struct statvfs * __restrict result)
74 {
75         struct statfs sfs;
76         int rv;
77         long pcval;
78
79         rv = statfs(path, &sfs);
80         if (rv != 0)
81                 return (rv);
82
83         sfs2svfs(&sfs, result);
84
85         /*
86          * Whether pathconf's -1 return means error or unlimited does not
87          * make any difference in this best-effort implementation.
88          */
89         pcval = pathconf(path, _PC_NAME_MAX);
90         if (pcval == -1)
91                 result->f_namemax = ~0UL;
92         else
93                 result->f_namemax = (unsigned long)pcval;
94         return (0);
95 }
96
97 static int
98 sfs2svfs(const struct statfs *from, struct statvfs *to)
99 {
100         static const struct statvfs zvfs;
101
102         *to = zvfs;
103
104         if (from->f_flags & MNT_RDONLY)
105                 to->f_flag |= ST_RDONLY;
106         if (from->f_flags & MNT_NOSUID)
107                 to->f_flag |= ST_NOSUID;
108
109         /* XXX should we clamp negative values? */
110 #define COPY(field) \
111         do { \
112                 to->field = from->field; \
113                 if (from->field != to->field) { \
114                         errno = EOVERFLOW; \
115                         return (-1); \
116                 } \
117         } while(0)
118
119         COPY(f_bavail);
120         COPY(f_bfree);
121         COPY(f_blocks);
122         COPY(f_ffree);
123         COPY(f_files);
124         to->f_bsize = from->f_iosize;
125         to->f_frsize = from->f_bsize;
126         to->f_favail = to->f_ffree;
127         return (0);
128 }
129                                                                  
130 #ifdef MAIN
131 #include <err.h>
132 #include <stdint.h>
133 #include <stdio.h>
134
135 int
136 main(int argc, char **argv)
137 {
138         struct statvfs buf;
139
140         if (statvfs(argv[1], &buf) < 0)
141                 err(1, "statvfs");
142
143 #define SHOW(field) \
144         printf(#field ": %ju\n", (uintmax_t)buf.field)
145
146         SHOW(f_bavail);
147         SHOW(f_bfree);
148         SHOW(f_blocks);
149         SHOW(f_favail);
150         SHOW(f_ffree);
151         SHOW(f_files);
152         SHOW(f_bsize);
153         SHOW(f_frsize);
154         SHOW(f_namemax);
155         printf("f_flag: %lx\n", (unsigned long)buf.f_flag);
156
157         return 0;
158 }
159
160 #endif /* MAIN */