1*a9fa9459Szrj /* rename.c -- rename a file, preserving symlinks.
2*a9fa9459Szrj Copyright (C) 1999-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj
4*a9fa9459Szrj This file is part of GNU Binutils.
5*a9fa9459Szrj
6*a9fa9459Szrj This program is free software; you can redistribute it and/or modify
7*a9fa9459Szrj it under the terms of the GNU General Public License as published by
8*a9fa9459Szrj the Free Software Foundation; either version 3 of the License, or
9*a9fa9459Szrj (at your option) any later version.
10*a9fa9459Szrj
11*a9fa9459Szrj This program is distributed in the hope that it will be useful,
12*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a9fa9459Szrj GNU General Public License for more details.
15*a9fa9459Szrj
16*a9fa9459Szrj You should have received a copy of the GNU General Public License
17*a9fa9459Szrj along with this program; if not, write to the Free Software
18*a9fa9459Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19*a9fa9459Szrj 02110-1301, USA. */
20*a9fa9459Szrj
21*a9fa9459Szrj #include "sysdep.h"
22*a9fa9459Szrj #include "bfd.h"
23*a9fa9459Szrj #include "bucomm.h"
24*a9fa9459Szrj
25*a9fa9459Szrj #ifdef HAVE_GOOD_UTIME_H
26*a9fa9459Szrj #include <utime.h>
27*a9fa9459Szrj #else /* ! HAVE_GOOD_UTIME_H */
28*a9fa9459Szrj #ifdef HAVE_UTIMES
29*a9fa9459Szrj #include <sys/time.h>
30*a9fa9459Szrj #endif /* HAVE_UTIMES */
31*a9fa9459Szrj #endif /* ! HAVE_GOOD_UTIME_H */
32*a9fa9459Szrj
33*a9fa9459Szrj #if ! defined (_WIN32) || defined (__CYGWIN32__)
34*a9fa9459Szrj static int simple_copy (const char *, const char *);
35*a9fa9459Szrj
36*a9fa9459Szrj /* The number of bytes to copy at once. */
37*a9fa9459Szrj #define COPY_BUF 8192
38*a9fa9459Szrj
39*a9fa9459Szrj /* Copy file FROM to file TO, performing no translations.
40*a9fa9459Szrj Return 0 if ok, -1 if error. */
41*a9fa9459Szrj
42*a9fa9459Szrj static int
simple_copy(const char * from,const char * to)43*a9fa9459Szrj simple_copy (const char *from, const char *to)
44*a9fa9459Szrj {
45*a9fa9459Szrj int fromfd, tofd, nread;
46*a9fa9459Szrj int saved;
47*a9fa9459Szrj char buf[COPY_BUF];
48*a9fa9459Szrj
49*a9fa9459Szrj fromfd = open (from, O_RDONLY | O_BINARY);
50*a9fa9459Szrj if (fromfd < 0)
51*a9fa9459Szrj return -1;
52*a9fa9459Szrj #ifdef O_CREAT
53*a9fa9459Szrj tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
54*a9fa9459Szrj #else
55*a9fa9459Szrj tofd = creat (to, 0777);
56*a9fa9459Szrj #endif
57*a9fa9459Szrj if (tofd < 0)
58*a9fa9459Szrj {
59*a9fa9459Szrj saved = errno;
60*a9fa9459Szrj close (fromfd);
61*a9fa9459Szrj errno = saved;
62*a9fa9459Szrj return -1;
63*a9fa9459Szrj }
64*a9fa9459Szrj while ((nread = read (fromfd, buf, sizeof buf)) > 0)
65*a9fa9459Szrj {
66*a9fa9459Szrj if (write (tofd, buf, nread) != nread)
67*a9fa9459Szrj {
68*a9fa9459Szrj saved = errno;
69*a9fa9459Szrj close (fromfd);
70*a9fa9459Szrj close (tofd);
71*a9fa9459Szrj errno = saved;
72*a9fa9459Szrj return -1;
73*a9fa9459Szrj }
74*a9fa9459Szrj }
75*a9fa9459Szrj saved = errno;
76*a9fa9459Szrj close (fromfd);
77*a9fa9459Szrj close (tofd);
78*a9fa9459Szrj if (nread < 0)
79*a9fa9459Szrj {
80*a9fa9459Szrj errno = saved;
81*a9fa9459Szrj return -1;
82*a9fa9459Szrj }
83*a9fa9459Szrj return 0;
84*a9fa9459Szrj }
85*a9fa9459Szrj #endif /* __CYGWIN32__ or not _WIN32 */
86*a9fa9459Szrj
87*a9fa9459Szrj /* Set the times of the file DESTINATION to be the same as those in
88*a9fa9459Szrj STATBUF. */
89*a9fa9459Szrj
90*a9fa9459Szrj void
set_times(const char * destination,const struct stat * statbuf)91*a9fa9459Szrj set_times (const char *destination, const struct stat *statbuf)
92*a9fa9459Szrj {
93*a9fa9459Szrj int result;
94*a9fa9459Szrj
95*a9fa9459Szrj {
96*a9fa9459Szrj #ifdef HAVE_GOOD_UTIME_H
97*a9fa9459Szrj struct utimbuf tb;
98*a9fa9459Szrj
99*a9fa9459Szrj tb.actime = statbuf->st_atime;
100*a9fa9459Szrj tb.modtime = statbuf->st_mtime;
101*a9fa9459Szrj result = utime (destination, &tb);
102*a9fa9459Szrj #else /* ! HAVE_GOOD_UTIME_H */
103*a9fa9459Szrj #ifndef HAVE_UTIMES
104*a9fa9459Szrj long tb[2];
105*a9fa9459Szrj
106*a9fa9459Szrj tb[0] = statbuf->st_atime;
107*a9fa9459Szrj tb[1] = statbuf->st_mtime;
108*a9fa9459Szrj result = utime (destination, tb);
109*a9fa9459Szrj #else /* HAVE_UTIMES */
110*a9fa9459Szrj struct timeval tv[2];
111*a9fa9459Szrj
112*a9fa9459Szrj tv[0].tv_sec = statbuf->st_atime;
113*a9fa9459Szrj tv[0].tv_usec = 0;
114*a9fa9459Szrj tv[1].tv_sec = statbuf->st_mtime;
115*a9fa9459Szrj tv[1].tv_usec = 0;
116*a9fa9459Szrj result = utimes (destination, tv);
117*a9fa9459Szrj #endif /* HAVE_UTIMES */
118*a9fa9459Szrj #endif /* ! HAVE_GOOD_UTIME_H */
119*a9fa9459Szrj }
120*a9fa9459Szrj
121*a9fa9459Szrj if (result != 0)
122*a9fa9459Szrj non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
123*a9fa9459Szrj }
124*a9fa9459Szrj
125*a9fa9459Szrj #ifndef S_ISLNK
126*a9fa9459Szrj #ifdef S_IFLNK
127*a9fa9459Szrj #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
128*a9fa9459Szrj #else
129*a9fa9459Szrj #define S_ISLNK(m) 0
130*a9fa9459Szrj #define lstat stat
131*a9fa9459Szrj #endif
132*a9fa9459Szrj #endif
133*a9fa9459Szrj
134*a9fa9459Szrj /* Rename FROM to TO, copying if TO is a link.
135*a9fa9459Szrj Return 0 if ok, -1 if error. */
136*a9fa9459Szrj
137*a9fa9459Szrj int
smart_rename(const char * from,const char * to,int preserve_dates ATTRIBUTE_UNUSED)138*a9fa9459Szrj smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
139*a9fa9459Szrj {
140*a9fa9459Szrj bfd_boolean exists;
141*a9fa9459Szrj struct stat s;
142*a9fa9459Szrj int ret = 0;
143*a9fa9459Szrj
144*a9fa9459Szrj exists = lstat (to, &s) == 0;
145*a9fa9459Szrj
146*a9fa9459Szrj #if defined (_WIN32) && !defined (__CYGWIN32__)
147*a9fa9459Szrj /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
148*a9fa9459Szrj fail instead. Also, chown is not present. */
149*a9fa9459Szrj
150*a9fa9459Szrj if (exists)
151*a9fa9459Szrj remove (to);
152*a9fa9459Szrj
153*a9fa9459Szrj ret = rename (from, to);
154*a9fa9459Szrj if (ret != 0)
155*a9fa9459Szrj {
156*a9fa9459Szrj /* We have to clean up here. */
157*a9fa9459Szrj non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
158*a9fa9459Szrj unlink (from);
159*a9fa9459Szrj }
160*a9fa9459Szrj #else
161*a9fa9459Szrj /* Use rename only if TO is not a symbolic link and has
162*a9fa9459Szrj only one hard link, and we have permission to write to it. */
163*a9fa9459Szrj if (! exists
164*a9fa9459Szrj || (!S_ISLNK (s.st_mode)
165*a9fa9459Szrj && S_ISREG (s.st_mode)
166*a9fa9459Szrj && (s.st_mode & S_IWUSR)
167*a9fa9459Szrj && s.st_nlink == 1)
168*a9fa9459Szrj )
169*a9fa9459Szrj {
170*a9fa9459Szrj ret = rename (from, to);
171*a9fa9459Szrj if (ret == 0)
172*a9fa9459Szrj {
173*a9fa9459Szrj if (exists)
174*a9fa9459Szrj {
175*a9fa9459Szrj /* Try to preserve the permission bits and ownership of
176*a9fa9459Szrj TO. First get the mode right except for the setuid
177*a9fa9459Szrj bit. Then change the ownership. Then fix the setuid
178*a9fa9459Szrj bit. We do the chmod before the chown because if the
179*a9fa9459Szrj chown succeeds, and we are a normal user, we won't be
180*a9fa9459Szrj able to do the chmod afterward. We don't bother to
181*a9fa9459Szrj fix the setuid bit first because that might introduce
182*a9fa9459Szrj a fleeting security problem, and because the chown
183*a9fa9459Szrj will clear the setuid bit anyhow. We only fix the
184*a9fa9459Szrj setuid bit if the chown succeeds, because we don't
185*a9fa9459Szrj want to introduce an unexpected setuid file owned by
186*a9fa9459Szrj the user running objcopy. */
187*a9fa9459Szrj chmod (to, s.st_mode & 0777);
188*a9fa9459Szrj if (chown (to, s.st_uid, s.st_gid) >= 0)
189*a9fa9459Szrj chmod (to, s.st_mode & 07777);
190*a9fa9459Szrj }
191*a9fa9459Szrj }
192*a9fa9459Szrj else
193*a9fa9459Szrj {
194*a9fa9459Szrj /* We have to clean up here. */
195*a9fa9459Szrj non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
196*a9fa9459Szrj unlink (from);
197*a9fa9459Szrj }
198*a9fa9459Szrj }
199*a9fa9459Szrj else
200*a9fa9459Szrj {
201*a9fa9459Szrj ret = simple_copy (from, to);
202*a9fa9459Szrj if (ret != 0)
203*a9fa9459Szrj non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
204*a9fa9459Szrj
205*a9fa9459Szrj if (preserve_dates)
206*a9fa9459Szrj set_times (to, &s);
207*a9fa9459Szrj unlink (from);
208*a9fa9459Szrj }
209*a9fa9459Szrj #endif /* _WIN32 && !__CYGWIN32__ */
210*a9fa9459Szrj
211*a9fa9459Szrj return ret;
212*a9fa9459Szrj }
213