blob: 5756147eea4afe458a3adf14d0b4ee51de10bf26 [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
24FileAction::FileAction() : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_path() {}
25
26void FileAction::Clear()
27{
28 m_action = eFileActionNone;
29 m_fd = -1;
30 m_arg = -1;
31 m_path.clear();
32}
33
34const char *FileAction::GetPath() const
35{
36 if (m_path.empty())
37 return NULL;
38 return m_path.c_str();
39}
40
41bool FileAction::Open(int fd, const char *path, bool read, bool write)
42{
43 if ((read || write) && fd >= 0 && path && path[0])
44 {
45 m_action = eFileActionOpen;
46 m_fd = fd;
47 if (read && write)
48 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
49 else if (read)
50 m_arg = O_NOCTTY | O_RDONLY;
51 else
52 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
53 m_path.assign(path);
54 return true;
55 }
56 else
57 {
58 Clear();
59 }
60 return false;
61}
62
63bool FileAction::Close(int fd)
64{
65 Clear();
66 if (fd >= 0)
67 {
68 m_action = eFileActionClose;
69 m_fd = fd;
70 }
71 return m_fd >= 0;
72}
73
74bool FileAction::Duplicate(int fd, int dup_fd)
75{
76 Clear();
77 if (fd >= 0 && dup_fd >= 0)
78 {
79 m_action = eFileActionDuplicate;
80 m_fd = fd;
81 m_arg = dup_fd;
82 }
83 return m_fd >= 0;
84}