From d27cf27eb1fe49a244260e53df8e93a1dc10f7c8 Mon Sep 17 00:00:00 2001 From: Andrew Ng Date: Thu, 14 Feb 2019 11:08:49 +0000 Subject: [PATCH] [Support] Fix TempFile::discard to not leave behind temporary files Moved the remove of the temporary file to after the close to avoid remove failures caused by ETXTBSY errors. This issue was seen when FileOutputBuffer falls back to an in memory buffer due to the inability to mmap the on disk file. This occurred when running LLD on an Ubuntu VM in VirtualBox on a Windows host attempting to write the output to a VirtualBox shared folder. Differential Revision: https://reviews.llvm.org/D57960 llvm-svn: 354017 --- llvm/lib/Support/Path.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 8f580c66d244..202248c18d83 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -1128,26 +1128,27 @@ TempFile::~TempFile() { assert(Done); } Error TempFile::discard() { Done = true; - std::error_code RemoveEC; -// On windows closing will remove the file. -#ifndef _WIN32 - // Always try to close and remove. - if (!TmpName.empty()) { - RemoveEC = fs::remove(TmpName); - sys::DontRemoveFileOnSignal(TmpName); - } -#endif - - if (!RemoveEC) - TmpName = ""; - if (FD != -1 && close(FD) == -1) { std::error_code EC = std::error_code(errno, std::generic_category()); return errorCodeToError(EC); } FD = -1; +#ifdef _WIN32 + // On windows closing will remove the file. + TmpName = ""; + return Error::success(); +#else + // Always try to close and remove. + std::error_code RemoveEC; + if (!TmpName.empty()) { + RemoveEC = fs::remove(TmpName); + sys::DontRemoveFileOnSignal(TmpName); + if (!RemoveEC) + TmpName = ""; + } return errorCodeToError(RemoveEC); +#endif } Error TempFile::keep(const Twine &Name) {