blob: c4ff563e5f44dc69d6dab9f6ee5cc2a77551f1c4 [file] [log] [blame]
Nick Kledzik5fce8c42012-08-01 02:29:50 +00001//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Utility for creating a in-memory buffer that will be written to a file.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/Support/FileOutputBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/Support/Errc.h"
Rui Ueyamaa16fe652017-11-01 21:38:14 +000018#include "llvm/Support/Memory.h"
Rafael Espindolad4b24ed2017-01-09 21:52:35 +000019#include "llvm/Support/Path.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000020#include <system_error>
Nick Kledzik5fce8c42012-08-01 02:29:50 +000021
Rafael Espindola7eb1f182014-12-11 20:12:55 +000022#if !defined(_MSC_VER) && !defined(__MINGW32__)
23#include <unistd.h>
24#else
25#include <io.h>
26#endif
27
Rui Ueyamaa16fe652017-11-01 21:38:14 +000028using namespace llvm;
29using namespace llvm::sys;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000030
Benjamin Kramer51ebcaa2017-11-24 14:55:41 +000031namespace {
Rui Ueyamaa16fe652017-11-01 21:38:14 +000032// 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().
35class OnDiskBuffer : public FileOutputBuffer {
36public:
Rafael Espindola58fe67a2017-11-13 18:33:44 +000037 OnDiskBuffer(StringRef Path, fs::TempFile Temp,
Rui Ueyamaa16fe652017-11-01 21:38:14 +000038 std::unique_ptr<fs::mapped_file_region> Buf)
Rafael Espindola58fe67a2017-11-13 18:33:44 +000039 : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000040
Rui Ueyamaa16fe652017-11-01 21:38:14 +000041 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
42
43 uint8_t *getBufferEnd() const override {
44 return (uint8_t *)Buffer->data() + Buffer->size();
Nick Kledzik5fce8c42012-08-01 02:29:50 +000045 }
46
Rui Ueyamaa16fe652017-11-01 21:38:14 +000047 size_t getBufferSize() const override { return Buffer->size(); }
48
Rafael Espindola0d7a38a2017-11-08 01:50:29 +000049 Error commit() override {
Rui Ueyamaa16fe652017-11-01 21:38:14 +000050 // Unmap buffer, letting OS flush dirty pages to file on disk.
51 Buffer.reset();
52
53 // Atomically replace the existing file with the new one.
Rafael Espindola58fe67a2017-11-13 18:33:44 +000054 return Temp.keep(FinalPath);
Rui Ueyamaa16fe652017-11-01 21:38:14 +000055 }
56
57 ~OnDiskBuffer() override {
58 // Close the mapping before deleting the temp file, so that the removal
59 // succeeds.
60 Buffer.reset();
Rafael Espindola58fe67a2017-11-13 18:33:44 +000061 consumeError(Temp.discard());
Rui Ueyamaa16fe652017-11-01 21:38:14 +000062 }
63
64private:
65 std::unique_ptr<fs::mapped_file_region> Buffer;
Rafael Espindola58fe67a2017-11-13 18:33:44 +000066 fs::TempFile Temp;
Rui Ueyamaa16fe652017-11-01 21:38:14 +000067};
68
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.
71class InMemoryBuffer : public FileOutputBuffer {
72public:
73 InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
74 : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
75
Rui Ueyamaa16fe652017-11-01 21:38:14 +000076 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
77
78 uint8_t *getBufferEnd() const override {
79 return (uint8_t *)Buffer.base() + Buffer.size();
80 }
81
82 size_t getBufferSize() const override { return Buffer.size(); }
83
Rafael Espindola0d7a38a2017-11-08 01:50:29 +000084 Error commit() override {
Rui Ueyamaa16fe652017-11-01 21:38:14 +000085 int FD;
86 std::error_code EC;
87 if (auto EC = openFileForWrite(FinalPath, FD, fs::F_None, Mode))
Rafael Espindola0d7a38a2017-11-08 01:50:29 +000088 return errorCodeToError(EC);
Rui Ueyamaa16fe652017-11-01 21:38:14 +000089 raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
90 OS << StringRef((const char *)Buffer.base(), Buffer.size());
Rafael Espindola0d7a38a2017-11-08 01:50:29 +000091 return Error::success();
Rui Ueyamaa16fe652017-11-01 21:38:14 +000092 }
93
94private:
95 OwningMemoryBlock Buffer;
96 unsigned Mode;
97};
Benjamin Kramer51ebcaa2017-11-24 14:55:41 +000098} // namespace
Rui Ueyamaa16fe652017-11-01 21:38:14 +000099
Rui Ueyamaf6490e02017-11-08 22:57:48 +0000100static Expected<std::unique_ptr<InMemoryBuffer>>
101createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
102 std::error_code EC;
103 MemoryBlock MB = Memory::allocateMappedMemory(
104 Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
105 if (EC)
106 return errorCodeToError(EC);
107 return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
108}
109
110static Expected<std::unique_ptr<OnDiskBuffer>>
111createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
Rafael Espindola58fe67a2017-11-13 18:33:44 +0000112 Expected<fs::TempFile> FileOrErr =
113 fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
114 if (!FileOrErr)
115 return FileOrErr.takeError();
116 fs::TempFile File = std::move(*FileOrErr);
Rafael Espindola7dbb5772015-09-18 15:17:53 +0000117
Rui Ueyamada9bc2e2015-03-06 06:07:32 +0000118#ifndef LLVM_ON_WIN32
119 // On Windows, CreateFileMapping (the mmap function on Windows)
120 // automatically extends the underlying file. We don't need to
121 // extend the file beforehand. _chsize (ftruncate on Windows) is
122 // pretty slow just like it writes specified amount of bytes,
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000123 // so we should avoid calling that function.
Rafael Espindola58fe67a2017-11-13 18:33:44 +0000124 if (auto EC = fs::resize_file(File.FD, Size)) {
125 consumeError(File.discard());
Rafael Espindolae0df3572017-11-08 01:05:44 +0000126 return errorCodeToError(EC);
Rafael Espindola58fe67a2017-11-13 18:33:44 +0000127 }
Rui Ueyamada9bc2e2015-03-06 06:07:32 +0000128#endif
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000129
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000130 // Mmap it.
131 std::error_code EC;
132 auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
Rafael Espindola58fe67a2017-11-13 18:33:44 +0000133 File.FD, fs::mapped_file_region::readwrite, Size, 0, EC);
134 if (EC) {
135 consumeError(File.discard());
Rafael Espindolae0df3572017-11-08 01:05:44 +0000136 return errorCodeToError(EC);
Rafael Espindola58fe67a2017-11-13 18:33:44 +0000137 }
138 return llvm::make_unique<OnDiskBuffer>(Path, std::move(File),
139 std::move(MappedFile));
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000140}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000141
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000142// Create an instance of FileOutputBuffer.
Rafael Espindolae0df3572017-11-08 01:05:44 +0000143Expected<std::unique_ptr<FileOutputBuffer>>
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000144FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
145 unsigned Mode = fs::all_read | fs::all_write;
146 if (Flags & F_executable)
147 Mode |= fs::all_exe;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000148
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000149 fs::file_status Stat;
150 fs::status(Path, Stat);
151
152 // Usually, we want to create OnDiskBuffer to create a temporary file in
153 // the same directory as the destination file and atomically replaces it
154 // by rename(2).
155 //
156 // However, if the destination file is a special file, we don't want to
157 // use rename (e.g. we don't want to replace /dev/null with a regular
158 // file.) If that's the case, we create an in-memory buffer, open the
159 // destination file and write to it on commit().
160 switch (Stat.type()) {
161 case fs::file_type::directory_file:
Rafael Espindolae0df3572017-11-08 01:05:44 +0000162 return errorCodeToError(errc::is_a_directory);
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000163 case fs::file_type::regular_file:
164 case fs::file_type::file_not_found:
165 case fs::file_type::status_error:
Rui Ueyamaf6490e02017-11-08 22:57:48 +0000166 return createOnDiskBuffer(Path, Size, Mode);
Rui Ueyamaa16fe652017-11-01 21:38:14 +0000167 default:
Rui Ueyamaf6490e02017-11-08 22:57:48 +0000168 return createInMemoryBuffer(Path, Size, Mode);
Rafael Espindolad4b24ed2017-01-09 21:52:35 +0000169 }
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000170}