path_utils.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* path_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "path_utils.h"
  31. #include "core/os/dir_access.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #ifdef WINDOWS_ENABLED
  36. #include <windows.h>
  37. #define ENV_PATH_SEP ";"
  38. #else
  39. #include <limits.h>
  40. #include <unistd.h>
  41. #define ENV_PATH_SEP ":"
  42. #endif
  43. #include <stdlib.h>
  44. namespace path {
  45. String find_executable(const String &p_name) {
  46. #ifdef WINDOWS_ENABLED
  47. Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
  48. #endif
  49. Vector<String> env_path = OS::get_singleton()->get_environment("PATH").split(ENV_PATH_SEP, false);
  50. if (env_path.empty())
  51. return String();
  52. for (int i = 0; i < env_path.size(); i++) {
  53. String p = path::join(env_path[i], p_name);
  54. #ifdef WINDOWS_ENABLED
  55. for (int j = 0; j < exts.size(); j++) {
  56. String p2 = p + exts[j].to_lower(); // lowercase to reduce risk of case mismatch warning
  57. if (FileAccess::exists(p2))
  58. return p2;
  59. }
  60. #else
  61. if (FileAccess::exists(p))
  62. return p;
  63. #endif
  64. }
  65. return String();
  66. }
  67. String cwd() {
  68. #ifdef WINDOWS_ENABLED
  69. const DWORD expected_size = ::GetCurrentDirectoryW(0, NULL);
  70. String buffer;
  71. buffer.resize((int)expected_size);
  72. if (::GetCurrentDirectoryW(expected_size, buffer.ptrw()) == 0)
  73. return ".";
  74. return buffer.simplify_path();
  75. #else
  76. char buffer[PATH_MAX];
  77. if (::getcwd(buffer, sizeof(buffer)) == NULL)
  78. return ".";
  79. String result;
  80. if (result.parse_utf8(buffer))
  81. return ".";
  82. return result.simplify_path();
  83. #endif
  84. }
  85. String abspath(const String &p_path) {
  86. if (p_path.is_abs_path()) {
  87. return p_path.simplify_path();
  88. } else {
  89. return path::join(path::cwd(), p_path).simplify_path();
  90. }
  91. }
  92. String realpath(const String &p_path) {
  93. #ifdef WINDOWS_ENABLED
  94. // Open file without read/write access
  95. HANDLE hFile = ::CreateFileW(p_path.c_str(), 0,
  96. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  97. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  98. if (hFile == INVALID_HANDLE_VALUE)
  99. return p_path;
  100. const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, NULL, 0, FILE_NAME_NORMALIZED);
  101. if (expected_size == 0) {
  102. ::CloseHandle(hFile);
  103. return p_path;
  104. }
  105. String buffer;
  106. buffer.resize((int)expected_size);
  107. ::GetFinalPathNameByHandleW(hFile, buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
  108. ::CloseHandle(hFile);
  109. return buffer.simplify_path();
  110. #elif UNIX_ENABLED
  111. char *resolved_path = ::realpath(p_path.utf8().get_data(), NULL);
  112. if (!resolved_path)
  113. return p_path;
  114. String result;
  115. bool parse_ok = result.parse_utf8(resolved_path);
  116. ::free(resolved_path);
  117. if (parse_ok)
  118. return p_path;
  119. return result.simplify_path();
  120. #endif
  121. }
  122. String join(const String &p_a, const String &p_b) {
  123. if (p_a.empty())
  124. return p_b;
  125. const CharType a_last = p_a[p_a.length() - 1];
  126. if ((a_last == '/' || a_last == '\\') ||
  127. (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
  128. return p_a + p_b;
  129. }
  130. return p_a + "/" + p_b;
  131. }
  132. String join(const String &p_a, const String &p_b, const String &p_c) {
  133. return path::join(path::join(p_a, p_b), p_c);
  134. }
  135. String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
  136. return path::join(path::join(path::join(p_a, p_b), p_c), p_d);
  137. }
  138. String relative_to_impl(const String &p_path, const String &p_relative_to) {
  139. // This function assumes arguments are normalized and absolute paths
  140. if (p_path.begins_with(p_relative_to)) {
  141. return p_path.substr(p_relative_to.length() + 1);
  142. } else {
  143. String base_dir = p_relative_to.get_base_dir();
  144. if (base_dir.length() <= 2 && (base_dir.empty() || base_dir.ends_with(":")))
  145. return p_path;
  146. return String("..").plus_file(relative_to_impl(p_path, base_dir));
  147. }
  148. }
  149. #ifdef WINDOWS_ENABLED
  150. String get_drive_letter(const String &p_norm_path) {
  151. int idx = p_norm_path.find(":/");
  152. if (idx != -1 && idx < p_norm_path.find("/"))
  153. return p_norm_path.substr(0, idx + 1);
  154. return String();
  155. }
  156. #endif
  157. String relative_to(const String &p_path, const String &p_relative_to) {
  158. String relative_to_abs_norm = abspath(p_relative_to);
  159. String path_abs_norm = abspath(p_path);
  160. #ifdef WINDOWS_ENABLED
  161. if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
  162. return path_abs_norm;
  163. }
  164. #endif
  165. return relative_to_impl(path_abs_norm, relative_to_abs_norm);
  166. }
  167. } // namespace path