From dd4e33fe6b1cf9feccaeae33c81fc8b08fb24d4d Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Sun, 28 Jul 2013 16:14:49 -0400 Subject: [PATCH 1/3] Fix calculating size for empty folders --- lib/files/cache/cache.php | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php index 3818fdbd840..458df56141c 100644 --- a/lib/files/cache/cache.php +++ b/lib/files/cache/cache.php @@ -485,27 +485,24 @@ class Cache { * @return int */ public function calculateFolderSize($path) { - $id = $this->getId($path); - if ($id === -1) { - return 0; - } - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?'; - $result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId())); $totalSize = 0; - $hasChilds = 0; - while ($row = $result->fetchRow()) { - $hasChilds = true; - $size = (int)$row['size']; - if ($size === -1) { - $totalSize = -1; - break; - } else { - $totalSize += $size; + $entry = $this->get($path); + if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { + $id = $entry['fileid']; + $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?'; + $result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId())); + while ($row = $result->fetchRow()) { + $size = (int)$row['size']; + if ($size === -1) { + $totalSize = -1; + break; + } else { + $totalSize += $size; + } + } + if ($entry['size'] !== $totalSize) { + $this->update($id, array('size' => $totalSize)); } - } - - if ($hasChilds) { - $this->update($id, array('size' => $totalSize)); } return $totalSize; } From 1faac6108c0c51d28021eb0676db9ad2b94a6f3d Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Mon, 29 Jul 2013 10:22:44 -0400 Subject: [PATCH 2/3] Use query to calculate folder size --- lib/files/cache/cache.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php index 458df56141c..c3d11ab4cfe 100644 --- a/lib/files/cache/cache.php +++ b/lib/files/cache/cache.php @@ -489,19 +489,22 @@ class Cache { $entry = $this->get($path); if ($entry && $entry['mimetype'] === 'httpd/unix-directory') { $id = $entry['fileid']; - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?'; + $sql = 'SELECT SUM(`size`), MIN(`size`) FROM `*PREFIX*filecache` '. + 'WHERE `parent` = ? AND `storage` = ?'; $result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId())); - while ($row = $result->fetchRow()) { - $size = (int)$row['size']; - if ($size === -1) { - $totalSize = -1; - break; + if ($row = $result->fetchRow()) { + list($sum, $min) = array_values($row); + $sum = (int)$sum; + $min = (int)$min; + if ($min === -1) { + $totalSize = $min; } else { - $totalSize += $size; + $totalSize = $sum; } - } - if ($entry['size'] !== $totalSize) { - $this->update($id, array('size' => $totalSize)); + if ($entry['size'] !== $totalSize) { + $this->update($id, array('size' => $totalSize)); + } + } } return $totalSize; From afff75001156e38620e08f81cad6372abb54ebb1 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Mon, 29 Jul 2013 10:23:14 -0400 Subject: [PATCH 3/3] Add test for calculating size of newly empty folder --- tests/lib/files/cache/cache.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index 527c1d1b2a1..247373a5cb9 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -127,6 +127,11 @@ class Cache extends \PHPUnit_Framework_TestCase { $this->assertEquals(1025, $this->cache->calculateFolderSize($file1)); + $this->cache->remove($file2); + $this->cache->remove($file3); + $this->cache->remove($file4); + $this->assertEquals(0, $this->cache->calculateFolderSize($file1)); + $this->cache->remove('folder'); $this->assertFalse($this->cache->inCache('folder/foo')); $this->assertFalse($this->cache->inCache('folder/bar'));