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 | |
| 14 | #include "llvm/Support/FileOutputBuffer.h" |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/OwningPtr.h" |
| 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" |
| 18 | #include "llvm/Support/system_error.h" |
| 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() { |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 31 | bool Existed; |
| 32 | sys::fs::remove(Twine(TempPath), Existed); |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 35 | error_code FileOutputBuffer::create(StringRef FilePath, |
| 36 | size_t Size, |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 37 | OwningPtr<FileOutputBuffer> &Result, |
| 38 | unsigned Flags) { |
| 39 | // If file already exists, it must be a regular file (to be mappable). |
| 40 | sys::fs::file_status Stat; |
| 41 | error_code EC = sys::fs::status(FilePath, Stat); |
| 42 | switch (Stat.type()) { |
| 43 | case sys::fs::file_type::file_not_found: |
| 44 | // If file does not exist, we'll create one. |
| 45 | break; |
| 46 | case sys::fs::file_type::regular_file: { |
| 47 | // If file is not currently writable, error out. |
| 48 | // FIXME: There is no sys::fs:: api for checking this. |
| 49 | // FIXME: In posix, you use the access() call to check this. |
| 50 | } |
| 51 | break; |
| 52 | default: |
| 53 | if (EC) |
| 54 | return EC; |
| 55 | else |
| 56 | return make_error_code(errc::operation_not_permitted); |
| 57 | } |
| 58 | |
| 59 | // Delete target file. |
| 60 | bool Existed; |
| 61 | EC = sys::fs::remove(FilePath, Existed); |
| 62 | if (EC) |
| 63 | return EC; |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 64 | |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 65 | // Create new file in same directory but with random name. |
| 66 | SmallString<128> TempFilePath; |
| 67 | int FD; |
Rafael Espindola | 200c748 | 2013-07-05 21:01:08 +0000 | [diff] [blame^] | 68 | EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", |
| 69 | FD, TempFilePath); |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 70 | if (EC) |
| 71 | return EC; |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 72 | |
Michael J. Spencer | cc3a595 | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 73 | OwningPtr<mapped_file_region> MappedFile(new mapped_file_region( |
| 74 | FD, true, mapped_file_region::readwrite, Size, 0, EC)); |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 75 | if (EC) |
| 76 | return EC; |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 77 | |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 78 | // If requested, make the output file executable. |
| 79 | if ( Flags & F_executable ) { |
| 80 | sys::fs::file_status Stat2; |
| 81 | EC = sys::fs::status(Twine(TempFilePath), Stat2); |
| 82 | if (EC) |
| 83 | return EC; |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 84 | |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 85 | sys::fs::perms new_perms = Stat2.permissions(); |
| 86 | if ( new_perms & sys::fs::owner_read ) |
| 87 | new_perms |= sys::fs::owner_exe; |
| 88 | if ( new_perms & sys::fs::group_read ) |
| 89 | new_perms |= sys::fs::group_exe; |
| 90 | if ( new_perms & sys::fs::others_read ) |
| 91 | new_perms |= sys::fs::others_exe; |
| 92 | new_perms |= sys::fs::add_perms; |
| 93 | EC = sys::fs::permissions(Twine(TempFilePath), new_perms); |
| 94 | if (EC) |
| 95 | return EC; |
| 96 | } |
| 97 | |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 98 | Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath)); |
| 99 | if (Result) |
| 100 | MappedFile.take(); |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 101 | |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 102 | return error_code::success(); |
| 103 | } |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 104 | |
| 105 | error_code FileOutputBuffer::commit(int64_t NewSmallerSize) { |
| 106 | // Unmap buffer, letting OS flush dirty pages to file on disk. |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 107 | Region.reset(0); |
| 108 | |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 109 | // If requested, resize file as part of commit. |
| 110 | if ( NewSmallerSize != -1 ) { |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 111 | error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 112 | if (EC) |
| 113 | return EC; |
| 114 | } |
Michael J. Spencer | 6bc8601 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 115 | |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 116 | // Rename file to final name. |
| 117 | return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); |
| 118 | } |
Nick Kledzik | adfe263 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 119 | } // namespace |