OpenCoverage

fsusage.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/fsusage.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* fsusage.c -- return space usage of mounted file systems-
2-
3 Copyright (C) 1991-1992, 1996, 1998-1999, 2002-2006, 2009-2018 Free Software-
4 Foundation, Inc.-
5-
6 This program is free software: you can redistribute it and/or modify-
7 it under the terms of the GNU General Public License as published by-
8 the Free Software Foundation; either version 3 of the License, or-
9 (at your option) any later version.-
10-
11 This program is distributed in the hope that it will be useful,-
12 but WITHOUT ANY WARRANTY; without even the implied warranty of-
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
14 GNU General Public License for more details.-
15-
16 You should have received a copy of the GNU General Public License-
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
18-
19#include <config.h>-
20-
21#include "fsusage.h"-
22-
23#include <limits.h>-
24#include <sys/types.h>-
25-
26#if STAT_STATVFS || STAT_STATVFS64 /* POSIX 1003.1-2001 (and later) with XSI */-
27# include <sys/statvfs.h>-
28#else-
29/* Don't include backward-compatibility files unless they're needed.-
30 Eventually we'd like to remove all this cruft. */-
31# include <fcntl.h>-
32# include <unistd.h>-
33# include <sys/stat.h>-
34#if HAVE_SYS_PARAM_H-
35# include <sys/param.h>-
36#endif-
37#if HAVE_SYS_MOUNT_H-
38# include <sys/mount.h>-
39#endif-
40#if HAVE_SYS_VFS_H-
41# include <sys/vfs.h>-
42#endif-
43# if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */-
44# include <sys/fs/s5param.h>-
45# endif-
46# if HAVE_SYS_STATFS_H-
47# include <sys/statfs.h>-
48# endif-
49# if HAVE_DUSTAT_H /* AIX PS/2 */-
50# include <sys/dustat.h>-
51# endif-
52#endif-
53-
54/* Many space usage primitives use all 1 bits to denote a value that is-
55 not applicable or unknown. Propagate this information by returning-
56 a uintmax_t value that is all 1 bits if X is all 1 bits, even if X-
57 is unsigned and narrower than uintmax_t. */-
58#define PROPAGATE_ALL_ONES(x) \-
59 ((sizeof (x) < sizeof (uintmax_t) \-
60 && (~ (x) == (sizeof (x) < sizeof (int) \-
61 ? - (1 << (sizeof (x) * CHAR_BIT)) \-
62 : 0))) \-
63 ? UINTMAX_MAX : (uintmax_t) (x))-
64-
65/* Extract the top bit of X as an uintmax_t value. */-
66#define EXTRACT_TOP_BIT(x) ((x) \-
67 & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))-
68-
69/* If a value is negative, many space usage primitives store it into an-
70 integer variable by assignment, even if the variable's type is unsigned.-
71 So, if a space usage variable X's top bit is set, convert X to the-
72 uintmax_t value V such that (- (uintmax_t) V) is the negative of-
73 the original value. If X's top bit is clear, just yield X.-
74 Use PROPAGATE_TOP_BIT if the original value might be negative;-
75 otherwise, use PROPAGATE_ALL_ONES. */-
76#define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))-
77-
78#ifdef STAT_STATVFS-
79/* Return true if statvfs works. This is false for statvfs on systems-
80 with GNU libc on Linux kernels before 2.6.36, which stats all-
81 preceding entries in /proc/mounts; that makes df hang if even one-
82 of the corresponding file systems is hard-mounted but not available. */-
83# if ! (__linux__ && (__GLIBC__ || __UCLIBC__))-
84/* The FRSIZE fallback is not required in this case. */-
85# undef STAT_STATFS2_FRSIZE-
86static int statvfs_works (void) { return 1; }-
87# else-
88# include <string.h> /* for strverscmp */-
89# include <sys/utsname.h>-
90# include <sys/statfs.h>-
91# define STAT_STATFS2_BSIZE 1-
92-
93static int-
94statvfs_works (void)-
95{-
96 static int statvfs_works_cache = -1;-
97 struct utsname name;-
98 if (statvfs_works_cache < 0)
statvfs_works_cache < 0Description
TRUEevaluated 67 times by 1 test
Evaluated by:
  • df
FALSEevaluated 450 times by 1 test
Evaluated by:
  • df
67-450
99 statvfs_works_cache = (uname (&name) == 0
executed 67 times by 1 test: statvfs_works_cache = (uname (&name) == 0 && 0 <= strverscmp (name.release, "2.6.36"));
Executed by:
  • df
uname (&name) == 0Description
TRUEevaluated 67 times by 1 test
Evaluated by:
  • df
FALSEnever evaluated
0-67
100 && 0 <= strverscmp (name.release, "2.6.36"));
executed 67 times by 1 test: statvfs_works_cache = (uname (&name) == 0 && 0 <= strverscmp (name.release, "2.6.36"));
Executed by:
  • df
0 <= strverscm...ase, "2.6.36")Description
TRUEevaluated 67 times by 1 test
Evaluated by:
  • df
