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
This commit is contained in:
Sergey Biryukov 2021-11-30 19:12:00 +00:00
parent 308f814105
commit 576116927b
3 changed files with 31 additions and 12 deletions

View File

@ -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'] );
}
}

View File

@ -1955,19 +1955,38 @@ function copy_dir( $from, $to, $skip_list = array() ) {
*
* @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;
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;
}

View File

@ -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.