Skip to content

Commit

Permalink
refactor(storage): Small code adjustements
Browse files Browse the repository at this point in the history
Signed-off-by: Git'Fellow <[email protected]>
  • Loading branch information
solracsf authored Nov 21, 2024
1 parent cbb937f commit c6765d4
Showing 1 changed file with 19 additions and 21 deletions.
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

0 comments on commit c6765d4

Please sign in to comment.