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 | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 20 | #include <system_error> |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 21 | |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 22 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 23 | #include <unistd.h> |
| 24 | #else |
| 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | using namespace llvm::sys; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 30 | |
Benjamin Kramer | 51ebcaa | 2017-11-24 14:55:41 +0000 | [diff] [blame] | 31 | namespace { |
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: |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 37 | OnDiskBuffer(StringRef Path, fs::TempFile Temp, |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 38 | std::unique_ptr<fs::mapped_file_region> Buf) |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 39 | : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {} |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 40 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 41 | 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 Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 47 | size_t getBufferSize() const override { return Buffer->size(); } |
| 48 | |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 49 | Error commit() override { |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 50 | // 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 Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 54 | return Temp.keep(FinalPath); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | ~OnDiskBuffer() override { |
| 58 | // Close the mapping before deleting the temp file, so that the removal |
| 59 | // succeeds. |
| 60 | Buffer.reset(); |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 61 | consumeError(Temp.discard()); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::unique_ptr<fs::mapped_file_region> Buffer; |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 66 | fs::TempFile Temp; |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 67 | }; |
| 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. |
| 71 | class InMemoryBuffer : public FileOutputBuffer { |
| 72 | public: |
| 73 | InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode) |
| 74 | : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {} |
| 75 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 76 | 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 Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 84 | Error commit() override { |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 85 | int FD; |
| 86 | std::error_code EC; |
| 87 | if (auto EC = openFileForWrite(FinalPath, FD, fs::F_None, Mode)) |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 88 | return errorCodeToError(EC); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 89 | raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true); |
| 90 | OS << StringRef((const char *)Buffer.base(), Buffer.size()); |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 91 | return Error::success(); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
| 95 | OwningMemoryBlock Buffer; |
| 96 | unsigned Mode; |
| 97 | }; |
Benjamin Kramer | 51ebcaa | 2017-11-24 14:55:41 +0000 | [diff] [blame] | 98 | } // namespace |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 99 | |
Rui Ueyama | f6490e0 | 2017-11-08 22:57:48 +0000 | [diff] [blame] | 100 | static Expected<std::unique_ptr<InMemoryBuffer>> |
| 101 | createInMemoryBuffer(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 | |
| 110 | static Expected<std::unique_ptr<OnDiskBuffer>> |
| 111 | createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) { |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 112 | 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 Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 117 | |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 118 | #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 Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 123 | // so we should avoid calling that function. |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 124 | if (auto EC = fs::resize_file(File.FD, Size)) { |
| 125 | consumeError(File.discard()); |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 126 | return errorCodeToError(EC); |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 127 | } |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 128 | #endif |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 129 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 130 | // Mmap it. |
| 131 | std::error_code EC; |
| 132 | auto MappedFile = llvm::make_unique<fs::mapped_file_region>( |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 133 | File.FD, fs::mapped_file_region::readwrite, Size, 0, EC); |
| 134 | if (EC) { |
| 135 | consumeError(File.discard()); |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 136 | return errorCodeToError(EC); |
Rafael Espindola | 58fe67a | 2017-11-13 18:33:44 +0000 | [diff] [blame] | 137 | } |
| 138 | return llvm::make_unique<OnDiskBuffer>(Path, std::move(File), |
| 139 | std::move(MappedFile)); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 140 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 141 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 142 | // Create an instance of FileOutputBuffer. |
Rafael Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 143 | Expected<std::unique_ptr<FileOutputBuffer>> |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 144 | FileOutputBuffer::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. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 148 | |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 149 | 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 Espindola | e0df357 | 2017-11-08 01:05:44 +0000 | [diff] [blame] | 162 | return errorCodeToError(errc::is_a_directory); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 163 | case fs::file_type::regular_file: |
| 164 | case fs::file_type::file_not_found: |
| 165 | case fs::file_type::status_error: |
Rui Ueyama | f6490e0 | 2017-11-08 22:57:48 +0000 | [diff] [blame] | 166 | return createOnDiskBuffer(Path, Size, Mode); |
Rui Ueyama | a16fe65 | 2017-11-01 21:38:14 +0000 | [diff] [blame] | 167 | default: |
Rui Ueyama | f6490e0 | 2017-11-08 22:57:48 +0000 | [diff] [blame] | 168 | return createInMemoryBuffer(Path, Size, Mode); |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame] | 169 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 170 | } |