From 6202d2c3ed4e3d5ef3e56b0f8a4bc6fff0968161 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 17 Jul 2014 11:22:59 -0500 Subject: [PATCH 1/3] SFTP: add unit test for stat cache fix --- tests/Functional/Net/SFTPUserStoryTest.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index bdf8e7e3..cd2c017f 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -342,7 +342,21 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase } /** - * @depends testSortOrder + * on older versions this would result in a fatal error + * @depends testReadlink + * @group github402 + */ + public function testStatcacheFix($sftp) + { + $sftp->mkdir('testdir'); + $sftp->chdir('testdir'); + $sftp->touch('testdir'); + $sftp->chdir('..'); + $sftp->delete('testdir', true); + } + + /** + * @depends testStatcacheFix */ public function testChDirUpHome($sftp) { From 41ae2fb1300d7f7470e6ea3c9db104956128d107 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 17 Jul 2014 11:35:58 -0500 Subject: [PATCH 2/3] SFTP: return $sftp object in new unit test method --- tests/Functional/Net/SFTPUserStoryTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index cd2c017f..4a2656ed 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -353,6 +353,8 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase $sftp->touch('testdir'); $sftp->chdir('..'); $sftp->delete('testdir', true); + + return $sftp; } /** From a84861bb52992c89dc7bf1db9adc4fbafe2a8c1e Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 19 Jul 2014 21:31:58 -0500 Subject: [PATCH 3/3] SFTP: stat cache fixes --- phpseclib/Net/SFTP.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index aaa8d71b..d7151b58 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -1061,11 +1061,12 @@ class Net_SFTP extends Net_SSH2 $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; - foreach ($dirs as $dir) { + $max = count($dirs) - 1; + foreach ($dirs as $i=>$dir) { if (!isset($temp[$dir])) { $temp[$dir] = array(); } - if ($dir == end($dirs)) { + if ($i === $max) { $temp[$dir] = $value; break; } @@ -1085,8 +1086,9 @@ class Net_SFTP extends Net_SSH2 $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; - foreach ($dirs as $dir) { - if ($dir == end($dirs)) { + $max = count($dirs) - 1; + foreach ($dirs as $i=>$dir) { + if ($i === $max) { unset($temp[$dir]); return true; } @@ -2206,7 +2208,7 @@ class Net_SFTP extends Net_SSH2 $result = $this->_query_stat_cache($path); if (isset($result)) { - // return true if $result is an array or if it's int(1) + // return true if $result is an array or if it's an stdClass object return $result !== false; } }