Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FileOutputBuffer.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/Support/Errc.h" |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Memory.h" |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Rafael Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Signals.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 21 | #include <system_error> |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 23 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 24 | #include <unistd.h> |
| 25 | #else |
| 26 | #include <io.h> |
| 27 | #endif |
| 28 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | using namespace llvm::sys; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 31 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 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 { |
| 36 | public: |
| 37 | OnDiskBuffer(StringRef Path, StringRef TempPath, |
| 38 | std::unique_ptr<fs::mapped_file_region> Buf) |
| 39 | : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {} |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 40 | |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 41 | static Expected<std::unique_ptr<OnDiskBuffer>> |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 42 | create(StringRef Path, size_t Size, unsigned Mode); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 43 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 44 | uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); } |
| 45 | |
| 46 | uint8_t *getBufferEnd() const override { |
| 47 | return (uint8_t *)Buffer->data() + Buffer->size(); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 50 | size_t getBufferSize() const override { return Buffer->size(); } |
| 51 | |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame^] | 52 | Error commit() override { |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 53 | // Unmap buffer, letting OS flush dirty pages to file on disk. |
| 54 | Buffer.reset(); |
| 55 | |
| 56 | // Atomically replace the existing file with the new one. |
| 57 | auto EC = fs::rename(TempPath, FinalPath); |
| 58 | sys::DontRemoveFileOnSignal(TempPath); |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame^] | 59 | return errorCodeToError(EC); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ~OnDiskBuffer() override { |
| 63 | // Close the mapping before deleting the temp file, so that the removal |
| 64 | // succeeds. |
| 65 | Buffer.reset(); |
| 66 | fs::remove(TempPath); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | std::unique_ptr<fs::mapped_file_region> Buffer; |
| 71 | std::string TempPath; |
| 72 | }; |
| 73 | |
| 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. |
| 76 | class InMemoryBuffer : public FileOutputBuffer { |
| 77 | public: |
| 78 | InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode) |
| 79 | : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {} |
| 80 | |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 81 | static Expected<std::unique_ptr<InMemoryBuffer>> |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 82 | create(StringRef Path, size_t Size, unsigned Mode) { |
| 83 | std::error_code EC; |
| 84 | MemoryBlock MB = Memory::allocateMappedMemory( |
| 85 | Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC); |
| 86 | if (EC) |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 87 | return errorCodeToError(EC); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 88 | return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode); |
| 89 | } |
| 90 | |
| 91 | uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); } |
| 92 | |
| 93 | uint8_t *getBufferEnd() const override { |
| 94 | return (uint8_t *)Buffer.base() + Buffer.size(); |
| 95 | } |
| 96 | |
| 97 | size_t getBufferSize() const override { return Buffer.size(); } |
| 98 | |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame^] | 99 | Error commit() override { |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 100 | int FD; |
| 101 | std::error_code EC; |
| 102 | if (auto EC = openFileForWrite(FinalPath, FD, fs::F_None, Mode)) |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame^] | 103 | return errorCodeToError(EC); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 104 | raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true); |
| 105 | OS << StringRef((const char *)Buffer.base(), Buffer.size()); |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame^] | 106 | return Error::success(); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | OwningMemoryBlock Buffer; |
| 111 | unsigned Mode; |
| 112 | }; |
| 113 | |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 114 | Expected<std::unique_ptr<OnDiskBuffer>> |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 115 | OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) { |
| 116 | // Create new file in same directory but with random name. |
| 117 | SmallString<128> TempPath; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 118 | int FD; |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 119 | if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode)) |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 120 | return errorCodeToError(EC); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 121 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 122 | sys::RemoveFileOnSignal(TempPath); |
Rafael Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 123 | |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 124 | #ifndef LLVM_ON_WIN32 |
| 125 | // On Windows, CreateFileMapping (the mmap function on Windows) |
| 126 | // automatically extends the underlying file. We don't need to |
| 127 | // extend the file beforehand. _chsize (ftruncate on Windows) is |
| 128 | // pretty slow just like it writes specified amount of bytes, |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 129 | // so we should avoid calling that function. |
| 130 | if (auto EC = fs::resize_file(FD, Size)) |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 131 | return errorCodeToError(EC); |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 132 | #endif |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 133 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 134 | // Mmap it. |
| 135 | std::error_code EC; |
| 136 | auto MappedFile = llvm::make_unique<fs::mapped_file_region>( |
| 137 | FD, fs::mapped_file_region::readwrite, Size, 0, EC); |
| 138 | close(FD); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 139 | if (EC) |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 140 | return errorCodeToError(EC); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 141 | return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile)); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 142 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 143 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 144 | // Create an instance of FileOutputBuffer. |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 145 | Expected<std::unique_ptr<FileOutputBuffer>> |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 146 | FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) { |
| 147 | unsigned Mode = fs::all_read | fs::all_write; |
| 148 | if (Flags & F_executable) |
| 149 | Mode |= fs::all_exe; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 151 | fs::file_status Stat; |
| 152 | fs::status(Path, Stat); |
| 153 | |
| 154 | // Usually, we want to create OnDiskBuffer to create a temporary file in |
| 155 | // the same directory as the destination file and atomically replaces it |
| 156 | // by rename(2). |
| 157 | // |
| 158 | // However, if the destination file is a special file, we don't want to |
| 159 | // use rename (e.g. we don't want to replace /dev/null with a regular |
| 160 | // file.) If that's the case, we create an in-memory buffer, open the |
| 161 | // destination file and write to it on commit(). |
| 162 | switch (Stat.type()) { |
| 163 | case fs::file_type::directory_file: |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 164 | return errorCodeToError(errc::is_a_directory); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 165 | case fs::file_type::regular_file: |
| 166 | case fs::file_type::file_not_found: |
| 167 | case fs::file_type::status_error: |
| 168 | return OnDiskBuffer::create(Path, Size, Mode); |
| 169 | default: |
| 170 | return InMemoryBuffer::create(Path, Size, Mode); |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame] | 171 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 172 | } |