Ejemplo n.º 1
0
 function testClearKeepEntriesOutsideJail()
 {
     $file1 = 'foo/foobar';
     $file2 = 'foo/foobar/asd';
     $file3 = 'folder/foobar';
     $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
     $this->sourceCache->put('foo', $data1);
     $this->sourceCache->put($file1, $data1);
     $this->sourceCache->put($file2, $data1);
     $this->sourceCache->put($file3, $data1);
     $this->cache->clear();
     $this->assertFalse($this->cache->inCache('foobar'));
     $this->assertTrue($this->sourceCache->inCache('folder/foobar'));
 }
Ejemplo n.º 2
0
 /**
  * Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
  *
  * @param IStorage $sourceStorage
  * @param string $source
  * @param string $target
  */
 public function renameFromStorage(IStorage $sourceStorage, $source, $target)
 {
     if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
         return;
     }
     $time = time();
     $sourceCache = $sourceStorage->getCache();
     $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);
     if ($sourceUpdater instanceof Updater) {
         $sourceUpdater->correctParentStorageMtime($source);
     }
     $this->correctParentStorageMtime($target);
     $this->updateStorageMTimeOnly($target);
     $sourcePropagator->propagateChange($source, $time);
     $this->propagator->propagateChange($target, $time);
 }
Ejemplo n.º 3
0
 protected function getExistingChildren($path)
 {
     $existingChildren = array();
     if ($this->cache->inCache($path)) {
         $children = $this->cache->getFolderContents($path);
         foreach ($children as $child) {
             $existingChildren[] = $child['name'];
         }
     }
     return $existingChildren;
 }
Ejemplo n.º 4
0
	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']);
		}
	}
Ejemplo n.º 5
0
 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'));
 }
Ejemplo n.º 6
0
 /**
  * walk over any folders that are not fully scanned yet and scan them
  */
 public function backgroundScan()
 {
     if (!$this->cache->inCache('')) {
         $this->runBackgroundScanJob(function () {
             $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG);
         }, '');
     } else {
         $lastPath = null;
         while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) {
             $this->runBackgroundScanJob(function () use($path) {
                 $this->scan($path, self::SCAN_RECURSIVE, self::REUSE_ETAG);
             }, $path);
             // FIXME: this won't proceed with the next item, needs revamping of getIncomplete()
             // to make this possible
             $lastPath = $path;
         }
     }
 }
Ejemplo n.º 7
0
 public function testRepairParentShallow()
 {
     $this->fillTestFolders();
     $this->scanner->scan('');
     $this->assertTrue($this->cache->inCache('folder/bar.txt'));
     $oldFolderId = $this->cache->getId('folder');
     // delete the folder without removing the childs
     $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
     \OC_DB::executeAudited($sql, array($oldFolderId));
     $cachedData = $this->cache->get('folder/bar.txt');
     $this->assertEquals($oldFolderId, $cachedData['parent']);
     $this->assertFalse($this->cache->inCache('folder'));
     $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
     $this->assertTrue($this->cache->inCache('folder'));
     $newFolderId = $this->cache->getId('folder');
     $this->assertNotEquals($oldFolderId, $newFolderId);
     $cachedData = $this->cache->get('folder/bar.txt');
     $this->assertEquals($newFolderId, $cachedData['parent']);
 }
Ejemplo n.º 8
0
	function testMove() {
		$file1 = 'folder';
		$file2 = 'folder/bar';
		$file3 = 'folder/foo';
		$file4 = 'folder/foo/1';
		$file5 = 'folder/foo/2';
		$data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar');
		$folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');

		$this->cache->put($file1, $folderData);
		$this->cache->put($file2, $folderData);
		$this->cache->put($file3, $folderData);
		$this->cache->put($file4, $data);
		$this->cache->put($file5, $data);

		/* simulate a second user with a different storage id but the same folder structure */
		$this->cache2->put($file1, $folderData);
		$this->cache2->put($file2, $folderData);
		$this->cache2->put($file3, $folderData);
		$this->cache2->put($file4, $data);
		$this->cache2->put($file5, $data);

		$this->cache->move('folder/foo', 'folder/foobar');

		$this->assertFalse($this->cache->inCache('folder/foo'));
		$this->assertFalse($this->cache->inCache('folder/foo/1'));
		$this->assertFalse($this->cache->inCache('folder/foo/2'));

		$this->assertTrue($this->cache->inCache('folder/bar'));
		$this->assertTrue($this->cache->inCache('folder/foobar'));
		$this->assertTrue($this->cache->inCache('folder/foobar/1'));
		$this->assertTrue($this->cache->inCache('folder/foobar/2'));

		/* the folder structure of the second user must not change! */
		$this->assertTrue($this->cache2->inCache('folder/bar'));
		$this->assertTrue($this->cache2->inCache('folder/foo'));
		$this->assertTrue($this->cache2->inCache('folder/foo/1'));
		$this->assertTrue($this->cache2->inCache('folder/foo/2'));

		$this->assertFalse($this->cache2->inCache('folder/foobar'));
		$this->assertFalse($this->cache2->inCache('folder/foobar/1'));
		$this->assertFalse($this->cache2->inCache('folder/foobar/2'));
	}
Ejemplo n.º 9
0
 public function testMove()
 {
     $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->rename('foo.txt', 'bar.txt');
     $this->assertFalse($this->cache->inCache('foo.txt'));
     $this->assertTrue($this->cache->inCache('bar.txt'));
     $cachedTarget = $this->cache->get('bar.txt');
     $this->assertEquals($cached['etag'], $cachedTarget['etag']);
     $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
     $this->assertEquals($cached['size'], $cachedTarget['size']);
     $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
 }
