1 //===-- FileAction.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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <fcntl.h> 10 11 #include "lldb/Host/FileAction.h" 12 #include "lldb/Host/PosixApi.h" 13 #include "lldb/Utility/Stream.h" 14 15 using namespace lldb_private; 16 17 // FileAction member functions 18 19 FileAction::FileAction() : m_file_spec() {} 20 21 void FileAction::Clear() { 22 m_action = eFileActionNone; 23 m_fd = -1; 24 m_arg = -1; 25 m_file_spec.Clear(); 26 } 27 28 llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); } 29 30 const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; } 31 32 bool FileAction::Open(int fd, const FileSpec &file_spec, bool read, 33 bool write) { 34 if ((read || write) && fd >= 0 && file_spec) { 35 m_action = eFileActionOpen; 36 m_fd = fd; 37 if (read && write) 38 m_arg = O_NOCTTY | O_CREAT | O_RDWR; 39 else if (read) 40 m_arg = O_NOCTTY | O_RDONLY; 41 else 42 m_arg = O_NOCTTY | O_CREAT | O_WRONLY; 43 m_file_spec = file_spec; 44 return true; 45 } else { 46 Clear(); 47 } 48 return false; 49 } 50 51 bool FileAction::Close(int fd) { 52 Clear(); 53 if (fd >= 0) { 54 m_action = eFileActionClose; 55 m_fd = fd; 56 } 57 return m_fd >= 0; 58 } 59 60 bool FileAction::Duplicate(int fd, int dup_fd) { 61 Clear(); 62 if (fd >= 0 && dup_fd >= 0) { 63 m_action = eFileActionDuplicate; 64 m_fd = fd; 65 m_arg = dup_fd; 66 } 67 return m_fd >= 0; 68 } 69 70 void FileAction::Dump(Stream &stream) const { 71 stream.PutCString("file action: "); 72 switch (m_action) { 73 case eFileActionClose: 74 stream.Printf("close fd %d", m_fd); 75 break; 76 case eFileActionDuplicate: 77 stream.Printf("duplicate fd %d to %d", m_fd, m_arg); 78 break; 79 case eFileActionNone: 80 stream.PutCString("no action"); 81 break; 82 case eFileActionOpen: 83 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd, 84 m_file_spec.GetCString(), m_arg); 85 break; 86 } 87 } 88