Skip to content

Commit

Permalink
Merge pull request #1682 from kynx/sftp-move-and-overwrite
Browse files Browse the repository at this point in the history
Fix SftpAdapter not overwriting on move
  • Loading branch information
frankdejonge authored Aug 11, 2024
2 parents 12d77c2 + 8bcdcb7 commit c6cc4fe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
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

0 comments on commit c6cc4fe

Please sign in to comment.