1 /* rename.c -- rename a file, preserving symlinks.
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #if defined HAVE_UTIMES
27 #elif defined HAVE_GOOD_UTIME_H
31 /* The number of bytes to copy at once. */
34 /* Copy file FROMFD to file TO, performing no translations.
35 Return 0 if ok, -1 if error. */
38 simple_copy (int fromfd
, const char *to
,
39 struct stat
*target_stat ATTRIBUTE_UNUSED
)
46 || lseek (fromfd
, 0, SEEK_SET
) != 0)
49 tofd
= open (to
, O_WRONLY
| O_TRUNC
| O_BINARY
);
58 while ((nread
= read (fromfd
, buf
, sizeof buf
)) > 0)
60 if (write (tofd
, buf
, nread
) != nread
)
72 #if !defined (_WIN32) || defined (__CYGWIN32__)
73 /* Writing to a setuid/setgid file may clear S_ISUID and S_ISGID.
74 Try to restore them, ignoring failure. */
75 if (target_stat
!= NULL
)
76 fchmod (tofd
, target_stat
->st_mode
);
89 /* The following defines and inline functions are copied from gnulib.
90 FIXME: Use a gnulib import and stat-time.h instead. */
91 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
92 # if defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
93 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
95 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
97 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
98 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
99 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
100 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
101 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
102 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
105 static inline long int get_stat_atime_ns (struct stat
const *) ATTRIBUTE_UNUSED
;
106 static inline long int get_stat_mtime_ns (struct stat
const *) ATTRIBUTE_UNUSED
;
108 /* Return the nanosecond component of *ST's access time. */
109 static inline long int
110 get_stat_atime_ns (struct stat
const *st ATTRIBUTE_UNUSED
)
112 # if defined STAT_TIMESPEC
113 return STAT_TIMESPEC (st
, st_atim
).tv_nsec
;
114 # elif defined STAT_TIMESPEC_NS
115 return STAT_TIMESPEC_NS (st
, st_atim
);
121 /* Return the nanosecond component of *ST's data modification time. */
122 static inline long int
123 get_stat_mtime_ns (struct stat
const *st ATTRIBUTE_UNUSED
)
125 # if defined STAT_TIMESPEC
126 return STAT_TIMESPEC (st
, st_mtim
).tv_nsec
;
127 # elif defined STAT_TIMESPEC_NS
128 return STAT_TIMESPEC_NS (st
, st_mtim
);
134 /* Return *ST's access time. */
135 static inline struct timespec
136 get_stat_atime (struct stat
const *st
)
139 return STAT_TIMESPEC (st
, st_atim
);
142 t
.tv_sec
= st
->st_atime
;
143 t
.tv_nsec
= get_stat_atime_ns (st
);
148 /* Return *ST's data modification time. */
149 static inline struct timespec
150 get_stat_mtime (struct stat
const *st
)
153 return STAT_TIMESPEC (st
, st_mtim
);
156 t
.tv_sec
= st
->st_mtime
;
157 t
.tv_nsec
= get_stat_mtime_ns (st
);
163 /* Set the times of the file DESTINATION to be the same as those in
167 set_times (const char *destination
, const struct stat
*statbuf
)
170 #if defined HAVE_UTIMENSAT
171 struct timespec times
[2];
172 times
[0] = get_stat_atime (statbuf
);
173 times
[1] = get_stat_mtime (statbuf
);
174 result
= utimensat (AT_FDCWD
, destination
, times
, 0);
175 #elif defined HAVE_UTIMES
176 struct timeval tv
[2];
178 tv
[0].tv_sec
= statbuf
->st_atime
;
179 tv
[0].tv_usec
= get_stat_atime_ns (statbuf
) / 1000;
180 tv
[1].tv_sec
= statbuf
->st_mtime
;
181 tv
[1].tv_usec
= get_stat_mtime_ns (statbuf
) / 1000;
182 result
= utimes (destination
, tv
);
183 #elif defined HAVE_GOOD_UTIME_H
186 tb
.actime
= statbuf
->st_atime
;
187 tb
.modtime
= statbuf
->st_mtime
;
188 result
= utime (destination
, &tb
);
192 tb
[0] = statbuf
->st_atime
;
193 tb
[1] = statbuf
->st_mtime
;
194 result
= utime (destination
, tb
);
198 non_fatal (_("%s: cannot set time: %s"), destination
, strerror (errno
));
201 /* Copy FROM to TO. TARGET_STAT has the file status that, if non-NULL,
202 is used to fix up timestamps. Return 0 if ok, -1 if error.
203 At one time this function renamed files, but file permissions are
204 tricky to update given the number of different schemes used by
205 various systems. So now we just copy. */
208 smart_rename (const char *from
, const char *to
, int fromfd
,
209 struct stat
*target_stat
, bool preserve_dates
)
215 ret
= simple_copy (fromfd
, to
, target_stat
);
217 non_fatal (_("unable to copy file '%s'; reason: %s"),
218 to
, strerror (errno
));
223 set_times (to
, target_stat
);