diff options
author | Robin Appelman <robin@icewind.nl> | 2023-04-24 15:24:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-24 15:24:50 +0200 |
commit | 4953296b9e87037b742a463018974f4cda76752a (patch) | |
tree | 19057e1d5f2c6edcbdd334d046f6031687356b90 /lib/private | |
parent | e8a48b51dc074ee2f5d23d5f48369764f917d499 (diff) | |
parent | 5267a740274b69055da16afe6084b58b9655f003 (diff) | |
download | nextcloud-server-4953296b9e87037b742a463018974f4cda76752a.tar.gz nextcloud-server-4953296b9e87037b742a463018974f4cda76752a.zip |
Merge pull request #36857 from nextcloud/scan-repair-unencrypted-size
fix unencrypted_size for folders when scanning the filesystem with encryption enabled
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/Cache/Cache.php | 31 | ||||
-rw-r--r-- | lib/private/Files/Cache/HomeCache.php | 34 | ||||
-rw-r--r-- | lib/private/Files/Cache/Scanner.php | 20 | ||||
-rw-r--r-- | lib/private/Files/Cache/Wrapper/CacheJail.php | 2 | ||||
-rw-r--r-- | lib/private/Files/Cache/Wrapper/CacheWrapper.php | 2 | ||||
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Encryption.php | 5 |
6 files changed, 54 insertions, 40 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index afa464a8138..933fee5630f 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -899,10 +899,23 @@ class Cache implements ICache { * calculate the size of a folder and set it in the cache * * @param string $path - * @param array $entry (optional) meta data of the folder + * @param array|null|ICacheEntry $entry (optional) meta data of the folder * @return int */ public function calculateFolderSize($path, $entry = null) { + return $this->calculateFolderSizeInner($path, $entry); + } + + + /** + * inner function because we can't add new params to the public function without breaking any child classes + * + * @param string $path + * @param array|null|ICacheEntry $entry (optional) meta data of the folder + * @param bool $ignoreUnknown don't mark the folder size as unknown if any of it's children are unknown + * @return int + */ + protected function calculateFolderSizeInner(string $path, $entry = null, bool $ignoreUnknown = false) { $totalSize = 0; if (is_null($entry) || !isset($entry['fileid'])) { $entry = $this->get($path); @@ -914,6 +927,9 @@ class Cache implements ICache { $query->select('size', 'unencrypted_size') ->from('filecache') ->whereParent($id); + if ($ignoreUnknown) { + $query->andWhere($query->expr()->gte('size', $query->createNamedParameter(0))); + } $result = $query->execute(); $rows = $result->fetchAll(); @@ -954,9 +970,16 @@ class Cache implements ICache { $unencryptedTotal = 0; $unencryptedMax = 0; } - if ($entry['size'] !== $totalSize) { - // only set unencrypted size for a folder if any child entries have it set, or the folder is empty - if ($unencryptedMax > 0 || $totalSize === 0) { + + // only set unencrypted size for a folder if any child entries have it set, or the folder is empty + $shouldWriteUnEncryptedSize = $unencryptedMax > 0 || $totalSize === 0 || $entry['unencrypted_size'] > 0; + if ($entry['size'] !== $totalSize || ($entry['unencrypted_size'] !== $unencryptedTotal && $shouldWriteUnEncryptedSize)) { + if ($shouldWriteUnEncryptedSize) { + // if all children have an unencrypted size of 0, just set the folder unencrypted size to 0 instead of summing the sizes + if ($unencryptedMax === 0) { + $unencryptedTotal = 0; + } + $this->update($id, [ 'size' => $totalSize, 'unencrypted_size' => $unencryptedTotal, diff --git a/lib/private/Files/Cache/HomeCache.php b/lib/private/Files/Cache/HomeCache.php index 5ea65b4d1cb..9dbbb46c57b 100644 --- a/lib/private/Files/Cache/HomeCache.php +++ b/lib/private/Files/Cache/HomeCache.php @@ -35,7 +35,7 @@ class HomeCache extends Cache { * get the size of a folder and set it in the cache * * @param string $path - * @param array $entry (optional) meta data of the folder + * @param array|null|ICacheEntry $entry (optional) meta data of the folder * @return int */ public function calculateFolderSize($path, $entry = null) { @@ -44,37 +44,9 @@ class HomeCache extends Cache { } elseif ($path === '' or $path === '/') { // since the size of / isn't used (the size of /files is used instead) there is no use in calculating it return 0; + } else { + return $this->calculateFolderSizeInner($path, $entry, true); } - - $totalSize = 0; - if (is_null($entry)) { - $entry = $this->get($path); - } - if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { - $id = $entry['fileid']; - - $query = $this->connection->getQueryBuilder(); - $query->selectAlias($query->func()->sum('size'), 'f1') - ->from('filecache') - ->where($query->expr()->eq('parent', $query->createNamedParameter($id))) - ->andWhere($query->expr()->eq('storage', $query->createNamedParameter($this->getNumericStorageId()))) - ->andWhere($query->expr()->gte('size', $query->createNamedParameter(0))); - - $result = $query->execute(); - $row = $result->fetch(); - $result->closeCursor(); - - if ($row) { - [$sum] = array_values($row); - $totalSize = 0 + $sum; - $entry['size'] += 0; - if ($entry['size'] !== $totalSize) { - $this->update($id, ['size' => $totalSize]); - } - } - $result->closeCursor(); - } - return $totalSize; } /** diff --git a/lib/private/Files/Cache/Scanner.php b/lib/private/Files/Cache/Scanner.php index d390037d0b8..6969828782a 100644 --- a/lib/private/Files/Cache/Scanner.php +++ b/lib/private/Files/Cache/Scanner.php @@ -36,6 +36,7 @@ namespace OC\Files\Cache; use Doctrine\DBAL\Exception; +use OC\Files\Storage\Wrapper\Encryption; use OCP\Files\Cache\IScanner; use OCP\Files\ForbiddenException; use OCP\Files\NotFoundException; @@ -208,9 +209,17 @@ class Scanner extends BasicEmitter implements IScanner { $data['etag'] = $etag; } } + + // we only updated unencrypted_size if it's already set + if ($cacheData['unencrypted_size'] === 0) { + unset($data['unencrypted_size']); + } + // Only update metadata that has changed $newData = array_diff_assoc($data, $cacheData->getData()); } else { + // we only updated unencrypted_size if it's already set + unset($data['unencrypted_size']); $newData = $data; $fileId = -1; } @@ -396,8 +405,15 @@ class Scanner extends BasicEmitter implements IScanner { } } $oldSize = $data['size'] ?? null; - if ($this->cacheActive && $oldSize !== $size) { - $this->cache->update($folderId, ['size' => $size]); + + // for encrypted storages, we trigger a regular folder size calculation instead of using the calculated size + // to make sure we also updated the unencrypted-size where applicable + if ($this->storage->instanceOfStorage(Encryption::class)) { + $this->cache->calculateFolderSize($path); + } else { + if ($this->cacheActive && $oldSize !== $size) { + $this->cache->update($folderId, ['size' => $size]); + } } $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', [$path, $this->storageId]); return $size; diff --git a/lib/private/Files/Cache/Wrapper/CacheJail.php b/lib/private/Files/Cache/Wrapper/CacheJail.php index 628ca3ee0e0..a5075ceef86 100644 --- a/lib/private/Files/Cache/Wrapper/CacheJail.php +++ b/lib/private/Files/Cache/Wrapper/CacheJail.php @@ -239,7 +239,7 @@ class CacheJail extends CacheWrapper { * get the size of a folder and set it in the cache * * @param string $path - * @param array $entry (optional) meta data of the folder + * @param array|null|ICacheEntry $entry (optional) meta data of the folder * @return int */ public function calculateFolderSize($path, $entry = null) { diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index 2cd378ad23c..c1043fbc6fb 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -249,7 +249,7 @@ class CacheWrapper extends Cache { * get the size of a folder and set it in the cache * * @param string $path - * @param array $entry (optional) meta data of the folder + * @param array|null|ICacheEntry $entry (optional) meta data of the folder * @return int */ public function calculateFolderSize($path, $entry = null) { diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index 0eba59d2156..720111793de 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -182,10 +182,12 @@ class Encryption extends Wrapper { if (isset($this->unencryptedSize[$fullPath])) { $data['encrypted'] = true; $data['size'] = $this->unencryptedSize[$fullPath]; + $data['unencrypted_size'] = $data['size']; } else { if (isset($info['fileid']) && $info['encrypted']) { $data['size'] = $this->verifyUnencryptedSize($path, $info->getUnencryptedSize()); $data['encrypted'] = true; + $data['unencrypted_size'] = $data['size']; } } @@ -496,7 +498,8 @@ class Encryption extends Wrapper { $result = $unencryptedSize; if ($unencryptedSize < 0 || - ($size > 0 && $unencryptedSize === $size) + ($size > 0 && $unencryptedSize === $size) || + $unencryptedSize > $size ) { // check if we already calculate the unencrypted size for the // given path to avoid recursions |