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 Thomas Müller <[email protected]> |
11
|
|
|
* @author Vincent Petry <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @license AGPL-3.0 |
14
|
|
|
* |
15
|
|
|
* This code is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
17
|
|
|
* as published by the Free Software Foundation. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU Affero General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace OC\Files\Cache\Wrapper; |
30
|
|
|
|
31
|
|
|
use OC\Files\Cache\Cache; |
32
|
|
|
use OC\Files\Search\SearchQuery; |
33
|
|
|
use OCP\Files\Cache\ICacheEntry; |
34
|
|
|
use OCP\Files\Search\ISearchQuery; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Jail to a subdirectory of the wrapped cache |
38
|
|
|
*/ |
39
|
|
|
class CacheJail extends CacheWrapper { |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $root; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \OCP\Files\Cache\ICache $cache |
47
|
|
|
* @param string $root |
48
|
|
|
*/ |
49
|
|
|
public function __construct($cache, $root) { |
50
|
|
|
parent::__construct($cache); |
51
|
|
|
$this->root = $root; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getRoot() { |
55
|
|
|
return $this->root; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getSourcePath($path) { |
59
|
|
|
if ($path === '') { |
60
|
|
|
return $this->getRoot(); |
61
|
|
|
} else { |
62
|
|
|
return $this->getRoot() . '/' . ltrim($path, '/'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $path |
68
|
|
|
* @return null|string the jailed path or null if the path is outside the jail |
69
|
|
|
*/ |
70
|
|
|
protected function getJailedPath($path) { |
71
|
|
|
if ($this->getRoot() === '') { |
72
|
|
|
return $path; |
73
|
|
|
} |
74
|
|
|
$rootLength = strlen($this->getRoot()) + 1; |
75
|
|
|
if ($path === $this->getRoot()) { |
76
|
|
|
return ''; |
77
|
|
|
} else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') { |
78
|
|
|
return substr($path, $rootLength); |
79
|
|
|
} else { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ICacheEntry|array $entry |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function formatCacheEntry($entry) { |
89
|
|
|
if (isset($entry['path'])) { |
90
|
|
|
$entry['path'] = $this->getJailedPath($entry['path']); |
91
|
|
|
} |
92
|
|
|
return $entry; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function filterCacheEntry($entry) { |
96
|
|
|
$rootLength = strlen($this->getRoot()) + 1; |
97
|
|
|
return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get the stored metadata of a file or folder |
102
|
|
|
* |
103
|
|
|
* @param string /int $file |
|
|
|
|
104
|
|
|
* @return ICacheEntry|false |
105
|
|
|
*/ |
106
|
|
|
public function get($file) { |
107
|
|
|
if (is_string($file) or $file == '') { |
108
|
|
|
$file = $this->getSourcePath($file); |
109
|
|
|
} |
110
|
|
|
return parent::get($file); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* insert meta data for a new file or folder |
115
|
|
|
* |
116
|
|
|
* @param string $file |
117
|
|
|
* @param array $data |
118
|
|
|
* |
119
|
|
|
* @return int file id |
120
|
|
|
* @throws \RuntimeException |
121
|
|
|
*/ |
122
|
|
|
public function insert($file, array $data) { |
123
|
|
|
return $this->getCache()->insert($this->getSourcePath($file), $data); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* update the metadata in the cache |
128
|
|
|
* |
129
|
|
|
* @param int $id |
130
|
|
|
* @param array $data |
131
|
|
|
*/ |
132
|
|
|
public function update($id, array $data) { |
133
|
|
|
$this->getCache()->update($id, $data); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* get the file id for a file |
138
|
|
|
* |
139
|
|
|
* @param string $file |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
public function getId($file) { |
143
|
|
|
return $this->getCache()->getId($this->getSourcePath($file)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* get the id of the parent folder of a file |
148
|
|
|
* |
149
|
|
|
* @param string $file |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
public function getParentId($file) { |
153
|
|
|
return $this->getCache()->getParentId($this->getSourcePath($file)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* check if a file is available in the cache |
158
|
|
|
* |
159
|
|
|
* @param string $file |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function inCache($file) { |
163
|
|
|
return $this->getCache()->inCache($this->getSourcePath($file)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* remove a file or folder from the cache |
168
|
|
|
* |
169
|
|
|
* @param string $file |
170
|
|
|
*/ |
171
|
|
|
public function remove($file) { |
172
|
|
|
$this->getCache()->remove($this->getSourcePath($file)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Move a file or folder in the cache |
177
|
|
|
* |
178
|
|
|
* @param string $source |
179
|
|
|
* @param string $target |
180
|
|
|
*/ |
181
|
|
|
public function move($source, $target) { |
182
|
|
|
$this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the storage id and path needed for a move |
187
|
|
|
* |
188
|
|
|
* @param string $path |
189
|
|
|
* @return array [$storageId, $internalPath] |
190
|
|
|
*/ |
191
|
|
|
protected function getMoveInfo($path) { |
192
|
|
|
return [$this->getNumericStorageId(), $this->getSourcePath($path)]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* remove all entries for files that are stored on the storage from the cache |
197
|
|
|
*/ |
198
|
|
|
public function clear() { |
199
|
|
|
$this->getCache()->remove($this->getRoot()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $file |
204
|
|
|
* |
205
|
|
|
* @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
206
|
|
|
*/ |
207
|
|
|
public function getStatus($file) { |
208
|
|
|
return $this->getCache()->getStatus($this->getSourcePath($file)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function formatSearchResults($results) { |
212
|
|
|
$results = array_filter($results, array($this, 'filterCacheEntry')); |
213
|
|
|
$results = array_values($results); |
214
|
|
|
return array_map(array($this, 'formatCacheEntry'), $results); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* search for files matching $pattern |
219
|
|
|
* |
220
|
|
|
* @param string $pattern |
221
|
|
|
* @return array an array of file data |
222
|
|
|
*/ |
223
|
|
|
public function search($pattern) { |
224
|
|
|
$results = $this->getCache()->search($pattern); |
|
|
|
|
225
|
|
|
return $this->formatSearchResults($results); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* search for files by mimetype |
230
|
|
|
* |
231
|
|
|
* @param string $mimetype |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function searchByMime($mimetype) { |
235
|
|
|
$results = $this->getCache()->searchByMime($mimetype); |
|
|
|
|
236
|
|
|
return $this->formatSearchResults($results); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function searchQuery(ISearchQuery $query) { |
240
|
|
|
$simpleQuery = new SearchQuery($query->getSearchOperation(), 0, 0, $query->getOrder(), $query->getUser()); |
241
|
|
|
$results = $this->getCache()->searchQuery($simpleQuery); |
242
|
|
|
$results = $this->formatSearchResults($results); |
243
|
|
|
|
244
|
|
|
$limit = $query->getLimit() === 0 ? NULL : $query->getLimit(); |
245
|
|
|
$results = array_slice($results, $query->getOffset(), $limit); |
246
|
|
|
|
247
|
|
|
return $results; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* update the folder size and the size of all parent folders |
252
|
|
|
* |
253
|
|
|
* @param string|boolean $path |
254
|
|
|
* @param array $data (optional) meta data of the folder |
255
|
|
|
*/ |
256
|
|
|
public function correctFolderSize($path, $data = null, $isBackgroundSize = false) { |
257
|
|
|
if ($this->getCache() instanceof Cache) { |
258
|
|
|
$this->getCache()->correctFolderSize($this->getSourcePath($path), $data, $isBackgroundSize); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* get the size of a folder and set it in the cache |
264
|
|
|
* |
265
|
|
|
* @param string $path |
266
|
|
|
* @param array $entry (optional) meta data of the folder |
267
|
|
|
* @return int |
268
|
|
|
*/ |
269
|
|
|
public function calculateFolderSize($path, $entry = null) { |
270
|
|
|
if ($this->getCache() instanceof Cache) { |
271
|
|
|
return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry); |
272
|
|
|
} else { |
273
|
|
|
return 0; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* get all file ids on the files on the storage |
280
|
|
|
* |
281
|
|
|
* @return int[] |
282
|
|
|
*/ |
283
|
|
|
public function getAll() { |
284
|
|
|
// not supported |
285
|
|
|
return array(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* find a folder in the cache which has not been fully scanned |
290
|
|
|
* |
291
|
|
|
* If multiply incomplete folders are in the cache, the one with the highest id will be returned, |
292
|
|
|
* use the one with the highest id gives the best result with the background scanner, since that is most |
293
|
|
|
* likely the folder where we stopped scanning previously |
294
|
|
|
* |
295
|
|
|
* @return string|bool the path of the folder or false when no folder matched |
296
|
|
|
*/ |
297
|
|
|
public function getIncomplete() { |
298
|
|
|
// not supported |
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* get the path of a file on this storage by it's id |
304
|
|
|
* |
305
|
|
|
* @param int $id |
306
|
|
|
* @return string|null |
307
|
|
|
*/ |
308
|
|
|
public function getPathById($id) { |
309
|
|
|
$path = $this->getCache()->getPathById($id); |
310
|
|
|
if ($path === null) { |
311
|
|
|
return null; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $this->getJailedPath($path); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Move a file or folder in the cache |
319
|
|
|
* |
320
|
|
|
* Note that this should make sure the entries are removed from the source cache |
321
|
|
|
* |
322
|
|
|
* @param \OCP\Files\Cache\ICache $sourceCache |
323
|
|
|
* @param string $sourcePath |
324
|
|
|
* @param string $targetPath |
325
|
|
|
*/ |
326
|
|
|
public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { |
327
|
|
|
if ($sourceCache === $this) { |
|
|
|
|
328
|
|
|
return $this->move($sourcePath, $targetPath); |
329
|
|
|
} |
330
|
|
|
return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath)); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|