2014-11-10 15:00:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Daniel Jagszent <daniel@jagszent.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-11-10 15:00:08 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-11-10 15:00:08 +00:00
|
|
|
namespace OC\Files\Cache\Wrapper;
|
2016-04-15 15:33:02 +00:00
|
|
|
use OC\Files\Cache\Cache;
|
2014-11-10 15:00:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Jail to a subdirectory of the wrapped cache
|
|
|
|
*/
|
|
|
|
class CacheJail extends CacheWrapper {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $root;
|
|
|
|
|
|
|
|
/**
|
2016-01-22 12:01:37 +00:00
|
|
|
* @param \OCP\Files\Cache\ICache $cache
|
2014-11-10 15:00:08 +00:00
|
|
|
* @param string $root
|
|
|
|
*/
|
|
|
|
public function __construct($cache, $root) {
|
|
|
|
parent::__construct($cache);
|
|
|
|
$this->root = $root;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getSourcePath($path) {
|
|
|
|
if ($path === '') {
|
|
|
|
return $this->root;
|
|
|
|
} else {
|
2015-01-09 09:18:32 +00:00
|
|
|
return $this->root . '/' . ltrim($path, '/');
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return null|string the jailed path or null if the path is outside the jail
|
|
|
|
*/
|
|
|
|
protected function getJailedPath($path) {
|
2016-08-05 11:32:26 +00:00
|
|
|
if ($this->root === '') {
|
|
|
|
return $path;
|
|
|
|
}
|
2014-11-10 15:00:08 +00:00
|
|
|
$rootLength = strlen($this->root) + 1;
|
|
|
|
if ($path === $this->root) {
|
|
|
|
return '';
|
|
|
|
} else if (substr($path, 0, $rootLength) === $this->root . '/') {
|
|
|
|
return substr($path, $rootLength);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $entry
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatCacheEntry($entry) {
|
|
|
|
if (isset($entry['path'])) {
|
|
|
|
$entry['path'] = $this->getJailedPath($entry['path']);
|
|
|
|
}
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filterCacheEntry($entry) {
|
|
|
|
$rootLength = strlen($this->root) + 1;
|
|
|
|
return ($entry['path'] === $this->root) or (substr($entry['path'], 0, $rootLength) === $this->root . '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the stored metadata of a file or folder
|
|
|
|
*
|
|
|
|
* @param string /int $file
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function get($file) {
|
|
|
|
if (is_string($file) or $file == '') {
|
|
|
|
$file = $this->getSourcePath($file);
|
|
|
|
}
|
|
|
|
return parent::get($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-02 13:41:14 +00:00
|
|
|
* insert meta data for a new file or folder
|
2014-11-10 15:00:08 +00:00
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return int file id
|
2016-02-02 13:41:14 +00:00
|
|
|
* @throws \RuntimeException
|
2014-11-10 15:00:08 +00:00
|
|
|
*/
|
2016-02-02 13:41:14 +00:00
|
|
|
public function insert($file, array $data) {
|
|
|
|
return $this->cache->insert($this->getSourcePath($file), $data);
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the metadata in the cache
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function update($id, array $data) {
|
2016-01-25 12:20:59 +00:00
|
|
|
$this->cache->update($id, $data);
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id for a file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getId($file) {
|
|
|
|
return $this->cache->getId($this->getSourcePath($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the id of the parent folder of a file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getParentId($file) {
|
|
|
|
if ($file === '') {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return $this->cache->getParentId($this->getSourcePath($file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file is available in the cache
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function inCache($file) {
|
|
|
|
return $this->cache->inCache($this->getSourcePath($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a file or folder from the cache
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public function remove($file) {
|
|
|
|
$this->cache->remove($this->getSourcePath($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a file or folder in the cache
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
public function move($source, $target) {
|
|
|
|
$this->cache->move($this->getSourcePath($source), $this->getSourcePath($target));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove all entries for files that are stored on the storage from the cache
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
$this->cache->remove($this->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
*
|
2015-01-16 18:31:15 +00:00
|
|
|
* @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
|
2014-11-10 15:00:08 +00:00
|
|
|
*/
|
|
|
|
public function getStatus($file) {
|
|
|
|
return $this->cache->getStatus($this->getSourcePath($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function formatSearchResults($results) {
|
|
|
|
$results = array_filter($results, array($this, 'filterCacheEntry'));
|
|
|
|
$results = array_values($results);
|
|
|
|
return array_map(array($this, 'formatCacheEntry'), $results);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files matching $pattern
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @return array an array of file data
|
|
|
|
*/
|
|
|
|
public function search($pattern) {
|
|
|
|
$results = $this->cache->search($pattern);
|
|
|
|
return $this->formatSearchResults($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
|
|
|
* @param string $mimetype
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
|
|
|
$results = $this->cache->searchByMime($mimetype);
|
|
|
|
return $this->formatSearchResults($results);
|
|
|
|
}
|
|
|
|
|
2014-12-12 10:48:42 +00:00
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
|
|
|
* @param string|int $tag name or tag id
|
|
|
|
* @param string $userId owner of the tags
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchByTag($tag, $userId) {
|
|
|
|
$results = $this->cache->searchByTag($tag, $userId);
|
|
|
|
return $this->formatSearchResults($results);
|
|
|
|
}
|
|
|
|
|
2014-11-10 15:00:08 +00:00
|
|
|
/**
|
|
|
|
* update the folder size and the size of all parent folders
|
|
|
|
*
|
|
|
|
* @param string|boolean $path
|
|
|
|
* @param array $data (optional) meta data of the folder
|
|
|
|
*/
|
|
|
|
public function correctFolderSize($path, $data = null) {
|
2016-04-15 15:33:02 +00:00
|
|
|
if ($this->cache instanceof Cache) {
|
|
|
|
$this->cache->correctFolderSize($this->getSourcePath($path), $data);
|
|
|
|
}
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the size of a folder and set it in the cache
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $entry (optional) meta data of the folder
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function calculateFolderSize($path, $entry = null) {
|
2016-04-15 15:33:02 +00:00
|
|
|
if ($this->cache instanceof Cache) {
|
|
|
|
return $this->cache->calculateFolderSize($this->getSourcePath($path), $entry);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all file ids on the files on the storage
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
// not supported
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find a folder in the cache which has not been fully scanned
|
|
|
|
*
|
|
|
|
* If multiply incomplete folders are in the cache, the one with the highest id will be returned,
|
|
|
|
* use the one with the highest id gives the best result with the background scanner, since that is most
|
|
|
|
* likely the folder where we stopped scanning previously
|
|
|
|
*
|
|
|
|
* @return string|bool the path of the folder or false when no folder matched
|
|
|
|
*/
|
|
|
|
public function getIncomplete() {
|
|
|
|
// not supported
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the path of a file on this storage by it's id
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getPathById($id) {
|
|
|
|
$path = $this->cache->getPathById($id);
|
|
|
|
return $this->getJailedPath($path);
|
|
|
|
}
|
2016-04-19 12:03:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a file or folder in the cache
|
|
|
|
*
|
|
|
|
* Note that this should make sure the entries are removed from the source cache
|
|
|
|
*
|
|
|
|
* @param \OCP\Files\Cache\ICache $sourceCache
|
|
|
|
* @param string $sourcePath
|
|
|
|
* @param string $targetPath
|
|
|
|
*/
|
|
|
|
public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
|
|
|
|
if ($sourceCache === $this) {
|
|
|
|
return $this->move($sourcePath, $targetPath);
|
|
|
|
}
|
2016-04-25 07:33:17 +00:00
|
|
|
return $this->cache->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
|
2016-04-19 12:03:39 +00:00
|
|
|
}
|
2014-11-10 15:00:08 +00:00
|
|
|
}
|