1/* Copyright (C) 1998-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <sys/statfs.h>
19#include <sys/statvfs.h>
20#include <internal_statvfs.h>
21#include <string.h>
22#include <time.h>
23#include <kernel_stat.h>
24
25/* Special internal-only bit value. */
26# define ST_VALID 0x0020
27
28#if !STATFS_IS_STATFS64
29void
30__internal_statvfs (struct statvfs *buf, const struct statfs *fsbuf)
31{
32 /* Now fill in the fields we have information for. */
33 buf->f_bsize = fsbuf->f_bsize;
34 /* Linux has the f_frsize size only in later version of the kernel.
35 If the value is not filled in use f_bsize. */
36 buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
37 buf->f_blocks = fsbuf->f_blocks;
38 buf->f_bfree = fsbuf->f_bfree;
39 buf->f_bavail = fsbuf->f_bavail;
40 buf->f_files = fsbuf->f_files;
41 buf->f_ffree = fsbuf->f_ffree;
42 if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
43 /* The shifting uses 'unsigned long long int' even though the target
44 field might only have 32 bits. This is OK since the 'if' branch
45 is not used in this case but the compiler would still generate
46 warnings. */
47 buf->f_fsid = ((fsbuf->f_fsid.__val[0]
48 & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1))
49 | ((unsigned long long int) fsbuf->f_fsid.__val[1]
50 << (8 * (sizeof (buf->f_fsid)
51 - sizeof (fsbuf->f_fsid.__val[0])))));
52 else
53 /* We cannot help here. The statvfs element is not large enough to
54 contain both words of the statfs f_fsid field. */
55 buf->f_fsid = fsbuf->f_fsid.__val[0];
56#ifdef _STATVFSBUF_F_UNUSED
57 buf->__f_unused = 0;
58#endif
59 buf->f_namemax = fsbuf->f_namelen;
60 buf->f_type = fsbuf->f_type;
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
72void
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 buf->f_type = fsbuf->f_type;
104 memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
105
106 /* What remains to do is to fill the fields f_favail and f_flag. */
107
108 /* XXX I have no idea how to compute f_favail. Any idea??? */
109 buf->f_favail = buf->f_ffree;
110
111 buf->f_flag = fsbuf->f_flags ^ ST_VALID;
112}
113

source code of glibc/sysdeps/unix/sysv/linux/internal_statvfs.c