1 //===-- SBFile.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/API/SBFile.h"
10 #include "lldb/API/SBError.h"
11 #include "lldb/Host/File.h"
12 #include "lldb/Utility/Instrumentation.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
17 SBFile::~SBFile() = default;
18
SBFile(FileSP file_sp)19 SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
20 // We have no way to capture the incoming FileSP as the class isn't
21 // instrumented, so pretend that it's always null.
22 LLDB_INSTRUMENT_VA(this, file_sp);
23 }
24
SBFile(const SBFile & rhs)25 SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26 LLDB_INSTRUMENT_VA(this, rhs);
27 }
28
operator =(const SBFile & rhs)29 SBFile &SBFile ::operator=(const SBFile &rhs) {
30 LLDB_INSTRUMENT_VA(this, rhs);
31
32 if (this != &rhs)
33 m_opaque_sp = rhs.m_opaque_sp;
34 return *this;
35 }
36
SBFile()37 SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }
38
SBFile(FILE * file,bool transfer_ownership)39 SBFile::SBFile(FILE *file, bool transfer_ownership) {
40 LLDB_INSTRUMENT_VA(this, file, transfer_ownership);
41
42 m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
43 }
44
SBFile(int fd,const char * mode,bool transfer_owndership)45 SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
46 LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);
47
48 auto options = File::GetOptionsFromMode(mode);
49 if (!options) {
50 llvm::consumeError(options.takeError());
51 return;
52 }
53 m_opaque_sp =
54 std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
55 }
56
Read(uint8_t * buf,size_t num_bytes,size_t * bytes_read)57 SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
58 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
59
60 SBError error;
61 if (!m_opaque_sp) {
62 error.SetErrorString("invalid SBFile");
63 *bytes_read = 0;
64 } else {
65 Status status = m_opaque_sp->Read(buf, num_bytes);
66 error.SetError(status);
67 *bytes_read = num_bytes;
68 }
69 return error;
70 }
71
Write(const uint8_t * buf,size_t num_bytes,size_t * bytes_written)72 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
73 size_t *bytes_written) {
74 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
75
76 SBError error;
77 if (!m_opaque_sp) {
78 error.SetErrorString("invalid SBFile");
79 *bytes_written = 0;
80 } else {
81 Status status = m_opaque_sp->Write(buf, num_bytes);
82 error.SetError(status);
83 *bytes_written = num_bytes;
84 }
85 return error;
86 }
87
Flush()88 SBError SBFile::Flush() {
89 LLDB_INSTRUMENT_VA(this);
90
91 SBError error;
92 if (!m_opaque_sp) {
93 error.SetErrorString("invalid SBFile");
94 } else {
95 Status status = m_opaque_sp->Flush();
96 error.SetError(status);
97 }
98 return error;
99 }
100
IsValid() const101 bool SBFile::IsValid() const {
102 LLDB_INSTRUMENT_VA(this);
103 return m_opaque_sp && m_opaque_sp->IsValid();
104 }
105
Close()106 SBError SBFile::Close() {
107 LLDB_INSTRUMENT_VA(this);
108 SBError error;
109 if (m_opaque_sp) {
110 Status status = m_opaque_sp->Close();
111 error.SetError(status);
112 }
113 return error;
114 }
115
operator bool() const116 SBFile::operator bool() const {
117 LLDB_INSTRUMENT_VA(this);
118 return IsValid();
119 }
120
operator !() const121 bool SBFile::operator!() const {
122 LLDB_INSTRUMENT_VA(this);
123 return !IsValid();
124 }
125
GetFile() const126 FileSP SBFile::GetFile() const {
127 LLDB_INSTRUMENT_VA(this);
128 return m_opaque_sp;
129 }
130