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