FileOutputBuffer.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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/OwningPtr.h" 16#include "llvm/ADT/SmallVector.h" 17#include "llvm/Support/raw_ostream.h" 18#include "llvm/Support/system_error.h" 19 20using llvm::sys::fs::mapped_file_region; 21 22namespace llvm { 23FileOutputBuffer::FileOutputBuffer(mapped_file_region * R, 24 StringRef Path, StringRef TmpPath) 25 : Region(R) 26 , FinalPath(Path) 27 , TempPath(TmpPath) { 28} 29 30FileOutputBuffer::~FileOutputBuffer() { 31 sys::fs::remove(Twine(TempPath)); 32} 33 34error_code FileOutputBuffer::create(StringRef FilePath, 35 size_t Size, 36 std::unique_ptr<FileOutputBuffer> &Result, 37 unsigned Flags) { 38 // If file already exists, it must be a regular file (to be mappable). 39 sys::fs::file_status Stat; 40 error_code EC = sys::fs::status(FilePath, Stat); 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. 59 EC = sys::fs::remove(FilePath); 60 if (EC) 61 return EC; 62 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 68 // Create new file in same directory but with random name. 69 SmallString<128> TempFilePath; 70 int FD; 71 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, 72 TempFilePath, Mode); 73 if (EC) 74 return EC; 75 76 std::unique_ptr<mapped_file_region> MappedFile(new mapped_file_region( 77 FD, true, mapped_file_region::readwrite, Size, 0, EC)); 78 if (EC) 79 return EC; 80 81 Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath)); 82 if (Result) 83 MappedFile.release(); 84 85 return error_code::success(); 86} 87 88error_code FileOutputBuffer::create(StringRef FilePath, 89 size_t Size, 90 OwningPtr<FileOutputBuffer> &Result, 91 unsigned Flags) { 92 std::unique_ptr<FileOutputBuffer> FOB; 93 error_code ec = create(FilePath, Size, FOB, Flags); 94 Result = std::move(FOB); 95 return ec; 96} 97 98error_code FileOutputBuffer::commit(int64_t NewSmallerSize) { 99 // Unmap buffer, letting OS flush dirty pages to file on disk. 100 Region.reset(0); 101 102 // If requested, resize file as part of commit. 103 if ( NewSmallerSize != -1 ) { 104 error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); 105 if (EC) 106 return EC; 107 } 108 109 // Rename file to final name. 110 return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); 111} 112} // namespace 113