180814287SRaphael Isemann //===-- FileAction.cpp ----------------------------------------------------===// 2eef758e9SPavel Labath // 3eef758e9SPavel Labath // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4eef758e9SPavel Labath // See https://llvm.org/LICENSE.txt for license information. 5eef758e9SPavel Labath // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6eef758e9SPavel Labath // 7eef758e9SPavel Labath //===----------------------------------------------------------------------===// 8eef758e9SPavel Labath 9eef758e9SPavel Labath #include <fcntl.h> 10eef758e9SPavel Labath 11eef758e9SPavel Labath #include "lldb/Host/FileAction.h" 12eef758e9SPavel Labath #include "lldb/Host/PosixApi.h" 13eef758e9SPavel Labath #include "lldb/Utility/Stream.h" 14eef758e9SPavel Labath 15eef758e9SPavel Labath using namespace lldb_private; 16eef758e9SPavel Labath 17eef758e9SPavel Labath // FileAction member functions 18eef758e9SPavel Labath 199494c510SJonas Devlieghere FileAction::FileAction() : m_file_spec() {} 20eef758e9SPavel Labath 21eef758e9SPavel Labath void FileAction::Clear() { 22eef758e9SPavel Labath m_action = eFileActionNone; 23eef758e9SPavel Labath m_fd = -1; 24eef758e9SPavel Labath m_arg = -1; 25eef758e9SPavel Labath m_file_spec.Clear(); 26eef758e9SPavel Labath } 27eef758e9SPavel Labath 28529a3d87SGreg Clayton llvm::StringRef FileAction::GetPath() const { 29529a3d87SGreg Clayton return m_file_spec.GetPathAsConstString().AsCString(); 30529a3d87SGreg Clayton } 31eef758e9SPavel Labath 32eef758e9SPavel Labath const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; } 33eef758e9SPavel Labath 34eef758e9SPavel Labath bool FileAction::Open(int fd, const FileSpec &file_spec, bool read, 35eef758e9SPavel Labath bool write) { 36eef758e9SPavel Labath if ((read || write) && fd >= 0 && file_spec) { 37eef758e9SPavel Labath m_action = eFileActionOpen; 38eef758e9SPavel Labath m_fd = fd; 39eef758e9SPavel Labath if (read && write) 40eef758e9SPavel Labath m_arg = O_NOCTTY | O_CREAT | O_RDWR; 41eef758e9SPavel Labath else if (read) 42eef758e9SPavel Labath m_arg = O_NOCTTY | O_RDONLY; 43eef758e9SPavel Labath else 44*efc6d33bSWanyi m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC; 45eef758e9SPavel Labath m_file_spec = file_spec; 46eef758e9SPavel Labath return true; 47eef758e9SPavel Labath } else { 48eef758e9SPavel Labath Clear(); 49eef758e9SPavel Labath } 50eef758e9SPavel Labath return false; 51eef758e9SPavel Labath } 52eef758e9SPavel Labath 53eef758e9SPavel Labath bool FileAction::Close(int fd) { 54eef758e9SPavel Labath Clear(); 55eef758e9SPavel Labath if (fd >= 0) { 56eef758e9SPavel Labath m_action = eFileActionClose; 57eef758e9SPavel Labath m_fd = fd; 58eef758e9SPavel Labath } 59eef758e9SPavel Labath return m_fd >= 0; 60eef758e9SPavel Labath } 61eef758e9SPavel Labath 62eef758e9SPavel Labath bool FileAction::Duplicate(int fd, int dup_fd) { 63eef758e9SPavel Labath Clear(); 64eef758e9SPavel Labath if (fd >= 0 && dup_fd >= 0) { 65eef758e9SPavel Labath m_action = eFileActionDuplicate; 66eef758e9SPavel Labath m_fd = fd; 67eef758e9SPavel Labath m_arg = dup_fd; 68eef758e9SPavel Labath } 69eef758e9SPavel Labath return m_fd >= 0; 70eef758e9SPavel Labath } 71eef758e9SPavel Labath 72eef758e9SPavel Labath void FileAction::Dump(Stream &stream) const { 73eef758e9SPavel Labath stream.PutCString("file action: "); 74eef758e9SPavel Labath switch (m_action) { 75eef758e9SPavel Labath case eFileActionClose: 76eef758e9SPavel Labath stream.Printf("close fd %d", m_fd); 77eef758e9SPavel Labath break; 78eef758e9SPavel Labath case eFileActionDuplicate: 79eef758e9SPavel Labath stream.Printf("duplicate fd %d to %d", m_fd, m_arg); 80eef758e9SPavel Labath break; 81eef758e9SPavel Labath case eFileActionNone: 82eef758e9SPavel Labath stream.PutCString("no action"); 83eef758e9SPavel Labath break; 84eef758e9SPavel Labath case eFileActionOpen: 85eef758e9SPavel Labath stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd, 86529a3d87SGreg Clayton m_file_spec.GetPath().c_str(), m_arg); 87eef758e9SPavel Labath break; 88eef758e9SPavel Labath } 89eef758e9SPavel Labath } 90