Use SAFE_STAT instead of stat to avoid unnecessary failure
[gnulib.git] / lib / fsusage.c
1 /* fsusage.c -- return space usage of mounted filesystems
2    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22    (which it would do because it found this file in $srcdir).  */
23 #include <config.h>
24 #else
25 #include "config.h"
26 #endif
27 #endif
28
29 #include <sys/types.h>
30 #include "fsusage.h"
31 #include "safe-stat.h"
32
33 int statfs ();
34
35 #if defined (STAT_STATFS3_OSF1) /* DEC Alpha running OSF/1 */
36 #  include <sys/mount.h>
37 #endif
38
39 #if defined(STAT_STATFS2_BSIZE) && !defined(_IBMR2) /* 4.3BSD, SunOS 4, HP-UX, AIX PS/2.  */
40 #include <sys/vfs.h>
41 #endif
42
43 #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
44 #include <sys/mount.h>
45 #endif
46
47 #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #endif
51
52 #ifdef STAT_READ                /* SVR2.  */
53 #include <sys/param.h>
54 #include <sys/filsys.h>
55 #include <fcntl.h>
56 #endif
57
58 #if defined(STAT_STATFS4) || (defined(_AIX) && defined(_IBMR2)) /* SVR3, Dynix, Irix, AIX RS6000.  */
59 #include <sys/statfs.h>
60 #endif
61
62 #if defined(_AIX) && defined(_I386) /* AIX PS/2.  */
63 #include <sys/stat.h>
64 #include <sys/dustat.h>
65 #endif
66
67 #ifdef STAT_STATVFS             /* SVR4.  */
68 #include <sys/statvfs.h>
69 int statvfs ();
70 #endif
71
72 /* Return the number of TOSIZE-byte blocks used by
73    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
74    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
75
76 static long
77 adjust_blocks (blocks, fromsize, tosize)
78      long blocks;
79      int fromsize, tosize;
80 {
81   if (tosize <= 0)
82     abort ();
83   if (fromsize <= 0)
84     return -1;
85                                                                     
86   if (fromsize == tosize)       /* E.g., from 512 to 512.  */
87     return blocks;
88   else if (fromsize > tosize)   /* E.g., from 2048 to 512.  */
89     return blocks * (fromsize / tosize);
90   else                          /* E.g., from 256 to 512.  */
91     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
92 }
93
94 /* Fill in the fields of FSP with information about space usage for
95    the filesystem on which PATH resides.
96    DISK is the device on which PATH is mounted, for space-getting
97    methods that need to know it.
98    Return 0 if successful, -1 if not. */
99
100 int
101 get_fs_usage (path, disk, fsp)
102      char *path, *disk;
103      struct fs_usage *fsp;
104 {
105 #if defined (STAT_STATFS3_OSF1)
106   struct statfs fsd;
107
108   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
109     return -1;
110 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
111 #endif /* STAT_STATFS3_OSF1 */
112
113 #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
114   struct fs_data fsd;
115
116   if (statfs (path, &fsd) != 1)
117     return -1;
118 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
119   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
120   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
121   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
122   fsp->fsu_files = fsd.fd_req.gtot;
123   fsp->fsu_ffree = fsd.fd_req.gfree;
124 #endif
125
126 #ifdef STAT_READ                /* SVR2.  */
127 #ifndef SUPERBOFF
128 #define SUPERBOFF (SUPERB * 512)
129 #endif
130   struct filsys fsd;
131   int fd;
132
133   fd = open (disk, O_RDONLY);
134   if (fd < 0)
135     return -1;
136   lseek (fd, (long) SUPERBOFF, 0);
137   if (read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
138     {
139       close (fd);
140       return -1;
141     }
142   close (fd);
143 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
144   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
145   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
146   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
147   fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
148   fsp->fsu_ffree = fsd.s_tinode;
149 #endif
150
151 #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
152   struct statfs fsd;
153
154   if (statfs (path, &fsd) < 0)
155     return -1;
156 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
157 #endif
158
159 #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
160   struct statfs fsd;
161
162   if (statfs (path, &fsd) < 0)
163     return -1;
164 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
165 #endif
166
167 #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix.  */
168   struct statfs fsd;
169
170   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
171     return -1;
172   /* Empirically, the block counts on most SVR3 and SVR3-derived
173      systems seem to always be in terms of 512-byte blocks,
174      no matter what value f_bsize has.  */
175 #define CONVERT_BLOCKS(b) (b)
176 #ifndef _SEQUENT_               /* _SEQUENT_ is DYNIX/ptx.  */
177 #ifndef DOLPHIN                 /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
178 #define f_bavail f_bfree
179 #endif
180 #endif
181 #endif
182
183 #ifdef STAT_STATVFS             /* SVR4.  */
184   struct statvfs fsd;
185
186   if (statvfs (path, &fsd) < 0)
187     return -1;
188   /* f_frsize isn't guaranteed to be supported.  */
189 #define CONVERT_BLOCKS(b) \
190   adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
191 #endif
192
193 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ) /* !Ultrix && !SVR2.  */
194   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
195   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
196   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
197   fsp->fsu_files = fsd.f_files;
198   fsp->fsu_ffree = fsd.f_ffree;
199 #endif
200
201   return 0;
202 }
203
204 #if defined(_AIX) && defined(_I386)
205 /* AIX PS/2 does not supply statfs.  */
206
207 int
208 statfs (path, fsb)
209      char *path;
210      struct statfs *fsb;
211 {
212   struct stat stats;
213   struct dustat fsd;
214
215   if (SAFE_STAT (path, &stats))
216     return -1;
217   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
218     return -1;
219   fsb->f_type   = 0;
220   fsb->f_bsize  = fsd.du_bsize;
221   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
222   fsb->f_bfree  = fsd.du_tfree;
223   fsb->f_bavail = fsd.du_tfree;
224   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
225   fsb->f_ffree  = fsd.du_tinode;
226   fsb->f_fsid.val[0] = fsd.du_site;
227   fsb->f_fsid.val[1] = fsd.du_pckno;
228   return 0;
229 }
230 #endif /* _AIX && _I386 */