Skip to content

Commit 68c4740

Browse files
committed
fix(files): report new files as new in the files:scan summary
The addToCache listener decides between NodeAddedToCache and FileCacheUpdated by checking `if ($fileId)`, but Cache\Scanner emits $fileId = -1 for newly inserted entries, which is truthy. As a result the NodeAddedToCache branch was unreachable and `occ files:scan` has reported every new file as "Updated" (never "New") since the summary was introduced in 292c0e5. Check for the actual -1 sentinel instead, and add a regression test asserting that a first scan dispatches NodeAddedToCache and a re-scan of a modified file dispatches FileCacheUpdated. Assisted-by: ClaudeCode:claude-fable-5 Signed-off-by: Nick Manning <nicholas.manning@gmail.com>
1 parent ff5db60 commit 68c4740

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

lib/private/Files/Utils/Scanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function scan(string $dir = '', $recursive = \OC\Files\Cache\Scanner::SCA
208208
});
209209
$scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path, $storageId, $data, $fileId) use ($storage): void {
210210
$this->triggerPropagator($storage, $path);
211-
if ($fileId) {
211+
if ($fileId !== -1) {
212212
$this->eventDispatcher->dispatchTyped(new FileCacheUpdated($storage, $path));
213213
} else {
214214
$this->eventDispatcher->dispatchTyped(new NodeAddedToCache($storage, $path));

tests/lib/Files/Utils/ScannerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use OCP\EventDispatcher\IEventDispatcher;
1919
use OCP\Files\Config\IMountProvider;
2020
use OCP\Files\Config\IMountProviderCollection;
21+
use OCP\Files\Events\FileCacheUpdated;
22+
use OCP\Files\Events\NodeAddedToCache;
2123
use OCP\Files\Storage\IStorageFactory;
2224
use OCP\IDBConnection;
2325
use OCP\IUser;
@@ -209,6 +211,53 @@ public function testPropagateEtag(): void {
209211
$this->assertNotEquals($oldRoot->getEtag(), $newRoot->getEtag());
210212
}
211213

214+
public function testScanDispatchesAddedForNewAndUpdatedForChangedEntries(): void {
215+
$storage = new Temporary([]);
216+
$mount = new MountPoint($storage, '');
217+
Filesystem::getMountManager()->addMount($mount);
218+
219+
$storage->mkdir('folder');
220+
$storage->file_put_contents('folder/bar.txt', 'qwerty');
221+
$storage->touch('folder/bar.txt', time() - 200);
222+
223+
/** @var list<object> $events */
224+
$events = [];
225+
$dispatcher = $this->createMock(IEventDispatcher::class);
226+
$dispatcher->method('dispatchTyped')
227+
->willReturnCallback(function (object $event) use (&$events): void {
228+
$events[] = $event;
229+
});
230+
231+
$scanner = new TestScanner(
232+
Server::get(IUserManager::class)->get(''),
233+
Server::get(IDBConnection::class),
234+
$dispatcher,
235+
Server::get(LoggerInterface::class),
236+
Server::get(SetupManager::class),
237+
);
238+
$scanner->addMount($mount);
239+
240+
$pathsForEvents = function (string $class) use (&$events): array {
241+
return array_values(array_map(
242+
static fn (object $event): string => $event->getPath(),
243+
array_filter($events, static fn (object $event): bool => $event instanceof $class),
244+
));
245+
};
246+
247+
// first scan: everything is new, nothing is updated
248+
$scanner->scan('');
249+
$this->assertContains('folder/bar.txt', $pathsForEvents(NodeAddedToCache::class));
250+
$this->assertContains('folder', $pathsForEvents(NodeAddedToCache::class));
251+
$this->assertSame([], $pathsForEvents(FileCacheUpdated::class));
252+
253+
// re-scan after modifying the file: updated, not new
254+
$events = [];
255+
$storage->file_put_contents('folder/bar.txt', 'qwerty asdf');
256+
$scanner->scan('');
257+
$this->assertContains('folder/bar.txt', $pathsForEvents(FileCacheUpdated::class));
258+
$this->assertSame([], $pathsForEvents(NodeAddedToCache::class));
259+
}
260+
212261
public function testShallow(): void {
213262
$storage = new Temporary([]);
214263
$mount = new MountPoint($storage, '');

0 commit comments

Comments
 (0)