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 | |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Errc.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileOutputBuffer.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 19 | #include <system_error> |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 20 | |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 21 | using llvm::sys::fs::mapped_file_region; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 24 | FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R, |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 25 | StringRef Path, StringRef TmpPath) |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 26 | : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {} |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 27 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 28 | FileOutputBuffer::~FileOutputBuffer() { |
Rafael Espindola | 81e7fd0 | 2014-01-10 21:40:29 +0000 | [diff] [blame] | 29 | sys::fs::remove(Twine(TempPath)); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 32 | std::error_code |
| 33 | FileOutputBuffer::create(StringRef FilePath, size_t Size, |
| 34 | std::unique_ptr<FileOutputBuffer> &Result, |
| 35 | unsigned Flags) { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 36 | // If file already exists, it must be a regular file (to be mappable). |
| 37 | sys::fs::file_status Stat; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 38 | std::error_code EC = sys::fs::status(FilePath, Stat); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 39 | switch (Stat.type()) { |
| 40 | case sys::fs::file_type::file_not_found: |
| 41 | // If file does not exist, we'll create one. |
| 42 | break; |
| 43 | case sys::fs::file_type::regular_file: { |
| 44 | // If file is not currently writable, error out. |
| 45 | // FIXME: There is no sys::fs:: api for checking this. |
| 46 | // FIXME: In posix, you use the access() call to check this. |
| 47 | } |
| 48 | break; |
| 49 | default: |
| 50 | if (EC) |
| 51 | return EC; |
| 52 | else |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 53 | return make_error_code(errc::operation_not_permitted); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Delete target file. |
Rafael Espindola | 81e7fd0 | 2014-01-10 21:40:29 +0000 | [diff] [blame] | 57 | EC = sys::fs::remove(FilePath); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 58 | if (EC) |
| 59 | return EC; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | 9a78015 | 2013-07-08 15:22:09 +0000 | [diff] [blame] | 61 | unsigned Mode = sys::fs::all_read | sys::fs::all_write; |
| 62 | // If requested, make the output file executable. |
| 63 | if (Flags & F_executable) |
| 64 | Mode |= sys::fs::all_exe; |
| 65 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 66 | // Create new file in same directory but with random name. |
| 67 | SmallString<128> TempFilePath; |
| 68 | int FD; |
Rafael Espindola | 9a78015 | 2013-07-08 15:22:09 +0000 | [diff] [blame] | 69 | EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, |
| 70 | TempFilePath, Mode); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 71 | if (EC) |
| 72 | return EC; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 73 | |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 74 | auto MappedFile = llvm::make_unique<mapped_file_region>( |
| 75 | FD, true, mapped_file_region::readwrite, Size, 0, EC); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 76 | if (EC) |
| 77 | return EC; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 78 | |
David Blaikie | f55e31a | 2014-09-02 17:49:23 +0000 | [diff] [blame] | 79 | Result.reset( |
| 80 | new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath)); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 81 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 82 | return std::error_code(); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 83 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 84 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 85 | std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 86 | // Unmap buffer, letting OS flush dirty pages to file on disk. |
David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 87 | Region.reset(); |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 88 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 89 | // If requested, resize file as part of commit. |
| 90 | if ( NewSmallerSize != -1 ) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 91 | std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 92 | if (EC) |
| 93 | return EC; |
| 94 | } |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 95 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 96 | // Rename file to final name. |
| 97 | return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); |
| 98 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 99 | } // namespace |