Skip to content

Commit a25e765

Browse files
committed
Revert "Remove inefficient fed share scanner"
This reverts commit ce31914. Signed-off-by: Carl Schwan <carl@carlschwan.eu> (cherry picked from commit 6667007)
1 parent be236ef commit a25e765

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

apps/files_sharing/lib/External/Scanner.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,29 @@
2929
use OCP\Files\NotFoundException;
3030
use OCP\Files\StorageInvalidException;
3131
use OCP\Files\StorageNotAvailableException;
32+
use OCP\Http\Client\LocalServerException;
33+
use Psr\Log\LoggerInterface;
3234

3335
class Scanner extends \OC\Files\Cache\Scanner {
3436
/** @var \OCA\Files_Sharing\External\Storage */
3537
protected $storage;
3638

39+
/** {@inheritDoc} */
40+
public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
41+
try {
42+
if (!$this->storage->remoteIsOwnCloud()) {
43+
return parent::scan($path, $recursive, $reuse, $lock);
44+
}
45+
} catch (LocalServerException $e) {
46+
// Scanner doesn't have dependency injection
47+
\OC::$server->get(LoggerInterface::class)
48+
->warning('Trying to scan files inside invalid external storage: ' . $this->storage->getRemote() . ' for mountpoint ' . $this->storage->getMountPoint() . ' and id ' . $this->storage->getId());
49+
return;
50+
}
51+
52+
$this->scanAll();
53+
}
54+
3755
/**
3856
* Scan a single file and store it in the cache.
3957
* If an exception happened while accessing the external storage,
@@ -63,4 +81,56 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData =
6381
$this->storage->checkStorageAvailability();
6482
}
6583
}
84+
85+
/**
86+
* Checks the remote share for changes.
87+
* If changes are available, scan them and update
88+
* the cache.
89+
* @throws NotFoundException
90+
* @throws StorageInvalidException
91+
* @throws \Exception
92+
*/
93+
public function scanAll() {
94+
try {
95+
$data = $this->storage->getShareInfo();
96+
} catch (\Exception $e) {
97+
$this->storage->checkStorageAvailability();
98+
throw new \Exception(
99+
'Error while scanning remote share: "' .
100+
$this->storage->getRemote() . '" ' .
101+
$e->getMessage()
102+
);
103+
}
104+
if ($data['status'] === 'success') {
105+
$this->addResult($data['data'], '');
106+
} else {
107+
throw new \Exception(
108+
'Error while scanning remote share: "' .
109+
$this->storage->getRemote() . '"'
110+
);
111+
}
112+
}
113+
114+
/**
115+
* @param array $data
116+
* @param string $path
117+
*/
118+
private function addResult($data, $path) {
119+
$id = $this->cache->put($path, $data);
120+
if (isset($data['children'])) {
121+
$children = [];
122+
foreach ($data['children'] as $child) {
123+
$children[$child['name']] = true;
124+
$this->addResult($child, ltrim($path . '/' . $child['name'], '/'));
125+
}
126+
127+
$existingCache = $this->cache->getFolderContentsById($id);
128+
foreach ($existingCache as $existingChild) {
129+
// if an existing child is not in the new data, remove it
130+
if (!isset($children[$existingChild['name']])) {
131+
$this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/'));
132+
}
133+
}
134+
}
135+
}
66136
}

apps/files_sharing/tests/External/ScannerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ protected function setUp(): void {
5050
$this->scanner = new Scanner($this->storage);
5151
}
5252

53+
public function testScanAll() {
54+
$this->storage->expects($this->any())
55+
->method('getShareInfo')
56+
->willReturn(['status' => 'success', 'data' => []]);
57+
58+
// FIXME add real tests, we are currently only checking for
59+
// Declaration of OCA\Files_Sharing\External\Scanner::*() should be
60+
// compatible with OC\Files\Cache\Scanner::*()
61+
$this->scanner->scanAll();
62+
$this->addToAssertionCount(1);
63+
}
64+
5365
public function testScan() {
5466
$this->storage->expects($this->any())
5567
->method('getShareInfo')

0 commit comments

Comments
 (0)