LLVM  10.0.0svn
FileOutputBuffer.cpp
Go to the documentation of this file.
1 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility for creating a in-memory buffer that will be written to a file.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/Memory.h"
18 #include "llvm/Support/Path.h"
19 #include <system_error>
20 
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
22 #include <unistd.h>
23 #else
24 #include <io.h>
25 #endif
26 
27 using namespace llvm;
28 using namespace llvm::sys;
29 
30 namespace {
31 // A FileOutputBuffer which creates a temporary file in the same directory
32 // as the final output file. The final output file is atomically replaced
33 // with the temporary file on commit().
34 class OnDiskBuffer : public FileOutputBuffer {
35 public:
36  OnDiskBuffer(StringRef Path, fs::TempFile Temp,
37  std::unique_ptr<fs::mapped_file_region> Buf)
38  : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
39 
40  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
41 
42  uint8_t *getBufferEnd() const override {
43  return (uint8_t *)Buffer->data() + Buffer->size();
44  }
45 
46  size_t getBufferSize() const override { return Buffer->size(); }
47 
48  Error commit() override {
49  // Unmap buffer, letting OS flush dirty pages to file on disk.
50  Buffer.reset();
51 
52  // Atomically replace the existing file with the new one.
53  return Temp.keep(FinalPath);
54  }
55 
56  ~OnDiskBuffer() override {
57  // Close the mapping before deleting the temp file, so that the removal
58  // succeeds.
59  Buffer.reset();
60  consumeError(Temp.discard());
61  }
62 
63  void discard() override {
64  // Delete the temp file if it still was open, but keeping the mapping
65  // active.
66  consumeError(Temp.discard());
67  }
68 
69 private:
70  std::unique_ptr<fs::mapped_file_region> Buffer;
71  fs::TempFile Temp;
72 };
73 
74 // A FileOutputBuffer which keeps data in memory and writes to the final
75 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
76 class InMemoryBuffer : public FileOutputBuffer {
77 public:
78  InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
79  unsigned Mode)
80  : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
81  Mode(Mode) {}
82 
83  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
84 
85  uint8_t *getBufferEnd() const override {
86  return (uint8_t *)Buffer.base() + BufferSize;
87  }
88 
89  size_t getBufferSize() const override { return BufferSize; }
90 
91  Error commit() override {
92  if (FinalPath == "-") {
93  llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
94  llvm::outs().flush();
95  return Error::success();
96  }
97 
98  using namespace sys::fs;
99  int FD;
100  std::error_code EC;
101  if (auto EC =
102  openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
103  return errorCodeToError(EC);
104  raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
105  OS << StringRef((const char *)Buffer.base(), BufferSize);
106  return Error::success();
107  }
108 
109 private:
110  // Buffer may actually contain a larger memory block than BufferSize
111  OwningMemoryBlock Buffer;
112  size_t BufferSize;
113  unsigned Mode;
114 };
115 } // namespace
116 
118 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
119  std::error_code EC;
121  Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
122  if (EC)
123  return errorCodeToError(EC);
124  return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
125 }
126 
128 createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
129  Expected<fs::TempFile> FileOrErr =
130  fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
131  if (!FileOrErr)
132  return FileOrErr.takeError();
133  fs::TempFile File = std::move(*FileOrErr);
134 
135 #ifndef _WIN32
136  // On Windows, CreateFileMapping (the mmap function on Windows)
137  // automatically extends the underlying file. We don't need to
138  // extend the file beforehand. _chsize (ftruncate on Windows) is
139  // pretty slow just like it writes specified amount of bytes,
140  // so we should avoid calling that function.
141  if (auto EC = fs::resize_file(File.FD, Size)) {
142  consumeError(File.discard());
143  return errorCodeToError(EC);
144  }
145 #endif
146 
147  // Mmap it.
148  std::error_code EC;
149  auto MappedFile = std::make_unique<fs::mapped_file_region>(
151  Size, 0, EC);
152 
153  // mmap(2) can fail if the underlying filesystem does not support it.
154  // If that happens, we fall back to in-memory buffer as the last resort.
155  if (EC) {
156  consumeError(File.discard());
157  return createInMemoryBuffer(Path, Size, Mode);
158  }
159 
160  return std::make_unique<OnDiskBuffer>(Path, std::move(File),
161  std::move(MappedFile));
162 }
163 
164 // Create an instance of FileOutputBuffer.
166 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
167  // Handle "-" as stdout just like llvm::raw_ostream does.
168  if (Path == "-")
169  return createInMemoryBuffer("-", Size, /*Mode=*/0);
170 
171  unsigned Mode = fs::all_read | fs::all_write;
172  if (Flags & F_executable)
173  Mode |= fs::all_exe;
174 
175  fs::file_status Stat;
176  fs::status(Path, Stat);
177 
178  // Usually, we want to create OnDiskBuffer to create a temporary file in
179  // the same directory as the destination file and atomically replaces it
180  // by rename(2).
181  //
182  // However, if the destination file is a special file, we don't want to
183  // use rename (e.g. we don't want to replace /dev/null with a regular
184  // file.) If that's the case, we create an in-memory buffer, open the
185  // destination file and write to it on commit().
186  switch (Stat.type()) {
192  return createOnDiskBuffer(Path, Size, Mode);
193  default:
194  return createInMemoryBuffer(Path, Size, Mode);
195  }
196 }
SI Whole Quad Mode
static Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Definition: Path.cpp:1227
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:246
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e...
static Expected< std::unique_ptr< FileOutputBuffer > > createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode)
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:979
Definition: BitVector.h:937
Represents a temporary file.
Definition: FileSystem.h:844
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1164
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
Instrumentation for Order File
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
std::error_code resize_file(int FD, uint64_t Size)
Resize path to size.
static Expected< std::unique_ptr< InMemoryBuffer > > createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode)
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1032
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
This class encapsulates the notion of a memory block which has an address and a size.
Definition: Memory.h:32
Owning version of MemoryBlock.
Definition: Memory.h:128
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:384
uint32_t Size
Definition: Profile.cpp:46
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:738
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48