| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  dir_access_windows.cpp                                               */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | #if defined(WINDOWS_ENABLED)
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "dir_access_windows.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/os/memory.h"
 | 
					
						
							|  |  |  | #include "core/print_string.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <wchar.h>
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 21:45:31 +01:00
										 |  |  | [03:57] <reduz> yessopie, so i don't havemak to rely on unicows | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | [03:58] <yessopie> reduz- yeah, all of the functions fail, and then you can call GetLastError () which will return 120 | 
					
						
							|  |  |  | [03:58] <drumstick> CategoryApl, hehe, what? :) | 
					
						
							|  |  |  | [03:59] <CategoryApl> didn't Verona lead to some trouble | 
					
						
							|  |  |  | [03:59] <yessopie> 120 = ERROR_CALL_NOT_IMPLEMENTED | 
					
						
							|  |  |  | [03:59] <yessopie> (you can use that constant if you include winerr.h) | 
					
						
							|  |  |  | [03:59] <CategoryApl> well answer with winning a compo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [04:02] <yessopie> if ( SetCurrentDirectoryW ( L"." ) == FALSE && GetLastError () == ERROR_CALL_NOT_IMPLEMENTED ) { use ANSI } | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct DirAccessWindowsPrivate { | 
					
						
							|  |  |  | 	HANDLE h; //handle for findfirstfile
 | 
					
						
							|  |  |  | 	WIN32_FIND_DATA f; | 
					
						
							|  |  |  | 	WIN32_FIND_DATAW fu; //unicode version
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | // CreateFolderAsync
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 09:16:41 -03:00
										 |  |  | Error DirAccessWindows::list_dir_begin() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	_cisdir = false; | 
					
						
							|  |  |  | 	_cishidden = false; | 
					
						
							| 
									
										
										
										
											2016-06-06 07:30:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	list_dir_end(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p->h = FindFirstFileExW((current_dir + "\\*").c_str(), FindExInfoStandard, &p->fu, FindExSearchNameMatch, NULL, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 01:17:16 -05:00
										 |  |  | 	return (p->h == INVALID_HANDLE_VALUE) ? ERR_CANT_OPEN : OK; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String DirAccessWindows::get_next() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p->h == INVALID_HANDLE_VALUE) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	_cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); | 
					
						
							|  |  |  | 	_cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN); | 
					
						
							| 
									
										
										
										
											2016-06-06 07:30:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String name = p->fu.cFileName; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	if (FindNextFileW(p->h, &p->fu) == 0) { | 
					
						
							|  |  |  | 		FindClose(p->h); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p->h = INVALID_HANDLE_VALUE; | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	return name; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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-09 22:10:30 -03:00
										 |  |  | void DirAccessWindows::list_dir_end() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p->h != INVALID_HANDLE_VALUE) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		FindClose(p->h); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p->h = INVALID_HANDLE_VALUE; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | int DirAccessWindows::get_drive_count() { | 
					
						
							|  |  |  | 	return drive_count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | String DirAccessWindows::get_drive(int p_drive) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_drive < 0 || p_drive >= drive_count) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return String::chr(drives[p_drive]) + ":"; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error DirAccessWindows::change_dir(String p_dir) { | 
					
						
							|  |  |  | 	GLOBAL_LOCK_FUNCTION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_dir = fix_path(p_dir); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	wchar_t real_current_dir_name[2048]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	GetCurrentDirectoryW(2048, real_current_dir_name); | 
					
						
							|  |  |  | 	String prev_dir = real_current_dir_name; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	SetCurrentDirectoryW(current_dir.c_str()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	bool worked = (SetCurrentDirectoryW(p_dir.c_str()) != 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	String base = _get_root_path(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (base != "") { | 
					
						
							|  |  |  | 		GetCurrentDirectoryW(2048, real_current_dir_name); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 		String new_dir; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		new_dir = String(real_current_dir_name).replace("\\", "/"); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 		if (!new_dir.begins_with(base)) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			worked = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	if (worked) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		GetCurrentDirectoryW(2048, real_current_dir_name); | 
					
						
							|  |  |  | 		current_dir = real_current_dir_name; // TODO, utf8 parser
 | 
					
						
							|  |  |  | 		current_dir = current_dir.replace("\\", "/"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	} //else {
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	SetCurrentDirectoryW(prev_dir.c_str()); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	//}
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return worked ? OK : ERR_INVALID_PARAMETER; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error DirAccessWindows::make_dir(String p_dir) { | 
					
						
							|  |  |  | 	GLOBAL_LOCK_FUNCTION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 	p_dir = fix_path(p_dir); | 
					
						
							| 
									
										
										
										
											2016-06-18 11:12:08 -03:00
										 |  |  | 	if (p_dir.is_rel_path()) | 
					
						
							| 
									
										
										
										
											2017-10-13 00:34:05 +02:00
										 |  |  | 		p_dir = current_dir.plus_file(p_dir); | 
					
						
							| 
									
										
										
										
											2016-06-30 18:23:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_dir = p_dir.replace("/", "\\"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	bool success; | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_dir = "\\\\?\\" + p_dir; //done according to
 | 
					
						
							|  |  |  | 	// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	success = CreateDirectoryW(p_dir.c_str(), NULL); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	err = GetLastError(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (success) { | 
					
						
							|  |  |  | 		return OK; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-14 12:35:18 +01:00
										 |  |  | 	if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ERR_ALREADY_EXISTS; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ERR_CANT_CREATE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String DirAccessWindows::get_current_dir() { | 
					
						
							|  |  |  | 	String base = _get_root_path(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (base != "") { | 
					
						
							|  |  |  | 		String bd = current_dir.replace("\\", "/").replace_first(base, ""); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		if (bd.begins_with("/")) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			return _get_root_string() + bd.substr(1, bd.length()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			return _get_root_string() + bd; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return current_dir; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 12:12:02 +01:00
										 |  |  | String DirAccessWindows::get_current_dir_without_drive() { | 
					
						
							|  |  |  | 	String dir = get_current_dir(); | 
					
						
							| 
									
										
										
										
											2020-04-01 10:22:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (_get_root_string() == "") { | 
					
						
							|  |  |  | 		int p = current_dir.find(":"); | 
					
						
							|  |  |  | 		if (p != -1) { | 
					
						
							|  |  |  | 			dir = dir.right(p + 1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dir; | 
					
						
							| 
									
										
										
										
											2020-03-06 12:12:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | bool DirAccessWindows::file_exists(String p_file) { | 
					
						
							|  |  |  | 	GLOBAL_LOCK_FUNCTION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-06 20:28:30 +08:00
										 |  |  | 	if (!p_file.is_abs_path()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_file = get_current_dir().plus_file(p_file); | 
					
						
							| 
									
										
										
										
											2014-07-06 20:28:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_file = fix_path(p_file); | 
					
						
							| 
									
										
										
										
											2016-06-06 07:30:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	//p_file.replace("/","\\");
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	//WIN32_FILE_ATTRIBUTE_DATA    fileInfo;
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	DWORD fileAttr; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	fileAttr = GetFileAttributesW(p_file.c_str()); | 
					
						
							|  |  |  | 	if (INVALID_FILE_ATTRIBUTES == fileAttr) | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-25 11:34:51 +08:00
										 |  |  | bool DirAccessWindows::dir_exists(String p_dir) { | 
					
						
							|  |  |  | 	GLOBAL_LOCK_FUNCTION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	if (p_dir.is_rel_path()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_dir = get_current_dir().plus_file(p_dir); | 
					
						
							| 
									
										
										
										
											2016-06-30 18:23:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_dir = fix_path(p_dir); | 
					
						
							| 
									
										
										
										
											2014-05-25 11:34:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	//p_dir.replace("/","\\");
 | 
					
						
							| 
									
										
										
										
											2014-05-25 11:34:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	//WIN32_FILE_ATTRIBUTE_DATA    fileInfo;
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	DWORD fileAttr; | 
					
						
							| 
									
										
										
										
											2014-05-25 11:34:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	fileAttr = GetFileAttributesW(p_dir.c_str()); | 
					
						
							|  |  |  | 	if (INVALID_FILE_ATTRIBUTES == fileAttr) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	return (fileAttr & FILE_ATTRIBUTE_DIRECTORY); | 
					
						
							| 
									
										
										
										
											2014-05-25 11:34:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error DirAccessWindows::rename(String p_path, String p_new_path) { | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 	if (p_path.is_rel_path()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_path = get_current_dir().plus_file(p_path); | 
					
						
							| 
									
										
										
										
											2016-06-30 18:23:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_path = fix_path(p_path); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_new_path.is_rel_path()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_new_path = get_current_dir().plus_file(p_new_path); | 
					
						
							| 
									
										
										
										
											2016-06-30 18:23:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_new_path = fix_path(p_new_path); | 
					
						
							| 
									
										
										
										
											2015-04-18 20:11:33 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 17:34:10 +01:00
										 |  |  | 	// If we're only changing file name case we need to do a little juggling
 | 
					
						
							|  |  |  | 	if (p_path.to_lower() == p_new_path.to_lower()) { | 
					
						
							|  |  |  | 		WCHAR tmpfile[MAX_PATH]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!GetTempFileNameW(fix_path(get_current_dir()).c_str(), NULL, 0, tmpfile)) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return FAILED; | 
					
						
							| 
									
										
										
										
											2018-02-24 17:34:10 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!::ReplaceFileW(tmpfile, p_path.c_str(), NULL, 0, NULL, NULL)) { | 
					
						
							|  |  |  | 			DeleteFileW(tmpfile); | 
					
						
							|  |  |  | 			return FAILED; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 17:34:10 +01:00
										 |  |  | 		return ::_wrename(tmpfile, p_new_path.c_str()) == 0 ? OK : FAILED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (file_exists(p_new_path)) { | 
					
						
							|  |  |  | 			if (remove(p_new_path) != OK) { | 
					
						
							|  |  |  | 				return FAILED; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return ::_wrename(p_path.c_str(), p_new_path.c_str()) == 0 ? OK : FAILED; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error DirAccessWindows::remove(String p_path) { | 
					
						
							| 
									
										
										
										
											2016-03-01 10:40:31 +00:00
										 |  |  | 	if (p_path.is_rel_path()) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_path = get_current_dir().plus_file(p_path); | 
					
						
							| 
									
										
										
										
											2016-06-30 18:23:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_path = fix_path(p_path); | 
					
						
							| 
									
										
										
										
											2016-03-01 10:40:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	DWORD fileAttr; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 13:05:34 -03:00
										 |  |  | 	fileAttr = GetFileAttributesW(p_path.c_str()); | 
					
						
							|  |  |  | 	if (INVALID_FILE_ATTRIBUTES == fileAttr) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		return FAILED; | 
					
						
							|  |  |  | 	if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) | 
					
						
							|  |  |  | 		return ::_wrmdir(p_path.c_str()) == 0 ? OK : FAILED; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		return ::_wunlink(p_path.c_str()) == 0 ? OK : FAILED; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FileType DirAccessWindows::get_file_type(const String& p_file) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wchar_t real_current_dir_name[2048]; | 
					
						
							|  |  |  | 	GetCurrentDirectoryW(2048,real_current_dir_name); | 
					
						
							|  |  |  | 	String prev_dir=real_current_dir_name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool worked SetCurrentDirectoryW(current_dir.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	DWORD attr; | 
					
						
							|  |  |  | 	if (worked) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 		WIN32_FILE_ATTRIBUTE_DATA    fileInfo; | 
					
						
							|  |  |  | 		attr = GetFileAttributesExW(p_file.c_str(), GetFileExInfoStandard, &fileInfo); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SetCurrentDirectoryW(prev_dir.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!worked) | 
					
						
							|  |  |  | 		return FILE_TYPE_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (attr&FILE_ATTRIBUTE_DIRECTORY)?FILE_TYPE_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | size_t DirAccessWindows::get_space_left() { | 
					
						
							| 
									
										
										
										
											2016-06-06 07:30:23 +02:00
										 |  |  | 	uint64_t bytes = 0; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (!GetDiskFreeSpaceEx(NULL, (PULARGE_INTEGER)&bytes, NULL, NULL)) | 
					
						
							| 
									
										
										
										
											2016-06-18 11:32:45 -03:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2016-06-06 07:30:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//this is either 0 or a value in bytes.
 | 
					
						
							|  |  |  | 	return (size_t)bytes; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 15:23:08 -03:00
										 |  |  | String DirAccessWindows::get_filesystem_type() const { | 
					
						
							| 
									
										
										
										
											2019-01-26 22:35:31 +01:00
										 |  |  | 	String path = fix_path(const_cast<DirAccessWindows *>(this)->get_current_dir()); | 
					
						
							| 
									
										
										
										
											2019-03-02 11:58:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 15:23:08 -03:00
										 |  |  | 	int unit_end = path.find(":"); | 
					
						
							| 
									
										
										
										
											2019-01-26 22:35:31 +01:00
										 |  |  | 	ERR_FAIL_COND_V(unit_end == -1, String()); | 
					
						
							|  |  |  | 	String unit = path.substr(0, unit_end + 1) + "\\"; | 
					
						
							| 
									
										
										
										
											2019-03-02 11:58:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (::GetVolumeInformationW(unit.c_str(), | 
					
						
							|  |  |  | 				szVolumeName, | 
					
						
							|  |  |  | 				sizeof(szVolumeName), | 
					
						
							|  |  |  | 				&dwSerialNumber, | 
					
						
							|  |  |  | 				&dwMaxFileNameLength, | 
					
						
							|  |  |  | 				&dwFileSystemFlags, | 
					
						
							|  |  |  | 				szFileSystemName, | 
					
						
							|  |  |  | 				sizeof(szFileSystemName)) == TRUE) { | 
					
						
							| 
									
										
										
										
											2019-01-21 15:23:08 -03:00
										 |  |  | 		return String(szFileSystemName); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_V(""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | DirAccessWindows::DirAccessWindows() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p = memnew(DirAccessWindowsPrivate); | 
					
						
							|  |  |  | 	p->h = INVALID_HANDLE_VALUE; | 
					
						
							|  |  |  | 	current_dir = "."; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	drive_count = 0; | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 18:57:35 -02:00
										 |  |  | #ifdef UWP_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Windows::Storage::StorageFolder ^ install_folder = Windows::ApplicationModel::Package::Current->InstalledLocation; | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 	change_dir(install_folder->Path->Data()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	DWORD mask = GetLogicalDrives(); | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < MAX_DRIVES; i++) { | 
					
						
							|  |  |  | 		if (mask & (1 << i)) { //DRIVE EXISTS
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-10 00:36:54 +05:30
										 |  |  | 			drives[drive_count] = 'A' + i; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			drive_count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	change_dir("."); | 
					
						
							| 
									
										
										
										
											2014-09-15 11:33:30 -03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DirAccessWindows::~DirAccessWindows() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	memdelete(p); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif //windows DirAccess support
 |