fsusage.c 7.8 KB

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