Back to index

glibc  2.9
internal_statvfs.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998-2003, 2004, 2005, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <mntent.h>
00023 #include <paths.h>
00024 #include <stdbool.h>
00025 #include <stdio_ext.h>
00026 #include <string.h>
00027 #include <sys/mount.h>
00028 #include <sys/stat.h>
00029 #include <sys/statfs.h>
00030 #include <sys/statvfs.h>
00031 #include "linux_fsinfo.h"
00032 
00033 
00034 #ifndef STATFS
00035 # define STATFS statfs
00036 # define STATVFS statvfs
00037 # define INTERNAL_STATVFS __internal_statvfs
00038 
00039 
00040 int
00041 __statvfs_getflags (const char *name, int fstype, struct stat64 *st)
00042 {
00043   if (st == NULL)
00044     return 0;
00045 
00046   const char *fsname = NULL;
00047   const char *fsname2 = NULL;
00048 
00049   /* Map the filesystem type we got from the statfs call to a string.  */
00050   switch (fstype)
00051     {
00052     case EXT2_SUPER_MAGIC:
00053       fsname = "ext3";
00054       fsname2 = "ext2";
00055       break;
00056     case DEVPTS_SUPER_MAGIC:
00057       fsname= "devpts";
00058       break;
00059     case SHMFS_SUPER_MAGIC:
00060       fsname = "tmpfs";
00061       break;
00062     case PROC_SUPER_MAGIC:
00063       fsname = "proc";
00064       break;
00065     case USBDEVFS_SUPER_MAGIC:
00066       fsname = "usbdevfs";
00067       break;
00068     case AUTOFS_SUPER_MAGIC:
00069       fsname = "autofs";
00070       break;
00071     case NFS_SUPER_MAGIC:
00072       fsname = "nfs";
00073       break;
00074     case SYSFS_MAGIC:
00075       fsname = "sysfs";
00076       break;
00077     case REISERFS_SUPER_MAGIC:
00078       fsname = "reiserfs";
00079       break;
00080     case XFS_SUPER_MAGIC:
00081       fsname = "xfs";
00082       break;
00083     case JFS_SUPER_MAGIC:
00084       fsname = "jfs";
00085       break;
00086     case HPFS_SUPER_MAGIC:
00087       fsname = "hpfs";
00088       break;
00089     case DEVFS_SUPER_MAGIC:
00090       fsname = "devfs";
00091       break;
00092     case ISOFS_SUPER_MAGIC:
00093       fsname = "iso9660";
00094       break;
00095     case MSDOS_SUPER_MAGIC:
00096       fsname = "msdos";
00097       break;
00098     case NTFS_SUPER_MAGIC:
00099       fsname = "ntfs";
00100       break;
00101     }
00102 
00103   FILE *mtab = __setmntent ("/proc/mounts", "r");
00104   if (mtab == NULL)
00105     mtab = __setmntent (_PATH_MOUNTED, "r");
00106 
00107   int result = 0;
00108   if (mtab != NULL)
00109     {
00110       bool success = false;
00111       struct mntent mntbuf;
00112       char tmpbuf[1024];
00113 
00114       /* No locking needed.  */
00115       (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
00116 
00117     again:
00118       while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
00119        {
00120          /* In a first round we look for a given mount point, if
00121             we have a name.  */
00122          if (name != NULL && strcmp (name, mntbuf.mnt_dir) != 0)
00123            continue;
00124          /* We need to look at the entry only if the filesystem
00125             name matches.  If we have a filesystem name.  */
00126          else if (fsname != NULL
00127                  && strcmp (fsname, mntbuf.mnt_type) != 0
00128                  && (fsname2 == NULL
00129                      || strcmp (fsname2, mntbuf.mnt_type) != 0))
00130            continue;
00131 
00132          /* Find out about the device the current entry is for.  */
00133          struct stat64 fsst;
00134          if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
00135              && st->st_dev == fsst.st_dev)
00136            {
00137              /* Bingo, we found the entry for the device FD is on.
00138                Now interpret the option string.  */
00139              char *cp = mntbuf.mnt_opts;
00140              char *opt;
00141 
00142              while ((opt = strsep (&cp, ",")) != NULL)
00143               if (strcmp (opt, "ro") == 0)
00144                 result |= ST_RDONLY;
00145               else if (strcmp (opt, "nosuid") == 0)
00146                 result |= ST_NOSUID;
00147               else if (strcmp (opt, "noexec") == 0)
00148                 result |= ST_NOEXEC;
00149               else if (strcmp (opt, "nodev") == 0)
00150                 result |= ST_NODEV;
00151               else if (strcmp (opt, "sync") == 0)
00152                 result |= ST_SYNCHRONOUS;
00153               else if (strcmp (opt, "mand") == 0)
00154                 result |= ST_MANDLOCK;
00155               else if (strcmp (opt, "noatime") == 0)
00156                 result |= ST_NOATIME;
00157               else if (strcmp (opt, "nodiratime") == 0)
00158                 result |= ST_NODIRATIME;
00159               else if (strcmp (opt, "relatime") == 0)
00160                 result |= ST_RELATIME;
00161 
00162              /* We can stop looking for more entries.  */
00163              success = true;
00164              break;
00165            }
00166        }
00167       /* Maybe the kernel names for the filesystems changed or the
00168         statvfs call got a name which was not the mount point.  Check
00169         again, this time without checking for name matches first.  */
00170       if (! success && (name != NULL || fsname != NULL))
00171        {
00172          if (name != NULL)
00173            /* Try without a mount point name.  */
00174            name = NULL;
00175          else
00176            {
00177              /* Try without a filesystem name.  */
00178              assert (fsname != NULL);
00179              fsname = fsname2 = NULL;
00180            }
00181 
00182          /* It is not strictly allowed to use rewind here.  But
00183             this code is part of the implementation so it is
00184             acceptable.  */
00185          rewind (mtab);
00186 
00187          goto again;
00188        }
00189 
00190       /* Close the file.  */
00191       __endmntent (mtab);
00192     }
00193 
00194   return result;
00195 }
00196 #else
00197 extern int __statvfs_getflags (const char *name, int fstype,
00198                             struct stat64 *st);
00199 #endif
00200 
00201 
00202 void
00203 INTERNAL_STATVFS (const char *name, struct STATVFS *buf,
00204                 struct STATFS *fsbuf, struct stat64 *st)
00205 {
00206   /* Now fill in the fields we have information for.  */
00207   buf->f_bsize = fsbuf->f_bsize;
00208   /* Linux has the f_frsize size only in later version of the kernel.
00209      If the value is not filled in use f_bsize.  */
00210   buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
00211   buf->f_blocks = fsbuf->f_blocks;
00212   buf->f_bfree = fsbuf->f_bfree;
00213   buf->f_bavail = fsbuf->f_bavail;
00214   buf->f_files = fsbuf->f_files;
00215   buf->f_ffree = fsbuf->f_ffree;
00216   if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
00217     buf->f_fsid = (fsbuf->f_fsid.__val[0]
00218                  | ((unsigned long int) fsbuf->f_fsid.__val[1]
00219                     << (8 * (sizeof (buf->f_fsid)
00220                             - sizeof (fsbuf->f_fsid.__val[0])))));
00221   else
00222     /* We cannot help here.  The statvfs element is not large enough to
00223        contain both words of the statfs f_fsid field.  */
00224     buf->f_fsid = fsbuf->f_fsid.__val[0];
00225 #ifdef _STATVFSBUF_F_UNUSED
00226   buf->__f_unused = 0;
00227 #endif
00228   buf->f_namemax = fsbuf->f_namelen;
00229   memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
00230 
00231   /* What remains to do is to fill the fields f_favail and f_flag.  */
00232 
00233   /* XXX I have no idea how to compute f_favail.  Any idea???  */
00234   buf->f_favail = buf->f_ffree;
00235 
00236   /* Determining the flags is tricky.  We have to read /proc/mounts or
00237      the /etc/mtab file and search for the entry which matches the given
00238      file.  The way we can test for matching filesystem is using the
00239      device number.  */
00240   buf->f_flag = __statvfs_getflags (name, fsbuf->f_type, st);
00241 }