From d5887aeec343f162410ec45ea44cfd9a0dc9d926 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 9 Oct 2021 03:38:57 +0000 Subject: [PATCH] Upgrade/Install: Introduce a `move_dir()` function. This replaces the `copy_dir()` usage in `WP_Upgrader::install_package()` and aims to avoid PHP timeout issues when installing or updating large plugins on slower systems like Vagrant or the WP Docker test environment. The new function attempts a native PHP `rename()` function first and falls back to the previous `copy_dir()`. Follow-up to [51815], [51898]. Props afragen, aristath, peterwilsoncc, galbaras, noisysocks, pbiron. Fixes #54166. See #51857. Built from https://develop.svn.wordpress.org/trunk@51899 git-svn-id: http://core.svn.wordpress.org/trunk@51492 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-upgrader.php | 4 ++-- wp-admin/includes/file.php | 28 +++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/wp-admin/includes/class-wp-upgrader.php b/wp-admin/includes/class-wp-upgrader.php index ed9302feb2..0fab410240 100644 --- a/wp-admin/includes/class-wp-upgrader.php +++ b/wp-admin/includes/class-wp-upgrader.php @@ -623,8 +623,8 @@ class WP_Upgrader { } } - // Copy new version of item into place. - $result = copy_dir( $source, $remote_destination ); + // Move new version of item into place. + $result = move_dir( $source, $remote_destination ); if ( is_wp_error( $result ) ) { if ( $args['clear_working'] ) { $wp_filesystem->delete( $remote_source, true ); diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 8c4797d113..50345c6331 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -1917,6 +1917,34 @@ function copy_dir( $from, $to, $skip_list = array() ) { return true; } +/** + * Moves a directory from one location to another via the rename() PHP function. + * If the renaming failed, falls back to copy_dir(). + * + * Assumes that WP_Filesystem() has already been called and setup. + * + * @since 5.9.0 + * + * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. + * + * @param string $from Source directory. + * @param string $to Destination directory. + * @return true|WP_Error True on success, WP_Error on failure. + */ +function move_dir( $from, $to ) { + global $wp_filesystem; + + $wp_filesystem->rmdir( $to ); + if ( @rename( $from, $to ) ) { + return true; + } + + $wp_filesystem->mkdir( $to ); + $result = copy_dir( $from, $to ); + + return $result; +} + /** * Initializes and connects the WordPress Filesystem Abstraction classes. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 5d1051425c..d2be51350d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-51898'; +$wp_version = '5.9-alpha-51899'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.