diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 103a7cd5..3c746565 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -1033,10 +1033,13 @@ class Net_SFTP extends Net_SSH2 $stat = $this->_stat($filename, NET_SFTP_STAT); if ($stat === false) { - $this->_update_stat_cache($filename, 0); + $this->_remove_from_stat_cache($filename); return false; } if (isset($stat['type'])) { + if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { + $filename.= '/.'; + } $this->_update_stat_cache($filename, (object) $stat); return $stat; } @@ -1047,6 +1050,9 @@ class Net_SFTP extends Net_SSH2 NET_SFTP_TYPE_REGULAR; $this->pwd = $pwd; + if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { + $filename.= '/.'; + } $this->_update_stat_cache($filename, (object) $stat); return $stat; @@ -1084,9 +1090,13 @@ class Net_SFTP extends Net_SSH2 $lstat = $this->_stat($filename, NET_SFTP_LSTAT); if ($lstat === false) { + $this->_remove_from_stat_cache($filename); return false; } if (isset($lstat['type'])) { + if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { + $filename.= '/.'; + } $this->_update_stat_cache($filename, (object) $lstat); return $lstat; } @@ -1105,6 +1115,9 @@ class Net_SFTP extends Net_SSH2 NET_SFTP_TYPE_REGULAR; $this->pwd = $pwd; + if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { + $filename.= '/.'; + } $this->_update_stat_cache($filename, (object) $lstat); return $lstat; diff --git a/tests/Net/SFTPFunctionalTest.php b/tests/Net/SFTPFunctionalTest.php index 4f524be8..9c43b55b 100644 --- a/tests/Net/SFTPFunctionalTest.php +++ b/tests/Net/SFTPFunctionalTest.php @@ -19,6 +19,7 @@ class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase 'This test hangs on Travis CI on PHP 5.3.3 and below.' ); } + parent::setUpBeforeClass(); self::$scratchDir = uniqid('phpseclib-sftp-scratch-'); @@ -126,6 +127,20 @@ class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase /** * @depends testChDirScratch */ + public function testStatOnDir($sftp) + { + $this->assertNotSame( + array(), + $sftp->stat('.'), + 'Failed asserting that the cwd has a non-empty stat.' + ); + + return $sftp; + } + + /** + * @depends testStatOnDir + */ public function testPutSizeGetFile($sftp) { $this->assertTrue( @@ -151,6 +166,19 @@ class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase /** * @depends testPutSizeGetFile */ + public function testChDirOnFile($sftp) + { + $this->assertFalse( + $sftp->chdir('file1.txt'), + 'Failed to assert that the cwd cannot be changed to a file' + ); + + return $sftp; + } + + /** + * @depends testChDirOnFile + */ public function testFileExistsIsFileIsDirFile($sftp) { $this->assertTrue(