1 /* Copyright (C) 1998-2022 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
29 void
__internal_statvfs(struct statvfs * buf,const struct statfs * fsbuf)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   memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
61 
62   /* What remains to do is to fill the fields f_favail and f_flag.  */
63 
64   /* XXX I have no idea how to compute f_favail.  Any idea???  */
65   buf->f_favail = buf->f_ffree;
66 
67   buf->f_flag = fsbuf->f_flags ^ ST_VALID;
68 }
69 #endif
70 
71 void
__internal_statvfs64(struct statvfs64 * buf,const struct statfs64 * fsbuf)72 __internal_statvfs64 (struct statvfs64 *buf, const struct statfs64 *fsbuf)
73 {
74   /* Now fill in the fields we have information for.  */
75   buf->f_bsize = fsbuf->f_bsize;
76   /* Linux has the f_frsize size only in later version of the kernel.
77      If the value is not filled in use f_bsize.  */
78   buf->f_frsize = fsbuf->f_frsize ?: fsbuf->f_bsize;
79   buf->f_blocks = fsbuf->f_blocks;
80   buf->f_bfree = fsbuf->f_bfree;
81   buf->f_bavail = fsbuf->f_bavail;
82   buf->f_files = fsbuf->f_files;
83   buf->f_ffree = fsbuf->f_ffree;
84   if (sizeof (buf->f_fsid) == sizeof (fsbuf->f_fsid))
85     /* The shifting uses 'unsigned long long int' even though the target
86        field might only have 32 bits.  This is OK since the 'if' branch
87        is not used in this case but the compiler would still generate
88        warnings.  */
89     buf->f_fsid = ((fsbuf->f_fsid.__val[0]
90 		    & ((1ULL << (8 * sizeof (fsbuf->f_fsid.__val[0]))) - 1))
91 		   | ((unsigned long long int) fsbuf->f_fsid.__val[1]
92 		      << (8 * (sizeof (buf->f_fsid)
93 			       - sizeof (fsbuf->f_fsid.__val[0])))));
94   else
95     /* We cannot help here.  The statvfs element is not large enough to
96        contain both words of the statfs f_fsid field.  */
97     buf->f_fsid = fsbuf->f_fsid.__val[0];
98 #ifdef _STATVFSBUF_F_UNUSED
99   buf->__f_unused = 0;
100 #endif
101   buf->f_namemax = fsbuf->f_namelen;
102   memset (buf->__f_spare, '\0', sizeof (buf->__f_spare));
103 
104   /* What remains to do is to fill the fields f_favail and f_flag.  */
105 
106   /* XXX I have no idea how to compute f_favail.  Any idea???  */
107   buf->f_favail = buf->f_ffree;
108 
109   buf->f_flag = fsbuf->f_flags ^ ST_VALID;
110 }
111