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