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" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/Support/Errc.h" 18 #include <system_error> 19 20 #if !defined(_MSC_VER) && !defined(__MINGW32__) 21 #include <unistd.h> 22 #else 23 #include <io.h> 24 #endif 25 26 using llvm::sys::fs::mapped_file_region; 27 28 namespace llvm { 29 FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R, 30 StringRef Path, StringRef TmpPath) 31 : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {} 32 33 FileOutputBuffer::~FileOutputBuffer() { 34 sys::fs::remove(Twine(TempPath)); 35 } 36 37 ErrorOr<std::unique_ptr<FileOutputBuffer>> 38 FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) { 39 // If file already exists, it must be a regular file (to be mappable). 40 sys::fs::file_status Stat; 41 std::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 EC = sys::fs::remove(FilePath); 61 if (EC) 62 return EC; 63 64 unsigned Mode = sys::fs::all_read | sys::fs::all_write; 65 // If requested, make the output file executable. 66 if (Flags & F_executable) 67 Mode |= sys::fs::all_exe; 68 69 // Create new file in same directory but with random name. 70 SmallString<128> TempFilePath; 71 int FD; 72 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, 73 TempFilePath, Mode); 74 if (EC) 75 return EC; 76 77 #ifndef LLVM_ON_WIN32 78 // On Windows, CreateFileMapping (the mmap function on Windows) 79 // automatically extends the underlying file. We don't need to 80 // extend the file beforehand. _chsize (ftruncate on Windows) is 81 // pretty slow just like it writes specified amount of bytes, 82 // so we should avoid calling that. 83 EC = sys::fs::resize_file(FD, Size); 84 if (EC) 85 return EC; 86 #endif 87 88 auto MappedFile = llvm::make_unique<mapped_file_region>( 89 FD, mapped_file_region::readwrite, Size, 0, EC); 90 int Ret = close(FD); 91 if (EC) 92 return EC; 93 if (Ret) 94 return std::error_code(errno, std::generic_category()); 95 96 std::unique_ptr<FileOutputBuffer> Buf( 97 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath)); 98 return std::move(Buf); 99 } 100 101 std::error_code FileOutputBuffer::commit() { 102 // Unmap buffer, letting OS flush dirty pages to file on disk. 103 Region.reset(); 104 105 106 // Rename file to final name. 107 return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); 108 } 109 } // namespace 110