Make WordPress Core

Ticket #36710: 36710.3.diff

File 36710.3.diff, 2.5 KB (added by pbiron, 4 years ago)

adds Windows-specific behavior for deleting a symlink that points to a directory.

  • src/wp-admin/includes/class-wp-filesystem-direct.php

    diff --git a/src/wp-admin/includes/class-wp-filesystem-direct.php b/src/wp-admin/includes/class-wp-filesystem-direct.php
    index 9b1757cc65..9a7efec0a2 100644
    a b class WP_Filesystem_Direct extends WP_Filesystem_Base { 
    332332                }
    333333                $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
    334334
    335                 if ( 'f' == $type || $this->is_file( $file ) ) {
    336                         return @unlink( $file );
     335                if ( 'f' == $type || $this->is_file( $file ) || $this->is_link( $file ) ) {
     336                        if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) &&
     337                                        $this->is_link( $file ) && $this->is_dir( $file ) ) {
     338                                // on windows unlink()'ing a symlink that points to a directory fails.
     339                                // @link https://bugs.php.net/bug.php?id=52176
     340                                return @rmdir( $file );
     341                        }
     342                        else {
     343                                // rtrim() needed to remove symlinks successfully.
     344                                return @unlink( rtrim( $file, '/' ) );
     345                        }
    337346                }
    338347                if ( ! $recursive && $this->is_dir( $file ) ) {
    339348                        return @rmdir( $file );
    class WP_Filesystem_Direct extends WP_Filesystem_Base { 
    383392                return @is_file( $file );
    384393        }
    385394
     395        /**
     396         * Checks if resource is a symbolic link.
     397         *
     398         * @since 5.3.0
     399         *
     400         * @param string $file File path.
     401         * @return bool Whether $file is a symbolic link.
     402         */
     403        public function is_link( $file ) {
     404                // Strip trailing slashes, to avoid directory symlinks resolving.
     405                return @is_link( rtrim( $file, '/' ) );
     406        }
     407
    386408        /**
    387409         * Checks if resource is a directory.
    388410         *
    class WP_Filesystem_Direct extends WP_Filesystem_Base { 
    557579         * }
    558580         */
    559581        public function dirlist( $path, $include_hidden = true, $recursive = false ) {
    560                 if ( $this->is_file( $path ) ) {
     582                if ( $this->is_dir( $path ) && $this->is_link( $path ) ) {
     583                        // Directory is symlink, therefore return no listing.
     584                        return array();
     585                }
     586                elseif ( $this->is_file( $path ) || $this->is_link( $path ) ) {
    561587                        $limit_file = basename( $path );
    562588                        $path       = dirname( $path );
    563589                } else {
    class WP_Filesystem_Direct extends WP_Filesystem_Base { 
    602628                        $struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
    603629                        $struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
    604630
    605                         if ( 'd' == $struc['type'] ) {
     631                        if ( 'd' == $struc['type'] && ! $this->is_link( $path . '/' . $struc['name'] ) ) {
    606632                                if ( $recursive ) {
    607633                                        $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
    608634                                } else {