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