brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 5690992 Raw
140 lines · cpp
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-exception6//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 14using namespace lldb;15using namespace lldb_private;16 17SBFile::~SBFile() = default;18 19SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {20  // We have no way to capture the incoming FileSP as the class isn't21  // instrumented, so pretend that it's always null.22  LLDB_INSTRUMENT_VA(this, file_sp);23}24 25SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {26  LLDB_INSTRUMENT_VA(this, rhs);27}28 29SBFile &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 37SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }38 39SBFile::SBFile(FILE *file, bool transfer_ownership) {40  LLDB_INSTRUMENT_VA(this, file, transfer_ownership);41 42  // For backwards comptability, this defaulted to ReadOnly previously.43  m_opaque_sp = std::make_shared<NativeFile>(file, File::eOpenOptionReadOnly,44                                             transfer_ownership);45}46 47SBFile::SBFile(FILE *file, const char *mode, bool transfer_ownership) {48  LLDB_INSTRUMENT_VA(this, file, transfer_ownership);49 50  auto options = File::GetOptionsFromMode(mode);51  if (!options) {52    llvm::consumeError(options.takeError());53    return;54  }55 56  m_opaque_sp =57      std::make_shared<NativeFile>(file, options.get(), transfer_ownership);58}59 60SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {61  LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);62 63  auto options = File::GetOptionsFromMode(mode);64  if (!options) {65    llvm::consumeError(options.takeError());66    return;67  }68  m_opaque_sp =69      std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);70}71 72SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {73  LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);74 75  SBError error;76  if (!m_opaque_sp) {77    error = Status::FromErrorString("invalid SBFile");78    *bytes_read = 0;79  } else {80    error.SetError(m_opaque_sp->Read(buf, num_bytes));81    *bytes_read = num_bytes;82  }83  return error;84}85 86SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,87                      size_t *bytes_written) {88  LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);89 90  SBError error;91  if (!m_opaque_sp) {92    error = Status::FromErrorString("invalid SBFile");93    *bytes_written = 0;94  } else {95    error.SetError(m_opaque_sp->Write(buf, num_bytes));96    *bytes_written = num_bytes;97  }98  return error;99}100 101SBError SBFile::Flush() {102  LLDB_INSTRUMENT_VA(this);103 104  SBError error;105  if (!m_opaque_sp) {106    error = Status::FromErrorString("invalid SBFile");107  } else {108    error.SetError(m_opaque_sp->Flush());109  }110  return error;111}112 113bool SBFile::IsValid() const {114  LLDB_INSTRUMENT_VA(this);115  return m_opaque_sp && m_opaque_sp->IsValid();116}117 118SBError SBFile::Close() {119  LLDB_INSTRUMENT_VA(this);120  SBError error;121  if (m_opaque_sp)122    error.SetError(m_opaque_sp->Close());123  return error;124}125 126SBFile::operator bool() const {127  LLDB_INSTRUMENT_VA(this);128  return IsValid();129}130 131bool SBFile::operator!() const {132  LLDB_INSTRUMENT_VA(this);133  return !IsValid();134}135 136FileSP SBFile::GetFile() const {137  LLDB_INSTRUMENT_VA(this);138  return m_opaque_sp;139}140