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/Host/StreamFile.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Utility/LLDBLog.h"
12 #include "lldb/Utility/Log.h"
13 
14 #include <cstdio>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
StreamFile(uint32_t flags,uint32_t addr_size,ByteOrder byte_order)19 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
20     : Stream(flags, addr_size, byte_order) {
21   m_file_sp = std::make_shared<File>();
22 }
23 
StreamFile(int fd,bool transfer_ownership)24 StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() {
25   m_file_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly,
26                                            transfer_ownership);
27 }
28 
StreamFile(FILE * fh,bool transfer_ownership)29 StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() {
30   m_file_sp = std::make_shared<NativeFile>(fh, transfer_ownership);
31 }
32 
StreamFile(const char * path,File::OpenOptions options,uint32_t permissions)33 StreamFile::StreamFile(const char *path, File::OpenOptions options,
34                        uint32_t permissions)
35     : Stream() {
36   auto file = FileSystem::Instance().Open(FileSpec(path), options, permissions);
37   if (file)
38     m_file_sp = std::move(file.get());
39   else {
40     // TODO refactor this so the error gets popagated up instead of logged here.
41     LLDB_LOG_ERROR(GetLog(LLDBLog::Host), file.takeError(),
42                    "Cannot open {1}: {0}", path);
43     m_file_sp = std::make_shared<File>();
44   }
45 }
46 
47 StreamFile::~StreamFile() = default;
48 
Flush()49 void StreamFile::Flush() { m_file_sp->Flush(); }
50 
WriteImpl(const void * s,size_t length)51 size_t StreamFile::WriteImpl(const void *s, size_t length) {
52   m_file_sp->Write(s, length);
53   return length;
54 }
55