LLVM  16.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/Support/Errc.h"
16 #include "llvm/Support/Memory.h"
17 #include <system_error>
18 
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
20 #include <unistd.h>
21 #else
22 #include <io.h>
23 #endif
24 
25 using namespace llvm;
26 using namespace llvm::sys;
27 
28 namespace {
29 // A FileOutputBuffer which creates a temporary file in the same directory
30 // as the final output file. The final output file is atomically replaced
31 // with the temporary file on commit().
32 class OnDiskBuffer : public FileOutputBuffer {
33 public:
34  OnDiskBuffer(StringRef Path, fs::TempFile Temp, fs::mapped_file_region Buf)
35  : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
36 
37  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.data(); }
38 
39  uint8_t *getBufferEnd() const override {
40  return (uint8_t *)Buffer.data() + Buffer.size();
41  }
42 
43  size_t getBufferSize() const override { return Buffer.size(); }
44 
45  Error commit() override {
46  // Unmap buffer, letting OS flush dirty pages to file on disk.
47  Buffer.unmap();
48 
49  // Atomically replace the existing file with the new one.
50  return Temp.keep(FinalPath);
51  }
52 
53  ~OnDiskBuffer() override {
54  // Close the mapping before deleting the temp file, so that the removal
55  // succeeds.
56  Buffer.unmap();
57  consumeError(Temp.discard());
58  }
59 
60  void discard() override {
61  // Delete the temp file if it still was open, but keeping the mapping
62  // active.
63  consumeError(Temp.discard());
64  }
65 
66 private:
68  fs::TempFile Temp;
69 };
70 
71 // A FileOutputBuffer which keeps data in memory and writes to the final
72 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
73 class InMemoryBuffer : public FileOutputBuffer {
74 public:
75  InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
76  unsigned Mode)
77  : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
78  Mode(Mode) {}
79 
80  uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
81 
82  uint8_t *getBufferEnd() const override {
83  return (uint8_t *)Buffer.base() + BufferSize;
84  }
85 
86  size_t getBufferSize() const override { return BufferSize; }
87 
88  Error commit() override {
89  if (FinalPath == "-") {
90  llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
91  llvm::outs().flush();
92  return Error::success();
93  }
94 
95  using namespace sys::fs;
96  int FD;
97  std::error_code EC;
98  if (auto EC =
99  openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
100  return errorCodeToError(EC);
101  raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
102  OS << StringRef((const char *)Buffer.base(), BufferSize);
103  return Error::success();
104  }
105 
106 private:
107  // Buffer may actually contain a larger memory block than BufferSize
108  OwningMemoryBlock Buffer;
109  size_t BufferSize;
110  unsigned Mode;
111 };
112 } // namespace
113 
115 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
116  std::error_code EC;
118  Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
119  if (EC)
120  return errorCodeToError(EC);
121  return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
122 }
123 
125 createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
126  Expected<fs::TempFile> FileOrErr =
127  fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
128  if (!FileOrErr)
129  return FileOrErr.takeError();
130  fs::TempFile File = std::move(*FileOrErr);
131 
132  if (auto EC = fs::resize_file_before_mapping_readwrite(File.FD, Size)) {
133  consumeError(File.discard());
134  return errorCodeToError(EC);
135  }
136 
137  // Mmap it.
138  std::error_code EC;
139  fs::mapped_file_region MappedFile =
141  fs::mapped_file_region::readwrite, Size, 0, EC);
142 
143  // mmap(2) can fail if the underlying filesystem does not support it.
144  // If that happens, we fall back to in-memory buffer as the last resort.
145  if (EC) {
146  consumeError(File.discard());
147  return createInMemoryBuffer(Path, Size, Mode);
148  }
149 
150  return std::make_unique<OnDiskBuffer>(Path, std::move(File),
151  std::move(MappedFile));
152 }
153 
154 // Create an instance of FileOutputBuffer.
156 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
157  // Handle "-" as stdout just like llvm::raw_ostream does.
158  if (Path == "-")
159  return createInMemoryBuffer("-", Size, /*Mode=*/0);
160 
161  unsigned Mode = fs::all_read | fs::all_write;
162  if (Flags & F_executable)
163  Mode |= fs::all_exe;
164 
165  // If Size is zero, don't use mmap which will fail with EINVAL.
166  if (Size == 0)
167  return createInMemoryBuffer(Path, Size, Mode);
168 
169  fs::file_status Stat;
170  fs::status(Path, Stat);
171 
172  // Usually, we want to create OnDiskBuffer to create a temporary file in
173  // the same directory as the destination file and atomically replaces it
174  // by rename(2).
175  //
176  // However, if the destination file is a special file, we don't want to
177  // use rename (e.g. we don't want to replace /dev/null with a regular
178  // file.) If that's the case, we create an in-memory buffer, open the
179  // destination file and write to it on commit().
180  switch (Stat.type()) {
186  if (Flags & F_no_mmap)
187  return createInMemoryBuffer(Path, Size, Mode);
188  else
189  return createOnDiskBuffer(Path, Size, Mode);
190  default:
191  return createInMemoryBuffer(Path, Size, Mode);
192  }
193 }
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:1065
llvm::sys::Memory::MF_READ
@ MF_READ
Definition: Memory.h:55
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
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:125
FileOutputBuffer.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
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:1043
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:883
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
llvm::sys::fs::file_type::status_error
@ status_error
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:185
llvm::pdb::PDB_ColorItem::Path
@ Path
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:1666
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::mapped_file_region::readwrite
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1273
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:156
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:440
llvm::sys::fs::convertFDToNativeFile
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:993
llvm::sys::fs::file_type::directory_file
@ directory_file
std
Definition: BitVector.h:851
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:155
createInMemoryBuffer
static Expected< std::unique_ptr< InMemoryBuffer > > createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode)
Definition: FileOutputBuffer.cpp:115
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:597
llvm::sys::fs::mapped_file_region
This class represents a memory mapped file.
Definition: FileSystem.h:1269
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:850
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:1337