From 3c62667e959e0fd5dc727f409b2d99741a3455ff Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 17 Jan 2025 02:27:27 -0600 Subject: [PATCH] SFTP: stat, lstat and rawlist didn't include permissions attribute --- src/Net/SFTP.php | 87 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/src/Net/SFTP.php b/src/Net/SFTP.php index 94ddd92..6df0b9f 100644 --- a/src/Net/SFTP.php +++ b/src/Net/SFTP.php @@ -108,10 +108,7 @@ * @method string|false realpath(string $path) * @method bool chdir(string $dir) * @method string[]|false nlist(string $dir = '.', bool $recursive = false) - * @method mixed[]|false rawlist(string $dir = '.', bool $recursive = false) * @method void setListOrder(mixed ...$args) - * @method mixed[]|false stat(string $filename) - * @method mixed[]|false lstat(string $filename) * @method bool truncate(string $filename, int $new_size) * @method bool touch(string $filename, int $time = null, int $atime = null) * @method bool chown(string $filename, int|string $uid, bool $recursive = false) @@ -234,24 +231,6 @@ public function login($username, ...$args) } } - /** - * Parse Attributes - * - * See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info. - * - * @param string $response - * @return array - * @access private - */ - protected function parseAttributes(&$response) - { - $r = $this->sftp->parseAttributes($response); - if (isset($r['mode'])) { - $r['permissions'] = $r['mode']; - } - return $r; - } - /** * Defines how nlist() and rawlist() will be sorted - if at all. * @@ -314,6 +293,72 @@ public function getSFTPObject() return $this->sftp; } + /** + * Returns general information about a file. + * + * Returns an array on success and false otherwise. + * + * @param string $filename + * @return array|false + */ + public function stat($filename) + { + $result = $this->sftp->stat($filename); + if (isset($result['mode'])) { + $result['permissions'] = $result['mode']; + } + return $result; + } + + /** + * Returns general information about a file or symbolic link. + * + * Returns an array on success and false otherwise. + * + * @param string $filename + * @return array|false + */ + public function lstat($filename) + { + $result = $this->sftp->lstat($filename); + if (isset($result['mode'])) { + $result['permissions'] = $result['mode']; + } + return $result; + } + + /** + * Returns a detailed list of files in the given directory + * + * @param string $dir + * @param bool $recursive + * @return array|false + */ + public function rawlist($dir = '.', $recursive = false) + { + $result = $this->sftp->rawlist($dir, $recursive); + if (!$result) { + return false; + } + + return self::rawlistHelper($result); + } + + /** + * Adds permissions variable to each array element + */ + private static function rawlistHelper(array $files) + { + foreach ($files as $file=>&$data) { + if (is_array($data)) { + $data = self::rawlistHelper($data); + } else { + $data->permissions = $data->mode; + } + } + return $files; + } + /** * __call() magic method *