stat-size.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* macros useful in interpreting size-related values in struct stat.
  2. Copyright (C) 1989, 1991-2016 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 3 of the License, or
  6. (at your option) 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, see <http://www.gnu.org/licenses/>. */
  13. /* *INDENT-OFF* */
  14. /*
  15. Macros defined by this file (s is an rvalue of type struct stat):
  16. DEV_BSIZE: The device blocksize. But use ST_NBLOCKSIZE instead.
  17. ST_BLKSIZE(s): Preferred (in the sense of best performance) I/O blocksize
  18. for the file, in bytes.
  19. ST_NBLOCKS(s): Number of blocks in the file, including indirect blocks.
  20. ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.
  21. */
  22. /* *INDENT-ON* */
  23. #ifndef STAT_SIZE_H
  24. #define STAT_SIZE_H
  25. /* sys/param.h may define DEV_BSIZE */
  26. #if HAVE_SYS_PARAM_H
  27. #include <sys/param.h>
  28. #endif
  29. /* Get or fake the disk device blocksize.
  30. Usually defined by sys/param.h (if at all). */
  31. #if !defined DEV_BSIZE && defined BSIZE
  32. #define DEV_BSIZE BSIZE
  33. #endif
  34. #if !defined DEV_BSIZE && defined BBSIZE /* SGI sys/param.h */
  35. #define DEV_BSIZE BBSIZE
  36. #endif
  37. #ifndef DEV_BSIZE
  38. #define DEV_BSIZE 4096
  39. #endif
  40. /* Extract or fake data from a 'struct stat'.
  41. ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
  42. ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
  43. ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
  44. #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
  45. #define ST_BLKSIZE(statbuf) DEV_BSIZE
  46. /* coreutils' fileblocks.c also uses BSIZE. */
  47. #if defined _POSIX_SOURCE || !defined BSIZE
  48. #define ST_NBLOCKS(statbuf) \
  49. ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
  50. #else
  51. /* This definition calls st_blocks, which is in the fileblocks module. */
  52. #define ST_NBLOCKS(statbuf) \
  53. (S_ISREG ((statbuf).st_mode) || S_ISDIR ((statbuf).st_mode) ? st_blocks ((statbuf).st_size) : 0)
  54. #endif
  55. #else
  56. /* When running 'rsh hpux11-system cat any-file', cat would
  57. determine that the output stream had an st_blksize of 2147421096.
  58. Conversely st_blksize can be 2 GiB (or maybe even larger) with XFS
  59. on 64-bit hosts. Somewhat arbitrarily, limit the "optimal" block
  60. size to SIZE_MAX / 8 + 1. (Dividing SIZE_MAX by only 4 wouldn't
  61. suffice, since "cat" sometimes multiplies the result by 4.) If
  62. anyone knows of a system for which this limit is too small, please
  63. report it as a bug in this code. */
  64. #define ST_BLKSIZE(statbuf) \
  65. ((0 < (statbuf).st_blksize && (size_t) ((statbuf).st_blksize) <= ((size_t) -1) / 8 + 1) \
  66. ? (size_t) ((statbuf).st_blksize) \
  67. : DEV_BSIZE)
  68. #if defined hpux || defined __hpux__ || defined __hpux
  69. /* HP-UX counts st_blocks in 1024-byte units.
  70. This loses when mixing HP-UX and BSD file systems with NFS. */
  71. #define ST_NBLOCKSIZE 1024
  72. #endif
  73. #endif
  74. #ifndef ST_NBLOCKS
  75. #define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
  76. #endif
  77. #ifndef ST_NBLOCKSIZE
  78. #ifdef S_BLKSIZE
  79. #define ST_NBLOCKSIZE S_BLKSIZE
  80. #else
  81. #define ST_NBLOCKSIZE 512
  82. #endif
  83. #endif
  84. #endif /* STAT_SIZE_H */