Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/wp-admin/includes/class-wp-filesystem-direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ public function copy( $source, $destination, $overwrite = false, $mode = false )
return false;
}

if ( $source === $destination ) {
return false;
}

if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination ) ) {
return false;
}
$rtval = copy( $source, $destination );

if ( $mode ) {
Expand Down Expand Up @@ -444,7 +451,7 @@ public function delete( $file, $recursive = false, $type = false ) {
* @return bool Whether $path exists or not.
*/
public function exists( $path ) {
return @file_exists( $path );
return @file_exists( $path ) || @is_link( $path );
}

/**
Expand All @@ -456,7 +463,7 @@ public function exists( $path ) {
* @return bool Whether $file is a file.
*/
public function is_file( $file ) {
return @is_file( $file );
return @is_file( $file ) || @is_link( $file );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/wp-admin/includes/class-wp-filesystem-ftpext.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ public function copy( $source, $destination, $overwrite = false, $mode = false )
return false;
}

if ( $source === $destination ) {
return false;
}

if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination ) ) {
return false;
}
$content = $this->get_contents( $source );

if ( false === $content ) {
Expand Down
7 changes: 7 additions & 0 deletions src/wp-admin/includes/class-wp-filesystem-ftpsockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ public function copy( $source, $destination, $overwrite = false, $mode = false )
return false;
}

if ( $source === $destination ) {
return false;
}

if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination ) ) {
return false;
}
$content = $this->get_contents( $source );

if ( false === $content ) {
Expand Down
15 changes: 13 additions & 2 deletions src/wp-admin/includes/class-wp-filesystem-ssh2.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,13 @@ public function copy( $source, $destination, $overwrite = false, $mode = false )
return false;
}

if ( $source === $destination ) {
return false;
}

if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination ) ) {
return false;
}
$content = $this->get_contents( $source );

if ( false === $content ) {
Expand Down Expand Up @@ -635,7 +642,9 @@ public function delete( $file, $recursive = false, $type = false ) {
* @return bool Whether $path exists or not.
*/
public function exists( $path ) {
return file_exists( $this->sftp_path( $path ) );
$path = $this->sftp_path( $path );

return file_exists( $path ) || is_link( $path );
}

/**
Expand All @@ -647,7 +656,9 @@ public function exists( $path ) {
* @return bool Whether $file is a file.
*/
public function is_file( $file ) {
return is_file( $this->sftp_path( $file ) );
$file = $this->sftp_path( $file );

return is_file( $file ) || is_link( $file );
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/tests/filesystem/wpFilesystemDirect/copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,34 @@ public function test_should_not_overwrite_an_existing_file_when_overwriting_is_d
* @ticket 57774
*/
public function test_should_not_overwrite_when_overwriting_is_enabled_and_source_and_destination_are_the_same() {

$source = self::$file_structure['test_dir']['path'] . 'a_file_that_exists.txt';
$this->assertFalse( self::$filesystem->copy( $source, $source, true ) );
}

/**
* Tests that `WP_Filesystem_Direct::copy()` replaces a broken symlink when
* overwriting is enabled.
*
* @ticket 46040
*/
public function test_should_replace_a_broken_symlink_when_overwriting_is_enabled() {
$source = self::$file_structure['visible_file']['path'];
$destination = self::$file_structure['test_dir']['path'] . '.broken_symlink';

if ( ! function_exists( 'symlink' ) ) {
$this->markTestSkipped( 'The environment does not support creating symlinks.' );
}

$created = symlink( 'does-not-exist', $destination );
if ( ! $created ) {
$this->markTestSkipped( 'The environment does not support creating symlinks.' );
}

$this->assertTrue( self::$filesystem->exists( $destination ) );
$this->assertTrue( self::$filesystem->copy( $source, $destination, true ) );
$this->assertSame( file_get_contents( $source ), file_get_contents( $destination ) );

unlink( $destination );
}
}
Loading