1 //===-- SBFile.cpp ------------------------------------------*- C++ -*-===//
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 "SBReproducerPrivate.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/Host/File.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 SBFile::~SBFile() {}
18 
19 SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
20   LLDB_RECORD_DUMMY(void, SBfile, SBFile, (FileSP), file_sp);
21 }
22 
23 SBFile::SBFile() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFile); }
24 
25 SBFile::SBFile(FILE *file, bool transfer_ownership) {
26   LLDB_RECORD_DUMMY(void, SBFile, (FILE *, bool), file, transfer_ownership);
27   m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
28 }
29 
30 SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
31   LLDB_RECORD_DUMMY(void, SBFile, (int, const char *, bool), fd, mode,
32                     transfer_owndership);
33   auto options = File::GetOptionsFromMode(mode);
34   m_opaque_sp = std::make_shared<NativeFile>(fd, options, transfer_owndership);
35 }
36 
37 SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
38   LLDB_RECORD_DUMMY(lldb::SBError, SBFile, Read, (uint8_t *, size_t, size_t *),
39                     buf, num_bytes, bytes_read);
40   SBError error;
41   if (!m_opaque_sp) {
42     error.SetErrorString("invalid SBFile");
43     *bytes_read = 0;
44   } else {
45     Status status = m_opaque_sp->Read(buf, num_bytes);
46     error.SetError(status);
47     *bytes_read = num_bytes;
48   }
49   return LLDB_RECORD_RESULT(error);
50 }
51 
52 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
53                       size_t *bytes_written) {
54   LLDB_RECORD_DUMMY(lldb::SBError, SBFile, Write,
55                     (const uint8_t *, size_t, size_t *), buf, num_bytes,
56                     bytes_written);
57   SBError error;
58   if (!m_opaque_sp) {
59     error.SetErrorString("invalid SBFile");
60     *bytes_written = 0;
61   } else {
62     Status status = m_opaque_sp->Write(buf, num_bytes);
63     error.SetError(status);
64     *bytes_written = num_bytes;
65   }
66   return LLDB_RECORD_RESULT(error);
67 }
68 
69 SBError SBFile::Flush() {
70   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Flush);
71   SBError error;
72   if (!m_opaque_sp) {
73     error.SetErrorString("invalid SBFile");
74   } else {
75     Status status = m_opaque_sp->Flush();
76     error.SetError(status);
77   }
78   return LLDB_RECORD_RESULT(error);
79 }
80 
81 bool SBFile::IsValid() const {
82   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, IsValid);
83   return m_opaque_sp && m_opaque_sp->IsValid();
84 }
85 
86 SBError SBFile::Close() {
87   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Close);
88   SBError error;
89   if (m_opaque_sp) {
90     Status status = m_opaque_sp->Close();
91     error.SetError(status);
92   }
93   return LLDB_RECORD_RESULT(error);
94 }
95 
96 SBFile::operator bool() const {
97   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator bool);
98   return LLDB_RECORD_RESULT(IsValid());
99 }
100 
101 bool SBFile::operator!() const {
102   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator!);
103   return LLDB_RECORD_RESULT(!IsValid());
104 }
105 
106 namespace lldb_private {
107 namespace repro {
108 
109 template <> void RegisterMethods<SBFile>(Registry &R) {
110 
111   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Flush, ());
112   LLDB_REGISTER_METHOD_CONST(bool, SBFile, IsValid, ());
113   LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator bool,());
114   LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator!,());
115   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Close, ());
116 }
117 } // namespace repro
118 } // namespace lldb_private
119