This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
output renaming in objcopy & strip
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: binutils at sources dot redhat dot com
- Date: Thu, 17 May 2007 18:09:11 +0100
- Subject: output renaming in objcopy & strip
I had reason to backport objcopy's behaviour of deleting the output file in the
face of error. I noticed some inconsistencies between strip_main and
objcopy_main, and this patch fixes them up.
1) objcopy detects when the output and input filenames are the same. strip did
not. Added the checking to strip
2) strip factors out the tempname generation, and uses the same code for the
actual stripping call. objcopy did not. Refactored the objcopy code.
tested on m68k-elf, ok?
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk
2007-05-17 Nathan Sidwell <nathan@codesourcery.com>
* objcopy.c (strip_main): Detect identical input and output file
names.
(copy_main): Refactor tempname detection and use.
Index: objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.111
diff -c -3 -p -r1.111 objcopy.c
*** objcopy.c 11 May 2007 14:09:50 -0000 1.111
--- objcopy.c 17 May 2007 17:03:40 -0000
*************** strip_main (int argc, char *argv[])
*** 2769,2778 ****
It has already been checked in get_file_size(). */
stat (argv[i], &statbuf);
! if (output_file != NULL)
! tmpname = output_file;
! else
tmpname = make_tempname (argv[i]);
if (tmpname == NULL)
{
--- 2769,2778 ----
It has already been checked in get_file_size(). */
stat (argv[i], &statbuf);
! if (output_file == NULL || strcmp (argv[i], output_file) == 0)
tmpname = make_tempname (argv[i]);
+ else
+ tmpname = output_file;
if (tmpname == NULL)
{
*************** strip_main (int argc, char *argv[])
*** 2788,2795 ****
{
if (preserve_dates)
set_times (tmpname, &statbuf);
! if (output_file == NULL)
! smart_rename (tmpname, argv[i], preserve_dates);
status = hold_status;
}
else
--- 2788,2796 ----
{
if (preserve_dates)
set_times (tmpname, &statbuf);
! if (output_file != tmpname)
! smart_rename (tmpname, output_file ? output_file : argv[i],
! preserve_dates);
status = hold_status;
}
else
*************** copy_main (int argc, char *argv[])
*** 2807,2812 ****
--- 2808,2814 ----
char * binary_architecture = NULL;
char *input_filename = NULL;
char *output_filename = NULL;
+ char *tmpname;
char *input_target = NULL;
char *output_target = NULL;
bfd_boolean show_version = FALSE;
*************** copy_main (int argc, char *argv[])
*** 3397,3428 ****
/* If there is no destination file, or the source and destination files
are the same, then create a temp and rename the result into the input. */
if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
! {
! char *tmpname = make_tempname (input_filename);
!
! if (tmpname == NULL)
! fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
! input_filename, strerror (errno));
!
! copy_file (input_filename, tmpname, input_target, output_target);
! if (status == 0)
! {
! if (preserve_dates)
! set_times (tmpname, &statbuf);
! smart_rename (tmpname, input_filename, preserve_dates);
! }
! else
! unlink (tmpname);
! }
else
! {
! copy_file (input_filename, output_filename, input_target, output_target);
! if (status == 0 && preserve_dates)
! set_times (output_filename, &statbuf);
! else if (status != 0)
! unlink_if_ordinary (output_filename);
}
if (change_warn)
{
--- 3399,3422 ----
/* If there is no destination file, or the source and destination files
are the same, then create a temp and rename the result into the input. */
if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
! tmpname = make_tempname (input_filename);
else
! tmpname = output_filename;
!
! if (tmpname == NULL)
! fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
! input_filename, strerror (errno));
! copy_file (input_filename, tmpname, input_target, output_target);
! if (status == 0)
! {
! if (preserve_dates)
! set_times (tmpname, &statbuf);
! if (tmpname != output_filename)
! smart_rename (tmpname, input_filename, preserve_dates);
}
+ else
+ unlink_if_ordinary (tmpname);
if (change_warn)
{