Skip to content

Commit

Permalink
refactor(filecache): Move to more strict typing
Browse files Browse the repository at this point in the history
Signed-off-by: Git'Fellow <[email protected]>
  • Loading branch information
solracsf committed Nov 25, 2024
1 parent 8995272 commit 373c7e8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/private/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use OC\Files\Filesystem;
use OC\Files\View;
use OCP\ICache;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use OCP\Server;
use Psr\Log\LoggerInterface;

class File implements ICache {
Expand All @@ -28,17 +30,18 @@ protected function getStorage() {
if ($this->storage !== null) {
return $this->storage;
}
if (\OC::$server->getUserSession()->isLoggedIn()) {
$session = Server::get(IUserSession::class);
if ($session->isLoggedIn()) {
$rootView = new View();
$user = \OC::$server->getUserSession()->getUser();
Filesystem::initMountPoints($user->getUID());
if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
$rootView->mkdir('/' . $user->getUID() . '/cache');
$userId = $session->getUser()->getUID();
Filesystem::initMountPoints($userId);
if (!$rootView->file_exists('/' . $userId . '/cache')) {
$rootView->mkdir('/' . $userId . '/cache');
}
$this->storage = new View('/' . $user->getUID() . '/cache');
$this->storage = new View('/' . $userId . '/cache');
return $this->storage;
} else {
\OCP\Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']);
Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']);
throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
}
}
Expand Down Expand Up @@ -83,15 +86,15 @@ public function set($key, $value, $ttl = 0) {
$storage = $this->getStorage();
$result = false;
// unique id to avoid chunk collision, just in case
$uniqueId = \OC::$server->get(ISecureRandom::class)->generate(
$uniqueId = Server::get(ISecureRandom::class)->generate(
16,
ISecureRandom::CHAR_ALPHANUMERIC
);

// use part file to prevent hasKey() to find the key
// while it is being written
$keyPart = $key . '.' . $uniqueId . '.part';
if ($storage and $storage->file_put_contents($keyPart, $value)) {
if ($storage && $storage->file_put_contents($keyPart, $value)) {
if ($ttl === 0) {
$ttl = 86400; // 60*60*24
}
Expand Down Expand Up @@ -134,11 +137,11 @@ public function remove($key) {
*/
public function clear($prefix = '') {
$storage = $this->getStorage();
if ($storage and $storage->is_dir('/')) {
if ($storage && $storage->is_dir('/')) {
$dh = $storage->opendir('/');
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' and $file != '..' and ($prefix === '' || str_starts_with($file, $prefix))) {
if ($file !== '.' && $file !== '..' && ($prefix === '' || str_starts_with($file, $prefix))) {
$storage->unlink('/' . $file);
}
}
Expand All @@ -162,19 +165,19 @@ public function gc() {
return null;
}
while (($file = readdir($dh)) !== false) {
if ($file != '.' and $file != '..') {
if ($file !== '.' && $file !== '..') {
try {
$mtime = $storage->filemtime('/' . $file);
if ($mtime < $now) {
$storage->unlink('/' . $file);
}
} catch (\OCP\Lock\LockedException $e) {
// ignore locked chunks
\OCP\Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
} catch (\OCP\Files\ForbiddenException $e) {
\OCP\Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']);
} catch (\OCP\Files\LockNotAcquiredException $e) {
\OCP\Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']);
}
}
}
Expand Down

0 comments on commit 373c7e8

Please sign in to comment.