Skip to content

Commit 60d956f

Browse files
committed
refactor(storage): Code adjustements and simplifications
Signed-off-by: Git'Fellow <[email protected]>
1 parent cbb937f commit 60d956f

File tree

4 files changed

+68
-242
lines changed

4 files changed

+68
-242
lines changed

lib/private/Files/Storage/Common.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Files\Storage\IStorage;
3333
use OCP\Files\Storage\IWriteStreamStorage;
3434
use OCP\Files\StorageNotAvailableException;
35+
use OCP\IConfig;
3536
use OCP\Lock\ILockingProvider;
3637
use OCP\Lock\LockedException;
3738
use OCP\Server;
@@ -74,8 +75,6 @@ protected function remove(string $path): bool {
7475
return $this->rmdir($path);
7576
} elseif ($this->is_file($path)) {
7677
return $this->unlink($path);
77-
} else {
78-
return false;
7978
}
8079
}
8180
return false;
@@ -94,11 +93,7 @@ public function filesize(string $path): int|float|false {
9493
return 0; //by definition
9594
} else {
9695
$stat = $this->stat($path);
97-
if (isset($stat['size'])) {
98-
return $stat['size'];
99-
} else {
100-
return 0;
101-
}
96+
return isset($stat['size']) ? $stat['size'] : 0;
10297
}
10398
}
10499

