2014-02-10 02:10:30 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* dir_access_windows.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 00:50:27 +01:00
|
|
|
|
2014-09-15 16:33:30 +02:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#include "dir_access_windows.h"
|
2024-02-09 09:47:07 +01:00
|
|
|
#include "file_access_windows.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-10-07 13:36:41 +02:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/memory.h"
|
2024-05-13 13:14:32 +02:00
|
|
|
#include "core/os/os.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
2021-09-23 08:56:12 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2014-02-10 02:10:30 +01:00
|
|
|
#include <windows.h>
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2023-10-07 13:36:41 +02:00
|
|
|
typedef struct _NT_IO_STATUS_BLOCK {
|
|
|
|
union {
|
|
|
|
LONG Status;
|
|
|
|
PVOID Pointer;
|
|
|
|
} DUMMY;
|
|
|
|
ULONG_PTR Information;
|
|
|
|
} NT_IO_STATUS_BLOCK;
|
|
|
|
|
|
|
|
typedef struct _NT_FILE_CASE_SENSITIVE_INFO {
|
|
|
|
ULONG Flags;
|
|
|
|
} NT_FILE_CASE_SENSITIVE_INFO;
|
|
|
|
|
|
|
|
typedef enum _NT_FILE_INFORMATION_CLASS {
|
|
|
|
FileCaseSensitiveInformation = 71,
|
|
|
|
} NT_FILE_INFORMATION_CLASS;
|
|
|
|
|
|
|
|
#define NT_FILE_CS_FLAG_CASE_SENSITIVE_DIR 0x00000001
|
|
|
|
|
|
|
|
extern "C" NTSYSAPI LONG NTAPI NtQueryInformationFile(HANDLE FileHandle, NT_IO_STATUS_BLOCK *IoStatusBlock, PVOID FileInformation, ULONG Length, NT_FILE_INFORMATION_CLASS FileInformationClass);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
struct DirAccessWindowsPrivate {
|
2023-09-07 15:01:59 +02:00
|
|
|
HANDLE h; // handle for FindFirstFile.
|
2014-02-10 02:10:30 +01:00
|
|
|
WIN32_FIND_DATA f;
|
2023-09-07 15:01:59 +02:00
|
|
|
WIN32_FIND_DATAW fu; // Unicode version.
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
String DirAccessWindows::fix_path(const String &p_path) const {
|
2024-05-13 13:14:32 +02:00
|
|
|
String r_path = DirAccess::fix_path(p_path.trim_prefix(R"(\\?\)").replace("\\", "/"));
|
2024-08-29 14:58:59 +02:00
|
|
|
if (r_path.ends_with(":")) {
|
|
|
|
r_path += "/";
|
|
|
|
}
|
2024-05-13 13:14:32 +02:00
|
|
|
if (r_path.is_relative_path()) {
|
|
|
|
r_path = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/").path_join(r_path);
|
|
|
|
} else if (r_path == ".") {
|
|
|
|
r_path = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/");
|
|
|
|
}
|
|
|
|
r_path = r_path.simplify_path();
|
|
|
|
r_path = r_path.replace("/", "\\");
|
|
|
|
if (!r_path.is_network_share_path() && !r_path.begins_with(R"(\\?\)")) {
|
|
|
|
r_path = R"(\\?\)" + r_path;
|
2023-05-05 02:12:55 +02:00
|
|
|
}
|
|
|
|
return r_path;
|
|
|
|
}
|
|
|
|
|
2014-09-15 16:33:30 +02:00
|
|
|
// CreateFolderAsync
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
Error DirAccessWindows::list_dir_begin() {
|
2014-02-10 02:10:30 +01:00
|
|
|
_cisdir = false;
|
2015-03-21 18:33:32 +01:00
|
|
|
_cishidden = false;
|
2016-06-06 07:30:23 +02:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
list_dir_end();
|
2020-07-27 12:43:20 +02:00
|
|
|
p->h = FindFirstFileExW((LPCWSTR)(String(current_dir + "\\*").utf16().get_data()), FindExInfoStandard, &p->fu, FindExSearchNameMatch, nullptr, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-06 08:17:16 +02:00
|
|
|
if (p->h == INVALID_HANDLE_VALUE) {
|
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccessWindows::get_next() {
|
2020-07-27 12:43:20 +02:00
|
|
|
if (p->h == INVALID_HANDLE_VALUE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
_cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
_cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
|
2016-06-06 07:30:23 +02:00
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
String name = String::utf16((const char16_t *)(p->fu.cFileName));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
if (FindNextFileW(p->h, &p->fu) == 0) {
|
|
|
|
FindClose(p->h);
|
|
|
|
p->h = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
return name;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessWindows::current_is_dir() const {
|
|
|
|
return _cisdir;
|
|
|
|
}
|
|
|
|
|
2015-03-21 18:33:32 +01:00
|
|
|
bool DirAccessWindows::current_is_hidden() const {
|
|
|
|
return _cishidden;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void DirAccessWindows::list_dir_end() {
|
|
|
|
if (p->h != INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(p->h);
|
|
|
|
p->h = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int DirAccessWindows::get_drive_count() {
|
|
|
|
return drive_count;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String DirAccessWindows::get_drive(int p_drive) {
|
2020-07-27 12:43:20 +02:00
|
|
|
if (p_drive < 0 || p_drive >= drive_count) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return String::chr(drives[p_drive]) + ":";
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessWindows::change_dir(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
String dir = fix_path(p_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
Char16String real_current_dir_name;
|
|
|
|
size_t str_len = GetCurrentDirectoryW(0, nullptr);
|
|
|
|
real_current_dir_name.resize(str_len + 1);
|
|
|
|
GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
|
|
|
|
String prev_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data()));
|
2024-05-13 13:14:32 +02:00
|
|
|
bool worked = (SetCurrentDirectoryW((LPCWSTR)(dir.utf16().get_data())) != 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
String base = _get_root_path();
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!base.is_empty()) {
|
2024-05-13 13:14:32 +02:00
|
|
|
str_len = GetCurrentDirectoryW(0, nullptr);
|
|
|
|
real_current_dir_name.resize(str_len + 1);
|
|
|
|
GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
|
|
|
|
String new_dir = String::utf16((const char16_t *)real_current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace("\\", "/");
|
2015-04-19 01:11:33 +02:00
|
|
|
if (!new_dir.begins_with(base)) {
|
|
|
|
worked = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2015-04-19 01:11:33 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
if (worked) {
|
2024-05-13 13:14:32 +02:00
|
|
|
str_len = GetCurrentDirectoryW(0, nullptr);
|
|
|
|
real_current_dir_name.resize(str_len + 1);
|
|
|
|
GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
|
|
|
|
current_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data()));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
return worked ? OK : ERR_INVALID_PARAMETER;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessWindows::make_dir(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2024-02-09 09:47:07 +01:00
|
|
|
if (FileAccessWindows::is_path_invalid(p_dir)) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
WARN_PRINT("The path :" + p_dir + " is a reserved Windows system pipe, so it can't be used for creating directories.");
|
|
|
|
#endif
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
String dir = fix_path(p_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool success;
|
|
|
|
int err;
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
success = CreateDirectoryW((LPCWSTR)(dir.utf16().get_data()), nullptr);
|
2015-04-19 01:11:33 +02:00
|
|
|
err = GetLastError();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
return OK;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-14 12:35:18 +01:00
|
|
|
if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_ALREADY_EXISTS;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
String DirAccessWindows::get_current_dir(bool p_include_drive) const {
|
2024-05-13 13:14:32 +02:00
|
|
|
String cdir = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
String base = _get_root_path();
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!base.is_empty()) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String bd = cdir.replace_first(base, "");
|
2020-07-27 12:43:20 +02:00
|
|
|
if (bd.begins_with("/")) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return _get_root_string() + bd.substr(1, bd.length());
|
2020-07-27 12:43:20 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
return _get_root_string() + bd;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 09:19:29 +01:00
|
|
|
if (p_include_drive) {
|
2024-05-13 13:14:32 +02:00
|
|
|
return cdir;
|
2020-02-10 09:19:29 +01:00
|
|
|
} else {
|
2021-12-09 10:42:46 +01:00
|
|
|
if (_get_root_string().is_empty()) {
|
2024-05-13 13:14:32 +02:00
|
|
|
int pos = cdir.find(":");
|
2022-09-29 11:53:28 +02:00
|
|
|
if (pos != -1) {
|
2024-05-13 13:14:32 +02:00
|
|
|
return cdir.substr(pos + 1);
|
2020-04-01 10:30:14 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-13 13:14:32 +02:00
|
|
|
return cdir;
|
2020-02-10 09:19:29 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessWindows::file_exists(String p_file) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
String file = fix_path(p_file);
|
2016-06-06 07:30:23 +02:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
DWORD fileAttr;
|
2024-05-13 13:14:32 +02:00
|
|
|
fileAttr = GetFileAttributesW((LPCWSTR)(file.utf16().get_data()));
|
2020-07-27 12:43:20 +02:00
|
|
|
if (INVALID_FILE_ATTRIBUTES == fileAttr) {
|
2015-04-19 01:11:33 +02:00
|
|
|
return false;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 05:34:51 +02:00
|
|
|
bool DirAccessWindows::dir_exists(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
String dir = fix_path(p_dir);
|
2014-05-25 05:34:51 +02:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
DWORD fileAttr;
|
2024-05-13 13:14:32 +02:00
|
|
|
fileAttr = GetFileAttributesW((LPCWSTR)(dir.utf16().get_data()));
|
2020-07-27 12:43:20 +02:00
|
|
|
if (INVALID_FILE_ATTRIBUTES == fileAttr) {
|
2015-05-09 18:05:34 +02:00
|
|
|
return false;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2015-04-19 01:11:33 +02:00
|
|
|
return (fileAttr & FILE_ATTRIBUTE_DIRECTORY);
|
2014-05-25 05:34:51 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Error DirAccessWindows::rename(String p_path, String p_new_path) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String path = fix_path(p_path);
|
|
|
|
String new_path = fix_path(p_new_path);
|
2015-04-19 01:11:33 +02:00
|
|
|
|
2018-02-24 17:34:10 +01:00
|
|
|
// If we're only changing file name case we need to do a little juggling
|
2024-05-13 13:14:32 +02:00
|
|
|
if (path.to_lower() == new_path.to_lower()) {
|
|
|
|
if (dir_exists(path)) {
|
2020-10-25 08:05:21 +01:00
|
|
|
// The path is a dir; just rename
|
2024-05-13 13:14:32 +02:00
|
|
|
return MoveFileW((LPCWSTR)(path.utf16().get_data()), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
|
2020-10-25 08:05:21 +01:00
|
|
|
}
|
|
|
|
// The path is a file; juggle
|
2024-05-13 13:14:32 +02:00
|
|
|
// Note: do not use GetTempFileNameW, it's not long path aware!
|
|
|
|
Char16String tmpfile_utf16;
|
|
|
|
uint64_t id = OS::get_singleton()->get_ticks_usec();
|
|
|
|
while (true) {
|
|
|
|
tmpfile_utf16 = (path + itos(id++) + ".tmp").utf16();
|
|
|
|
HANDLE handle = CreateFileW((LPCWSTR)tmpfile_utf16.get_data(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(handle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (GetLastError() != ERROR_FILE_EXISTS && GetLastError() != ERROR_SHARING_VIOLATION) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-02-24 17:34:10 +01:00
|
|
|
}
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
if (!::ReplaceFileW((LPCWSTR)tmpfile_utf16.get_data(), (LPCWSTR)(path.utf16().get_data()), nullptr, 0, nullptr, nullptr)) {
|
|
|
|
DeleteFileW((LPCWSTR)tmpfile_utf16.get_data());
|
2018-02-24 17:34:10 +01:00
|
|
|
return FAILED;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
return MoveFileW((LPCWSTR)tmpfile_utf16.get_data(), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
|
2018-02-24 17:34:10 +01:00
|
|
|
|
|
|
|
} else {
|
2024-05-13 13:14:32 +02:00
|
|
|
if (file_exists(new_path)) {
|
|
|
|
if (remove(new_path) != OK) {
|
2018-02-24 17:34:10 +01:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 04:51:38 +02:00
|
|
|
return MoveFileW((LPCWSTR)(path.utf16().get_data()), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
|
2018-02-24 17:34:10 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessWindows::remove(String p_path) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String path = fix_path(p_path);
|
|
|
|
const Char16String &path_utf16 = path.utf16();
|
2016-03-01 11:40:31 +01:00
|
|
|
|
2015-05-09 18:05:34 +02:00
|
|
|
DWORD fileAttr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
fileAttr = GetFileAttributesW((LPCWSTR)(path_utf16.get_data()));
|
2020-07-27 12:43:20 +02:00
|
|
|
if (INVALID_FILE_ATTRIBUTES == fileAttr) {
|
2015-05-09 18:05:34 +02:00
|
|
|
return FAILED;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
|
|
|
if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
|
2024-05-13 13:14:32 +02:00
|
|
|
return RemoveDirectoryW((LPCWSTR)(path_utf16.get_data())) != 0 ? OK : FAILED;
|
2020-07-27 12:43:20 +02:00
|
|
|
} else {
|
2024-05-13 13:14:32 +02:00
|
|
|
return DeleteFileW((LPCWSTR)(path_utf16.get_data())) != 0 ? OK : FAILED;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t DirAccessWindows::get_space_left() {
|
2016-06-06 07:30:23 +02:00
|
|
|
uint64_t bytes = 0;
|
2020-07-27 12:43:20 +02:00
|
|
|
if (!GetDiskFreeSpaceEx(nullptr, (PULARGE_INTEGER)&bytes, nullptr, nullptr)) {
|
2016-06-18 16:32:45 +02:00
|
|
|
return 0;
|
2020-07-27 12:43:20 +02:00
|
|
|
}
|
2016-06-06 07:30:23 +02:00
|
|
|
|
2022-10-03 11:43:20 +02:00
|
|
|
// This is either 0 or a value in bytes.
|
2019-03-26 18:51:13 +01:00
|
|
|
return bytes;
|
2016-06-06 07:30:23 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-01-21 19:23:08 +01:00
|
|
|
String DirAccessWindows::get_filesystem_type() const {
|
2024-05-13 13:14:32 +02:00
|
|
|
String path = current_dir.trim_prefix(R"(\\?\)");
|
2019-03-02 15:58:42 +01:00
|
|
|
|
2022-01-24 12:12:46 +01:00
|
|
|
if (path.is_network_share_path()) {
|
|
|
|
return "Network Share";
|
|
|
|
}
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
int unit_end = path.find(":");
|
|
|
|
ERR_FAIL_COND_V(unit_end == -1, String());
|
|
|
|
String unit = path.substr(0, unit_end + 1) + "\\";
|
|
|
|
|
2019-01-26 16:27:10 +01:00
|
|
|
WCHAR szVolumeName[100];
|
|
|
|
WCHAR szFileSystemName[10];
|
2019-01-26 22:35:31 +01:00
|
|
|
DWORD dwSerialNumber = 0;
|
|
|
|
DWORD dwMaxFileNameLength = 0;
|
|
|
|
DWORD dwFileSystemFlags = 0;
|
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
if (::GetVolumeInformationW((LPCWSTR)(unit.utf16().get_data()),
|
2019-01-26 22:35:31 +01:00
|
|
|
szVolumeName,
|
|
|
|
sizeof(szVolumeName),
|
|
|
|
&dwSerialNumber,
|
|
|
|
&dwMaxFileNameLength,
|
|
|
|
&dwFileSystemFlags,
|
|
|
|
szFileSystemName,
|
|
|
|
sizeof(szFileSystemName)) == TRUE) {
|
2020-07-27 12:43:20 +02:00
|
|
|
return String::utf16((const char16_t *)szFileSystemName);
|
2019-01-21 19:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_V("");
|
|
|
|
}
|
|
|
|
|
2023-10-07 13:36:41 +02:00
|
|
|
bool DirAccessWindows::is_case_sensitive(const String &p_path) const {
|
2024-05-13 13:14:32 +02:00
|
|
|
String f = fix_path(p_path);
|
2023-10-07 13:36:41 +02:00
|
|
|
|
|
|
|
HANDLE h_file = ::CreateFileW((LPCWSTR)(f.utf16().get_data()), 0,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
|
|
|
|
|
|
|
if (h_file == INVALID_HANDLE_VALUE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NT_IO_STATUS_BLOCK io_status_block;
|
|
|
|
NT_FILE_CASE_SENSITIVE_INFO file_info;
|
|
|
|
LONG out = NtQueryInformationFile(h_file, &io_status_block, &file_info, sizeof(NT_FILE_CASE_SENSITIVE_INFO), FileCaseSensitiveInformation);
|
|
|
|
::CloseHandle(h_file);
|
|
|
|
|
|
|
|
if (out >= 0) {
|
|
|
|
return file_info.Flags & NT_FILE_CS_FLAG_CASE_SENSITIVE_DIR;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 09:48:47 +02:00
|
|
|
bool DirAccessWindows::is_link(String p_file) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String f = fix_path(p_file);
|
2024-04-24 09:48:47 +02:00
|
|
|
|
|
|
|
DWORD attr = GetFileAttributesW((LPCWSTR)(f.utf16().get_data()));
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (attr & FILE_ATTRIBUTE_REPARSE_POINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccessWindows::read_link(String p_file) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String f = fix_path(p_file);
|
2024-04-24 09:48:47 +02:00
|
|
|
|
|
|
|
HANDLE hfile = CreateFileW((LPCWSTR)(f.utf16().get_data()), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
|
|
|
if (hfile == INVALID_HANDLE_VALUE) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD ret = GetFinalPathNameByHandleW(hfile, nullptr, 0, VOLUME_NAME_DOS | FILE_NAME_NORMALIZED);
|
|
|
|
if (ret == 0) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
Char16String cs;
|
|
|
|
cs.resize(ret + 1);
|
|
|
|
GetFinalPathNameByHandleW(hfile, (LPWSTR)cs.ptrw(), ret, VOLUME_NAME_DOS | FILE_NAME_NORMALIZED);
|
|
|
|
CloseHandle(hfile);
|
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
return String::utf16((const char16_t *)cs.ptr(), ret).trim_prefix(R"(\\?\)").replace("\\", "/");
|
2024-04-24 09:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessWindows::create_link(String p_source, String p_target) {
|
2024-05-13 13:14:32 +02:00
|
|
|
String source = fix_path(p_source);
|
|
|
|
String target = fix_path(p_target);
|
2024-04-24 09:48:47 +02:00
|
|
|
|
2024-05-13 13:14:32 +02:00
|
|
|
DWORD file_attr = GetFileAttributesW((LPCWSTR)(source.utf16().get_data()));
|
2024-04-24 09:48:47 +02:00
|
|
|
bool is_dir = (file_attr & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
|
|
|
|
DWORD flags = ((is_dir) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
|
2024-05-13 13:14:32 +02:00
|
|
|
if (CreateSymbolicLinkW((LPCWSTR)target.utf16().get_data(), (LPCWSTR)source.utf16().get_data(), flags) != 0) {
|
2024-04-24 09:48:47 +02:00
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
DirAccessWindows::DirAccessWindows() {
|
|
|
|
p = memnew(DirAccessWindowsPrivate);
|
2014-09-15 16:33:30 +02:00
|
|
|
p->h = INVALID_HANDLE_VALUE;
|
2024-05-13 13:14:32 +02:00
|
|
|
|
|
|
|
Char16String real_current_dir_name;
|
|
|
|
size_t str_len = GetCurrentDirectoryW(0, nullptr);
|
|
|
|
real_current_dir_name.resize(str_len + 1);
|
|
|
|
GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
|
|
|
|
current_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DWORD mask = GetLogicalDrives();
|
2014-09-15 16:33:30 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
for (int i = 0; i < MAX_DRIVES; i++) {
|
|
|
|
if (mask & (1 << i)) { //DRIVE EXISTS
|
|
|
|
|
2019-03-09 20:06:54 +01:00
|
|
|
drives[drive_count] = 'A' + i;
|
2014-02-10 02:10:30 +01:00
|
|
|
drive_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
change_dir(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessWindows::~DirAccessWindows() {
|
2022-08-15 19:27:29 +02:00
|
|
|
list_dir_end();
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(p);
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:01:59 +02:00
|
|
|
#endif // WINDOWS_ENABLED
|