Dan Gohman | e4f1a9b | 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 | 99ccd5d | 2013-06-17 20:37:56 +0000 | [diff] [blame^] | 15 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Signals.h" |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | tool_output_file::CleanupInstaller::CleanupInstaller(const char *filename) |
| 20 | : Filename(filename), Keep(false) { |
| 21 | // Arrange for the file to be deleted if the process is killed. |
| 22 | if (Filename != "-") |
Rafael Espindola | b7e2188 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 23 | sys::RemoveFileOnSignal(Filename); |
Dan Gohman | e4f1a9b | 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 | 99ccd5d | 2013-06-17 20:37:56 +0000 | [diff] [blame^] | 28 | if (!Keep && Filename != "-") { |
| 29 | bool Existed; |
| 30 | sys::fs::remove(Filename, Existed); |
| 31 | } |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 32 | |
| 33 | // Ok, the file is successfully written and closed, or deleted. There's no |
| 34 | // further need to clean it up on signals. |
| 35 | if (Filename != "-") |
Rafael Espindola | b7e2188 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 36 | sys::DontRemoveFileOnSignal(Filename); |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo, |
| 40 | unsigned Flags) |
| 41 | : Installer(filename), |
| 42 | OS(filename, ErrorInfo, Flags) { |
| 43 | // If open fails, no cleanup is needed. |
| 44 | if (!ErrorInfo.empty()) |
| 45 | Installer.Keep = true; |
| 46 | } |
Rafael Espindola | 68c0efa | 2013-06-17 18:05:35 +0000 | [diff] [blame] | 47 | |
| 48 | tool_output_file::tool_output_file(const char *Filename, int FD) |
| 49 | : Installer(Filename), OS(FD, true) { |
| 50 | } |