Merge branch '1986_confirm_replace_fix'
[free-mc.git] / src / fsusage.c
blobe75b763eaf3ea7c08ce78ddfaeacf87b499efc96
1 /* fsusage.c -- return space usage of mounted file systems
3 Copyright (C) 1991, 1992, 1996, 1998, 1999, 2002, 2003, 2004, 2005, 2006
4 Free Software 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 2, or (at your option)
9 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, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /** \file fsusage.c
21 * \brief Source: return space usage of mounted file systems
23 * Space usage statistics for a file system. Blocks are 512-byte
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #include "fsusage.h"
32 #include <limits.h>
33 #include <sys/types.h>
35 #if STAT_STATVFS /* POSIX 1003.1-2001 (and later) with XSI */
36 # include <sys/statvfs.h>
37 #else
38 /* Don't include backward-compatibility files unless they're needed.
39 Eventually we'd like to remove all this cruft. */
40 # include <fcntl.h>
41 # include <unistd.h>
42 # include <sys/stat.h>
43 # if HAVE_SYS_PARAM_H
44 # include <sys/param.h>
45 # endif
46 # if HAVE_SYS_MOUNT_H
47 # include <sys/mount.h>
48 # endif
49 # if HAVE_SYS_VFS_H
50 # include <sys/vfs.h>
51 # endif
52 # if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
53 # include <sys/fs/s5param.h>
54 # endif
55 # if defined HAVE_SYS_FILSYS_H && !defined _CRAY
56 # include <sys/filsys.h> /* SVR2 */
57 # endif
58 # if HAVE_SYS_STATFS_H
59 # include <sys/statfs.h>
60 # endif
61 # if HAVE_DUSTAT_H /* AIX PS/2 */
62 # include <sys/dustat.h>
63 # endif
64 # include "full-read.h"
65 #endif
67 /* Many space usage primitives use all 1 bits to denote a value that is
68 not applicable or unknown. Propagate this information by returning
69 a uintmax_t value that is all 1 bits if X is all 1 bits, even if X
70 is unsigned and narrower than uintmax_t. */
71 #define PROPAGATE_ALL_ONES(x) \
72 ((sizeof (x) < sizeof (uintmax_t) \
73 && (~ (x) == (sizeof (x) < sizeof (int) \
74 ? - (1 << (sizeof (x) * CHAR_BIT)) \
75 : 0))) \
76 ? UINTMAX_MAX : (uintmax_t) (x))
78 /* Extract the top bit of X as an uintmax_t value. */
79 #define EXTRACT_TOP_BIT(x) ((x) \
80 & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))
82 /* If a value is negative, many space usage primitives store it into an
83 integer variable by assignment, even if the variable's type is unsigned.
84 So, if a space usage variable X's top bit is set, convert X to the
85 uintmax_t value V such that (- (uintmax_t) V) is the negative of
86 the original value. If X's top bit is clear, just yield X.
87 Use PROPAGATE_TOP_BIT if the original value might be negative;
88 otherwise, use PROPAGATE_ALL_ONES. */
89 #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
91 /* Fill in the fields of FSP with information about space usage for
92 the file system on which FILE resides.
93 DISK is the device on which FILE is mounted, for space-getting
94 methods that need to know it.
95 Return 0 if successful, -1 if not. When returning -1, ensure that
96 ERRNO is either a system error value, or zero if DISK is NULL
97 on a system that requires a non-NULL value. */
98 int get_fs_usage (char const *file, char const *disk, struct fs_usage *fsp)
100 #if defined STAT_STATVFS /* POSIX */
102 struct statvfs fsd;
104 if (statvfs (file, &fsd) < 0)
105 return -1;
107 /* f_frsize isn't guaranteed to be supported. */
108 fsp->fsu_blocksize = (fsd.f_frsize
109 ? PROPAGATE_ALL_ONES (fsd.f_frsize)
110 : PROPAGATE_ALL_ONES (fsd.f_bsize));
112 #elif defined STAT_STATFS2_FS_DATA /* Ultrix */
114 struct fs_data fsd;
116 if (statfs (file, &fsd) != 1)
117 return -1;
119 fsp->fsu_blocksize = 1024;
120 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot);
121 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree);
122 fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen);
123 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0;
124 fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot);
125 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);
127 #elif defined STAT_READ_FILSYS /* SVR2 */
128 # ifndef SUPERBOFF
129 # define SUPERBOFF (SUPERB * 512)
130 # endif
132 struct filsys fsd;
133 int fd;
135 if (! disk)
137 errno = 0;
138 return -1;
141 fd = open (disk, O_RDONLY);
142 if (fd < 0)
143 return -1;
144 lseek (fd, (off_t) SUPERBOFF, 0);
145 if (full_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
147 close (fd);
148 return -1;
150 close (fd);
152 fsp->fsu_blocksize = (fsd.s_type == Fs2b ? 1024 : 512);
153 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.s_fsize);
154 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.s_tfree);
155 fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree);
156 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0;
157 fsp->fsu_files = (fsd.s_isize == -1
158 ? UINTMAX_MAX
159 : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1));
160 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode);
162 #elif defined STAT_STATFS3_OSF1
164 struct statfs fsd;
166 if (statfs (file, &fsd, sizeof (struct statfs)) != 0)
167 return -1;
169 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
171 #elif defined STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
173 struct statfs fsd;
175 if (statfs (file, &fsd) < 0)
176 return -1;
178 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
180 # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
182 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
183 struct statfs are truncated to 2GB. These conditions detect that
184 truncation, presumably without botching the 4.1.1 case, in which
185 the values are not truncated. The correct counts are stored in
186 undocumented spare fields. */
187 if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0)
189 fsd.f_blocks = fsd.f_spare[0];
190 fsd.f_bfree = fsd.f_spare[1];
191 fsd.f_bavail = fsd.f_spare[2];
193 # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
195 #elif defined STAT_STATFS2_FSIZE /* 4.4BSD */
197 struct statfs fsd;
199 if (statfs (file, &fsd) < 0)
200 return -1;
202 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
204 #elif defined STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
206 # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
207 # define f_bavail f_bfree
208 # endif
210 struct statfs fsd;
212 if (statfs (file, &fsd, sizeof fsd, 0) < 0)
213 return -1;
215 /* Empirically, the block counts on most SVR3 and SVR3-derived
216 systems seem to always be in terms of 512-byte blocks,
217 no matter what value f_bsize has. */
218 # if _AIX || defined _CRAY
219 fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
220 # else
221 fsp->fsu_blocksize = 512;
222 # endif
224 #endif
226 #if (defined STAT_STATVFS \
227 || (!defined STAT_STATFS2_FS_DATA && !defined STAT_READ_FILSYS))
229 fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
230 fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
231 fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail);
232 fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0;
233 fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files);
234 fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
236 #endif
238 return 0;
241 #if defined _AIX && defined _I386
242 /* AIX PS/2 does not supply statfs. */
243 static int statfs (char *file, struct statfs *fsb)
245 struct stat stats;
246 struct dustat fsd;
248 if (stat (file, &stats) != 0)
249 return -1;
250 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
251 return -1;
252 fsb->f_type = 0;
253 fsb->f_bsize = fsd.du_bsize;
254 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
255 fsb->f_bfree = fsd.du_tfree;
256 fsb->f_bavail = fsd.du_tfree;
257 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
258 fsb->f_ffree = fsd.du_tinode;
259 fsb->f_fsid.val[0] = fsd.du_site;
260 fsb->f_fsid.val[1] = fsd.du_pckno;
261 return 0;
263 #endif /* _AIX && _I386 */