1 //===-- StreamFile.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Core/StreamFile.h" 10 #include "lldb/Host/FileSystem.h" 11 #include "lldb/Utility/Log.h" 12 13 #include <stdio.h> 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order) 19 : Stream(flags, addr_size, byte_order) { 20 m_file_sp = std::make_shared<File>(); 21 } 22 23 StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() { 24 m_file_sp = 25 std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, transfer_ownership); 26 } 27 28 StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() { 29 m_file_sp = std::make_shared<NativeFile>(fh, transfer_ownership); 30 } 31 32 StreamFile::StreamFile(const char *path, File::OpenOptions options, 33 uint32_t permissions) 34 : Stream() { 35 auto file = FileSystem::Instance().Open(FileSpec(path), options, permissions); 36 if (file) 37 m_file_sp = std::move(file.get()); 38 else { 39 // TODO refactor this so the error gets popagated up instead of logged here. 40 LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), file.takeError(), 41 "Cannot open {1}: {0}", path); 42 m_file_sp = std::make_shared<File>(); 43 } 44 } 45 46 StreamFile::~StreamFile() {} 47 48 void StreamFile::Flush() { m_file_sp->Flush(); } 49 50 size_t StreamFile::WriteImpl(const void *s, size_t length) { 51 m_file_sp->Write(s, length); 52 return length; 53 } 54