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