fsusage.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* fsusage.c -- return space usage of mounted filesystems
  2. Copyright (C) 1991, 1992, 1996, 1998, 1999 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "../plugins/config.h"
  15. #if HAVE_INTTYPES_H
  16. # include <inttypes.h>
  17. #endif
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include "fsusage.h"
  21. #if HAVE_LIMITS_H
  22. # include <limits.h>
  23. #endif
  24. #ifndef CHAR_BIT
  25. # define CHAR_BIT 8
  26. #endif
  27. int statfs ();
  28. #if HAVE_SYS_PARAM_H
  29. # include <sys/param.h>
  30. #endif
  31. #if HAVE_SYS_MOUNT_H
  32. # include <sys/mount.h>
  33. #endif
  34. #if HAVE_SYS_VFS_H
  35. # include <sys/vfs.h>
  36. #endif
  37. #if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
  38. # include <sys/fs/s5param.h>
  39. #endif
  40. #if defined (HAVE_SYS_FILSYS_H) && !defined (_CRAY)
  41. # include <sys/filsys.h> /* SVR2 */
  42. #endif
  43. #if HAVE_FCNTL_H
  44. # include <fcntl.h>
  45. #endif
  46. #if HAVE_SYS_STATFS_H
  47. # include <sys/statfs.h>
  48. #endif
  49. #if HAVE_DUSTAT_H /* AIX PS/2 */
  50. # include <sys/dustat.h>
  51. #endif
  52. #if HAVE_SYS_STATVFS_H /* SVR4 */
  53. # include <sys/statvfs.h>
  54. int statvfs ();
  55. #endif
  56. /* Many space usage primitives use all 1 bits to denote a value that is
  57. not applicable or unknown. Propagate this information by returning
  58. a uintmax_t value that is all 1 bits if the argument is all 1 bits,
  59. even if the argument is unsigned and smaller than uintmax_t. */
  60. #define PROPAGATE_ALL_ONES(x) ((x) == -1 ? (uintmax_t) -1 : (uintmax_t) (x))
  61. /* Extract the top bit of X as an uintmax_t value. */
  62. #define EXTRACT_TOP_BIT(x) ((x) \
  63. & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))
  64. /* If a value is negative, many space usage primitives store it into an
  65. integer variable by assignment, even if the variable's type is unsigned.
  66. So, if a space usage variable X's top bit is set, convert X to the
  67. uintmax_t value V such that (- (uintmax_t) V) is the negative of
  68. the original value. If X's top bit is clear, just yield X.
  69. Use PROPAGATE_TOP_BIT if the original value might be negative;
  70. otherwise, use PROPAGATE_ALL_ONES. */
  71. #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
  72. int safe_read ();
  73. /* Fill in the fields of FSP with information about space usage for
  74. the filesystem on which PATH resides.
  75. DISK is the device on which PATH is mounted, for space-getting
  76. methods that need to know it.
  77. Return 0 if successful, -1 if not. When returning -1, ensure that
  78. ERRNO is either a system error value, or zero if DISK is NULL
  79. on a system that requires a non-NULL value. */
  80. int
  81. get_fs_usage (const char *path, const char *disk, struct fs_usage *fsp)
  82. {
  83. #ifdef STAT_STATFS3_OSF1
  84. struct statfs fsd;
  85. if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
  86. return -1;
  87. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  88. #endif /* STAT_STATFS3_OSF1 */
  89. #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
  90. struct fs_data fsd;
  91. if (statfs (path, &fsd) != 1)
  92. return -1;
  93. fsp->fsu_blocksize = 1024;
  94. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot);
  95. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree);
  96. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen);
  97. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0;
  98. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot);
  99. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);
  100. #endif /* STAT_STATFS2_FS_DATA */
  101. #ifdef STAT_READ_FILSYS /* SVR2 */
  102. # ifndef SUPERBOFF
  103. # define SUPERBOFF (SUPERB * 512)
  104. # endif
  105. struct filsys fsd;
  106. int fd;
  107. if (! disk)
  108. {
  109. errno = 0;
  110. return -1;
  111. }
  112. fd = open (disk, O_RDONLY);
  113. if (fd < 0)
  114. return -1;
  115. lseek (fd, (off_t) SUPERBOFF, 0);
  116. if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
  117. {
  118. close (fd);
  119. return -1;
  120. }
  121. close (fd);
  122. fsp->fsu_blocksize = (fsd.s_type == Fs2b ? 1024 : 512);
  123. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.s_fsize);
  124. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.s_tfree);
  125. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree);
  126. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0;
  127. fsp->fsu_files = (fsd.s_isize == -1
  128. ? (uintmax_t) -1
  129. : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1));
  130. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode);
  131. #endif /* STAT_READ_FILSYS */
  132. #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
  133. struct statfs fsd;
  134. if (statfs (path, &fsd) < 0)
  135. return -1;
  136. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  137. # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
  138. /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
  139. struct statfs are truncated to 2GB. These conditions detect that
  140. truncation, presumably without botching the 4.1.1 case, in which
  141. the values are not truncated. The correct counts are stored in
  142. undocumented spare fields. */
  143. if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0)
  144. {
  145. fsd.f_blocks = fsd.f_spare[0];
  146. fsd.f_bfree = fsd.f_spare[1];
  147. fsd.f_bavail = fsd.f_spare[2];
  148. }
  149. # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
  150. #endif /* STAT_STATFS2_BSIZE */
  151. #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
  152. struct statfs fsd;
  153. if (statfs (path, &fsd) < 0)
  154. return -1;
  155. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);
  156. #endif /* STAT_STATFS2_FSIZE */
  157. #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
  158. # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
  159. # define f_bavail f_bfree
  160. # endif
  161. struct statfs fsd;
  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 || defined(_CRAY)
  168. fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);
  169. # else
  170. fsp->fsu_blocksize = 512;
  171. # endif
  172. #endif /* STAT_STATFS4 */
  173. #ifdef STAT_STATVFS /* SVR4 */
  174. struct statvfs fsd;
  175. if (statvfs (path, &fsd) < 0)
  176. return -1;
  177. /* f_frsize isn't guaranteed to be supported. */
  178. fsp->fsu_blocksize =
  179. PROPAGATE_ALL_ONES (fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize);
  180. #endif /* STAT_STATVFS */
  181. #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS)
  182. /* !Ultrix && !SVR2 */
  183. fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks);
  184. fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree);
  185. fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail);
  186. fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0;
  187. fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files);
  188. fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);
  189. #endif /* not STAT_STATFS2_FS_DATA && not STAT_READ_FILSYS */
  190. return 0;
  191. }
  192. #if defined(_AIX) && defined(_I386)
  193. /* AIX PS/2 does not supply statfs. */
  194. int
  195. statfs (char *path, struct statfs *fsb)
  196. {
  197. struct stat stats;
  198. struct dustat fsd;
  199. if (stat (path, &stats))
  200. return -1;
  201. if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  202. return -1;
  203. fsb->f_type = 0;
  204. fsb->f_bsize = fsd.du_bsize;
  205. fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  206. fsb->f_bfree = fsd.du_tfree;
  207. fsb->f_bavail = fsd.du_tfree;
  208. fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
  209. fsb->f_ffree = fsd.du_tinode;
  210. fsb->f_fsid.val[0] = fsd.du_site;
  211. fsb->f_fsid.val[1] = fsd.du_pckno;
  212. return 0;
  213. }
  214. #endif /* _AIX && _I386 */