1 | /* Convert between the kernel's `struct stat' format, and libc's. |
2 | Copyright (C) 1991-2021 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <sys/stat.h> |
21 | #include <kernel_stat.h> |
22 | #include <sysdep.h> |
23 | |
24 | #if STAT_IS_KERNEL_STAT |
25 | |
26 | /* Dummy. */ |
27 | struct kernel_stat; |
28 | |
29 | #else |
30 | |
31 | #include <string.h> |
32 | |
33 | |
34 | #if XSTAT_IS_XSTAT64 |
35 | int |
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 | |
106 | int |
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 | |
179 | int |
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 | buf->st_ino = kbuf->st_ino; |
193 | if (sizeof (buf->st_ino) != sizeof (kbuf->st_ino) |
194 | && buf->st_ino != kbuf->st_ino) |
195 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW); |
196 | buf->st_mode = kbuf->st_mode; |
197 | buf->st_nlink = kbuf->st_nlink; |
198 | buf->st_uid = kbuf->st_uid; |
199 | buf->st_gid = kbuf->st_gid; |
200 | buf->st_rdev = kbuf->st_rdev; |
201 | #ifdef _HAVE_STAT___PAD2 |
202 | buf->__pad2 = 0; |
203 | #endif |
204 | buf->st_size = kbuf->st_size; |
205 | /* Check for overflow. */ |
206 | if (sizeof (buf->st_size) != sizeof (kbuf->st_size) |
207 | && buf->st_size != kbuf->st_size) |
208 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW); |
209 | buf->st_blksize = kbuf->st_blksize; |
210 | buf->st_blocks = kbuf->st_blocks; |
211 | /* Check for overflow. */ |
212 | if (sizeof (buf->st_blocks) != sizeof (kbuf->st_blocks) |
213 | && buf->st_blocks != kbuf->st_blocks) |
214 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW); |
215 | #ifdef _HAVE_STAT_NSEC |
216 | buf->st_atim.tv_sec = kbuf->st_atim.tv_sec; |
217 | buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec; |
218 | buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec; |
219 | buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec; |
220 | buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec; |
221 | buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec; |
222 | #else |
223 | buf->st_atime = kbuf->st_atime; |
224 | buf->st_mtime = kbuf->st_mtime; |
225 | buf->st_ctime = kbuf->st_ctime; |
226 | #endif |
227 | |
228 | #ifdef _HAVE_STAT___UNUSED1 |
229 | buf->__glibc_reserved1 = 0; |
230 | #endif |
231 | #ifdef _HAVE_STAT___UNUSED2 |
232 | buf->__glibc_reserved2 = 0; |
233 | #endif |
234 | #ifdef _HAVE_STAT___UNUSED3 |
235 | buf->__glibc_reserved3 = 0; |
236 | #endif |
237 | #ifdef _HAVE_STAT___UNUSED4 |
238 | buf->__glibc_reserved4 = 0; |
239 | #endif |
240 | #ifdef _HAVE_STAT___UNUSED5 |
241 | buf->__glibc_reserved5 = 0; |
242 | #endif |
243 | } |
244 | break; |
245 | |
246 | /* If struct stat64 is different from struct stat then |
247 | _STAT_VER_KERNEL does not make sense. */ |
248 | case _STAT_VER_KERNEL: |
249 | default: |
250 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); |
251 | } |
252 | |
253 | return 0; |
254 | } |
255 | |
256 | #endif |
257 | |