1 //===-- FileAction.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Host/FileAction.h"
12 #include "lldb/Host/PosixApi.h"
13 #include "lldb/Utility/Stream.h"
15 using namespace lldb_private;
17 // FileAction member functions
19 FileAction::FileAction()
20 : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_file_spec() {}
22 void FileAction::Clear() {
23 m_action = eFileActionNone;
29 llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
31 const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
33 bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
35 if ((read || write) && fd >= 0 && file_spec) {
36 m_action = eFileActionOpen;
39 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
41 m_arg = O_NOCTTY | O_RDONLY;
43 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
44 m_file_spec = file_spec;
52 bool FileAction::Close(int fd) {
55 m_action = eFileActionClose;
61 bool FileAction::Duplicate(int fd, int dup_fd) {
63 if (fd >= 0 && dup_fd >= 0) {
64 m_action = eFileActionDuplicate;
71 void FileAction::Dump(Stream &stream) const {
72 stream.PutCString("file action: ");
74 case eFileActionClose:
75 stream.Printf("close fd %d", m_fd);
77 case eFileActionDuplicate:
78 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
81 stream.PutCString("no action");
84 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
85 m_file_spec.GetCString(), m_arg);