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" |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/Path.h" |
Rafael Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Signals.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 | |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 28 | using llvm::sys::fs::mapped_file_region; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 31 | FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R, |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 32 | StringRef Path, StringRef TmpPath, |
| 33 | bool IsRegular) |
| 34 | : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath), |
| 35 | IsRegular(IsRegular) {} |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 36 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 37 | FileOutputBuffer::~FileOutputBuffer() { |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 38 | // Close the mapping before deleting the temp file, so that the removal |
| 39 | // succeeds. |
| 40 | Region.reset(); |
Rafael Espindola | 81e7fd0 | 2014-01-10 21:40:29 +0000 | [diff] [blame] | 41 | sys::fs::remove(Twine(TempPath)); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 44 | ErrorOr<std::unique_ptr<FileOutputBuffer>> |
| 45 | FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) { |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 46 | // Check file is not a regular file, in which case we cannot remove it. |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 47 | sys::fs::file_status Stat; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 48 | std::error_code EC = sys::fs::status(FilePath, Stat); |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 49 | bool IsRegular = true; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 50 | switch (Stat.type()) { |
| 51 | case sys::fs::file_type::file_not_found: |
| 52 | // If file does not exist, we'll create one. |
| 53 | break; |
| 54 | case sys::fs::file_type::regular_file: { |
| 55 | // If file is not currently writable, error out. |
| 56 | // FIXME: There is no sys::fs:: api for checking this. |
| 57 | // FIXME: In posix, you use the access() call to check this. |
| 58 | } |
| 59 | break; |
| 60 | default: |
| 61 | if (EC) |
| 62 | return EC; |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 63 | IsRegular = false; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 66 | if (IsRegular) { |
| 67 | // Delete target file. |
| 68 | EC = sys::fs::remove(FilePath); |
| 69 | if (EC) |
| 70 | return EC; |
| 71 | } |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 72 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 73 | SmallString<128> TempFilePath; |
| 74 | int FD; |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 75 | if (IsRegular) { |
| 76 | unsigned Mode = sys::fs::all_read | sys::fs::all_write; |
| 77 | // If requested, make the output file executable. |
| 78 | if (Flags & F_executable) |
| 79 | Mode |= sys::fs::all_exe; |
| 80 | // Create new file in same directory but with random name. |
| 81 | EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, |
| 82 | TempFilePath, Mode); |
| 83 | } else { |
| 84 | // Create a temporary file. Since this is a special file, we will not move |
| 85 | // it and the new file can be in another filesystem. This avoids trying to |
| 86 | // create a temporary file in /dev when outputting to /dev/null for example. |
| 87 | EC = sys::fs::createTemporaryFile(sys::path::filename(FilePath), "", FD, |
| 88 | TempFilePath); |
| 89 | } |
| 90 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 91 | if (EC) |
| 92 | return EC; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 94 | sys::RemoveFileOnSignal(TempFilePath); |
| 95 | |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 96 | #ifndef LLVM_ON_WIN32 |
| 97 | // On Windows, CreateFileMapping (the mmap function on Windows) |
| 98 | // automatically extends the underlying file. We don't need to |
| 99 | // extend the file beforehand. _chsize (ftruncate on Windows) is |
| 100 | // pretty slow just like it writes specified amount of bytes, |
| 101 | // so we should avoid calling that. |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 102 | EC = sys::fs::resize_file(FD, Size); |
| 103 | if (EC) |
| 104 | return EC; |
Rui Ueyama | da9bc2e | 2015-03-06 06:07:32 +0000 | [diff] [blame] | 105 | #endif |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 106 | |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 107 | auto MappedFile = llvm::make_unique<mapped_file_region>( |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 108 | FD, mapped_file_region::readwrite, Size, 0, EC); |
| 109 | int Ret = close(FD); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 110 | if (EC) |
| 111 | return EC; |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 112 | if (Ret) |
| 113 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 115 | std::unique_ptr<FileOutputBuffer> Buf(new FileOutputBuffer( |
| 116 | std::move(MappedFile), FilePath, TempFilePath, IsRegular)); |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 117 | return std::move(Buf); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 118 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 119 | |
Rafael Espindola | 5753cf3 | 2014-12-12 17:35:34 +0000 | [diff] [blame] | 120 | std::error_code FileOutputBuffer::commit() { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 121 | // Unmap buffer, letting OS flush dirty pages to file on disk. |
David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 122 | Region.reset(); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 123 | |
Rafael Espindola | d4b24ed | 2017-01-09 21:52:35 +0000 | [diff] [blame^] | 124 | std::error_code EC; |
| 125 | if (IsRegular) { |
| 126 | // Rename file to final name. |
| 127 | EC = sys::fs::rename(Twine(TempPath), Twine(FinalPath)); |
| 128 | sys::DontRemoveFileOnSignal(TempPath); |
| 129 | } else { |
| 130 | EC = sys::fs::copy_file(TempPath, FinalPath); |
| 131 | std::error_code RMEC = sys::fs::remove(TempPath); |
| 132 | sys::DontRemoveFileOnSignal(TempPath); |
| 133 | if (RMEC) |
| 134 | return RMEC; |
| 135 | } |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 136 | |
Rafael Espindola | 7dbb577 | 2015-09-18 15:17:53 +0000 | [diff] [blame] | 137 | return EC; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 138 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 139 | } // namespace |