4
0

fsusage.c 7.7 KB

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