diff options
Diffstat (limited to 'binutils/objcopy.c')
-rw-r--r-- | binutils/objcopy.c | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/binutils/objcopy.c b/binutils/objcopy.c index b6cf6ea..04ba95e 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -4815,6 +4815,7 @@ strip_main (int argc, char *argv[]) struct stat statbuf; char *tmpname; int tmpfd = -1; + int copyfd = -1; if (get_file_size (argv[i]) < 1) { @@ -4828,7 +4829,12 @@ strip_main (int argc, char *argv[]) else tmpname = output_file; - if (tmpname == NULL) + 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 + ) { bfd_nonfatal_message (argv[i], NULL, NULL, _("could not create temporary file to hold stripped copy")); @@ -4846,12 +4852,18 @@ strip_main (int argc, char *argv[]) if (output_file != tmpname) status = (smart_rename (tmpname, output_file ? output_file : argv[i], - preserve_dates) != 0); + copyfd, &statbuf, preserve_dates) != 0); if (status == 0) status = hold_status; } else - unlink_if_ordinary (tmpname); + { +#if !defined (_WIN32) || defined (__CYGWIN32__) + if (copyfd >= 0) + close (copyfd); +#endif + unlink_if_ordinary (tmpname); + } if (output_file != tmpname) free (tmpname); } @@ -5059,6 +5071,7 @@ 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; @@ -5903,9 +5916,16 @@ copy_main (int argc, char *argv[]) else tmpname = output_filename; - if (tmpname == NULL) - fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"), - input_filename, strerror (errno)); + 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 + ) + { + fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"), + input_filename, strerror (errno)); + } copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target, output_target, input_arch); @@ -5914,11 +5934,17 @@ copy_main (int argc, char *argv[]) if (preserve_dates) set_times (tmpname, &statbuf); if (tmpname != output_filename) - status = (smart_rename (tmpname, input_filename, + status = (smart_rename (tmpname, input_filename, copyfd, &statbuf, preserve_dates) != 0); } else - unlink_if_ordinary (tmpname); + { +#if !defined (_WIN32) || defined (__CYGWIN32__) + if (copyfd >= 0) + close (copyfd); +#endif + unlink_if_ordinary (tmpname); + } if (tmpname != output_filename) free (tmpname); |