2014-02-10 02:10:30 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* dir_access.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-02-10 02:10:30 +01:00
|
|
|
#include "dir_access.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/os/os.h"
|
2022-07-20 14:10:07 +02:00
|
|
|
#include "core/templates/local_vector.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
thread_local Error DirAccess::last_dir_open_error = OK;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String DirAccess::_get_root_path() const {
|
|
|
|
switch (_access_type) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case ACCESS_RESOURCES:
|
|
|
|
return ProjectSettings::get_singleton()->get_resource_path();
|
|
|
|
case ACCESS_USERDATA:
|
|
|
|
return OS::get_singleton()->get_user_data_dir();
|
|
|
|
default:
|
|
|
|
return "";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String DirAccess::_get_root_string() const {
|
|
|
|
switch (_access_type) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case ACCESS_RESOURCES:
|
|
|
|
return "res://";
|
|
|
|
case ACCESS_USERDATA:
|
|
|
|
return "user://";
|
|
|
|
default:
|
|
|
|
return "";
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:36:35 +02:00
|
|
|
int DirAccess::get_current_drive() {
|
|
|
|
String path = get_current_dir().to_lower();
|
|
|
|
for (int i = 0; i < get_drive_count(); i++) {
|
|
|
|
String d = get_drive(i).to_lower();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (path.begins_with(d)) {
|
2015-05-20 02:36:35 +02:00
|
|
|
return i;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-05-20 02:36:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-10 09:19:29 +01:00
|
|
|
bool DirAccess::drives_are_shortcuts() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
static Error _erase_recursive(DirAccess *da) {
|
|
|
|
List<String> dirs;
|
|
|
|
List<String> files;
|
|
|
|
|
|
|
|
da->list_dir_begin();
|
|
|
|
String n = da->get_next();
|
2021-12-09 10:42:46 +01:00
|
|
|
while (!n.is_empty()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (n != "." && n != "..") {
|
2024-04-12 09:06:49 +02:00
|
|
|
if (da->current_is_dir() && !da->is_link(n)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
dirs.push_back(n);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
files.push_back(n);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
n = da->get_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
da->list_dir_end();
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const String &E : dirs) {
|
2021-07-16 05:45:57 +02:00
|
|
|
Error err = da->change_dir(E);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err == OK) {
|
|
|
|
err = _erase_recursive(da);
|
|
|
|
if (err) {
|
2017-10-02 17:01:43 +02:00
|
|
|
da->change_dir("..");
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
err = da->change_dir("..");
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2022-08-30 02:34:01 +02:00
|
|
|
err = da->remove(da->get_current_dir().path_join(E));
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const String &E : files) {
|
2022-08-30 02:34:01 +02:00
|
|
|
Error err = da->remove(da->get_current_dir().path_join(E));
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::erase_contents_recursive() {
|
|
|
|
return _erase_recursive(this);
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
Error DirAccess::make_dir_recursive(const String &p_dir) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_dir.length() < 1) {
|
|
|
|
return OK;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-09-19 23:39:50 +02:00
|
|
|
|
|
|
|
String full_dir;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-08-30 01:43:47 +02:00
|
|
|
if (p_dir.is_relative_path()) {
|
2016-06-18 16:12:08 +02:00
|
|
|
//append current
|
2022-08-30 02:34:01 +02:00
|
|
|
full_dir = get_current_dir().path_join(p_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-09-19 23:39:50 +02:00
|
|
|
} else {
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir = p_dir;
|
2014-09-19 23:39:50 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir = full_dir.replace("\\", "/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
String base;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (full_dir.begins_with("res://")) {
|
2016-06-18 16:12:08 +02:00
|
|
|
base = "res://";
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (full_dir.begins_with("user://")) {
|
2016-06-18 16:12:08 +02:00
|
|
|
base = "user://";
|
2022-01-24 12:12:46 +01:00
|
|
|
} else if (full_dir.is_network_share_path()) {
|
|
|
|
int pos = full_dir.find("/", 2);
|
|
|
|
ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER);
|
|
|
|
pos = full_dir.find("/", pos + 1);
|
|
|
|
ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER);
|
|
|
|
base = full_dir.substr(0, pos + 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (full_dir.begins_with("/")) {
|
2016-06-18 16:12:08 +02:00
|
|
|
base = "/";
|
2022-02-03 17:03:38 +01:00
|
|
|
} else if (full_dir.contains(":/")) {
|
2016-06-18 16:12:08 +02:00
|
|
|
base = full_dir.substr(0, full_dir.find(":/") + 2);
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir = full_dir.replace_first(base, "").simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
Vector<String> subdirs = full_dir.split("/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
String curpath = base;
|
|
|
|
for (int i = 0; i < subdirs.size(); i++) {
|
2022-08-30 02:34:01 +02:00
|
|
|
curpath = curpath.path_join(subdirs[i]);
|
2016-06-18 16:12:08 +02:00
|
|
|
Error err = make_dir(curpath);
|
|
|
|
if (err != OK && err != ERR_ALREADY_EXISTS) {
|
2021-01-10 02:13:25 +01:00
|
|
|
ERR_FAIL_V_MSG(err, "Could not create directory: " + curpath);
|
2016-06-18 16:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
DirAccess::AccessType DirAccess::get_access_type() const {
|
|
|
|
return _access_type;
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
String DirAccess::fix_path(const String &p_path) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
switch (_access_type) {
|
|
|
|
case ACCESS_RESOURCES: {
|
2017-07-19 22:00:46 +02:00
|
|
|
if (ProjectSettings::get_singleton()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_path.begins_with("res://")) {
|
2017-07-19 22:00:46 +02:00
|
|
|
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!resource_path.is_empty()) {
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("res:/", resource_path);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("res://", "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_USERDATA: {
|
|
|
|
if (p_path.begins_with("user://")) {
|
2017-11-17 15:25:22 +01:00
|
|
|
String data_dir = OS::get_singleton()->get_user_data_dir();
|
2021-12-09 10:42:46 +01:00
|
|
|
if (!data_dir.is_empty()) {
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("user:/", data_dir);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("user://", "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_FILESYSTEM: {
|
|
|
|
return p_path;
|
|
|
|
} break;
|
2020-05-10 13:00:47 +02:00
|
|
|
case ACCESS_MAX:
|
|
|
|
break; // Can't happen, but silences warning
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return p_path;
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { nullptr, nullptr, nullptr };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> DirAccess::create_for_path(const String &p_path) {
|
|
|
|
Ref<DirAccess> da;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_path.begins_with("res://")) {
|
|
|
|
da = create(ACCESS_RESOURCES);
|
|
|
|
} else if (p_path.begins_with("user://")) {
|
|
|
|
da = create(ACCESS_USERDATA);
|
|
|
|
} else {
|
|
|
|
da = create(ACCESS_FILESYSTEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> DirAccess::open(const String &p_path, Error *r_error) {
|
|
|
|
Ref<DirAccess> da = create_for_path(p_path);
|
|
|
|
ERR_FAIL_COND_V_MSG(da.is_null(), nullptr, "Cannot create DirAccess for path '" + p_path + "'.");
|
2014-02-10 02:10:30 +01:00
|
|
|
Error err = da->change_dir(p_path);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_error) {
|
2014-02-10 02:10:30 +01:00
|
|
|
*r_error = err;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err != OK) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
Ref<DirAccess> DirAccess::_open(const String &p_path) {
|
|
|
|
Error err = OK;
|
|
|
|
Ref<DirAccess> da = open(p_path, &err);
|
|
|
|
last_dir_open_error = err;
|
|
|
|
if (err) {
|
|
|
|
return Ref<DirAccess>();
|
|
|
|
}
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:10:36 +02:00
|
|
|
int DirAccess::_get_drive_count() {
|
|
|
|
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
return d->get_drive_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccess::get_drive_name(int p_idx) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
return d->get_drive(p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::make_dir_absolute(const String &p_dir) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create_for_path(p_dir);
|
|
|
|
return d->make_dir(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::make_dir_recursive_absolute(const String &p_dir) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create_for_path(p_dir);
|
|
|
|
return d->make_dir_recursive(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccess::dir_exists_absolute(const String &p_dir) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create_for_path(p_dir);
|
|
|
|
return d->dir_exists(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
// Support copying from res:// to user:// etc.
|
|
|
|
String from = ProjectSettings::get_singleton()->globalize_path(p_from);
|
|
|
|
String to = ProjectSettings::get_singleton()->globalize_path(p_to);
|
|
|
|
return d->copy(from, to, p_chmod_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::rename_absolute(const String &p_from, const String &p_to) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
String from = ProjectSettings::get_singleton()->globalize_path(p_from);
|
|
|
|
String to = ProjectSettings::get_singleton()->globalize_path(p_to);
|
|
|
|
return d->rename(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::remove_absolute(const String &p_path) {
|
|
|
|
Ref<DirAccess> d = DirAccess::create_for_path(p_path);
|
|
|
|
return d->remove(p_path);
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> DirAccess::create(AccessType p_access) {
|
|
|
|
Ref<DirAccess> da = create_func[p_access] ? create_func[p_access]() : nullptr;
|
|
|
|
if (da.is_valid()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
da->_access_type = p_access;
|
2022-03-06 03:48:41 +01:00
|
|
|
|
|
|
|
// for ACCESS_RESOURCES and ACCESS_FILESYSTEM, current_dir already defaults to where game was started
|
|
|
|
// in case current directory is force changed elsewhere for ACCESS_RESOURCES
|
|
|
|
if (p_access == ACCESS_RESOURCES) {
|
|
|
|
da->change_dir("res://");
|
|
|
|
} else if (p_access == ACCESS_USERDATA) {
|
|
|
|
da->change_dir("user://");
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
Error DirAccess::get_open_error() {
|
|
|
|
return last_dir_open_error;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String DirAccess::get_full_path(const String &p_path, AccessType p_access) {
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> d = DirAccess::create(p_access);
|
|
|
|
if (d.is_null()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return p_path;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
d->change_dir(p_path);
|
|
|
|
String full = d->get_current_dir();
|
|
|
|
return full;
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
Error DirAccess::copy(const String &p_from, const String &p_to, int p_chmod_flags) {
|
2014-02-10 02:10:30 +01:00
|
|
|
//printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
|
|
|
|
Error err;
|
2022-04-12 09:12:40 +02:00
|
|
|
{
|
|
|
|
Ref<FileAccess> fsrc = FileAccess::open(p_from, FileAccess::READ, &err);
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to open " + p_from);
|
|
|
|
|
|
|
|
Ref<FileAccess> fdst = FileAccess::open(p_to, FileAccess::WRITE, &err);
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to open " + p_to);
|
|
|
|
|
2022-07-20 14:10:07 +02:00
|
|
|
const size_t copy_buffer_limit = 65536; // 64 KB
|
|
|
|
|
2022-04-12 09:12:40 +02:00
|
|
|
fsrc->seek_end(0);
|
2022-10-19 08:27:40 +02:00
|
|
|
uint64_t size = fsrc->get_position();
|
2022-04-12 09:12:40 +02:00
|
|
|
fsrc->seek(0);
|
|
|
|
err = OK;
|
2022-07-20 14:10:07 +02:00
|
|
|
size_t buffer_size = MIN(size * sizeof(uint8_t), copy_buffer_limit);
|
|
|
|
LocalVector<uint8_t> buffer;
|
|
|
|
buffer.resize(buffer_size);
|
|
|
|
while (size > 0) {
|
2022-04-12 09:12:40 +02:00
|
|
|
if (fsrc->get_error() != OK) {
|
|
|
|
err = fsrc->get_error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fdst->get_error() != OK) {
|
|
|
|
err = fdst->get_error();
|
|
|
|
break;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2022-07-20 14:10:07 +02:00
|
|
|
int bytes_read = fsrc->get_buffer(buffer.ptr(), buffer_size);
|
|
|
|
if (bytes_read <= 0) {
|
|
|
|
err = FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fdst->store_buffer(buffer.ptr(), bytes_read);
|
|
|
|
|
|
|
|
size -= bytes_read;
|
2022-04-12 09:12:40 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-01-04 05:17:30 +01:00
|
|
|
if (err == OK && p_chmod_flags != -1) {
|
2019-04-07 20:46:52 +02:00
|
|
|
err = FileAccess::set_unix_permissions(p_to, p_chmod_flags);
|
2017-12-06 20:25:41 +01:00
|
|
|
// If running on a platform with no chmod support (i.e., Windows), don't fail
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err == ERR_UNAVAILABLE) {
|
2017-12-06 20:25:41 +01:00
|
|
|
err = OK;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-09-17 19:40:58 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:01:43 +02:00
|
|
|
// Changes dir for the current scope, returning back to the original dir
|
|
|
|
// when scope exits
|
|
|
|
class DirChanger {
|
|
|
|
DirAccess *da;
|
|
|
|
String original_dir;
|
|
|
|
|
|
|
|
public:
|
2024-01-09 02:36:19 +01:00
|
|
|
DirChanger(DirAccess *p_da, const String &p_dir) :
|
2018-12-08 21:07:33 +01:00
|
|
|
da(p_da),
|
|
|
|
original_dir(p_da->get_current_dir()) {
|
2017-10-02 17:01:43 +02:00
|
|
|
p_da->change_dir(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DirChanger() {
|
|
|
|
da->change_dir(original_dir);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
Error DirAccess::_copy_dir(Ref<DirAccess> &p_target_da, const String &p_to, int p_chmod_flags, bool p_copy_links) {
|
2017-10-02 17:01:43 +02:00
|
|
|
List<String> dirs;
|
|
|
|
|
|
|
|
String curdir = get_current_dir();
|
|
|
|
list_dir_begin();
|
|
|
|
String n = get_next();
|
2021-12-09 10:42:46 +01:00
|
|
|
while (!n.is_empty()) {
|
2017-10-02 17:01:43 +02:00
|
|
|
if (n != "." && n != "..") {
|
2022-08-30 02:34:01 +02:00
|
|
|
if (p_copy_links && is_link(get_current_dir().path_join(n))) {
|
|
|
|
create_link(read_link(get_current_dir().path_join(n)), p_to + n);
|
2021-03-10 11:55:31 +01:00
|
|
|
} else if (current_is_dir()) {
|
2017-10-02 17:01:43 +02:00
|
|
|
dirs.push_back(n);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2019-06-26 15:08:25 +02:00
|
|
|
const String &rel_path = n;
|
2021-08-30 01:43:47 +02:00
|
|
|
if (!n.is_relative_path()) {
|
2017-10-02 17:01:43 +02:00
|
|
|
list_dir_end();
|
|
|
|
return ERR_BUG;
|
|
|
|
}
|
2022-08-30 02:34:01 +02:00
|
|
|
Error err = copy(get_current_dir().path_join(n), p_to + rel_path, p_chmod_flags);
|
2017-10-02 17:01:43 +02:00
|
|
|
if (err) {
|
|
|
|
list_dir_end();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = get_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
list_dir_end();
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const String &rel_path : dirs) {
|
2017-10-02 17:01:43 +02:00
|
|
|
String target_dir = p_to + rel_path;
|
|
|
|
if (!p_target_da->dir_exists(target_dir)) {
|
|
|
|
Error err = p_target_da->make_dir(target_dir);
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + target_dir + "'.");
|
2017-10-02 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
Error err = change_dir(rel_path);
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot change current directory to '" + rel_path + "'.");
|
2019-09-25 10:28:50 +02:00
|
|
|
|
2021-03-10 11:55:31 +01:00
|
|
|
err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags, p_copy_links);
|
2017-10-02 17:01:43 +02:00
|
|
|
if (err) {
|
|
|
|
change_dir("..");
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_V_MSG(err, "Failed to copy recursively.");
|
2017-10-02 17:01:43 +02:00
|
|
|
}
|
|
|
|
err = change_dir("..");
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to go back.");
|
2017-10-02 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
Error DirAccess::copy_dir(const String &p_from, String p_to, int p_chmod_flags, bool p_copy_links) {
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!dir_exists(p_from), ERR_FILE_NOT_FOUND, "Source directory doesn't exist.");
|
2017-10-02 17:01:43 +02:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> target_da = DirAccess::create_for_path(p_to);
|
|
|
|
ERR_FAIL_COND_V_MSG(target_da.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_to + "'.");
|
2017-10-02 17:01:43 +02:00
|
|
|
|
|
|
|
if (!target_da->dir_exists(p_to)) {
|
|
|
|
Error err = target_da->make_dir_recursive(p_to);
|
2019-09-25 10:28:50 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + p_to + "'.");
|
2017-10-02 17:01:43 +02:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:19:11 +02:00
|
|
|
if (!p_to.ends_with("/")) {
|
|
|
|
p_to = p_to + "/";
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:01:43 +02:00
|
|
|
DirChanger dir_changer(this, p_from);
|
2021-03-10 11:55:31 +01:00
|
|
|
Error err = _copy_dir(target_da, p_to, p_chmod_flags, p_copy_links);
|
2017-10-02 17:01:43 +02:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:36:19 +01:00
|
|
|
bool DirAccess::exists(const String &p_dir) {
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> da = DirAccess::create_for_path(p_dir);
|
2022-03-10 15:27:09 +01:00
|
|
|
return da->change_dir(p_dir) == OK;
|
2015-04-12 21:45:59 +02:00
|
|
|
}
|
2022-09-05 13:01:31 +02:00
|
|
|
|
|
|
|
PackedStringArray DirAccess::get_files() {
|
|
|
|
return _get_contents(false);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:10:36 +02:00
|
|
|
PackedStringArray DirAccess::get_files_at(const String &p_path) {
|
|
|
|
Ref<DirAccess> da = DirAccess::open(p_path);
|
2022-09-22 09:13:07 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path));
|
2022-04-21 13:10:36 +02:00
|
|
|
return da->get_files();
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
PackedStringArray DirAccess::get_directories() {
|
|
|
|
return _get_contents(true);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:10:36 +02:00
|
|
|
PackedStringArray DirAccess::get_directories_at(const String &p_path) {
|
|
|
|
Ref<DirAccess> da = DirAccess::open(p_path);
|
2022-09-22 09:13:07 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path));
|
2022-04-21 13:10:36 +02:00
|
|
|
return da->get_directories();
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
PackedStringArray DirAccess::_get_contents(bool p_directories) {
|
|
|
|
PackedStringArray ret;
|
|
|
|
|
|
|
|
list_dir_begin();
|
|
|
|
String s = _get_next();
|
|
|
|
while (!s.is_empty()) {
|
|
|
|
if (current_is_dir() == p_directories) {
|
|
|
|
ret.append(s);
|
|
|
|
}
|
|
|
|
s = _get_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.sort();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccess::_get_next() {
|
|
|
|
String next = get_next();
|
|
|
|
while (!next.is_empty() && ((!include_navigational && (next == "." || next == "..")) || (!include_hidden && current_is_hidden()))) {
|
|
|
|
next = get_next();
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirAccess::set_include_navigational(bool p_enable) {
|
|
|
|
include_navigational = p_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccess::get_include_navigational() const {
|
|
|
|
return include_navigational;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirAccess::set_include_hidden(bool p_enable) {
|
|
|
|
include_hidden = p_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccess::get_include_hidden() const {
|
|
|
|
return include_hidden;
|
|
|
|
}
|
|
|
|
|
2023-10-07 13:36:41 +02:00
|
|
|
bool DirAccess::is_case_sensitive(const String &p_path) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
void DirAccess::_bind_methods() {
|
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("open", "path"), &DirAccess::_open);
|
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("get_open_error"), &DirAccess::get_open_error);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("list_dir_begin"), &DirAccess::list_dir_begin, DEFVAL(false), DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_next"), &DirAccess::_get_next);
|
|
|
|
ClassDB::bind_method(D_METHOD("current_is_dir"), &DirAccess::current_is_dir);
|
|
|
|
ClassDB::bind_method(D_METHOD("list_dir_end"), &DirAccess::list_dir_end);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_files"), &DirAccess::get_files);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("get_files_at", "path"), &DirAccess::get_files_at);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_directories"), &DirAccess::get_directories);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("get_directories_at", "path"), &DirAccess::get_directories_at);
|
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_count"), &DirAccess::_get_drive_count);
|
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_name", "idx"), &DirAccess::get_drive_name);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_drive"), &DirAccess::get_current_drive);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("change_dir", "to_dir"), &DirAccess::change_dir);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_dir", "include_drive"), &DirAccess::get_current_dir, DEFVAL(true));
|
|
|
|
ClassDB::bind_method(D_METHOD("make_dir", "path"), &DirAccess::make_dir);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_absolute", "path"), &DirAccess::make_dir_absolute);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &DirAccess::make_dir_recursive);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_recursive_absolute", "path"), &DirAccess::make_dir_recursive_absolute);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("file_exists", "path"), &DirAccess::file_exists);
|
|
|
|
ClassDB::bind_method(D_METHOD("dir_exists", "path"), &DirAccess::dir_exists);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("dir_exists_absolute", "path"), &DirAccess::dir_exists_absolute);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_space_left"), &DirAccess::get_space_left);
|
|
|
|
ClassDB::bind_method(D_METHOD("copy", "from", "to", "chmod_flags"), &DirAccess::copy, DEFVAL(-1));
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("copy_absolute", "from", "to", "chmod_flags"), &DirAccess::copy_absolute, DEFVAL(-1));
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("rename", "from", "to"), &DirAccess::rename);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("rename_absolute", "from", "to"), &DirAccess::rename_absolute);
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("remove", "path"), &DirAccess::remove);
|
2022-04-21 13:10:36 +02:00
|
|
|
ClassDB::bind_static_method("DirAccess", D_METHOD("remove_absolute", "path"), &DirAccess::remove_absolute);
|
2022-09-05 13:01:31 +02:00
|
|
|
|
2024-04-24 09:48:47 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("is_link", "path"), &DirAccess::is_link);
|
|
|
|
ClassDB::bind_method(D_METHOD("read_link", "path"), &DirAccess::read_link);
|
|
|
|
ClassDB::bind_method(D_METHOD("create_link", "source", "target"), &DirAccess::create_link);
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_include_navigational", "enable"), &DirAccess::set_include_navigational);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_include_navigational"), &DirAccess::get_include_navigational);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_include_hidden", "enable"), &DirAccess::set_include_hidden);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_include_hidden"), &DirAccess::get_include_hidden);
|
|
|
|
|
2023-10-07 13:36:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("is_case_sensitive", "path"), &DirAccess::is_case_sensitive);
|
|
|
|
|
2022-09-05 13:01:31 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_navigational"), "set_include_navigational", "get_include_navigational");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_hidden"), "set_include_hidden", "get_include_hidden");
|
|
|
|
}
|