internal_statvfs.c 3.7 KB

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