1218885Sdim//===--- ToolOutputFile.cpp - Implement the tool_output_file class --------===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This implements the tool_output_file class.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14218885Sdim#include "llvm/Support/ToolOutputFile.h"
15263509Sdim#include "llvm/Support/FileSystem.h"
16218885Sdim#include "llvm/Support/Signals.h"
17218885Sdimusing namespace llvm;
18218885Sdim
19218885Sdimtool_output_file::CleanupInstaller::CleanupInstaller(const char *filename)
20218885Sdim  : Filename(filename), Keep(false) {
21218885Sdim  // Arrange for the file to be deleted if the process is killed.
22218885Sdim  if (Filename != "-")
23263509Sdim    sys::RemoveFileOnSignal(Filename);
24218885Sdim}
25218885Sdim
26218885Sdimtool_output_file::CleanupInstaller::~CleanupInstaller() {
27218885Sdim  // Delete the file if the client hasn't told us not to.
28263509Sdim  if (!Keep && Filename != "-") {
29263509Sdim    bool Existed;
30263509Sdim    sys::fs::remove(Filename, Existed);
31263509Sdim  }
32218885Sdim
33218885Sdim  // Ok, the file is successfully written and closed, or deleted. There's no
34218885Sdim  // further need to clean it up on signals.
35218885Sdim  if (Filename != "-")
36263509Sdim    sys::DontRemoveFileOnSignal(Filename);
37218885Sdim}
38218885Sdim
39218885Sdimtool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
40263509Sdim                                   sys::fs::OpenFlags Flags)
41263509Sdim    : Installer(filename), OS(filename, ErrorInfo, Flags) {
42218885Sdim  // If open fails, no cleanup is needed.
43218885Sdim  if (!ErrorInfo.empty())
44218885Sdim    Installer.Keep = true;
45218885Sdim}
46263509Sdim
47263509Sdimtool_output_file::tool_output_file(const char *Filename, int FD)
48263509Sdim    : Installer(Filename), OS(FD, true) {
49263509Sdim}
50