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