Path: blob/master/drivers/unix/file_access_unix_pipe.cpp
9903 views
/**************************************************************************/1/* file_access_unix_pipe.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "file_access_unix_pipe.h"3132#if defined(UNIX_ENABLED)3334#include "core/os/os.h"35#include "core/string/print_string.h"3637#include <fcntl.h>38#include <sys/ioctl.h>39#include <sys/stat.h>40#include <sys/types.h>41#include <unistd.h>42#include <cerrno>4344Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd, bool p_blocking) {45// Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.46_close();4748path_src = String();49unlink_on_close = false;50ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");51fd[0] = p_rfd;52fd[1] = p_wfd;5354if (!p_blocking) {55fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);56fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL) | O_NONBLOCK);57}5859last_error = OK;60return OK;61}6263Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {64_close();6566path_src = p_path;67ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");6869path = String("/tmp/") + p_path.replace("pipe://", "").replace_char('/', '_');70const CharString path_utf8 = path.utf8();7172struct stat st = {};73int err = stat(path_utf8.get_data(), &st);74if (err) {75if (mkfifo(path_utf8.get_data(), 0600) != 0) {76last_error = ERR_FILE_CANT_OPEN;77return last_error;78}79unlink_on_close = true;80} else {81ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");82}8384int f = ::open(path_utf8.get_data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);85if (f < 0) {86switch (errno) {87case ENOENT: {88last_error = ERR_FILE_NOT_FOUND;89} break;90default: {91last_error = ERR_FILE_CANT_OPEN;92} break;93}94return last_error;95}9697// Set close on exec to avoid leaking it to subprocesses.98fd[0] = f;99fd[1] = f;100101last_error = OK;102return OK;103}104105void FileAccessUnixPipe::_close() {106if (fd[0] < 0) {107return;108}109110if (fd[1] != fd[0]) {111::close(fd[1]);112}113::close(fd[0]);114fd[0] = -1;115fd[1] = -1;116117if (unlink_on_close) {118::unlink(path.utf8().ptr());119}120unlink_on_close = false;121}122123bool FileAccessUnixPipe::is_open() const {124return (fd[0] >= 0 || fd[1] >= 0);125}126127String FileAccessUnixPipe::get_path() const {128return path_src;129}130131String FileAccessUnixPipe::get_path_absolute() const {132return path_src;133}134135uint64_t FileAccessUnixPipe::get_length() const {136ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");137138int buf_rem = 0;139ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);140return buf_rem;141}142143uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {144ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");145ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);146147ssize_t read = ::read(fd[0], p_dst, p_length);148if (read == -1) {149last_error = ERR_FILE_CANT_READ;150read = 0;151} else if (read != (ssize_t)p_length) {152last_error = ERR_FILE_CANT_READ;153} else {154last_error = OK;155}156return read;157}158159Error FileAccessUnixPipe::get_error() const {160return last_error;161}162163bool FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {164ERR_FAIL_COND_V_MSG(fd[1] < 0, false, "Pipe must be opened before use.");165ERR_FAIL_COND_V(!p_src && p_length > 0, false);166167if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) {168last_error = ERR_FILE_CANT_WRITE;169return false;170} else {171last_error = OK;172return true;173}174}175176void FileAccessUnixPipe::close() {177_close();178}179180FileAccessUnixPipe::~FileAccessUnixPipe() {181_close();182}183184#endif // UNIX_ENABLED185186187