From e762b0dc29c46a2e3a4c355b5b8667afe34eab35 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 5 May 2016 10:54:29 -0500 Subject: [PATCH] SFTP: add is_writable, is_writeable and is_readable --- phpseclib/Net/SFTP.php | 70 ++++++++++++++++++++++ tests/Functional/Net/SFTPUserStoryTest.php | 22 +++++++ 2 files changed, 92 insertions(+) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 4d10dbe1..4840f269 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -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 * diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index c01948f4..9f0977e6 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -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')); } }