godot/core/io/dir_access.cpp

427 lines
12 KiB
C++
Raw Normal View History

2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* dir_access.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 09:10:30 +08: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. */
/*************************************************************************/
2014-02-10 09:10:30 +08:00
#include "dir_access.h"
#include "core/config/project_settings.h"
#include "core/io/file_access.h"
#include "core/os/memory.h"
#include "core/os/os.h"
#include "core/templates/local_vector.h"
2014-02-10 09:10:30 +08:00
String DirAccess::_get_root_path() const {
switch (_access_type) {
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 09:10:30 +08:00
}
}
2014-02-10 09:10:30 +08:00
String DirAccess::_get_root_string() const {
switch (_access_type) {
case ACCESS_RESOURCES:
return "res://";
case ACCESS_USERDATA:
return "user://";
default:
return "";
2014-02-10 09:10:30 +08: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();
if (path.begins_with(d)) {
return i;
}
}
return 0;
}
2014-02-10 09:10:30 +08:00
bool DirAccess::drives_are_shortcuts() {
return false;
}
2014-02-10 09:10:30 +08:00
static Error _erase_recursive(DirAccess *da) {
List<String> dirs;
List<String> files;
da->list_dir_begin();
String n = da->get_next();
while (!n.is_empty()) {
if (n != "." && n != "..") {
if (da->current_is_dir()) {
2014-02-10 09:10:30 +08:00
dirs.push_back(n);
} else {
2014-02-10 09:10:30 +08:00
files.push_back(n);
}
2014-02-10 09:10:30 +08:00
}
n = da->get_next();
2014-02-10 09:10:30 +08:00
}
da->list_dir_end();
for (const String &E : dirs) {
Error err = da->change_dir(E);
if (err == OK) {
2014-02-10 09:10:30 +08:00
err = _erase_recursive(da);
if (err) {
da->change_dir("..");
2014-02-10 09:10:30 +08:00
return err;
}
err = da->change_dir("..");
if (err) {
return err;
}
err = da->remove(da->get_current_dir().plus_file(E));
2014-02-10 09:10:30 +08:00
if (err) {
return err;
}
} else {
return err;
}
}
for (const String &E : files) {
Error err = da->remove(da->get_current_dir().plus_file(E));
2014-02-10 09:10:30 +08:00
if (err) {
return err;
}
}
return OK;
}
Error DirAccess::erase_contents_recursive() {
return _erase_recursive(this);
}
Error DirAccess::make_dir_recursive(String p_dir) {
if (p_dir.length() < 1) {
return OK;
}
String full_dir;
2014-02-10 09:10:30 +08:00
if (p_dir.is_relative_path()) {
//append current
full_dir = get_current_dir().plus_file(p_dir);
2014-02-10 09:10:30 +08:00
} else {
full_dir = p_dir;
}
2014-02-10 09:10:30 +08:00
full_dir = full_dir.replace("\\", "/");
2014-02-10 09:10:30 +08:00
String base;
2014-02-10 09:10:30 +08:00
if (full_dir.begins_with("res://")) {
base = "res://";
} else if (full_dir.begins_with("user://")) {
base = "user://";
} 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);
} else if (full_dir.begins_with("/")) {
base = "/";
2022-02-04 00:03:38 +08:00
} else if (full_dir.contains(":/")) {
base = full_dir.substr(0, full_dir.find(":/") + 2);
} else {
ERR_FAIL_V(ERR_INVALID_PARAMETER);
}
2014-02-10 09:10:30 +08:00
full_dir = full_dir.replace_first(base, "").simplify_path();
2014-02-10 09:10:30 +08:00
Vector<String> subdirs = full_dir.split("/");
2014-02-10 09:10:30 +08:00
String curpath = base;
for (int i = 0; i < subdirs.size(); i++) {
curpath = curpath.plus_file(subdirs[i]);
Error err = make_dir(curpath);
if (err != OK && err != ERR_ALREADY_EXISTS) {
ERR_FAIL_V_MSG(err, "Could not create directory: " + curpath);
}
}
2014-02-10 09:10:30 +08:00
return OK;
}
2014-02-10 09:10:30 +08:00
DirAccess::AccessType DirAccess::get_access_type() const {
return _access_type;
}
2014-02-10 09:10:30 +08:00
String DirAccess::fix_path(String p_path) const {
switch (_access_type) {
2014-02-10 09:10:30 +08:00
case ACCESS_RESOURCES: {
if (ProjectSettings::get_singleton()) {
2014-02-10 09:10:30 +08:00
if (p_path.begins_with("res://")) {
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
if (!resource_path.is_empty()) {
return p_path.replace_first("res:/", resource_path);
}
return p_path.replace_first("res://", "");
2014-02-10 09:10:30 +08:00
}
}
} break;
case ACCESS_USERDATA: {
if (p_path.begins_with("user://")) {
String data_dir = OS::get_singleton()->get_user_data_dir();
if (!data_dir.is_empty()) {
return p_path.replace_first("user:/", data_dir);
}
return p_path.replace_first("user://", "");
2014-02-10 09:10:30 +08:00
}
} break;
case ACCESS_FILESYSTEM: {
return p_path;
} break;
case ACCESS_MAX:
break; // Can't happen, but silences warning
2014-02-10 09:10:30 +08:00
}
return p_path;
}
2020-04-02 07:20:12 +08:00
DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { nullptr, nullptr, nullptr };
2014-02-10 09:10:30 +08:00
Ref<DirAccess> DirAccess::create_for_path(const String &p_path) {
Ref<DirAccess> da;
2014-02-10 09:10:30 +08:00
if (p_path.begins_with("res://")) {
da = create(ACCESS_RESOURCES);
2014-02-10 09:10:30 +08:00
} else if (p_path.begins_with("user://")) {
da = create(ACCESS_USERDATA);
2014-02-10 09:10:30 +08:00
} else {
da = create(ACCESS_FILESYSTEM);
2014-02-10 09:10:30 +08:00
}
return da;
}
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 09:10:30 +08:00
Error err = da->change_dir(p_path);
if (r_error) {
*r_error = err;
}
if (err != OK) {
2020-04-02 07:20:12 +08:00
return nullptr;
2014-02-10 09:10:30 +08:00
}
return da;
}
Ref<DirAccess> DirAccess::create(AccessType p_access) {
Ref<DirAccess> da = create_func[p_access] ? create_func[p_access]() : nullptr;
if (da.is_valid()) {
da->_access_type = p_access;
// 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 09:10:30 +08:00
}
return da;
}
2014-02-10 09:10:30 +08:00
String DirAccess::get_full_path(const String &p_path, AccessType p_access) {
Ref<DirAccess> d = DirAccess::create(p_access);
if (d.is_null()) {
2014-02-10 09:10:30 +08:00
return p_path;
}
2016-03-09 07:00:52 +08:00
2014-02-10 09:10:30 +08:00
d->change_dir(p_path);
String full = d->get_current_dir();
2014-02-10 09:10:30 +08:00
return full;
}
Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) {
2014-02-10 09:10:30 +08:00
//printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
Error err;
{
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);
const size_t copy_buffer_limit = 65536; // 64 KB
fsrc->seek_end(0);
int size = fsrc->get_position();
fsrc->seek(0);
err = OK;
size_t buffer_size = MIN(size * sizeof(uint8_t), copy_buffer_limit);
LocalVector<uint8_t> buffer;
buffer.resize(buffer_size);
while (size > 0) {
if (fsrc->get_error() != OK) {
err = fsrc->get_error();
break;
}
if (fdst->get_error() != OK) {
err = fdst->get_error();
break;
}
2016-03-09 07:00:52 +08: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;
}
2014-02-10 09:10:30 +08:00
}
2016-03-09 07:00:52 +08:00
if (err == OK && p_chmod_flags != -1) {
err = FileAccess::set_unix_permissions(p_to, p_chmod_flags);
// If running on a platform with no chmod support (i.e., Windows), don't fail
if (err == ERR_UNAVAILABLE) {
err = OK;
}
}
2014-02-10 09:10:30 +08:00
return err;
}
// Changes dir for the current scope, returning back to the original dir
// when scope exits
class DirChanger {
DirAccess *da;
String original_dir;
public:
DirChanger(DirAccess *p_da, String p_dir) :
da(p_da),
original_dir(p_da->get_current_dir()) {
p_da->change_dir(p_dir);
}
~DirChanger() {
da->change_dir(original_dir);
}
};
Error DirAccess::_copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod_flags, bool p_copy_links) {
List<String> dirs;
String curdir = get_current_dir();
list_dir_begin();
String n = get_next();
while (!n.is_empty()) {
if (n != "." && n != "..") {
if (p_copy_links && is_link(get_current_dir().plus_file(n))) {
create_link(read_link(get_current_dir().plus_file(n)), p_to + n);
} else if (current_is_dir()) {
dirs.push_back(n);
} else {
2019-06-26 21:08:25 +08:00
const String &rel_path = n;
if (!n.is_relative_path()) {
list_dir_end();
return ERR_BUG;
}
Error err = copy(get_current_dir().plus_file(n), p_to + rel_path, p_chmod_flags);
if (err) {
list_dir_end();
return err;
}
}
}
n = get_next();
}
list_dir_end();
for (const String &rel_path : dirs) {
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 16:28:50 +08:00
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + target_dir + "'.");
}
Error err = change_dir(rel_path);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot change current directory to '" + rel_path + "'.");
2019-09-25 16:28:50 +08:00
err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags, p_copy_links);
if (err) {
change_dir("..");
2019-09-25 16:28:50 +08:00
ERR_FAIL_V_MSG(err, "Failed to copy recursively.");
}
err = change_dir("..");
2019-09-25 16:28:50 +08:00
ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to go back.");
}
return OK;
}
Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags, bool p_copy_links) {
2019-09-25 16:28:50 +08:00
ERR_FAIL_COND_V_MSG(!dir_exists(p_from), ERR_FILE_NOT_FOUND, "Source directory doesn't exist.");
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 + "'.");
if (!target_da->dir_exists(p_to)) {
Error err = target_da->make_dir_recursive(p_to);
2019-09-25 16:28:50 +08:00
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + p_to + "'.");
}
if (!p_to.ends_with("/")) {
p_to = p_to + "/";
}
DirChanger dir_changer(this, p_from);
Error err = _copy_dir(target_da, p_to, p_chmod_flags, p_copy_links);
return err;
}
bool DirAccess::exists(String p_dir) {
Ref<DirAccess> da = DirAccess::create_for_path(p_dir);
return da->change_dir(p_dir) == OK;
}