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