1 | /* Copyright (C) 1998-2021 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 <sys/statfs.h> |
20 | #include <sys/statvfs.h> |
21 | #include <internal_statvfs.h> |
22 | #include <string.h> |
23 | #include <time.h> |
24 | #include <kernel_stat.h> |
25 | |
26 | /* Special internal-only bit value. */ |
27 | # define ST_VALID 0x0020 |
28 | |
29 | #if !STATFS_IS_STATFS64 |
30 | void |
31 | __internal_statvfs (struct statvfs *buf, const struct statfs *fsbuf) |
32 | { |
33 | /* Now fill in the fields we have information for. */ |
34 | buf->f_bsize = fsbuf->f_bsize; |
35 | /* Linux has the f_frsize size only in later version of the kernel. |
36 | If the value is not filled in use f_bsize. */ |
37 | buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize; |
38 | buf->f_blocks = fsbuf->f_blocks; |
39 | buf->f_bfree = fsbuf->f_bfree; |
40 | buf->f_bavail = fsbuf->f_bavail; |
41 | buf->f_files = fsbuf->f_files; |
42 | buf->f_ffree = fsbuf->f_ffree; |
43 | if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid)) |
44 | /* The shifting uses 'unsigned long long int' even though the target |
45 | field might only have 32 bits. This is OK since the 'if' branch |
46 | is not used in this case but the compiler would still generate |
47 | warnings. */ |
48 | buf->f_fsid = ((fsbuf->f_fsid.__val[0] |
49 | & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1)) |
50 | | ((unsigned long long int) fsbuf->f_fsid.__val[1] |
51 | << (8 * (sizeof (buf->f_fsid) |
52 | - sizeof (fsbuf->f_fsid.__val[0]))))); |
53 | else |
54 | /* We cannot help here. The statvfs element is not large enough to |
55 | contain both words of the statfs f_fsid field. */ |
56 | buf->f_fsid = fsbuf->f_fsid.__val[0]; |
57 | #ifdef _STATVFSBUF_F_UNUSED |
58 | buf->__f_unused = 0; |
59 | #endif |
60 | buf->f_namemax = fsbuf->f_namelen; |
61 | memset (buf->__f_spare, '\0', sizeof (buf->__f_spare)); |
62 | |
63 | /* What remains to do is to fill the fields f_favail and f_flag. */ |
64 | |
65 | /* XXX I have no idea how to compute f_favail. Any idea??? */ |
66 | buf->f_favail = buf->f_ffree; |
67 | |
68 | buf->f_flag = fsbuf->f_flags ^ ST_VALID; |
69 | } |
70 | #endif |
71 | |
72 | void |
73 | __internal_statvfs64 (struct statvfs64 *buf, const struct statfs64 *fsbuf) |
74 | { |
75 | /* Now fill in the fields we have information for. */ |
76 | buf->f_bsize = fsbuf->f_bsize; |
77 | /* Linux has the f_frsize size only in later version of the kernel. |
78 | If the value is not filled in use f_bsize. */ |
79 | buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize; |
80 | buf->f_blocks = fsbuf->f_blocks; |
81 | buf->f_bfree = fsbuf->f_bfree; |
82 | buf->f_bavail = fsbuf->f_bavail; |
83 | buf->f_files = fsbuf->f_files; |
84 | buf->f_ffree = fsbuf->f_ffree; |
85 | if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid)) |
86 | /* The shifting uses 'unsigned long long int' even though the target |
87 | field might only have 32 bits. This is OK since the 'if' branch |
88 | is not used in this case but the compiler would still generate |
89 | warnings. */ |
90 | buf->f_fsid = ((fsbuf->f_fsid.__val[0] |
91 | & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1)) |
92 | | ((unsigned long long int) fsbuf->f_fsid.__val[1] |
93 | << (8 * (sizeof (buf->f_fsid) |
94 | - sizeof (fsbuf->f_fsid.__val[0]))))); |
95 | else |
96 | /* We cannot help here. The statvfs element is not large enough to |
97 | contain both words of the statfs f_fsid field. */ |
98 | buf->f_fsid = fsbuf->f_fsid.__val[0]; |
99 | #ifdef _STATVFSBUF_F_UNUSED |
100 | buf->__f_unused = 0; |
101 | #endif |
102 | buf->f_namemax = fsbuf->f_namelen; |
103 | memset (buf->__f_spare, '\0', sizeof (buf->__f_spare)); |
104 | |
105 | /* What remains to do is to fill the fields f_favail and f_flag. */ |
106 | |
107 | /* XXX I have no idea how to compute f_favail. Any idea??? */ |
108 | buf->f_favail = buf->f_ffree; |
109 | |
110 | buf->f_flag = fsbuf->f_flags ^ ST_VALID; |
111 | } |
112 | |