Make WordPress Core


Ignore:
Timestamp:
11/30/2021 07:10:31 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r52242 r52289  
    19541954 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
    19551955 *
    1956  * @param string $from Source directory.
    1957  * @param string $to   Destination directory.
     1956 * @param string $from        Source directory.
     1957 * @param string $to          Destination directory.
     1958 * @param string $working_dir Optional. Remote file source directory.
     1959 *                            Default empty string.
    19581960 * @return true|WP_Error True on success, WP_Error on failure.
    19591961 */
    1960 function move_dir( $from, $to ) {
     1962function move_dir( $from, $to, $working_dir = '' ) {
    19611963    global $wp_filesystem;
    19621964
    1963     $wp_filesystem->rmdir( $to );
    1964     if ( @rename( $from, $to ) ) {
    1965         return true;
    1966     }
    1967 
    1968     $wp_filesystem->mkdir( $to );
     1965    if ( 'direct' === $wp_filesystem->method ) {
     1966        $wp_filesystem->rmdir( $to );
     1967        if ( @rename( $from, $to ) ) {
     1968            return true;
     1969        }
     1970    }
     1971
     1972    if ( ! $wp_filesystem->is_dir( $to ) ) {
     1973        if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) {
     1974
     1975            // Clear the working directory?
     1976            if ( ! empty( $working_dir ) ) {
     1977                $wp_filesystem->delete( $working_dir, true );
     1978            }
     1979
     1980            return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to );
     1981        }
     1982    }
    19691983    $result = copy_dir( $from, $to );
     1984
     1985    // Clear the working directory?
     1986    if ( ! empty( $working_dir ) ) {
     1987        $wp_filesystem->delete( $working_dir, true );
     1988    }
    19701989
    19711990    return $result;
Note: See TracChangeset for help on using the changeset viewer.