immutable: Add tests.
[gnulib.git] / m4 / fsusage.m4
blobd005579f9e0fd19ccbb0cd801bcc57bc4a8bf5ff
1 # serial 35
2 # Obtaining file system usage information.
4 # Copyright (C) 1997-1998, 2000-2001, 2003-2021 Free Software Foundation, Inc.
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
10 # Written by Jim Meyering.
12 AC_DEFUN([gl_FSUSAGE],
14   AC_CHECK_HEADERS_ONCE([sys/param.h])
15   AC_CHECK_HEADERS_ONCE([sys/vfs.h sys/fs_types.h])
16   AC_CHECK_HEADERS([sys/mount.h], [], [],
17     [AC_INCLUDES_DEFAULT
18      [#if HAVE_SYS_PARAM_H
19        #include <sys/param.h>
20       #endif]])
21   gl_FILE_SYSTEM_USAGE([gl_cv_fs_space=yes], [gl_cv_fs_space=no])
24 # Try to determine how a program can obtain file system usage information.
25 # If successful, define the appropriate symbol (see fsusage.c) and
26 # execute ACTION-IF-FOUND.  Otherwise, execute ACTION-IF-NOT-FOUND.
28 # gl_FILE_SYSTEM_USAGE([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
30 AC_DEFUN([gl_FILE_SYSTEM_USAGE],
32   dnl Enable large-file support. This has the effect of changing the size
33   dnl of field f_blocks in 'struct statvfs' from 32 bit to 64 bit on
34   dnl glibc/Hurd, HP-UX 11, Solaris (32-bit mode). It also changes the size
35   dnl of field f_blocks in 'struct statfs' from 32 bit to 64 bit on
36   dnl Mac OS X >= 10.5 (32-bit mode).
37   AC_REQUIRE([AC_SYS_LARGEFILE])
39   ac_fsusage_space=no
41   # Perform only the link test since it seems there are no variants of the
42   # statvfs function.  This check is more than just AC_CHECK_FUNCS([statvfs])
43   # because that got a false positive on SCO OSR5.  Adding the declaration
44   # of a 'struct statvfs' causes this test to fail (as it should) on such
45   # systems.  That system is reported to work fine with STAT_STATFS4 which
46   # is what it gets when this test fails.
47   if test $ac_fsusage_space = no; then
48     # glibc/{Hurd,kFreeBSD}, FreeBSD >= 5.0, NetBSD >= 3.0,
49     # OpenBSD >= 4.4, AIX, HP-UX, IRIX, Solaris, Cygwin, Interix, BeOS.
50     AC_CACHE_CHECK([for statvfs function (SVR4)],
51       [fu_cv_sys_stat_statvfs],
52       [AC_LINK_IFELSE(
53          [AC_LANG_PROGRAM([[
54 #include <sys/types.h>
55 #ifdef __osf__
56 "Do not use Tru64's statvfs implementation"
57 #endif
59 #include <sys/statvfs.h>
61 struct statvfs fsd;
63 #if defined __APPLE__ && defined __MACH__
64 #include <limits.h>
65 /* On Mac OS X >= 10.5, f_blocks in 'struct statvfs' is a 32-bit quantity;
66    that commonly limits file systems to 4 TiB.  Whereas f_blocks in
67    'struct statfs' is a 64-bit type, thanks to the large-file support
68    that was enabled above.  In this case, don't use statvfs(); use statfs()
69    instead.  */
70 int check_f_blocks_size[sizeof fsd.f_blocks * CHAR_BIT <= 32 ? -1 : 1];
71 #endif
72 ]],
73             [[statvfs (0, &fsd);]])],
74          [fu_cv_sys_stat_statvfs=yes],
75          [fu_cv_sys_stat_statvfs=no])
76       ])
77     if test $fu_cv_sys_stat_statvfs = yes; then
78       ac_fsusage_space=yes
79       # AIX >= 5.2 has statvfs64 that has a wider f_blocks field than statvfs.
80       # glibc, HP-UX, IRIX, Solaris have statvfs64 as well, but on these systems
81       # statvfs with large-file support is already equivalent to statvfs64.
82       AC_CACHE_CHECK([whether to use statvfs64],
83         [fu_cv_sys_stat_statvfs64],
84         [AC_LINK_IFELSE(
85            [AC_LANG_PROGRAM(
86               [[#include <sys/types.h>
87                 #include <sys/statvfs.h>
88                 struct statvfs64 fsd;
89                 int check_f_blocks_larger_in_statvfs64
90                   [sizeof (((struct statvfs64 *) 0)->f_blocks)
91                    > sizeof (((struct statvfs *) 0)->f_blocks)
92                    ? 1 : -1];
93               ]],
94               [[statvfs64 (0, &fsd);]])],
95            [fu_cv_sys_stat_statvfs64=yes],
96            [fu_cv_sys_stat_statvfs64=no])
97         ])
98       if test $fu_cv_sys_stat_statvfs64 = yes; then
99         AC_DEFINE([STAT_STATVFS64], [1],
100           [Define if statvfs64 should be preferred over statvfs.])
101       else
102         AC_DEFINE([STAT_STATVFS], [1],
103           [Define if there is a function named statvfs.  (SVR4)])
104       fi
105     fi
106   fi
108   # Check for this unconditionally so we have a
109   # good fallback on glibc/Linux > 2.6 < 2.6.36
110   AC_CACHE_CHECK([for two-argument statfs with statfs.f_frsize member],
111     [fu_cv_sys_stat_statfs2_frsize],
112     [AC_RUN_IFELSE(
113        [AC_LANG_SOURCE([[
114 #ifdef HAVE_SYS_PARAM_H
115 #include <sys/param.h>
116 #endif
117 #ifdef HAVE_SYS_MOUNT_H
118 #include <sys/mount.h>
119 #endif
120 #ifdef HAVE_SYS_VFS_H
121 #include <sys/vfs.h>
122 #endif
123   int
124   main ()
125   {
126     struct statfs fsd;
127     fsd.f_frsize = 0;
128     return statfs (".", &fsd) != 0;
129   }]])],
130        [fu_cv_sys_stat_statfs2_frsize=yes],
131        [fu_cv_sys_stat_statfs2_frsize=no],
132        [fu_cv_sys_stat_statfs2_frsize=no])
133     ])
134   if test $fu_cv_sys_stat_statfs2_frsize = yes; then
135     ac_fsusage_space=yes
136     AC_DEFINE([STAT_STATFS2_FRSIZE], [1],
137       [Define if statfs takes 2 args and struct statfs has a field named f_frsize.
138        (glibc/Linux > 2.6)])
139   fi
141   if test $ac_fsusage_space = no; then
142     # DEC Alpha running OSF/1
143     AC_CACHE_CHECK([for 3-argument statfs function (DEC OSF/1)],
144       [fu_cv_sys_stat_statfs3_osf1],
145       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
146 #include <sys/param.h>
147 #include <sys/types.h>
148 #include <sys/mount.h>
149   int
150   main ()
151   {
152     struct statfs fsd;
153     fsd.f_fsize = 0;
154     return statfs (".", &fsd, sizeof (struct statfs)) != 0;
155   }]])],
156          [fu_cv_sys_stat_statfs3_osf1=yes],
157          [fu_cv_sys_stat_statfs3_osf1=no],
158          [fu_cv_sys_stat_statfs3_osf1=no])
159       ])
160     if test $fu_cv_sys_stat_statfs3_osf1 = yes; then
161       ac_fsusage_space=yes
162       AC_DEFINE([STAT_STATFS3_OSF1], [1],
163         [Define if statfs takes 3 args.  (DEC Alpha running OSF/1)])
164     fi
165   fi
167   if test $ac_fsusage_space = no; then
168     # glibc/Linux, Mac OS X, FreeBSD < 5.0, NetBSD < 3.0, OpenBSD < 4.4.
169     # (glibc/{Hurd,kFreeBSD}, FreeBSD >= 5.0, NetBSD >= 3.0,
170     # OpenBSD >= 4.4, AIX, HP-UX, OSF/1, Cygwin already handled above.)
171     # (On IRIX you need to include <sys/statfs.h>, not only <sys/mount.h> and
172     # <sys/vfs.h>.)
173     # (On Solaris, statfs has 4 arguments.)
174     AC_CACHE_CHECK([for two-argument statfs with statfs.f_bsize member (AIX, 4.3BSD)],
175       [fu_cv_sys_stat_statfs2_bsize],
176       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
177 #ifdef HAVE_SYS_PARAM_H
178 #include <sys/param.h>
179 #endif
180 #ifdef HAVE_SYS_MOUNT_H
181 #include <sys/mount.h>
182 #endif
183 #ifdef HAVE_SYS_VFS_H
184 #include <sys/vfs.h>
185 #endif
186   int
187   main ()
188   {
189     struct statfs fsd;
190     fsd.f_bsize = 0;
191     return statfs (".", &fsd) != 0;
192   }]])],
193          [fu_cv_sys_stat_statfs2_bsize=yes],
194          [fu_cv_sys_stat_statfs2_bsize=no],
195          [fu_cv_sys_stat_statfs2_bsize=no])
196       ])
197     if test $fu_cv_sys_stat_statfs2_bsize = yes; then
198       ac_fsusage_space=yes
199       AC_DEFINE([STAT_STATFS2_BSIZE], [1],
200         [Define if statfs takes 2 args and struct statfs has a field named f_bsize.
201          (4.3BSD, SunOS 4, HP-UX)])
202     fi
203   fi
205   if test $ac_fsusage_space = no; then
206     # SVR3
207     # (Solaris already handled above.)
208     AC_CACHE_CHECK([for four-argument statfs (SVR3)],
209       [fu_cv_sys_stat_statfs4],
210       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
211 #include <sys/types.h>
212 #include <sys/statfs.h>
213   int
214   main ()
215   {
216     struct statfs fsd;
217     return statfs (".", &fsd, sizeof fsd, 0) != 0;
218   }]])],
219          [fu_cv_sys_stat_statfs4=yes],
220          [fu_cv_sys_stat_statfs4=no],
221          [fu_cv_sys_stat_statfs4=no])
222       ])
223     if test $fu_cv_sys_stat_statfs4 = yes; then
224       ac_fsusage_space=yes
225       AC_DEFINE([STAT_STATFS4], [1],
226         [Define if statfs takes 4 args.  (SVR3, old Irix)])
227     fi
228   fi
230   if test $ac_fsusage_space = no; then
231     # 4.4BSD and older NetBSD
232     # (OSF/1 already handled above.)
233     # (On AIX, you need to include <sys/statfs.h>, not only <sys/mount.h>.)
234     # (On Solaris, statfs has 4 arguments and 'struct statfs' is not declared in
235     # <sys/mount.h>.)
236     AC_CACHE_CHECK([for two-argument statfs with statfs.f_fsize member (4.4BSD and NetBSD)],
237       [fu_cv_sys_stat_statfs2_fsize],
238       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
239 #include <sys/types.h>
240 #ifdef HAVE_SYS_PARAM_H
241 #include <sys/param.h>
242 #endif
243 #ifdef HAVE_SYS_MOUNT_H
244 #include <sys/mount.h>
245 #endif
246   int
247   main ()
248   {
249     struct statfs fsd;
250     fsd.f_fsize = 0;
251     return statfs (".", &fsd) != 0;
252   }]])],
253          [fu_cv_sys_stat_statfs2_fsize=yes],
254          [fu_cv_sys_stat_statfs2_fsize=no],
255          [fu_cv_sys_stat_statfs2_fsize=no])
256       ])
257     if test $fu_cv_sys_stat_statfs2_fsize = yes; then
258       ac_fsusage_space=yes
259       AC_DEFINE([STAT_STATFS2_FSIZE], [1],
260         [Define if statfs takes 2 args and struct statfs has a field named f_fsize.
261          (4.4BSD, NetBSD)])
262     fi
263   fi
265   AS_IF([test $ac_fsusage_space = yes], [$1], [$2])
270 # Check for SunOS statfs brokenness wrt partitions 2GB and larger.
271 # If <sys/vfs.h> exists and struct statfs has a member named f_spare,
272 # enable the work-around code in fsusage.c.
273 AC_DEFUN([gl_STATFS_TRUNCATES],
275   AC_CACHE_CHECK([for statfs that truncates block counts],
276     [fu_cv_sys_truncating_statfs],
277     [AC_COMPILE_IFELSE(
278        [AC_LANG_PROGRAM([[
279 #if !defined(sun) && !defined(__sun)
280 choke -- this is a workaround for a Sun-specific problem
281 #endif
282 #include <sys/types.h>
283 #include <sys/vfs.h>
284          ]],
285          [[struct statfs t; long c = *(t.f_spare);
286            if (c) return 0;
287          ]])],
288        [fu_cv_sys_truncating_statfs=yes],
289        [fu_cv_sys_truncating_statfs=no])
290     ])
291   if test $fu_cv_sys_truncating_statfs = yes; then
292     AC_DEFINE([STATFS_TRUNCATES_BLOCK_COUNTS], [1],
293       [Define if the block counts reported by statfs may be truncated to 2GB
294        and the correct values may be stored in the f_spare array.
295        (SunOS 4.1.2, 4.1.3, and 4.1.3_U1 are reported to have this problem.
296        SunOS 4.1.1 seems not to be affected.)])
297   fi
301 # Prerequisites of lib/fsusage.c not done by gl_FILE_SYSTEM_USAGE.
302 AC_DEFUN([gl_PREREQ_FSUSAGE_EXTRA],
304   AC_CHECK_HEADERS([sys/fs/s5param.h sys/statfs.h])
305   gl_STATFS_TRUNCATES