LLVM  8.0.0svn
FileOutputBuffer.cpp
Go to the documentation of this file.
1 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
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 // Utility for creating a in-memory buffer that will be written to a file.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Memory.h"
19 #include "llvm/Support/Path.h"
20 #include <system_error>
21 
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
23 #include <unistd.h>
24 #else
25 #include <io.h>
26 #endif
27 
28 using namespace llvm;
29 using namespace llvm::sys;
30 
31 namespace {
32 // A FileOutputBuffer which creates a temporary file in the same directory
33 // as the final output file. The final output file is atomically replaced
34 // with the temporary file on commit().
35 class OnDiskBuffer : public FileOutputBuffer {
36 public:
37  OnDiskBuffer(StringRef Path, fs::TempFile Temp,
38  std::unique_ptr<fs::mapped_file_region> Buf)
39  : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
40 
41  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
42 
43  uint8_t *getBufferEnd() const override {
44  return (uint8_t *)Buffer->data() + Buffer->size();
45  }
46 
47  size_t getBufferSize() const override { return Buffer->size(); }
48 
49  Error commit() override {
50  // Unmap buffer, letting OS flush dirty pages to file on disk.
51  Buffer.reset();
52 
53  // Atomically replace the existing file with the new one.
54  return Temp.keep(FinalPath);
55  }
56 
57  ~OnDiskBuffer() override {
58  // Close the mapping before deleting the temp file, so that the removal
59  // succeeds.
60  Buffer.reset();
61  consumeError(Temp.discard());
62  }
63 
64 private:
65  std::unique_ptr<fs::mapped_file_region> Buffer;
66  fs::TempFile Temp;
67 };
68 
69 // A FileOutputBuffer which keeps data in memory and writes to the final
70 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
71 class InMemoryBuffer : public FileOutputBuffer {
72 public:
73  InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
74  : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
75 
76  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
77 
78  uint8_t *getBufferEnd() const override {
79  return (uint8_t *)Buffer.base() + Buffer.size();
80  }
81 
82  size_t getBufferSize() const override { return Buffer.size(); }
83 
84  Error commit() override {
85  using namespace sys::fs;
86  int FD;
87  std::error_code EC;
88  if (auto EC =
89  openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
90  return errorCodeToError(EC);
91  raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
92  OS << StringRef((const char *)Buffer.base(), Buffer.size());
93  return Error::success();
94  }
95 
96 private:
97  OwningMemoryBlock Buffer;
98  unsigned Mode;
99 };
100 } // namespace
101 
103 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
104  std::error_code EC;
106  Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
107  if (EC)
108  return errorCodeToError(EC);
109  return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
110 }
111 
113 createOnDiskBuffer(StringRef Path, size_t Size, bool InitExisting,
114  unsigned Mode) {
115  Expected<fs::TempFile> FileOrErr =
116  fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
117  if (!FileOrErr)
118  return FileOrErr.takeError();
119  fs::TempFile File = std::move(*FileOrErr);
120 
121  if (InitExisting) {
122  if (auto EC = sys::fs::copy_file(Path, File.FD))
123  return errorCodeToError(EC);
124  } else {
125 #ifndef _WIN32
126  // On Windows, CreateFileMapping (the mmap function on Windows)
127  // automatically extends the underlying file. We don't need to
128  // extend the file beforehand. _chsize (ftruncate on Windows) is
129  // pretty slow just like it writes specified amount of bytes,
130  // so we should avoid calling that function.
131  if (auto EC = fs::resize_file(File.FD, Size)) {
132  consumeError(File.discard());
133  return errorCodeToError(EC);
134  }
135 #endif
136  }
137 
138  // Mmap it.
139  std::error_code EC;
140  auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
142  if (EC) {
143  consumeError(File.discard());
144  return errorCodeToError(EC);
145  }
146  return llvm::make_unique<OnDiskBuffer>(Path, std::move(File),
147  std::move(MappedFile));
148 }
149 
150 // Create an instance of FileOutputBuffer.
152 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
153  unsigned Mode = fs::all_read | fs::all_write;
154  if (Flags & F_executable)
155  Mode |= fs::all_exe;
156 
157  fs::file_status Stat;
158  fs::status(Path, Stat);
159 
160  if ((Flags & F_modify) && Size == size_t(-1)) {
161  if (Stat.type() == fs::file_type::regular_file)
162  Size = Stat.getSize();
163  else if (Stat.type() == fs::file_type::file_not_found)
165  else
167  }
168 
169  // Usually, we want to create OnDiskBuffer to create a temporary file in
170  // the same directory as the destination file and atomically replaces it
171  // by rename(2).
172  //
173  // However, if the destination file is a special file, we don't want to
174  // use rename (e.g. we don't want to replace /dev/null with a regular
175  // file.) If that's the case, we create an in-memory buffer, open the
176  // destination file and write to it on commit().
177  switch (Stat.type()) {
183  return createOnDiskBuffer(Path, Size, !!(Flags & F_modify), Mode);
184  default:
185  return createInMemoryBuffer(Path, Size, Mode);
186  }
187 }
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...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Error takeError()
Take ownership of the stored error.
Definition: Error.h:550
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:1222
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:229
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...
Definition: BitVector.h:921
Represents a temporary file.
Definition: FileSystem.h:782
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: CachePruning.h:23
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1055
static Expected< std::unique_ptr< OnDiskBuffer > > createOnDiskBuffer(StringRef Path, size_t Size, bool InitExisting, unsigned Mode)
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:963
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
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:927
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:975
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:29
Owning version of MemoryBlock.
Definition: Memory.h:119
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:697
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49