1/* Convert between the kernel's `struct stat' format, and libc's.
2 Copyright (C) 1991-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <http://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <sys/stat.h>
21#include <kernel_stat.h>
22#include <kernel-features.h>
23
24#ifdef STAT_IS_KERNEL_STAT
25
26/* Dummy. */
27struct kernel_stat;
28
29#else
30
31#include <string.h>
32
33
34#if XSTAT_IS_XSTAT64
35int
36__xstat_conv (int vers, struct kernel_stat *kbuf, void *ubuf)
37{
38 switch (vers)
39 {
40 case _STAT_VER_KERNEL:
41 /* Nothing to do. The struct is in the form the kernel expects.
42 We should have short-circuted before we got here, but for
43 completeness... */
44 *(struct kernel_stat *) ubuf = *kbuf;
45 break;
46
47 case _STAT_VER_LINUX:
48 {
49 struct stat *buf = ubuf;
50
51 /* Convert to current kernel version of `struct stat'. */
52 buf->st_dev = kbuf->st_dev;
53#ifdef _HAVE_STAT___PAD1
54 buf->__pad1 = 0;
55#endif
56 buf->st_ino = kbuf->st_ino;
57 buf->st_mode = kbuf->st_mode;
58 buf->st_nlink = kbuf->st_nlink;
59 buf->st_uid = kbuf->st_uid;
60 buf->st_gid = kbuf->st_gid;
61 buf->st_rdev = kbuf->st_rdev;
62#ifdef _HAVE_STAT___PAD2
63 buf->__pad2 = 0;
64#endif
65 buf->st_size = kbuf->st_size;
66 buf->st_blksize = kbuf->st_blksize;
67 buf->st_blocks = kbuf->st_blocks;
68#ifdef _HAVE_STAT_NSEC
69 buf->st_atim.tv_sec = kbuf->st_atim.tv_sec;
70 buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec;
71 buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec;
72 buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec;
73 buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec;
74 buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec;
75#else
76 buf->st_atime = kbuf->st_atime;
77 buf->st_mtime = kbuf->st_mtime;
78 buf->st_ctime = kbuf->st_ctime;
79#endif
80#ifdef _HAVE_STAT___UNUSED1
81 buf->__glibc_reserved1 = 0;
82#endif
83#ifdef _HAVE_STAT___UNUSED2
84 buf->__glibc_reserved2 = 0;
85#endif
86#ifdef _HAVE_STAT___UNUSED3
87 buf->__glibc_reserved3 = 0;
88#endif
89#ifdef _HAVE_STAT___UNUSED4
90 buf->__glibc_reserved4 = 0;
91#endif
92#ifdef _HAVE_STAT___UNUSED5
93 buf->__glibc_reserved5 = 0;
94#endif
95 }
96 break;
97
98 default:
99 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
100 }
101
102 return 0;
103}
104#endif
105
106int
107__xstat64_conv (int vers, struct kernel_stat *kbuf, void *ubuf)
108{
109#if XSTAT_IS_XSTAT64
110 return __xstat_conv (vers, kbuf, ubuf);
111#else
112 switch (vers)
113 {
114 case _STAT_VER_LINUX:
115 {
116 struct stat64 *buf = ubuf;
117
118 /* Convert to current kernel version of `struct stat64'. */
119 buf->st_dev = kbuf->st_dev;
120#ifdef _HAVE_STAT64___PAD1
121 buf->__pad1 = 0;
122#endif
123 buf->st_ino = kbuf->st_ino;
124#ifdef _HAVE_STAT64___ST_INO
125 buf->__st_ino = kbuf->st_ino;
126#endif
127 buf->st_mode = kbuf->st_mode;
128 buf->st_nlink = kbuf->st_nlink;
129 buf->st_uid = kbuf->st_uid;
130 buf->st_gid = kbuf->st_gid;
131 buf->st_rdev = kbuf->st_rdev;
132#ifdef _HAVE_STAT64___PAD2
133 buf->__pad2 = 0;
134#endif
135 buf->st_size = kbuf->st_size;
136 buf->st_blksize = kbuf->st_blksize;
137 buf->st_blocks = kbuf->st_blocks;
138#ifdef _HAVE_STAT64_NSEC
139 buf->st_atim.tv_sec = kbuf->st_atim.tv_sec;
140 buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec;
141 buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec;
142 buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec;
143 buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec;
144 buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec;
145#else
146 buf->st_atime = kbuf->st_atime;
147 buf->st_mtime = kbuf->st_mtime;
148 buf->st_ctime = kbuf->st_ctime;
149#endif
150#ifdef _HAVE_STAT64___UNUSED1
151 buf->__glibc_reserved1 = 0;
152#endif
153#ifdef _HAVE_STAT64___UNUSED2
154 buf->__glibc_reserved2 = 0;
155#endif
156#ifdef _HAVE_STAT64___UNUSED3
157 buf->__glibc_reserved3 = 0;
158#endif
159#ifdef _HAVE_STAT64___UNUSED4
160 buf->__glibc_reserved4 = 0;
161#endif
162#ifdef _HAVE_STAT64___UNUSED5
163 buf->__glibc_reserved5 = 0;
164#endif
165 }
166 break;
167
168 /* If struct stat64 is different from struct stat then
169 _STAT_VER_KERNEL does not make sense. */
170 case _STAT_VER_KERNEL:
171 default:
172 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
173 }
174
175 return 0;
176#endif
177}
178
179int
180__xstat32_conv (int vers, struct stat64 *kbuf, struct stat *buf)
181{
182 switch (vers)
183 {
184 case _STAT_VER_LINUX:
185 {
186 /* Convert current kernel version of `struct stat64' to
187 `struct stat'. */
188 buf->st_dev = kbuf->st_dev;
189#ifdef _HAVE_STAT___PAD1
190 buf->__pad1 = 0;
191#endif
192#ifdef _HAVE_STAT64___ST_INO
193# if !__ASSUME_ST_INO_64_BIT
194 if (kbuf->st_ino == 0)
195 buf->st_ino = kbuf->__st_ino;
196 else
197# endif
198 {
199 buf->st_ino = kbuf->st_ino;
200 if (sizeof (buf->st_ino) != sizeof (kbuf->st_ino)
201 && buf->st_ino != kbuf->st_ino)
202 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
203 }
204#else
205 buf->st_ino = kbuf->st_ino;
206 if (sizeof (buf->st_ino) != sizeof (kbuf->st_ino)
207 && buf->st_ino != kbuf->st_ino)
208 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
209#endif
210 buf->st_mode = kbuf->st_mode;
211 buf->st_nlink = kbuf->st_nlink;
212 buf->st_uid = kbuf->st_uid;
213 buf->st_gid = kbuf->st_gid;
214 buf->st_rdev = kbuf->st_rdev;
215#ifdef _HAVE_STAT___PAD2
216 buf->__pad2 = 0;
217#endif
218 buf->st_size = kbuf->st_size;
219 /* Check for overflow. */
220 if (sizeof (buf->st_size) != sizeof (kbuf->st_size)
221 && buf->st_size != kbuf->st_size)
222 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
223 buf->st_blksize = kbuf->st_blksize;
224 buf->st_blocks = kbuf->st_blocks;
225 /* Check for overflow. */
226 if (sizeof (buf->st_blocks) != sizeof (kbuf->st_blocks)
227 && buf->st_blocks != kbuf->st_blocks)
228 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
229#ifdef _HAVE_STAT_NSEC
230 buf->st_atim.tv_sec = kbuf->st_atim.tv_sec;
231 buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec;
232 buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec;
233 buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec;
234 buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec;
235 buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec;
236#else
237 buf->st_atime = kbuf->st_atime;
238 buf->st_mtime = kbuf->st_mtime;
239 buf->st_ctime = kbuf->st_ctime;
240#endif
241
242#ifdef _HAVE_STAT___UNUSED1
243 buf->__glibc_reserved1 = 0;
244#endif
245#ifdef _HAVE_STAT___UNUSED2
246 buf->__glibc_reserved2 = 0;
247#endif
248#ifdef _HAVE_STAT___UNUSED3
249 buf->__glibc_reserved3 = 0;
250#endif
251#ifdef _HAVE_STAT___UNUSED4
252 buf->__glibc_reserved4 = 0;
253#endif
254#ifdef _HAVE_STAT___UNUSED5
255 buf->__glibc_reserved5 = 0;
256#endif
257 }
258 break;
259
260 /* If struct stat64 is different from struct stat then
261 _STAT_VER_KERNEL does not make sense. */
262 case _STAT_VER_KERNEL:
263 default:
264 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
265 }
266
267 return 0;
268}
269
270#endif
271