Skip to content

Commit 395e0b4

Browse files
committed
feat: Forward sizeDifference on write
Remove unneeded oldSize hack Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 96e22c6 commit 395e0b4

File tree

2 files changed

+39
-45
lines changed

2 files changed

+39
-45
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
namespace OCA\Files_Trashbin\BackgroundJob;
2828

29+
use OC\Files\SetupManager;
2930
use OCA\Files_Trashbin\Expiration;
3031
use OCA\Files_Trashbin\Helper;
3132
use OCA\Files_Trashbin\Trashbin;
@@ -36,17 +37,18 @@
3637
use Psr\Log\LoggerInterface;
3738

3839
class ExpireTrash extends TimedJob {
40+
private const THIRTY_MINUTES = 30 * 60;
3941

4042
public function __construct(
4143
private IAppConfig $appConfig,
4244
private IUserManager $userManager,
4345
private Expiration $expiration,
4446
private LoggerInterface $logger,
47+
private SetupManager $setupManager,
4548
ITimeFactory $time
4649
) {
4750
parent::__construct($time);
48-
// Run once per 30 minutes
49-
$this->setInterval(60 * 30);
51+
$this->setInterval(self::THIRTY_MINUTES);
5052
}
5153

5254
protected function run($argument) {
@@ -60,44 +62,43 @@ protected function run($argument) {
6062
return;
6163
}
6264

63-
$stopTime = time() + 60 * 30; // Stops after 30 minutes.
64-
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
65-
$users = $this->userManager->getSeenUsers($offset);
65+
$stopTime = time() + self::THIRTY_MINUTES;
6666

67-
foreach ($users as $user) {
68-
try {
69-
$uid = $user->getUID();
70-
if (!$this->setupFS($uid)) {
71-
continue;
67+
do {
68+
$this->appConfig->clearCache();
69+
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
70+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset + 10);
71+
72+
$users = $this->userManager->getLastLoggedInUsers(10, $offset);
73+
74+
foreach ($users as $uid) {
75+
try {
76+
if ($this->setupFS($uid)) {
77+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
78+
Trashbin::deleteExpiredFiles($dirContent, $uid);
79+
}
80+
} catch (\Throwable $e) {
81+
$this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]);
7282
}
73-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
74-
Trashbin::deleteExpiredFiles($dirContent, $uid);
75-
} catch (\Throwable $e) {
76-
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
83+
84+
$this->setupManager->tearDown();
7785
}
7886

79-
$offset++;
87+
} while (time() < $stopTime && count($users) === 10);
8088

81-
if ($stopTime < time()) {
82-
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
83-
\OC_Util::tearDownFS();
84-
return;
85-
}
89+
if (count($users) < 10) {
90+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
8691
}
87-
88-
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
89-
\OC_Util::tearDownFS();
9092
}
9193

9294
/**
9395
* Act on behalf on trash item owner
9496
*/
95-
protected function setupFS(string $user): bool {
96-
\OC_Util::tearDownFS();
97-
\OC_Util::setupFS($user);
97+
protected function setupFS(string $uid): bool {
98+
$this->setupManager->setupForUser($this->userManager->get($uid));
9899

99100
// Check if this user has a trashbin directory
100-
$view = new \OC\Files\View('/' . $user);
101+
$view = new View('/' . $uid);
101102
if (!$view->is_dir('/files_trashbin/files')) {
102103
return false;
103104
}

apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace OCA\Files_Trashbin\Tests\BackgroundJob;
2727

28+
use OC\Files\SetupManager;
2829
use OCA\Files_Trashbin\BackgroundJob\ExpireTrash;
2930
use OCA\Files_Trashbin\Expiration;
3031
use OCP\AppFramework\Utility\ITimeFactory;
@@ -36,23 +37,14 @@
3637
use Test\TestCase;
3738

3839
class ExpireTrashTest extends TestCase {
39-
/** @var IAppConfig&MockObject */
40-
private $appConfig;
4140

42-
/** @var IUserManager&MockObject */
43-
private $userManager;
44-
45-
/** @var Expiration&MockObject */
46-
private $expiration;
47-
48-
/** @var IJobList&MockObject */
49-
private $jobList;
50-
51-
/** @var LoggerInterface&MockObject */
52-
private $logger;
53-
54-
/** @var ITimeFactory&MockObject */
55-
private $time;
41+
private IAppConfig&MockObject $appConfig;
42+
private IUserManager&MockObject $userManager;
43+
private Expiration&MockObject $expiration;
44+
private IJobList&MockObject $jobList;
45+
private LoggerInterface&MockObject $logger;
46+
private ITimeFactory&MockObject $time;
47+
private SetupManager&MockObject $setupManager;
5648

5749
protected function setUp(): void {
5850
parent::setUp();
@@ -62,6 +54,7 @@ protected function setUp(): void {
6254
$this->expiration = $this->createMock(Expiration::class);
6355
$this->jobList = $this->createMock(IJobList::class);
6456
$this->logger = $this->createMock(LoggerInterface::class);
57+
$this->setupManager = $this->createMock(SetupManager::class);
6558

6659
$this->time = $this->createMock(ITimeFactory::class);
6760
$this->time->method('getTime')
@@ -81,7 +74,7 @@ public function testConstructAndRun(): void {
8174
->with('files_trashbin', 'background_job_expire_trash_offset', 0)
8275
->willReturn(0);
8376

84-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
77+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->setupManager, $this->time);
8578
$job->start($this->jobList);
8679
}
8780

@@ -92,7 +85,7 @@ public function testBackgroundJobDeactivated(): void {
9285
$this->expiration->expects($this->never())
9386
->method('getMaxAgeAsTimestamp');
9487

95-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
88+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->setupManager, $this->time);
9689
$job->start($this->jobList);
9790
}
9891
}

0 commit comments

Comments
 (0)