From 21cfd1014a99e25f4ee255b872957fc1b98a7610 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 10 Jun 2014 15:26:18 +0200 Subject: [PATCH 1/3] Repair broken parent link in the scanner --- lib/private/files/cache/scanner.php | 70 +++++++++++++++-------------- tests/lib/files/cache/scanner.php | 51 ++++++++++++++++++++- 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index 965013c5c98..1f67ce0062c 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -100,45 +100,47 @@ class Scanner extends BasicEmitter { \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); $data = $this->getData($file); if ($data) { - if ($file and !$parentExistsInCache) { - $parent = dirname($file); - if ($parent === '.' or $parent === '/') { - $parent = ''; - } - if (!$this->cache->inCache($parent)) { - $this->scanFile($parent); - } + $parent = dirname($file); + if ($parent === '.' or $parent === '/') { + $parent = ''; + } + $parentId = $this->cache->getId($parent); + if ($file and $parentId === -1) { + $parentData = $this->scanFile($parent); + $parentId = $parentData['fileid']; + } + if ($parent) { + $data['parent'] = $parentId; } - $newData = $data; $cacheData = $this->cache->get($file); - if ($cacheData) { - if ($reuseExisting) { - // prevent empty etag - if (empty($cacheData['etag'])) { - $etag = $data['etag']; - } else { - $etag = $cacheData['etag']; - } - // only reuse data if the file hasn't explicitly changed - if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { - $data['mtime'] = $cacheData['mtime']; - if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { - $data['size'] = $cacheData['size']; - } - if ($reuseExisting & self::REUSE_ETAG) { - $data['etag'] = $etag; - } + if ($cacheData and $reuseExisting) { + // prevent empty etag + if (empty($cacheData['etag'])) { + $etag = $data['etag']; + } else { + $etag = $cacheData['etag']; + } + // only reuse data if the file hasn't explicitly changed + if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { + $data['mtime'] = $cacheData['mtime']; + if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { + $data['size'] = $cacheData['size']; } - // Only update metadata that has changed - $newData = array_diff_assoc($data, $cacheData); - if (isset($newData['etag'])) { - $cacheDataString = print_r($cacheData, true); - $dataString = print_r($data, true); - \OCP\Util::writeLog('OC\Files\Cache\Scanner', - "!!! No reuse of etag for '$file' !!! \ncache: $cacheDataString \ndata: $dataString", - \OCP\Util::DEBUG); + if ($reuseExisting & self::REUSE_ETAG) { + $data['etag'] = $etag; } } + // Only update metadata that has changed + $newData = array_diff_assoc($data, $cacheData); + if (isset($newData['etag'])) { + $cacheDataString = print_r($cacheData, true); + $dataString = print_r($data, true); + \OCP\Util::writeLog('OC\Files\Cache\Scanner', + "!!! No reuse of etag for '$file' !!! \ncache: $cacheDataString \ndata: $dataString", + \OCP\Util::DEBUG); + } + } else { + $newData = $data; } if (!empty($newData)) { $data['fileid'] = $this->addToCache($file, $newData); diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index 263e5b3445f..0a274631d1c 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -32,7 +32,6 @@ class Scanner extends \PHPUnit_Framework_TestCase { function tearDown() { if ($this->cache) { - $ids = $this->cache->getAll(); $this->cache->clear(); } } @@ -199,7 +198,7 @@ class Scanner extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->cache->inCache('folder/bar.txt')); } - public function testScanRemovedFile(){ + public function testScanRemovedFile() { $this->fillTestFolders(); $this->scanner->scan(''); @@ -233,4 +232,52 @@ class Scanner extends \PHPUnit_Framework_TestCase { $this->assertInternalType('string', $newData0['etag']); $this->assertNotEmpty($newData0['etag']); } + + public function testRepairParent() { + $this->fillTestFolders(); + $this->scanner->scan(''); + $this->assertTrue($this->cache->inCache('folder/bar.txt')); + $oldFolderId = $this->cache->getId('folder'); + + // delete the folder without removing the childs + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; + \OC_DB::executeAudited($sql, array($oldFolderId)); + + $cachedData = $this->cache->get('folder/bar.txt'); + $this->assertEquals($oldFolderId, $cachedData['parent']); + $this->assertFalse($this->cache->inCache('folder')); + + $this->scanner->scan(''); + + $this->assertTrue($this->cache->inCache('folder')); + $newFolderId = $this->cache->getId('folder'); + $this->assertNotEquals($oldFolderId, $newFolderId); + + $cachedData = $this->cache->get('folder/bar.txt'); + $this->assertEquals($newFolderId, $cachedData['parent']); + } + + public function testRepairParentShallow() { + $this->fillTestFolders(); + $this->scanner->scan(''); + $this->assertTrue($this->cache->inCache('folder/bar.txt')); + $oldFolderId = $this->cache->getId('folder'); + + // delete the folder without removing the childs + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; + \OC_DB::executeAudited($sql, array($oldFolderId)); + + $cachedData = $this->cache->get('folder/bar.txt'); + $this->assertEquals($oldFolderId, $cachedData['parent']); + $this->assertFalse($this->cache->inCache('folder')); + + $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW); + + $this->assertTrue($this->cache->inCache('folder')); + $newFolderId = $this->cache->getId('folder'); + $this->assertNotEquals($oldFolderId, $newFolderId); + + $cachedData = $this->cache->get('folder/bar.txt'); + $this->assertEquals($newFolderId, $cachedData['parent']); + } } From 054083b9cd13244842f8664601d8566593f9737d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 10 Jun 2014 15:37:43 +0200 Subject: [PATCH 2/3] add some comments --- lib/private/files/cache/scanner.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index 1f67ce0062c..3f681e57310 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -105,6 +105,8 @@ class Scanner extends BasicEmitter { $parent = ''; } $parentId = $this->cache->getId($parent); + + // scan the parent if it's not in the cache (id -1) and the current file is not the root folder if ($file and $parentId === -1) { $parentData = $this->scanFile($parent); $parentId = $parentData['fileid']; From 6b1d8a56bbff688107f86a4adb67495de7c01e9d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 10 Jun 2014 15:42:37 +0200 Subject: [PATCH 3/3] remove unused argument --- lib/private/files/cache/scanner.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/private/files/cache/scanner.php b/lib/private/files/cache/scanner.php index 3f681e57310..89fa6b8cc13 100644 --- a/lib/private/files/cache/scanner.php +++ b/lib/private/files/cache/scanner.php @@ -89,10 +89,9 @@ class Scanner extends BasicEmitter { * * @param string $file * @param int $reuseExisting - * @param bool $parentExistsInCache * @return array an array of metadata of the scanned file */ - public function scanFile($file, $reuseExisting = 0, $parentExistsInCache = false) { + public function scanFile($file, $reuseExisting = 0) { if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file) ) { @@ -242,7 +241,7 @@ class Scanner extends BasicEmitter { if (!Filesystem::isIgnoredDir($file)) { $newChildren[] = $file; try { - $data = $this->scanFile($child, $reuse, true); + $data = $this->scanFile($child, $reuse); if ($data) { if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { $childQueue[] = $child;