]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Target/FileAction.cpp
Import LLDB as of upstream SVN r216948 (git 50f7fe44)
[FreeBSD/FreeBSD.git] / 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/Target/FileAction.h"
17
18 using namespace lldb_private;
19
20 //----------------------------------------------------------------------------
21 // FileAction member functions
22 //----------------------------------------------------------------------------
23
24 FileAction::FileAction()
25     : m_action(eFileActionNone)
26     , m_fd(-1)
27     , m_arg(-1)
28     , m_path()
29 {
30 }
31
32 void
33 FileAction::Clear()
34 {
35     m_action = eFileActionNone;
36     m_fd = -1;
37     m_arg = -1;
38     m_path.clear();
39 }
40
41 const char *
42 FileAction::GetPath() const
43 {
44     if (m_path.empty())
45         return NULL;
46     return m_path.c_str();
47 }
48
49 bool
50 FileAction::Open(int fd, const char *path, bool read, bool write)
51 {
52     if ((read || write) && fd >= 0 && path && path[0])
53     {
54         m_action = eFileActionOpen;
55         m_fd = fd;
56         if (read && write)
57             m_arg = O_NOCTTY | O_CREAT | O_RDWR;
58         else if (read)
59             m_arg = O_NOCTTY | O_RDONLY;
60         else
61             m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
62         m_path.assign(path);
63         return true;
64     }
65     else
66     {
67         Clear();
68     }
69     return false;
70 }
71
72 bool
73 FileAction::Close(int fd)
74 {
75     Clear();
76     if (fd >= 0)
77     {
78         m_action = eFileActionClose;
79         m_fd = fd;
80     }
81     return m_fd >= 0;
82 }
83
84 bool
85 FileAction::Duplicate(int fd, int dup_fd)
86 {
87     Clear();
88     if (fd >= 0 && dup_fd >= 0)
89     {
90         m_action = eFileActionDuplicate;
91         m_fd = fd;
92         m_arg = dup_fd;
93     }
94     return m_fd >= 0;
95 }