Skip to content

Commit d27cf27

Browse files
committedFeb 14, 2019
[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
1 parent 7f95f96 commit d27cf27

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
 

‎llvm/lib/Support/Path.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -1128,26 +1128,27 @@ TempFile::~TempFile() { assert(Done); }
11281128

11291129
Error TempFile::discard() {
11301130
Done = true;
1131-
std::error_code RemoveEC;
1132-
// On windows closing will remove the file.
1133-
#ifndef _WIN32
1134-
// Always try to close and remove.
1135-
if (!TmpName.empty()) {
1136-
RemoveEC = fs::remove(TmpName);
1137-
sys::DontRemoveFileOnSignal(TmpName);
1138-
}
1139-
#endif
1140-
1141-
if (!RemoveEC)
1142-
TmpName = "";
1143-
11441131
if (FD != -1 && close(FD) == -1) {
11451132
std::error_code EC = std::error_code(errno, std::generic_category());
11461133
return errorCodeToError(EC);
11471134
}
11481135
FD = -1;
11491136

1137+
#ifdef _WIN32
1138+
// On windows closing will remove the file.
1139+
TmpName = "";
1140+
return Error::success();
1141+
#else
1142+
// Always try to close and remove.
1143+
std::error_code RemoveEC;
1144+
if (!TmpName.empty()) {
1145+
RemoveEC = fs::remove(TmpName);
1146+
sys::DontRemoveFileOnSignal(TmpName);
1147+
if (!RemoveEC)
1148+
TmpName = "";
1149+
}
11501150
return errorCodeToError(RemoveEC);
1151+
#endif
11511152
}
11521153

11531154
Error TempFile::keep(const Twine &Name) {

0 commit comments

Comments
 (0)
Please sign in to comment.