From 0dde3e5b59741630547a3ac6685d1125ab207b91 Mon Sep 17 00:00:00 2001 From: kobewi Date: Tue, 17 Aug 2021 14:15:51 +0200 Subject: [PATCH] Fix renaming directories --- core/core_bind.cpp | 6 ++++-- doc/classes/Directory.xml | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/core_bind.cpp b/core/core_bind.cpp index f3f51e57ee2..49c22ecc723 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1560,15 +1560,17 @@ Error _Directory::copy(String p_from, String p_to) { Error _Directory::rename(String p_from, String p_to) { ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use."); + ERR_FAIL_COND_V_MSG(p_from.is_empty() || p_from == "." || p_from == "..", ERR_INVALID_PARAMETER, "Invalid path to rename."); + if (!p_from.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_from); - ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist."); + ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist."); Error err = d->rename(p_from, p_to); memdelete(d); return err; } - ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist."); + ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist."); return d->rename(p_from, p_to); } diff --git a/doc/classes/Directory.xml b/doc/classes/Directory.xml index 919960cc9fd..e8e5a286b49 100644 --- a/doc/classes/Directory.xml +++ b/doc/classes/Directory.xml @@ -187,7 +187,7 @@ - Renames (move) the [code]from[/code] file to the [code]to[/code] destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten. + Renames (move) the [code]from[/code] file or directory to the [code]to[/code] destination. Both arguments should be paths to files or directories, either relative or absolute. If the destination file or directory exists and is not access-protected, it will be overwritten. Returns one of the [enum Error] code constants ([code]OK[/code] on success).