1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-11-30 04:39:02 +01:00

SFTP: add is_writable, is_writeable and is_readable

This commit is contained in:
terrafrost 2016-05-05 10:54:29 -05:00
parent d22bcd63cc
commit e762b0dc29
2 changed files with 92 additions and 0 deletions

View File

@ -2350,6 +2350,76 @@ class Net_SFTP extends Net_SSH2
return $result === NET_SFTP_TYPE_SYMLINK;
}
/**
* Tells whether a file exists and is readable
*
* @param string $path
* @return bool
* @access public
*/
function is_readable($path)
{
$path = $this->_realpath($path);
$packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_READ, 0);
if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
return false;
}
$response = $this->_get_sftp_packet();
switch ($this->packet_type) {
case NET_SFTP_HANDLE:
return true;
case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
return false;
default:
user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
}
/**
* Tells whether the filename is writable
*
* @param string $path
* @return bool
* @access public
*/
function is_writable($path)
{
$path = $this->_realpath($path);
$packet = pack('Na*N2', strlen($path), $path, NET_SFTP_OPEN_WRITE, 0);
if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
return false;
}
$response = $this->_get_sftp_packet();
switch ($this->packet_type) {
case NET_SFTP_HANDLE:
return true;
case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
return false;
default:
user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
return false;
}
}
/**
* Tells whether the filename is writeable
*
* Alias of is_writable
*
* @param string $path
* @return bool
* @access public
*/
function is_writeable($path)
{
return $this->is_writable($path);
}
/**
* Gets last access time of file
*

View File

@ -686,5 +686,27 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase
$sftp->get('offset.txt'),
'Failed asserting that you could upload into the middle of a file.'
);
return $sftp;
}
/**
* @depends testUploadOffsets
*/
public function testReadableWritable($sftp)
{
$sftp->chmod(0000, 'offset.txt');
$this->assertFalse($sftp->is_writable('offset.txt'));
$this->assertFalse($sftp->is_writeable('offset.txt'));
$this->assertFalse($sftp->is_readable('offset.txt'));
$sftp->chmod(0777, 'offset.txt');
$this->assertTrue($sftp->is_writable('offset.txt'));
$this->assertTrue($sftp->is_writeable('offset.txt'));
$this->assertTrue($sftp->is_readable('offset.txt'));
$this->assertFalse($sftp->is_writable('nonexistantfile.ext'));
$this->assertFalse($sftp->is_writeable('nonexistantfile.ext'));
$this->assertFalse($sftp->is_readable('nonexistantfile.ext'));
}
}