revert previous fix and added normalizer to cache class

This commit is contained in:
Florin Peter 2013-05-23 20:29:46 +02:00
parent 85e0c78166
commit d4b700ef4e
3 changed files with 92 additions and 1 deletions

View File

@ -100,6 +100,9 @@ class Cache {
*/ */
public function get($file) { public function get($file) {
if (is_string($file) or $file == '') { if (is_string($file) or $file == '') {
// normalize file
$file = $this->normalize($file);
$where = 'WHERE `storage` = ? AND `path_hash` = ?'; $where = 'WHERE `storage` = ? AND `path_hash` = ?';
$params = array($this->getNumericStorageId(), md5($file)); $params = array($this->getNumericStorageId(), md5($file));
} else { //file id } else { //file id
@ -177,6 +180,9 @@ class Cache {
$this->update($id, $data); $this->update($id, $data);
return $id; return $id;
} else { } else {
// normalize file
$file = $this->normalize($file);
if (isset($this->partial[$file])) { //add any saved partial data if (isset($this->partial[$file])) { //add any saved partial data
$data = array_merge($this->partial[$file], $data); $data = array_merge($this->partial[$file], $data);
unset($this->partial[$file]); unset($this->partial[$file]);
@ -265,6 +271,9 @@ class Cache {
* @return int * @return int
*/ */
public function getId($file) { public function getId($file) {
// normalize file
$file = $this->normalize($file);
$pathHash = md5($file); $pathHash = md5($file);
$query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'); $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
@ -549,4 +558,19 @@ class Cache {
return null; return null;
} }
} }
/**
* normalize the given path
* @param $path
* @return string
*/
public function normalize($path) {
//normalize unicode if possible
if (class_exists('Normalizer')) {
$path = \Normalizer::normalize($path);
}
return $path;
}
} }

View File

@ -114,7 +114,7 @@ class Scanner {
$size = 0; $size = 0;
if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) { if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) {
\OC_DB::beginTransaction(); \OC_DB::beginTransaction();
while ($file = utf8_encode(readdir($dh))) { while ($file = readdir($dh)) {
$child = ($path) ? $path . '/' . $file : $file; $child = ($path) ? $path . '/' . $file : $file;
if (!$this->isIgnoredDir($file)) { if (!$this->isIgnoredDir($file)) {
$data = $this->scanFile($child, $recursive === self::SCAN_SHALLOW); $data = $this->scanFile($child, $recursive === self::SCAN_SHALLOW);

View File

@ -8,6 +8,8 @@
namespace Test\Files\Cache; namespace Test\Files\Cache;
use PHPUnit_Framework_MockObject_MockObject;
class LongId extends \OC\Files\Storage\Temporary { class LongId extends \OC\Files\Storage\Temporary {
public function getId() { public function getId() {
return 'long:' . str_repeat('foo', 50) . parent::getId(); return 'long:' . str_repeat('foo', 50) . parent::getId();
@ -237,6 +239,71 @@ class Cache extends \PHPUnit_Framework_TestCase {
$this->assertEquals(array(md5($storageId), 'foo'), \OC\Files\Cache\Cache::getById($id)); $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() {
// create folder Schön with U+00F6
$folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
// create folder Schön with U+0308
$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');
$this->assertFalse($cacheMock->get('folder'));
$this->assertGreaterThan(0, $cacheMock->put('folder', $data));
$this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
$this->assertGreaterThan(0, $cacheMock->put('folder/' .$folderWith00F6, $data));
$this->assertFalse($cacheMock->get('folder/' .$folderWith0308));
$this->assertGreaterThan(0, $cacheMock->put('folder/' .$folderWith0308, $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('Normalizer')) {
$this->markTestSkipped('The Normalizer extension is not available.');
return;
}
// folder name Schön with U+00F6
$folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
// folder name Schön with U+0308
$folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
$data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
$this->assertFalse($this->cache->get('folder'));
$this->assertGreaterThan(0, $this->cache->put('folder', $data));
$this->assertFalse($this->cache->get('folder/' . $folderWith00F6));
$this->assertGreaterThan(0, $this->cache->put('folder/' .$folderWith00F6, $data));
$this->assertTrue(is_array($this->cache->get('folder/' .$folderWith0308)));
$this->assertGreaterThan(0, $this->cache->put('folder/' .$folderWith0308, $data));
// at this point we should have only one folder named "Schön"
$this->assertEquals(1, count($this->cache->getFolderContents('folder')));
}
public function tearDown() { public function tearDown() {
$this->cache->clear(); $this->cache->clear();
} }