]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache: hash long storage ids to ensure they fit in the database
authorRobin Appelman <icewind@owncloud.com>
Fri, 15 Feb 2013 20:49:40 +0000 (21:49 +0100)
committerRobin Appelman <icewind@owncloud.com>
Fri, 15 Feb 2013 21:11:39 +0000 (22:11 +0100)
lib/files/cache/cache.php
lib/files/mount.php
tests/lib/files/cache/cache.php
tests/lib/files/mount.php

index 5feed37ae486ffb0c31f2e869b647f46854e55c4..3652dc7cf23a3ca2a17e468a7bec602e3bd96fd1 100644 (file)
@@ -48,6 +48,9 @@ class Cache {
                } else {
                        $this->storageId = $storage;
                }
+               if (strlen($this->storageId) > 64) {
+                       $this->storageId = md5($this->storageId);
+               }
 
                $query = \OC_DB::prepare('SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?');
                $result = $query->execute(array($this->storageId));
@@ -199,7 +202,7 @@ class Cache {
                        $valuesPlaceholder = array_fill(0, count($queryParts), '?');
 
                        $query = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`(' . implode(', ', $queryParts) . ')'
-                               .' VALUES(' . implode(', ', $valuesPlaceholder) . ')');
+                               . ' VALUES(' . implode(', ', $valuesPlaceholder) . ')');
                        $query->execute($params);
 
                        return (int)\OC_DB::insertid('*PREFIX*filecache');
@@ -217,7 +220,7 @@ class Cache {
                $params[] = $id;
 
                $query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=?'
-                       .' WHERE fileid = ?');
+                       . ' WHERE fileid = ?');
                $query->execute($params);
        }
 
@@ -335,7 +338,7 @@ class Cache {
                }
 
                $query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ?, `parent` =?'
-                       .' WHERE `fileid` = ?');
+                       . ' WHERE `fileid` = ?');
                $query->execute(array($target, md5($target), $newParentId, $sourceId));
        }
 
@@ -496,7 +499,7 @@ class Cache {
         */
        public function getIncomplete() {
                $query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache`'
-                       .' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1');
+                       . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1');
                $query->execute(array($this->numericId));
                if ($row = $query->fetchRow()) {
                        return $row['path'];
index 74ee483b1bef928306907e914ce81a2db12d3a6d..6e99d8eabb49f000305b0a77386963c991bcb9c4 100644 (file)
@@ -93,6 +93,9 @@ class Mount {
                                $this->storage = $this->createStorage();
                        }
                        $this->storageId = $this->storage->getId();
+                       if (strlen($this->storageId) > 64) {
+                               $this->storageId = md5($this->storageId);
+                       }
                }
                return $this->storageId;
        }
@@ -177,6 +180,9 @@ class Mount {
         * @return \OC\Files\Storage\Storage[]
         */
        public static function findById($id) {
+               if (strlen($id) > 64) {
+                       $id = md5($id);
+               }
                $result = array();
                foreach (self::$mounts as $mount) {
                        if ($mount->getStorageId() === $id) {
index c466fbb63e79bda189e37d05252ce1eaf53e5896..250842805e5d32072fc253a5c6fc892baf008594 100644 (file)
@@ -8,6 +8,12 @@
 
 namespace Test\Files\Cache;
 
+class LongId extends \OC\Files\Storage\Temporary {
+       public function getId() {
+               return 'long:' . str_repeat('foo', 50) . parent::getId();
+       }
+}
+
 class Cache extends \PHPUnit_Framework_TestCase {
        /**
         * @var \OC\Files\Storage\Temporary $storage;
@@ -204,6 +210,15 @@ class Cache extends \PHPUnit_Framework_TestCase {
                $this->assertEquals(array($storageId, 'foo'), \OC\Files\Cache\Cache::getById($id));
        }
 
+       function testLongId() {
+               $storage = new LongId(array());
+               $cache = $storage->getCache();
+               $storageId = $storage->getId();
+               $data = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
+               $id = $cache->put('foo', $data);
+               $this->assertEquals(array(md5($storageId), 'foo'), \OC\Files\Cache\Cache::getById($id));
+       }
+
        public function tearDown() {
                $this->cache->clear();
        }
index f223f0f6c534de73fa4219bbff21db85390112f1..4e6aaf0679b5b130f6344bc48aca219b533c7c1a 100644 (file)
@@ -10,6 +10,12 @@ namespace Test\Files;
 
 use \OC\Files\Storage\Temporary;
 
+class LongId extends Temporary {
+       public function getId() {
+               return 'long:' . str_repeat('foo', 50) . parent::getId();
+       }
+}
+
 class Mount extends \PHPUnit_Framework_TestCase {
        public function setup() {
                \OC_Util::setupFS();
@@ -38,4 +44,15 @@ class Mount extends \PHPUnit_Framework_TestCase {
                $mount2 = new \OC\Files\Mount($storage, '/foo/bar');
                $this->assertEquals(array($mount, $mount2), \OC\Files\Mount::findById($id));
        }
+
+       public function testLong() {
+               $storage = new LongId(array());
+               $mount = new \OC\Files\Mount($storage, '/foo');
+
+               $id = $mount->getStorageId();
+               $storageId = $storage->getId();
+               $this->assertEquals(array($mount), \OC\Files\Mount::findById($id));
+               $this->assertEquals(array($mount), \OC\Files\Mount::findById($storageId));
+               $this->assertEquals(array($mount), \OC\Files\Mount::findById(md5($storageId)));
+       }
 }