2014-11-10 18:00:08 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Ari Selseng <ari@selseng.net>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Daniel Jagszent <daniel@jagszent.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-11-10 18:00:08 +03:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-11-10 18:00:08 +03:00
|
|
|
namespace OC\Files\Cache\Wrapper;
|
2017-12-04 17:16:39 +03:00
|
|
|
|
2016-04-15 18:33:02 +03:00
|
|
|
use OC\Files\Cache\Cache;
|
2018-02-07 11:35:55 +03:00
|
|
|
use OC\Files\Search\SearchQuery;
|
2016-11-17 13:48:29 +03:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2017-02-02 20:20:08 +03:00
|
|
|
use OCP\Files\Search\ISearchQuery;
|
2014-11-10 18:00:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Jail to a subdirectory of the wrapped cache
|
|
|
|
*/
|
|
|
|
class CacheJail extends CacheWrapper {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $root;
|
|
|
|
|
|
|
|
/**
|
2016-01-22 15:01:37 +03:00
|
|
|
* @param \OCP\Files\Cache\ICache $cache
|
2014-11-10 18:00:08 +03:00
|
|
|
* @param string $root
|
|
|
|
*/
|
|
|
|
public function __construct($cache, $root) {
|
|
|
|
parent::__construct($cache);
|
|
|
|
$this->root = $root;
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:16:39 +03:00
|
|
|
protected function getRoot() {
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:00:08 +03:00
|
|
|
protected function getSourcePath($path) {
|
|
|
|
if ($path === '') {
|
2017-12-04 17:16:39 +03:00
|
|
|
return $this->getRoot();
|
2014-11-10 18:00:08 +03:00
|
|
|
} else {
|
2017-12-04 17:16:39 +03:00
|
|
|
return $this->getRoot() . '/' . ltrim($path, '/');
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return null|string the jailed path or null if the path is outside the jail
|
|
|
|
*/
|
|
|
|
protected function getJailedPath($path) {
|
2017-12-04 17:16:39 +03:00
|
|
|
if ($this->getRoot() === '') {
|
2016-08-05 14:32:26 +03:00
|
|
|
return $path;
|
|
|
|
}
|
2017-12-04 17:16:39 +03:00
|
|
|
$rootLength = strlen($this->getRoot()) + 1;
|
|
|
|
if ($path === $this->getRoot()) {
|
2014-11-10 18:00:08 +03:00
|
|
|
return '';
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (substr($path, 0, $rootLength) === $this->getRoot() . '/') {
|
2014-11-10 18:00:08 +03:00
|
|
|
return substr($path, $rootLength);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-17 13:48:29 +03:00
|
|
|
* @param ICacheEntry|array $entry
|
2014-11-10 18:00:08 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatCacheEntry($entry) {
|
|
|
|
if (isset($entry['path'])) {
|
|
|
|
$entry['path'] = $this->getJailedPath($entry['path']);
|
|
|
|
}
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filterCacheEntry($entry) {
|
2017-12-04 17:16:39 +03:00
|
|
|
$rootLength = strlen($this->getRoot()) + 1;
|
2018-11-05 18:27:43 +03:00
|
|
|
return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/');
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the stored metadata of a file or folder
|
|
|
|
*
|
|
|
|
* @param string /int $file
|
2017-06-15 14:59:06 +03:00
|
|
|
* @return ICacheEntry|false
|
2014-11-10 18:00:08 +03:00
|
|
|
*/
|
|
|
|
public function get($file) {
|
|
|
|
if (is_string($file) or $file == '') {
|
|
|
|
$file = $this->getSourcePath($file);
|
|
|
|
}
|
|
|
|
return parent::get($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-02 16:41:14 +03:00
|
|
|
* insert meta data for a new file or folder
|
2014-11-10 18:00:08 +03:00
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return int file id
|
2016-02-02 16:41:14 +03:00
|
|
|
* @throws \RuntimeException
|
2014-11-10 18:00:08 +03:00
|
|
|
*/
|
2016-02-02 16:41:14 +03:00
|
|
|
public function insert($file, array $data) {
|
2016-11-17 14:47:28 +03:00
|
|
|
return $this->getCache()->insert($this->getSourcePath($file), $data);
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the metadata in the cache
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function update($id, array $data) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$this->getCache()->update($id, $data);
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id for a file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getId($file) {
|
2016-11-17 14:47:28 +03:00
|
|
|
return $this->getCache()->getId($this->getSourcePath($file));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the id of the parent folder of a file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getParentId($file) {
|
2017-01-25 19:03:45 +03:00
|
|
|
return $this->getCache()->getParentId($this->getSourcePath($file));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file is available in the cache
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function inCache($file) {
|
2016-11-17 14:47:28 +03:00
|
|
|
return $this->getCache()->inCache($this->getSourcePath($file));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a file or folder from the cache
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public function remove($file) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$this->getCache()->remove($this->getSourcePath($file));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a file or folder in the cache
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
public function move($source, $target) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:59:06 +03:00
|
|
|
/**
|
|
|
|
* Get the storage id and path needed for a move
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return array [$storageId, $internalPath]
|
|
|
|
*/
|
|
|
|
protected function getMoveInfo($path) {
|
|
|
|
return [$this->getNumericStorageId(), $this->getSourcePath($path)];
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:00:08 +03:00
|
|
|
/**
|
|
|
|
* remove all entries for files that are stored on the storage from the cache
|
|
|
|
*/
|
|
|
|
public function clear() {
|
2017-12-04 17:16:39 +03:00
|
|
|
$this->getCache()->remove($this->getRoot());
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
*
|
2015-01-16 21:31:15 +03:00
|
|
|
* @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
|
2014-11-10 18:00:08 +03:00
|
|
|
*/
|
|
|
|
public function getStatus($file) {
|
2016-11-17 14:47:28 +03:00
|
|
|
return $this->getCache()->getStatus($this->getSourcePath($file));
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function formatSearchResults($results) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$results = array_filter($results, [$this, 'filterCacheEntry']);
|
2014-11-10 18:00:08 +03:00
|
|
|
$results = array_values($results);
|
2020-03-26 11:30:18 +03:00
|
|
|
return array_map([$this, 'formatCacheEntry'], $results);
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files matching $pattern
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @return array an array of file data
|
|
|
|
*/
|
|
|
|
public function search($pattern) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$results = $this->getCache()->search($pattern);
|
2014-11-10 18:00:08 +03:00
|
|
|
return $this->formatSearchResults($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
|
|
|
* @param string $mimetype
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$results = $this->getCache()->searchByMime($mimetype);
|
2014-11-10 18:00:08 +03:00
|
|
|
return $this->formatSearchResults($results);
|
|
|
|
}
|
|
|
|
|
2017-02-02 20:20:08 +03:00
|
|
|
public function searchQuery(ISearchQuery $query) {
|
2018-02-07 11:35:55 +03:00
|
|
|
$simpleQuery = new SearchQuery($query->getSearchOperation(), 0, 0, $query->getOrder(), $query->getUser());
|
|
|
|
$results = $this->getCache()->searchQuery($simpleQuery);
|
|
|
|
$results = $this->formatSearchResults($results);
|
|
|
|
|
2020-03-25 23:53:04 +03:00
|
|
|
$limit = $query->getLimit() === 0 ? null : $query->getLimit();
|
2018-02-07 11:35:55 +03:00
|
|
|
$results = array_slice($results, $query->getOffset(), $limit);
|
|
|
|
|
|
|
|
return $results;
|
2017-02-02 20:20:08 +03:00
|
|
|
}
|
|
|
|
|
2014-11-10 18:00:08 +03: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
|
|
|
|
*/
|
2019-03-02 01:52:58 +03:00
|
|
|
public function correctFolderSize($path, $data = null, $isBackgroundSize = false) {
|
2016-11-17 14:47:28 +03:00
|
|
|
if ($this->getCache() instanceof Cache) {
|
2019-03-02 01:52:58 +03:00
|
|
|
$this->getCache()->correctFolderSize($this->getSourcePath($path), $data, $isBackgroundSize);
|
2016-04-15 18:33:02 +03:00
|
|
|
}
|
2014-11-10 18:00:08 +03: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-11-17 14:47:28 +03:00
|
|
|
if ($this->getCache() instanceof Cache) {
|
|
|
|
return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
|
2016-04-15 18:33:02 +03:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all file ids on the files on the storage
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
// not supported
|
2020-03-26 11:30:18 +03:00
|
|
|
return [];
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2016-11-17 14:47:28 +03:00
|
|
|
$path = $this->getCache()->getPathById($id);
|
2018-02-02 22:08:23 +03:00
|
|
|
if ($path === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:00:08 +03:00
|
|
|
return $this->getJailedPath($path);
|
|
|
|
}
|
2016-04-19 15:03:39 +03: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-11-17 14:47:28 +03:00
|
|
|
return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
|
2016-04-19 15:03:39 +03:00
|
|
|
}
|
2014-11-10 18:00:08 +03:00
|
|
|
}
|