From 6bc3513914e028c41314bf5e30132c2e4bfca07b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 20 Jun 2015 19:33:19 -0500 Subject: [PATCH 1/2] SFTP: stat cache updates --- phpseclib/Net/SFTP.php | 70 +++++++++++++++++----- tests/Functional/Net/SFTPUserStoryTest.php | 37 +++++++++++- 2 files changed, 90 insertions(+), 17 deletions(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index dac16506..90708304 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -901,7 +901,7 @@ class Net_SFTP extends Net_SSH2 } else { $temp = $dir . '/' . $shortname; } - $this->_update_stat_cache($temp, (object) $attributes); + $this->_update_stat_cache($temp, (object) array('stat' => $attributes)); } // SFTPv6 has an optional boolean end-of-list field, but we'll ignore that, since the // final SSH_FXP_STATUS packet should tell us that, already. @@ -1072,6 +1072,14 @@ class Net_SFTP extends Net_SSH2 $temp[$dir] = array(); } if ($i === $max) { + if (is_object($temp[$dir])) { + if (!isset($value->stat) && isset($temp[$dir]->stat)) { + $value->stat = $temp[$dir]->stat; + } + if (!isset($value->lstat) && isset($temp[$dir]->lstat)) { + $value->lstat = $temp[$dir]->lstat; + } + } $temp[$dir] = $value; break; } @@ -1150,10 +1158,10 @@ class Net_SFTP extends Net_SSH2 if ($this->use_stat_cache) { $result = $this->_query_stat_cache($filename); if (is_array($result) && isset($result['.'])) { - return (array) $result['.']; + return $result['.']->stat; } - if (is_object($result)) { - return (array) $result; + if (is_object($result) && isset($result->stat)) { + return $result->stat; } } @@ -1166,7 +1174,7 @@ class Net_SFTP extends Net_SSH2 if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } - $this->_update_stat_cache($filename, (object) $stat); + $this->_update_stat_cache($filename, (object) array('stat' => $stat)); return $stat; } @@ -1179,7 +1187,7 @@ class Net_SFTP extends Net_SSH2 if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } - $this->_update_stat_cache($filename, (object) $stat); + $this->_update_stat_cache($filename, (object) array('stat' => $stat)); return $stat; } @@ -1207,10 +1215,10 @@ class Net_SFTP extends Net_SSH2 if ($this->use_stat_cache) { $result = $this->_query_stat_cache($filename); if (is_array($result) && isset($result['.'])) { - return (array) $result['.']; + return $result['.']->lstat; } - if (is_object($result)) { - return (array) $result; + if (is_object($result) && isset($result->lstat)) { + return $result->lstat; } } @@ -1223,7 +1231,7 @@ class Net_SFTP extends Net_SSH2 if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } - $this->_update_stat_cache($filename, (object) $lstat); + $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $lstat; } @@ -1231,7 +1239,7 @@ class Net_SFTP extends Net_SSH2 if ($lstat != $stat) { $lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK)); - $this->_update_stat_cache($filename, (object) $lstat); + $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $stat; } @@ -1244,7 +1252,7 @@ class Net_SFTP extends Net_SSH2 if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) { $filename.= '/.'; } - $this->_update_stat_cache($filename, (object) $lstat); + $this->_update_stat_cache($filename, (object) array('lstat' => $lstat)); return $lstat; } @@ -2303,7 +2311,7 @@ class Net_SFTP extends Net_SSH2 */ function is_link($path) { - $result = $this->_get_stat_cache_prop($path, 'type'); + $result = $this->_get_lstat_cache_prop($path, 'type'); if ($result === false) { return false; } @@ -2418,18 +2426,48 @@ class Net_SFTP extends Net_SSH2 * @access private */ function _get_stat_cache_prop($path, $prop) + { + return $this->_get_xstat_cache_prop($path, $prop, 'stat'); + } + + /** + * Return an lstat properity + * + * Uses cache if appropriate. + * + * @param String $path + * @param String $prop + * @return Mixed + * @access private + */ + function _get_lstat_cache_prop($path, $prop) + { + return $this->_get_xstat_cache_prop($path, $prop, 'lstat'); + } + + /** + * Return a stat or lstat properity + * + * Uses cache if appropriate. + * + * @param String $path + * @param String $prop + * @return Mixed + * @access private + */ + function _get_xstat_cache_prop($path, $prop, $type) { if ($this->use_stat_cache) { $path = $this->_realpath($path); $result = $this->_query_stat_cache($path); - if (is_object($result) && isset($result->$prop)) { - return $result->$prop; + if (is_object($result) && isset($result->$type)) { + return $result->{$type}[$prop]; } } - $result = $this->stat($path); + $result = $this->$type($path); if ($result === false || !isset($result[$prop])) { return false; diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index 687ef4c6..730c2dbf 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -167,7 +167,6 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase return $sftp; } - /** * @depends testStatOnDir */ @@ -382,6 +381,42 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase /** * @depends testSymlink */ + public function testStatLstatCache($sftp) + { + $stat = $sftp->stat('symlink'); + $lstat = $sftp->lstat('symlink'); + $this->assertNotEquals( + $stat, $lstat, + 'Failed asserting that stat and lstat returned different output for a symlink' + ); + + return $sftp; + } + + /** + * @depends testStatLstatCache + */ + public function testLinkFile($sftp) + { + $this->assertTrue( + $sftp->is_link('symlink'), + 'Failed asserting that symlink is a link' + ); + $this->assertTrue( + $sftp->is_file('symlink'), + 'Failed asserting that symlink is a file' + ); + $this->assertFalse( + $sftp->is_dir('symlink'), + 'Failed asserting that symlink is not a directory' + ); + + return $sftp; + } + + /** + * @depends testLinkFile + */ public function testReadlink($sftp) { $this->assertInternalType('string', $sftp->readlink('symlink'), From 5385104d2c9bdcaa0d88765edd11edb0b2fd4b47 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 20 Jun 2015 20:46:30 -0500 Subject: [PATCH 2/2] Unit/X509/CSR: looks like a line got overwritten.. --- tests/Unit/File/X509/CSRTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/File/X509/CSRTest.php b/tests/Unit/File/X509/CSRTest.php index fe1ab36d..7a91262b 100644 --- a/tests/Unit/File/X509/CSRTest.php +++ b/tests/Unit/File/X509/CSRTest.php @@ -62,7 +62,7 @@ secxBTTCNgI48YezK3GDkn65cmlnkt6F6Mf0MwoDaXTuB88Jycbwb5ihKnHEJIsO draiRBZruwMPwPIP -----END NEW CERTIFICATE REQUEST-----'; - $x509 = new File_X509(); + $x509 = new X509(); $csr = $x509->loadCSR($test);