]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix unencrypted folder size when no children with unencrypted size set are left
authorRobin Appelman <robin@icewind.nl>
Thu, 9 Mar 2023 14:25:46 +0000 (15:25 +0100)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Wed, 11 Oct 2023 12:41:43 +0000 (14:41 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/Files/Cache/Cache.php
lib/private/Files/Cache/HomeCache.php

index 7ad16e34a62d4ad72618775e26f2d83ed362638f..99f3602fe51b76d4e6380705058d3736da804eb9 100644 (file)
@@ -883,9 +883,23 @@ class Cache implements ICache {
         *
         * @param string $path
         * @param array $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|float
         */
        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 $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, array $entry = null, bool $ignoreUnknown = false) {
                $totalSize = 0;
                if (is_null($entry) || !isset($entry['fileid'])) {
                        $entry = $this->get($path);
@@ -897,6 +911,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();
@@ -939,9 +956,15 @@ class Cache implements ICache {
                        }
 
                        // only set unencrypted size for a folder if any child entries have it set, or the folder is empty
-                       $shouldWriteUnEncryptedSize = $unencryptedMax > 0 || $totalSize === 0;
+                       $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,
index cd6408a86f6911cc447d630a0ce7d4259f703164..ebf81c91385a05784dfd5648aad525e8a6e2f651 100644 (file)
@@ -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;
        }
 
        /**