]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Make objcopy -p work when an output file is specified
authorAlan Modra <amodra@gmail.com>
Wed, 14 Apr 2021 16:03:13 +0000 (01:33 +0930)
committerAlan Modra <amodra@gmail.com>
Thu, 15 Apr 2021 05:33:50 +0000 (15:03 +0930)
More fallout from the PR27456 fixes.

PR 27456
* rename.c (smart_rename): When TO and FROM are equal, just set
file timestamp.
* objcopy.c (strip_main, copy_main): Always call smart_rename.

(cherry picked from commit d0ecdcddc363ad7f05fc50cf1eee4028fa7f8964)

binutils/ChangeLog
binutils/objcopy.c
binutils/rename.c

index 57ee4bc8ab20c54222400bb1fe66d6359e8a7ffe..11ff778cc356b2b34acb99512ab6552fd2ca71f4 100644 (file)
@@ -1,3 +1,10 @@
+2021-04-15  Alan Modra  <amodra@gmail.com>
+
+       PR 27456
+       * rename.c (smart_rename): When TO and FROM are equal, just set
+       file timestamp.
+       * objcopy.c (strip_main, copy_main): Always call smart_rename.
+
 2021-02-26  Alan Modra  <amodra@gmail.com>
 
        Backport from mainline
index 620baee6dec34ed7683ef280616bfb60767eb352..00ef851dc5d005310762417c5d8f0f9cf15e29f2 100644 (file)
@@ -4865,10 +4865,9 @@ strip_main (int argc, char *argv[])
                 output_target, NULL);
       if (status == 0)
        {
-         if (output_file != tmpname)
-           status = smart_rename (tmpname,
-                                  output_file ? output_file : argv[i],
-                                  copyfd, &statbuf, preserve_dates) != 0;
+         const char *oname = output_file ? output_file : argv[i];
+         status = smart_rename (tmpname, oname, copyfd,
+                                &statbuf, preserve_dates) != 0;
          if (status == 0)
            status = hold_status;
        }
@@ -5944,9 +5943,9 @@ copy_main (int argc, char *argv[])
             output_target, input_arch);
   if (status == 0)
     {
-      if (tmpname != output_filename)
-       status = smart_rename (tmpname, input_filename, copyfd,
-                              &statbuf, preserve_dates) != 0;
+      const char *oname = output_filename ? output_filename : input_filename;
+      status = smart_rename (tmpname, oname, copyfd,
+                            &statbuf, preserve_dates) != 0;
     }
   else
     {
index 861c2b56d183b4f194bcc9afe65fd2e7150c3093..0427348df5b6d83065fe7129e36ffb1fe7a6da9d 100644 (file)
@@ -129,16 +129,19 @@ int
 smart_rename (const char *from, const char *to, int fromfd,
              struct stat *target_stat, bfd_boolean preserve_dates)
 {
-  int ret;
+  int ret = 0;
 
-  ret = simple_copy (fromfd, to, target_stat);
-  if (ret != 0)
-    non_fatal (_("unable to copy file '%s'; reason: %s"),
-              to, strerror (errno));
+  if (to != from)
+    {
+      ret = simple_copy (fromfd, to, target_stat);
+      if (ret != 0)
+       non_fatal (_("unable to copy file '%s'; reason: %s"),
+                  to, strerror (errno));
+      unlink (from);
+    }
 
   if (preserve_dates)
     set_times (to, target_stat);
-  unlink (from);
 
   return ret;
 }