2005-05-08 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / binutils / rename.c
CommitLineData
252b5132 1/* rename.c -- rename a file, preserving symlinks.
2da42df6 2 Copyright 1999, 2002, 2003 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GNU Binutils.
5
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 2 of the License, or
9 (at your option) any later version.
10
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.
15
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., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#include "bfd.h"
22#include "bucomm.h"
23
24#include <sys/stat.h>
25
26#ifdef HAVE_GOOD_UTIME_H
27#include <utime.h>
28#else /* ! HAVE_GOOD_UTIME_H */
29#ifdef HAVE_UTIMES
30#include <sys/time.h>
31#endif /* HAVE_UTIMES */
32#endif /* ! HAVE_GOOD_UTIME_H */
33
18226413
ILT
34/* We need to open the file in binary modes on system where that makes
35 a difference. */
36#ifndef O_BINARY
37#define O_BINARY 0
38#endif
39
2da42df6 40static int simple_copy (const char *, const char *);
252b5132
RH
41
42/* The number of bytes to copy at once. */
43#define COPY_BUF 8192
44
45/* Copy file FROM to file TO, performing no translations.
46 Return 0 if ok, -1 if error. */
47
48static int
2da42df6 49simple_copy (const char *from, const char *to)
252b5132
RH
50{
51 int fromfd, tofd, nread;
52 int saved;
53 char buf[COPY_BUF];
54
18226413 55 fromfd = open (from, O_RDONLY | O_BINARY);
252b5132
RH
56 if (fromfd < 0)
57 return -1;
18226413
ILT
58#ifdef O_CREAT
59 tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
60#else
252b5132 61 tofd = creat (to, 0777);
18226413 62#endif
252b5132
RH
63 if (tofd < 0)
64 {
65 saved = errno;
66 close (fromfd);
67 errno = saved;
68 return -1;
69 }
70 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
71 {
72 if (write (tofd, buf, nread) != nread)
73 {
74 saved = errno;
75 close (fromfd);
76 close (tofd);
77 errno = saved;
78 return -1;
79 }
80 }
81 saved = errno;
82 close (fromfd);
83 close (tofd);
84 if (nread < 0)
85 {
86 errno = saved;
87 return -1;
88 }
89 return 0;
90}
91
92/* Set the times of the file DESTINATION to be the same as those in
93 STATBUF. */
94
95void
2da42df6 96set_times (const char *destination, const struct stat *statbuf)
252b5132
RH
97{
98 int result;
99
100 {
101#ifdef HAVE_GOOD_UTIME_H
102 struct utimbuf tb;
103
104 tb.actime = statbuf->st_atime;
105 tb.modtime = statbuf->st_mtime;
106 result = utime (destination, &tb);
107#else /* ! HAVE_GOOD_UTIME_H */
108#ifndef HAVE_UTIMES
109 long tb[2];
110
111 tb[0] = statbuf->st_atime;
112 tb[1] = statbuf->st_mtime;
113 result = utime (destination, tb);
114#else /* HAVE_UTIMES */
115 struct timeval tv[2];
116
117 tv[0].tv_sec = statbuf->st_atime;
118 tv[0].tv_usec = 0;
119 tv[1].tv_sec = statbuf->st_mtime;
120 tv[1].tv_usec = 0;
121 result = utimes (destination, tv);
122#endif /* HAVE_UTIMES */
123#endif /* ! HAVE_GOOD_UTIME_H */
124 }
125
126 if (result != 0)
127 non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
128}
129
130#ifndef S_ISLNK
131#ifdef S_IFLNK
132#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
133#else
134#define S_ISLNK(m) 0
135#define lstat stat
136#endif
137#endif
138
139/* Rename FROM to TO, copying if TO is a link.
140 Return 0 if ok, -1 if error. */
141
142int
2da42df6 143smart_rename (const char *from, const char *to, int preserve_dates)
252b5132 144{
b34976b6 145 bfd_boolean exists;
252b5132
RH
146 struct stat s;
147 int ret = 0;
148
82716b78 149 exists = lstat (to, &s) == 0;
252b5132
RH
150
151#if defined (_WIN32) && !defined (__CYGWIN32__)
152 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
153 fail instead. Also, chown is not present. */
154
82716b78 155 if (exists)
252b5132
RH
156 remove (to);
157
158 ret = rename (from, to);
159 if (ret != 0)
160 {
53c7db4b 161 /* We have to clean up here. */
195f52b5 162 non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
252b5132
RH
163 unlink (from);
164 }
165#else
166 /* Use rename only if TO is not a symbolic link and has
195f52b5
NC
167 only one hard link, and we have permission to write to it. */
168 if (! exists
169 || (!S_ISLNK (s.st_mode)
170 && S_ISREG (s.st_mode)
171 && (s.st_mode & S_IWUSR)
172 && s.st_nlink == 1)
173 )
252b5132
RH
174 {
175 ret = rename (from, to);
176 if (ret == 0)
177 {
178 if (exists)
179 {
180 /* Try to preserve the permission bits and ownership of
181 TO. First get the mode right except for the setuid
182 bit. Then change the ownership. Then fix the setuid
183 bit. We do the chmod before the chown because if the
184 chown succeeds, and we are a normal user, we won't be
185 able to do the chmod afterward. We don't bother to
186 fix the setuid bit first because that might introduce
187 a fleeting security problem, and because the chown
188 will clear the setuid bit anyhow. We only fix the
189 setuid bit if the chown succeeds, because we don't
190 want to introduce an unexpected setuid file owned by
191 the user running objcopy. */
192 chmod (to, s.st_mode & 0777);
193 if (chown (to, s.st_uid, s.st_gid) >= 0)
194 chmod (to, s.st_mode & 07777);
195 }
196 }
197 else
198 {
53c7db4b 199 /* We have to clean up here. */
195f52b5 200 non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
252b5132
RH
201 unlink (from);
202 }
203 }
204 else
205 {
206 ret = simple_copy (from, to);
207 if (ret != 0)
195f52b5 208 non_fatal (_("unable to copy file '%s' reason: %s"), to, strerror (errno));
252b5132
RH
209
210 if (preserve_dates)
211 set_times (to, &s);
212 unlink (from);
213 }
214#endif /* _WIN32 && !__CYGWIN32__ */
215
216 return ret;
217}
This page took 0.273571 seconds and 4 git commands to generate.