LLDB mainline
FileAction.cpp
Go to the documentation of this file.
1//===-- FileAction.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <fcntl.h>
10
12#include "lldb/Host/PosixApi.h"
13#include "lldb/Utility/Stream.h"
14
15using namespace lldb_private;
16
17// FileAction member functions
18
19FileAction::FileAction() : m_file_spec() {}
20
23 m_fd = -1;
24 m_arg = -1;
26}
27
28llvm::StringRef FileAction::GetPath() const {
30}
31
33
34bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
35 bool write) {
36 if ((read || write) && fd >= 0 && file_spec) {
38 m_fd = fd;
39 if (read && write)
40 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
41 else if (read)
42 m_arg = O_NOCTTY | O_RDONLY;
43 else
44 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
45 m_file_spec = file_spec;
46 return true;
47 } else {
48 Clear();
49 }
50 return false;
51}
52
53bool FileAction::Close(int fd) {
54 Clear();
55 if (fd >= 0) {
57 m_fd = fd;
58 }
59 return m_fd >= 0;
60}
61
62bool FileAction::Duplicate(int fd, int dup_fd) {
63 Clear();
64 if (fd >= 0 && dup_fd >= 0) {
66 m_fd = fd;
67 m_arg = dup_fd;
68 }
69 return m_fd >= 0;
70}
71
72void FileAction::Dump(Stream &stream) const {
73 stream.PutCString("file action: ");
74 switch (m_action) {
76 stream.Printf("close fd %d", m_fd);
77 break;
79 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
80 break;
81 case eFileActionNone:
82 stream.PutCString("no action");
83 break;
84 case eFileActionOpen:
85 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
86 m_file_spec.GetPath().c_str(), m_arg);
87 break;
88 }
89}
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
bool Duplicate(int fd, int dup_fd)
Definition: FileAction.cpp:62
void Dump(Stream &stream) const
Definition: FileAction.cpp:72
llvm::StringRef GetPath() const
Definition: FileAction.cpp:28
const FileSpec & GetFileSpec() const
Definition: FileAction.cpp:32
bool Open(int fd, const FileSpec &file_spec, bool read, bool write)
Definition: FileAction.cpp:34
A file utility class.
Definition: FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
void Clear()
Clears the object state.
Definition: FileSpec.cpp:259
ConstString GetPathAsConstString(bool denormalize=true) const
Get the full path as a ConstString.
Definition: FileSpec.cpp:383
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
A class that represents a running process on the host machine.
#define O_NOCTTY