/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/FileAction.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
11 | | #include "lldb/Host/FileAction.h" |
12 | | #include "lldb/Host/PosixApi.h" |
13 | | #include "lldb/Utility/Stream.h" |
14 | | |
15 | | using namespace lldb_private; |
16 | | |
17 | | // FileAction member functions |
18 | | |
19 | 54.3k | FileAction::FileAction() : m_file_spec() {} |
20 | | |
21 | 36.8k | void FileAction::Clear() { |
22 | 36.8k | m_action = eFileActionNone; |
23 | 36.8k | m_fd = -1; |
24 | 36.8k | m_arg = -1; |
25 | 36.8k | m_file_spec.Clear(); |
26 | 36.8k | } |
27 | | |
28 | 17.5k | llvm::StringRef FileAction::GetPath() const { |
29 | 17.5k | return m_file_spec.GetPathAsConstString().AsCString(); |
30 | 17.5k | } |
31 | | |
32 | 22 | const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; } |
33 | | |
34 | | bool FileAction::Open(int fd, const FileSpec &file_spec, bool read, |
35 | 44.3k | bool write) { |
36 | 44.3k | if ((read || write29.1k ) && fd >= 0 && file_spec) { |
37 | 17.5k | m_action = eFileActionOpen; |
38 | 17.5k | m_fd = fd; |
39 | 17.5k | if (read && write6.33k ) |
40 | 0 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
41 | 17.5k | else if (read) |
42 | 6.33k | m_arg = O_NOCTTY | O_RDONLY; |
43 | 11.2k | else |
44 | 11.2k | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
45 | 17.5k | m_file_spec = file_spec; |
46 | 17.5k | return true; |
47 | 26.8k | } else { |
48 | 26.8k | Clear(); |
49 | 26.8k | } |
50 | 26.8k | return false; |
51 | 44.3k | } |
52 | | |
53 | 6.36k | bool FileAction::Close(int fd) { |
54 | 6.36k | Clear(); |
55 | 6.36k | if (fd >= 0) { |
56 | 6.36k | m_action = eFileActionClose; |
57 | 6.36k | m_fd = fd; |
58 | 6.36k | } |
59 | 6.36k | return m_fd >= 0; |
60 | 6.36k | } |
61 | | |
62 | 3.58k | bool FileAction::Duplicate(int fd, int dup_fd) { |
63 | 3.58k | Clear(); |
64 | 3.58k | if (fd >= 0 && dup_fd >= 0) { |
65 | 3.58k | m_action = eFileActionDuplicate; |
66 | 3.58k | m_fd = fd; |
67 | 3.58k | m_arg = dup_fd; |
68 | 3.58k | } |
69 | 3.58k | return m_fd >= 0; |
70 | 3.58k | } |
71 | | |
72 | 0 | void FileAction::Dump(Stream &stream) const { |
73 | 0 | stream.PutCString("file action: "); |
74 | 0 | switch (m_action) { |
75 | 0 | case eFileActionClose: |
76 | 0 | stream.Printf("close fd %d", m_fd); |
77 | 0 | break; |
78 | 0 | case eFileActionDuplicate: |
79 | 0 | stream.Printf("duplicate fd %d to %d", m_fd, m_arg); |
80 | 0 | break; |
81 | 0 | case eFileActionNone: |
82 | 0 | stream.PutCString("no action"); |
83 | 0 | break; |
84 | 0 | case eFileActionOpen: |
85 | 0 | stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd, |
86 | 0 | m_file_spec.GetPath().c_str(), m_arg); |
87 | 0 | break; |
88 | 0 | } |
89 | 0 | } |