[llvm] r340996 - [llvm-strip] Fix -p|--preserve-dates to not truncate output when used in-place.

Jordan Rupprecht via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 29 16:21:56 PDT 2018


Author: rupprecht
Date: Wed Aug 29 16:21:56 2018
New Revision: 340996

URL: http://llvm.org/viewvc/llvm-project?rev=340996&view=rev
Log:
[llvm-strip] Fix -p|--preserve-dates to not truncate output when used in-place.

The restoreDateOnFile() method used to preserve dates uses sys::fs::openFileForWrite(). That method defaults to opening files with CD_CreateAlways, which truncates the output file if it exists. Use CD_OpenExisting instead to open it and *not* truncate it, which also has the side benefit of erroring if the file does not exist (it should always exist, because we just wrote it out).

Also, fix the test case to make sure the output is a valid output file, and not empty. The extra test assertions are enough to catch this regression.

Modified:
    llvm/trunk/test/tools/llvm-objcopy/strip-preserve-time.test
    llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp

Modified: llvm/trunk/test/tools/llvm-objcopy/strip-preserve-time.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/strip-preserve-time.test?rev=340996&r1=340995&r2=340996&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-preserve-time.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/strip-preserve-time.test Wed Aug 29 16:21:56 2018
@@ -7,6 +7,8 @@
 # RUN: llvm-strip -p %t.1.o -o %t-preserved.1.o
 # RUN: ls -lu %t-preserved.1.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t-preserved.1.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# Check that the stripped output is in fact a valid object file.
+# RUN: llvm-readobj %t-preserved.1.o
 
 # Preserve dates available via objcopy interface as well.
 # RUN: yaml2obj %s > %t.2.o
@@ -15,6 +17,7 @@
 # RUN: llvm-objcopy -p %t.2.o %t-preserved.2.o
 # RUN: ls -lu %t-preserved.2.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t-preserved.2.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj %t-preserved.2.o
 
 # Preserve dates when stripping in place.
 # RUN: yaml2obj %s > %t.3.o
@@ -23,6 +26,7 @@
 # RUN: llvm-strip -p %t.3.o
 # RUN: ls -lu %t.3.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t.3.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj %t.3.o
 
 # Without -p set, don't preserve dates.
 # RUN: yaml2obj %s > %t.4.o
@@ -31,6 +35,7 @@
 # RUN: llvm-strip %t.4.o
 # RUN: ls -lu %t.4.o | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-ATIME
 # RUN: ls -l %t.4.o | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-MTIME
+# RUN: llvm-readobj %t.4.o
 
 # Preserve dates in archives.
 # RUN: yaml2obj %s > %t.5.o
@@ -41,6 +46,7 @@
 # RUN: llvm-strip -p %t.a
 # RUN: ls -lu %t.a | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t.a | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj %t.a
 
 # Preserve dates in split DWO files.
 # RUN: cp %p/Inputs/dwarf.dwo %t-input.dwo
@@ -49,8 +55,10 @@
 # RUN: llvm-objcopy -p -split-dwo=%t-dwo %t-input.dwo %t-nondwo
 # RUN: ls -lu %t-dwo | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t-dwo | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj %t-dwo
 # RUN: ls -lu %t-nondwo | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
 # RUN: ls -l %t-nondwo | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj %t-nondwo
 
 # CHECK-PRESERVE-ATIME:        1995
 # CHECK-PRESERVE-MTIME:        1997

Modified: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp?rev=340996&r1=340995&r2=340996&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp Wed Aug 29 16:21:56 2018
@@ -748,7 +748,8 @@ static void restoreDateOnFile(StringRef
                               const sys::fs::file_status &Stat) {
   int FD;
 
-  if (auto EC = sys::fs::openFileForWrite(Filename, FD))
+  if (auto EC =
+          sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
     reportError(Filename, EC);
 
   if (auto EC = sys::fs::setLastAccessAndModificationTime(




More information about the llvm-commits mailing list