summaryrefslogtreecommitdiff
path: root/lib/Support/ToolOutputFile.cpp
blob: a1c94f47648f6820e3abff9031a6c9fb806785d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//===- ToolOutputFile.cpp -------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/Support/ToolOutputFile.h>

#include <mcld/Support/Path.h>
#include <mcld/Support/FileHandle.h>
#include <mcld/Support/MemoryArea.h>

#include <mcld/Support/SystemUtils.h>
#include <mcld/Support/MsgHandling.h>

#include <llvm/Support/Signals.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/FormattedStream.h>

using namespace mcld;

//===----------------------------------------------------------------------===//
// CleanupInstaller
//===----------------------------------------------------------------------===//
ToolOutputFile::CleanupInstaller::CleanupInstaller(const sys::fs::Path& pPath)
  : Keep(false), m_Path(pPath) {
  // Arrange for the file to be deleted if the process is killed.
  if ("-" != m_Path.native())
    llvm::sys::RemoveFileOnSignal(m_Path.native());
}

ToolOutputFile::CleanupInstaller::~CleanupInstaller()
{
  // Delete the file if the client hasn't told us not to.
  // FIXME: In Windows, some path in CJK characters can not be removed by LLVM
  // llvm::sys::Path
  if (!Keep && "_" != m_Path.native()) {
    bool Existed;
    llvm::sys::fs::remove(m_Path.native(), Existed);
  }

  // Ok, the file is successfully written and closed, or deleted. There's no
  // further need to clean it up on signals.
  if ("_" != m_Path.native())
    llvm::sys::DontRemoveFileOnSignal(m_Path.native());
}

//===----------------------------------------------------------------------===//
// ToolOutputFile
//===----------------------------------------------------------------------===//
ToolOutputFile::ToolOutputFile(const sys::fs::Path& pPath,
                               FileHandle::OpenMode pMode,
                               FileHandle::Permission pPermission)
  : m_Installer(pPath),
    m_pFdOstream(NULL),
    m_pFormattedOstream(NULL) {

  if (!m_FileHandle.open(pPath, pMode, pPermission)) {
    // If open fails, no clean-up is needed.
    m_Installer.Keep = true;
    fatal(diag::err_cannot_open_output_file)
      << pPath
      << sys::strerror(m_FileHandle.error());
    return;
  }
}

ToolOutputFile::~ToolOutputFile()
{
  if (m_pFormattedOstream != NULL)
    delete m_pFormattedOstream;
  if (m_pFdOstream != NULL)
    delete m_pFdOstream;
}

void ToolOutputFile::keep()
{
  m_Installer.Keep = true;
}

/// os - Return the containeed raw_fd_ostream.
/// Since os is rarely used, we lazily initialize it.
llvm::raw_fd_ostream& ToolOutputFile::os()
{
  if (m_pFdOstream == NULL) {
    assert(m_FileHandle.isOpened() &&
           m_FileHandle.isGood() &&
           m_FileHandle.isWritable());
    m_pFdOstream = new llvm::raw_fd_ostream(m_FileHandle.handler(), false);
  }
  return *m_pFdOstream;
}

/// formatted_os - Return the contained formatted_raw_ostream
llvm::formatted_raw_ostream& ToolOutputFile::formatted_os()
{
  if (m_pFormattedOstream == NULL) {
    m_pFormattedOstream = new llvm::formatted_raw_ostream(os());
  }
  return *m_pFormattedOstream;
}