LLVM 17.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
25using namespace llvm;
26using namespace llvm::sys;
27
28namespace {
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().
32class OnDiskBuffer : public FileOutputBuffer {
33public:
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
66private:
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.
73class InMemoryBuffer : public FileOutputBuffer {
74public:
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
106private:
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
115createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
116 std::error_code EC;
119 if (EC)
120 return errorCodeToError(EC);
121 return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
122}
123
125createOnDiskBuffer(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 =
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.
156FileOutputBuffer::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()) {
181 case fs::file_type::directory_file:
183 case fs::file_type::regular_file:
184 case fs::file_type::file_not_found:
185 case fs::file_type::status_error:
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}
uint64_t Size
static Expected< std::unique_ptr< FileOutputBuffer > > createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode)
static Expected< std::unique_ptr< InMemoryBuffer > > createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
virtual uint8_t * getBufferStart() const =0
Returns a pointer to the start of the buffer.
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...
virtual uint8_t * getBufferEnd() const =0
Returns a pointer to the end of the buffer.
virtual Error commit()=0
Flushes the content of the buffer to its file and deallocates the buffer.
virtual size_t getBufferSize() const =0
Returns size of the buffer.
virtual void discard()
This removes the temporary file (unless it already was committed) but keeps the memory mapping alive.
@ F_no_mmap
Don't use mmap and instead write an in-memory buffer to a file when this buffer is closed.
@ F_executable
Set the 'x' bit on the resulting file.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
This class encapsulates the notion of a memory block which has an address and a size.
Definition: Memory.h:31
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....
Owning version of MemoryBlock.
Definition: Memory.h:137
Represents a temporary file.
Definition: FileSystem.h:850
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
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
This class represents a memory mapped file.
Definition: FileSystem.h:1269
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1273
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
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
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
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:993
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
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:1862
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
Definition: BitVector.h:851