internal_statvfs.c 3.6 KB

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