Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SftpAdapter not overwriting on move #1682

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/PhpseclibV3/SftpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,19 @@ public function move(string $source, string $destination, Config $config): void
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
}

if ( ! $connection->rename($sourceLocation, $destinationLocation)) {
throw UnableToMoveFile::fromLocationTo($source, $destination);
if ($connection->rename($sourceLocation, $destinationLocation)) {
return;
}

// Overwrite existing file / dir
if ($connection->is_file($destinationLocation)) {
$this->delete($destination);
if ($connection->rename($sourceLocation, $destinationLocation)) {
return;
}
}

throw UnableToMoveFile::fromLocationTo($source, $destination);
}

public function copy(string $source, string $destination, Config $config): void
Expand Down
32 changes: 32 additions & 0 deletions src/PhpseclibV3/SftpAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use phpseclib3\Net\SFTP;

use function class_exists;
Expand Down Expand Up @@ -230,6 +231,37 @@ public function it_can_proactively_close_a_connection(): void

self::assertFalse(static::$connectionProvider->connection->isConnected());
}
/**
* @test
* @fixme Move to FilesystemAdapterTestCase once all adapters pass
*/
public function moving_a_file_and_overwriting(): void
{
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
'contents to be moved',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->write(
'destination.txt',
'contents to be overwritten',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->move('source.txt', 'destination.txt', new Config());
$this->assertFalse(
$adapter->fileExists('source.txt'),
'After moving a file should no longer exist in the original location.'
);
$this->assertTrue(
$adapter->fileExists('destination.txt'),
'After moving, a file should be present at the new location.'
);
$this->assertEquals(Visibility::PUBLIC, $adapter->visibility('destination.txt')->visibility());
$this->assertEquals('contents to be moved', $adapter->read('destination.txt'));
});
}

private static function connectionProvider(): StubSftpConnectionProvider
{
Expand Down