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