Skip to content

Commit c6cc4fe

Browse files
authored
Merge pull request #1682 from kynx/sftp-move-and-overwrite
Fix SftpAdapter not overwriting on move
2 parents 12d77c2 + 8bcdcb7 commit c6cc4fe

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/PhpseclibV3/SftpAdapter.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,19 @@ public function move(string $source, string $destination, Config $config): void
323323
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
324324
}
325325

326-
if ( ! $connection->rename($sourceLocation, $destinationLocation)) {
327-
throw UnableToMoveFile::fromLocationTo($source, $destination);
326+
if ($connection->rename($sourceLocation, $destinationLocation)) {
327+
return;
328328
}
329+
330+
// Overwrite existing file / dir
331+
if ($connection->is_file($destinationLocation)) {
332+
$this->delete($destination);
333+
if ($connection->rename($sourceLocation, $destinationLocation)) {
334+
return;
335+
}
336+
}
337+
338+
throw UnableToMoveFile::fromLocationTo($source, $destination);
329339
}
330340

331341
public function copy(string $source, string $destination, Config $config): void

src/PhpseclibV3/SftpAdapterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use League\Flysystem\UnableToMoveFile;
1313
use League\Flysystem\UnableToReadFile;
1414
use League\Flysystem\UnableToWriteFile;
15+
use League\Flysystem\Visibility;
1516
use phpseclib3\Net\SFTP;
1617

1718
use function class_exists;
@@ -230,6 +231,37 @@ public function it_can_proactively_close_a_connection(): void
230231

231232
self::assertFalse(static::$connectionProvider->connection->isConnected());
232233
}
234+
/**
235+
* @test
236+
* @fixme Move to FilesystemAdapterTestCase once all adapters pass
237+
*/
238+
public function moving_a_file_and_overwriting(): void
239+
{
240+
$this->runScenario(function() {
241+
$adapter = $this->adapter();
242+
$adapter->write(
243+
'source.txt',
244+
'contents to be moved',
245+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
246+
);
247+
$adapter->write(
248+
'destination.txt',
249+
'contents to be overwritten',
250+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
251+
);
252+
$adapter->move('source.txt', 'destination.txt', new Config());
253+
$this->assertFalse(
254+
$adapter->fileExists('source.txt'),
255+
'After moving a file should no longer exist in the original location.'
256+
);
257+
$this->assertTrue(
258+
$adapter->fileExists('destination.txt'),
259+
'After moving, a file should be present at the new location.'
260+
);
261+
$this->assertEquals(Visibility::PUBLIC, $adapter->visibility('destination.txt')->visibility());
262+
$this->assertEquals('contents to be moved', $adapter->read('destination.txt'));
263+
});
264+
}
233265

234266
private static function connectionProvider(): StubSftpConnectionProvider
235267
{

0 commit comments

Comments
 (0)