2017-10-02 21:24:00 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* path_utils.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-02 21:24:00 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
#include "path_utils.h"
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
2021-09-23 06:56:12 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2019-07-08 13:16:35 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
#define ENV_PATH_SEP ";"
|
|
|
|
#else
|
|
|
|
#include <limits.h>
|
2019-07-08 13:16:35 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define ENV_PATH_SEP ":"
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-07-08 13:16:35 +00:00
|
|
|
namespace path {
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-08 13:16:35 +00:00
|
|
|
String cwd() {
|
|
|
|
#ifdef WINDOWS_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
const DWORD expected_size = ::GetCurrentDirectoryW(0, nullptr);
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-09-04 19:17:37 +00:00
|
|
|
Char16String buffer;
|
2019-07-08 13:16:35 +00:00
|
|
|
buffer.resize((int)expected_size);
|
2022-02-16 12:56:32 +00:00
|
|
|
if (::GetCurrentDirectoryW(expected_size, (wchar_t *)buffer.ptrw()) == 0) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return ".";
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-09-04 19:17:37 +00:00
|
|
|
String result;
|
|
|
|
if (result.parse_utf16(buffer.ptr())) {
|
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
return result.simplify_path();
|
2019-07-08 13:16:35 +00:00
|
|
|
#else
|
|
|
|
char buffer[PATH_MAX];
|
2020-07-05 17:19:36 +00:00
|
|
|
if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return ".";
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
String result;
|
2020-07-05 17:19:36 +00:00
|
|
|
if (result.parse_utf8(buffer)) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return ".";
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-08 13:16:35 +00:00
|
|
|
return result.simplify_path();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
String abspath(const String &p_path) {
|
2021-06-03 13:41:22 +00:00
|
|
|
if (p_path.is_absolute_path()) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return p_path.simplify_path();
|
|
|
|
} else {
|
|
|
|
return path::join(path::cwd(), p_path).simplify_path();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:16:35 +00:00
|
|
|
String realpath(const String &p_path) {
|
2017-10-02 21:24:00 +00:00
|
|
|
#ifdef WINDOWS_ENABLED
|
2019-07-08 13:16:35 +00:00
|
|
|
// Open file without read/write access
|
2020-07-27 10:43:20 +00:00
|
|
|
HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
|
2019-07-08 13:16:35 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
2020-04-01 23:20:12 +00:00
|
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2022-02-16 12:56:32 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return p_path;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
if (expected_size == 0) {
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
return p_path;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-09-04 19:17:37 +00:00
|
|
|
Char16String buffer;
|
2019-07-08 13:16:35 +00:00
|
|
|
buffer.resize((int)expected_size);
|
2020-09-04 19:17:37 +00:00
|
|
|
::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
::CloseHandle(hFile);
|
2020-09-04 19:17:37 +00:00
|
|
|
|
|
|
|
String result;
|
|
|
|
if (result.parse_utf16(buffer.ptr())) {
|
|
|
|
return p_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.simplify_path();
|
2019-07-08 13:16:35 +00:00
|
|
|
#elif UNIX_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (!resolved_path) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return p_path;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
String result;
|
|
|
|
bool parse_ok = result.parse_utf8(resolved_path);
|
|
|
|
::free(resolved_path);
|
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (parse_ok) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return p_path;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
return result.simplify_path();
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
|
|
|
String join(const String &p_a, const String &p_b) {
|
2020-12-15 12:04:21 +00:00
|
|
|
if (p_a.is_empty()) {
|
2019-07-08 13:16:35 +00:00
|
|
|
return p_b;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
|
2020-07-27 10:43:20 +00:00
|
|
|
const char32_t a_last = p_a[p_a.length() - 1];
|
2019-07-08 13:16:35 +00:00
|
|
|
if ((a_last == '/' || a_last == '\\') ||
|
|
|
|
(p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
|
|
|
|
return p_a + p_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_a + "/" + p_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
String join(const String &p_a, const String &p_b, const String &p_c) {
|
|
|
|
return path::join(path::join(p_a, p_b), p_c);
|
|
|
|
}
|
|
|
|
|
|
|
|
String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
|
|
|
|
return path::join(path::join(path::join(p_a, p_b), p_c), p_d);
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:12:32 +00:00
|
|
|
String relative_to_impl(const String &p_path, const String &p_relative_to) {
|
|
|
|
// This function assumes arguments are normalized and absolute paths
|
|
|
|
|
|
|
|
if (p_path.begins_with(p_relative_to)) {
|
|
|
|
return p_path.substr(p_relative_to.length() + 1);
|
|
|
|
} else {
|
|
|
|
String base_dir = p_relative_to.get_base_dir();
|
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
|
2019-12-28 18:12:32 +00:00
|
|
|
return p_path;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-12-28 18:12:32 +00:00
|
|
|
|
|
|
|
return String("..").plus_file(relative_to_impl(p_path, base_dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
String get_drive_letter(const String &p_norm_path) {
|
|
|
|
int idx = p_norm_path.find(":/");
|
2022-02-16 12:56:32 +00:00
|
|
|
if (idx != -1 && idx < p_norm_path.find("/")) {
|
2019-12-28 18:12:32 +00:00
|
|
|
return p_norm_path.substr(0, idx + 1);
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2019-12-28 18:12:32 +00:00
|
|
|
return String();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
String relative_to(const String &p_path, const String &p_relative_to) {
|
|
|
|
String relative_to_abs_norm = abspath(p_relative_to);
|
|
|
|
String path_abs_norm = abspath(p_path);
|
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
|
|
|
|
return path_abs_norm;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return relative_to_impl(path_abs_norm, relative_to_abs_norm);
|
|
|
|
}
|
2019-07-08 13:16:35 +00:00
|
|
|
} // namespace path
|