13d8817e4Smiod /* rename.c -- rename a file, preserving symlinks.
23d8817e4Smiod Copyright 1999, 2002, 2003 Free Software Foundation, Inc.
33d8817e4Smiod
43d8817e4Smiod This file is part of GNU Binutils.
53d8817e4Smiod
63d8817e4Smiod This program is free software; you can redistribute it and/or modify
73d8817e4Smiod it under the terms of the GNU General Public License as published by
83d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
93d8817e4Smiod (at your option) any later version.
103d8817e4Smiod
113d8817e4Smiod This program is distributed in the hope that it will be useful,
123d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
133d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
143d8817e4Smiod GNU General Public License for more details.
153d8817e4Smiod
163d8817e4Smiod You should have received a copy of the GNU General Public License
173d8817e4Smiod along with this program; if not, write to the Free Software
183d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
193d8817e4Smiod 02110-1301, USA. */
203d8817e4Smiod
213d8817e4Smiod #include "bfd.h"
223d8817e4Smiod #include "bucomm.h"
233d8817e4Smiod
243d8817e4Smiod #include <sys/stat.h>
253d8817e4Smiod
263d8817e4Smiod #ifdef HAVE_GOOD_UTIME_H
273d8817e4Smiod #include <utime.h>
283d8817e4Smiod #else /* ! HAVE_GOOD_UTIME_H */
293d8817e4Smiod #ifdef HAVE_UTIMES
303d8817e4Smiod #include <sys/time.h>
313d8817e4Smiod #endif /* HAVE_UTIMES */
323d8817e4Smiod #endif /* ! HAVE_GOOD_UTIME_H */
333d8817e4Smiod
343d8817e4Smiod /* We need to open the file in binary modes on system where that makes
353d8817e4Smiod a difference. */
363d8817e4Smiod #ifndef O_BINARY
373d8817e4Smiod #define O_BINARY 0
383d8817e4Smiod #endif
393d8817e4Smiod
403d8817e4Smiod #if ! defined (_WIN32) || defined (__CYGWIN32__)
413d8817e4Smiod static int simple_copy (const char *, const char *);
423d8817e4Smiod
433d8817e4Smiod /* The number of bytes to copy at once. */
443d8817e4Smiod #define COPY_BUF 8192
453d8817e4Smiod
463d8817e4Smiod /* Copy file FROM to file TO, performing no translations.
473d8817e4Smiod Return 0 if ok, -1 if error. */
483d8817e4Smiod
493d8817e4Smiod static int
simple_copy(const char * from,const char * to)503d8817e4Smiod simple_copy (const char *from, const char *to)
513d8817e4Smiod {
523d8817e4Smiod int fromfd, tofd, nread;
533d8817e4Smiod int saved;
543d8817e4Smiod char buf[COPY_BUF];
553d8817e4Smiod
563d8817e4Smiod fromfd = open (from, O_RDONLY | O_BINARY);
573d8817e4Smiod if (fromfd < 0)
583d8817e4Smiod return -1;
593d8817e4Smiod #ifdef O_CREAT
603d8817e4Smiod tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
613d8817e4Smiod #else
623d8817e4Smiod tofd = creat (to, 0777);
633d8817e4Smiod #endif
643d8817e4Smiod if (tofd < 0)
653d8817e4Smiod {
663d8817e4Smiod saved = errno;
673d8817e4Smiod close (fromfd);
683d8817e4Smiod errno = saved;
693d8817e4Smiod return -1;
703d8817e4Smiod }
713d8817e4Smiod while ((nread = read (fromfd, buf, sizeof buf)) > 0)
723d8817e4Smiod {
733d8817e4Smiod if (write (tofd, buf, nread) != nread)
743d8817e4Smiod {
753d8817e4Smiod saved = errno;
763d8817e4Smiod close (fromfd);
773d8817e4Smiod close (tofd);
783d8817e4Smiod errno = saved;
793d8817e4Smiod return -1;
803d8817e4Smiod }
813d8817e4Smiod }
823d8817e4Smiod saved = errno;
833d8817e4Smiod close (fromfd);
843d8817e4Smiod close (tofd);
853d8817e4Smiod if (nread < 0)
863d8817e4Smiod {
873d8817e4Smiod errno = saved;
883d8817e4Smiod return -1;
893d8817e4Smiod }
903d8817e4Smiod return 0;
913d8817e4Smiod }
923d8817e4Smiod #endif /* __CYGWIN32__ or not _WIN32 */
933d8817e4Smiod
943d8817e4Smiod /* Set the times of the file DESTINATION to be the same as those in
953d8817e4Smiod STATBUF. */
963d8817e4Smiod
973d8817e4Smiod void
set_times(const char * destination,const struct stat * statbuf)983d8817e4Smiod set_times (const char *destination, const struct stat *statbuf)
993d8817e4Smiod {
1003d8817e4Smiod int result;
1013d8817e4Smiod
1023d8817e4Smiod {
1033d8817e4Smiod #ifdef HAVE_GOOD_UTIME_H
1043d8817e4Smiod struct utimbuf tb;
1053d8817e4Smiod
1063d8817e4Smiod tb.actime = statbuf->st_atime;
1073d8817e4Smiod tb.modtime = statbuf->st_mtime;
1083d8817e4Smiod result = utime (destination, &tb);
1093d8817e4Smiod #else /* ! HAVE_GOOD_UTIME_H */
1103d8817e4Smiod #ifndef HAVE_UTIMES
1113d8817e4Smiod long tb[2];
1123d8817e4Smiod
1133d8817e4Smiod tb[0] = statbuf->st_atime;
1143d8817e4Smiod tb[1] = statbuf->st_mtime;
1153d8817e4Smiod result = utime (destination, tb);
1163d8817e4Smiod #else /* HAVE_UTIMES */
1173d8817e4Smiod struct timeval tv[2];
1183d8817e4Smiod
1193d8817e4Smiod tv[0].tv_sec = statbuf->st_atime;
1203d8817e4Smiod tv[0].tv_usec = 0;
1213d8817e4Smiod tv[1].tv_sec = statbuf->st_mtime;
1223d8817e4Smiod tv[1].tv_usec = 0;
1233d8817e4Smiod result = utimes (destination, tv);
1243d8817e4Smiod #endif /* HAVE_UTIMES */
1253d8817e4Smiod #endif /* ! HAVE_GOOD_UTIME_H */
1263d8817e4Smiod }
1273d8817e4Smiod
1283d8817e4Smiod if (result != 0)
1293d8817e4Smiod non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
1303d8817e4Smiod }
1313d8817e4Smiod
1323d8817e4Smiod #ifndef S_ISLNK
1333d8817e4Smiod #ifdef S_IFLNK
1343d8817e4Smiod #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1353d8817e4Smiod #else
1363d8817e4Smiod #define S_ISLNK(m) 0
1373d8817e4Smiod #define lstat stat
1383d8817e4Smiod #endif
1393d8817e4Smiod #endif
1403d8817e4Smiod
1413d8817e4Smiod /* Rename FROM to TO, copying if TO is a link.
1423d8817e4Smiod Return 0 if ok, -1 if error. */
1433d8817e4Smiod
1443d8817e4Smiod int
smart_rename(const char * from,const char * to,int preserve_dates ATTRIBUTE_UNUSED)1453d8817e4Smiod smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
1463d8817e4Smiod {
1473d8817e4Smiod bfd_boolean exists;
1483d8817e4Smiod struct stat s;
1493d8817e4Smiod int ret = 0;
1503d8817e4Smiod
1513d8817e4Smiod exists = lstat (to, &s) == 0;
1523d8817e4Smiod
1533d8817e4Smiod #if defined (_WIN32) && !defined (__CYGWIN32__)
1543d8817e4Smiod /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1553d8817e4Smiod fail instead. Also, chown is not present. */
1563d8817e4Smiod
1573d8817e4Smiod if (exists)
1583d8817e4Smiod remove (to);
1593d8817e4Smiod
1603d8817e4Smiod ret = rename (from, to);
1613d8817e4Smiod if (ret != 0)
1623d8817e4Smiod {
1633d8817e4Smiod /* We have to clean up here. */
1643d8817e4Smiod non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
1653d8817e4Smiod unlink (from);
1663d8817e4Smiod }
1673d8817e4Smiod #else
1683d8817e4Smiod /* Use rename only if TO is not a symbolic link and has
1693d8817e4Smiod only one hard link, and we have permission to write to it. */
1703d8817e4Smiod if (! exists
1713d8817e4Smiod || (!S_ISLNK (s.st_mode)
1723d8817e4Smiod && S_ISREG (s.st_mode)
1733d8817e4Smiod && (s.st_mode & S_IWUSR)
1743d8817e4Smiod && s.st_nlink == 1)
1753d8817e4Smiod )
1763d8817e4Smiod {
1773d8817e4Smiod ret = rename (from, to);
1783d8817e4Smiod if (ret == 0)
1793d8817e4Smiod {
1803d8817e4Smiod if (exists)
1813d8817e4Smiod {
182*032c11f7Sjca /* Try to preserve the permission bits of TO. Don't
183*032c11f7Sjca * restore special bits. */
1843d8817e4Smiod chmod (to, s.st_mode & 0777);
1853d8817e4Smiod }
1863d8817e4Smiod }
1873d8817e4Smiod else
1883d8817e4Smiod {
1893d8817e4Smiod /* We have to clean up here. */
1903d8817e4Smiod non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
1913d8817e4Smiod unlink (from);
1923d8817e4Smiod }
1933d8817e4Smiod }
1943d8817e4Smiod else
1953d8817e4Smiod {
1963d8817e4Smiod ret = simple_copy (from, to);
1973d8817e4Smiod if (ret != 0)
1983d8817e4Smiod non_fatal (_("unable to copy file '%s' reason: %s"), to, strerror (errno));
1993d8817e4Smiod
2003d8817e4Smiod if (preserve_dates)
2013d8817e4Smiod set_times (to, &s);
2023d8817e4Smiod unlink (from);
2033d8817e4Smiod }
2043d8817e4Smiod #endif /* _WIN32 && !__CYGWIN32__ */
2053d8817e4Smiod
2063d8817e4Smiod return ret;
2073d8817e4Smiod }
208