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