diff options
author | Git'Fellow <12234510+solracsf@users.noreply.github.com> | 2024-11-21 08:38:50 +0100 |
---|---|---|
committer | Git'Fellow <12234510+solracsf@users.noreply.github.com> | 2024-11-21 10:02:00 +0100 |
commit | 60d956f39f6226dcd66e800844834dc9f3614ce7 (patch) | |
tree | f3d7daf0d33f94a2065768a20355681ae35ed165 | |
parent | cbb937fa3260c6a445e5027fee288bd01380ddd1 (diff) | |
download | nextcloud-server-60d956f39f6226dcd66e800844834dc9f3614ce7.tar.gz nextcloud-server-60d956f39f6226dcd66e800844834dc9f3614ce7.zip |
refactor(storage): Code adjustements and simplificationsrefactSmallAdjust
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
-rw-r--r-- | lib/private/Files/Storage/Common.php | 40 | ||||
-rw-r--r-- | lib/private/Files/Storage/Local.php | 15 | ||||
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Availability.php | 253 | ||||
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Encryption.php | 2 |
4 files changed, 68 insertions, 242 deletions
diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index 8a95e637372..036ccad23a5 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -32,6 +32,7 @@ use OCP\Files\Storage\ILockingStorage; use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IWriteStreamStorage; use OCP\Files\StorageNotAvailableException; +use OCP\IConfig; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; use OCP\Server; @@ -74,8 +75,6 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, return $this->rmdir($path); } elseif ($this->is_file($path)) { return $this->unlink($path); - } else { - return false; } } return false; @@ -94,11 +93,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, return 0; //by definition } else { $stat = $this->stat($path); - if (isset($stat['size'])) { - return $stat['size']; - } else { - return 0; - } + return isset($stat['size']) ? $stat['size'] : 0; } } @@ -211,7 +206,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, $targetStream = $this->fopen($target, 'w'); [, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream); if (!$result) { - \OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); + Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); } $this->removeCachedFile($target); return $result; @@ -230,6 +225,9 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, public function hash(string $type, string $path, bool $raw = false): string|false { $fh = $this->fopen($path, 'rb'); + if (!$fh) { + return false; + } $ctx = hash_init($type); hash_update_stream($ctx, $fh); fclose($fh); @@ -244,7 +242,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, $dh = $this->opendir($path); if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { + if (!Filesystem::isIgnoredDir($file)) { if ($this->is_dir($path . '/' . $file)) { mkdir($target . '/' . $file); $this->addLocalFolder($path . '/' . $file, $target . '/' . $file); @@ -262,7 +260,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, $dh = $this->opendir($dir); if (is_resource($dh)) { while (($item = readdir($dh)) !== false) { - if (\OC\Files\Filesystem::isIgnoredDir($item)) { + if (Filesystem::isIgnoredDir($item)) { continue; } if (strstr(strtolower($item), strtolower($query)) !== false) { @@ -328,7 +326,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, } if (!isset($this->watcher)) { $this->watcher = new Watcher($storage); - $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); + $globalPolicy = Server::get(IConfig::class)->getSystemValueInt('filesystem_check_changes', Watcher::CHECK_NEVER); $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); } return $this->watcher; @@ -343,8 +341,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, } /** @var self $storage */ if (!isset($storage->propagator)) { - $config = \OC::$server->getSystemConfig(); - $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]); + $config = Server::get(IConfig::class); + $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getSystemValueString('instanceid')]); } return $storage->propagator; } @@ -389,7 +387,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, * @return string cleaned path */ public function cleanPath(string $path): string { - if (strlen($path) == 0 or $path[0] != '/') { + if (strlen($path) == 0 || $path[0] != '/') { $path = '/' . $path; } @@ -413,10 +411,10 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, if ($this->stat('')) { return true; } - \OC::$server->get(LoggerInterface::class)->info('External storage not available: stat() failed'); + Server::get(LoggerInterface::class)->info('External storage not available: stat() failed'); return false; } catch (\Exception $e) { - \OC::$server->get(LoggerInterface::class)->warning( + Server::get(LoggerInterface::class)->warning( 'External storage not available: ' . $e->getMessage(), ['exception' => $e] ); @@ -478,7 +476,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, */ protected function getFilenameValidator(): IFilenameValidator { if ($this->filenameValidator === null) { - $this->filenameValidator = \OCP\Server::get(IFilenameValidator::class); + $this->filenameValidator = Server::get(IFilenameValidator::class); } return $this->filenameValidator; } @@ -501,7 +499,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, $result = $this->mkdir($targetInternalPath); if (is_resource($dh)) { $result = true; - while ($result and ($file = readdir($dh)) !== false) { + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); } @@ -515,7 +513,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, $this->writeStream($targetInternalPath, $source); $result = true; } catch (\Exception $e) { - \OC::$server->get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]); + Server::get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]); } } @@ -701,8 +699,8 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, private function getLockLogger(): ?LoggerInterface { if (is_null($this->shouldLogLocks)) { - $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValueBool('filelocking.debug', false); - $this->logger = $this->shouldLogLocks ? \OC::$server->get(LoggerInterface::class) : null; + $this->shouldLogLocks = Server::get(IConfig::class)->getSystemValueBool('filelocking.debug', false); + $this->logger = $this->shouldLogLocks ? Server::get(LoggerInterface::class) : null; } return $this->logger; } diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index f06e409cd8f..260f9218a88 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -17,6 +17,7 @@ use OCP\Files\IMimeTypeDetector; use OCP\Files\Storage\IStorage; use OCP\Files\StorageNotAvailableException; use OCP\IConfig; +use OCP\Server; use OCP\Util; use Psr\Log\LoggerInterface; @@ -56,8 +57,8 @@ class Local extends \OC\Files\Storage\Common { $this->datadir .= '/'; } $this->dataDirLength = strlen($this->realDataDir); - $this->config = \OC::$server->get(IConfig::class); - $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class); + $this->config = Server::get(IConfig::class); + $this->mimeTypeDetector = Server::get(IMimeTypeDetector::class); $this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022); $this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false); @@ -272,7 +273,7 @@ class Local extends \OC\Files\Storage\Common { // sets the modification time of the file to the given value. // If mtime is nil the current time is set. // note that the access time of the file always changes to the current time. - if ($this->file_exists($path) and !$this->isUpdatable($path)) { + if ($this->file_exists($path) && !$this->isUpdatable($path)) { return false; } $oldMask = umask($this->defUMask); @@ -328,17 +329,17 @@ class Local extends \OC\Files\Storage\Common { $dstParent = dirname($target); if (!$this->isUpdatable($srcParent)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']); return false; } if (!$this->isUpdatable($dstParent)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']); return false; } if (!$this->file_exists($source)) { - \OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']); + Server::get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']); return false; } @@ -487,7 +488,7 @@ class Local extends \OC\Files\Storage\Common { return $fullPath; } - \OC::$server->get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']); + Server::get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']); throw new ForbiddenException('Following symlinks is not allowed', false); } diff --git a/lib/private/Files/Storage/Wrapper/Availability.php b/lib/private/Files/Storage/Wrapper/Availability.php index 70212cacab8..32c51a1b25e 100644 --- a/lib/private/Files/Storage/Wrapper/Availability.php +++ b/lib/private/Files/Storage/Wrapper/Availability.php @@ -24,7 +24,7 @@ class Availability extends Wrapper { protected $config; public function __construct(array $parameters) { - $this->config = $parameters['config'] ?? \OC::$server->getConfig(); + $this->config = $parameters['config'] ?? \OCP\Server::get(IConfig::class); parent::__construct($parameters); } @@ -70,277 +70,128 @@ class Availability extends Wrapper { } } - public function mkdir(string $path): bool { + /** + * Handles availability checks and delegates method calls dynamically + */ + private function handleAvailability(string $method, mixed ...$args): mixed { $this->checkAvailability(); try { - return parent::mkdir($path); + return call_user_func_array([parent::class, $method], $args); } catch (StorageNotAvailableException $e) { $this->setUnavailable($e); return false; } } + public function mkdir(string $path): bool { + return $this->handleAvailability('mkdir', $path); + } + public function rmdir(string $path): bool { - $this->checkAvailability(); - try { - return parent::rmdir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('rmdir', $path); } public function opendir(string $path) { - $this->checkAvailability(); - try { - return parent::opendir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('opendir', $path); } public function is_dir(string $path): bool { - $this->checkAvailability(); - try { - return parent::is_dir($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('is_dir', $path); } public function is_file(string $path): bool { - $this->checkAvailability(); - try { - return parent::is_file($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('is_file', $path); } public function stat(string $path): array|false { - $this->checkAvailability(); - try { - return parent::stat($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('stat', $path); } public function filetype(string $path): string|false { - $this->checkAvailability(); - try { - return parent::filetype($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('filetype', $path); } public function filesize(string $path): int|float|false { - $this->checkAvailability(); - try { - return parent::filesize($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('filesize', $path); } public function isCreatable(string $path): bool { - $this->checkAvailability(); - try { - return parent::isCreatable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('isCreatable', $path); } public function isReadable(string $path): bool { - $this->checkAvailability(); - try { - return parent::isReadable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('isReadable', $path); } public function isUpdatable(string $path): bool { - $this->checkAvailability(); - try { - return parent::isUpdatable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('isUpdatable', $path); } public function isDeletable(string $path): bool { - $this->checkAvailability(); - try { - return parent::isDeletable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('isDeletable', $path); } public function isSharable(string $path): bool { - $this->checkAvailability(); - try { - return parent::isSharable($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('isSharable', $path); } public function getPermissions(string $path): int { - $this->checkAvailability(); - try { - return parent::getPermissions($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return 0; - } + return $this->handleAvailability('getPermissions', $path); } public function file_exists(string $path): bool { if ($path === '') { return true; } - $this->checkAvailability(); - try { - return parent::file_exists($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('file_exists', $path); } public function filemtime(string $path): int|false { - $this->checkAvailability(); - try { - return parent::filemtime($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('filemtime', $path); } public function file_get_contents(string $path): string|false { - $this->checkAvailability(); - try { - return parent::file_get_contents($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('file_get_contents', $path); } public function file_put_contents(string $path, mixed $data): int|float|false { - $this->checkAvailability(); - try { - return parent::file_put_contents($path, $data); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('file_put_contents', $path, $data); } public function unlink(string $path): bool { - $this->checkAvailability(); - try { - return parent::unlink($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('unlink', $path); } public function rename(string $source, string $target): bool { - $this->checkAvailability(); - try { - return parent::rename($source, $target); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('rename', $source, $target); } public function copy(string $source, string $target): bool { - $this->checkAvailability(); - try { - return parent::copy($source, $target); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('copy', $source, $target); } public function fopen(string $path, string $mode) { - $this->checkAvailability(); - try { - return parent::fopen($path, $mode); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('fopen', $path, $mode); } public function getMimeType(string $path): string|false { - $this->checkAvailability(); - try { - return parent::getMimeType($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('getMimeType', $path); } public function hash(string $type, string $path, bool $raw = false): string|false { - $this->checkAvailability(); - try { - return parent::hash($type, $path, $raw); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('hash', $type, $path, $raw); } public function free_space(string $path): int|float|false { - $this->checkAvailability(); - try { - return parent::free_space($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('free_space', $path); } public function touch(string $path, ?int $mtime = null): bool { - $this->checkAvailability(); - try { - return parent::touch($path, $mtime); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('touch', $path, $mtime); } public function getLocalFile(string $path): string|false { - $this->checkAvailability(); - try { - return parent::getLocalFile($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('getLocalFile', $path); } public function hasUpdated(string $path, int $time): bool { @@ -366,43 +217,19 @@ class Availability extends Wrapper { } public function getETag(string $path): string|false { - $this->checkAvailability(); - try { - return parent::getETag($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('getETag', $path); } public function getDirectDownload(string $path): array|false { - $this->checkAvailability(); - try { - return parent::getDirectDownload($path); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('getDirectDownload', $path); } public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { - $this->checkAvailability(); - try { - return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('copyFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath); } public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { - $this->checkAvailability(); - try { - return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); - } catch (StorageNotAvailableException $e) { - $this->setUnavailable($e); - return false; - } + return $this->handleAvailability('moveFromStorage', $sourceStorage, $sourceInternalPath, $targetInternalPath); } public function getMetaData(string $path): ?array { diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index c9ed9c7cb53..ba23f3c43ec 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -652,7 +652,7 @@ class Encryption extends Wrapper { $result = true; } if (is_resource($dh)) { - while ($result and ($file = readdir($dh)) !== false) { + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); } |