@@ -211,7 +206,7 @@ public function copy(string $source, string $target): bool {
211206
$targetStream = $this->fopen($target, 'w');
212207
[, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream);
213208
if (!$result) {
214-
\OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
209+
Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
215210
}
216211
$this->removeCachedFile($target);
217212
return $result;
@@ -230,6 +225,9 @@ public function getMimeType(string $path): string|false {
230225

231226
public function hash(string $type, string $path, bool $raw = false): string|false {
232227
$fh = $this->fopen($path, 'rb');
228+
if (!$fh) {
229+
return false;
230+
}
233231
$ctx = hash_init($type);
234232
hash_update_stream($ctx, $fh);
235233
fclose($fh);
@@ -244,7 +242,7 @@ private function addLocalFolder(string $path, string $target): void {
244242
$dh = $this->opendir($path);
245243
if (is_resource($dh)) {
246244
while (($file = readdir($dh)) !== false) {
247-
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
245+
if (!Filesystem::isIgnoredDir($file)) {
248246
if ($this->is_dir($path . '/' . $file)) {
249247
mkdir($target . '/' . $file);
250248
$this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
@@ -262,7 +260,7 @@ protected function searchInDir(string $query, string $dir = ''): array {
262260
$dh = $this->opendir($dir);
263261
if (is_resource($dh)) {
264262
while (($item = readdir($dh)) !== false) {
265-
if (\OC\Files\Filesystem::isIgnoredDir($item)) {
263+
if (Filesystem::isIgnoredDir($item)) {
266264
continue;
267265
}
268266
if (strstr(strtolower($item), strtolower($query)) !== false) {
@@ -328,7 +326,7 @@ public function getWatcher(string $path = '', ?IStorage $storage = null): IWatch
328326
}
329327
if (!isset($this->watcher)) {
330328
$this->watcher = new Watcher($storage);
331-
$globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER);
329+
$globalPolicy = Server::get(IConfig::class)->getSystemValueInt('filesystem_check_changes', Watcher::CHECK_NEVER);
332330
$this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy));
333331
}
334332
return $this->watcher;
@@ -343,8 +341,8 @@ public function getPropagator(?IStorage $storage = null): IPropagator {
343341
}
344342
/** @var self $storage */
345343
if (!isset($storage->propagator)) {
346-
$config = \OC::$server->getSystemConfig();
347-
$storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]);
344+
$config = Server::get(IConfig::class);
345+
$storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getSystemValueString('instanceid')]);
348346
}
349347
return $storage->propagator;
350348
}
@@ -389,7 +387,7 @@ public function getETag(string $path): string|false {
389387
* @return string cleaned path
390388
*/
391389
public function cleanPath(string $path): string {
392-
if (strlen($path) == 0 or $path[0] != '/') {
390+
if (strlen($path) == 0 || $path[0] != '/') {
393391
$path = '/' . $path;
394392
}
395393

@@ -413,10 +411,10 @@ public function test(): bool {
413411
if ($this->stat('')) {
414412
return true;
415413
}
416-
\OC::$server->get(LoggerInterface::class)->info('External storage not available: stat() failed');
414+
Server::get(LoggerInterface::class)->info('External storage not available: stat() failed');
417415
return false;
418416
} catch (\Exception $e) {
419-
\OC::$server->get(LoggerInterface::class)->warning(
417+
Server::get(LoggerInterface::class)->warning(
420418
'External storage not available: ' . $e->getMessage(),
421419
['exception' => $e]
422420
);
@@ -478,7 +476,7 @@ public function verifyPath(string $path, string $fileName): void {
478476
*/
479477
protected function getFilenameValidator(): IFilenameValidator {
480478
if ($this->filenameValidator === null) {
481-
$this->filenameValidator = \OCP\Server::get(IFilenameValidator::class);
479+
$this->filenameValidator = Server::get(IFilenameValidator::class);
482480
}
483481
return $this->filenameValidator;
484482
}
@@ -501,7 +499,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
501499
$result = $this->mkdir($targetInternalPath);
502500
if (is_resource($dh)) {
503501
$result = true;
504-
while ($result and ($file = readdir($dh)) !== false) {
502+
while ($result && ($file = readdir($dh)) !== false) {
505503
if (!Filesystem::isIgnoredDir($file)) {
506504
$result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file);
507505
}
@@ -515,7 +513,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
515513
$this->writeStream($targetInternalPath, $source);
516514
$result = true;
517515
} catch (\Exception $e) {
518-
\OC::$server->get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
516+
Server::get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
519517
}
520518
}
521519

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

702700
private function getLockLogger(): ?LoggerInterface {
703701
if (is_null($this->shouldLogLocks)) {
704-
$this->shouldLogLocks = \OC::$server->getConfig()->getSystemValueBool('filelocking.debug', false);
705-
$this->logger = $this->shouldLogLocks ? \OC::$server->get(LoggerInterface::class) : null;
702+
$this->shouldLogLocks = Server::get(IConfig::class)->getSystemValueBool('filelocking.debug', false);
703+
$this->logger = $this->shouldLogLocks ? Server::get(LoggerInterface::class) : null;
706704
}
707705
return $this->logger;
708706
}

lib/private/Files/Storage/Local.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\Files\Storage\IStorage;
1818
use OCP\Files\StorageNotAvailableException;
1919
use OCP\IConfig;
20+
use OCP\Server;
2021
use OCP\Util;
2122
use Psr\Log\LoggerInterface;
2223

@@ -56,8 +57,8 @@ public function __construct(array $parameters) {
5657
$this->datadir .= '/';
5758
}
5859
$this->dataDirLength = strlen($this->realDataDir);
59-
$this->config = \OC::$server->get(IConfig::class);
60-
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
60+
$this->config = Server::get(IConfig::class);
61+
$this->mimeTypeDetector = Server::get(IMimeTypeDetector::class);
6162
$this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
6263
$this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false);
6364

@@ -272,7 +273,7 @@ public function touch(string $path, ?int $mtime = null): bool {
272273
// sets the modification time of the file to the given value.
273274
// If mtime is nil the current time is set.
274275
// note that the access time of the file always changes to the current time.
275-
if ($this->file_exists($path) and !$this->isUpdatable($path)) {
276+
if ($this->file_exists($path) && !$this->isUpdatable($path)) {
276277
return false;
277278
}
278279
$oldMask = umask($this->defUMask);
@@ -328,17 +329,17 @@ public function rename(string $source, string $target): bool {
328329
$dstParent = dirname($target);
329330

330331
if (!$this->isUpdatable($srcParent)) {
331-
\OC::$server->get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']);
332+
Server::get(LoggerInterface::class)->error('unable to rename, source directory is not writable : ' . $srcParent, ['app' => 'core']);
332333
return false;
333334
}
334335

335336
if (!$this->isUpdatable($dstParent)) {
336-
\OC::$server->get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']);
337+
Server::get(LoggerInterface::class)->error('unable to rename, destination directory is not writable : ' . $dstParent, ['app' => 'core']);
337338
return false;
338339
}
339340

340341
if (!$this->file_exists($source)) {
341-
\OC::$server->get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']);
342+
Server::get(LoggerInterface::class)->error('unable to rename, file does not exists : ' . $source, ['app' => 'core']);
342343
return false;
343344
}
344345

@@ -487,7 +488,7 @@ public function getSourcePath(string $path): string {
487488
return $fullPath;
488489
}
489490

490-
\OC::$server->get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']);
491+
Server::get(LoggerInterface::class)->error("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ['app' => 'core']);
491492
throw new ForbiddenException('Following symlinks is not allowed', false);
492493
}
493494

0 commit comments

Comments
 (0)