/** * @param \OC\Files\Storage\Storage|string $storage * @throws \RuntimeException */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } $this->storageId = self::adjustStorageId($this->storageId); $sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; $result = \OC_DB::executeAudited($sql, array($this->storageId)); if ($row = $result->fetchRow()) { $this->numericId = $row['numeric_id']; } else { $connection = \OC_DB::getConnection(); if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId])) { $this->numericId = \OC_DB::insertid('*PREFIX*storages'); } else { $result = \OC_DB::executeAudited($sql, array($this->storageId)); if ($row = $result->fetchRow()) { $this->numericId = $row['numeric_id']; } else { throw new \RuntimeException('Storage could neither be inserted nor be selected from the database'); } } } }
/** * @param \OC\Files\Storage\Storage|string $storage */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } }
/** * remove deleted files in $path from the cache * * @param string $path */ public function cleanFolder($path) { $cachedContent = $this->cache->getFolderContents($path); foreach ($cachedContent as $entry) { if (!$this->storage->file_exists($entry['path'])) { $this->cache->remove($entry['path']); } } }
function testBackgroundScan() { $this->fillTestFolders(); $this->storage->mkdir('folder2'); $this->storage->file_put_contents('folder2/bar.txt', 'foobar'); $this->assertEquals([], $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW), 'Asserting that no error occurred while scan(SCAN_SHALLOW)'); $this->scanner->backgroundScan(); $this->assertTrue(true, 'Asserting that no error occurred while backgroundScan()'); }
protected function setUp() { parent::setUp(); $this->remoteUser = $this->getUniqueID('remoteuser'); $this->storage = $this->getMockBuilder('\\OCA\\Files_Sharing\\External\\Storage')->disableOriginalConstructor()->getMock(); $this->storage->expects($this->any())->method('getId')->will($this->returnValue('dummystorage::')); $this->cache = new \OCA\Files_Sharing\External\Cache($this->storage, 'http://example.com/owncloud', $this->remoteUser); $this->cache->put('test.txt', array('mimetype' => 'text/plain', 'size' => 5, 'mtime' => 123)); }
/** * Test that the permissions of shared directory are returned correctly */ function testGetPermissions() { $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir'); $this->assertEquals(31, $sharedDirPerms); $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir/textfile.txt'); $this->assertEquals(31, $sharedDirPerms); $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted'); $this->assertEquals(7, $sharedDirRestrictedPerms); $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted/textfile.txt'); $this->assertEquals(7, $sharedDirRestrictedPerms); }
/** * @param \OC\Files\Storage\Storage|string $storage */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } if (strlen($this->storageId) > 64) { $this->storageId = md5($this->storageId); } $this->storageCache = new Storage($storage); }
/** * Returns a storage mock that returns the given value as * free space * * @param int $freeSpace free space value * @return \OC\Files\Storage\Storage */ private function getStorageMock($freeSpace = 12) { $this->storageMock = $this->getMock( '\OC\Files\Storage\Temporary', array('free_space'), array('') ); $this->storageMock->expects($this->once()) ->method('free_space') ->will($this->returnValue(12)); return $this->storageMock; }
/** * @param \OC\Files\Storage\Storage|string $storage */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } if (strlen($this->storageId) > 64) { $this->storageId = md5($this->storageId); } $this->storageCache = new Storage($storage); $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); }
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 tearDown() { \OC_User::setUserId($this->user); foreach ($this->storages as $storage) { $cache = $storage->getCache(); $ids = $cache->getAll(); $cache->clear(); } if ($this->tempStorage && !\OC_Util::runningOnWindows()) { system('rm -rf ' . escapeshellarg($this->tempStorage->getDataDir())); } $this->logout(); parent::tearDown(); }
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 tearDown() { \OC_User::setUserId($this->user); foreach ($this->storages as $storage) { $cache = $storage->getCache(); $ids = $cache->getAll(); $cache->clear(); } if ($this->tempStorage && !\OC_Util::runningOnWindows()) { system('rm -rf ' . escapeshellarg($this->tempStorage->getDataDir())); } \OC\Files\Filesystem::clearMounts(); \OC\Files\Filesystem::mount($this->originalStorage, array(), '/'); 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']); }
/** * Commit the active propagation batch */ public function commitBatch() { if (!$this->inBatch) { throw new \BadMethodCallException('Not in batch'); } $this->inBatch = false; $this->connection->beginTransaction(); $query = $this->connection->getQueryBuilder(); $storageId = (int) $this->storage->getStorageCache()->getNumericId(); $query->update('filecache')->set('mtime', $query->createFunction('GREATEST(`mtime`, ' . $query->createParameter('time') . ')'))->set('etag', $query->expr()->literal(uniqid()))->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash'))); $sizeQuery = $this->connection->getQueryBuilder(); $sizeQuery->update('filecache')->set('size', $sizeQuery->createFunction('`size` + ' . $sizeQuery->createParameter('size')))->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT)))->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash')))->andWhere($sizeQuery->expr()->gt('size', $sizeQuery->expr()->literal(-1, IQueryBuilder::PARAM_INT))); foreach ($this->batch as $item) { $query->setParameter('time', $item['time'], IQueryBuilder::PARAM_INT); $query->setParameter('hash', $item['hash']); $query->execute(); if ($item['size']) { $sizeQuery->setParameter('size', $item['size'], IQueryBuilder::PARAM_INT); $sizeQuery->setParameter('hash', $item['hash']); $sizeQuery->execute(); } } $this->batch = []; $this->connection->commit(); }
public function stream_open($path, $mode, $options, &$opened_path) { $this->loadContext('ocencryption'); $this->position = 0; $this->cache = ''; $this->writeFlag = false; $this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed); if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+' || $mode === 'r+' || $mode === 'rb+') { $this->readOnly = false; } else { $this->readOnly = true; } $sharePath = $this->fullPath; if (!$this->storage->file_exists($this->internalPath)) { $sharePath = dirname($sharePath); } $accessList = $this->file->getAccessList($sharePath); $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList); if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') { // We're writing a new file so start write counter with 0 bytes $this->unencryptedSize = 0; $this->writeHeader(); $this->headerSize = $this->util->getHeaderSize(); $this->size = $this->headerSize; } else { $this->skipHeader(); } return true; }
/** * update the storage_mtime of the direct parent in the cache to the mtime from the storage * * @param string $internalPath */ private function correctParentStorageMtime($internalPath) { $parentId = $this->cache->getParentId($internalPath); $parent = dirname($internalPath); if ($parentId != -1) { $this->cache->update($parentId, array('storage_mtime' => $this->storage->filemtime($parent))); } }
/** * @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; }
public function testScanRemovedFile() { $this->fillTestFolders(); $this->scanner->scan(''); $this->assertTrue($this->cache->inCache('folder/bar.txt')); $this->storage->unlink('folder/bar.txt'); $this->scanner->scanFile('folder/bar.txt'); $this->assertFalse($this->cache->inCache('folder/bar.txt')); }
/** * @param \OC\Files\Storage\Storage|string $storage */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } $this->storageId = self::adjustStorageId($this->storageId); $sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'; $result = \OC_DB::executeAudited($sql, array($this->storageId)); if ($row = $result->fetchRow()) { $this->numericId = $row['numeric_id']; } else { $sql = 'INSERT INTO `*PREFIX*storages` (`id`) VALUES(?)'; \OC_DB::executeAudited($sql, array($this->storageId)); $this->numericId = \OC_DB::insertid('*PREFIX*storages'); } }
/** * Adds the test file to the filesystem * * @param string $fileName name of the file to create * @param string $fileContent path to file to use for test * * @return string */ protected function prepareTestFile($fileName, $fileContent) { $imgData = file_get_contents($fileContent); $imgPath = '/' . $this->userId . '/files/' . $fileName; $this->rootView->file_put_contents($imgPath, $imgData); $scanner = $this->storage->getScanner(); $scanner->scan(''); return $imgPath; }
public function testMoveFolderCrossStorage() { $storage2 = new Temporary(array()); $cache2 = $storage2->getCache(); Filesystem::mount($storage2, array(), '/bar'); $this->storage->mkdir('foo'); $this->storage->mkdir('foo/bar'); $this->storage->file_put_contents('foo/foo.txt', 'qwerty'); $this->storage->file_put_contents('foo/bar.txt', 'foo'); $this->storage->file_put_contents('foo/bar/bar.txt', 'qwertyuiop'); $this->storage->getScanner()->scan(''); $this->assertTrue($this->cache->inCache('foo')); $this->assertTrue($this->cache->inCache('foo/foo.txt')); $this->assertTrue($this->cache->inCache('foo/bar.txt')); $this->assertTrue($this->cache->inCache('foo/bar')); $this->assertTrue($this->cache->inCache('foo/bar/bar.txt')); $cached = []; $cached[] = $this->cache->get('foo'); $cached[] = $this->cache->get('foo/foo.txt'); $cached[] = $this->cache->get('foo/bar.txt'); $cached[] = $this->cache->get('foo/bar'); $cached[] = $this->cache->get('foo/bar/bar.txt'); // add extension to trigger the possible mimetype change $this->view->rename('/foo', '/bar/foo.b'); $this->assertFalse($this->cache->inCache('foo')); $this->assertFalse($this->cache->inCache('foo/foo.txt')); $this->assertFalse($this->cache->inCache('foo/bar.txt')); $this->assertFalse($this->cache->inCache('foo/bar')); $this->assertFalse($this->cache->inCache('foo/bar/bar.txt')); $this->assertTrue($cache2->inCache('foo.b')); $this->assertTrue($cache2->inCache('foo.b/foo.txt')); $this->assertTrue($cache2->inCache('foo.b/bar.txt')); $this->assertTrue($cache2->inCache('foo.b/bar')); $this->assertTrue($cache2->inCache('foo.b/bar/bar.txt')); $cachedTarget = []; $cachedTarget[] = $cache2->get('foo.b'); $cachedTarget[] = $cache2->get('foo.b/foo.txt'); $cachedTarget[] = $cache2->get('foo.b/bar.txt'); $cachedTarget[] = $cache2->get('foo.b/bar'); $cachedTarget[] = $cache2->get('foo.b/bar/bar.txt'); foreach ($cached as $i => $old) { $new = $cachedTarget[$i]; $this->assertEquals($old['mtime'], $new['mtime']); $this->assertEquals($old['size'], $new['size']); $this->assertEquals($old['etag'], $new['etag']); $this->assertEquals($old['fileid'], $new['fileid']); $this->assertEquals($old['mimetype'], $new['mimetype']); } }
/** * @param \OC\Files\Storage\Storage|string $storage */ public function __construct($storage) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } if (strlen($this->storageId) > 64) { $this->storageId = md5($this->storageId); } $query = \OC_DB::prepare('SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?'); $result = $query->execute(array($this->storageId)); if ($row = $result->fetchRow()) { $this->numericId = $row['numeric_id']; } else { $query = \OC_DB::prepare('INSERT INTO `*PREFIX*storages`(`id`) VALUES(?)'); $query->execute(array($this->storageId)); $this->numericId = \OC_DB::insertid('*PREFIX*storages'); } }
/** * @param string $internalPath * @param int $time * @param int $sizeDifference number of bytes the file has grown */ public function propagateChange($internalPath, $time, $sizeDifference = 0) { $storageId = (int) $this->storage->getStorageCache()->getNumericId(); $parents = $this->getParents($internalPath); $parentHashes = array_map('md5', $parents); $etag = uniqid(); // since we give all folders the same etag we don't ask the storage for the etag $builder = $this->connection->getQueryBuilder(); $hashParams = array_map(function ($hash) use($builder) { return $builder->expr()->literal($hash); }, $parentHashes); $builder->update('filecache')->set('mtime', $builder->createFunction('GREATEST(`mtime`, ' . $builder->createNamedParameter($time) . ')'))->set('etag', $builder->createNamedParameter($etag, IQueryBuilder::PARAM_STR))->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))->andWhere($builder->expr()->in('path_hash', $hashParams)); $builder->execute(); if ($sizeDifference !== 0) { // we need to do size separably so we can ignore entries with uncalculated size $builder = $this->connection->getQueryBuilder(); $builder->update('filecache')->set('size', $builder->createFunction('`size` + ' . $builder->createNamedParameter($sizeDifference)))->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))->andWhere($builder->expr()->in('path_hash', $hashParams))->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT))); } $builder->execute(); }
/** * @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; }
/** * Get the children from the storage * * @param string $folder * @return string[] */ protected function getNewChildren($folder) { $children = array(); if ($dh = $this->storage->opendir($folder)) { if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { $children[] = $file; } } } } return $children; }
/** * @param \OC\Files\Storage\Storage|string $storage * @param bool $isAvailable * @throws \RuntimeException */ public function __construct($storage, $isAvailable = true) { if ($storage instanceof \OC\Files\Storage\Storage) { $this->storageId = $storage->getId(); } else { $this->storageId = $storage; } $this->storageId = self::adjustStorageId($this->storageId); if ($row = self::getStorageById($this->storageId)) { $this->numericId = $row['numeric_id']; } else { $connection = \OC_DB::getConnection(); if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $isAvailable])) { $this->numericId = \OC_DB::insertid('*PREFIX*storages'); } else { if ($row = self::getStorageById($this->storageId)) { $this->numericId = $row['numeric_id']; } else { throw new \RuntimeException('Storage could neither be inserted nor be selected from the database'); } } } }
public function testMoveDisabled() { $this->storage->file_put_contents('foo.txt', 'qwerty'); $this->updater->update('foo.txt'); $this->assertTrue($this->cache->inCache('foo.txt')); $this->assertFalse($this->cache->inCache('bar.txt')); $cached = $this->cache->get('foo.txt'); $this->storage->rename('foo.txt', 'bar.txt'); $this->assertTrue($this->cache->inCache('foo.txt')); $this->assertFalse($this->cache->inCache('bar.txt')); $this->updater->disable(); $this->updater->rename('foo.txt', 'bar.txt'); $this->assertTrue($this->cache->inCache('foo.txt')); $this->assertFalse($this->cache->inCache('bar.txt')); }
/** * 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); } }