]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/FileAction.cpp
Upgrade to OpenSSH 6.8p1.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / FileAction.cpp
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
12 #if defined(_WIN32)
13 #include "lldb/Host/Windows/win32.h" // For O_NOCTTY
14 #endif
15
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Target/FileAction.h"
18
19 using namespace lldb_private;
20
21 //----------------------------------------------------------------------------
22 // FileAction member functions
23 //----------------------------------------------------------------------------
24
25 FileAction::FileAction() : 
26     m_action(eFileActionNone),
27     m_fd(-1),
28     m_arg(-1),
29     m_file_spec()
30 {
31 }
32
33 void
34 FileAction::Clear()
35 {
36     m_action = eFileActionNone;
37     m_fd = -1;
38     m_arg = -1;
39     m_file_spec.Clear();
40 }
41
42 const char *
43 FileAction::GetPath() const
44 {
45     return m_file_spec.GetCString();
46 }
47
48 const FileSpec &
49 FileAction::GetFileSpec() const
50 {
51     return m_file_spec;
52 }
53
54 bool
55 FileAction::Open(int fd, const FileSpec &file_spec, bool read, bool write)
56 {
57     if ((read || write) && fd >= 0 && file_spec)
58     {
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;
67         m_file_spec = file_spec;
68         return true;
69     }
70     else
71     {
72         Clear();
73     }
74     return false;
75 }
76
77 bool
78 FileAction::Close(int fd)
79 {
80     Clear();
81     if (fd >= 0)
82     {
83         m_action = eFileActionClose;
84         m_fd = fd;
85     }
86     return m_fd >= 0;
87 }
88
89 bool
90 FileAction::Duplicate(int fd, int dup_fd)
91 {
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 }
101
102 void
103 FileAction::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:
118             stream.Printf("open fd %d with '%s', OFLAGS = 0x%x",
119                     m_fd, m_file_spec.GetCString(), m_arg);
120             break;
121     }
122 }