LLVM  14.0.0git
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/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, fs::mapped_file_region Buf)
37  : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
38 
39  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.data(); }
40 
41  uint8_t *getBufferEnd() const override {
42  return (uint8_t *)Buffer.data() + Buffer.size();
43  }
44 
45  size_t getBufferSize() const override { return Buffer.size(); }
46 
47  Error commit() override {
48  // Unmap buffer, letting OS flush dirty pages to file on disk.
49  Buffer.unmap();
50 
51  // Atomically replace the existing file with the new one.
52  return Temp.keep(FinalPath);
53  }
54 
55  ~OnDiskBuffer() override {
56  // Close the mapping before deleting the temp file, so that the removal
57  // succeeds.
58  Buffer.unmap();
59  consumeError(Temp.discard());
60  }
61 
62  void discard() override {
63  // Delete the temp file if it still was open, but keeping the mapping
64  // active.
65  consumeError(Temp.discard());
66  }
67 
68 private:
70  fs::TempFile Temp;
71 };
72 
73 // A FileOutputBuffer which keeps data in memory and writes to the final
74 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
75 class InMemoryBuffer : public FileOutputBuffer {
76 public:
77  InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
78  unsigned Mode)
79  : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
80  Mode(Mode) {}
81 
82  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
83 
84  uint8_t *getBufferEnd() const override {
85  return (uint8_t *)Buffer.base() + BufferSize;
86  }
87 
88  size_t getBufferSize() const override { return BufferSize; }
89 
90  Error commit() override {
91  if (FinalPath == "-") {
92  llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
93  llvm::outs().flush();
94  return Error::success();
95  }
96 
97  using namespace sys::fs;
98  int FD;
99  std::error_code EC;
100  if (auto EC =
101  openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
102  return errorCodeToError(EC);
103  raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
104  OS << StringRef((const char *)Buffer.base(), BufferSize);
105  return Error::success();
106  }
107 
108 private:
109  // Buffer may actually contain a larger memory block than BufferSize
110  OwningMemoryBlock Buffer;
111  size_t BufferSize;
112  unsigned Mode;
113 };
114 } // namespace
115 
117 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
118  std::error_code EC;
121  if (EC)
122  return errorCodeToError(EC);
123  return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
124 }
125 
127 createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
128  Expected<fs::TempFile> FileOrErr =
129  fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
130  if (!FileOrErr)
131  return FileOrErr.takeError();
132  fs::TempFile File = std::move(*FileOrErr);
133 
135  consumeError(File.discard());
136  return errorCodeToError(EC);
137  }
138 
139  // Mmap it.
140  std::error_code EC;
141  fs::mapped_file_region MappedFile =
144 
145  // mmap(2) can fail if the underlying filesystem does not support it.
146  // If that happens, we fall back to in-memory buffer as the last resort.
147  if (EC) {
148  consumeError(File.discard());
149  return createInMemoryBuffer(Path, Size, Mode);
150  }
151 
152  return std::make_unique<OnDiskBuffer>(Path, std::move(File),
153  std::move(MappedFile));
154 }
155 
156 // Create an instance of FileOutputBuffer.
158 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
159  // Handle "-" as stdout just like llvm::raw_ostream does.
160  if (Path == "-")
161  return createInMemoryBuffer("-", Size, /*Mode=*/0);
162 
163  unsigned Mode = fs::all_read | fs::all_write;
164  if (Flags & F_executable)
165  Mode |= fs::all_exe;
166 
167  // If Size is zero, don't use mmap which will fail with EINVAL.
168  if (Size == 0)
169  return createInMemoryBuffer(Path, Size, Mode);
170 
171  fs::file_status Stat;
172  fs::status(Path, Stat);
173 
174  // Usually, we want to create OnDiskBuffer to create a temporary file in
175  // the same directory as the destination file and atomically replaces it
176  // by rename(2).
177  //
178  // However, if the destination file is a special file, we don't want to
179  // use rename (e.g. we don't want to replace /dev/null with a regular
180  // file.) If that's the case, we create an in-memory buffer, open the
181  // destination file and write to it on commit().
182  switch (Stat.type()) {
188  if (Flags & F_no_mmap)
189  return createInMemoryBuffer(Path, Size, Mode);
190  else
191  return createOnDiskBuffer(Path, Size, Mode);
192  default:
193  return createInMemoryBuffer(Path, Size, Mode);
194  }
195 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::sys::fs::openFileForWrite
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:1040
llvm::sys::Memory::MF_READ
@ MF_READ
Definition: Memory.h:55
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::sys::Memory::MF_WRITE
@ MF_WRITE
Definition: Memory.h:56
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
llvm::sys::fs::resize_file_before_mapping_readwrite
std::error_code resize_file_before_mapping_readwrite(int FD, uint64_t Size)
Resize FD to Size before mapping mapped_file_region::readwrite.
Definition: FileSystem.h:418
llvm::errc::is_a_directory
@ is_a_directory
Path.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::sys::Memory::allocateMappedMemory
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....
Errc.h
llvm::sys::MemoryBlock
This class encapsulates the notion of a memory block which has an address and a size.
Definition: Memory.h:31
llvm::sys::fs::file_type::file_not_found
@ file_not_found
llvm::sys::fs::file_type::regular_file
@ regular_file
createOnDiskBuffer
static Expected< std::unique_ptr< FileOutputBuffer > > createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode)
Definition: FileOutputBuffer.cpp:127
FileOutputBuffer.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::sys
Definition: Atomic.h:28
llvm::sys::fs::all_write
@ all_write
Definition: FileSystem.h:101
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:884
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
llvm::sys::fs::file_type::status_error
@ status_error
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:186
llvm::sys::OwningMemoryBlock
Owning version of MemoryBlock.
Definition: Memory.h:137
llvm::sys::fs::file_status
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::FileOutputBuffer
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
Definition: FileOutputBuffer.h:28
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1609
llvm::codeview::CompileSym2Flags::EC
@ EC
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
llvm::sys::fs::CD_CreateAlways
@ CD_CreateAlways
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:733
llvm::sys::fs::basic_file_status::type
file_type type() const
Definition: FileSystem.h:184
llvm::sys::fs::mapped_file_region::readwrite
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1248
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Memory.h
llvm::FileOutputBuffer::create
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...
Definition: FileOutputBuffer.cpp:158
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:443
llvm::sys::fs::convertFDToNativeFile
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:987
llvm::sys::fs::file_type::directory_file
@ directory_file
std
Definition: BitVector.h:838
llvm::sys::fs::all_exe
@ all_exe
Definition: FileSystem.h:102
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
createInMemoryBuffer
static Expected< std::unique_ptr< InMemoryBuffer > > createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode)
Definition: FileOutputBuffer.cpp:117
llvm::sys::fs::all_read
@ all_read
Definition: FileSystem.h:100
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
llvm::sys::fs::mapped_file_region
This class represents a memory mapped file.
Definition: FileSystem.h:1244
llvm::sys::fs::status
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
llvm::sys::fs::TempFile
Represents a temporary file.
Definition: FileSystem.h:849
llvm::sys::fs::TempFile::create
static Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Definition: Path.cpp:1291
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205