LLVM 19.0.0git
Go to the documentation of this file.
1//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
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
9// Utility for creating a in-memory buffer that will be written to a file.
14#include "llvm/Support/Errc.h"
16#include "llvm/Support/Memory.h"
18#include <system_error>
20#if !defined(_MSC_VER) && !defined(__MINGW32__)
21#include <unistd.h>
23#include <io.h>
26using namespace llvm;
27using namespace llvm::sys;
29namespace {
30// A FileOutputBuffer which creates a temporary file in the same directory
31// as the final output file. The final output file is atomically replaced
32// with the temporary file on commit().
33class OnDiskBuffer : public FileOutputBuffer {
35 OnDiskBuffer(StringRef Path, fs::TempFile Temp, fs::mapped_file_region Buf)
36 : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
38 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.data(); }
40 uint8_t *getBufferEnd() const override {
41 return (uint8_t *)Buffer.data() + Buffer.size();
42 }
44 size_t getBufferSize() const override { return Buffer.size(); }
46 Error commit() override {
47 llvm::TimeTraceScope timeScope("Commit buffer to disk");
49 // Unmap buffer, letting OS flush dirty pages to file on disk.
50 Buffer.unmap();
52 // Atomically replace the existing file with the new one.
53 return Temp.keep(FinalPath);
54 }
56 ~OnDiskBuffer() override {
57 // Close the mapping before deleting the temp file, so that the removal
58 // succeeds.
59 Buffer.unmap();
60 consumeError(Temp.discard());
61 }
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 }
71 fs::TempFile Temp;
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.
76class InMemoryBuffer : public FileOutputBuffer {
78 InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
79 unsigned Mode)
80 : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
81 Mode(Mode) {}
83 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
85 uint8_t *getBufferEnd() const override {
86 return (uint8_t *)Buffer.base() + BufferSize;
87 }
89 size_t getBufferSize() const override { return BufferSize; }
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 }
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 }
110 // Buffer may actually contain a larger memory block than BufferSize
111 OwningMemoryBlock Buffer;
112 size_t BufferSize;
113 unsigned Mode;
115} // namespace
118createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
119 std::error_code EC;
122 if (EC)
123 return errorCodeToError(EC);
124 return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
128createOnDiskBuffer(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);
135 if (auto EC = fs::resize_file_before_mapping_readwrite(File.FD, Size)) {
136 consumeError(File.discard());
137 return errorCodeToError(EC);
138 }
140 // Mmap it.
141 std::error_code EC;
142 fs::mapped_file_region MappedFile =
146 // mmap(2) can fail if the underlying filesystem does not support it.
147 // If that happens, we fall back to in-memory buffer as the last resort.
148 if (EC) {
149 consumeError(File.discard());
150 return createInMemoryBuffer(Path, Size, Mode);
151 }
153 return std::make_unique<OnDiskBuffer>(Path, std::move(File),
154 std::move(MappedFile));
157// Create an instance of FileOutputBuffer.
159FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
160 // Handle "-" as stdout just like llvm::raw_ostream does.
161 if (Path == "-")
162 return createInMemoryBuffer("-", Size, /*Mode=*/0);
164 unsigned Mode = fs::all_read | fs::all_write;
165 if (Flags & F_executable)
166 Mode |= fs::all_exe;
168 // If Size is zero, don't use mmap which will fail with EINVAL.
169 if (Size == 0)
170 return createInMemoryBuffer(Path, Size, Mode);
172 fs::file_status Stat;
173 fs::status(Path, Stat);
175 // Usually, we want to create OnDiskBuffer to create a temporary file in
176 // the same directory as the destination file and atomically replaces it
177 // by rename(2).
178 //
179 // However, if the destination file is a special file, we don't want to
180 // use rename (e.g. we don't want to replace /dev/null with a regular
181 // file.) If that's the case, we create an in-memory buffer, open the
182 // destination file and write to it on commit().
183 switch (Stat.type()) {
184 case fs::file_type::directory_file:
186 case fs::file_type::regular_file:
187 case fs::file_type::file_not_found:
188 case fs::file_type::status_error:
189 if (Flags & F_no_mmap)
190 return createInMemoryBuffer(Path, Size, Mode);
191 else
192 return createOnDiskBuffer(Path, Size, Mode);
193 default:
194 return createInMemoryBuffer(Path, Size, Mode);
195 }
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)
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
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
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Definition: TimeProfiler.h:147
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:471
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:848
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:1326
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
This class represents a memory mapped file.
Definition: FileSystem.h:1267
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1271
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:416
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:1063
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:991
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:1849
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858