aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Storage/Wrapper/Encryption.php
diff options
context:
space:
mode:
authorprovokateurin <kate@provokateurin.de>2024-09-19 18:19:34 +0200
committerprovokateurin <kate@provokateurin.de>2024-09-26 18:13:03 +0200
commit7cdccd058f98a5e4164fc314f06f62fd8dfcd73c (patch)
tree948927053d2d1b06f9fa27e7f2c897ac922181e1 /lib/private/Files/Storage/Wrapper/Encryption.php
parent256a8d8903ef3a81a425f417c0eb626f42036a25 (diff)
downloadnextcloud-server-7cdccd058f98a5e4164fc314f06f62fd8dfcd73c.tar.gz
nextcloud-server-7cdccd058f98a5e4164fc314f06f62fd8dfcd73c.zip
fix(Storage): Fix IStorage return types
Signed-off-by: provokateurin <kate@provokateurin.de>
Diffstat (limited to 'lib/private/Files/Storage/Wrapper/Encryption.php')
-rw-r--r--lib/private/Files/Storage/Wrapper/Encryption.php148
1 files changed, 34 insertions, 114 deletions
diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php
index 3b45e996f5d..f2f3cdb94f6 100644
--- a/lib/private/Files/Storage/Wrapper/Encryption.php
+++ b/lib/private/Files/Storage/Wrapper/Encryption.php
@@ -18,7 +18,6 @@ use OC\Files\Storage\Common;
use OC\Files\Storage\LocalTempFileTrait;
use OC\Memcache\ArrayCache;
use OCP\Cache\CappedMemoryCache;
-use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Encryption\IFile;
use OCP\Encryption\IManager;
use OCP\Encryption\Keys\IStorage;
@@ -104,11 +103,7 @@ class Encryption extends Wrapper {
parent::__construct($parameters);
}
- /**
- * see https://www.php.net/manual/en/function.filesize.php
- * The result for filesize when called on a folder is required to be 0
- */
- public function filesize($path): false|int|float {
+ public function filesize($path): int|float|false {
$fullPath = $this->getFullPath($path);
$info = $this->getCache()->get($path);
@@ -179,7 +174,7 @@ class Encryption extends Wrapper {
return $data;
}
- public function getMetaData($path) {
+ public function getMetaData($path): ?array {
$data = $this->storage->getMetaData($path);
if (is_null($data)) {
return null;
@@ -194,13 +189,7 @@ class Encryption extends Wrapper {
}
}
- /**
- * see https://www.php.net/manual/en/function.file_get_contents.php
- *
- * @param string $path
- * @return string|false
- */
- public function file_get_contents($path) {
+ public function file_get_contents($path): string|false {
$encryptionModule = $this->getEncryptionModule($path);
if ($encryptionModule) {
@@ -215,14 +204,7 @@ class Encryption extends Wrapper {
return $this->storage->file_get_contents($path);
}
- /**
- * see https://www.php.net/manual/en/function.file_put_contents.php
- *
- * @param string $path
- * @param mixed $data
- * @return int|false
- */
- public function file_put_contents($path, $data) {
+ public function file_put_contents($path, $data): int|float|false {
// file put content will always be translated to a stream write
$handle = $this->fopen($path, 'w');
if (is_resource($handle)) {
@@ -234,13 +216,7 @@ class Encryption extends Wrapper {
return false;
}
- /**
- * see https://www.php.net/manual/en/function.unlink.php
- *
- * @param string $path
- * @return bool
- */
- public function unlink($path) {
+ public function unlink($path): bool {
$fullPath = $this->getFullPath($path);
if ($this->util->isExcluded($fullPath)) {
return $this->storage->unlink($path);
@@ -254,14 +230,7 @@ class Encryption extends Wrapper {
return $this->storage->unlink($path);
}
- /**
- * see https://www.php.net/manual/en/function.rename.php
- *
- * @param string $source
- * @param string $target
- * @return bool
- */
- public function rename($source, $target) {
+ public function rename($source, $target): bool {
$result = $this->storage->rename($source, $target);
if ($result &&
@@ -286,13 +255,7 @@ class Encryption extends Wrapper {
return $result;
}
- /**
- * see https://www.php.net/manual/en/function.rmdir.php
- *
- * @param string $path
- * @return bool
- */
- public function rmdir($path) {
+ public function rmdir($path): bool {
$result = $this->storage->rmdir($path);
$fullPath = $this->getFullPath($path);
if ($result &&
@@ -305,13 +268,7 @@ class Encryption extends Wrapper {
return $result;
}
- /**
- * check if a file can be read
- *
- * @param string $path
- * @return bool
- */
- public function isReadable($path) {
+ public function isReadable($path): bool {
$isReadable = true;
$metaData = $this->getMetaData($path);
@@ -328,12 +285,6 @@ class Encryption extends Wrapper {
return $this->storage->isReadable($path) && $isReadable;
}
- /**
- * see https://www.php.net/manual/en/function.copy.php
- *
- * @param string $source
- * @param string $target
- */
public function copy($source, $target): bool {
$sourcePath = $this->getFullPath($source);
@@ -347,15 +298,6 @@ class Encryption extends Wrapper {
return $this->copyFromStorage($this, $source, $target);
}
- /**
- * see https://www.php.net/manual/en/function.fopen.php
- *
- * @param string $path
- * @param string $mode
- * @return resource|bool
- * @throws GenericEncryptionException
- * @throws ModuleDoesNotExistsException
- */
public function fopen($path, $mode) {
// check if the file is stored in the array cache, this means that we
// copy a file over to the versions folder, in this case we don't want to
@@ -409,10 +351,8 @@ class Encryption extends Wrapper {
// if we update a encrypted file with a un-encrypted one we change the db flag
if ($targetIsEncrypted && $encryptionEnabled === false) {
$cache = $this->storage->getCache();
- if ($cache) {
- $entry = $cache->get($path);
- $cache->update($entry->getId(), ['encrypted' => 0]);
- }
+ $entry = $cache->get($path);
+ $cache->update($entry->getId(), ['encrypted' => 0]);
}
if ($encryptionEnabled) {
// if $encryptionModuleId is empty, the default module will be used
@@ -508,10 +448,8 @@ class Encryption extends Wrapper {
* @param string $path internal path relative to the storage root
* @param int $size size of the physical file
* @param int $unencryptedSize size of the unencrypted file
- *
- * @return int calculated unencrypted size
*/
- protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int {
+ protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int|float {
$headerSize = $this->getHeaderSize($path);
$header = $this->getHeader($path);
$encryptionModule = $this->getEncryptionModule($path);
@@ -580,12 +518,10 @@ class Encryption extends Wrapper {
// write to cache if applicable
$cache = $this->storage->getCache();
- if ($cache) {
- $entry = $cache->get($path);
- $cache->update($entry['fileid'], [
- 'unencrypted_size' => $newUnencryptedSize
- ]);
- }
+ $entry = $cache->get($path);
+ $cache->update($entry['fileid'], [
+ 'unencrypted_size' => $newUnencryptedSize
+ ]);
return $newUnencryptedSize;
}
@@ -617,19 +553,12 @@ class Encryption extends Wrapper {
return $data;
}
- /**
- * @param Storage\IStorage $sourceStorage
- * @param string $sourceInternalPath
- * @param string $targetInternalPath
- * @param bool $preserveMtime
- * @return bool
- */
public function moveFromStorage(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime = true,
- ) {
+ ): bool {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
@@ -647,30 +576,21 @@ class Encryption extends Wrapper {
$result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true);
if ($result) {
if ($sourceStorage->is_dir($sourceInternalPath)) {
- $result &= $sourceStorage->rmdir($sourceInternalPath);
+ $result = $sourceStorage->rmdir($sourceInternalPath);
} else {
- $result &= $sourceStorage->unlink($sourceInternalPath);
+ $result = $sourceStorage->unlink($sourceInternalPath);
}
}
return $result;
}
-
- /**
- * @param Storage\IStorage $sourceStorage
- * @param string $sourceInternalPath
- * @param string $targetInternalPath
- * @param bool $preserveMtime
- * @param bool $isRename
- * @return bool
- */
public function copyFromStorage(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime = false,
$isRename = false,
- ) {
+ ): bool {
// TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed:
// - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage
// - copy the file cache update from $this->copyBetweenStorage to this method
@@ -695,7 +615,7 @@ class Encryption extends Wrapper {
$targetInternalPath,
$isRename,
$keepEncryptionVersion,
- ) {
+ ): void {
$isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath);
$cacheInformation = [
'encrypted' => $isEncrypted,
@@ -750,7 +670,7 @@ class Encryption extends Wrapper {
$targetInternalPath,
$preserveMtime,
$isRename,
- ) {
+ ): bool {
// for versions we have nothing to do, because versions should always use the
// key from the original file. Just create a 1:1 copy and done
if ($this->isVersion($targetInternalPath) ||
@@ -828,7 +748,7 @@ class Encryption extends Wrapper {
return (bool)$result;
}
- public function getLocalFile($path) {
+ public function getLocalFile($path): string|false {
if ($this->encryptionManager->isEnabled()) {
$cachedFile = $this->getCachedFile($path);
if (is_string($cachedFile)) {
@@ -838,14 +758,14 @@ class Encryption extends Wrapper {
return $this->storage->getLocalFile($path);
}
- public function isLocal() {
+ public function isLocal(): bool {
if ($this->encryptionManager->isEnabled()) {
return false;
}
return $this->storage->isLocal();
}
- public function stat($path) {
+ public function stat($path): array|false {
$stat = $this->storage->stat($path);
if (!$stat) {
return false;
@@ -857,7 +777,7 @@ class Encryption extends Wrapper {
return $stat;
}
- public function hash($type, $path, $raw = false) {
+ public function hash($type, $path, $raw = false): string|false {
$fh = $this->fopen($path, 'rb');
$ctx = hash_init($type);
hash_update_stream($ctx, $fh);
@@ -871,7 +791,7 @@ class Encryption extends Wrapper {
* @param string $path relative to mount point
* @return string full path including mount point
*/
- protected function getFullPath($path) {
+ protected function getFullPath($path): string {
return Filesystem::normalizePath($this->mountPoint . '/' . $path);
}
@@ -882,7 +802,7 @@ class Encryption extends Wrapper {
* @param string $path
* @return string
*/
- protected function readFirstBlock($path) {
+ protected function readFirstBlock($path): string {
$firstBlock = '';
if ($this->storage->is_file($path)) {
$handle = $this->storage->fopen($path, 'r');
@@ -898,7 +818,7 @@ class Encryption extends Wrapper {
* @param string $path
* @return int
*/
- protected function getHeaderSize($path) {
+ protected function getHeaderSize($path): int {
$headerSize = 0;
$realFile = $this->util->stripPartialFileExtension($path);
if ($this->storage->is_file($realFile)) {
@@ -919,7 +839,7 @@ class Encryption extends Wrapper {
* @param string $path
* @return array
*/
- protected function getHeader($path) {
+ protected function getHeader($path): array {
$realFile = $this->util->stripPartialFileExtension($path);
$exists = $this->storage->is_file($realFile);
if ($exists) {
@@ -956,7 +876,7 @@ class Encryption extends Wrapper {
* @throws ModuleDoesNotExistsException
* @throws \Exception
*/
- protected function getEncryptionModule($path) {
+ protected function getEncryptionModule($path): ?\OCP\Encryption\IEncryptionModule {
$encryptionModule = null;
$header = $this->getHeader($path);
$encryptionModuleId = $this->util->getEncryptionModuleId($header);
@@ -976,7 +896,7 @@ class Encryption extends Wrapper {
* @param string $path
* @param int $unencryptedSize
*/
- public function updateUnencryptedSize($path, $unencryptedSize) {
+ public function updateUnencryptedSize($path, $unencryptedSize): void {
$this->unencryptedSize[$path] = $unencryptedSize;
}
@@ -987,7 +907,7 @@ class Encryption extends Wrapper {
* @param string $target path relative to data/
* @return bool
*/
- protected function copyKeys($source, $target) {
+ protected function copyKeys($source, $target): bool {
if (!$this->util->isExcluded($source)) {
return $this->keyStorage->copyKeys($source, $target);
}
@@ -1001,7 +921,7 @@ class Encryption extends Wrapper {
* @param $path
* @return bool
*/
- protected function isVersion($path) {
+ protected function isVersion($path): bool {
$normalized = Filesystem::normalizePath($path);
return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/';
}
@@ -1012,7 +932,7 @@ class Encryption extends Wrapper {
* @param $path
* @return bool
*/
- protected function shouldEncrypt($path) {
+ protected function shouldEncrypt($path): bool {
$fullPath = $this->getFullPath($path);
$mountPointConfig = $this->mount->getOption('encrypt', true);
if ($mountPointConfig === false) {