[BZ #1978]
[glibc.git] / sysdeps / unix / sysv / linux / xstatconv.c
blob805e3397828bb3917981fabaf2a6a6d1091d4c1f
1 /* Convert between the kernel's `struct stat' format, and libc's.
2 Copyright (C) 1991,1995-1997,2000,2002,2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sys/stat.h>
22 #include <kernel_stat.h>
24 #ifdef STAT_IS_KERNEL_STAT
26 /* Dummy. */
27 struct kernel_stat;
29 #else
31 #include <string.h>
34 #if !defined __ASSUME_STAT64_SYSCALL || defined XSTAT_IS_XSTAT64
35 int
36 __xstat_conv (int vers, struct kernel_stat *kbuf, void *ubuf)
38 switch (vers)
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;
47 case _STAT_VER_LINUX:
49 struct stat *buf = ubuf;
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->__unused1 = 0;
82 #endif
83 #ifdef _HAVE_STAT___UNUSED2
84 buf->__unused2 = 0;
85 #endif
86 #ifdef _HAVE_STAT___UNUSED3
87 buf->__unused3 = 0;
88 #endif
89 #ifdef _HAVE_STAT___UNUSED4
90 buf->__unused4 = 0;
91 #endif
92 #ifdef _HAVE_STAT___UNUSED5
93 buf->__unused5 = 0;
94 #endif
96 break;
98 default:
99 __set_errno (EINVAL);
100 return -1;
103 return 0;
105 #endif
108 __xstat64_conv (int vers, struct kernel_stat *kbuf, void *ubuf)
110 #ifdef XSTAT_IS_XSTAT64
111 return __xstat_conv (vers, kbuf, ubuf);
112 #else
113 switch (vers)
115 case _STAT_VER_LINUX:
117 struct stat64 *buf = ubuf;
119 /* Convert to current kernel version of `struct stat64'. */
120 buf->st_dev = kbuf->st_dev;
121 #ifdef _HAVE_STAT64___PAD1
122 buf->__pad1 = 0;
123 #endif
124 buf->st_ino = kbuf->st_ino;
125 #ifdef _HAVE_STAT64___ST_INO
126 buf->__st_ino = kbuf->st_ino;
127 #endif
128 buf->st_mode = kbuf->st_mode;
129 buf->st_nlink = kbuf->st_nlink;
130 buf->st_uid = kbuf->st_uid;
131 buf->st_gid = kbuf->st_gid;
132 buf->st_rdev = kbuf->st_rdev;
133 #ifdef _HAVE_STAT64___PAD2
134 buf->__pad2 = 0;
135 #endif
136 buf->st_size = kbuf->st_size;
137 buf->st_blksize = kbuf->st_blksize;
138 buf->st_blocks = kbuf->st_blocks;
139 #ifdef _HAVE_STAT64_NSEC
140 buf->st_atim.tv_sec = kbuf->st_atim.tv_sec;
141 buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec;
142 buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec;
143 buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec;
144 buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec;
145 buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec;
146 #else
147 buf->st_atime = kbuf->st_atime;
148 buf->st_mtime = kbuf->st_mtime;
149 buf->st_ctime = kbuf->st_ctime;
150 #endif
151 #ifdef _HAVE_STAT64___UNUSED1
152 buf->__unused1 = 0;
153 #endif
154 #ifdef _HAVE_STAT64___UNUSED2
155 buf->__unused2 = 0;
156 #endif
157 #ifdef _HAVE_STAT64___UNUSED3
158 buf->__unused3 = 0;
159 #endif
160 #ifdef _HAVE_STAT64___UNUSED4
161 buf->__unused4 = 0;
162 #endif
163 #ifdef _HAVE_STAT64___UNUSED5
164 buf->__unused5 = 0;
165 #endif
167 break;
169 /* If struct stat64 is different from struct stat then
170 _STAT_VER_KERNEL does not make sense. */
171 case _STAT_VER_KERNEL:
172 default:
173 __set_errno (EINVAL);
174 return -1;
177 return 0;
178 #endif
182 __xstat32_conv (int vers, struct stat64 *kbuf, struct stat *buf)
184 switch (vers)
186 case _STAT_VER_LINUX:
188 /* Convert current kernel version of `struct stat64' to
189 `struct stat'. */
190 buf->st_dev = kbuf->st_dev;
191 #ifdef _HAVE_STAT___PAD1
192 buf->__pad1 = 0;
193 #endif
194 #ifdef _HAVE_STAT64___ST_INO
195 # if __ASSUME_ST_INO_64_BIT == 0
196 if (kbuf->st_ino == 0)
197 buf->st_ino = kbuf->__st_ino;
198 else
199 # endif
201 buf->st_ino = kbuf->st_ino;
202 if (sizeof (buf->st_ino) != sizeof (kbuf->st_ino)
203 && buf->st_ino != kbuf->st_ino)
205 __set_errno (EOVERFLOW);
206 return -1;
209 #else
210 buf->st_ino = kbuf->st_ino;
211 if (sizeof (buf->st_ino) != sizeof (kbuf->st_ino)
212 && buf->st_ino != kbuf->st_ino)
214 __set_errno (EOVERFLOW);
215 return -1;
217 #endif
218 buf->st_mode = kbuf->st_mode;
219 buf->st_nlink = kbuf->st_nlink;
220 buf->st_uid = kbuf->st_uid;
221 buf->st_gid = kbuf->st_gid;
222 buf->st_rdev = kbuf->st_rdev;
223 #ifdef _HAVE_STAT___PAD2
224 buf->__pad2 = 0;
225 #endif
226 buf->st_size = kbuf->st_size;
227 /* Check for overflow. */
228 if (sizeof (buf->st_size) != sizeof (kbuf->st_size)
229 && buf->st_size != kbuf->st_size)
231 __set_errno (EOVERFLOW);
232 return -1;
234 buf->st_blksize = kbuf->st_blksize;
235 buf->st_blocks = kbuf->st_blocks;
236 /* Check for overflow. */
237 if (sizeof (buf->st_blocks) != sizeof (kbuf->st_blocks)
238 && buf->st_blocks != kbuf->st_blocks)
240 __set_errno (EOVERFLOW);
241 return -1;
243 #ifdef _HAVE_STAT_NSEC
244 buf->st_atim.tv_sec = kbuf->st_atim.tv_sec;
245 buf->st_atim.tv_nsec = kbuf->st_atim.tv_nsec;
246 buf->st_mtim.tv_sec = kbuf->st_mtim.tv_sec;
247 buf->st_mtim.tv_nsec = kbuf->st_mtim.tv_nsec;
248 buf->st_ctim.tv_sec = kbuf->st_ctim.tv_sec;
249 buf->st_ctim.tv_nsec = kbuf->st_ctim.tv_nsec;
250 #else
251 buf->st_atime = kbuf->st_atime;
252 buf->st_mtime = kbuf->st_mtime;
253 buf->st_ctime = kbuf->st_ctime;
254 #endif
256 #ifdef _HAVE_STAT___UNUSED1
257 buf->__unused1 = 0;
258 #endif
259 #ifdef _HAVE_STAT___UNUSED2
260 buf->__unused2 = 0;
261 #endif
262 #ifdef _HAVE_STAT___UNUSED3
263 buf->__unused3 = 0;
264 #endif
265 #ifdef _HAVE_STAT___UNUSED4
266 buf->__unused4 = 0;
267 #endif
268 #ifdef _HAVE_STAT___UNUSED5
269 buf->__unused5 = 0;
270 #endif
272 break;
274 /* If struct stat64 is different from struct stat then
275 _STAT_VER_KERNEL does not make sense. */
276 case _STAT_VER_KERNEL:
277 default:
278 __set_errno (EINVAL);
279 return -1;
282 return 0;
285 #endif