(get_fs_usage, statfs): Use safe_read, and safe_stat rather than
[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 #include <config.h>
20 #endif
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include "fsusage.h"
25 #include "safe-stat.h"
26
27 int statfs ();
28
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32
33 #ifdef HAVE_SYS_MOUNT_H
34 #include <sys/mount.h>
35 #endif
36
37 #ifdef HAVE_SYS_VFS_H
38 #include <sys/vfs.h>
39 #endif
40
41 #ifdef HAVE_SYS_FILSYS_H
42 #include <sys/filsys.h>         /* SVR2.  */
43 #endif
44
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
48
49 #ifdef HAVE_SYS_STATFS_H
50 #include <sys/statfs.h>
51 #endif
52
53 #ifdef HAVE_DUSTAT_H            /* AIX PS/2.  */
54 #include <sys/dustat.h>
55 #endif
56
57 #ifdef HAVE_SYS_STATVFS_H       /* SVR4.  */
58 #include <sys/statvfs.h>
59 int statvfs ();
60 #endif
61
62 int safe_read ();
63
64 /* Return the number of TOSIZE-byte blocks used by
65    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
66    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
67
68 static long
69 adjust_blocks (blocks, fromsize, tosize)
70      long blocks;
71      int fromsize, tosize;
72 {
73   if (tosize <= 0)
74     abort ();
75   if (fromsize <= 0)
76     return -1;
77                                                                     
78   if (fromsize == tosize)       /* E.g., from 512 to 512.  */
79     return blocks;
80   else if (fromsize > tosize)   /* E.g., from 2048 to 512.  */
81     return blocks * (fromsize / tosize);
82   else                          /* E.g., from 256 to 512.  */
83     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
84 }
85
86 /* Fill in the fields of FSP with information about space usage for
87    the filesystem on which PATH resides.
88    DISK is the device on which PATH is mounted, for space-getting
89    methods that need to know it.
90    Return 0 if successful, -1 if not. */
91
92 int
93 get_fs_usage (path, disk, fsp)
94      char *path, *disk;
95      struct fs_usage *fsp;
96 {
97 #if defined (STAT_STATFS3_OSF1)
98   struct statfs fsd;
99
100   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
101     return -1;
102 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
103 #endif /* STAT_STATFS3_OSF1 */
104
105 #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
106   struct fs_data fsd;
107
108   if (statfs (path, &fsd) != 1)
109     return -1;
110 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
111   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
112   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
113   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
114   fsp->fsu_files = fsd.fd_req.gtot;
115   fsp->fsu_ffree = fsd.fd_req.gfree;
116 #endif
117
118 #ifdef STAT_READ_FILSYS         /* SVR2.  */
119 #ifndef SUPERBOFF
120 #define SUPERBOFF (SUPERB * 512)
121 #endif
122   struct filsys fsd;
123   int fd;
124
125   fd = open (disk, O_RDONLY);
126   if (fd < 0)
127     return -1;
128   lseek (fd, (long) SUPERBOFF, 0);
129   if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
130     {
131       close (fd);
132       return -1;
133     }
134   close (fd);
135 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
136   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
137   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
138   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
139   fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
140   fsp->fsu_ffree = fsd.s_tinode;
141 #endif
142
143 #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
144   struct statfs fsd;
145
146   if (statfs (path, &fsd) < 0)
147     return -1;
148 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
149 #endif
150
151 #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
152   struct statfs fsd;
153
154   if (statfs (path, &fsd) < 0)
155     return -1;
156 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
157 #endif
158
159 #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix, AIX.  */
160   struct statfs fsd;
161
162   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
163     return -1;
164   /* Empirically, the block counts on most SVR3 and SVR3-derived
165      systems seem to always be in terms of 512-byte blocks,
166      no matter what value f_bsize has.  */
167 # if _AIX
168 #  define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
169 # else
170 #  define CONVERT_BLOCKS(b) (b)
171 #  ifndef _SEQUENT_             /* _SEQUENT_ is DYNIX/ptx.  */
172 #   ifndef DOLPHIN              /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
173 #    define f_bavail f_bfree
174 #   endif
175 #  endif
176 # endif
177 #endif
178
179 #ifdef STAT_STATVFS             /* SVR4.  */
180   struct statvfs fsd;
181
182   if (statvfs (path, &fsd) < 0)
183     return -1;
184   /* f_frsize isn't guaranteed to be supported.  */
185 #define CONVERT_BLOCKS(b) \
186   adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
187 #endif
188
189 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2.  */
190   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
191   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
192   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
193   fsp->fsu_files = fsd.f_files;
194   fsp->fsu_ffree = fsd.f_ffree;
195 #endif
196
197   return 0;
198 }
199
200 #if defined(_AIX) && defined(_I386)
201 /* AIX PS/2 does not supply statfs.  */
202
203 int
204 statfs (path, fsb)
205      char *path;
206      struct statfs *fsb;
207 {
208   struct stat stats;
209   struct dustat fsd;
210
211   if (safe_stat (path, &stats))
212     return -1;
213   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
214     return -1;
215   fsb->f_type   = 0;
216   fsb->f_bsize  = fsd.du_bsize;
217   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
218   fsb->f_bfree  = fsd.du_tfree;
219   fsb->f_bavail = fsd.du_tfree;
220   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
221   fsb->f_ffree  = fsd.du_tinode;
222   fsb->f_fsid.val[0] = fsd.du_site;
223   fsb->f_fsid.val[1] = fsd.du_pckno;
224   return 0;
225 }
226 #endif /* _AIX && _I386 */