180814287SRaphael Isemann //===-- FileAction.cpp ----------------------------------------------------===//
2eef758e9SPavel Labath //
3eef758e9SPavel Labath // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4eef758e9SPavel Labath // See https://llvm.org/LICENSE.txt for license information.
5eef758e9SPavel Labath // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eef758e9SPavel Labath //
7eef758e9SPavel Labath //===----------------------------------------------------------------------===//
8eef758e9SPavel Labath
9eef758e9SPavel Labath #include <fcntl.h>
10eef758e9SPavel Labath
11eef758e9SPavel Labath #include "lldb/Host/FileAction.h"
12eef758e9SPavel Labath #include "lldb/Host/PosixApi.h"
13eef758e9SPavel Labath #include "lldb/Utility/Stream.h"
14eef758e9SPavel Labath
15eef758e9SPavel Labath using namespace lldb_private;
16eef758e9SPavel Labath
17eef758e9SPavel Labath // FileAction member functions
18eef758e9SPavel Labath
FileAction()199494c510SJonas Devlieghere FileAction::FileAction() : m_file_spec() {}
20eef758e9SPavel Labath
Clear()21eef758e9SPavel Labath void FileAction::Clear() {
22eef758e9SPavel Labath m_action = eFileActionNone;
23eef758e9SPavel Labath m_fd = -1;
24eef758e9SPavel Labath m_arg = -1;
25eef758e9SPavel Labath m_file_spec.Clear();
26eef758e9SPavel Labath }
27eef758e9SPavel Labath
GetPath() const28*1b4b12a3SNico Weber llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
29eef758e9SPavel Labath
GetFileSpec() const30eef758e9SPavel Labath const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
31eef758e9SPavel Labath
Open(int fd,const FileSpec & file_spec,bool read,bool write)32eef758e9SPavel Labath bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
33eef758e9SPavel Labath bool write) {
34eef758e9SPavel Labath if ((read || write) && fd >= 0 && file_spec) {
35eef758e9SPavel Labath m_action = eFileActionOpen;
36eef758e9SPavel Labath m_fd = fd;
37eef758e9SPavel Labath if (read && write)
38eef758e9SPavel Labath m_arg = O_NOCTTY | O_CREAT | O_RDWR;
39eef758e9SPavel Labath else if (read)
40eef758e9SPavel Labath m_arg = O_NOCTTY | O_RDONLY;
41eef758e9SPavel Labath else
42eef758e9SPavel Labath m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
43eef758e9SPavel Labath m_file_spec = file_spec;
44eef758e9SPavel Labath return true;
45eef758e9SPavel Labath } else {
46eef758e9SPavel Labath Clear();
47eef758e9SPavel Labath }
48eef758e9SPavel Labath return false;
49eef758e9SPavel Labath }
50eef758e9SPavel Labath
Close(int fd)51eef758e9SPavel Labath bool FileAction::Close(int fd) {
52eef758e9SPavel Labath Clear();
53eef758e9SPavel Labath if (fd >= 0) {
54eef758e9SPavel Labath m_action = eFileActionClose;
55eef758e9SPavel Labath m_fd = fd;
56eef758e9SPavel Labath }
57eef758e9SPavel Labath return m_fd >= 0;
58eef758e9SPavel Labath }
59eef758e9SPavel Labath
Duplicate(int fd,int dup_fd)60eef758e9SPavel Labath bool FileAction::Duplicate(int fd, int dup_fd) {
61eef758e9SPavel Labath Clear();
62eef758e9SPavel Labath if (fd >= 0 && dup_fd >= 0) {
63eef758e9SPavel Labath m_action = eFileActionDuplicate;
64eef758e9SPavel Labath m_fd = fd;
65eef758e9SPavel Labath m_arg = dup_fd;
66eef758e9SPavel Labath }
67eef758e9SPavel Labath return m_fd >= 0;
68eef758e9SPavel Labath }
69eef758e9SPavel Labath
Dump(Stream & stream) const70eef758e9SPavel Labath void FileAction::Dump(Stream &stream) const {
71eef758e9SPavel Labath stream.PutCString("file action: ");
72eef758e9SPavel Labath switch (m_action) {
73eef758e9SPavel Labath case eFileActionClose:
74eef758e9SPavel Labath stream.Printf("close fd %d", m_fd);
75eef758e9SPavel Labath break;
76eef758e9SPavel Labath case eFileActionDuplicate:
77eef758e9SPavel Labath stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
78eef758e9SPavel Labath break;
79eef758e9SPavel Labath case eFileActionNone:
80eef758e9SPavel Labath stream.PutCString("no action");
81eef758e9SPavel Labath break;
82eef758e9SPavel Labath case eFileActionOpen:
83eef758e9SPavel Labath stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
84*1b4b12a3SNico Weber m_file_spec.GetCString(), m_arg);
85eef758e9SPavel Labath break;
86eef758e9SPavel Labath }
87eef758e9SPavel Labath }
88