* lisp/progmodes/cperl-mode.el: Use cl-lib. Fix comment convention
[emacs.git] / lib / fsusage.c
blobb670c0c43a1cbf7d536684863d9b8bde17c8feb0
1 /* fsusage.c -- return space usage of mounted file systems
3 Copyright (C) 1991-1992, 1996, 1998-1999, 2002-2006, 2009-2017 Free Software
4 Foundation, Inc.
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.
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.
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/>. */
19 #include <config.h>
21 #include "fsusage.h"
23 #include <limits.h>
24 #include <sys/types.h>
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
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))
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)))
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))
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
86 static 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
93 static int
94 statvfs_works (void)
96 static int statvfs_works_cache = -1;
97 struct utsname name;
98 if (statvfs_works_cache < 0)
99 statvfs_works_cache = (uname (&name) == 0
100 && 0 <= strverscmp (name.release, "2.6.36"));
101 return statvfs_works_cache;
103 # endif
104 #endif
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. */
115 get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)
117 #ifdef STAT_STATVFS /* POSIX, except pre-2.6.36 glibc/Linux */
119 if (statvfs_works ())
121 struct statvfs vfsd;
123 if (statvfs (file, &vfsd) < 0)
124 return -1;
126 /* f_frsize isn't guaranteed to be supported. */
127 fsp->fsu_blocksize = (vfsd.f_frsize
128 ? PROPAGATE_ALL_ONES (vfsd.f_frsize)
129 : PROPAGATE_ALL_ONES (vfsd.f_bsize));
131 fsp->fsu_blocks = PROPAGATE_ALL_ONES (vfsd.f_blocks);
132 fsp->fsu_bfree = PROPAGATE_ALL_ONES (vfsd.f_bfree);
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);
136 fsp->fsu_ffree = PROPAGATE_ALL_ONES (vfsd.f_ffree);
137 return 0;
140 #endif
142 #if defined STAT_STATVFS64 /* AIX */
144 struct statvfs64 fsd;
146 if (statvfs64 (file, &fsd) < 0)
147 return -1;
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));
154 #elif defined STAT_STATFS2_FS_DATA /* Ultrix */
156 struct fs_data fsd;
158 if (statfs (file, &fsd) != 1)
159 return -1;
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);
169 #elif defined STAT_STATFS3_OSF1 /* OSF/1 */
171 struct statfs fsd;
173 if (statfs (file, &fsd, sizeof (struct statfs)) != 0)
174 return -1;
176 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
178 #elif defined STAT_STATFS2_FRSIZE /* 2.6 < glibc/Linux < 2.6.36 */
180 struct statfs fsd;
182 if (statfs (file, &fsd) < 0)
183 return -1;
185 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_frsize);
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 */
191 struct statfs fsd;
193 if (statfs (file, &fsd) < 0)
194 return -1;
196 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
198 # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
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)
207 fsd.f_blocks = fsd.f_spare[0];
208 fsd.f_bfree = fsd.f_spare[1];
209 fsd.f_bavail = fsd.f_spare[2];
211 # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
213 #elif defined STAT_STATFS2_FSIZE /* 4.4BSD and older NetBSD */
215 struct statfs fsd;
217 if (statfs (file, &fsd) < 0)
218 return -1;
220 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
222 #elif defined STAT_STATFS4 /* SVR3, Dynix, old Irix, old AIX, \
223 Dolphin */
225 # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
226 # define f_bavail f_bfree
227 # endif
229 struct statfs fsd;
231 if (statfs (file, &fsd, sizeof fsd, 0) < 0)
232 return -1;
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
243 #endif
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)
249 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
250 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
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);
254 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
256 #endif
258 (void) disk; /* avoid argument-unused warning */
259 return 0;
262 #if defined _AIX && defined _I386
263 /* AIX PS/2 does not supply statfs. */
266 statfs (char *file, struct statfs *fsb)
268 struct stat stats;
269 struct dustat fsd;
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;
287 #endif /* _AIX && _I386 */