1*9880d681SAndroid Build Coastguard Worker //===--- ToolOutputFile.cpp - Implement the tool_output_file class --------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This implements the tool_output_file class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ToolOutputFile.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Signals.h"
17*9880d681SAndroid Build Coastguard Worker using namespace llvm;
18*9880d681SAndroid Build Coastguard Worker
CleanupInstaller(StringRef Filename)19*9880d681SAndroid Build Coastguard Worker tool_output_file::CleanupInstaller::CleanupInstaller(StringRef Filename)
20*9880d681SAndroid Build Coastguard Worker : Filename(Filename), Keep(false) {
21*9880d681SAndroid Build Coastguard Worker // Arrange for the file to be deleted if the process is killed.
22*9880d681SAndroid Build Coastguard Worker if (Filename != "-")
23*9880d681SAndroid Build Coastguard Worker sys::RemoveFileOnSignal(Filename);
24*9880d681SAndroid Build Coastguard Worker }
25*9880d681SAndroid Build Coastguard Worker
~CleanupInstaller()26*9880d681SAndroid Build Coastguard Worker tool_output_file::CleanupInstaller::~CleanupInstaller() {
27*9880d681SAndroid Build Coastguard Worker // Delete the file if the client hasn't told us not to.
28*9880d681SAndroid Build Coastguard Worker if (!Keep && Filename != "-")
29*9880d681SAndroid Build Coastguard Worker sys::fs::remove(Filename);
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker // Ok, the file is successfully written and closed, or deleted. There's no
32*9880d681SAndroid Build Coastguard Worker // further need to clean it up on signals.
33*9880d681SAndroid Build Coastguard Worker if (Filename != "-")
34*9880d681SAndroid Build Coastguard Worker sys::DontRemoveFileOnSignal(Filename);
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker
tool_output_file(StringRef Filename,std::error_code & EC,sys::fs::OpenFlags Flags)37*9880d681SAndroid Build Coastguard Worker tool_output_file::tool_output_file(StringRef Filename, std::error_code &EC,
38*9880d681SAndroid Build Coastguard Worker sys::fs::OpenFlags Flags)
39*9880d681SAndroid Build Coastguard Worker : Installer(Filename), OS(Filename, EC, Flags) {
40*9880d681SAndroid Build Coastguard Worker // If open fails, no cleanup is needed.
41*9880d681SAndroid Build Coastguard Worker if (EC)
42*9880d681SAndroid Build Coastguard Worker Installer.Keep = true;
43*9880d681SAndroid Build Coastguard Worker }
44*9880d681SAndroid Build Coastguard Worker
tool_output_file(StringRef Filename,int FD)45*9880d681SAndroid Build Coastguard Worker tool_output_file::tool_output_file(StringRef Filename, int FD)
46*9880d681SAndroid Build Coastguard Worker : Installer(Filename), OS(FD, true) {}
47