aboutsummaryrefslogtreecommitdiff
path: root/binutils/objcopy.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@gotplt.org>2021-02-19 08:05:33 +0530
committerSiddhesh Poyarekar <siddhesh@gotplt.org>2021-02-19 08:05:33 +0530
commit3685de750e6a091663a0abe42528cad29e960e35 (patch)
tree2c71a3c3f24af21550fde8cef7431a3f9d248659 /binutils/objcopy.c
parent668c18f17fc4ea91675a2b638c31bfbefa29665f (diff)
downloadgdb-3685de750e6a091663a0abe42528cad29e960e35.zip
gdb-3685de750e6a091663a0abe42528cad29e960e35.tar.gz
gdb-3685de750e6a091663a0abe42528cad29e960e35.tar.bz2
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.
Diffstat (limited to 'binutils/objcopy.c')
-rw-r--r--binutils/objcopy.c42
1 files changed, 7 insertions, 35 deletions
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 54a5943..abbcb7c 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -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);