| 1 | /* Copyright (C) 1998-2020 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <errno.h> |
| 21 | #include <mntent.h> |
| 22 | #include <paths.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio_ext.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/mount.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/statfs.h> |
| 29 | #include "internal_statvfs.h" |
| 30 | #include "linux_fsinfo.h" |
| 31 | #include <kernel-features.h> |
| 32 | |
| 33 | |
| 34 | /* Special internal-only bit value. */ |
| 35 | #define ST_VALID 0x0020 |
| 36 | |
| 37 | |
| 38 | #ifndef STATFS |
| 39 | # define STATFS statfs |
| 40 | # define STATVFS statvfs |
| 41 | # define INTERNAL_STATVFS __internal_statvfs |
| 42 | #else |
| 43 | extern int __statvfs_getflags (const char *name, int fstype, int fd); |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | void |
| 48 | INTERNAL_STATVFS (const char *name, struct STATVFS *buf, |
| 49 | struct STATFS *fsbuf, int fd) |
| 50 | { |
| 51 | /* Now fill in the fields we have information for. */ |
| 52 | buf->f_bsize = fsbuf->f_bsize; |
| 53 | /* Linux has the f_frsize size only in later version of the kernel. |
| 54 | If the value is not filled in use f_bsize. */ |
| 55 | buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize; |
| 56 | buf->f_blocks = fsbuf->f_blocks; |
| 57 | buf->f_bfree = fsbuf->f_bfree; |
| 58 | buf->f_bavail = fsbuf->f_bavail; |
| 59 | buf->f_files = fsbuf->f_files; |
| 60 | buf->f_ffree = fsbuf->f_ffree; |
| 61 | if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid)) |
| 62 | /* The shifting uses 'unsigned long long int' even though the target |
| 63 | field might only have 32 bits. This is OK since the 'if' branch |
| 64 | is not used in this case but the compiler would still generate |
| 65 | warnings. */ |
| 66 | buf->f_fsid = ((fsbuf->f_fsid.__val[0] |
| 67 | & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1)) |
| 68 | | ((unsigned long long int) fsbuf->f_fsid.__val[1] |
| 69 | << (8 * (sizeof (buf->f_fsid) |
| 70 | - sizeof (fsbuf->f_fsid.__val[0]))))); |
| 71 | else |
| 72 | /* We cannot help here. The statvfs element is not large enough to |
| 73 | contain both words of the statfs f_fsid field. */ |
| 74 | buf->f_fsid = fsbuf->f_fsid.__val[0]; |
| 75 | #ifdef _STATVFSBUF_F_UNUSED |
| 76 | buf->__f_unused = 0; |
| 77 | #endif |
| 78 | buf->f_namemax = fsbuf->f_namelen; |
| 79 | memset (buf->__f_spare, '\0', sizeof (buf->__f_spare)); |
| 80 | |
| 81 | /* What remains to do is to fill the fields f_favail and f_flag. */ |
| 82 | |
| 83 | /* XXX I have no idea how to compute f_favail. Any idea??? */ |
| 84 | buf->f_favail = buf->f_ffree; |
| 85 | |
| 86 | buf->f_flag = fsbuf->f_flags ^ ST_VALID; |
| 87 | } |
| 88 | |