blob: af44b4386669cde54398fcf8e13a9e7ca2869294 [file] [log] [blame]
Dan Gohmane4f1a9b2010-10-07 20:32:40 +00001//===--- 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 Espindola338d8592013-06-17 19:54:17 +000015#include "llvm/Support/PathV1.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000016#include "llvm/Support/Signals.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000017using namespace llvm;
18
19tool_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 Espindolab7e21882013-06-13 21:16:58 +000023 sys::RemoveFileOnSignal(Filename);
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000024}
25
26tool_output_file::CleanupInstaller::~CleanupInstaller() {
27 // Delete the file if the client hasn't told us not to.
Rafael Espindola338d8592013-06-17 19:54:17 +000028 if (!Keep && Filename != "-")
29 sys::Path(Filename).eraseFromDisk();
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000030
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 Espindolab7e21882013-06-13 21:16:58 +000034 sys::DontRemoveFileOnSignal(Filename);
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000035}
36
37tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
38 unsigned Flags)
39 : Installer(filename),
40 OS(filename, ErrorInfo, Flags) {
41 // If open fails, no cleanup is needed.
42 if (!ErrorInfo.empty())
43 Installer.Keep = true;
44}
Rafael Espindola68c0efa2013-06-17 18:05:35 +000045
46tool_output_file::tool_output_file(const char *Filename, int FD)
47 : Installer(Filename), OS(FD, true) {
48}