blob: c9cc325b5a5393556b496b426ec2faa88282af78 [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
Zachary Turnerf343968f2016-08-09 23:06:08 +000012#include "lldb/Host/PosixApi.h"
Zachary Turner696b5282014-08-14 16:01:25 +000013#include "lldb/Target/FileAction.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/Stream.h"
Zachary Turner696b5282014-08-14 16:01:25 +000015
16using namespace lldb_private;
17
18//----------------------------------------------------------------------------
19// FileAction member functions
20//----------------------------------------------------------------------------
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022FileAction::FileAction()
23 : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_file_spec() {}
24
25void FileAction::Clear() {
26 m_action = eFileActionNone;
27 m_fd = -1;
28 m_arg = -1;
29 m_file_spec.Clear();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000030}
Zachary Turner696b5282014-08-14 16:01:25 +000031
Zachary Turner27a5c2b2016-09-23 22:11:51 +000032llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
Zachary Turner696b5282014-08-14 16:01:25 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
Chaoren Lind3173f32015-05-29 19:52:29 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
37 bool write) {
38 if ((read || write) && fd >= 0 && file_spec) {
39 m_action = eFileActionOpen;
40 m_fd = fd;
41 if (read && write)
42 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
43 else if (read)
44 m_arg = O_NOCTTY | O_RDONLY;
Zachary Turner696b5282014-08-14 16:01:25 +000045 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
47 m_file_spec = file_spec;
48 return true;
49 } else {
Zachary Turner696b5282014-08-14 16:01:25 +000050 Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 }
52 return false;
Zachary Turner696b5282014-08-14 16:01:25 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055bool FileAction::Close(int fd) {
56 Clear();
57 if (fd >= 0) {
58 m_action = eFileActionClose;
59 m_fd = fd;
60 }
61 return m_fd >= 0;
Zachary Turner696b5282014-08-14 16:01:25 +000062}
Todd Fiala348fb382014-10-10 00:09:16 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064bool FileAction::Duplicate(int fd, int dup_fd) {
65 Clear();
66 if (fd >= 0 && dup_fd >= 0) {
67 m_action = eFileActionDuplicate;
68 m_fd = fd;
69 m_arg = dup_fd;
70 }
71 return m_fd >= 0;
72}
73
74void FileAction::Dump(Stream &stream) const {
75 stream.PutCString("file action: ");
76 switch (m_action) {
77 case eFileActionClose:
78 stream.Printf("close fd %d", m_fd);
79 break;
80 case eFileActionDuplicate:
81 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
82 break;
83 case eFileActionNone:
84 stream.PutCString("no action");
85 break;
86 case eFileActionOpen:
87 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
88 m_file_spec.GetCString(), m_arg);
89 break;
90 }
Todd Fiala348fb382014-10-10 00:09:16 +000091}