Completed
Push — master ( ad1d4d...b4a221 )
by Morris
19:26 queued 04:04
created

CacheJail::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Daniel Jagszent <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Vincent Petry <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC\Files\Cache\Wrapper;
29
use OC\Files\Cache\Cache;
30
use OCP\Files\Cache\ICacheEntry;
31
use OCP\Files\Search\ISearchQuery;
32
33
/**
34
 * Jail to a subdirectory of the wrapped cache
35
 */
36
class CacheJail extends CacheWrapper {
37
	/**
38
	 * @var string
39
	 */
40
	protected $root;
41
42
	/**
43
	 * @param \OCP\Files\Cache\ICache $cache
44
	 * @param string $root
45
	 */
46
	public function __construct($cache, $root) {
47
		parent::__construct($cache);
48
		$this->root = $root;
49
	}
50
51
	protected function getSourcePath($path) {
52
		if ($path === '') {
53
			return $this->root;
54
		} else {
55
			return $this->root . '/' . ltrim($path, '/');
56
		}
57
	}
58
59
	/**
60
	 * @param string $path
61
	 * @return null|string the jailed path or null if the path is outside the jail
62
	 */
63
	protected function getJailedPath($path) {
64
		if ($this->root === '') {
65
			return $path;
66
		}
67
		$rootLength = strlen($this->root) + 1;
68
		if ($path === $this->root) {
69
			return '';
70
		} else if (substr($path, 0, $rootLength) === $this->root . '/') {
71
			return substr($path, $rootLength);
72
		} else {
73
			return null;
74
		}
75
	}
76
77
	/**
78
	 * @param ICacheEntry|array $entry
79
	 * @return array
80
	 */
81
	protected function formatCacheEntry($entry) {
82
		if (isset($entry['path'])) {
83
			$entry['path'] = $this->getJailedPath($entry['path']);
84
		}
85
		return $entry;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $entry; of type OCP\Files\Cache\ICacheEntry|array is incompatible with the return type of the parent method OC\Files\Cache\Wrapper\C...apper::formatCacheEntry of type OCP\Files\Cache\ICacheEntry as it can also be of type array which is not included in this return type.
Loading history...
86
	}
87
88
	protected function filterCacheEntry($entry) {
89
		$rootLength = strlen($this->root) + 1;
90
		return ($entry['path'] === $this->root) or (substr($entry['path'], 0, $rootLength) === $this->root . '/');
91
	}
92
93
	/**
94
	 * get the stored metadata of a file or folder
95
	 *
96
	 * @param string /int $file
97
	 * @return ICacheEntry|false
98
	 */
99
	public function get($file) {
100
		if (is_string($file) or $file == '') {
101
			$file = $this->getSourcePath($file);
102
		}
103
		return parent::get($file);
104
	}
105
106
	/**
107
	 * insert meta data for a new file or folder
108
	 *
109
	 * @param string $file
110
	 * @param array $data
111
	 *
112
	 * @return int file id
113
	 * @throws \RuntimeException
114
	 */
115
	public function insert($file, array $data) {
116
		return $this->getCache()->insert($this->getSourcePath($file), $data);
117
	}
118
119
	/**
120
	 * update the metadata in the cache
121
	 *
122
	 * @param int $id
123
	 * @param array $data
124
	 */
125
	public function update($id, array $data) {
126
		$this->getCache()->update($id, $data);
127
	}
128
129
	/**
130
	 * get the file id for a file
131
	 *
132
	 * @param string $file
133
	 * @return int
134
	 */
135
	public function getId($file) {
136
		return $this->getCache()->getId($this->getSourcePath($file));
137
	}
138
139
	/**
140
	 * get the id of the parent folder of a file
141
	 *
142
	 * @param string $file
143
	 * @return int
144
	 */
145
	public function getParentId($file) {
146
		return $this->getCache()->getParentId($this->getSourcePath($file));
147
	}
148
149
	/**
150
	 * check if a file is available in the cache
151
	 *
152
	 * @param string $file
153
	 * @return bool
154
	 */
155
	public function inCache($file) {
156
		return $this->getCache()->inCache($this->getSourcePath($file));
157
	}
158
159
	/**
160
	 * remove a file or folder from the cache
161
	 *
162
	 * @param string $file
163
	 */
164
	public function remove($file) {
165
		$this->getCache()->remove($this->getSourcePath($file));
166
	}
167
168
	/**
169
	 * Move a file or folder in the cache
170
	 *
171
	 * @param string $source
172
	 * @param string $target
173
	 */
174
	public function move($source, $target) {
175
		$this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
176
	}
177
178
	/**
179
	 * Get the storage id and path needed for a move
180
	 *
181
	 * @param string $path
182
	 * @return array [$storageId, $internalPath]
183
	 */
184
	protected function getMoveInfo($path) {
185
		return [$this->getNumericStorageId(), $this->getSourcePath($path)];
186
	}
187
188
	/**
189
	 * remove all entries for files that are stored on the storage from the cache
190
	 */
191
	public function clear() {
192
		$this->getCache()->remove($this->root);
193
	}
194
195
	/**
196
	 * @param string $file
197
	 *
198
	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
199
	 */
200
	public function getStatus($file) {
201
		return $this->getCache()->getStatus($this->getSourcePath($file));
202
	}
203
204
	private function formatSearchResults($results) {
205
		$results = array_filter($results, array($this, 'filterCacheEntry'));
206
		$results = array_values($results);
207
		return array_map(array($this, 'formatCacheEntry'), $results);
208
	}
209
210
	/**
211
	 * search for files matching $pattern
212
	 *
213
	 * @param string $pattern
214
	 * @return array an array of file data
215
	 */
216
	public function search($pattern) {
217
		$results = $this->getCache()->search($pattern);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Files\Cache\ICache::search() has been deprecated with message: 9.0.0 due to lack of pagination, not all backends might implement this

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
218
		return $this->formatSearchResults($results);
219
	}
220
221
	/**
222
	 * search for files by mimetype
223
	 *
224
	 * @param string $mimetype
225
	 * @return array
226
	 */
227
	public function searchByMime($mimetype) {
228
		$results = $this->getCache()->searchByMime($mimetype);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Files\Cache\ICache::searchByMime() has been deprecated with message: 9.0.0 due to lack of pagination, not all backends might implement this

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
229
		return $this->formatSearchResults($results);
230
	}
231
232
	public function searchQuery(ISearchQuery $query) {
233
		$results = $this->getCache()->searchQuery($query);
234
		return $this->formatSearchResults($results);
235
	}
236
237
	/**
238
	 * search for files by mimetype
239
	 *
240
	 * @param string|int $tag name or tag id
241
	 * @param string $userId owner of the tags
242
	 * @return array
243
	 */
244
	public function searchByTag($tag, $userId) {
245
		$results = $this->getCache()->searchByTag($tag, $userId);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Files\Cache\ICache::searchByTag() has been deprecated with message: 9.0.0 due to lack of pagination, not all backends might implement this

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
246
		return $this->formatSearchResults($results);
247
	}
248
249
	/**
250
	 * update the folder size and the size of all parent folders
251
	 *
252
	 * @param string|boolean $path
253
	 * @param array $data (optional) meta data of the folder
254
	 */
255
	public function correctFolderSize($path, $data = null) {
256
		if ($this->getCache() instanceof Cache) {
257
			$this->getCache()->correctFolderSize($this->getSourcePath($path), $data);
258
		}
259
	}
260
261
	/**
262
	 * get the size of a folder and set it in the cache
263
	 *
264
	 * @param string $path
265
	 * @param array $entry (optional) meta data of the folder
266
	 * @return int
267
	 */
268 View Code Duplication
	public function calculateFolderSize($path, $entry = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
		if ($this->getCache() instanceof Cache) {
270
			return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
271
		} else {
272
			return 0;
273
		}
274
275
	}
276
277
	/**
278
	 * get all file ids on the files on the storage
279
	 *
280
	 * @return int[]
281
	 */
282
	public function getAll() {
283
		// not supported
284
		return array();
285
	}
286
287
	/**
288
	 * find a folder in the cache which has not been fully scanned
289
	 *
290
	 * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
291
	 * use the one with the highest id gives the best result with the background scanner, since that is most
292
	 * likely the folder where we stopped scanning previously
293
	 *
294
	 * @return string|bool the path of the folder or false when no folder matched
295
	 */
296
	public function getIncomplete() {
297
		// not supported
298
		return false;
299
	}
300
301
	/**
302
	 * get the path of a file on this storage by it's id
303
	 *
304
	 * @param int $id
305
	 * @return string|null
306
	 */
307
	public function getPathById($id) {
308
		$path = $this->getCache()->getPathById($id);
309
		return $this->getJailedPath($path);
310
	}
311
312
	/**
313
	 * Move a file or folder in the cache
314
	 *
315
	 * Note that this should make sure the entries are removed from the source cache
316
	 *
317
	 * @param \OCP\Files\Cache\ICache $sourceCache
318
	 * @param string $sourcePath
319
	 * @param string $targetPath
320
	 */
321
	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
322
		if ($sourceCache === $this) {
323
			return $this->move($sourcePath, $targetPath);
324
		}
325
		return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
326
	}
327
}
328