internal_statvfs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 1998-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <mntent.h>
  18. #include <paths.h>
  19. #include <stdbool.h>
  20. #include <stdio_ext.h>
  21. #include <string.h>
  22. #include <sys/mount.h>
  23. #include <sys/stat.h>
  24. #include <sys/statfs.h>
  25. #include "internal_statvfs.h"
  26. #include "linux_fsinfo.h"
  27. #include <kernel-features.h>
  28. /* Special internal-only bit value. */
  29. #define ST_VALID 0x0020
  30. #ifndef STATFS
  31. # define STATFS statfs
  32. # define STATVFS statvfs
  33. # define INTERNAL_STATVFS __internal_statvfs
  34. #else
  35. extern int __statvfs_getflags (const char *name, int fstype, int fd);
  36. #endif
  37. void
  38. INTERNAL_STATVFS (const char *name, struct STATVFS *buf,
  39. struct STATFS *fsbuf, int fd)
  40. {
  41. /* Now fill in the fields we have information for. */
  42. buf->f_bsize = fsbuf->f_bsize;
  43. /* Linux has the f_frsize size only in later version of the kernel.
  44. If the value is not filled in use f_bsize. */
  45. buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
  46. buf->f_blocks = fsbuf->f_blocks;
  47. buf->f_bfree = fsbuf->f_bfree;
  48. buf->f_bavail = fsbuf->f_bavail;
  49. buf->f_files = fsbuf->f_files;
  50. buf->f_ffree = fsbuf->f_ffree;
  51. if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
  52. /* The shifting uses 'unsigned long long int' even though the target
  53. field might only have 32 bits. This is OK since the 'if' branch
  54. is not used in this case but the compiler would still generate
  55. warnings. */
  56. buf->f_fsid = ((fsbuf->f_fsid.__val[0]
  57. & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1))
  58. | ((unsigned long long int) fsbuf->f_fsid.__val[1]
  59. << (8 * (sizeof (buf->f_fsid)
  60. - sizeof (fsbuf->f_fsid.__val[0])))));
  61. else
  62. /* We cannot help here. The statvfs element is not large enough to
  63. contain both words of the statfs f_fsid field. */
  64. buf->f_fsid = fsbuf->f_fsid.__val[0];
  65. #ifdef _STATVFSBUF_F_UNUSED
  66. buf->__f_unused = 0;
  67. #endif
  68. buf->f_namemax = fsbuf->f_namelen;
  69. memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
  70. /* What remains to do is to fill the fields f_favail and f_flag. */
  71. /* XXX I have no idea how to compute f_favail. Any idea??? */
  72. buf->f_favail = buf->f_ffree;
  73. buf->f_flag = fsbuf->f_flags ^ ST_VALID;
  74. }