blob: 21c37e8e6f85f76267aea1de3cd4561f8e2e942d [file] [log] [blame]
Zachary Turner696b5282014-08-14 16:01:25 +00001//===-- 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
12#if defined(_WIN32)
13#include "lldb/Host/Windows/win32.h" // For O_NOCTTY
14#endif
15
Todd Fiala348fb382014-10-10 00:09:16 +000016#include "lldb/Core/Stream.h"
Zachary Turner696b5282014-08-14 16:01:25 +000017#include "lldb/Target/FileAction.h"
18
19using namespace lldb_private;
20
21//----------------------------------------------------------------------------
22// FileAction member functions
23//----------------------------------------------------------------------------
24
Jason Molendae6481c72014-09-12 01:50:46 +000025FileAction::FileAction() :
26 m_action(eFileActionNone),
27 m_fd(-1),
28 m_arg(-1),
Chaoren Lind3173f32015-05-29 19:52:29 +000029 m_file_spec()
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000030{
31}
Zachary Turner696b5282014-08-14 16:01:25 +000032
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000033void
34FileAction::Clear()
Zachary Turner696b5282014-08-14 16:01:25 +000035{
36 m_action = eFileActionNone;
37 m_fd = -1;
38 m_arg = -1;
Chaoren Lind3173f32015-05-29 19:52:29 +000039 m_file_spec.Clear();
Zachary Turner696b5282014-08-14 16:01:25 +000040}
41
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000042const char *
43FileAction::GetPath() const
Zachary Turner696b5282014-08-14 16:01:25 +000044{
Chaoren Lind3173f32015-05-29 19:52:29 +000045 return m_file_spec.GetCString();
46}
47
48const FileSpec &
49FileAction::GetFileSpec() const
50{
51 return m_file_spec;
Zachary Turner696b5282014-08-14 16:01:25 +000052}
53
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000054bool
Chaoren Lind3173f32015-05-29 19:52:29 +000055FileAction::Open(int fd, const FileSpec &file_spec, bool read, bool write)
Zachary Turner696b5282014-08-14 16:01:25 +000056{
Chaoren Lind3173f32015-05-29 19:52:29 +000057 if ((read || write) && fd >= 0 && file_spec)
Zachary Turner696b5282014-08-14 16:01:25 +000058 {
59 m_action = eFileActionOpen;
60 m_fd = fd;
61 if (read && write)
62 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
63 else if (read)
64 m_arg = O_NOCTTY | O_RDONLY;
65 else
66 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
Chaoren Lind3173f32015-05-29 19:52:29 +000067 m_file_spec = file_spec;
Zachary Turner696b5282014-08-14 16:01:25 +000068 return true;
69 }
70 else
71 {
72 Clear();
73 }
74 return false;
75}
76
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000077bool
78FileAction::Close(int fd)
Zachary Turner696b5282014-08-14 16:01:25 +000079{
80 Clear();
81 if (fd >= 0)
82 {
83 m_action = eFileActionClose;
84 m_fd = fd;
85 }
86 return m_fd >= 0;
87}
88
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000089bool
90FileAction::Duplicate(int fd, int dup_fd)
Zachary Turner696b5282014-08-14 16:01:25 +000091{
92 Clear();
93 if (fd >= 0 && dup_fd >= 0)
94 {
95 m_action = eFileActionDuplicate;
96 m_fd = fd;
97 m_arg = dup_fd;
98 }
99 return m_fd >= 0;
100}
Todd Fiala348fb382014-10-10 00:09:16 +0000101
102void
103FileAction::Dump(Stream &stream) const
104{
105 stream.PutCString("file action: ");
106 switch (m_action)
107 {
108 case eFileActionClose:
109 stream.Printf("close fd %d", m_fd);
110 break;
111 case eFileActionDuplicate:
112 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
113 break;
114 case eFileActionNone:
115 stream.PutCString("no action");
116 break;
117 case eFileActionOpen:
Chaoren Lind3173f32015-05-29 19:52:29 +0000118 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x",
119 m_fd, m_file_spec.GetCString(), m_arg);
Todd Fiala348fb382014-10-10 00:09:16 +0000120 break;
121 }
122}