/* rename.c -- rename a file, preserving symlinks.
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Binutils.
#endif /* HAVE_UTIMES */
#endif /* ! HAVE_GOOD_UTIME_H */
-static int simple_copy PARAMS ((const char *, const char *));
+/* We need to open the file in binary modes on system where that makes
+ a difference. */
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+static int simple_copy (const char *, const char *);
/* The number of bytes to copy at once. */
#define COPY_BUF 8192
Return 0 if ok, -1 if error. */
static int
-simple_copy (from, to)
- const char *from;
- const char *to;
+simple_copy (const char *from, const char *to)
{
int fromfd, tofd, nread;
int saved;
char buf[COPY_BUF];
- fromfd = open (from, O_RDONLY);
+ fromfd = open (from, O_RDONLY | O_BINARY);
if (fromfd < 0)
return -1;
+#ifdef O_CREAT
+ tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
+#else
tofd = creat (to, 0777);
+#endif
if (tofd < 0)
{
saved = errno;
STATBUF. */
void
-set_times (destination, statbuf)
- const char *destination;
- const struct stat *statbuf;
+set_times (const char *destination, const struct stat *statbuf)
{
int result;
Return 0 if ok, -1 if error. */
int
-smart_rename (from, to, preserve_dates)
- const char *from;
- const char *to;
- int preserve_dates;
+smart_rename (const char *from, const char *to, int preserve_dates)
{
- int exists;
+ bfd_boolean exists;
struct stat s;
int ret = 0;
- exists = lstat (to, &s);
+ exists = lstat (to, &s) == 0;
#if defined (_WIN32) && !defined (__CYGWIN32__)
/* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
fail instead. Also, chown is not present. */
- if (exists == 0)
+ if (exists)
remove (to);
ret = rename (from, to);
if (ret != 0)
{
- /* We have to clean up here. */
-
- non_fatal (_("%s: rename: %s"), to, strerror (errno));
+ /* We have to clean up here. */
+ non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
unlink (from);
}
#else
/* Use rename only if TO is not a symbolic link and has
- only one hard link. */
- if (exists < 0 || (!S_ISLNK (s.st_mode) && s.st_nlink == 1))
+ only one hard link, and we have permission to write to it. */
+ if (! exists
+ || (!S_ISLNK (s.st_mode)
+ && S_ISREG (s.st_mode)
+ && (s.st_mode & S_IWUSR)
+ && s.st_nlink == 1)
+ )
{
ret = rename (from, to);
if (ret == 0)
}
else
{
- /* We have to clean up here. */
- non_fatal (_("%s: rename: %s"), to, strerror (errno));
+ /* We have to clean up here. */
+ non_fatal (_("unable to rename '%s' reason: %s"), to, strerror (errno));
unlink (from);
}
}
{
ret = simple_copy (from, to);
if (ret != 0)
- non_fatal (_("%s: simple_copy: %s"), to, strerror (errno));
+ non_fatal (_("unable to copy file '%s' reason: %s"), to, strerror (errno));
if (preserve_dates)
set_times (to, &s);