ToolOutputFile.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Signals.h"
17using 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 != "-")
23    sys::RemoveFileOnSignal(Filename);
24}
25
26tool_output_file::CleanupInstaller::~CleanupInstaller() {
27  // Delete the file if the client hasn't told us not to.
28  if (!Keep && Filename != "-")
29    sys::fs::remove(Filename);
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 != "-")
34    sys::DontRemoveFileOnSignal(Filename);
35}
36
37tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
38                                   sys::fs::OpenFlags Flags)
39    : Installer(filename), OS(filename, ErrorInfo, Flags) {
40  // If open fails, no cleanup is needed.
41  if (!ErrorInfo.empty())
42    Installer.Keep = true;
43}
44
45tool_output_file::tool_output_file(const char *Filename, int FD)
46    : Installer(Filename), OS(FD, true) {
47}
48