Path: blob/main/contrib/llvm-project/lldb/source/Host/common/FileAction.cpp
39606 views
//===-- FileAction.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <fcntl.h>910#include "lldb/Host/FileAction.h"11#include "lldb/Host/PosixApi.h"12#include "lldb/Utility/Stream.h"1314using namespace lldb_private;1516// FileAction member functions1718FileAction::FileAction() : m_file_spec() {}1920void FileAction::Clear() {21m_action = eFileActionNone;22m_fd = -1;23m_arg = -1;24m_file_spec.Clear();25}2627llvm::StringRef FileAction::GetPath() const {28return m_file_spec.GetPathAsConstString().AsCString();29}3031const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }3233bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,34bool write) {35if ((read || write) && fd >= 0 && file_spec) {36m_action = eFileActionOpen;37m_fd = fd;38if (read && write)39m_arg = O_NOCTTY | O_CREAT | O_RDWR;40else if (read)41m_arg = O_NOCTTY | O_RDONLY;42else43m_arg = O_NOCTTY | O_CREAT | O_WRONLY;44m_file_spec = file_spec;45return true;46} else {47Clear();48}49return false;50}5152bool FileAction::Close(int fd) {53Clear();54if (fd >= 0) {55m_action = eFileActionClose;56m_fd = fd;57}58return m_fd >= 0;59}6061bool FileAction::Duplicate(int fd, int dup_fd) {62Clear();63if (fd >= 0 && dup_fd >= 0) {64m_action = eFileActionDuplicate;65m_fd = fd;66m_arg = dup_fd;67}68return m_fd >= 0;69}7071void FileAction::Dump(Stream &stream) const {72stream.PutCString("file action: ");73switch (m_action) {74case eFileActionClose:75stream.Printf("close fd %d", m_fd);76break;77case eFileActionDuplicate:78stream.Printf("duplicate fd %d to %d", m_fd, m_arg);79break;80case eFileActionNone:81stream.PutCString("no action");82break;83case eFileActionOpen:84stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,85m_file_spec.GetPath().c_str(), m_arg);86break;87}88}899091