binutils: Avoid renaming over existing files

Renaming over existing files needs additional care to restore
permissions and ownership, which may not always succeed.
Additionally, other properties of the file such as extended attributes
may be lost, making the operation flaky.

For predictable results, resort to rename() only if the file does not
exist, otherwise copy the file contents into the existing file.  This
ensures that no additional tricks are needed to retain file
properties.

This also allows dropping of the redundant set_times on the tmpfile in
objcopy/strip since now we no longer rename over existing files.

binutils/

	* ar.c (write_archive): Remove TARGET_STAT.  Adjust call to
	SMART_RENAME.
	* arsup.c (ar_save): Likewise.
	* objcopy (strip_main): Don't copy TMPFD.  Don't set times on
	temporary file and adjust call to SMART_RENAME.
	(copy_main): Likewise.
	* rename.c [!S_ISLNK]: Remove definitions.
	(try_preserve_permissions): Remove function.
	(smart_rename): Remove FD, PRESERVE_DATES arguments.  Use
	rename system call only if TO does not exist.
	* bucomm.h (smart_rename): Adjust declaration.
This commit is contained in:
Siddhesh Poyarekar 2021-02-19 08:05:33 +05:30
parent 668c18f17f
commit 3685de750e
6 changed files with 33 additions and 142 deletions

View file

@ -4837,7 +4837,6 @@ strip_main (int argc, char *argv[])
struct stat statbuf;
char *tmpname;
int tmpfd = -1;
int copyfd = -1;
if (get_file_size (argv[i]) < 1)
{
@ -4851,12 +4850,7 @@ strip_main (int argc, char *argv[])
else
tmpname = output_file;
if (tmpname == NULL
#if !defined (_WIN32) || defined (__CYGWIN32__)
/* Retain a copy of TMPFD since we will need it for SMART_RENAME. */
|| (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
#endif
)
if (tmpname == NULL)
{
bfd_nonfatal_message (argv[i], NULL, NULL,
_("could not create temporary file to hold stripped copy"));
@ -4869,23 +4863,15 @@ strip_main (int argc, char *argv[])
output_target, NULL);
if (status == 0)
{
if (preserve_dates)
set_times (tmpname, &statbuf);
if (output_file != tmpname)
status = (smart_rename (tmpname,
output_file ? output_file : argv[i],
copyfd, &statbuf, preserve_dates) != 0);
preserve_dates ? &statbuf : NULL) != 0);
if (status == 0)
status = hold_status;
}
else
{
#if !defined (_WIN32) || defined (__CYGWIN32__)
if (copyfd >= 0)
close (copyfd);
#endif
unlink_if_ordinary (tmpname);
}
unlink_if_ordinary (tmpname);
if (output_file != tmpname)
free (tmpname);
}
@ -5093,7 +5079,6 @@ copy_main (int argc, char *argv[])
bfd_boolean use_globalize = FALSE;
bfd_boolean use_keep_global = FALSE;
int c, tmpfd = -1;
int copyfd = -1;
struct stat statbuf;
const bfd_arch_info_type *input_arch = NULL;
@ -5938,12 +5923,7 @@ copy_main (int argc, char *argv[])
else
tmpname = output_filename;
if (tmpname == NULL
#if !defined (_WIN32) || defined (__CYGWIN32__)
/* Retain a copy of TMPFD since we will need it for SMART_RENAME. */
|| (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
#endif
)
if (tmpname == NULL)
{
fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
input_filename, strerror (errno));
@ -5953,20 +5933,12 @@ copy_main (int argc, char *argv[])
output_target, input_arch);
if (status == 0)
{
if (preserve_dates)
set_times (tmpname, &statbuf);
if (tmpname != output_filename)
status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
preserve_dates) != 0);
status = (smart_rename (tmpname, input_filename,
preserve_dates ? &statbuf : NULL) != 0);
}
else
{
#if !defined (_WIN32) || defined (__CYGWIN32__)
if (copyfd >= 0)
close (copyfd);
#endif
unlink_if_ordinary (tmpname);
}
unlink_if_ordinary (tmpname);
if (tmpname != output_filename)
free (tmpname);