Skip to content

Commit 052644e

Browse files
committed
fix cachjail searching for root
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent ceef59b commit 052644e

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

lib/private/Files/Cache/QuerySearchHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ private function getOperatorFieldAndValue(ISearchComparison $operator) {
166166
$field = 'tag.category';
167167
} elseif ($field === 'fileid') {
168168
$field = 'file.fileid';
169+
} elseif ($field === 'path' && $type === ISearchComparison::COMPARE_EQUAL) {
170+
$field = 'path_hash';
171+
$value = md5((string)$value);
169172
}
170173
return [$field, $value, $type];
171174
}

lib/private/Files/Cache/Wrapper/CacheJail.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ public function search($pattern) {
238238
$query = $this->getQueryBuilder();
239239
$query->selectFileCache()
240240
->whereStorageId()
241-
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')))
241+
->andWhere($query->expr()->orX(
242+
$query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')),
243+
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getRoot()))),
244+
))
242245
->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern)));
243246

244247
$result = $query->execute();
@@ -263,7 +266,10 @@ public function searchByMime($mimetype) {
263266
$query = $this->getQueryBuilder();
264267
$query->selectFileCache()
265268
->whereStorageId()
266-
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')));
269+
->andWhere($query->expr()->orX(
270+
$query->expr()->like('path', $query->createNamedParameter($this->getRoot() . '/%')),
271+
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($this->getRoot()))),
272+
));
267273

268274
if (strpos($mimetype, '/')) {
269275
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
@@ -287,9 +293,14 @@ public function searchQuery(ISearchQuery $query) {
287293
'path',
288294
$this->getRoot() . '/%'
289295
);
296+
$rootFilter = new SearchComparison(
297+
ISearchComparison::COMPARE_EQUAL,
298+
'path',
299+
$this->getRoot()
300+
);
290301
$operation = new SearchBinaryOperator(
291302
ISearchBinaryOperator::OPERATOR_AND,
292-
[$prefixFilter, $query->getSearchOperation()]
303+
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [$prefixFilter, $rootFilter]) , $query->getSearchOperation()]
293304
);
294305
$simpleQuery = new SearchQuery($operation, 0, 0, $query->getOrder(), $query->getUser());
295306
$results = $this->getCache()->searchQuery($simpleQuery);

tests/lib/Files/Cache/Wrapper/CacheJailTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CacheJailTest extends CacheTest {
3232
protected function setUp(): void {
3333
parent::setUp();
3434
$this->storage->mkdir('foo');
35+
$this->storage->getScanner()->scan('');
3536
$this->sourceCache = $this->cache;
3637
$this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo');
3738
}
@@ -49,6 +50,11 @@ public function testSearchOutsideJail() {
4950
$result = $this->cache->search('%foobar%');
5051
$this->assertCount(1, $result);
5152
$this->assertEquals('foobar', $result[0]['path']);
53+
54+
$result = $this->cache->search('%foo%');
55+
$this->assertCount(2, $result);
56+
$this->assertEquals('', $result[0]['path']);
57+
$this->assertEquals('foobar', $result[1]['path']);
5258
}
5359

5460
public function testSearchMimeOutsideJail() {
@@ -76,11 +82,15 @@ public function testSearchQueryOutsideJail() {
7682

7783
$user = new User('foo', null, $this->createMock(EventDispatcherInterface::class));
7884
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), 10, 0, [], $user);
79-
$this->assertCount(2, $this->sourceCache->searchQuery($query));
85+
$result = $this->cache->searchQuery($query);
8086

81-
$result = $this->cache->search('%foobar%');
8287
$this->assertCount(1, $result);
8388
$this->assertEquals('foobar', $result[0]['path']);
89+
90+
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user);
91+
$result = $this->cache->searchQuery($query);
92+
$this->assertCount(1, $result);
93+
$this->assertEquals('', $result[0]['path']);
8494
}
8595

8696
public function testClearKeepEntriesOutsideJail() {

0 commit comments

Comments
 (0)