From 15dae6198fc4855519a0ed2347c29485a061f6aa Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 20 Apr 2013 16:38:03 +0200 Subject: Cache: add a backgroundjob to check for external changes to the filesystem --- apps/files/appinfo/app.php | 4 +- lib/files/cache/backgroundwatcher.php | 73 +++++++++++++++++++++++++++++++++++ lib/files/cache/cache.php | 1 + lib/files/cache/permissions.php | 15 +++++++ tests/lib/files/cache/permissions.php | 6 ++- 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 lib/files/cache/backgroundwatcher.php diff --git a/apps/files/appinfo/app.php b/apps/files/appinfo/app.php index 703b1c7cb6c..05ab1722b3e 100644 --- a/apps/files/appinfo/app.php +++ b/apps/files/appinfo/app.php @@ -18,4 +18,6 @@ OC_Search::registerProvider('OC_Search_Provider_File'); \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook'); \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook'); \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook'); -\OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook'); \ No newline at end of file +\OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook'); + +\OC_BackgroundJob_RegularTask::register('\OC\Files\Cache\BackgroundWatcher', 'checkNext'); diff --git a/lib/files/cache/backgroundwatcher.php b/lib/files/cache/backgroundwatcher.php new file mode 100644 index 00000000000..3bcd447f53a --- /dev/null +++ b/lib/files/cache/backgroundwatcher.php @@ -0,0 +1,73 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Cache; + +use \OC\Files\Mount; +use \OC\Files\Filesystem; + +class BackgroundWatcher { + static private function checkUpdate($id) { + $cacheItem = Cache::getById($id); + if (is_null($cacheItem)) { + return; + } + list($storageId, $internalPath) = $cacheItem; + $mounts = Mount::findByStorageId($storageId); + + if (count($mounts) === 0) { + //if the storage we need isn't mounted on default, try to find a user that has access to the storage + $permissionsCache = new Permissions($storageId); + $users = $permissionsCache->getUsers($id); + if (count($users) === 0) { + return; + } + Filesystem::initMountPoints($users[0]); + $mounts = Mount::findByStorageId($storageId); + if (count($mounts) === 0) { + return; + } + } + $storage = $mounts[0]->getStorage(); + $watcher = new Watcher($storage); + $watcher->checkUpdate($internalPath); + } + + /** + * get the next fileid in the cache + * + * @param int $previous + * @return int + */ + static private function getNextFileId($previous) { + $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? ORDER BY `fileid` ASC', 1); + $result = $query->execute(array($previous)); + if ($row = $result->fetchRow()) { + return $row['fileid']; + } else { + return 0; + } + } + + static public function checkNext() { + $previous = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous', 0); + $next = self::getNextFileId($previous); + error_log($next); + \OC_Appconfig::setValue('files', 'backgroundwatcher_previous', $next); + self::checkUpdate($next); + } + + static public function checkAll() { + $previous = 0; + $next = 1; + while ($next != 0) { + $next = self::getNextFileId($previous); + self::checkUpdate($next); + } + } +} diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php index 71b70abe3fe..3ed3490f29b 100644 --- a/lib/files/cache/cache.php +++ b/lib/files/cache/cache.php @@ -517,6 +517,7 @@ class Cache { /** * get the storage id of the storage for a file and the internal path of the file * + * @param int $id * @return array, first element holding the storage id, second the path */ static public function getById($id) { diff --git a/lib/files/cache/permissions.php b/lib/files/cache/permissions.php index a5c9c144054..faa5ff5eacc 100644 --- a/lib/files/cache/permissions.php +++ b/lib/files/cache/permissions.php @@ -107,4 +107,19 @@ class Permissions { $query->execute(array($fileId, $user)); } } + + /** + * get the list of users which have permissions stored for a file + * + * @param int $fileId + */ + public function getUsers($fileId) { + $query = \OC_DB::prepare('SELECT `user` FROM `*PREFIX*permissions` WHERE `fileid` = ?'); + $result = $query->execute(array($fileId)); + $users = array(); + while ($row = $result->fetchRow()) { + $users[] = $row['user']; + } + return $users; + } } diff --git a/tests/lib/files/cache/permissions.php b/tests/lib/files/cache/permissions.php index 56dbbc4518e..7e6e11e2eb2 100644 --- a/tests/lib/files/cache/permissions.php +++ b/tests/lib/files/cache/permissions.php @@ -14,8 +14,8 @@ class Permissions extends \PHPUnit_Framework_TestCase { */ private $permissionsCache; - function setUp(){ - $this->permissionsCache=new \OC\Files\Cache\Permissions('dummy'); + function setUp() { + $this->permissionsCache = new \OC\Files\Cache\Permissions('dummy'); } function testSimple() { @@ -23,8 +23,10 @@ class Permissions extends \PHPUnit_Framework_TestCase { $user = uniqid(); $this->assertEquals(-1, $this->permissionsCache->get(1, $user)); + $this->assertNotContains($user, $this->permissionsCache->getUsers(1)); $this->permissionsCache->set(1, $user, 1); $this->assertEquals(1, $this->permissionsCache->get(1, $user)); + $this->assertContains($user, $this->permissionsCache->getUsers(1)); $this->assertEquals(-1, $this->permissionsCache->get(2, $user)); $this->assertEquals(-1, $this->permissionsCache->get(1, $user . '2')); -- cgit v1.2.3 From eed5e9f804c0aac0467e1f502d86266ea232f350 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 20 Apr 2013 16:56:47 +0200 Subject: Cache: check one folder and one file each time the backgroundwatcher runs Because there are usually way less folders than files it walks trought the list of all folder quicker, this causes new files to be detected quicker --- lib/files/cache/backgroundwatcher.php | 47 +++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/files/cache/backgroundwatcher.php b/lib/files/cache/backgroundwatcher.php index 3bcd447f53a..7549745e7d7 100644 --- a/lib/files/cache/backgroundwatcher.php +++ b/lib/files/cache/backgroundwatcher.php @@ -12,6 +12,18 @@ use \OC\Files\Mount; use \OC\Files\Filesystem; class BackgroundWatcher { + static $folderMimetype = null; + + static private function getFolderMimetype() { + if (!is_null(self::$folderMimetype)) { + return self::$folderMimetype; + } + $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?'); + $result = $query->execute(array('httpd/unix-directory')); + $row = $result->fetchRow(); + return $row['id']; + } + static private function checkUpdate($id) { $cacheItem = Cache::getById($id); if (is_null($cacheItem)) { @@ -42,10 +54,15 @@ class BackgroundWatcher { * get the next fileid in the cache * * @param int $previous + * @param bool $folder * @return int */ - static private function getNextFileId($previous) { - $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? ORDER BY `fileid` ASC', 1); + static private function getNextFileId($previous, $folder) { + if ($folder) { + $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? AND mimetype = ' . self::getFolderMimetype() . ' ORDER BY `fileid` ASC', 1); + } else { + $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? AND mimetype != ' . self::getFolderMimetype() . ' ORDER BY `fileid` ASC', 1); + } $result = $query->execute(array($previous)); if ($row = $result->fetchRow()) { return $row['fileid']; @@ -55,18 +72,32 @@ class BackgroundWatcher { } static public function checkNext() { - $previous = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous', 0); - $next = self::getNextFileId($previous); - error_log($next); - \OC_Appconfig::setValue('files', 'backgroundwatcher_previous', $next); - self::checkUpdate($next); + // check both 1 file and 1 folder, this way new files are detected quicker because there are less folders than files usually + $previousFile = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous_file', 0); + $previousFolder = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous_folder', 0); + $nextFile = self::getNextFileId($previousFile, false); + $nextFolder = self::getNextFileId($previousFolder, true); + \OC_Appconfig::setValue('files', 'backgroundwatcher_previous_file', $nextFile); + \OC_Appconfig::setValue('files', 'backgroundwatcher_previous_folder', $nextFolder); + if ($nextFile > 0) { + self::checkUpdate($nextFile); + } + if ($nextFolder > 0) { + self::checkUpdate($nextFolder); + } } static public function checkAll() { $previous = 0; $next = 1; while ($next != 0) { - $next = self::getNextFileId($previous); + $next = self::getNextFileId($previous, true); + self::checkUpdate($next); + } + $previous = 0; + $next = 1; + while ($next != 0) { + $next = self::getNextFileId($previous, false); self::checkUpdate($next); } } -- cgit v1.2.3