From 9eb20e37c8dadf79a8cdae65e720f90b2651b8ed Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 23 Mar 2021 10:16:26 +0200 Subject: [PATCH] Fixed `Folder::move()` deleting the folder if you move folder into itself, created empty file instead --- CHANGELOG.md | 3 ++- system/src/Grav/Common/Filesystem/Folder.php | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7c9a4772..8fd756d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ 1. [](#bugfix) * Ignore errors when using `set_time_limit` in `Archiver` and `GPM\Response` classes [#3023](https://github.com/getgrav/grav/issues/3023) - * Fixed moving page to itself causing the page folder to be lost (converted to an empty file) + * Fixed `Folder::move()` deleting the folder if you move folder into itself, created empty file instead + * Fixed moving `Flex Page` to itself causing the page to be lost [#3227](https://github.com/getgrav/grav/issues/3227) * Fixed `PageStorage` detecting files as pages # v1.7.9 diff --git a/system/src/Grav/Common/Filesystem/Folder.php b/system/src/Grav/Common/Filesystem/Folder.php index ef09cee76..6a3783b91 100644 --- a/system/src/Grav/Common/Filesystem/Folder.php +++ b/system/src/Grav/Common/Filesystem/Folder.php @@ -371,6 +371,10 @@ abstract class Folder return; } + if (strpos($target, $source) === 0) { + throw new RuntimeException('Cannot move folder to itself'); + } + if (file_exists($target)) { // Rename fails if target folder exists. throw new RuntimeException('Cannot move files to existing folder/file.'); @@ -383,11 +387,7 @@ abstract class Folder @rename($source, $target); // Rename function can fail while still succeeding, so let's check if the folder exists. - if (!file_exists($target) || !is_dir($target)) { - // In some rare cases rename() creates file, not a folder. Get rid of it. - if (file_exists($target)) { - @unlink($target); - } + if (is_dir($source)) { // Rename doesn't support moving folders across filesystems. Use copy instead. self::copy($source, $target); self::delete($source);