LLVM API Documentation
00001 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Utility for creating a in-memory buffer that will be written to a file. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Support/Errc.h" 00015 #include "llvm/Support/FileOutputBuffer.h" 00016 #include "llvm/ADT/SmallVector.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/Support/raw_ostream.h" 00019 #include <system_error> 00020 00021 using llvm::sys::fs::mapped_file_region; 00022 00023 namespace llvm { 00024 FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R, 00025 StringRef Path, StringRef TmpPath) 00026 : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {} 00027 00028 FileOutputBuffer::~FileOutputBuffer() { 00029 sys::fs::remove(Twine(TempPath)); 00030 } 00031 00032 std::error_code 00033 FileOutputBuffer::create(StringRef FilePath, size_t Size, 00034 std::unique_ptr<FileOutputBuffer> &Result, 00035 unsigned Flags) { 00036 // If file already exists, it must be a regular file (to be mappable). 00037 sys::fs::file_status Stat; 00038 std::error_code EC = sys::fs::status(FilePath, Stat); 00039 switch (Stat.type()) { 00040 case sys::fs::file_type::file_not_found: 00041 // If file does not exist, we'll create one. 00042 break; 00043 case sys::fs::file_type::regular_file: { 00044 // If file is not currently writable, error out. 00045 // FIXME: There is no sys::fs:: api for checking this. 00046 // FIXME: In posix, you use the access() call to check this. 00047 } 00048 break; 00049 default: 00050 if (EC) 00051 return EC; 00052 else 00053 return make_error_code(errc::operation_not_permitted); 00054 } 00055 00056 // Delete target file. 00057 EC = sys::fs::remove(FilePath); 00058 if (EC) 00059 return EC; 00060 00061 unsigned Mode = sys::fs::all_read | sys::fs::all_write; 00062 // If requested, make the output file executable. 00063 if (Flags & F_executable) 00064 Mode |= sys::fs::all_exe; 00065 00066 // Create new file in same directory but with random name. 00067 SmallString<128> TempFilePath; 00068 int FD; 00069 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, 00070 TempFilePath, Mode); 00071 if (EC) 00072 return EC; 00073 00074 auto MappedFile = llvm::make_unique<mapped_file_region>( 00075 FD, true, mapped_file_region::readwrite, Size, 0, EC); 00076 if (EC) 00077 return EC; 00078 00079 Result.reset( 00080 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath)); 00081 00082 return std::error_code(); 00083 } 00084 00085 std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) { 00086 // Unmap buffer, letting OS flush dirty pages to file on disk. 00087 Region.reset(); 00088 00089 // If requested, resize file as part of commit. 00090 if ( NewSmallerSize != -1 ) { 00091 std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); 00092 if (EC) 00093 return EC; 00094 } 00095 00096 // Rename file to final name. 00097 return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); 00098 } 00099 } // namespace