internal_statvfs.c 4.2 KB

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