Skip to content

Commit

Permalink
Merge pull request #37430 from nextcloud/trash-cleanup-logging
Browse files Browse the repository at this point in the history
add a bit more verbose option for trashbin cleanup
  • Loading branch information
icewind1991 authored Apr 5, 2023
2 parents e60ffea + 9a731ad commit a1b7f13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
27 changes: 20 additions & 7 deletions apps/files_trashbin/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ protected function configure() {

protected function execute(InputInterface $input, OutputInterface $output): int {
$users = $input->getArgument('user_id');
$verbose = $input->getOption('verbose');
if ((!empty($users)) and ($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>");
$this->removeDeletedFiles($user);
$this->removeDeletedFiles($user, $output, $verbose);
} else {
$output->writeln("<error>Unknown user $user</error>");
return 1;
Expand All @@ -104,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$this->removeDeletedFiles($user);
$this->removeDeletedFiles($user, $output, $verbose);
}
$offset += $limit;
} while (count($users) >= $limit);
Expand All @@ -117,19 +118,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

if ($verbose) {
$output->writeln("Deleting <info>" . \OC_Helper::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->execute();
} else {
if ($verbose) {
$output->writeln("No trash found for <info>$uid</info>");
}
}
}
}
52 changes: 32 additions & 20 deletions apps/files_trashbin/tests/Command/CleanUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IDBConnection;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

Expand Down Expand Up @@ -101,27 +102,27 @@ public function initTable() {
* @dataProvider dataTestRemoveDeletedFiles
* @param boolean $nodeExists
*/
public function testRemoveDeletedFiles($nodeExists) {
public function testRemoveDeletedFiles(bool $nodeExists) {
$this->initTable();
$this->rootFolder->expects($this->once())
$this->rootFolder
->method('nodeExists')
->with('/' . $this->user0 . '/files_trashbin')
->willReturn($nodeExists);
->willReturnOnConsecutiveCalls($nodeExists, false);
if ($nodeExists) {
$this->rootFolder->expects($this->once())
$this->rootFolder
->method('get')
->with('/' . $this->user0 . '/files_trashbin')
->willReturn($this->rootFolder);
$this->rootFolder->expects($this->once())
$this->rootFolder
->method('delete');
} else {
$this->rootFolder->expects($this->never())->method('get');
$this->rootFolder->expects($this->never())->method('delete');
}
$this->invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0]);
$this->invokePrivate($this->cleanup, 'removeDeletedFiles', [$this->user0, new NullOutput(), false]);

if ($nodeExists) {
// if the delete operation was execute only files from user1
// if the delete operation was executed only files from user1
// should be left.
$query = $this->dbConnection->getQueryBuilder();
$query->select('user')
Expand All @@ -136,7 +137,7 @@ public function testRemoveDeletedFiles($nodeExists) {
$this->assertSame('user1', $r['user']);
}
} else {
// if no delete operation was execute we should still have all 10
// if no delete operation was executed we should still have all 10
// database entries
$getAllQuery = $this->dbConnection->getQueryBuilder();
$result = $getAllQuery->select('id')
Expand Down Expand Up @@ -171,9 +172,14 @@ public function testExecuteDeleteListOfUsers() {
->method('userExists')->willReturn(true);
$inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
->disableOriginalConstructor()->getMock();
$inputInterface->expects($this->once())->method('getArgument')
$inputInterface->method('getArgument')
->with('user_id')
->willReturn($userIds);
$inputInterface->method('getOption')
->willReturnMap([
['all-users', false],
['verbose', false],
]);
$outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
->disableOriginalConstructor()->getMock();
$this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
Expand All @@ -190,7 +196,7 @@ public function testExecuteAllUsers() {
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->dbConnection])
->getMock();
$backend = $this->createMock(\OCP\UserInterface::class);
$backend->expects($this->once())->method('getUsers')
$backend->method('getUsers')
->with('', 500, 0)
->willReturn($backendUsers);
$instance->expects($this->exactly(count($backendUsers)))
Expand All @@ -199,27 +205,31 @@ public function testExecuteAllUsers() {
$this->assertTrue(in_array($user, $backendUsers));
});
$inputInterface = $this->createMock(InputInterface::class);
$inputInterface->expects($this->once())->method('getArgument')
$inputInterface->method('getArgument')
->with('user_id')
->willReturn($userIds);
$inputInterface->method('getOption')
->with('all-users')
->willReturn(true);
->willReturnMap([
['all-users', true],
['verbose', false],
]);
$outputInterface = $this->createMock(OutputInterface::class);
$this->userManager->expects($this->once())
$this->userManager
->method('getBackends')
->willReturn([$backend]);
$this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
}

public function testExecuteNoUsersAndNoAllUsers() {
$inputInterface = $this->createMock(InputInterface::class);
$inputInterface->expects($this->once())->method('getArgument')
$inputInterface->method('getArgument')
->with('user_id')
->willReturn([]);
$inputInterface->method('getOption')
->with('all-users')
->willReturn(false);
->willReturnMap([
['all-users', false],
['verbose', false],
]);
$outputInterface = $this->createMock(OutputInterface::class);

$this->expectException(InvalidOptionException::class);
Expand All @@ -230,12 +240,14 @@ public function testExecuteNoUsersAndNoAllUsers() {

public function testExecuteUsersAndAllUsers() {
$inputInterface = $this->createMock(InputInterface::class);
$inputInterface->expects($this->once())->method('getArgument')
$inputInterface->method('getArgument')
->with('user_id')
->willReturn(['user1', 'user2']);
$inputInterface->method('getOption')
->with('all-users')
->willReturn(true);
->willReturnMap([
['all-users', true],
['verbose', false],
]);
$outputInterface = $this->createMock(OutputInterface::class);

$this->expectException(InvalidOptionException::class);
Expand Down

0 comments on commit a1b7f13

Please sign in to comment.