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