Skip to content
Closed
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
36 changes: 19 additions & 17 deletions apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Override;
use Psr\Log\LoggerInterface;

class ExpireTrash extends TimedJob {
Expand All @@ -40,7 +41,8 @@ public function __construct(
$this->setInterval(self::THIRTY_MINUTES);
}

protected function run($argument) {
#[Override]
protected function run($argument): void {
$backgroundJob = $this->appConfig->getValueBool(Application::APP_ID, self::TOGGLE_CONFIG_KEY_NAME, true);
if (!$backgroundJob) {
return;
Expand All @@ -64,9 +66,8 @@ protected function run($argument) {
$count++;

try {
if ($this->setupFS($user)) {
Trashbin::expire($uid);
}
$folder = $this->getTrashRoot($user);
Trashbin::expire($folder, $user);
} catch (\Throwable $e) {
$this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]);
} finally {
Expand All @@ -82,23 +83,19 @@ protected function run($argument) {
}
}

/**
* Act on behalf on trash item owner
*/
protected function setupFS(IUser $user): bool {
private function getTrashRoot(IUser $user): Folder {
$this->setupManager->tearDown();
$this->setupManager->setupForUser($user);

// Check if this user has a trashbin directory
$view = new View('/' . $user->getUID());
if (!$view->is_dir('/files_trashbin/files')) {
return false;
$folder = $this->rootFolder->getUserFolder($user->getUID())->getParent()->get('files_trashbin');
if (!$folder instanceof Folder) {
throw new \LogicException("Didn't expect files_trashbin to be a file instead of a folder");
}

return true;
return $folder;
}

private function getNextOffset(): int {
return $this->runMutexOperation(function () {
return $this->runMutexOperation(function (): int {
$this->appConfig->clearCache();

$offset = $this->appConfig->getValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0);
Expand All @@ -109,13 +106,18 @@ private function getNextOffset(): int {

}

private function resetOffset() {
private function resetOffset(): void {
$this->runMutexOperation(function (): void {
$this->appConfig->setValueInt(Application::APP_ID, self::OFFSET_CONFIG_KEY_NAME, 0);
});
}

private function runMutexOperation($operation): mixed {
/**
* @template T
* @param callable(): T $operation
* @return T
*/
private function runMutexOperation(callable $operation): mixed {
$acquired = false;

while ($acquired === false) {
Expand Down
82 changes: 46 additions & 36 deletions apps/files_trashbin/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@
*/
namespace OCA\Files_Trashbin\Command;

use OC\Core\Command\Base;
use OC\Files\SetupManager;
use OC\User\LazyUser;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\Util;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CleanUp extends Command {
class CleanUp extends Base {

public function __construct(
protected IRootFolder $rootFolder,
protected IUserManager $userManager,
protected IDBConnection $dbConnection,
protected SetupManager $setupManager,
) {
parent::__construct();
}

protected function configure() {
protected function configure(): void {
parent::configure();
$this
->setName('trashbin:cleanup')
->setDescription('Remove deleted files')
Expand All @@ -47,17 +54,18 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$users = $input->getArgument('user_id');
$userIds = $input->getArgument('user_id');
$verbose = $input->getOption('verbose');
if (!empty($users) && $input->getOption('all-users')) {
if (!empty($userIds) && $input->getOption('all-users')) {
throw new InvalidOptionException('Either specify a user_id or --all-users');
} elseif (!empty($users)) {
foreach ($users as $user) {
if ($this->userManager->userExists($user)) {
$output->writeln("Remove deleted files of <info>$user</info>");
} elseif (!empty($userIds)) {
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
if ($user) {
$output->writeln("Remove deleted files of <info>$userId</info>");
$this->removeDeletedFiles($user, $output, $verbose);
} else {
$output->writeln("<error>Unknown user $user</error>");
$output->writeln("<error>Unknown user $userId</error>");
return 1;
}
}
Expand All @@ -72,13 +80,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$limit = 500;
$offset = 0;
do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$userIds = $backend->getUsers('', $limit, $offset);
foreach ($userIds as $userId) {
$output->writeln(" <info>$userId</info>");
$user = new LazyUser($userId, $this->userManager, null, $backend);
$this->removeDeletedFiles($user, $output, $verbose);
}
$offset += $limit;
} while (count($users) >= $limit);
} while (count($userIds) >= $limit);
}
} else {
throw new InvalidOptionException('Either specify a user_id or --all-users');
Expand All @@ -87,32 +96,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* remove deleted files for the given user
* Remove deleted files for the given user.
*/
protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void {
\OC_Util::tearDownFS();
\OC_Util::setupFS($uid);
$path = '/' . $uid . '/files_trashbin';
if ($this->rootFolder->nodeExists($path)) {
protected function removeDeletedFiles(IUser $user, OutputInterface $output, bool $verbose): void {
$this->setupManager->tearDown();
$this->setupManager->setupForUser($user);
$path = '/' . $user->getUID() . '/files_trashbin';
try {
$node = $this->rootFolder->get($path);

if ($verbose) {
$output->writeln('Deleting <info>' . Util::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
}
$node->delete();
if ($this->rootFolder->nodeExists($path)) {
$output->writeln('<error>Trash folder sill exists after attempting to delete it</error>');
return;
}
$query = $this->dbConnection->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createParameter('uid')))
->setParameter('uid', $uid);
$query->executeStatement();
} else {
} catch (NotFoundException|NotPermittedException) {
if ($verbose) {
$output->writeln("No trash found for <info>$uid</info>");
$output->writeln("No trash found for <info>{$user->getUID()}</info>");
}
return;
}

if ($verbose) {
$output->writeln('Deleting <info>' . Util::humanFileSize($node->getSize()) . "</info> in trash for <info>{$user->getUID()}</info>.");
}
$node->delete();
if ($this->rootFolder->nodeExists($path)) {
$output->writeln('<error>Trash folder sill exists after attempting to delete it</error>');
return;
}
$query = $this->dbConnection->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createParameter('uid')))
->setParameter('uid', $user->getUID());
$query->executeStatement();
}
}
36 changes: 26 additions & 10 deletions apps/files_trashbin/lib/Command/Expire.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,48 @@
namespace OCA\Files_Trashbin\Command;

use OC\Command\FileAccess;
use OC\Files\SetupManager;
use OCA\Files_Trashbin\Trashbin;
use OCP\Command\ICommand;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IUserManager;
use OCP\Server;
use Override;
use Psr\Log\LoggerInterface;

class Expire implements ICommand {
use FileAccess;

/**
* @param string $user
*/
public function __construct(
private $user,
private readonly string $userId,
) {
}

public function handle() {
#[Override]
public function handle(): void {
// can't use DI because Expire needs to be serializable
$userManager = Server::get(IUserManager::class);
if (!$userManager->userExists($this->user)) {
$user = $userManager->get($this->userId);
if (!$user) {
// User has been deleted already
return;
}

\OC_Util::tearDownFS();
\OC_Util::setupFS($this->user);
Trashbin::expire($this->user);
\OC_Util::tearDownFS();
try {
$setupManager = Server::get(SetupManager::class);
$setupManager->tearDown();
$setupManager->setupForUser($user);

$trashRoot = Server::get(IRootFolder::class)->getUserFolder($user->getUID())->getParent()->get('files_trashbin');
if (!$trashRoot instanceof Folder) {
throw new \LogicException("Didn't expect files_trashbin to be a file instead of a folder");
}
Trashbin::expire($trashRoot, $user);
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
} finally {
$setupManager->tearDown();
}
}
}
Loading