public function setUp() { // remember files_encryption state $this->stateFilesEncryption = \OC_App::isEnabled('files_encryption'); // we want to tests with the encryption app disabled \OC_App::disable('files_encryption'); $this->storage = new \OC\Files\Storage\Temporary(array()); $textData = "dummy file data\n"; $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png'); $this->storage->mkdir('folder'); $this->storage->file_put_contents('foo.txt', $textData); $this->storage->file_put_contents('foo.png', $imgData); $this->storage->file_put_contents('folder/bar.txt', $textData); $this->storage->file_put_contents('folder/bar2.txt', $textData); $this->scanner = $this->storage->getScanner(); $this->scanner->scan(''); $this->cache = $this->storage->getCache(); \OC\Files\Filesystem::tearDown(); if (!self::$user) { self::$user = uniqid(); } \OC_User::createUser(self::$user, 'password'); \OC_User::setUserId(self::$user); \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files'); Filesystem::clearMounts(); Filesystem::mount($this->storage, array(), '/' . self::$user . '/files'); \OC_Hook::clear('OC_Filesystem'); }
protected function setUp() { parent::setUp(); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // prepare user1's dir structure $textData = "dummy file data\n"; $this->view->mkdir('container'); $this->view->mkdir('container/shareddir'); $this->view->mkdir('container/shareddir/subdir'); $this->view->mkdir('container/shareddirrestricted'); $this->view->mkdir('container/shareddirrestricted/subdir'); $this->view->file_put_contents('container/shareddir/textfile.txt', $textData); $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData); list($this->ownerStorage, $internalPath) = $this->view->resolvePath(''); $this->ownerCache = $this->ownerStorage->getCache(); $this->ownerStorage->getScanner()->scan(''); // share "shareddir" with user2 $fileinfo = $this->view->getFileInfo('container/shareddir'); \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31); $fileinfo2 = $this->view->getFileInfo('container/shareddirrestricted'); \OCP\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 7); // login as user2 self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // retrieve the shared storage $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir'); list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted'); $this->sharedCache = $this->sharedStorage->getCache(); $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache(); }
protected function setUp() { parent::setUp(); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // prepare user1's dir structure $textData = "dummy file data\n"; $this->view->mkdir('container'); $this->view->mkdir('container/shareddir'); $this->view->mkdir('container/shareddir/subdir'); $this->view->mkdir('container/shareddirrestricted'); $this->view->mkdir('container/shareddirrestricted/subdir'); $this->view->file_put_contents('container/shareddir/textfile.txt', $textData); $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData); list($this->ownerStorage, $internalPath) = $this->view->resolvePath(''); $this->ownerCache = $this->ownerStorage->getCache(); $this->ownerStorage->getScanner()->scan(''); // share "shareddir" with user2 $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('container/shareddir'); $share = $this->shareManager->newShare(); $share->setNode($node)->setShareType(\OCP\Share::SHARE_TYPE_USER)->setSharedWith(self::TEST_FILES_SHARING_API_USER2)->setSharedBy(self::TEST_FILES_SHARING_API_USER1)->setPermissions(\OCP\Constants::PERMISSION_ALL); $this->shareManager->createShare($share); $node = $rootFolder->get('container/shareddirrestricted'); $share = $this->shareManager->newShare(); $share->setNode($node)->setShareType(\OCP\Share::SHARE_TYPE_USER)->setSharedWith(self::TEST_FILES_SHARING_API_USER2)->setSharedBy(self::TEST_FILES_SHARING_API_USER1)->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); $this->shareManager->createShare($share); // login as user2 self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // retrieve the shared storage $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir'); list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted'); $this->sharedCache = $this->sharedStorage->getCache(); $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache(); }
protected function setUp() { parent::setUp(); $this->storage = new \OC\Files\Storage\Temporary(array()); $textData = "dummy file data\n"; $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png'); $this->storage->mkdir('folder'); $this->storage->file_put_contents('foo.txt', $textData); $this->storage->file_put_contents('foo.png', $imgData); $this->storage->file_put_contents('folder/bar.txt', $textData); $this->storage->file_put_contents('folder/bar2.txt', $textData); $this->scanner = $this->storage->getScanner(); $this->scanner->scan(''); $this->cache = $this->storage->getCache(); if (!self::$user) { self::$user = $this->getUniqueID(); } \OC_User::createUser(self::$user, 'password'); $this->loginAsUser(self::$user); Filesystem::init(self::$user, '/' . self::$user . '/files'); Filesystem::clearMounts(); Filesystem::mount($this->storage, array(), '/' . self::$user . '/files'); \OC_Hook::clear('OC_Filesystem'); }
protected function setUp() { parent::setUp(); $this->loginAsUser(); $this->storage = new Temporary(array()); $this->updater = $this->storage->getUpdater(); $this->cache = $this->storage->getCache(); }
public function setUp() { $this->storage = new Temporary(array()); Filesystem::clearMounts(); Filesystem::mount($this->storage, array(), '/'); $this->view = new View(''); $this->updater = new \OC\Files\Cache\Updater($this->view); $this->cache = $this->storage->getCache(); }
protected function setUp() { parent::setUp(); $this->loginAsUser(); $this->storage = new Temporary(array()); Filesystem::mount($this->storage, array(), '/'); $this->view = new View(''); $this->updater = new \OC\Files\Cache\Updater($this->view); $this->cache = $this->storage->getCache(); }
protected function setUp() { parent::setUp(); $this->originalStorage = Filesystem::getStorage('/'); $this->storage = new Temporary(array()); Filesystem::clearMounts(); Filesystem::mount($this->storage, array(), '/'); $this->view = new View(''); $this->updater = new \OC\Files\Cache\Updater($this->view); $this->cache = $this->storage->getCache(); }
protected function tearDown() { $this->user = null; if ($this->storageMock) { $this->storageMock->getCache()->clear(); $this->storageMock = null; } \OC\Files\Filesystem::tearDown(); \OC\Files\Filesystem::mount($this->storage, array(), '/'); \OC_User::setUserId(''); \OC_User::deleteUser($this->user); \OC::$server->getConfig()->deleteAllUserValues($this->user); parent::tearDown(); }
public function testDontLowerMtime() { $time = time(); $this->view->mkdir('/foo'); $this->view->mkdir('/foo/bar'); $cache = $this->storage->getCache(); $cache->put('', ['mtime' => $time - 50]); $cache->put('foo', ['mtime' => $time - 150]); $cache->put('foo/bar', ['mtime' => $time - 250]); $this->propagator->addChange('/foo/bar/foo'); $this->propagator->propagateChanges($time - 100); $this->assertEquals(50, $time - $cache->get('')['mtime']); $this->assertEquals(100, $time - $cache->get('foo')['mtime']); $this->assertEquals(100, $time - $cache->get('foo/bar')['mtime']); }
public function __construct(\OC\Files\Storage\Storage $storage) { $this->storage = $storage; $this->storageId = $this->storage->getId(); $this->cache = $storage->getCache(); $this->cacheActive = !Config::getSystemValue('filesystem_cache_readonly', false); }
/** * @param \OC\Files\Storage\Storage $storage */ public function __construct(\OC\Files\Storage\Storage $storage) { $this->storage = $storage; $this->propagator = $storage->getPropagator(); $this->scanner = $storage->getScanner(); $this->cache = $storage->getCache(); }
/** * get a cache instance for the storage * * @param string $path * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache * @return \OC\Files\Cache\Cache */ public function getCache($path = '', $storage = null) { if (!$storage) { $storage = $this; } return $this->storage->getCache($path, $storage); }
public function __construct(\OC\Files\Storage\Storage $storage) { $this->storage = $storage; $this->storageId = $this->storage->getId(); $this->cache = $storage->getCache(); $this->permissionsCache = $storage->getPermissionsCache(); }
/** * @param string $internalPath * @param int $time * @return array[] all propagated entries */ public function propagateChange($internalPath, $time) { $cache = $this->storage->getCache($internalPath); $parentId = $cache->getParentId($internalPath); $propagatedEntries = []; while ($parentId !== -1) { $entry = $cache->get($parentId); $propagatedEntries[] = $entry; if (!$entry) { return $propagatedEntries; } $mtime = max($time, $entry['mtime']); $cache->update($parentId, ['mtime' => $mtime, 'etag' => $this->storage->getETag($entry['path'])]); $parentId = $entry['parent']; } return $propagatedEntries; }
/** * @param string $internalPath * @param int $time * @param int $sizeDifference number of bytes the file has grown * @return array[] all propagated entries */ public function propagateChange($internalPath, $time, $sizeDifference = 0) { $cache = $this->storage->getCache($internalPath); $parentId = $cache->getParentId($internalPath); $propagatedEntries = []; while ($parentId !== -1) { $entry = $cache->get($parentId); $propagatedEntries[] = $entry; if (!$entry) { return $propagatedEntries; } $mtime = max($time, $entry['mtime']); if ($entry['size'] === -1) { $newSize = -1; } else { $newSize = $entry['size'] + $sizeDifference; } $cache->update($parentId, ['mtime' => $mtime, 'etag' => $this->storage->getETag($entry['path']), 'size' => $newSize]); $parentId = $entry['parent']; } return $propagatedEntries; }
/** * Test searching by tag for multiple sections of the tree */ function testSearchByTagTree() { $userId = \OC::$server->getUserSession()->getUser()->getUId(); $this->sharedStorage->mkdir('subdir/emptydir'); $this->sharedStorage->mkdir('subdir/emptydir2'); $this->ownerStorage->getScanner()->scan(''); $allIds = array($this->sharedCache->get('')['fileid'], $this->sharedCache->get('bar.txt')['fileid'], $this->sharedCache->get('subdir/another too.txt')['fileid'], $this->sharedCache->get('subdir/not a text file.xml')['fileid'], $this->sharedCache->get('subdir/another.txt')['fileid'], $this->sharedCache->get('subdir/emptydir')['fileid'], $this->sharedCache->get('subdir/emptydir2')['fileid']); $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); foreach ($allIds as $id) { $tagManager->tagAs($id, 'tag1'); } $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); $check = array(array('name' => 'shareddir', 'path' => ''), array('name' => 'bar.txt', 'path' => 'bar.txt'), array('name' => 'another.txt', 'path' => 'subdir/another.txt'), array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), array('name' => 'emptydir', 'path' => 'subdir/emptydir'), array('name' => 'emptydir2', 'path' => 'subdir/emptydir2'), array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml')); $this->verifyFiles($check, $results); $tagManager->delete(array('tag1')); }
public function tearDown() { if (is_null($this->storage)) { return; } $cache = $this->storage->getCache(); $ids = $cache->getAll(); $cache->clear(); $app = new Application(); $container = $app->getContainer(); /** @var StatusMapper $mapper */ $mapper = $container->query('StatusMapper'); foreach ($ids as $id) { $status = new Status($id); $mapper->delete($status); } }
/** * Tests that writing a file using the shared storage will propagate the file * size to the owner's parent folders. */ function testSubFolderSizePropagationToOwnerStorage() { $initialSizes = self::getOwnerDirSizes('files/container/shareddir/subdir'); $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $dataLen = strlen($textData); $this->sharedCache->put('subdir/bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain')); $this->sharedStorage->file_put_contents('subdir/bar.txt', $textData); $this->sharedCache->put('subdir', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain')); // run the propagation code $this->sharedStorage->getWatcher()->checkUpdate('subdir'); $this->sharedStorage->getCache()->correctFolderSize('subdir'); // the owner's parent dirs must have increase size $newSizes = self::getOwnerDirSizes('files/container/shareddir/subdir'); $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']); $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']); $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']); $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']); $this->assertEquals($initialSizes['files/container/shareddir/subdir'] + $dataLen, $newSizes['files/container/shareddir/subdir']); // no more updates $result = $this->sharedStorage->getWatcher()->checkUpdate('subdir'); $this->assertFalse($result); }
/** * Get file info from cache * * If the file is not in cached it will be scanned * If the file has changed on storage the cache will be updated * * @param \OC\Files\Storage\Storage $storage * @param string $internalPath * @param string $relativePath * @return array|bool */ private function getCacheEntry($storage, $internalPath, $relativePath) { $cache = $storage->getCache($internalPath); $data = $cache->get($internalPath); $watcher = $storage->getWatcher($internalPath); try { // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data if (!$data || $data['size'] === -1) { $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); if (!$storage->file_exists($internalPath)) { $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); return false; } $scanner = $storage->getScanner($internalPath); $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); $data = $cache->get($internalPath); $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); } else { if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); $watcher->update($internalPath, $data); $storage->getPropagator()->propagateChange($internalPath, time()); $data = $cache->get($internalPath); $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); } } } catch (LockedException $e) { // if the file is locked we just use the old cache info } return $data; }
/** * Test searching by mime type */ function testSearchByMime() { $results = $this->sharedStorage->getCache()->searchByMime('text'); $check = array(array('name' => 'bar.txt', 'path' => 'bar.txt'), array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), array('name' => 'another.txt', 'path' => 'subdir/another.txt')); $this->verifyFiles($check, $results); }
/** * get the numeric storage id * * @param \OC\Files\Storage\Storage $storage * @return int */ function getNumericId($storage) { $storageId = $storage->getId(); if (!isset($this->numericIds[$storageId])) { $cache = $storage->getCache(); $this->numericIds[$storageId] = $cache->getNumericStorageId(); } return $this->numericIds[$storageId]; }
/** * Rename a file or folder in the cache and update the size, etag and mtime of the parent folders * * @param \OC\Files\Storage\Storage $sourceStorage * @param string $source * @param string $target */ public function renameFromStorage(\OC\Files\Storage\Storage $sourceStorage, $source, $target) { if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) { return; } $time = time(); $sourceCache = $sourceStorage->getCache($source); $sourceUpdater = $sourceStorage->getUpdater(); $sourcePropagator = $sourceStorage->getPropagator(); if ($sourceCache->inCache($source)) { if ($this->cache->inCache($target)) { $this->cache->remove($target); } if ($sourceStorage === $this->storage) { $this->cache->move($source, $target); } else { $this->cache->moveFromCache($sourceCache, $source, $target); } } if (pathinfo($source, PATHINFO_EXTENSION) !== pathinfo($target, PATHINFO_EXTENSION)) { // handle mime type change $mimeType = $this->storage->getMimeType($target); $fileId = $this->cache->getId($target); $this->cache->update($fileId, ['mimetype' => $mimeType]); } $sourceCache->correctFolderSize($source); $this->cache->correctFolderSize($target); $sourceUpdater->correctParentStorageMtime($source); $this->correctParentStorageMtime($target); $this->updateStorageMTimeOnly($target); $sourcePropagator->propagateChange($source, $time); $this->propagator->propagateChange($target, $time); }
/** * update the storage_mtime of the parent * * @param \OC\Files\Storage\Storage $storage * @param string $internalPath */ private function correctParentStorageMtime($storage, $internalPath) { $cache = $storage->getCache(); $parentId = $cache->getParentId($internalPath); $parent = dirname($internalPath); if ($parentId != -1) { $cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent))); } }
/** * get a cache instance for the storage * * @param string $path * @return \OC\Files\Cache\Cache */ public function getCache($path = '') { return $this->storage->getCache($path); }