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
16using namespace lldb;
17using namespace lldb_private;
18
19StreamFile::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
24StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() {
25 m_file_sp = std::make_shared<NativeFile>(args&: fd, args: File::eOpenOptionWriteOnly,
26 args&: transfer_ownership);
27}
28
29StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() {
30 m_file_sp = std::make_shared<NativeFile>(args&: fh, args&: transfer_ownership);
31}
32
33StreamFile::StreamFile(const char *path, File::OpenOptions options,
34 uint32_t permissions)
35 : Stream() {
36 auto file = FileSystem::Instance().Open(file_spec: 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
47StreamFile::~StreamFile() = default;
48
49void StreamFile::Flush() { m_file_sp->Flush(); }
50
51size_t StreamFile::WriteImpl(const void *s, size_t length) {
52 m_file_sp->Write(buf: s, num_bytes&: length);
53 return length;
54}
55

source code of lldb/source/Host/common/StreamFile.cpp