Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 1 | //===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===// |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 9 | // This implements the ToolOutputFile class. |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Support/ToolOutputFile.h" |
Rafael Espindola | 702a80c | 2013-06-17 20:37:56 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Signals.h" |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 18 | ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename) |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 19 | : Filename(Filename), Keep(false) { |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 20 | // Arrange for the file to be deleted if the process is killed. |
| 21 | if (Filename != "-") |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 22 | sys::RemoveFileOnSignal(Filename); |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 25 | ToolOutputFile::CleanupInstaller::~CleanupInstaller() { |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 26 | // Delete the file if the client hasn't told us not to. |
Rafael Espindola | 81e7fd0 | 2014-01-10 21:40:29 +0000 | [diff] [blame] | 27 | if (!Keep && Filename != "-") |
| 28 | sys::fs::remove(Filename); |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 29 | |
| 30 | // Ok, the file is successfully written and closed, or deleted. There's no |
| 31 | // further need to clean it up on signals. |
| 32 | if (Filename != "-") |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 33 | sys::DontRemoveFileOnSignal(Filename); |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 36 | ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC, |
| 37 | sys::fs::OpenFlags Flags) |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 38 | : Installer(Filename), OS(Filename, EC, Flags) { |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 39 | // If open fails, no cleanup is needed. |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 40 | if (EC) |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 41 | Installer.Keep = true; |
| 42 | } |
Rafael Espindola | 40c908b | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 43 | |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 44 | ToolOutputFile::ToolOutputFile(StringRef Filename, int FD) |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 45 | : Installer(Filename), OS(FD, true) {} |