file_access_unix_pipe.cpp 5.0 KB

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