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