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