FALSEnever evaluated
0-67
101 return statvfs_works_cache;
executed 517 times by 1 test: return statvfs_works_cache;
Executed by:
  • df
517
102}-
103# endif-
104#endif-
105-
106-
107/* Fill in the fields of FSP with information about space usage for-
108 the file system on which FILE resides.-
109 DISK is the device on which FILE is mounted, for space-getting-
110 methods that need to know it.-
111 Return 0 if successful, -1 if not. When returning -1, ensure that-
112 ERRNO is either a system error value, or zero if DISK is NULL-
113 on a system that requires a non-NULL value. */-
114int-
115get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)-
116{-
117#ifdef STAT_STATVFS /* POSIX, except pre-2.6.36 glibc/Linux */-
118-
119 if (statvfs_works ())
statvfs_works ()Description
TRUEevaluated 517 times by 1 test
Evaluated by:
  • df
FALSEnever evaluated
0-517
120 {-
121 struct statvfs vfsd;-
122-
123 if (statvfs (file, &vfsd) < 0)
statvfs (file, &vfsd) < 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • df
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
1-516
124 return -1;
executed 1 time by 1 test: return -1;
Executed by:
  • df
1
125-
126 /* f_frsize isn't guaranteed to be supported. */-
127 fsp->fsu_blocksize = (vfsd.f_frsize
vfsd.f_frsizeDescription
TRUEevaluated 516 times by 1 test
Evaluated by:
  • df
FALSEnever evaluated
0-516
128 ? PROPAGATE_ALL_ONES (vfsd.f_frsize)
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
(~ (vfsd.f_frs...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0-516
129 : PROPAGATE_ALL_ONES (vfsd.f_bsize));
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (vfsd.f_bsi...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
130-
131 fsp->fsu_blocks = PROPAGATE_ALL_ONES (vfsd.f_blocks);
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
(~ (vfsd.f_blo...s) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0-516
132 fsp->fsu_bfree = PROPAGATE_ALL_ONES (vfsd.f_bfree);
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
(~ (vfsd.f_bfr...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0-516
133 fsp->fsu_bavail = PROPAGATE_TOP_BIT (vfsd.f_bavail);-
134 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (vfsd.f_bavail) != 0;-
135 fsp->fsu_files = PROPAGATE_ALL_ONES (vfsd.f_files);
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
(~ (vfsd.f_fil...s) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0-516
136 fsp->fsu_ffree = PROPAGATE_ALL_ONES (vfsd.f_ffree);
sizeof (vfsd.f...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (vfsd.f...of (uintmax_t)Description
TRUEnever evaluated
FALSEevaluated 516 times by 1 test
Evaluated by:
  • df
(~ (vfsd.f_ffr...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0-516
137 return 0;
executed 516 times by 1 test: return 0;
Executed by:
  • df
516
138 }-
139-
140#endif-
141-
142#if defined STAT_STATVFS64 /* AIX */-
143-
144 struct statvfs64 fsd;-
145-
146 if (statvfs64 (file, &fsd) < 0)-
147 return -1;-
148-
149 /* f_frsize isn't guaranteed to be supported. */-
150 fsp->fsu_blocksize = (fsd.f_frsize-
151 ? PROPAGATE_ALL_ONES (fsd.f_frsize)-
152 : PROPAGATE_ALL_ONES (fsd.f_bsize));-
153-
154#elif defined STAT_STATFS2_FS_DATA /* Ultrix */-
155-
156 struct fs_data fsd;-
157-
158 if (statfs (file, &fsd) != 1)-
159 return -1;-
160-
161 fsp->fsu_blocksize = 1024;-
162 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot);-
163 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree);-
164 fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen);-
165 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0;-
166 fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot);-
167 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);-
168-
169#elif defined STAT_STATFS3_OSF1 /* OSF/1 */-
170-
171 struct statfs fsd;-
172-
173 if (statfs (file, &fsd, sizeof (struct statfs)) != 0)-
174 return -1;-
175-
176 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);-
177-
178#elif defined STAT_STATFS2_FRSIZE /* 2.6 < glibc/Linux < 2.6.36 */-
179-
180 struct statfs fsd;-
181-
182 if (statfs (file, &fsd) < 0)
statfs (file, &fsd) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
183 return -1;
never executed: return -1;
0
184-
185 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_frsize);
sizeof (fsd.f_...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (fsd.f_...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (fsd.f_frsi...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
186-
187#elif defined STAT_STATFS2_BSIZE /* glibc/Linux < 2.6, 4.3BSD, SunOS 4, \-
188 Mac OS X < 10.4, FreeBSD < 5.0, \-
189 NetBSD < 3.0, OpenBSD < 4.4 */-
190-
191 struct statfs fsd;-
192-
193 if (statfs (file, &fsd) < 0)-
194 return -1;-
195-
196 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);-
197-
198# ifdef STATFS_TRUNCATES_BLOCK_COUNTS-
199-
200 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the-
201 struct statfs are truncated to 2GB. These conditions detect that-
202 truncation, presumably without botching the 4.1.1 case, in which-
203 the values are not truncated. The correct counts are stored in-
204 undocumented spare fields. */-
205 if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0)-
206 {-
207 fsd.f_blocks = fsd.f_spare[0];-
208 fsd.f_bfree = fsd.f_spare[1];-
209 fsd.f_bavail = fsd.f_spare[2];-
210 }-
211# endif /* STATFS_TRUNCATES_BLOCK_COUNTS */-
212-
213#elif defined STAT_STATFS2_FSIZE /* 4.4BSD and older NetBSD */-
214-
215 struct statfs fsd;-
216-
217 if (statfs (file, &fsd) < 0)-
218 return -1;-
219-
220 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);-
221-
222#elif defined STAT_STATFS4 /* SVR3, Dynix, old Irix, old AIX, \-
223 Dolphin */-
224-
225# if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN-
226# define f_bavail f_bfree-
227# endif-
228-
229 struct statfs fsd;-
230-
231 if (statfs (file, &fsd, sizeof fsd, 0) < 0)-
232 return -1;-
233-
234 /* Empirically, the block counts on most SVR3 and SVR3-derived-
235 systems seem to always be in terms of 512-byte blocks,-
236 no matter what value f_bsize has. */-
237# if _AIX || defined _CRAY-
238 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);-
239# else-
240 fsp->fsu_blocksize = 512;-
241# endif-
242-
243#endif-
244-
245#if (defined STAT_STATVFS64 || defined STAT_STATFS3_OSF1 \-
246 || defined STAT_STATFS2_FRSIZE || defined STAT_STATFS2_BSIZE \-
247 || defined STAT_STATFS2_FSIZE || defined STAT_STATFS4)-
248-
249 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
sizeof (fsd.f_...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (fsd.f_...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (fsd.f_bloc...s) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
250 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
sizeof (fsd.f_...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (fsd.f_...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (fsd.f_bfre...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
251 fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail);-
252 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0;-
253 fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files);
sizeof (fsd.f_...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (fsd.f_...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (fsd.f_file...s) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
254 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
sizeof (fsd.f_...< sizeof (int)Description
TRUEnever evaluated
FALSEnever evaluated
sizeof (fsd.f_...of (uintmax_t)Description
TRUEnever evaluated
FALSEnever evaluated
(~ (fsd.f_ffre...e) * 8)) : 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
255-
256#endif-
257-
258 (void) disk; /* avoid argument-unused warning */-
259 return 0;
never executed: return 0;
0
260}-
261-
262#if defined _AIX && defined _I386-
263/* AIX PS/2 does not supply statfs. */-
264-
265int-
266statfs (char *file, struct statfs *fsb)-
267{-
268 struct stat stats;-
269 struct dustat fsd;-
270-
271 if (stat (file, &stats) != 0)-
272 return -1;-
273 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))-
274 return -1;-
275 fsb->f_type = 0;-
276 fsb->f_bsize = fsd.du_bsize;-
277 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;-
278 fsb->f_bfree = fsd.du_tfree;-
279 fsb->f_bavail = fsd.du_tfree;-
280 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;-
281 fsb->f_ffree = fsd.du_tinode;-
282 fsb->f_fsid.val[0] = fsd.du_site;-
283 fsb->f_fsid.val[1] = fsd.du_pckno;-
284 return 0;-
285}-
286-
287#endif /* _AIX && _I386 */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2