1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-12 09:09:39 +01:00

Merge branch 'master' into php5

* master:
  SFTP: stat cache updates
  SFTP: Rudimentary tests for file_exists(), is_file(), is_dir().
This commit is contained in:
Andreas Fischer 2014-05-21 17:55:42 +02:00
commit 4524b9f663
2 changed files with 92 additions and 37 deletions

View File

@ -877,7 +877,12 @@ class Net_SFTP extends Net_SSH2
if (isset($attributes['type']) && $attributes['type'] == NET_SFTP_TYPE_DIRECTORY && ($shortname != '.' && $shortname != '..')) { if (isset($attributes['type']) && $attributes['type'] == NET_SFTP_TYPE_DIRECTORY && ($shortname != '.' && $shortname != '..')) {
$this->_update_stat_cache($dir . '/' . $shortname, array()); $this->_update_stat_cache($dir . '/' . $shortname, array());
} else { } else {
$this->_update_stat_cache($dir . '/' . $shortname, (object) $attributes); if ($shortname == '..') {
$temp = $this->_realpath($dir . '/..') . '/.';
} else {
$temp = $dir . '/' . $shortname;
}
$this->_update_stat_cache($temp, (object) $attributes);
} }
// SFTPv6 has an optional boolean end-of-list field, but we'll ignore that, since the // 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. // final SSH_FXP_STATUS packet should tell us that, already.
@ -918,12 +923,11 @@ class Net_SFTP extends Net_SSH2
return false; return false;
} }
$filename = $this->_realpath($filename); $result = $this->stat($filename);
if ($filename === false) { if ($result === false) {
return false; return false;
} }
return isset($result['size']) ? $result['size'] : -1;
return $this->_size($filename);
} }
/** /**
@ -1019,10 +1023,10 @@ class Net_SFTP extends Net_SSH2
if ($this->use_stat_cache) { if ($this->use_stat_cache) {
$result = $this->_query_stat_cache($filename); $result = $this->_query_stat_cache($filename);
if (is_array($result)) { if (is_array($result) && isset($result['.'])) {
return (array) $result['.']; return (array) $result['.'];
} }
if ($result !== false) { if (is_object($result)) {
return (array) $result; return (array) $result;
} }
} }
@ -1033,7 +1037,7 @@ class Net_SFTP extends Net_SSH2
return false; return false;
} }
if (isset($stat['type'])) { if (isset($stat['type'])) {
$this->_update_stat_cache($filename, $stat); $this->_update_stat_cache($filename, (object) $stat);
return $stat; return $stat;
} }
@ -1043,7 +1047,7 @@ class Net_SFTP extends Net_SSH2
NET_SFTP_TYPE_REGULAR; NET_SFTP_TYPE_REGULAR;
$this->pwd = $pwd; $this->pwd = $pwd;
$this->_update_stat_cache($filename, $stat); $this->_update_stat_cache($filename, (object) $stat);
return $stat; return $stat;
} }
@ -1070,10 +1074,10 @@ class Net_SFTP extends Net_SSH2
if ($this->use_stat_cache) { if ($this->use_stat_cache) {
$result = $this->_query_stat_cache($filename); $result = $this->_query_stat_cache($filename);
if (is_array($result)) { if (is_array($result) && isset($result['.'])) {
return (array) $result['.']; return (array) $result['.'];
} }
if ($result !== false) { if (is_object($result)) {
return (array) $result; return (array) $result;
} }
} }
@ -1083,7 +1087,7 @@ class Net_SFTP extends Net_SSH2
return false; return false;
} }
if (isset($lstat['type'])) { if (isset($lstat['type'])) {
$this->_update_stat_cache($filename, $lstat); $this->_update_stat_cache($filename, (object) $lstat);
return $lstat; return $lstat;
} }
@ -1091,7 +1095,7 @@ class Net_SFTP extends Net_SSH2
if ($lstat != $stat) { if ($lstat != $stat) {
$lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK)); $lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK));
$this->_update_stat_cache($filename, $lstat); $this->_update_stat_cache($filename, (object) $lstat);
return $stat; return $stat;
} }
@ -1101,7 +1105,7 @@ class Net_SFTP extends Net_SSH2
NET_SFTP_TYPE_REGULAR; NET_SFTP_TYPE_REGULAR;
$this->pwd = $pwd; $this->pwd = $pwd;
$this->_update_stat_cache($filename, $lstat); $this->_update_stat_cache($filename, (object) $lstat);
return $lstat; return $lstat;
} }
@ -1138,24 +1142,6 @@ class Net_SFTP extends Net_SSH2
return false; return false;
} }
/**
* Returns the file size, in bytes, or false, on failure
*
* Determines the size without calling Net_SFTP::_realpath()
*
* @param String $filename
* @return Mixed
* @access private
*/
function _size($filename)
{
$result = $this->_stat($filename, NET_SFTP_STAT);
if ($result === false) {
return false;
}
return isset($result['size']) ? $result['size'] : -1;
}
/** /**
* Truncates a file to a given length * Truncates a file to a given length
* *
@ -1609,7 +1595,7 @@ class Net_SFTP extends Net_SSH2
$offset = $start; $offset = $start;
} elseif ($mode & NET_SFTP_RESUME) { } elseif ($mode & NET_SFTP_RESUME) {
// if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called // if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called
$size = $this->_size($remote_file); $size = $this->size($remote_file);
$offset = $size !== false ? $size : 0; $offset = $size !== false ? $size : 0;
} else { } else {
$offset = 0; $offset = 0;
@ -2028,8 +2014,8 @@ class Net_SFTP extends Net_SSH2
$result = $this->_query_stat_cache($path); $result = $this->_query_stat_cache($path);
if ($result !== false) { if (is_object($result) && isset($result->type)) {
return is_array($result); return $result->type === NET_SFTP_TYPE_DIRECTORY;
} }
} }
@ -2052,8 +2038,8 @@ class Net_SFTP extends Net_SSH2
$result = $this->_query_stat_cache($path); $result = $this->_query_stat_cache($path);
if ($result !== false) { if (is_object($result) && isset($result->type)) {
return !is_array($result); return $result->type === NET_SFTP_TYPE_REGULAR;
} }
} }

View File

@ -151,6 +151,52 @@ class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase
/** /**
* @depends testPutSizeGetFile * @depends testPutSizeGetFile
*/ */
public function testFileExistsIsFileIsDirFile($sftp)
{
$this->assertTrue(
$sftp->file_exists('file1.txt'),
'Failed asserting that file_exists() on example file returns true.'
);
$this->assertTrue(
$sftp->is_file('file1.txt'),
'Failed asserting that is_file() on example file returns true.'
);
$this->assertFalse(
$sftp->is_dir('file1.txt'),
'Failed asserting that is_dir() on example file returns false.'
);
return $sftp;
}
/**
* @depends testFileExistsIsFileIsDirFile
*/
public function testFileExistsIsFileIsDirFileNonexistent($sftp)
{
$this->assertFalse(
$sftp->file_exists('file2.txt'),
'Failed asserting that a nonexistent file does not exist.'
);
$this->assertFalse(
$sftp->is_file('file2.txt'),
'Failed asserting that is_file() on nonexistent file returns false.'
);
$this->assertFalse(
$sftp->is_dir('file2.txt'),
'Failed asserting that is_dir() on nonexistent file returns false.'
);
return $sftp;
}
/**
* @depends testFileExistsIsFileIsDirFileNonexistent
*/
public function testChDirUpHome($sftp) public function testChDirUpHome($sftp)
{ {
$this->assertTrue( $this->assertTrue(
@ -170,6 +216,29 @@ class Net_SFTPFunctionalTest extends PhpseclibFunctionalTestCase
/** /**
* @depends testChDirUpHome * @depends testChDirUpHome
*/ */
public function testFileExistsIsFileIsDirDir($sftp)
{
$this->assertTrue(
$sftp->file_exists(self::$scratchDir),
'Failed asserting that file_exists() on scratch dir returns true.'
);
$this->assertFalse(
$sftp->is_file(self::$scratchDir),
'Failed asserting that is_file() on example file returns false.'
);
$this->assertTrue(
$sftp->is_dir(self::$scratchDir),
'Failed asserting that is_dir() on example file returns true.'
);
return $sftp;
}
/**
* @depends testFileExistsIsFileIsDirDir
*/
public function testRmDirScratch($sftp) public function testRmDirScratch($sftp)
{ {
$this->assertFalse( $this->assertFalse(