2017-09-23 09:03:17 +08:00
|
|
|
//===--- ToolOutputFile.cpp - Implement the ToolOutputFile class --------===//
|
2010-10-08 04:32:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-10-08 04:32:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-09-23 09:03:17 +08:00
|
|
|
// This implements the ToolOutputFile class.
|
2010-10-08 04:32:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2013-06-18 04:37:56 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2010-10-08 04:32:40 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
|
2020-01-29 03:23:46 +08:00
|
|
|
: Filename(std::string(Filename)), Keep(false) {
|
2010-10-08 04:32:40 +08:00
|
|
|
// Arrange for the file to be deleted if the process is killed.
|
|
|
|
if (Filename != "-")
|
2013-06-14 05:16:58 +08:00
|
|
|
sys::RemoveFileOnSignal(Filename);
|
2010-10-08 04:32:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
|
2010-10-08 04:32:40 +08:00
|
|
|
// Delete the file if the client hasn't told us not to.
|
2014-01-11 05:40:29 +08:00
|
|
|
if (!Keep && Filename != "-")
|
|
|
|
sys::fs::remove(Filename);
|
2010-10-08 04:32:40 +08:00
|
|
|
|
|
|
|
// Ok, the file is successfully written and closed, or deleted. There's no
|
|
|
|
// further need to clean it up on signals.
|
|
|
|
if (Filename != "-")
|
2013-06-14 05:16:58 +08:00
|
|
|
sys::DontRemoveFileOnSignal(Filename);
|
2010-10-08 04:32:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile::ToolOutputFile(StringRef Filename, std::error_code &EC,
|
|
|
|
sys::fs::OpenFlags Flags)
|
2014-08-26 02:16:47 +08:00
|
|
|
: Installer(Filename), OS(Filename, EC, Flags) {
|
2010-10-08 04:32:40 +08:00
|
|
|
// If open fails, no cleanup is needed.
|
2014-08-26 02:16:47 +08:00
|
|
|
if (EC)
|
2010-10-08 04:32:40 +08:00
|
|
|
Installer.Keep = true;
|
|
|
|
}
|
2013-06-18 02:05:35 +08:00
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile::ToolOutputFile(StringRef Filename, int FD)
|
2014-08-26 02:16:47 +08:00
|
|
|
: Installer(Filename), OS(FD, true) {}
|