Merge pull request #3792 from owncloud/scanfolder-remove

remove deleted files when re-scanning a folder
This commit is contained in:
Thomas Müller 2013-06-20 04:02:10 -07:00
commit 4232ccbc6d
2 changed files with 35 additions and 0 deletions

View File

@ -137,11 +137,20 @@ class Scanner {
\OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_folder', array('path' => $path, 'storage' => $this->storageId)); \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_folder', array('path' => $path, 'storage' => $this->storageId));
$size = 0; $size = 0;
$childQueue = array(); $childQueue = array();
$existingChildren = array();
if ($this->cache->inCache($path)) {
$children = $this->cache->getFolderContents($path);
foreach ($children as $child) {
$existingChildren[] = $child['name'];
}
}
$newChildren = array();
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 = readdir($dh)) { while ($file = readdir($dh)) {
$child = ($path) ? $path . '/' . $file : $file; $child = ($path) ? $path . '/' . $file : $file;
if (!Filesystem::isIgnoredDir($file)) { if (!Filesystem::isIgnoredDir($file)) {
$newChildren[] = $file;
$data = $this->scanFile($child, $reuse); $data = $this->scanFile($child, $reuse);
if ($data) { if ($data) {
if ($data['size'] === -1) { if ($data['size'] === -1) {
@ -156,6 +165,11 @@ class Scanner {
} }
} }
} }
$removedChildren = \array_diff($existingChildren, $newChildren);
foreach ($removedChildren as $childName) {
$child = ($path) ? $path . '/' . $childName : $childName;
$this->cache->remove($child);
}
\OC_DB::commit(); \OC_DB::commit();
foreach ($childQueue as $child) { foreach ($childQueue as $child) {
$childSize = $this->scanChildren($child, self::SCAN_RECURSIVE); $childSize = $this->scanChildren($child, self::SCAN_RECURSIVE);

View File

@ -144,6 +144,27 @@ class Scanner extends \PHPUnit_Framework_TestCase {
$this->assertEquals(-1, $newData['size']); $this->assertEquals(-1, $newData['size']);
} }
public function testRemovedFile() {
$this->fillTestFolders();
$this->scanner->scan('');
$this->assertTrue($this->cache->inCache('foo.txt'));
$this->storage->unlink('foo.txt');
$this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
$this->assertFalse($this->cache->inCache('foo.txt'));
}
public function testRemovedFolder() {
$this->fillTestFolders();
$this->scanner->scan('');
$this->assertTrue($this->cache->inCache('folder/bar.txt'));
$this->storage->unlink('/folder');
$this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
$this->assertFalse($this->cache->inCache('folder'));
$this->assertFalse($this->cache->inCache('folder/bar.txt'));
}
function setUp() { function setUp() {
$this->storage = new \OC\Files\Storage\Temporary(array()); $this->storage = new \OC\Files\Storage\Temporary(array());
$this->scanner = new \OC\Files\Cache\Scanner($this->storage); $this->scanner = new \OC\Files\Cache\Scanner($this->storage);