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