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