Ejemplo n.º 10
0
 public function testRename()
 {
     $textSize = strlen("dummy file data\n");
     $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
     $rootCachedData = $this->cache->get('');
     $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
     $this->assertTrue($this->cache->inCache('foo.txt'));
     $fooCachedData = $this->cache->get('foo.txt');
     $this->assertFalse($this->cache->inCache('bar.txt'));
     Filesystem::rename('foo.txt', 'bar.txt');
     $this->assertFalse($this->cache->inCache('foo.txt'));
     $this->assertTrue($this->cache->inCache('bar.txt'));
     $cachedData = $this->cache->get('bar.txt');
     $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
     $mtime = $cachedData['mtime'];
     $cachedData = $this->cache->get('');
     $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
     $this->assertInternalType('string', $rootCachedData['etag']);
     $this->assertInternalType('string', $cachedData['etag']);
     $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
 }
Ejemplo n.º 11
0
 /**
  * @param string $name
  * @dataProvider escapingProvider
  */
 public function testEscaping($name)
 {
     $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain');
     $this->cache->put($name, $data);
     $this->assertTrue($this->cache->inCache($name));
     $retrievedData = $this->cache->get($name);
     foreach ($data as $key => $value) {
         $this->assertEquals($value, $retrievedData[$key]);
     }
     $this->cache->move($name, $name . 'asd');
     $this->assertFalse($this->cache->inCache($name));
     $this->assertTrue($this->cache->inCache($name . 'asd'));
     $this->cache->remove($name . 'asd');
     $this->assertFalse($this->cache->inCache($name . 'asd'));
     $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
     $this->cache->put($name, $folderData);
     $this->cache->put('other', $folderData);
     $childs = ['asd', 'bar', 'foo', 'sub/folder'];
     $this->cache->put($name . '/sub', $folderData);
     $this->cache->put('other/sub', $folderData);
     foreach ($childs as $child) {
         $this->cache->put($name . '/' . $child, $data);
         $this->cache->put('other/' . $child, $data);
         $this->assertTrue($this->cache->inCache($name . '/' . $child));
     }
     $this->cache->move($name, $name . 'asd');
     foreach ($childs as $child) {
         $this->assertTrue($this->cache->inCache($name . 'asd/' . $child));
         $this->assertTrue($this->cache->inCache('other/' . $child));
     }
     foreach ($childs as $child) {
         $this->cache->remove($name . 'asd/' . $child);
         $this->assertFalse($this->cache->inCache($name . 'asd/' . $child));
         $this->assertTrue($this->cache->inCache('other/' . $child));
     }
 }
Ejemplo n.º 12
0
 /**
  * check if a file is available in the cache
  *
  * @param string $file
  * @return bool
  */
 public function inCache($file)
 {
     if ($file == '') {
         return true;
     }
     return parent::inCache($file);
 }
Ejemplo n.º 13
0
 /**
  * scan all the files and folders in a folder
  *
  * @param string $path
  * @param bool $recursive
  * @param int $reuse
  * @return int the size of the scanned folder or -1 if the size is unknown at this stage
  */
 public function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1)
 {
     if ($reuse === -1) {
         $reuse = $recursive === self::SCAN_SHALLOW ? self::REUSE_ETAG | self::REUSE_SIZE : 0;
     }
     $this->emit('\\OC\\Files\\Cache\\Scanner', 'scanFolder', array($path, $this->storageId));
     $size = 0;
     $childQueue = array();
     $existingChildren = array();
     if ($this->cache->inCache($path)) {
         $children = $this->cache->getFolderContents($path);
         foreach ($children as $child) {
             $existingChildren[] = $child['name'];
         }
     }
     $newChildren = array();
     if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) {
         $exceptionOccurred = false;
         \OC_DB::beginTransaction();
         if (is_resource($dh)) {
             while (($file = readdir($dh)) !== false) {
                 $child = $path ? $path . '/' . $file : $file;
                 if (!Filesystem::isIgnoredDir($file)) {
                     $newChildren[] = $file;
                     try {
                         $data = $this->scanFile($child, $reuse, true);
                         if ($data) {
                             if ($data['size'] === -1) {
                                 if ($recursive === self::SCAN_RECURSIVE) {
                                     $childQueue[] = $child;
                                 } else {
                                     $size = -1;
                                 }
                             } else {
                                 if ($size !== -1) {
                                     $size += $data['size'];
                                 }
                             }
                         }
                     } catch (\Doctrine\DBAL\DBALException $ex) {
                         // might happen if inserting duplicate while a scanning
                         // process is running in parallel
                         // log and ignore
                         \OC_Log::write('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OC_Log::DEBUG);
                         $exceptionOccurred = true;
                     }
                 }
             }
         }
         $removedChildren = \array_diff($existingChildren, $newChildren);
         foreach ($removedChildren as $childName) {
             $child = $path ? $path . '/' . $childName : $childName;
             $this->cache->remove($child);
         }
         \OC_DB::commit();
         if ($exceptionOccurred) {
             // It might happen that the parallel scan process has already
             // inserted mimetypes but those weren't available yet inside the transaction
             // To make sure to have the updated mime types in such cases,
             // we reload them here
             $this->cache->loadMimetypes();
         }
         foreach ($childQueue as $child) {
             $childSize = $this->scanChildren($child, self::SCAN_RECURSIVE, $reuse);
             if ($childSize === -1) {
                 $size = -1;
             } else {
                 $size += $childSize;
             }
         }
         $this->cache->put($path, array('size' => $size));
     }
     $this->emit('\\OC\\Files\\Cache\\Scanner', 'postScanFolder', array($path, $this->storageId));
     return $size;
 }
Ejemplo n.º 14
0
 /**
  * check if a file is available in the cache
  *
  * @param string $file
  * @return bool
  */
 public function inCache($file)
 {
     return $this->cache->inCache($file);
 }