]> source.dussan.org Git - nextcloud-server.git/commitdiff
manual backport of changes related to #3459
authorThomas Müller <thomas.mueller@tmit.eu>
Mon, 29 Jul 2013 08:00:17 +0000 (10:00 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Mon, 29 Jul 2013 08:00:17 +0000 (10:00 +0200)
lib/files/cache/cache.php
lib/files/filesystem.php
lib/util.php
tests/lib/files/cache/cache.php
tests/lib/files/filesystem.php

index 9928a84798c3bec7a538db7785a3281ae0b27ef5..7b38f1290dd829e5c5b6a135cb594ecc6a2ff801 100644 (file)
@@ -110,6 +110,9 @@ class Cache {
         */
        public function get($file) {
                if (is_string($file) or $file == '') {
+                       // normalize file
+                       $file = $this->normalize($file);
+
                        $where = 'WHERE `storage` = ? AND `path_hash` = ?';
                        $params = array($this->numericId, md5($file));
                } else { //file id
@@ -188,6 +191,9 @@ class Cache {
                        $this->update($id, $data);
                        return $id;
                } else {
+                       // normalize file
+                       $file = $this->normalize($file);
+
                        if (isset($this->partial[$file])) { //add any saved partial data
                                $data = array_merge($this->partial[$file], $data);
                                unset($this->partial[$file]);
@@ -229,6 +235,16 @@ class Cache {
         * @param array $data
         */
        public function update($id, array $data) {
+               if(isset($data['path'])) {
+                       // normalize path
+                       $data['path'] = $this->normalize($data['path']);
+               }
+
+               if(isset($data['name'])) {
+                       // normalize path
+                       $data['name'] = $this->normalize($data['name']);
+               }
+
                list($queryParts, $params) = $this->buildParts($data);
                $params[] = $id;
 
@@ -271,6 +287,9 @@ class Cache {
         * @return int
         */
        public function getId($file) {
+               // normalize path
+               $file = $this->normalize($file);
+
                $pathHash = md5($file);
 
                $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
@@ -338,6 +357,10 @@ class Cache {
         * @param string $target
         */
        public function move($source, $target) {
+               // normalize source and target
+               $source = $this->normalize($source);
+               $target = $this->normalize($target);
+
                $sourceData = $this->get($source);
                $sourceId = $sourceData['fileid'];
                $newParentId = $this->getParentId($target);
@@ -378,6 +401,9 @@ class Cache {
         * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
         */
        public function getStatus($file) {
+               // normalize file
+               $file = $this->normalize($file);
+
                $pathHash = md5($file);
                $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
                $result = $query->execute(array($this->numericId, $pathHash));
@@ -406,6 +432,9 @@ class Cache {
         * @return array of file data
         */
        public function search($pattern) {
+               // normalize pattern
+               $pattern = $this->normalize($pattern);
+
                $query = \OC_DB::prepare('
                        SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `unencrypted_size`, `etag`
                        FROM `*PREFIX*filecache` WHERE `name` LIKE ? AND `storage` = ?'
@@ -556,4 +585,13 @@ class Cache {
                        return null;
                }
        }
-}
+
+       /**
+        * normalize the given path
+        * @param $path
+        * @return string
+        */
+       public function normalize($path) {
+
+               return \OC_Util::normalizeUnicode($path);
+       }}
index 5570f91fe09301a0729dc23014e7739d8a4d580e..4b445588a65b285f46866459446aaf26c6a87c94 100644 (file)
@@ -598,9 +598,7 @@ class Filesystem {
                        $path = substr($path, 0, -1);
                }
                //normalize unicode if possible
-               if (class_exists('Normalizer')) {
-                       $path = \Normalizer::normalize($path);
-               }
+               $path = \OC_Util::normalizeUnicode($path);
                return $path;
        }
 
index 86f3a7cac0132c3726b315c8eebbd4a24ddd8ac3..cb214bb3dae700e5f408d14dbbffff586455d1f4 100755 (executable)
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Class for utility functions
  *
@@ -854,4 +855,23 @@ class OC_Util {
                }
        }
 
+       /**
+        * Normalize a unicode string
+        * @param string $value a not normalized string
+        * @return bool|string
+        */
+       public static function normalizeUnicode($value) {
+               require_once 'Patchwork/PHP/Shim/Normalizer.php';
+
+               if(class_exists('Patchwork\PHP\Shim\Normalizer')) {
+                       $normalizedValue = \Patchwork\PHP\Shim\Normalizer::normalize($value);
+                       if($normalizedValue === false) {
+                               \OC_Log::write( 'core', 'normalizing failed for "' . $value . '"', \OC_Log::WARN);
+                       } else {
+                               $value = $normalizedValue;
+                       }
+               }
+
+               return $value;
+       }
 }
index 4051a6e234b4177e5d041a73e0f783dbbb4d7603..b7c276536c2cd4fac332a81f72158b8fef0851d4 100644 (file)
@@ -220,6 +220,91 @@ class Cache extends \PHPUnit_Framework_TestCase {
                $this->assertEquals(array(md5($storageId), 'foo'), \OC\Files\Cache\Cache::getById($id));
        }
 
+       /**
+        * @brief this test show the bug resulting if we have no normalizer installed
+        */
+       public function testWithoutNormalizer()
+       {
+               // folder name "Schön" with U+00F6 (normalized)
+               $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
+
+               // folder name "Schön" with U+0308 (un-normalized)
+               $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
+
+               /**
+                * @var \OC\Files\Cache\Cache | PHPUnit_Framework_MockObject_MockObject $cacheMock
+                */
+               $cacheMock = $this->getMock('\OC\Files\Cache\Cache', array('normalize'), array($this->storage), '', true);
+
+               $cacheMock->expects($this->any())
+                       ->method('normalize')
+                       ->will($this->returnArgument(0));
+
+               $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
+
+               // put root folder
+               $this->assertFalse($cacheMock->get('folder'));
+               $this->assertGreaterThan(0, $cacheMock->put('folder', $data));
+
+               // put un-normalized folder
+               $this->assertFalse($cacheMock->get('folder/' . $folderWith0308));
+               $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith0308, $data));
+
+               // get un-normalized folder by name
+               $unNormalizedFolderName = $cacheMock->get('folder/' . $folderWith0308);
+
+               // check if database layer normalized the folder name (this should not happen)
+               $this->assertEquals($folderWith0308, $unNormalizedFolderName['name']);
+
+               // put normalized folder
+               $this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
+               $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith00F6, $data));
+
+               // this is our bug, we have two different hashes with the same name (Schön)
+               $this->assertEquals(2, count($cacheMock->getFolderContents('folder')));
+       }
+
+       /**
+        * @brief this test shows that there is no bug if we use the normalizer
+        */
+       public function testWithNormalizer()
+       {
+
+               if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
+                       $this->markTestSkipped('The 3rdparty Normalizer extension is not available.');
+                       return;
+               }
+
+               // folder name "Schön" with U+00F6 (normalized)
+               $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
+
+               // folder name "Schön" with U+0308 (un-normalized)
+               $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
+
+               $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
+
+               // put root folder
+               $this->assertFalse($this->cache->get('folder'));
+               $this->assertGreaterThan(0, $this->cache->put('folder', $data));
+
+               // put un-normalized folder
+               $this->assertFalse($this->cache->get('folder/' . $folderWith0308));
+               $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith0308, $data));
+
+               // get un-normalized folder by name
+               $unNormalizedFolderName = $this->cache->get('folder/' . $folderWith0308);
+
+               // check if folder name was normalized
+               $this->assertEquals($folderWith00F6, $unNormalizedFolderName['name']);
+
+               // put normalized folder
+               $this->assertTrue(is_array($this->cache->get('folder/' . $folderWith00F6)));
+               $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith00F6, $data));
+
+               // at this point we should have only one folder named "Schön"
+               $this->assertEquals(1, count($this->cache->getFolderContents('folder')));
+       }
+
        public function tearDown() {
                $this->cache->clear();
        }
index 6ce45e6178a7c15c9c43d9e1163a4484a702ef4a..bef70cc725b167c4ced0495c31638d8571f1a88c 100644 (file)
@@ -72,7 +72,7 @@ class Filesystem extends \PHPUnit_Framework_TestCase {
                $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\path'));
                $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo//bar/'));
                $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo////bar'));
-               if (class_exists('Normalizer')) {
+               if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
                        $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
                }
        }