From f01e6d70bfd865be30c38f4900d5e54a4f17a252 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 15 Sep 2014 17:26:33 +0200 Subject: [PATCH] Cleanup filesystem setup code --- apps/files/ajax/scan.php | 2 +- apps/files/command/scan.php | 2 +- apps/files/tests/ajax_rename.php | 2 +- apps/files_encryption/hooks/hooks.php | 15 +- apps/files_encryption/lib/helper.php | 4 + apps/files_encryption/lib/util.php | 4 +- apps/files_sharing/ajax/publicpreview.php | 1 - apps/files_sharing/lib/cache.php | 2 +- apps/files_sharing/lib/helper.php | 4 +- apps/files_sharing/lib/share/file.php | 2 +- apps/files_sharing/lib/sharedstorage.php | 2 +- apps/files_sharing/lib/updater.php | 2 +- apps/files_trashbin/lib/trashbin.php | 2 +- apps/files_versions/lib/versions.php | 2 +- lib/private/cache/file.php | 2 +- lib/private/fileproxy/fileoperations.php | 37 --- lib/private/files/factory.php | 192 ++++++++++++++ lib/private/files/fileinfomanager.php | 37 +++ lib/private/files/filesystem.php | 300 +++++++--------------- lib/private/files/mount/manager.php | 29 ++- lib/private/files/node/node.php | 9 + lib/private/files/node/root.php | 38 ++- lib/private/files/objectstorefactory.php | 55 ++++ lib/private/files/utils/scanner.php | 18 +- lib/private/files/view.php | 3 - lib/private/image.php | 2 +- lib/private/server.php | 83 +++--- lib/private/util.php | 176 ++----------- lib/public/files/node.php | 7 + lib/public/files/storage.php | 9 +- lib/public/iservercontainer.php | 7 + tests/lib/cache/file.php | 14 +- tests/lib/cache/usercache.php | 14 +- tests/lib/files/cache/updaterlegacy.php | 12 +- tests/lib/files/etagtest.php | 52 ++-- tests/lib/files/filesystem.php | 26 +- tests/lib/files/node/file.php | 15 +- tests/lib/files/node/folder.php | 9 +- tests/lib/files/node/integration.php | 5 +- tests/lib/files/node/node.php | 4 +- tests/lib/files/node/root.php | 9 +- tests/lib/files/utils/scanner.php | 9 +- tests/lib/helperstorage.php | 2 +- tests/lib/preview.php | 4 +- tests/lib/testcase.php | 9 + 45 files changed, 635 insertions(+), 599 deletions(-) delete mode 100644 lib/private/fileproxy/fileoperations.php create mode 100644 lib/private/files/factory.php create mode 100644 lib/private/files/fileinfomanager.php create mode 100644 lib/private/files/objectstorefactory.php diff --git a/apps/files/ajax/scan.php b/apps/files/ajax/scan.php index f8977c2971e6..52ba421ae8d1 100644 --- a/apps/files/ajax/scan.php +++ b/apps/files/ajax/scan.php @@ -20,7 +20,7 @@ foreach ($users as $user) { $eventSource->send('user', $user); - $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection()); + $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getUserFolder($user)); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', array($listener, 'file')); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', array($listener, 'folder')); if ($force) { diff --git a/apps/files/command/scan.php b/apps/files/command/scan.php index 2dca0e0e67c7..7031d741f716 100644 --- a/apps/files/command/scan.php +++ b/apps/files/command/scan.php @@ -46,7 +46,7 @@ protected function configure() { } protected function scanFiles($user, OutputInterface $output) { - $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection()); + $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getUserFolder($user)); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $output->writeln("Scanning $path"); }); diff --git a/apps/files/tests/ajax_rename.php b/apps/files/tests/ajax_rename.php index 48aed05823b4..e8d46a0bfaf3 100644 --- a/apps/files/tests/ajax_rename.php +++ b/apps/files/tests/ajax_rename.php @@ -48,7 +48,7 @@ protected function setUp() { \OC_User::createUser(self::$user, 'password'); \OC_User::setUserId(self::$user); - \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files'); + \OC_Util::setupFS(self::$user); $l10nMock = $this->getMock('\OC_L10N', array('t'), array(), '', false); $l10nMock->expects($this->any()) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index eadd2b64b80c..32405deb0567 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -37,6 +37,8 @@ class Hooks { // file for which we want to delete the keys after the delete operation was successful private static $umountedFiles = array(); + private static $proxyStatus; + /** * Startup encryption backend upon user login * @note This method should never be called for users using client side encryption @@ -233,7 +235,7 @@ public static function setPassphrase($params) { $newUserPassword = $params['password']; // make sure that the users home is mounted - \OC\Files\Filesystem::initMountPoints($user); + \OC::$server->setupFilesystem($user); $keypair = Crypt::createKeypair(); @@ -704,4 +706,15 @@ public static function postUmount($params) { } } + /** + * disable encryption while copying the skeleton + */ + public static function preCopySkeleton(){ + self::$proxyStatus = \OC_FileProxy::$enabled; + self::$proxyStatus = false; + } + + public static function postCopySkeleton() { + \OC_FileProxy::$enabled = self::$proxyStatus; + } } diff --git a/apps/files_encryption/lib/helper.php b/apps/files_encryption/lib/helper.php index 7a50ade82f39..fcd6ebadf0ab 100644 --- a/apps/files_encryption/lib/helper.php +++ b/apps/files_encryption/lib/helper.php @@ -71,6 +71,10 @@ public static function registerFilesystemHooks() { \OCP\Util::connectHook('OC_Filesystem', 'post_umount', 'OCA\Encryption\Hooks', 'postUmount'); \OCP\Util::connectHook('OC_Filesystem', 'umount', 'OCA\Encryption\Hooks', 'preUmount'); \OCP\Util::connectHook('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', 'OCA\Encryption\Hooks', 'postPasswordReset'); + + $factory = \OC::$server->getFilesystemFactory(); + $factory->listen('\OC\Files','preCopySkeleton', array('OCA\Encryption\Hooks', 'preCopySkeleton')); + $factory->listen('\OC\Files', 'postCopySkeleton', array('OCA\Encryption\Hooks', 'postCopySkeleton')); } /** diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index d214d13de693..a71245092978 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -81,7 +81,7 @@ public function __construct($view, $userId, $client = false) { $this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key // make sure that the owners home is mounted - \OC\Files\Filesystem::initMountPoints($userId); + \OC::$server->setupFilesystem($userId); if (\OCA\Encryption\Helper::isPublicAccess()) { $this->keyId = $this->publicShareKeyId; @@ -1215,7 +1215,7 @@ public function getUidAndFilename($path) { } // NOTE: Bah, this dependency should be elsewhere - \OC\Files\Filesystem::initMountPoints($fileOwnerUid); + \OC::$server->setupFilesystem($fileOwnerUid); // If the file owner is the currently logged in user if ($fileOwnerUid === $this->userId) { diff --git a/apps/files_sharing/ajax/publicpreview.php b/apps/files_sharing/ajax/publicpreview.php index f5343a7ef269..a285905acdc6 100644 --- a/apps/files_sharing/ajax/publicpreview.php +++ b/apps/files_sharing/ajax/publicpreview.php @@ -41,7 +41,6 @@ OCP\JSON::checkUserExists($rootLinkItem['uid_owner']); \OC_Util::setupFS($userId); -\OC\Files\Filesystem::initMountPoints($userId); $view = new \OC\Files\View('/' . $userId . '/files'); $pathId = $linkedItem['file_source']; diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index 270ed704bbd1..c0f0f4119c0c 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -53,7 +53,7 @@ private function getSourceCache($target) { } $source = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType()); if (isset($source['path']) && isset($source['fileOwner'])) { - \OC\Files\Filesystem::initMountPoints($source['fileOwner']); + \OC::$server->setupFilesystem($source['fileOwner']); $mounts = \OC\Files\Filesystem::getMountByNumericId($source['storage']); if (is_array($mounts) and !empty($mounts)) { $fullPath = $mounts[0]->getMountPoint() . $source['path']; diff --git a/apps/files_sharing/lib/helper.php b/apps/files_sharing/lib/helper.php index f7204a8db8f6..86543e3b516d 100644 --- a/apps/files_sharing/lib/helper.php +++ b/apps/files_sharing/lib/helper.php @@ -141,7 +141,7 @@ public static function authenticate($linkItem, $password = null) { public static function getSharesFromItem($target) { $result = array(); $owner = \OC\Files\Filesystem::getOwner($target); - \OC\Files\Filesystem::initMountPoints($owner); + \OC::$server->setupFilesystem($owner); $info = \OC\Files\Filesystem::getFileInfo($target); $ownerView = new \OC\Files\View('/'.$owner.'/files'); if ( $owner != \OCP\User::getUser() ) { @@ -179,7 +179,7 @@ public static function getSharesFromItem($target) { public static function getUidAndFilename($filename) { $uid = \OC\Files\Filesystem::getOwner($filename); - \OC\Files\Filesystem::initMountPoints($uid); + \OC::$server->setupFilesystem($uid); if ( $uid != \OCP\User::getUser() ) { $info = \OC\Files\Filesystem::getFileInfo($filename); $ownerView = new \OC\Files\View('/'.$uid.'/files'); diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index a5b4e75bcebb..266f408212fc 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -74,7 +74,7 @@ public function generateTarget($filePath, $shareWith, $exclude = null) { return $target; } - \OC\Files\Filesystem::initMountPoints($shareWith); + \OC::$server->setupFilesystem($shareWith); $view = new \OC\Files\View('/' . $shareWith . '/files'); if (!$view->is_dir($shareFolder)) { diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 19ee6085e47e..7c40049243ce 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -86,7 +86,7 @@ public function getSourcePath($target) { $source = $this->getFile($target); if ($source) { if (!isset($source['fullPath'])) { - \OC\Files\Filesystem::initMountPoints($source['fileOwner']); + \OC::$server->setupFilesystem($source['fileOwner']); $mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']); if (is_array($mount) && !empty($mount)) { $this->files[$target]['fullPath'] = $mount[key($mount)]->getMountPoint() . $source['path']; diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index a34140f5a353..3ad2bfe6500a 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -35,7 +35,7 @@ static private function correctUsersFolder($user, $path) { // $path points to the mount point which is a virtual folder, so we start with // the parent $path = '/files' . dirname($path); - \OC\Files\Filesystem::initMountPoints($user); + \OC::$server->setupFilesystem($user); $view = new \OC\Files\View('/' . $user); if ($view->file_exists($path)) { while ($path !== dirname($path)) { diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 52d24143902b..b48d5f314ba1 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -32,7 +32,7 @@ class Trashbin { public static function getUidAndFilename($filename) { $uid = \OC\Files\Filesystem::getOwner($filename); - \OC\Files\Filesystem::initMountPoints($uid); + \OC::$server->setupFilesystem($uid); if ($uid != \OCP\User::getUser()) { $info = \OC\Files\Filesystem::getFileInfo($filename); $ownerView = new \OC\Files\View('/' . $uid . '/files'); diff --git a/apps/files_versions/lib/versions.php b/apps/files_versions/lib/versions.php index 82e0ecc3e2f2..26d07d455cdd 100644 --- a/apps/files_versions/lib/versions.php +++ b/apps/files_versions/lib/versions.php @@ -43,7 +43,7 @@ class Storage { public static function getUidAndFilename($filename) { $uid = \OC\Files\Filesystem::getOwner($filename); - \OC\Files\Filesystem::initMountPoints($uid); + \OC::$server->setupFilesystem($uid); if ( $uid != \OCP\User::getUser() ) { $info = \OC\Files\Filesystem::getFileInfo($filename); $ownerView = new \OC\Files\View('/'.$uid.'/files'); diff --git a/lib/private/cache/file.php b/lib/private/cache/file.php index 4e7c065678e3..1e978e8a0f6b 100644 --- a/lib/private/cache/file.php +++ b/lib/private/cache/file.php @@ -21,7 +21,7 @@ protected function getStorage() { return $this->storage; } if(\OC_User::isLoggedIn()) { - \OC\Files\Filesystem::initMountPoints(\OC_User::getUser()); + \OC::$server->setupFilesystem(\OC_User::getUser()); $this->storage = new \OC\Files\View('/' . \OC_User::getUser() . '/cache'); return $this->storage; }else{ diff --git a/lib/private/fileproxy/fileoperations.php b/lib/private/fileproxy/fileoperations.php deleted file mode 100644 index b2ff2e7e5e99..000000000000 --- a/lib/private/fileproxy/fileoperations.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU AFFERO GENERAL PUBLIC LICENSE for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see . - * - */ - -/** - * check if standard file operations - */ - -class OC_FileProxy_FileOperations extends OC_FileProxy{ - static $rootView; - - public function premkdir($path) { - if(!self::$rootView) { - self::$rootView = new \OC\Files\View(''); - } - return !self::$rootView->file_exists($path); - } - -} diff --git a/lib/private/files/factory.php b/lib/private/files/factory.php new file mode 100644 index 000000000000..e67f9e232b1e --- /dev/null +++ b/lib/private/files/factory.php @@ -0,0 +1,192 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files; + +use OC\Files\Cache\Scanner; +use OC\Files\Mount\Manager; +use OC\Files\Node\Root; +use OC\Files\Storage\Loader; +use OC\Files\Storage\Wrapper\Quota; +use OC\Hooks\BasicEmitter; +use OCP\Files\FileInfo; + +class Factory extends BasicEmitter { + /** + * @var \OCP\IConfig + */ + protected $config; + + /** + * @var \OC\Files\Node\Root + */ + protected $root; + + /** + * @var \OCP\Files\Folder[] + */ + protected $userFolders = array(); + + /** + * @param \OCP\IConfig $config + */ + public function __construct($config) { + $this->config = $config; + } + + /** + * Mount the data dir as root storage + * + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\Files\Storage\Loader $storageLoader + */ + protected function mountRoot($mountManager, $storageLoader) { + // mount local file backend as root + $configDataDirectory = $this->config->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data"); + //first set up the local "root" storage + $mount = new Mount\Mount('\OC\Files\Storage\Local', '/', array('datadir' => $configDataDirectory), $storageLoader); + $mountManager->addMount($mount); + } + + /** + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\Files\Storage\Loader $storageLoader + * @param \OCP\IUser $user + */ + protected function mountUserFolder($mountManager, $storageLoader, $user) { + // check for legacy home id (<= 5.0.12) + $legacy = \OC\Files\Cache\Storage::exists('local::' . $user->getHome() . '/'); + $mount = new Mount\Mount('\OC\Files\Storage\Home', '/' . $user->getUID(), array( + 'datadir' => $user->getHome(), + 'user' => $user, + 'legacy' => $legacy + ), $storageLoader); + $mountManager->addMount($mount); + } + + /** + * Mount the cache storage if configured and create the cache dir if needed + * + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\Files\Storage\Loader $storageLoader + * @param \OCP\IUser $user + */ + protected function setupCacheDir($mountManager, $storageLoader, $user) { + $cacheBaseDir = $this->config->getSystemValue('cache_path', ''); + if ($cacheBaseDir === '') { + // use local cache dir relative to the user's home + $mount = $mountManager->find('/' . $user->getUID()); + $userStorage = $mount->getStorage(); + if (!$userStorage->file_exists('cache')) { + $userStorage->mkdir('cache'); + } + } else { + $cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user->getUID(); + if (!file_exists($cacheDir)) { + mkdir($cacheDir, 0770, true); + } + $mount = new Mount\Mount('\OC\Files\Storage\Local', '/' . $user->getUID() . '/cache', array('datadir' => $cacheDir), $storageLoader); + $mountManager->addMount($mount); + } + } + + /** + * @param \OCP\IUser $user + */ + protected function copySkeleton($user) { + $root = $this->getRoot(); + $path = '/' . $user->getUID() . '/files'; + if (!$root->nodeExists($path)) { + $this->emit('\OC\Files', 'preCopySkeleton', array($user)); + $userDirectory = $root->newFolder($path); + $skeletonDirectory = $this->config->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); + if (!empty($skeletonDirectory)) { + \OC_Util::copyr($skeletonDirectory, $userDirectory); + } + $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); + $this->emit('\OC\Files', 'postCopySkeleton', array($user)); + } + } + + /** + * Create the root filesystem + * + * @return \OC\Files\Node\Root + */ + public function getRoot() { + if ($this->root) { + return $this->root; + } + $mountManager = new Manager(); + $storageLoader = new Loader(); + \OC::$server->getEventLogger()->start('setup_fs_root', 'Setup filesystem root'); + + $storageLoader->addStorageWrapper('oc_quota', function ($mountPoint, $storage) { + /** + * @var \OC\Files\Storage\Storage $storage + */ + if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { + /** + * @var \OCP\Files\IHomeStorage $storage + */ + if (is_object($storage->getUser())) { + $user = $storage->getUser()->getUID(); + $quota = \OC_Util::getUserQuota($user); + if ($quota !== FileInfo::SPACE_UNLIMITED) { + return new Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); + } + } + } + return $storage; + }); + + $this->mountRoot($mountManager, $storageLoader); + \OC::$server->getEventLogger()->end('setup_fs_root'); + $this->root = new Root($mountManager, $storageLoader, new View('')); + return $this->root; + } + + /** + * Mount the storages for a user + * + * @param \OCP\IUser $user + * @return \OCP\Files\Folder + */ + public function getUserFolder($user) { + if (isset($this->userFolders[$user->getUID()])) { + return $this->userFolders[$user->getUID()]; + } + $root = $this->getRoot(); + $mountManager = $root->getMountManager(); + $storageLoader = $root->getStorageLoader(); + if (is_null($mountManager->getMount('/' . $user->getUID()))) { + \OC::$server->getEventLogger()->start('setup_fs_' . $user->getUID(), 'Setup filesystem for ' . $user->getUID()); + $this->mountUserFolder($mountManager, $storageLoader, $user); + $this->setupCacheDir($mountManager, $storageLoader, $user); + $this->copySkeleton($user); + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user->getUID(), 'user_dir' => $user->getHome())); + + \OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user->getUID(), 'user_dir' => $user->getHome() . '/files')); + \OC::$server->getEventLogger()->end('setup_fs_' . $user->getUID()); + } + $this->userFolders[$user->getUID()] = $root->get('/' . $user->getUID()); + return $this->userFolders[$user->getUID()]; + } + + /** + * Clear all mounts in the filesystem + * + * @param bool $preserveRoot whether or not to keep the root storage + */ + public function clear($preserveRoot = false) { + if ($this->root) { + $this->userFolders = array(); + $this->root->getMountManager()->clear($preserveRoot); + } + } +} diff --git a/lib/private/files/fileinfomanager.php b/lib/private/files/fileinfomanager.php new file mode 100644 index 000000000000..923cf7139178 --- /dev/null +++ b/lib/private/files/fileinfomanager.php @@ -0,0 +1,37 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files; + +class FileInfoManager { + /** + * @var \OC\Files\FileInfo[] + */ + protected $entries = array(); + + /** + * @var \OC\Files\View + */ + protected $view; + + public function __construct(View $view) { + $this->view = $view; + } + + public function register(FileInfo $entry) { + $this->entries[$entry->getPath()] = $entry; + } + + public function getFileInfo($path) { + if (isset($this->entries[$path])) { + return $this->entries[$path]; + } else { + return null; + } + } +} diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 6c8fa8c90baf..35fc62df1c6c 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -30,22 +30,18 @@ namespace OC\Files; -use OC\Files\Storage\Loader; - class Filesystem { - /** - * @var Mount\Manager $mounts + * @var \OC\Files\View $defaultInstance */ - private static $mounts; + static private $defaultInstance; - public static $loaded = false; /** - * @var \OC\Files\View $defaultInstance + * @var \OCP\IUser */ - static private $defaultInstance; + static public $activeUser; - static private $usersSetup = array(); + static public $loaded; /** * classname which used for hooks handling @@ -165,11 +161,7 @@ class Filesystem { const signal_param_users = 'users'; /** - * @var \OC\Files\Storage\Loader $loader - */ - private static $loader; - - /** + * @param string $wrapperName * @param callable $wrapper */ public static function addStorageWrapper($wrapperName, $wrapper) { @@ -182,17 +174,15 @@ public static function addStorageWrapper($wrapperName, $wrapper) { } public static function getLoader() { - if (!self::$loader) { - self::$loader = new Loader(); - } - return self::$loader; + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + return $root->getStorageLoader(); } public static function getMountManager() { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - return self::$mounts; + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + return $root->getMountManager(); } /** @@ -205,10 +195,9 @@ public static function getMountManager() { * @return string */ static public function getMountPoint($path) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - $mount = self::$mounts->find($path); + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + $mount = $root->getMount($path); if ($mount) { return $mount->getMountPoint(); } else { @@ -223,11 +212,10 @@ static public function getMountPoint($path) { * @return string[] */ static public function getMountPoints($path) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + $mounts = $root->getMountsIn($path); $result = array(); - $mounts = self::$mounts->findIn($path); foreach ($mounts as $mount) { $result[] = $mount->getMountPoint(); } @@ -241,10 +229,9 @@ static public function getMountPoints($path) { * @return \OC\Files\Storage\Storage */ public static function getStorage($mountPoint) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - $mount = self::$mounts->find($mountPoint); + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + $mount = $root->getMount($mountPoint); return $mount->getStorage(); } @@ -253,10 +240,9 @@ public static function getStorage($mountPoint) { * @return Mount\Mount[] */ public static function getMountByStorageId($id) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - return self::$mounts->findByStorageId($id); + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + return $root->getMountByStorageId($id); } /** @@ -264,10 +250,9 @@ public static function getMountByStorageId($id) { * @return Mount\Mount[] */ public static function getMountByNumericId($id) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - return self::$mounts->findByNumericId($id); + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + return $root->getMountByNumericStorageId($id); } /** @@ -277,10 +262,9 @@ public static function getMountByNumericId($id) { * @return array an array consisting of the storage and the internal path */ static public function resolvePath($path) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } - $mount = self::$mounts->find($path); + /** @var \OC\Files\Node\Root $root */ + $root = \OC::$server->getRootFolder(); + $mount = $root->getMount($path); if ($mount) { return array($mount->getStorage(), $mount->getInternalPath($path)); } else { @@ -288,112 +272,13 @@ static public function resolvePath($path) { } } - static public function init($user, $root) { - if (self::$defaultInstance) { - return false; - } - self::getLoader(); - self::$defaultInstance = new View($root); - - if (!self::$mounts) { - self::$mounts = new Mount\Manager(); - } - - //load custom mount config - self::initMountPoints($user); - - self::$loaded = true; - - return true; - } - - static public function initMounts() { - if (!self::$mounts) { - self::$mounts = new Mount\Manager(); - } - } - /** * Initialize system and personal mount points for a user * * @param string $user */ public static function initMountPoints($user = '') { - if ($user == '') { - $user = \OC_User::getUser(); - } - if (isset(self::$usersSetup[$user])) { - return; - } - self::$usersSetup[$user] = true; - - $root = \OC_User::getHome($user); - - $userObject = \OC_User::getManager()->get($user); - - if (!is_null($userObject)) { - $homeStorage = \OC_Config::getValue( 'objectstore' ); - if (!empty($homeStorage)) { - // sanity checks - if (empty($homeStorage['class'])) { - \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR); - } - if (!isset($homeStorage['arguments'])) { - $homeStorage['arguments'] = array(); - } - // instantiate object store implementation - $homeStorage['arguments']['objectstore'] = new $homeStorage['class']($homeStorage['arguments']); - // mount with home object store implementation - $homeStorage['class'] = '\OC\Files\ObjectStore\HomeObjectStoreStorage'; - } else { - $homeStorage = array( - //default home storage configuration: - 'class' => '\OC\Files\Storage\Home', - 'arguments' => array() - ); - } - $homeStorage['arguments']['user'] = $userObject; - - // check for legacy home id (<= 5.0.12) - if (\OC\Files\Cache\Storage::exists('local::' . $root . '/')) { - $homeStorage['arguments']['legacy'] = true; - } - - self::mount($homeStorage['class'], $homeStorage['arguments'], $user); - - $home = \OC\Files\Filesystem::getStorage($user); - } - else { - self::mount('\OC\Files\Storage\Local', array('datadir' => $root), $user); - } - - self::mountCacheDir($user); - - // Chance to mount for other storages - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root)); - } - - /** - * Mounts the cache directory - * @param string $user user name - */ - private static function mountCacheDir($user) { - $cacheBaseDir = \OC_Config::getValue('cache_path', ''); - if ($cacheBaseDir === '') { - // use local cache dir relative to the user's home - $subdir = 'cache'; - $view = new \OC\Files\View('/' . $user); - if(!$view->file_exists($subdir)) { - $view->mkdir($subdir); - } - } else { - $cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user; - if (!file_exists($cacheDir)) { - mkdir($cacheDir, 0770, true); - } - // mount external cache dir to "/$user/cache" mount point - self::mount('\OC\Files\Storage\Local', array('datadir' => $cacheDir), '/' . $user . '/cache'); - } + \OC::$server->setupFilesystem($user); } /** @@ -402,6 +287,14 @@ private static function mountCacheDir($user) { * @return View */ static public function getView() { + if (!self::$defaultInstance) { + $user = self::$activeUser; + if ($user) { + self::$defaultInstance = new View('/' . $user->getUID() . '/files'); + } else { + self::$defaultInstance = new View(); + } + } return self::$defaultInstance; } @@ -409,31 +302,26 @@ static public function getView() { * tear down the filesystem, removing all storage providers */ static public function tearDown() { - self::clearMounts(); + \OC::$server->getFilesystemFactory()->clear(true); self::$defaultInstance = null; } /** * get the relative path of the root data directory for the current user + * * @return string * * Returns path like /admin/files */ static public function getRoot() { - if (!self::$defaultInstance) { - return null; - } - return self::$defaultInstance->getRoot(); + return self::getView()->getRoot(); } /** * clear all mounts and storage backends */ public static function clearMounts() { - if (self::$mounts) { - self::$usersSetup = array(); - self::$mounts->clear(); - } + \OC::$server->getFilesystemFactory()->clear(); } /** @@ -444,11 +332,8 @@ public static function clearMounts() { * @param string $mountpoint */ static public function mount($class, $arguments, $mountpoint) { - if (!self::$mounts) { - \OC_Util::setupFS(); - } $mount = new Mount\Mount($class, $mountpoint, $arguments, self::getLoader()); - self::$mounts->addMount($mount); + self::getMountManager()->addMount($mount); } /** @@ -460,7 +345,7 @@ static public function mount($class, $arguments, $mountpoint) { * @return string */ static public function getLocalFile($path) { - return self::$defaultInstance->getLocalFile($path); + return self::getView()->getLocalFile($path); } /** @@ -468,22 +353,7 @@ static public function getLocalFile($path) { * @return string */ static public function getLocalFolder($path) { - return self::$defaultInstance->getLocalFolder($path); - } - - /** - * return path to file which reflects one visible in browser - * - * @param string $path - * @return string - */ - static public function getLocalPath($path) { - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; - $newpath = $path; - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { - $newpath = substr($path, strlen($datadir)); - } - return $newpath; + return self::getView()->getLocalFolder($path); } /** @@ -535,6 +405,7 @@ static public function isFileBlacklisted($filename) { /** * check if the directory should be ignored when scanning * NOTE: the special directories . and .. would cause never ending recursion + * * @param String $dir * @return boolean */ @@ -549,136 +420,136 @@ static public function isIgnoredDir($dir) { * following functions are equivalent to their php builtin equivalents for arguments/return values. */ static public function mkdir($path) { - return self::$defaultInstance->mkdir($path); + return self::getView()->mkdir($path); } static public function rmdir($path) { - return self::$defaultInstance->rmdir($path); + return self::getView()->rmdir($path); } static public function opendir($path) { - return self::$defaultInstance->opendir($path); + return self::getView()->opendir($path); } static public function readdir($path) { - return self::$defaultInstance->readdir($path); + return self::getView()->readdir($path); } static public function is_dir($path) { - return self::$defaultInstance->is_dir($path); + return self::getView()->is_dir($path); } static public function is_file($path) { - return self::$defaultInstance->is_file($path); + return self::getView()->is_file($path); } static public function stat($path) { - return self::$defaultInstance->stat($path); + return self::getView()->stat($path); } static public function filetype($path) { - return self::$defaultInstance->filetype($path); + return self::getView()->filetype($path); } static public function filesize($path) { - return self::$defaultInstance->filesize($path); + return self::getView()->filesize($path); } static public function readfile($path) { - return self::$defaultInstance->readfile($path); + return self::getView()->readfile($path); } static public function isCreatable($path) { - return self::$defaultInstance->isCreatable($path); + return self::getView()->isCreatable($path); } static public function isReadable($path) { - return self::$defaultInstance->isReadable($path); + return self::getView()->isReadable($path); } static public function isUpdatable($path) { - return self::$defaultInstance->isUpdatable($path); + return self::getView()->isUpdatable($path); } static public function isDeletable($path) { - return self::$defaultInstance->isDeletable($path); + return self::getView()->isDeletable($path); } static public function isSharable($path) { - return self::$defaultInstance->isSharable($path); + return self::getView()->isSharable($path); } static public function file_exists($path) { - return self::$defaultInstance->file_exists($path); + return self::getView()->file_exists($path); } static public function filemtime($path) { - return self::$defaultInstance->filemtime($path); + return self::getView()->filemtime($path); } static public function touch($path, $mtime = null) { - return self::$defaultInstance->touch($path, $mtime); + return self::getView()->touch($path, $mtime); } /** * @return string */ static public function file_get_contents($path) { - return self::$defaultInstance->file_get_contents($path); + return self::getView()->file_get_contents($path); } static public function file_put_contents($path, $data) { - return self::$defaultInstance->file_put_contents($path, $data); + return self::getView()->file_put_contents($path, $data); } static public function unlink($path) { - return self::$defaultInstance->unlink($path); + return self::getView()->unlink($path); } static public function rename($path1, $path2) { - return self::$defaultInstance->rename($path1, $path2); + return self::getView()->rename($path1, $path2); } static public function copy($path1, $path2) { - return self::$defaultInstance->copy($path1, $path2); + return self::getView()->copy($path1, $path2); } static public function fopen($path, $mode) { - return self::$defaultInstance->fopen($path, $mode); + return self::getView()->fopen($path, $mode); } /** * @return string */ static public function toTmpFile($path) { - return self::$defaultInstance->toTmpFile($path); + return self::getView()->toTmpFile($path); } static public function fromTmpFile($tmpFile, $path) { - return self::$defaultInstance->fromTmpFile($tmpFile, $path); + return self::getView()->fromTmpFile($tmpFile, $path); } static public function getMimeType($path) { - return self::$defaultInstance->getMimeType($path); + return self::getView()->getMimeType($path); } static public function hash($type, $path, $raw = false) { - return self::$defaultInstance->hash($type, $path, $raw); + return self::getView()->hash($type, $path, $raw); } static public function free_space($path = '/') { - return self::$defaultInstance->free_space($path); + return self::getView()->free_space($path); } static public function search($query) { - return self::$defaultInstance->search($query); + return self::getView()->search($query); } /** * @param string $query */ static public function searchByMime($query) { - return self::$defaultInstance->searchByMime($query); + return self::getView()->searchByMime($query); } /** @@ -689,11 +560,12 @@ static public function searchByMime($query) { * @return bool */ static public function hasUpdated($path, $time) { - return self::$defaultInstance->hasUpdated($path, $time); + return self::getView()->hasUpdated($path, $time); } /** * Fix common problems with a file path + * * @param string $path * @param bool $stripTrailingSlash * @return string @@ -754,7 +626,7 @@ public static function normalizePath($path, $stripTrailingSlash = true, $isAbsol * @return \OC\Files\FileInfo */ public static function getFileInfo($path, $includeMountPoints = true) { - return self::$defaultInstance->getFileInfo($path, $includeMountPoints); + return self::getView()->getFileInfo($path, $includeMountPoints); } /** @@ -767,7 +639,7 @@ public static function getFileInfo($path, $includeMountPoints = true) { * returns the fileid of the updated file */ public static function putFileInfo($path, $data) { - return self::$defaultInstance->putFileInfo($path, $data); + return self::getView()->putFileInfo($path, $data); } /** @@ -778,7 +650,7 @@ public static function putFileInfo($path, $data) { * @return \OC\Files\FileInfo[] */ public static function getDirectoryContent($directory, $mimetype_filter = '') { - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); + return self::getView()->getDirectoryContent($directory, $mimetype_filter); } /** @@ -790,7 +662,7 @@ public static function getDirectoryContent($directory, $mimetype_filter = '') { * @return string */ public static function getPath($id) { - return self::$defaultInstance->getPath($id); + return self::getView()->getPath($id); } /** @@ -800,7 +672,7 @@ public static function getPath($id) { * @return string */ public static function getOwner($path) { - return self::$defaultInstance->getOwner($path); + return self::getView()->getOwner($path); } /** @@ -810,6 +682,6 @@ public static function getOwner($path) { * @return string */ static public function getETag($path) { - return self::$defaultInstance->getETag($path); + return self::getView()->getETag($path); } } diff --git a/lib/private/files/mount/manager.php b/lib/private/files/mount/manager.php index 0ccf42941dec..7ba3cc572d69 100644 --- a/lib/private/files/mount/manager.php +++ b/lib/private/files/mount/manager.php @@ -23,6 +23,14 @@ public function addMount(Mount $mount) { $this->mounts[$mount->getMountPoint()] = $mount; } + /** + * @param string $mountPoint + * @return Mount + */ + public function getMount($mountPoint) { + return isset($this->mounts[$mountPoint]) ? $this->mounts[$mountPoint] : null; + } + /** * @param string $mountPoint */ @@ -38,7 +46,7 @@ public function removeMount($mountPoint) { * @param string $mountPoint * @param string $target */ - public function moveMount($mountPoint, $target){ + public function moveMount($mountPoint, $target) { $this->mounts[$target] = $this->mounts[$mountPoint]; unset($this->mounts[$mountPoint]); } @@ -50,7 +58,6 @@ public function moveMount($mountPoint, $target){ * @return Mount */ public function find($path) { - \OC_Util::setupFS(); $path = $this->formatPath($path); if (isset($this->mounts[$path])) { return $this->mounts[$path]; @@ -78,7 +85,6 @@ public function find($path) { * @return Mount[] */ public function findIn($path) { - \OC_Util::setupFS(); $path = $this->formatPath($path); $result = array(); $pathLength = strlen($path); @@ -91,8 +97,20 @@ public function findIn($path) { return $result; } - public function clear() { - $this->mounts = array(); + /** + * Clear all mounts in the filesystem + * + * @param bool $preserveRoot whether or not to keep the root storage + */ + public function clear($preserveRoot) { + if ($preserveRoot) { + $this->mounts = array_filter($this->mounts, function ($mount) { + /** @var \OC\Files\Mount\Mount $mount */ + return $mount->getMountPoint() === '/'; + }); + } else { + $this->mounts = array(); + } } /** @@ -102,7 +120,6 @@ public function clear() { * @return Mount[] */ public function findByStorageId($id) { - \OC_Util::setupFS(); if (strlen($id) > 64) { $id = md5($id); } diff --git a/lib/private/files/node/node.php b/lib/private/files/node/node.php index bc0759117497..687b482611d9 100644 --- a/lib/private/files/node/node.php +++ b/lib/private/files/node/node.php @@ -225,6 +225,15 @@ protected function normalizePath($path) { return $path; } + /** + * Get the root folder of the filesystem + * + * @return \OCP\Files\Folder + */ + public function getRoot() { + return $this->root; + } + /** * check if the requested path is valid * diff --git a/lib/private/files/node/root.php b/lib/private/files/node/root.php index 18e7a6b681a8..39ba27e1bd05 100644 --- a/lib/private/files/node/root.php +++ b/lib/private/files/node/root.php @@ -43,34 +43,39 @@ class Root extends Folder implements Emitter { private $mountManager; /** - * @var \OC\Hooks\PublicEmitter + * @var \OC\Files\Storage\Loader */ - private $emitter; + private $loader; /** - * @var \OC\User\User $user + * @var \OC\Hooks\PublicEmitter */ - private $user; + private $emitter; /** * @param \OC\Files\Mount\Manager $manager + * @param \OC\Files\Storage\Loader $loader * @param \OC\Files\View $view - * @param \OC\User\User $user */ - public function __construct($manager, $view, $user) { + public function __construct($manager, $loader, $view) { parent::__construct($this, $view, ''); + $this->loader = $loader; $this->mountManager = $manager; - $this->user = $user; $this->emitter = new PublicEmitter(); } /** - * Get the user for which the filesystem is setup - * - * @return \OC\User\User + * @return \OC\Files\Mount\Manager + */ + public function getMountManager() { + return $this->mountManager; + } + + /** + * @return \OC\Files\Storage\Loader */ - public function getUser() { - return $this->user; + public function getStorageLoader() { + return $this->loader; } /** @@ -307,4 +312,13 @@ public function getParent() { public function getName() { return ''; } + + /** + * Get the root folder of the filesystem + * + * @return \OCP\Files\Folder + */ + public function getRoot() { + return $this; + } } diff --git a/lib/private/files/objectstorefactory.php b/lib/private/files/objectstorefactory.php new file mode 100644 index 000000000000..7b3416150d13 --- /dev/null +++ b/lib/private/files/objectstorefactory.php @@ -0,0 +1,55 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files; + +class ObjectStoreFactory extends Factory { + /** + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\Files\Storage\Loader $storageLoader + */ + protected function mountRoot($mountManager, $storageLoader) { + $config = $this->config->getSystemValue('objectstore'); + // check misconfiguration + if (empty($config['class'])) { + \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR); + } + if (!isset($config['arguments'])) { + $config['arguments'] = array(); + } + + // instantiate object store implementation + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); + // mount with plain / root object store implementation + $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; + + $mount = new Mount\Mount('\OC\Files\ObjectStore\ObjectStoreStorage', '/', $config['arguments'], $storageLoader); + $mountManager->addMount($mount); + } + + /** + * @param \OC\Files\Mount\Manager $mountManager + * @param \OC\Files\Storage\Loader $storageLoader + * @param \OCP\IUser $user + */ + protected function mountUserFolder($mountManager, $storageLoader, $user) { + $config = $this->config->getSystemValue('objectstore'); + // sanity checks + if (empty($config['class'])) { + \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR); + } + if (!isset($config['arguments'])) { + $config['arguments'] = array(); + } + // instantiate object store implementation + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); + + $mount = new Mount\Mount('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $storageLoader); + $mountManager->addMount($mount); + } +} diff --git a/lib/private/files/utils/scanner.php b/lib/private/files/utils/scanner.php index adb66497be07..07067ab74f7d 100644 --- a/lib/private/files/utils/scanner.php +++ b/lib/private/files/utils/scanner.php @@ -39,14 +39,21 @@ class Scanner extends PublicEmitter { */ protected $db; + /** + * @var \OCP\Files\Folder + */ + protected $userFolder; + /** * @param string $user * @param \OCP\IDBConnection $db + * @param \OCP\Files\Folder $userFolder */ - public function __construct($user, $db) { + public function __construct($user, $db, $userFolder) { $this->user = $user; $this->propagator = new ChangePropagator(new View('')); $this->db = $db; + $this->userFolder = $userFolder; } /** @@ -56,12 +63,11 @@ public function __construct($user, $db) { * @return \OC\Files\Mount\Mount[] */ protected function getMounts($dir) { - //TODO: move to the node based fileapi once that's done - \OC_Util::tearDownFS(); - \OC_Util::setupFS($this->user); - $absolutePath = Filesystem::getView()->getAbsolutePath($dir); + /** @var \OC\Files\Node\Root $root */ + $root = $this->userFolder->getRoot(); + $mountManager = $root->getMountManager(); + $absolutePath=$this->userFolder->getFullPath($dir); - $mountManager = Filesystem::getMountManager(); $mounts = $mountManager->findIn($absolutePath); $mounts[] = $mountManager->find($absolutePath); $mounts = array_reverse($mounts); //start with the mount of $dir diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 19676524a0ed..dac06ecebc2f 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -804,9 +804,6 @@ private function shouldEmitHooks($path = '') { if ($path && Cache\Scanner::isPartialFile($path)) { return false; } - if (!Filesystem::$loaded) { - return false; - } $defaultRoot = Filesystem::getRoot(); if ($defaultRoot === null) { return false; diff --git a/lib/private/image.php b/lib/private/image.php index bab91745c050..7cc2f1388a7d 100644 --- a/lib/private/image.php +++ b/lib/private/image.php @@ -528,7 +528,7 @@ public function loadFromFile($imagePath=false) { default: // this is mostly file created from encrypted file - $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); + $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents($imagePath)); $iType = IMAGETYPE_PNG; OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG); break; diff --git a/lib/private/server.php b/lib/private/server.php index c413ee8bf6d7..8a8ba2f25038 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -73,7 +73,7 @@ function __construct() { $this->registerService('PreviewManager', function ($c) { return new PreviewManager(); }); - $this->registerService('TagMapper', function(Server $c) { + $this->registerService('TagMapper', function (Server $c) { return new TagMapper($c->getDb()); }); $this->registerService('TagManager', function (Server $c) { @@ -81,15 +81,22 @@ function __construct() { $user = \OC_User::getUser(); return new TagManager($tagMapper, $user); }); + $this->registerService('FilesystemFactory', function (Server $c) { + \OC_App::loadApps(array('filesystem')); + $config = $c->getConfig(); + if ($config->getSystemValue('objectstore', false)) { + return new \OC\Files\ObjectStoreFactory($config); + } else { + return new \OC\Files\Factory($config); + } + }); $this->registerService('RootFolder', function (Server $c) { - // TODO: get user and user manager from container as well - $user = \OC_User::getUser(); - /** @var $c SimpleContainer */ - $userManager = $c->query('UserManager'); - $user = $userManager->get($user); - $manager = \OC\Files\Filesystem::getMountManager(); - $view = new View(); - return new Root($manager, $view, $user); + $userSession = $c->getUserSession(); + $user = $userSession->getUser(); + $factory = $c->getFilesystemFactory(); + \OC\Files\Filesystem::$activeUser = $user; + \OC\Files\Filesystem::$loaded = true; + return $factory->getRoot(); }); $this->registerService('UserManager', function (Server $c) { $config = $c->getConfig(); @@ -291,6 +298,15 @@ function getAvatarManager() { return $this->query('AvatarManager'); } + /** + * Returns filesystem factory + * + * @return \OC\Files\Factory + */ + function getFilesystemFactory() { + return $this->query('FilesystemFactory'); + } + /** * Returns the root folder of ownCloud's data directory * @@ -300,6 +316,21 @@ function getRootFolder() { return $this->query('RootFolder'); } + /** + * Setup the filesystem for a user + * + * @param string $userId + */ + function setupFilesystem($userId) { + /** @var \OC\Files\Node\Root $root */ + $this->getRootFolder(); + $user = $this->getUserManager()->get($userId); + if ($user) { + $factory = \OC::$server->getFilesystemFactory(); + $factory->getUserFolder($user); + } + } + /** * Returns a view to ownCloud's files folder * @@ -312,41 +343,13 @@ function getUserFolder($userId = null) { if (!$user) { return null; } - $userId = $user->getUID(); } else { $user = $this->getUserManager()->get($userId); } - $dir = '/' . $userId; - $root = $this->getRootFolder(); - $folder = null; - - if (!$root->nodeExists($dir)) { - $folder = $root->newFolder($dir); - } else { - $folder = $root->get($dir); - } - - $dir = '/files'; - if (!$folder->nodeExists($dir)) { - $folder = $folder->newFolder($dir); + $factory = $this->getFilesystemFactory(); - if (\OCP\App::isEnabled('files_encryption')) { - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - } - - \OC_Util::copySkeleton($user, $folder); - - if (\OCP\App::isEnabled('files_encryption')) { - // re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - } - } else { - $folder = $folder->get($dir); - } - - return $folder; + $userFolder = $factory->getUserFolder($user); + return $userFolder->get('/files'); } /** diff --git a/lib/private/util.php b/lib/private/util.php index 4190f0aa3d8f..810e01a70aff 100644 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -11,128 +11,26 @@ class OC_Util { private static $rootMounted = false; private static $fsSetup = false; - private static function initLocalStorageRootFS() { - // mount local file backend as root - $configDataDirectory = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data"); - //first set up the local "root" storage - \OC\Files\Filesystem::initMounts(); - if (!self::$rootMounted) { - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/'); - self::$rootMounted = true; - } - } - - /** - * mounting an object storage as the root fs will in essence remove the - * necessity of a data folder being present. - * TODO make home storage aware of this and use the object storage instead of local disk access - * - * @param array $config containing 'class' and optional 'arguments' - */ - private static function initObjectStoreRootFS($config) { - // check misconfiguration - if (empty($config['class'])) { - \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR); - } - if (!isset($config['arguments'])) { - $config['arguments'] = array(); - } - - // instantiate object store implementation - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); - // mount with plain / root object store implementation - $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; - - // mount object storage as root - \OC\Files\Filesystem::initMounts(); - if (!self::$rootMounted) { - \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); - self::$rootMounted = true; - } - } - /** * Can be set up * - * @param string $user + * @param string $userId * @return boolean * @description configure the initial filesystem based on the configuration */ - public static function setupFS($user = '') { - //setting up the filesystem twice can only lead to trouble - if (self::$fsSetup) { - return false; - } - - \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); - - // If we are not forced to load a specific user we load the one that is logged in - if ($user == "" && OC_User::isLoggedIn()) { - $user = OC_User::getUser(); - } - - // load all filesystem apps before, so no setup-hook gets lost - OC_App::loadApps(array('filesystem')); - - // the filesystem will finish when $user is not empty, - // mark fs setup here to avoid doing the setup from loading - // OC_Filesystem - if ($user != '') { - self::$fsSetup = true; - } - - //check if we are using an object storage - $objectStore = OC_Config::getValue('objectstore'); - if (isset($objectStore)) { - self::initObjectStoreRootFS($objectStore); + public static function setupFS($userId = '') { + /** @var \OC\Files\Node\Root $root */ + \OC::$server->getRootFolder(); + if ($userId) { + $user = \OC::$server->getUserManager()->get($userId); } else { - self::initLocalStorageRootFS(); - } - - if ($user != '' && !OCP\User::userExists($user)) { - \OC::$server->getEventLogger()->end('setup_fs'); - return false; + $user = \OC::$server->getUserSession()->getUser(); } - - //if we aren't logged in, there is no use to set up the filesystem - if ($user != "") { - \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { - // set up quota for home storages, even for other users - // which can happen when using sharing - - /** - * @var \OC\Files\Storage\Storage $storage - */ - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') - ) { - if (is_object($storage->getUser())) { - $user = $storage->getUser()->getUID(); - $quota = OC_Util::getUserQuota($user); - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { - return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); - } - } - } - - return $storage; - }); - - $userDir = '/' . $user . '/files'; - - //jail the user into his "home" directory - \OC\Files\Filesystem::init($user, $userDir); - - $fileOperationProxy = new OC_FileProxy_FileOperations(); - OC_FileProxy::register($fileOperationProxy); - - //trigger creation of user home and /files folder - \OC::$server->getUserFolder($user); - - OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); + if ($user) { + $factory = \OC::$server->getFilesystemFactory(); + \OC\Files\Filesystem::$activeUser = $user; + $factory->getUserFolder($user); } - \OC::$server->getEventLogger()->end('setup_fs'); - return true; } /** @@ -197,35 +95,13 @@ public static function getUserQuota($user) { if ($userQuota === 'default') { $userQuota = $config->getAppValue('files', 'default_quota', 'none'); } - if($userQuota === 'none') { + if ($userQuota === 'none') { return \OCP\Files\FileInfo::SPACE_UNLIMITED; - }else{ + } else { return OC_Helper::computerFileSize($userQuota); } } - /** - * copies the skeleton to the users /files - * - * @param \OC\User\User $user - * @param \OCP\Files\Folder $userDirectory - */ - public static function copySkeleton(\OC\User\User $user, \OCP\Files\Folder $userDirectory) { - - $skeletonDirectory = \OCP\Config::getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); - - if (!empty($skeletonDirectory)) { - \OCP\Util::writeLog( - 'files_skeleton', - 'copying skeleton for '.$user->getUID().' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), - \OCP\Util::DEBUG - ); - self::copyr($skeletonDirectory, $userDirectory); - // update the file cache - $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); - } - } - /** * copies a directory recursively by using streams * @@ -500,8 +376,8 @@ public static function checkServer(\OCP\IConfig $config) { $errors[] = array( 'error' => $l->t('Cannot write into "config" directory'), 'hint' => $l->t('This can usually be fixed by ' - . '%sgiving the webserver write access to the config directory%s.', - array('', '')) + . '%sgiving the webserver write access to the config directory%s.', + array('', '')) ); } @@ -514,9 +390,9 @@ public static function checkServer(\OCP\IConfig $config) { $errors[] = array( 'error' => $l->t('Cannot write into "apps" directory'), 'hint' => $l->t('This can usually be fixed by ' - . '%sgiving the webserver write access to the apps directory%s' - . ' or disabling the appstore in the config file.', - array('', '')) + . '%sgiving the webserver write access to the apps directory%s' + . ' or disabling the appstore in the config file.', + array('', '')) ); } } @@ -547,7 +423,7 @@ public static function checkServer(\OCP\IConfig $config) { if (!OC_Util::isSetLocaleWorking()) { $errors[] = array( 'error' => $l->t('Setting locale to %s failed', - array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' + array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), 'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') ); @@ -609,7 +485,7 @@ public static function checkServer(\OCP\IConfig $config) { $errors[] = array( 'error' => $l->t('PHP %s or higher is required.', '5.3.3'), 'hint' => $l->t('Please ask your server administrator to update PHP to the latest version.' - . ' Your PHP version is no longer supported by ownCloud and the PHP community.') + . ' Your PHP version is no longer supported by ownCloud and the PHP community.') ); $webServerRestart = true; } @@ -621,7 +497,7 @@ public static function checkServer(\OCP\IConfig $config) { $errors[] = array( 'error' => $l->t('PHP Safe Mode is enabled. ownCloud requires that it is disabled to work properly.'), 'hint' => $l->t('PHP Safe Mode is a deprecated and mostly useless setting that should be disabled. ' - . 'Please ask your server administrator to disable it in php.ini or in your webserver config.') + . 'Please ask your server administrator to disable it in php.ini or in your webserver config.') ); $webServerRestart = true; } @@ -629,7 +505,7 @@ public static function checkServer(\OCP\IConfig $config) { $errors[] = array( 'error' => $l->t('Magic Quotes is enabled. ownCloud requires that it is disabled to work properly.'), 'hint' => $l->t('Magic Quotes is a deprecated and mostly useless setting that should be disabled. ' - . 'Please ask your server administrator to disable it in php.ini or in your webserver config.') + . 'Please ask your server administrator to disable it in php.ini or in your webserver config.') ); $webServerRestart = true; } @@ -683,7 +559,7 @@ public static function checkDatabaseVersion() { $errors[] = array( 'error' => $l->t('Error occurred while checking PostgreSQL version'), 'hint' => $l->t('Please make sure you have PostgreSQL >= 9 or' - . ' check the logs for more information about the error') + . ' check the logs for more information about the error') ); } } @@ -779,7 +655,7 @@ public static function checkDataDirectoryValidity($dataDirectory) { $errors[] = array( 'error' => $l->t('Data directory (%s) is invalid', array($dataDirectory)), 'hint' => $l->t('Please check that the data directory contains a file' . - ' ".ocdata" in its root.') + ' ".ocdata" in its root.') ); } return $errors; @@ -1047,7 +923,7 @@ public static function isHtaccessWorking() { // creating a test file $testFile = OC::$server->getConfig()->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; - if (file_exists($testFile)) {// already running this test, possible recursive call + if (file_exists($testFile)) { // already running this test, possible recursive call return false; } @@ -1184,7 +1060,7 @@ public static function obEnd() { * @deprecated Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead */ public static function generateRandomBytes($length = 30) { - return \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); + return \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); } /** diff --git a/lib/public/files/node.php b/lib/public/files/node.php index a380394095b0..9a9295611777 100644 --- a/lib/public/files/node.php +++ b/lib/public/files/node.php @@ -179,4 +179,11 @@ public function getParent(); * @return string */ public function getName(); + + /** + * Get the root folder of the filesystem + * + * @return \OCP\Files\Folder + */ + public function getRoot(); } diff --git a/lib/public/files/storage.php b/lib/public/files/storage.php index 8f8d7852ee42..99e267e50ddc 100644 --- a/lib/public/files/storage.php +++ b/lib/public/files/storage.php @@ -337,6 +337,11 @@ public function isLocal(); public function instanceOfStorage($class); } -interface IHomeStorage { - +interface IHomeStorage extends Storage { + /** + * Get the user to which the home storage belongs + * + * @return \OCP\IUser + */ + public function getUser(); } diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index b734d1b41617..778a1b75e7de 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -78,6 +78,13 @@ function getTagManager(); */ function getRootFolder(); + /** + * Setup the filesystem for a user + * + * @param string $userId + */ + function setupFilesystem($userId); + /** * Returns a view to ownCloud's files folder * diff --git a/tests/lib/cache/file.php b/tests/lib/cache/file.php index d51322036c87..3c78a796a0b1 100644 --- a/tests/lib/cache/file.php +++ b/tests/lib/cache/file.php @@ -47,14 +47,7 @@ protected function setUp() { // OC_FileProxy::register(new OC_FileProxy_Encryption()); //} - //set up temporary storage - $this->storage = \OC\Files\Filesystem::getStorage('/'); - \OC\Files\Filesystem::clearMounts(); - $storage = new \OC\Files\Storage\Temporary(array()); - \OC\Files\Filesystem::mount($storage,array(),'/'); - $datadir = str_replace('local::', '', $storage->getId()); - $this->datadir = \OC_Config::getValue('cachedirectory', \OC::$SERVERROOT.'/data/cache'); - \OC_Config::setValue('cachedirectory', $datadir); + $this->clearFileSystem(); \OC_User::clearBackends(); \OC_User::useBackend(new \OC_User_Dummy()); @@ -74,11 +67,6 @@ protected function setUp() { protected function tearDown() { \OC_User::setUserId($this->user); - \OC_Config::setValue('cachedirectory', $this->datadir); - - // Restore the original mount point - \OC\Files\Filesystem::clearMounts(); - \OC\Files\Filesystem::mount($this->storage, array(), '/'); parent::tearDown(); } diff --git a/tests/lib/cache/usercache.php b/tests/lib/cache/usercache.php index 3822a714d5a5..3962e2d01520 100644 --- a/tests/lib/cache/usercache.php +++ b/tests/lib/cache/usercache.php @@ -44,13 +44,7 @@ protected function setUp() { //} //set up temporary storage - $this->storage = \OC\Files\Filesystem::getStorage('/'); - \OC\Files\Filesystem::clearMounts(); - $storage = new \OC\Files\Storage\Temporary(array()); - \OC\Files\Filesystem::mount($storage,array(),'/'); - $datadir = str_replace('local::', '', $storage->getId()); - $this->datadir = \OC_Config::getValue('cachedirectory', \OC::$SERVERROOT.'/data/cache'); - \OC_Config::setValue('cachedirectory', $datadir); + $this->clearFileSystem(); \OC_User::clearBackends(); \OC_User::useBackend(new \OC_User_Dummy()); @@ -70,12 +64,6 @@ protected function setUp() { protected function tearDown() { \OC_User::setUserId($this->user); - \OC_Config::setValue('cachedirectory', $this->datadir); - - // Restore the original mount point - \OC\Files\Filesystem::clearMounts(); - \OC\Files\Filesystem::mount($this->storage, array(), '/'); - parent::tearDown(); } } diff --git a/tests/lib/files/cache/updaterlegacy.php b/tests/lib/files/cache/updaterlegacy.php index 7c05800cd6ba..fca56833d4c4 100644 --- a/tests/lib/files/cache/updaterlegacy.php +++ b/tests/lib/files/cache/updaterlegacy.php @@ -34,7 +34,9 @@ class UpdaterLegacy extends \Test\TestCase { private static $user; - protected function setUp() { + private $userBackend; + + public function setUp() { parent::setUp(); // remember files_encryption state @@ -61,10 +63,14 @@ protected function setUp() { self::$user = uniqid(); } - \OC_User::createUser(self::$user, 'password'); + if (!$this->userBackend) { + $this->userBackend = new \OC_User_Dummy(); + $this->userBackend->createUser(self::$user, 'foo'); + } + \OC_User::useBackend($this->userBackend); \OC_User::setUserId(self::$user); - Filesystem::init(self::$user, '/' . self::$user . '/files'); + \OC_Util::setupFS(self::$user); Filesystem::clearMounts(); Filesystem::mount($this->storage, array(), '/' . self::$user . '/files'); diff --git a/tests/lib/files/etagtest.php b/tests/lib/files/etagtest.php index eec24d9f4c66..f92399b7a49c 100644 --- a/tests/lib/files/etagtest.php +++ b/tests/lib/files/etagtest.php @@ -12,9 +12,6 @@ use OCP\Share; class EtagTest extends \Test\TestCase { - private $datadir; - - private $tmpDir; private $uid; @@ -23,9 +20,6 @@ class EtagTest extends \Test\TestCase { */ private $userBackend; - /** @var \OC\Files\Storage\Storage */ - private $originalStorage; - protected function setUp() { parent::setUp(); @@ -34,47 +28,29 @@ protected function setUp() { \OCP\Share::registerBackend('file', 'OC_Share_Backend_File'); \OCP\Share::registerBackend('folder', 'OC_Share_Backend_Folder', 'file'); - $this->datadir = \OC_Config::getValue('datadirectory'); - $this->tmpDir = \OC_Helper::tmpFolder(); - \OC_Config::setValue('datadirectory', $this->tmpDir); - $this->uid = \OC_User::getUser(); - \OC_User::setUserId(null); - $this->userBackend = new \OC_User_Dummy(); \OC_User::useBackend($this->userBackend); - $this->originalStorage = \OC\Files\Filesystem::getStorage('/'); - \OC_Util::tearDownFS(); - } - - protected function tearDown() { - \OC_Config::setValue('datadirectory', $this->datadir); - \OC_User::setUserId($this->uid); - \OC_Util::setupFS($this->uid); - \OC\Files\Filesystem::mount($this->originalStorage, array(), '/'); - - parent::tearDown(); } public function testNewUser() { $user1 = $this->getUniqueID('user_'); $this->userBackend->createUser($user1, ''); - \OC_Util::tearDownFS(); - \OC_User::setUserId($user1); - \OC_Util::setupFS($user1); - Filesystem::mkdir('/folder'); - Filesystem::mkdir('/folder/subfolder'); - Filesystem::file_put_contents('/foo.txt', 'asd'); - Filesystem::file_put_contents('/folder/bar.txt', 'fgh'); - Filesystem::file_put_contents('/folder/subfolder/qwerty.txt', 'jkl'); + Filesystem::mount('\OC\Files\Storage\Temporary', array(), '/' . $user1); + $folder = \OC::$server->getUserFolder($user1); + $folder->newFolder('/folder'); + $folder->newFolder('/folder/subfolder'); + $folder->newFile('/foo.txt')->putContent('asd'); + $folder->newFile('/folder/bar.txt')->putContent('fgh'); + $folder->newFile('/folder/subfolder/qwerty.txt')->putContent('jkl'); $files = array('/foo.txt', '/folder/bar.txt', '/folder/subfolder', '/folder/subfolder/qwerty.txt'); - $originalEtags = $this->getEtags($files); + $originalEtags = $this->getEtags($folder, $files); - $scanner = new \OC\Files\Utils\Scanner($user1, \OC::$server->getDatabaseConnection()); + $scanner = new \OC\Files\Utils\Scanner($user1, \OC::$server->getDatabaseConnection(), \OC::$server->getUserFolder($user1)); $scanner->backgroundScan('/'); - $newEtags = $this->getEtags($files); + $newEtags = $this->getEtags($folder, $files); // loop over array and use assertSame over assertEquals to prevent false positives foreach ($originalEtags as $file => $originalEtag) { $this->assertSame($originalEtag, $newEtags[$file]); @@ -82,13 +58,15 @@ public function testNewUser() { } /** + * @param \OCP\Files\Folder $folder * @param string[] $files + * @return string[] */ - private function getEtags($files) { + private function getEtags($folder, $files) { $etags = array(); foreach ($files as $file) { - $info = Filesystem::getFileInfo($file); - $etags[$file] = $info['etag']; + $node = $folder->get($file); + $etags[$file] = $node->getEtag(); } return $etags; } diff --git a/tests/lib/files/filesystem.php b/tests/lib/files/filesystem.php index 746600c7d15f..80b4d2136a35 100644 --- a/tests/lib/files/filesystem.php +++ b/tests/lib/files/filesystem.php @@ -190,7 +190,7 @@ public function testHooks() { $user = \OC_User::getUser(); } else { $user = uniqid(); - \OC\Files\Filesystem::init($user, '/' . $user . '/files'); + \OC_Util::setupFS($user); } \OC_Hook::clear('OC_Filesystem'); \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); @@ -211,22 +211,6 @@ public function testHooks() { // \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh); } - /** - * Tests that a local storage mount is used when passed user - * does not exist. - */ - public function testLocalMountWhenUserDoesNotExist() { - $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data"); - $userId = uniqid('user_'); - - \OC\Files\Filesystem::initMountPoints($userId); - - $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); - - $this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Local')); - $this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId()); - } - /** * Tests that the home storage is used for the user's mount point */ @@ -235,7 +219,7 @@ public function testHomeMount() { \OC_User::createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + \OC::$server->setupFilesystem($userId); $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); @@ -260,7 +244,7 @@ public function testLegacyHomeMount() { $cache = $localStorage->getCache(); \OC_User::createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + \OC::$server->setupFilesystem($userId); $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); @@ -287,7 +271,7 @@ public function testMountDefaultCacheDir() { \OC_Config::setValue('cache_path', ''); \OC_User::createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + \OC::$server->setupFilesystem($userId); $this->assertEquals( '/' . $userId . '/', @@ -314,7 +298,7 @@ public function testMountExternalCacheDir() { \OC_Config::setValue('cache_path', $cachePath); \OC_User::createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + \OC::$server->setupFilesystem($userId); $this->assertEquals( '/' . $userId . '/cache/', diff --git a/tests/lib/files/node/file.php b/tests/lib/files/node/file.php index 34ec7dee2135..7bd3566c5c54 100644 --- a/tests/lib/files/node/file.php +++ b/tests/lib/files/node/file.php @@ -8,6 +8,7 @@ namespace Test\Files\Node; +use OC\Files\Storage\Loader; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OC\Files\View; @@ -82,7 +83,7 @@ public function testDeleteHooks() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $root->listen('\OC\Files', 'preDelete', $preListener); $root->listen('\OC\Files', 'postDelete', $postListener); @@ -139,7 +140,7 @@ public function testGetContent() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hook = function ($file) { throw new \Exception('Hooks are not supposed to be called'); @@ -262,7 +263,7 @@ public function testFOpenRead() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hook = function ($file) { throw new \Exception('Hooks are not supposed to be called'); @@ -298,7 +299,7 @@ public function testFOpenWrite() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, new $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hooksCalled = 0; $hook = function ($file) use (&$hooksCalled) { @@ -339,7 +340,7 @@ public function testFOpenReadNotPermitted() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hook = function ($file) { throw new \Exception('Hooks are not supposed to be called'); @@ -366,7 +367,7 @@ public function testFOpenReadWriteNoReadPermissions() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hook = function () { throw new \Exception('Hooks are not supposed to be called'); @@ -393,7 +394,7 @@ public function testFOpenReadWriteNoWritePermissions() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, new $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $hook = function () { throw new \Exception('Hooks are not supposed to be called'); diff --git a/tests/lib/files/node/folder.php b/tests/lib/files/node/folder.php index e348e452f6fc..119730207ff2 100644 --- a/tests/lib/files/node/folder.php +++ b/tests/lib/files/node/folder.php @@ -11,6 +11,7 @@ use OC\Files\Cache\Cache; use OC\Files\Mount\Mount; use OC\Files\Node\Node; +use OC\Files\Storage\Loader; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OC\Files\View; @@ -81,7 +82,7 @@ public function testDeleteHooks() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $root->listen('\OC\Files', 'preDelete', $preListener); $root->listen('\OC\Files', 'postDelete', $postListener); @@ -477,7 +478,7 @@ public function testGetById() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, new Loader(), $view)); $root->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); @@ -520,7 +521,7 @@ public function testGetByIdOutsideFolder() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, new Loader(), $view)); $root->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); @@ -558,7 +559,7 @@ public function testGetByIdMultipleStorages() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, new Loader(), $view)); $root->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); diff --git a/tests/lib/files/node/integration.php b/tests/lib/files/node/integration.php index fd777460ec6c..4def8e3ea70f 100644 --- a/tests/lib/files/node/integration.php +++ b/tests/lib/files/node/integration.php @@ -10,6 +10,7 @@ use OC\Files\Cache\Cache; use OC\Files\Node\Root; +use OC\Files\Storage\Loader; use OC\Files\Storage\Temporary; use OC\Files\View; use OC\User\User; @@ -37,7 +38,7 @@ protected function setUp() { parent::setUp(); $this->originalStorage = \OC\Files\Filesystem::getStorage('/'); - \OC\Files\Filesystem::init('', ''); + \OC_Util::setupFS(); \OC\Files\Filesystem::clearMounts(); $manager = \OC\Files\Filesystem::getMountManager(); @@ -51,7 +52,7 @@ protected function setUp() { $user = new User(uniqid('user'), new \OC_User_Dummy); \OC_User::setUserId($user->getUID()); $this->view = new View(); - $this->root = new Root($manager, $this->view, $user); + $this->root = new \OC\Files\Node\Root($manager, new Loader(), $this->view); $storage = new Temporary(array()); $subStorage = new Temporary(array()); $this->storages[] = $storage; diff --git a/tests/lib/files/node/node.php b/tests/lib/files/node/node.php index 80e3ac6f80af..b3dfd6d69e89 100644 --- a/tests/lib/files/node/node.php +++ b/tests/lib/files/node/node.php @@ -8,6 +8,8 @@ namespace Test\Files\Node; +use OC\Files\Storage\Loader; + class Node extends \Test\TestCase { private $user; @@ -282,7 +284,7 @@ public function testTouchHooks() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $root->listen('\OC\Files', 'preTouch', $preListener); $root->listen('\OC\Files', 'postTouch', $postListener); diff --git a/tests/lib/files/node/root.php b/tests/lib/files/node/root.php index fcce7070f5db..5566bcc6e887 100644 --- a/tests/lib/files/node/root.php +++ b/tests/lib/files/node/root.php @@ -8,6 +8,7 @@ namespace Test\Files\Node; +use OC\Files\Storage\Loader; use OCP\Files\NotPermittedException; use OC\Files\Mount\Manager; @@ -29,7 +30,7 @@ public function testGet() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $view->expects($this->once()) ->method('getFileInfo') @@ -65,7 +66,7 @@ public function testGetNotFound() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $view->expects($this->once()) ->method('file_exists') @@ -85,7 +86,7 @@ public function testGetInvalidPath() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $root->get('/../foo'); } @@ -99,7 +100,7 @@ public function testGetNoStorages() { * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMock('\OC\Files\View'); - $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root = new \OC\Files\Node\Root($manager, new Loader(), $view); $root->get('/bar/foo'); } diff --git a/tests/lib/files/utils/scanner.php b/tests/lib/files/utils/scanner.php index f729be81bd7d..74b54fa39106 100644 --- a/tests/lib/files/utils/scanner.php +++ b/tests/lib/files/utils/scanner.php @@ -64,7 +64,8 @@ public function testReuseExistingRoot() { $storage->file_put_contents('foo.txt', 'qwerty'); $storage->file_put_contents('folder/bar.txt', 'qwerty'); - $scanner = new TestScanner('', \OC::$server->getDatabaseConnection()); + $userFolder = $this->getMock('\OCP\Files\Folder'); + $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $userFolder); $scanner->addMount($mount); $scanner->scan(''); @@ -86,7 +87,8 @@ public function testReuseExistingFile() { $storage->file_put_contents('foo.txt', 'qwerty'); $storage->file_put_contents('folder/bar.txt', 'qwerty'); - $scanner = new TestScanner('', \OC::$server->getDatabaseConnection()); + $userFolder = $this->getMock('\OCP\Files\Folder'); + $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $userFolder); $scanner->addMount($mount); $scanner->scan(''); @@ -113,7 +115,8 @@ public function testChangePropagator() { $storage->file_put_contents('foo.txt', 'qwerty'); $storage->file_put_contents('folder/bar.txt', 'qwerty'); - $scanner = new TestScanner('', \OC::$server->getDatabaseConnection()); + $userFolder = $this->getMock('\OCP\Files\Folder'); + $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $userFolder); $originalPropagator = $scanner->getPropagator(); $scanner->setPropagator($propagator); $scanner->addMount($mount); diff --git a/tests/lib/helperstorage.php b/tests/lib/helperstorage.php index 9f3bd8824f7b..075d61663f9a 100644 --- a/tests/lib/helperstorage.php +++ b/tests/lib/helperstorage.php @@ -27,7 +27,7 @@ protected function setUp() { $this->storage = \OC\Files\Filesystem::getStorage('/'); \OC\Files\Filesystem::tearDown(); \OC_User::setUserId($this->user); - \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files'); + \OC_Util::setupFS($this->user); \OC\Files\Filesystem::clearMounts(); $this->storageMock = null; diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 2a6761403f45..b424a0907705 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -32,9 +32,7 @@ protected function setUp() { // this gets called by each test in this test class $this->user = $this->getUniqueID(); \OC_User::setUserId($this->user); - \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files'); - - \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/'); + $this->clearFileSystem(); $this->rootView = new \OC\Files\View(''); $this->rootView->mkdir('/'.$this->user); diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index e6f5ca71dacb..6b715d0e8899 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -38,6 +38,15 @@ protected function getUniqueID($prefix = '', $length = 13) { ); } + /** + * Setup a clean filesystem + */ + protected function clearFileSystem() { + \OC\Files\Filesystem::clearMounts(); + $storage = new \OC\Files\Storage\Temporary(array()); + \OC\Files\Filesystem::mount($storage, array(), '/'); + } + public static function tearDownAfterClass() { $dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');