From 186c9e77e89dcd057a311ed08df9e1bb9e13ea8f Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Wed, 21 Nov 2012 23:02:43 +0100
Subject: [PATCH] add Cache->getIncomplete for use in background scanning

---
 lib/files/cache/cache.php       | 19 +++++++++++++++++++
 tests/lib/files/cache/cache.php | 17 +++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php
index 7c6bba4fad..b52c0f4067 100644
--- a/lib/files/cache/cache.php
+++ b/lib/files/cache/cache.php
@@ -402,4 +402,23 @@ class Cache {
 		}
 		return $ids;
 	}
+
+	/**
+	 * find a folder in the cache which has not been fully scanned
+	 *
+	 * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
+	 * use the one with the highest id gives the best result with the background scanner, since that is most
+	 * likely the folder where we stopped scanning previously
+	 *
+	 * @return string|bool the path of the folder or false when no folder matched
+	 */
+	public function getIncomplete(){
+		$query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1');
+		$query->execute(array($this->storageId));
+		if($row = $query->fetchRow()){
+			return $row['path'];
+		}else{
+			return false;
+		}
+	}
 }
diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php
index 9c469aa937..e9105cd5ab 100644
--- a/tests/lib/files/cache/cache.php
+++ b/tests/lib/files/cache/cache.php
@@ -175,6 +175,23 @@ class Cache extends \UnitTestCase {
 		$this->assertTrue($this->cache->inCache('folder/foobar/2'));
 	}
 
+	function testGetIncomplete() {
+		$file1 = 'folder1';
+		$file2 = 'folder2';
+		$file3 = 'folder3';
+		$file4 = 'folder4';
+		$data = array('size' => 10, 'mtime' => 50, 'mimetype' => 'foo/bar');
+
+		$this->cache->put($file1, $data);
+		$data['size'] = -1;
+		$this->cache->put($file2, $data);
+		$this->cache->put($file3, $data);
+		$data['size'] = 12;
+		$this->cache->put($file4, $data);
+
+		$this->assertEquals($file3, $this->cache->getIncomplete());
+	}
+
 	public function tearDown() {
 		$this->cache->clear();
 	}
-- 
GitLab