1 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Utility for creating a in-memory buffer that will be written to a file.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/FileOutputBuffer.h"
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>
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
29 using namespace llvm::sys;
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 {
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)) {}
41 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
43 uint8_t *getBufferEnd() const override {
44 return (uint8_t *)Buffer->data() + Buffer->size();
47 size_t getBufferSize() const override { return Buffer->size(); }
49 Error commit() override {
50 // Unmap buffer, letting OS flush dirty pages to file on disk.
53 // Atomically replace the existing file with the new one.
54 return Temp.keep(FinalPath);
57 ~OnDiskBuffer() override {
58 // Close the mapping before deleting the temp file, so that the removal
61 consumeError(Temp.discard());
65 std::unique_ptr<fs::mapped_file_region> Buffer;
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 {
73 InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
74 : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
76 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
78 uint8_t *getBufferEnd() const override {
79 return (uint8_t *)Buffer.base() + Buffer.size();
82 size_t getBufferSize() const override { return Buffer.size(); }
84 Error commit() override {
85 using namespace sys::fs;
88 if (auto EC = openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None))
89 return errorCodeToError(EC);
90 raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
91 OS << StringRef((const char *)Buffer.base(), Buffer.size());
92 return Error::success();
96 OwningMemoryBlock Buffer;
101 static Expected<std::unique_ptr<InMemoryBuffer>>
102 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
104 MemoryBlock MB = Memory::allocateMappedMemory(
105 Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
107 return errorCodeToError(EC);
108 return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
111 static Expected<std::unique_ptr<OnDiskBuffer>>
112 createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
113 Expected<fs::TempFile> FileOrErr =
114 fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
116 return FileOrErr.takeError();
117 fs::TempFile File = std::move(*FileOrErr);
120 // On Windows, CreateFileMapping (the mmap function on Windows)
121 // automatically extends the underlying file. We don't need to
122 // extend the file beforehand. _chsize (ftruncate on Windows) is
123 // pretty slow just like it writes specified amount of bytes,
124 // so we should avoid calling that function.
125 if (auto EC = fs::resize_file(File.FD, Size)) {
126 consumeError(File.discard());
127 return errorCodeToError(EC);
133 auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
134 File.FD, fs::mapped_file_region::readwrite, Size, 0, EC);
136 consumeError(File.discard());
137 return errorCodeToError(EC);
139 return llvm::make_unique<OnDiskBuffer>(Path, std::move(File),
140 std::move(MappedFile));
143 // Create an instance of FileOutputBuffer.
144 Expected<std::unique_ptr<FileOutputBuffer>>
145 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
146 unsigned Mode = fs::all_read | fs::all_write;
147 if (Flags & F_executable)
150 fs::file_status Stat;
151 fs::status(Path, Stat);
153 // Usually, we want to create OnDiskBuffer to create a temporary file in
154 // the same directory as the destination file and atomically replaces it
157 // However, if the destination file is a special file, we don't want to
158 // use rename (e.g. we don't want to replace /dev/null with a regular
159 // file.) If that's the case, we create an in-memory buffer, open the
160 // destination file and write to it on commit().
161 switch (Stat.type()) {
162 case fs::file_type::directory_file:
163 return errorCodeToError(errc::is_a_directory);
164 case fs::file_type::regular_file:
165 case fs::file_type::file_not_found:
166 case fs::file_type::status_error:
167 return createOnDiskBuffer(Path, Size, Mode);
169 return createInMemoryBuffer(Path, Size, Mode);