From 576116927ba6a58dcc4073d4d94cdd960973f9fa Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 30 Nov 2021 19:12:00 +0000 Subject: [PATCH] Upgrade/Install: Make some adjustments to the `move_dir()` function: * Check for direct PHP flle access and only use `rename()` if true. * Check whether the destination directory was successfully created. * Clear the working directory so there is internal parity within the function between the results of a successful `rename()` and a fallback to `copy_dir()`. * Use `move_dir()` in `WP_Upgrader::move_to_temp_backup_dir()` and `::restore_temp_backup()`. Follow-up to [51815], [51898], [51899], [51902], [52192], [52284]. Props afragen, peterwilsoncc, dd32, SergeyBiryukov. See #54166, #51857. Built from https://develop.svn.wordpress.org/trunk@52289 git-svn-id: http://core.svn.wordpress.org/trunk@51881 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/class-wp-upgrader.php | 8 +++--- wp-admin/includes/file.php | 33 +++++++++++++++++++------ wp-includes/version.php | 2 +- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/wp-admin/includes/class-wp-upgrader.php b/wp-admin/includes/class-wp-upgrader.php index 9487dc6cd4..396672585d 100644 --- a/wp-admin/includes/class-wp-upgrader.php +++ b/wp-admin/includes/class-wp-upgrader.php @@ -628,7 +628,7 @@ class WP_Upgrader { } // Move new version of item into place. - $result = move_dir( $source, $remote_destination ); + $result = move_dir( $source, $remote_destination, $remote_source ); if ( is_wp_error( $result ) ) { if ( $args['clear_working'] ) { $wp_filesystem->delete( $remote_source, true ); @@ -636,7 +636,7 @@ class WP_Upgrader { return $result; } - // Clear the working folder? + // Clear the working directory? if ( $args['clear_working'] ) { $wp_filesystem->delete( $remote_source, true ); } @@ -1047,7 +1047,7 @@ class WP_Upgrader { } // Move to the temp-backup directory. - if ( ! $wp_filesystem->move( $src, $dest, true ) ) { + if ( ! move_dir( $src, $dest ) ) { return new WP_Error( 'fs_temp_backup_move', $this->strings['temp_backup_move_failed'] ); } @@ -1081,7 +1081,7 @@ class WP_Upgrader { } // Move it. - if ( ! $wp_filesystem->move( $src, $dest, true ) ) { + if ( ! move_dir( $src, $dest ) ) { return new WP_Error( 'fs_temp_backup_delete', $this->strings['temp_backup_restore_failed'] ); } } diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 2eb0111d09..6dbb8f514a 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -1953,21 +1953,40 @@ function copy_dir( $from, $to, $skip_list = array() ) { * * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. * - * @param string $from Source directory. - * @param string $to Destination directory. + * @param string $from Source directory. + * @param string $to Destination directory. + * @param string $working_dir Optional. Remote file source directory. + * Default empty string. * @return true|WP_Error True on success, WP_Error on failure. */ -function move_dir( $from, $to ) { +function move_dir( $from, $to, $working_dir = '' ) { global $wp_filesystem; - $wp_filesystem->rmdir( $to ); - if ( @rename( $from, $to ) ) { - return true; + if ( 'direct' === $wp_filesystem->method ) { + $wp_filesystem->rmdir( $to ); + if ( @rename( $from, $to ) ) { + return true; + } } - $wp_filesystem->mkdir( $to ); + if ( ! $wp_filesystem->is_dir( $to ) ) { + if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) { + + // Clear the working directory? + if ( ! empty( $working_dir ) ) { + $wp_filesystem->delete( $working_dir, true ); + } + + return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to ); + } + } $result = copy_dir( $from, $to ); + // Clear the working directory? + if ( ! empty( $working_dir ) ) { + $wp_filesystem->delete( $working_dir, true ); + } + return $result; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 5164063e77..42fa03f631 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52288'; +$wp_version = '5.9-alpha-52289'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.