PR27456, lstat in rename.c on MinGW

PR 27456
	* rename.c: Tidy throughout.
	(smart_rename): Always copy.  Remove windows specific code.
This commit is contained in:
Alan Modra 2021-02-23 09:37:39 +10:30
parent 08ac57714c
commit cca8873dd5
2 changed files with 38 additions and 73 deletions

View File

@ -1,3 +1,9 @@
2021-02-24 Alan Modra <amodra@gmail.com>
PR 27456
* rename.c: Tidy throughout.
(smart_rename): Always copy. Remove windows specific code.
2021-02-20 Alan Modra <amodra@gmail.com> 2021-02-20 Alan Modra <amodra@gmail.com>
* testsuite/lib/binutils-common.exp: Whitespace fixes throughout. * testsuite/lib/binutils-common.exp: Whitespace fixes throughout.

View File

@ -24,14 +24,9 @@
#ifdef HAVE_GOOD_UTIME_H #ifdef HAVE_GOOD_UTIME_H
#include <utime.h> #include <utime.h>
#else /* ! HAVE_GOOD_UTIME_H */ #elif defined HAVE_UTIMES
#ifdef HAVE_UTIMES
#include <sys/time.h> #include <sys/time.h>
#endif /* HAVE_UTIMES */ #endif
#endif /* ! HAVE_GOOD_UTIME_H */
#if ! defined (_WIN32) || defined (__CYGWIN32__)
static int simple_copy (const char *, const char *);
/* The number of bytes to copy at once. */ /* The number of bytes to copy at once. */
#define COPY_BUF 8192 #define COPY_BUF 8192
@ -82,7 +77,6 @@ simple_copy (const char *from, const char *to)
} }
return 0; return 0;
} }
#endif /* __CYGWIN32__ or not _WIN32 */
/* Set the times of the file DESTINATION to be the same as those in /* Set the times of the file DESTINATION to be the same as those in
STATBUF. */ STATBUF. */
@ -91,22 +85,13 @@ void
set_times (const char *destination, const struct stat *statbuf) set_times (const char *destination, const struct stat *statbuf)
{ {
int result; int result;
{
#ifdef HAVE_GOOD_UTIME_H #ifdef HAVE_GOOD_UTIME_H
struct utimbuf tb; struct utimbuf tb;
tb.actime = statbuf->st_atime; tb.actime = statbuf->st_atime;
tb.modtime = statbuf->st_mtime; tb.modtime = statbuf->st_mtime;
result = utime (destination, &tb); result = utime (destination, &tb);
#else /* ! HAVE_GOOD_UTIME_H */ #elif defined HAVE_UTIMES
#ifndef HAVE_UTIMES
long tb[2];
tb[0] = statbuf->st_atime;
tb[1] = statbuf->st_mtime;
result = utime (destination, tb);
#else /* HAVE_UTIMES */
struct timeval tv[2]; struct timeval tv[2];
tv[0].tv_sec = statbuf->st_atime; tv[0].tv_sec = statbuf->st_atime;
@ -114,64 +99,38 @@ set_times (const char *destination, const struct stat *statbuf)
tv[1].tv_sec = statbuf->st_mtime; tv[1].tv_sec = statbuf->st_mtime;
tv[1].tv_usec = 0; tv[1].tv_usec = 0;
result = utimes (destination, tv); result = utimes (destination, tv);
#endif /* HAVE_UTIMES */ #else
#endif /* ! HAVE_GOOD_UTIME_H */ long tb[2];
}
tb[0] = statbuf->st_atime;
tb[1] = statbuf->st_mtime;
result = utime (destination, tb);
#endif
if (result != 0) if (result != 0)
non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno)); non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
} }
/* Rename FROM to TO, copying if TO exists. TARGET_STAT has the file status /* Copy FROM to TO. TARGET_STAT has the file status that, if non-NULL,
that, if non-NULL, is used to fix up timestamps after rename. Return 0 if is used to fix up timestamps. Return 0 if ok, -1 if error.
ok, -1 if error. */ At one time this function renamed files, but file permissions are
tricky to update given the number of different schemes used by
various systems. So now we just copy. */
int int
smart_rename (const char *from, const char *to, smart_rename (const char *from, const char *to,
struct stat *target_stat ATTRIBUTE_UNUSED) struct stat *target_stat)
{ {
int ret = 0; int ret;
struct stat to_stat;
bfd_boolean exists;
exists = lstat (to, &to_stat) == 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)
remove (to);
ret = rename (from, to);
if (ret != 0)
{
/* We have to clean up here. */
non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
unlink (from);
}
#else
/* Avoid a full copy and use rename if TO does not exist. */
if (!exists)
{
if ((ret = rename (from, to)) != 0)
{
/* We have to clean up here. */
non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
unlink (from);
}
}
else
{
ret = simple_copy (from, to); ret = simple_copy (from, to);
if (ret != 0) if (ret != 0)
non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno)); non_fatal (_("unable to copy file '%s'; reason: %s"),
to, strerror (errno));
if (target_stat != NULL) if (target_stat != NULL)
set_times (to, target_stat); set_times (to, target_stat);
unlink (from); unlink (from);
}
#endif /* _WIN32 && !__CYGWIN32__ */
return ret; return ret;
} }