Debian Codegraph @

drivers/windows/file_access_windows_pipe.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* file_access_windows_pipe.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #ifdef WINDOWS_ENABLED
32
33 #include "file_access_windows_pipe.h"
34
35 #include "core/os/os.h"
36 #include "core/string/print_string.h"
37
38 Error FileAccessWindowsPipe::open_existing(HANDLE p_rfd, HANDLE p_wfd, bool p_blocking) {
39 // Open pipe using handles created by CreatePipe(rfd, wfd, NULL, 4096) call in the OS.execute_with_pipe.
40 _close();
41
42 path_src = String();
43 ERR_FAIL_COND_V_MSG(fd[0] != nullptr || fd[1] != nullptr, ERR_ALREADY_IN_USE, "Pipe is already in use.");
44 fd[0] = p_rfd;
45 fd[1] = p_wfd;
46
47 if (!p_blocking) {
48 DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
49 SetNamedPipeHandleState(fd[0], &mode, nullptr, nullptr);
50 SetNamedPipeHandleState(fd[1], &mode, nullptr, nullptr);
51 }
52
53 last_error = OK;
54 return OK;
55 }
56
57 Error FileAccessWindowsPipe::open_internal(const String &p_path, int p_mode_flags) {
58 _close();
59
60 path_src = p_path;
61 ERR_FAIL_COND_V_MSG(fd[0] != nullptr || fd[1] != nullptr, ERR_ALREADY_IN_USE, "Pipe is already in use.");
62
63 path = String("\\\\.\\pipe\\LOCAL\\") + p_path.replace("pipe://", "").replace_char('/', '_');
64
65 HANDLE h = CreateFileW((LPCWSTR)path.utf16().get_data(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
66 if (h == INVALID_HANDLE_VALUE) {
67 h = CreateNamedPipeW((LPCWSTR)path.utf16().get_data(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 4096, 4096, 0, nullptr);
68 if (h == INVALID_HANDLE_VALUE) {
69 last_error = ERR_FILE_CANT_OPEN;
70 return last_error;
71 }
72 ConnectNamedPipe(h, nullptr);
73 }
74 fd[0] = h;
75 fd[1] = h;
76
77 last_error = OK;
78 return OK;
79 }
80
81 void FileAccessWindowsPipe::_close() {
82 if (fd[0] == nullptr) {
83 return;
84 }
85 if (fd[1] != fd[0]) {
86 CloseHandle(fd[1]);
87 }
88 CloseHandle(fd[0]);
89 fd[0] = nullptr;
90 fd[1] = nullptr;
91 }
92
93 bool FileAccessWindowsPipe::is_open() const {
94 return (fd[0] != nullptr || fd[1] != nullptr);
95 }
96
97 String FileAccessWindowsPipe::get_path() const {
98 return path_src;
99 }
100
101 String FileAccessWindowsPipe::get_path_absolute() const {
102 return path_src;
103 }
104
105 uint64_t FileAccessWindowsPipe::get_length() const {
106 ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
107
108 DWORD buf_rem = 0;
109 ERR_FAIL_COND_V(!PeekNamedPipe(fd[0], nullptr, 0, nullptr, &buf_rem, nullptr), 0);
110 return buf_rem;
111 }
112
113 uint64_t FileAccessWindowsPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
114 ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
115 ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
116
117 DWORD read = 0;
118 if (!ReadFile(fd[0], p_dst, p_length, &read, nullptr) || read != p_length) {
119 last_error = ERR_FILE_CANT_READ;
120 } else {
121 last_error = OK;
122 }
123 return read;
124 }
125
126 Error FileAccessWindowsPipe::get_error() const {
127 return last_error;
128 }
129
130 bool FileAccessWindowsPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
131 ERR_FAIL_COND_V_MSG(fd[1] == nullptr, false, "Pipe must be opened before use.");
132 ERR_FAIL_COND_V(!p_src && p_length > 0, false);
133
134 DWORD read = -1;
135 bool ok = WriteFile(fd[1], p_src, p_length, &read, nullptr);
136 if (!ok || read != p_length) {
137 last_error = ERR_FILE_CANT_WRITE;
138 return false;
139 } else {
140 last_error = OK;
141 return true;
142 }
143 }
144
145 void FileAccessWindowsPipe::close() {
146 _close();
147 }
148
149 FileAccessWindowsPipe::~FileAccessWindowsPipe() {
150 _close();
151 }
152
153 #endif // WINDOWS_ENABLED