Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1 | //===-- FileAction.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <fcntl.h> |
| 11 | |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 12 | #include "lldb/Host/PosixApi.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 13 | #include "lldb/Target/FileAction.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/Stream.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | //---------------------------------------------------------------------------- |
| 19 | // FileAction member functions |
| 20 | //---------------------------------------------------------------------------- |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | FileAction::FileAction() |
| 23 | : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_file_spec() {} |
| 24 | |
| 25 | void FileAction::Clear() { |
| 26 | m_action = eFileActionNone; |
| 27 | m_fd = -1; |
| 28 | m_arg = -1; |
| 29 | m_file_spec.Clear(); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 30 | } |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 31 | |
Zachary Turner | 27a5c2b | 2016-09-23 22:11:51 +0000 | [diff] [blame] | 32 | llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); } |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | bool FileAction::Open(int fd, const FileSpec &file_spec, bool read, |
| 37 | bool write) { |
| 38 | if ((read || write) && fd >= 0 && file_spec) { |
| 39 | m_action = eFileActionOpen; |
| 40 | m_fd = fd; |
| 41 | if (read && write) |
| 42 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
| 43 | else if (read) |
| 44 | m_arg = O_NOCTTY | O_RDONLY; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 45 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
| 47 | m_file_spec = file_spec; |
| 48 | return true; |
| 49 | } else { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 50 | Clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | } |
| 52 | return false; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | bool FileAction::Close(int fd) { |
| 56 | Clear(); |
| 57 | if (fd >= 0) { |
| 58 | m_action = eFileActionClose; |
| 59 | m_fd = fd; |
| 60 | } |
| 61 | return m_fd >= 0; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 62 | } |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | bool FileAction::Duplicate(int fd, int dup_fd) { |
| 65 | Clear(); |
| 66 | if (fd >= 0 && dup_fd >= 0) { |
| 67 | m_action = eFileActionDuplicate; |
| 68 | m_fd = fd; |
| 69 | m_arg = dup_fd; |
| 70 | } |
| 71 | return m_fd >= 0; |
| 72 | } |
| 73 | |
| 74 | void FileAction::Dump(Stream &stream) const { |
| 75 | stream.PutCString("file action: "); |
| 76 | switch (m_action) { |
| 77 | case eFileActionClose: |
| 78 | stream.Printf("close fd %d", m_fd); |
| 79 | break; |
| 80 | case eFileActionDuplicate: |
| 81 | stream.Printf("duplicate fd %d to %d", m_fd, m_arg); |
| 82 | break; |
| 83 | case eFileActionNone: |
| 84 | stream.PutCString("no action"); |
| 85 | break; |
| 86 | case eFileActionOpen: |
| 87 | stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd, |
| 88 | m_file_spec.GetCString(), m_arg); |
| 89 | break; |
| 90 | } |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 91 | } |