blob: b2d2a76a623328520f946f55f53305fa72e1467f [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
16#include "lldb/Target/FileAction.h"
17
18using namespace lldb_private;
19
20//----------------------------------------------------------------------------
21// FileAction member functions
22//----------------------------------------------------------------------------
23
Jason Molendae6481c72014-09-12 01:50:46 +000024FileAction::FileAction() :
25 m_action(eFileActionNone),
26 m_fd(-1),
27 m_arg(-1),
28 m_path()
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000029{
30}
Zachary Turner696b5282014-08-14 16:01:25 +000031
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000032void
33FileAction::Clear()
Zachary Turner696b5282014-08-14 16:01:25 +000034{
35 m_action = eFileActionNone;
36 m_fd = -1;
37 m_arg = -1;
38 m_path.clear();
39}
40
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000041const char *
42FileAction::GetPath() const
Zachary Turner696b5282014-08-14 16:01:25 +000043{
44 if (m_path.empty())
45 return NULL;
46 return m_path.c_str();
47}
48
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000049bool
50FileAction::Open(int fd, const char *path, bool read, bool write)
Zachary Turner696b5282014-08-14 16:01:25 +000051{
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
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000072bool
73FileAction::Close(int fd)
Zachary Turner696b5282014-08-14 16:01:25 +000074{
75 Clear();
76 if (fd >= 0)
77 {
78 m_action = eFileActionClose;
79 m_fd = fd;
80 }
81 return m_fd >= 0;
82}
83
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000084bool
85FileAction::Duplicate(int fd, int dup_fd)
Zachary Turner696b5282014-08-14 16:01:25 +000086{
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}