Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
sftp psalm fixes
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot-nextcloud[bot] committed Sep 18, 2023
commit 656fd512b5370e62213ec2e0b6a397c5ed47d7b7
24 changes: 14 additions & 10 deletions apps/files_external/lib/Lib/Storage/SFTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OC\Files\Storage\Common;
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeDetector;
use phpseclib\Net\SFTP\Stream;

/**
Expand All @@ -61,6 +62,7 @@ class SFTP extends Common {
* @var \phpseclib\Net\SFTP
*/
protected $client;
private IMimeTypeDetector $mimeTypeDetector;

const COPY_CHUNK_SIZE = 8 * 1024 * 1024;

Expand Down Expand Up @@ -118,6 +120,7 @@ public function __construct($params) {

$this->root = '/' . ltrim($this->root, '/');
$this->root = rtrim($this->root, '/') . '/';
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
}

/**
Expand Down Expand Up @@ -392,6 +395,7 @@ public function fopen($path, $mode) {
case 'w':
case 'wb':
SFTPWriteStream::register();
// the SFTPWriteStream doesn't go through the "normal" methods so it doesn't clear the stat cache.
$connection->_remove_from_stat_cache($absPath);
$context = stream_context_create(['sftp' => ['session' => $connection]]);
return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
Expand Down Expand Up @@ -460,14 +464,14 @@ public function rename($source, $target) {
}

/**
* {@inheritdoc}
* @return array{mtime: int, size: int, ctime: int}|false
*/
public function stat($path) {
try {
$stat = $this->getConnection()->stat($this->absPath($path));

$mtime = $stat ? $stat['mtime'] : -1;
$size = $stat ? $stat['size'] : 0;
$mtime = $stat ? (int)$stat['mtime'] : -1;
$size = $stat ? (int)$stat['size'] : 0;

return ['mtime' => $mtime, 'size' => $size, 'ctime' => -1];
} catch (\Exception $e) {
Expand Down Expand Up @@ -499,9 +503,12 @@ public function file_put_contents($path, $data) {

public function writeStream(string $path, $stream, int $size = null): int {
if ($size === null) {
$stream = CountWrapper::wrap($stream, function ($writtenSize) use (&$size) {
$stream = CountWrapper::wrap($stream, function (int $writtenSize) use (&$size) {
$size = $writtenSize;
});
if (!$stream) {
throw new \Exception("Failed to wrap stream");
}
}
/** @psalm-suppress InternalMethod */
$result = $this->getConnection()->put($this->absPath($path), $stream);
Expand Down Expand Up @@ -559,26 +566,23 @@ public function getMetaData($path) {
}

if ($stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
$permissions = Constants::PERMISSION_ALL;
$stat['permissions'] = Constants::PERMISSION_ALL;
} else {
$permissions = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
$stat['permissions'] = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
}

if ($stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
$stat['size'] = -1;
$stat['mimetype'] = FileInfo::MIMETYPE_FOLDER;
} else {
$stat['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($path);
$stat['mimetype'] = $this->mimeTypeDetector->detectPath($path);
}

$stat['etag'] = $this->getETag($path);
$stat['storage_mtime'] = $stat['mtime'];
$stat['permissions'] = $permissions;
$stat['name'] = basename($path);

$keys = ['size', 'mtime', 'mimetype', 'etag', 'storage_mtime', 'permissions', 'name'];
return array_intersect_key($stat, array_flip($keys));
}


}
2 changes: 1 addition & 1 deletion apps/files_external/lib/Lib/Storage/SFTPReadStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function stream_seek($offset, $whence = SEEK_SET) {
return true;
}

private function seekTo(int $offset) {
private function seekTo(int $offset): void {
$this->internalPosition = $offset;
$this->readPosition = $offset;
$this->buffer = '';
Expand Down