|
| 1 | +/**************************************************************************/ |
| 2 | +/* file_access_unix_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 | +#include "file_access_unix_pipe.h" |
| 32 | + |
| 33 | +#if defined(UNIX_ENABLED) |
| 34 | + |
| 35 | +#include "core/os/os.h" |
| 36 | +#include "core/string/print_string.h" |
| 37 | + |
| 38 | +#include <errno.h> |
| 39 | +#include <fcntl.h> |
| 40 | +#include <sys/stat.h> |
| 41 | +#include <sys/types.h> |
| 42 | +#include <unistd.h> |
| 43 | + |
| 44 | +Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd) { |
| 45 | + // Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe. |
| 46 | + _close(); |
| 47 | + |
| 48 | + path_src = String(); |
| 49 | + unlink_on_close = false; |
| 50 | + ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use."); |
| 51 | + fd[0] = p_rfd; |
| 52 | + fd[1] = p_wfd; |
| 53 | + |
| 54 | + last_error = OK; |
| 55 | + return OK; |
| 56 | +} |
| 57 | + |
| 58 | +Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) { |
| 59 | + _close(); |
| 60 | + |
| 61 | + path_src = p_path; |
| 62 | + ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use."); |
| 63 | + |
| 64 | + path = String("/tmp/") + p_path.replace("pipe://", "").replace("/", "_"); |
| 65 | + struct stat st = {}; |
| 66 | + int err = stat(path.utf8().get_data(), &st); |
| 67 | + if (err) { |
| 68 | + if (mkfifo(path.utf8().get_data(), 0666) != 0) { |
| 69 | + last_error = ERR_FILE_CANT_OPEN; |
| 70 | + return last_error; |
| 71 | + } |
| 72 | + unlink_on_close = true; |
| 73 | + } else { |
| 74 | + ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file."); |
| 75 | + } |
| 76 | + |
| 77 | + int f = ::open(path.utf8().get_data(), O_RDWR | O_CLOEXEC); |
| 78 | + if (f < 0) { |
| 79 | + switch (errno) { |
| 80 | + case ENOENT: { |
| 81 | + last_error = ERR_FILE_NOT_FOUND; |
| 82 | + } break; |
| 83 | + default: { |
| 84 | + last_error = ERR_FILE_CANT_OPEN; |
| 85 | + } break; |
| 86 | + } |
| 87 | + return last_error; |
| 88 | + } |
| 89 | + |
| 90 | + // Set close on exec to avoid leaking it to subprocesses. |
| 91 | + fd[0] = f; |
| 92 | + fd[1] = f; |
| 93 | + |
| 94 | + last_error = OK; |
| 95 | + return OK; |
| 96 | +} |
| 97 | + |
| 98 | +void FileAccessUnixPipe::_close() { |
| 99 | + if (fd[0] < 0) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + if (fd[1] != fd[0]) { |
| 104 | + ::close(fd[1]); |
| 105 | + } |
| 106 | + ::close(fd[0]); |
| 107 | + fd[0] = -1; |
| 108 | + fd[1] = -1; |
| 109 | + |
| 110 | + if (unlink_on_close) { |
| 111 | + ::unlink(path.utf8().ptr()); |
| 112 | + } |
| 113 | + unlink_on_close = false; |
| 114 | +} |
| 115 | + |
| 116 | +bool FileAccessUnixPipe::is_open() const { |
| 117 | + return (fd[0] >= 0 || fd[1] >= 0); |
| 118 | +} |
| 119 | + |
| 120 | +String FileAccessUnixPipe::get_path() const { |
| 121 | + return path_src; |
| 122 | +} |
| 123 | + |
| 124 | +String FileAccessUnixPipe::get_path_absolute() const { |
| 125 | + return path_src; |
| 126 | +} |
| 127 | + |
| 128 | +uint8_t FileAccessUnixPipe::get_8() const { |
| 129 | + ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use."); |
| 130 | + |
| 131 | + uint8_t b; |
| 132 | + if (::read(fd[0], &b, 1) == 0) { |
| 133 | + last_error = ERR_FILE_CANT_READ; |
| 134 | + b = '\0'; |
| 135 | + } else { |
| 136 | + last_error = OK; |
| 137 | + } |
| 138 | + return b; |
| 139 | +} |
| 140 | + |
| 141 | +uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const { |
| 142 | + ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); |
| 143 | + ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use."); |
| 144 | + |
| 145 | + uint64_t read = ::read(fd[0], p_dst, p_length); |
| 146 | + if (read == p_length) { |
| 147 | + last_error = ERR_FILE_CANT_READ; |
| 148 | + } else { |
| 149 | + last_error = OK; |
| 150 | + } |
| 151 | + return read; |
| 152 | +} |
| 153 | + |
| 154 | +Error FileAccessUnixPipe::get_error() const { |
| 155 | + return last_error; |
| 156 | +} |
| 157 | + |
| 158 | +void FileAccessUnixPipe::store_8(uint8_t p_src) { |
| 159 | + ERR_FAIL_COND_MSG(fd[1] < 0, "Pipe must be opened before use."); |
| 160 | + if (::write(fd[1], &p_src, 1) != 1) { |
| 161 | + last_error = ERR_FILE_CANT_WRITE; |
| 162 | + } else { |
| 163 | + last_error = OK; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) { |
| 168 | + ERR_FAIL_COND_MSG(fd[1] < 0, "Pipe must be opened before use."); |
| 169 | + ERR_FAIL_COND(!p_src && p_length > 0); |
| 170 | + if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) { |
| 171 | + last_error = ERR_FILE_CANT_WRITE; |
| 172 | + } else { |
| 173 | + last_error = OK; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +void FileAccessUnixPipe::close() { |
| 178 | + _close(); |
| 179 | +} |
| 180 | + |
| 181 | +FileAccessUnixPipe::~FileAccessUnixPipe() { |
| 182 | + _close(); |
| 183 | +} |
| 184 | + |
| 185 | +#endif // UNIX_ENABLED |
0 commit comments