[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.

llvm-svn: 340996
This commit is contained in:
Jordan Rupprecht 2018-08-29 23:21:56 +00:00
parent 6edcc9ee86
commit 7481540fd9
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -748,7 +748,8 @@ static void restoreDateOnFile(StringRef Filename,
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(