internal_statvfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 1998, 1999, 2000, 2001 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. /* The kernel hints us if the f_flags is valid */
  16. #define ST_VALID 0x0020
  17. /* Now fill in the fields we have information for. */
  18. buf->f_bsize = fsbuf.f_bsize;
  19. #ifdef _STATFS_F_FRSIZE
  20. buf->f_frsize = fsbuf.f_frsize;
  21. #else
  22. /* No support for f_frsize so set it to the full block size. */
  23. buf->f_frsize = fsbuf.f_bsize;
  24. #endif
  25. buf->f_blocks = fsbuf.f_blocks;
  26. buf->f_bfree = fsbuf.f_bfree;
  27. buf->f_bavail = fsbuf.f_bavail;
  28. buf->f_files = fsbuf.f_files;
  29. buf->f_ffree = fsbuf.f_ffree;
  30. if (sizeof (buf->f_fsid) == sizeof (fsbuf.f_fsid))
  31. buf->f_fsid = (fsbuf.f_fsid.__val[0]
  32. | ((unsigned long long int) fsbuf.f_fsid.__val[1]
  33. << (8 * (sizeof (buf->f_fsid)
  34. - sizeof (fsbuf.f_fsid.__val[0])))));
  35. else
  36. /* We cannot help here. The statvfs element is not large enough to
  37. contain both words of the statfs f_fsid field. */
  38. buf->f_fsid = fsbuf.f_fsid.__val[0];
  39. #ifdef _STATVFSBUF_F_UNUSED
  40. buf->__f_unused = 0;
  41. #endif
  42. buf->f_namemax = fsbuf.f_namelen;
  43. memset (buf->__f_spare, '\0', sizeof(buf->__f_spare));
  44. /* XXX I have no idea how to compute f_favail. Any idea??? */
  45. buf->f_favail = buf->f_ffree;
  46. /* Determining the flags is tricky. We have to read /proc/mounts or
  47. the /etc/mtab file and search for the entry which matches the given
  48. file. The way we can test for matching filesystem is using the
  49. device number. */
  50. buf->f_flag = 0;
  51. if (STAT (&st) >= 0
  52. #ifdef _STATFS_F_FLAGS
  53. && (fsbuf.f_flags & ST_VALID) == 0
  54. #endif
  55. ) {
  56. int save_errno = errno;
  57. struct mntent mntbuf;
  58. FILE *mtab;
  59. mtab = setmntent ("/proc/mounts", "r");
  60. if (mtab == NULL)
  61. mtab = setmntent (_PATH_MOUNTED, "r");
  62. if (mtab != NULL) {
  63. char tmpbuf[1024];
  64. while (getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf))) {
  65. struct stat fsst;
  66. /* Find out about the device the current entry is for. */
  67. if (stat (mntbuf.mnt_dir, &fsst) >= 0
  68. && st.st_dev == fsst.st_dev) {
  69. /* Bingo, we found the entry for the device FD is on.
  70. Now interpret the option string. */
  71. char *cp = mntbuf.mnt_opts;
  72. char *opt;
  73. while ((opt = strsep (&cp, ",")) != NULL)
  74. if (strcmp (opt, "ro") == 0)
  75. buf->f_flag |= ST_RDONLY;
  76. else if (strcmp (opt, "nosuid") == 0)
  77. buf->f_flag |= ST_NOSUID;
  78. #ifdef __USE_GNU
  79. else if (strcmp (opt, "noexec") == 0)
  80. buf->f_flag |= ST_NOEXEC;
  81. else if (strcmp (opt, "nodev") == 0)
  82. buf->f_flag |= ST_NODEV;
  83. else if (strcmp (opt, "sync") == 0)
  84. buf->f_flag |= ST_SYNCHRONOUS;
  85. else if (strcmp (opt, "mand") == 0)
  86. buf->f_flag |= ST_MANDLOCK;
  87. else if (strcmp (opt, "noatime") == 0)
  88. buf->f_flag |= ST_NOATIME;
  89. else if (strcmp (opt, "nodiratime") == 0)
  90. buf->f_flag |= ST_NODIRATIME;
  91. else if (strcmp (opt, "relatime") == 0)
  92. buf->f_flag |= ST_RELATIME;
  93. #endif
  94. /* We can stop looking for more entries. */
  95. break;
  96. }
  97. }
  98. /* Close the file. */
  99. endmntent (mtab);
  100. }
  101. __set_errno (save_errno);
  102. }
  103. #ifdef _STATFS_F_FLAGS
  104. else
  105. buf->f_flag = fsbuf.f_flags ^ ST_VALID;
  106. #endif