Fix one left over from last change to statvfs.
[glibc.git] / sysdeps / unix / sysv / linux / internal_statvfs.c
blob59b173ed73663d7c3fe44a05ab47986cbdc85e6a
1 /* Copyright (C) 1998-2006, 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <mntent.h>
23 #include <paths.h>
24 #include <stdbool.h>
25 #include <stdio_ext.h>
26 #include <string.h>
27 #include <sys/mount.h>
28 #include <sys/stat.h>
29 #include <sys/statfs.h>
30 #include <sys/statvfs.h>
31 #include "linux_fsinfo.h"
34 #ifndef STATFS
35 # define STATFS statfs
36 # define STATVFS statvfs
37 # define INTERNAL_STATVFS __internal_statvfs
40 int
41 __statvfs_getflags (const char *name, int fstype, struct stat64 *st)
43 if (st == NULL)
44 return 0;
46 const char *fsname = NULL;
47 const char *fsname2 = NULL;
48 const char *fsname3 = NULL;
50 /* Map the filesystem type we got from the statfs call to a string. */
51 switch (fstype)
53 case EXT2_SUPER_MAGIC:
54 fsname = "ext4";
55 fsname2 = "ext3";
56 fsname3 = "ext2";
57 break;
58 case DEVPTS_SUPER_MAGIC:
59 fsname= "devpts";
60 break;
61 case SHMFS_SUPER_MAGIC:
62 fsname = "tmpfs";
63 break;
64 case PROC_SUPER_MAGIC:
65 fsname = "proc";
66 break;
67 case USBDEVFS_SUPER_MAGIC:
68 fsname = "usbdevfs";
69 break;
70 case AUTOFS_SUPER_MAGIC:
71 fsname = "autofs";
72 break;
73 case NFS_SUPER_MAGIC:
74 fsname = "nfs";
75 break;
76 case SYSFS_MAGIC:
77 fsname = "sysfs";
78 break;
79 case REISERFS_SUPER_MAGIC:
80 fsname = "reiserfs";
81 break;
82 case XFS_SUPER_MAGIC:
83 fsname = "xfs";
84 break;
85 case JFS_SUPER_MAGIC:
86 fsname = "jfs";
87 break;
88 case HPFS_SUPER_MAGIC:
89 fsname = "hpfs";
90 break;
91 case DEVFS_SUPER_MAGIC:
92 fsname = "devfs";
93 break;
94 case ISOFS_SUPER_MAGIC:
95 fsname = "iso9660";
96 break;
97 case MSDOS_SUPER_MAGIC:
98 fsname = "msdos";
99 break;
100 case NTFS_SUPER_MAGIC:
101 fsname = "ntfs";
102 break;
103 case LOGFS_MAGIC_U32:
104 fsname = "logfs";
105 break;
108 FILE *mtab = __setmntent ("/proc/mounts", "r");
109 if (mtab == NULL)
110 mtab = __setmntent (_PATH_MOUNTED, "r");
112 int result = 0;
113 if (mtab != NULL)
115 bool success = false;
116 struct mntent mntbuf;
117 char tmpbuf[1024];
119 /* No locking needed. */
120 (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
122 again:
123 while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
125 /* In a first round we look for a given mount point, if
126 we have a name. */
127 if (name != NULL && strcmp (name, mntbuf.mnt_dir) != 0)
128 continue;
129 /* We need to look at the entry only if the filesystem
130 name matches. If we have a filesystem name. */
131 else if (fsname != NULL
132 && strcmp (fsname, mntbuf.mnt_type) != 0
133 && (fsname2 == NULL
134 || strcmp (fsname2, mntbuf.mnt_type) != 0)
135 && (fsname3 == NULL
136 || strcmp (fsname3, mntbuf.mnt_type) != 0))
137 continue;
139 /* Find out about the device the current entry is for. */
140 struct stat64 fsst;
141 if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
142 && st->st_dev == fsst.st_dev)
144 /* Bingo, we found the entry for the device FD is on.
145 Now interpret the option string. */
146 char *cp = mntbuf.mnt_opts;
147 char *opt;
149 while ((opt = strsep (&cp, ",")) != NULL)
150 if (strcmp (opt, "ro") == 0)
151 result |= ST_RDONLY;
152 else if (strcmp (opt, "nosuid") == 0)
153 result |= ST_NOSUID;
154 else if (strcmp (opt, "noexec") == 0)
155 result |= ST_NOEXEC;
156 else if (strcmp (opt, "nodev") == 0)
157 result |= ST_NODEV;
158 else if (strcmp (opt, "sync") == 0)
159 result |= ST_SYNCHRONOUS;
160 else if (strcmp (opt, "mand") == 0)
161 result |= ST_MANDLOCK;
162 else if (strcmp (opt, "noatime") == 0)
163 result |= ST_NOATIME;
164 else if (strcmp (opt, "nodiratime") == 0)
165 result |= ST_NODIRATIME;
166 else if (strcmp (opt, "relatime") == 0)
167 result |= ST_RELATIME;
169 /* We can stop looking for more entries. */
170 success = true;
171 break;
174 /* Maybe the kernel names for the filesystems changed or the
175 statvfs call got a name which was not the mount point. Check
176 again, this time without checking for name matches first. */
177 if (! success && (name != NULL || fsname != NULL))
179 if (name != NULL)
180 /* Try without a mount point name. */
181 name = NULL;
182 else
184 /* Try without a filesystem name. */
185 assert (fsname != NULL);
186 fsname = fsname2 = fsname3 = NULL;
189 /* It is not strictly allowed to use rewind here. But
190 this code is part of the implementation so it is
191 acceptable. */
192 rewind (mtab);
194 goto again;
197 /* Close the file. */
198 __endmntent (mtab);
201 return result;
203 #else
204 extern int __statvfs_getflags (const char *name, int fstype,
205 struct stat64 *st);
206 #endif
209 void
210 INTERNAL_STATVFS (const char *name, struct STATVFS *buf,
211 struct STATFS *fsbuf, struct stat64 *st)
213 /* Now fill in the fields we have information for. */
214 buf->f_bsize = fsbuf->f_bsize;
215 /* Linux has the f_frsize size only in later version of the kernel.
216 If the value is not filled in use f_bsize. */
217 buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
218 buf->f_blocks = fsbuf->f_blocks;
219 buf->f_bfree = fsbuf->f_bfree;
220 buf->f_bavail = fsbuf->f_bavail;
221 buf->f_files = fsbuf->f_files;
222 buf->f_ffree = fsbuf->f_ffree;
223 if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
224 buf->f_fsid = (fsbuf->f_fsid.__val[0]
225 | ((unsigned long int) fsbuf->f_fsid.__val[1]
226 << (8 * (sizeof (buf->f_fsid)
227 - sizeof (fsbuf->f_fsid.__val[0])))));
228 else
229 /* We cannot help here. The statvfs element is not large enough to
230 contain both words of the statfs f_fsid field. */
231 buf->f_fsid = fsbuf->f_fsid.__val[0];
232 #ifdef _STATVFSBUF_F_UNUSED
233 buf->__f_unused = 0;
234 #endif
235 buf->f_namemax = fsbuf->f_namelen;
236 memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
238 /* What remains to do is to fill the fields f_favail and f_flag. */
240 /* XXX I have no idea how to compute f_favail. Any idea??? */
241 buf->f_favail = buf->f_ffree;
243 /* Determining the flags is tricky. We have to read /proc/mounts or
244 the /etc/mtab file and search for the entry which matches the given
245 file. The way we can test for matching filesystem is using the
246 device number. */
247 buf->f_flag = __statvfs_getflags (name, fsbuf->f_type, st);