Skip to content

Commit 3c62667

Browse files
committed
SFTP: stat, lstat and rawlist didn't include permissions attribute
1 parent 2f16b2d commit 3c62667

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

src/Net/SFTP.php

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@
108108
* @method string|false realpath(string $path)
109109
* @method bool chdir(string $dir)
110110
* @method string[]|false nlist(string $dir = '.', bool $recursive = false)
111-
* @method mixed[]|false rawlist(string $dir = '.', bool $recursive = false)
112111
* @method void setListOrder(mixed ...$args)
113-
* @method mixed[]|false stat(string $filename)
114-
* @method mixed[]|false lstat(string $filename)
115112
* @method bool truncate(string $filename, int $new_size)
116113
* @method bool touch(string $filename, int $time = null, int $atime = null)
117114
* @method bool chown(string $filename, int|string $uid, bool $recursive = false)
@@ -234,24 +231,6 @@ public function login($username, ...$args)
234231
}
235232
}
236233

237-
/**
238-
* Parse Attributes
239-
*
240-
* See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info.
241-
*
242-
* @param string $response
243-
* @return array
244-
* @access private
245-
*/
246-
protected function parseAttributes(&$response)
247-
{
248-
$r = $this->sftp->parseAttributes($response);
249-
if (isset($r['mode'])) {
250-
$r['permissions'] = $r['mode'];
251-
}
252-
return $r;
253-
}
254-
255234
/**
256235
* Defines how nlist() and rawlist() will be sorted - if at all.
257236
*
@@ -314,6 +293,72 @@ public function getSFTPObject()
314293
return $this->sftp;
315294
}
316295

296+
/**
297+
* Returns general information about a file.
298+
*
299+
* Returns an array on success and false otherwise.
300+
*
301+
* @param string $filename
302+
* @return array|false
303+
*/
304+
public function stat($filename)
305+
{
306+
$result = $this->sftp->stat($filename);
307+
if (isset($result['mode'])) {
308+
$result['permissions'] = $result['mode'];
309+
}
310+
return $result;
311+
}
312+
313+
/**
314+
* Returns general information about a file or symbolic link.
315+
*
316+
* Returns an array on success and false otherwise.
317+
*
318+
* @param string $filename
319+
* @return array|false
320+
*/
321+
public function lstat($filename)
322+
{
323+
$result = $this->sftp->lstat($filename);
324+
if (isset($result['mode'])) {
325+
$result['permissions'] = $result['mode'];
326+
}
327+
return $result;
328+
}
329+
330+
/**
331+
* Returns a detailed list of files in the given directory
332+
*
333+
* @param string $dir
334+
* @param bool $recursive
335+
* @return array|false
336+
*/
337+
public function rawlist($dir = '.', $recursive = false)
338+
{
339+
$result = $this->sftp->rawlist($dir, $recursive);
340+
if (!$result) {
341+
return false;
342+
}
343+
344+
return self::rawlistHelper($result);
345+
}
346+
347+
/**
348+
* Adds permissions variable to each array element
349+
*/
350+
private static function rawlistHelper(array $files)
351+
{
352+
foreach ($files as $file=>&$data) {
353+
if (is_array($data)) {
354+
$data = self::rawlistHelper($data);
355+
} else {
356+
$data->permissions = $data->mode;
357+
}
358+
}
359+
return $files;
360+
}
361+
317362
/**
318363
* __call() magic method
319364
*

0 commit comments

Comments
 (0)