Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(storage): Small code adjustements #49417

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 19 additions & 21 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -74,8 +75,6 @@ protected function remove(string $path): bool {
return $this->rmdir($path);
} elseif ($this->is_file($path)) {
return $this->unlink($path);
} else {
return false;
}
}
return false;
Expand All @@ -94,11 +93,7 @@ public function filesize(string $path): int|float|false {
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;
}
}

Expand Down Expand Up @@ -211,7 +206,7 @@ public function copy(string $source, string $target): bool {
$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;
Expand All @@ -230,6 +225,9 @@ public function getMimeType(string $path): string|false {

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);
Expand All @@ -244,7 +242,7 @@ private function addLocalFolder(string $path, string $target): void {
$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);
Expand All @@ -262,7 +260,7 @@ protected function searchInDir(string $query, string $dir = ''): array {
$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) {
Expand Down Expand Up @@ -328,7 +326,7 @@ public function getWatcher(string $path = '', ?IStorage $storage = null): IWatch
}
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;
Expand All @@ -343,8 +341,8 @@ public function getPropagator(?IStorage $storage = null): IPropagator {
}
/** @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;
}
Expand Down Expand Up @@ -389,7 +387,7 @@ public function getETag(string $path): string|false {
* @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;
}

Expand All @@ -413,10 +411,10 @@ public function test(): bool {
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]
);
Expand Down Expand Up @@ -478,7 +476,7 @@ public function verifyPath(string $path, string $fileName): void {
*/
protected function getFilenameValidator(): IFilenameValidator {
if ($this->filenameValidator === null) {
$this->filenameValidator = \OCP\Server::get(IFilenameValidator::class);
$this->filenameValidator = Server::get(IFilenameValidator::class);
}
return $this->filenameValidator;
}
Expand All @@ -501,7 +499,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
$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);
}
Expand All @@ -515,7 +513,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
$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]);
}
}

Expand Down Expand Up @@ -701,8 +699,8 @@ public function changeLock(string $path, int $type, ILockingProvider $provider):

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;
}
Expand Down
15 changes: 8 additions & 7 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\Server;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -56,8 +57,8 @@ public function __construct(array $parameters) {
$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);

Expand Down Expand Up @@ -272,7 +273,7 @@ public function touch(string $path, ?int $mtime = null): bool {
// 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);
Expand Down Expand Up @@ -328,17 +329,17 @@ public function rename(string $source, string $target): bool {
$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;
}

Expand Down Expand Up @@ -487,7 +488,7 @@ public function getSourcePath(string $path): string {
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);
}

Expand Down
Loading
Loading