1 /* Struct stat/stat64 to stat/stat64 conversion for Linux.
2    Copyright (C) 2020-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 <stat_t64_cp.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <time.h>
23 
24 #if __TIMESIZE != 64
25 
26 static inline bool
in_time_t_range(__time64_t t)27 in_time_t_range (__time64_t t)
28 {
29   time_t s = t;
30   return s == t;
31 }
32 
33 static inline struct timespec
valid_timespec64_to_timespec(const struct __timespec64 ts64)34 valid_timespec64_to_timespec (const struct __timespec64 ts64)
35 {
36   struct timespec ts;
37 
38   ts.tv_sec = (time_t) ts64.tv_sec;
39   ts.tv_nsec = ts64.tv_nsec;
40 
41   return ts;
42 }
43 
44 int
__cp_stat64_t64_stat64(const struct __stat64_t64 * st64_t64,struct stat64 * st64)45 __cp_stat64_t64_stat64 (const struct __stat64_t64 *st64_t64,
46 			struct stat64 *st64)
47 {
48   if (! in_time_t_range (st64_t64->st_atim.tv_sec)
49       || ! in_time_t_range (st64_t64->st_mtim.tv_sec)
50       || ! in_time_t_range (st64_t64->st_ctim.tv_sec))
51     {
52       __set_errno (EOVERFLOW);
53       return -1;
54     }
55 
56   /* Clear both pad and reserved fields.  */
57   memset (st64, 0, sizeof (*st64));
58 
59   st64->st_dev = st64_t64->st_dev,
60   st64->st_ino = st64_t64->st_ino;
61   st64->st_mode = st64_t64->st_mode;
62   st64->st_nlink = st64_t64->st_nlink;
63   st64->st_uid = st64_t64->st_uid;
64   st64->st_gid = st64_t64->st_gid;
65   st64->st_rdev = st64_t64->st_rdev;
66   st64->st_size = st64_t64->st_size;
67   st64->st_blksize = st64_t64->st_blksize;
68   st64->st_blocks  = st64_t64->st_blocks;
69   st64->st_atim = valid_timespec64_to_timespec (st64_t64->st_atim);
70   st64->st_mtim = valid_timespec64_to_timespec (st64_t64->st_mtim);
71   st64->st_ctim = valid_timespec64_to_timespec (st64_t64->st_ctim);
72 
73   return 0;
74 }
75 #endif
76