Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions lib/private/Files/Cache/FileAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,20 @@ public function getByAncestorInStorage(int $storageId, int $folderId, int $fileI

$path = $root['path'] === '' ? '' : $root['path'] . '/';

$qb->selectDistinct('*')
$qb->selectDistinct('f.*')
->from('filecache', 'f')
->where($qb->expr()->like('f.path', $qb->createNamedParameter($this->connection->escapeLikeParameter($path) . '%')))
->andWhere($qb->expr()->eq('f.storage', $qb->createNamedParameter($storageId)))
->andWhere($qb->expr()->gt('f.fileid', $qb->createNamedParameter($fileIdCursor, IQueryBuilder::PARAM_INT)));
->andWhere($qb->expr()->gt('f.fileid', $qb->createNamedParameter($fileIdCursor, IQueryBuilder::PARAM_INT)))
->hintShardKey('storage', $storageId);

if (!$endToEndEncrypted) {
if (!$endToEndEncrypted && $this->connection->getShardDefinition('filecache') === null) {
// End to end encrypted files are descendants of a folder with encrypted=1
// Use a subquery to check the `encrypted` status of the parent folder
$subQuery = $this->getQuery()->select('p.encrypted')
->from('filecache', 'p')
->andWhere($qb->expr()->eq('p.fileid', 'f.parent'))
->getSQL();
// We can only do this inner join if the filecache table is not sharded
$qb->innerJoin('f', 'filecache', 'f2', $qb->expr()->eq('f2.fileid', 'f.parent'));

$qb->andWhere(
$qb->expr()->eq($qb->createFunction(sprintf('(%s)', $subQuery)), $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('f2.encrypted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
);
}

Expand All @@ -148,11 +146,42 @@ public function getByAncestorInStorage(int $storageId, int $folderId, int $fileI
$qb->orderBy('f.fileid', 'ASC');
$files = $qb->executeQuery();

while (
/** @var array */
$row = $files->fetch()
) {
yield Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
if (!$endToEndEncrypted && $this->connection->getShardDefinition('filecache') !== null) {
// End to end encrypted files are descendants of a folder with encrypted=1
// If the filecache table is sharded we need to check with a separate query if the parent is encrypted
$rows = [];
do {
while (count($rows) < 1000 && ($row = $files->fetch())) {
$rows[] = $row;
}
$parents = array_map(function ($row) {
return $row['parent'];
}, $rows);

$parentQuery = $this->getQuery();
$parentQuery->select('fileid', 'encrypted')->from('filecache');
$parentQuery->where($parentQuery->expr()->in('fileid', $parentQuery->createNamedParameter($parents, IQueryBuilder::PARAM_INT_ARRAY)));
$parentQuery->hintShardKey('storage', $storageId);
$result = $parentQuery->executeQuery();
$parentRows = $result->fetchAll();
$result->closeCursor();

$encryptedByFileId = array_column($parentRows, 'encrypted', 'fileid');
foreach ($rows as $row) {
if ($encryptedByFileId[$row['parent']]) {
continue;
}
yield Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
}
$rows = [];
} while ($rows[] = $files->fetch());
} else {
while (
/** @var array */
$row = $files->fetch()
) {
yield Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
}
}

$files->closeCursor();
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/Files/Cache/FileAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('files'),
'mimetype' => 1,
'encrypted' => 0,
'size' => 1,
])
->executeStatement();

Expand All @@ -224,6 +225,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('documents'),
'mimetype' => 2,
'encrypted' => 1,
'size' => 1,
])
->executeStatement();

Expand All @@ -237,6 +239,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('photos'),
'mimetype' => 3,
'encrypted' => 1,
'size' => 1,
])
->executeStatement();

Expand All @@ -250,6 +253,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('endtoendencrypted'),
'mimetype' => 4,
'encrypted' => 0,
'size' => 1,
])
->executeStatement();

Expand All @@ -263,6 +267,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('serversideencrypted'),
'mimetype' => 4,
'encrypted' => 1,
'size' => 1,
])
->executeStatement();

Expand All @@ -276,6 +281,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('storage2'),
'mimetype' => 5,
'encrypted' => 0,
'size' => 1,
])
->executeStatement();

Expand All @@ -289,6 +295,7 @@ private function setUpTestDatabaseForGetByAncestorInStorage(): void {
'name' => $queryBuilder->createNamedParameter('file'),
'mimetype' => 6,
'encrypted' => 0,
'size' => 1,
])
->executeStatement();
}
Expand Down
Loading