Completed
Push — stable13 ( 204466...b89996 )
by Roeland
14:34
created
lib/private/Files/Cache/Wrapper/CacheJail.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 		if ($path === '') {
60 60
 			return $this->getRoot();
61 61
 		} else {
62
-			return $this->getRoot() . '/' . ltrim($path, '/');
62
+			return $this->getRoot().'/'.ltrim($path, '/');
63 63
 		}
64 64
 	}
65 65
 
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 		$rootLength = strlen($this->getRoot()) + 1;
75 75
 		if ($path === $this->getRoot()) {
76 76
 			return '';
77
-		} else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') {
77
+		} else if (substr($path, 0, $rootLength) === $this->getRoot().'/') {
78 78
 			return substr($path, $rootLength);
79 79
 		} else {
80 80
 			return null;
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
 
95 95
 	protected function filterCacheEntry($entry) {
96 96
 		$rootLength = strlen($this->getRoot()) + 1;
97
-		return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/');
97
+		return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot().'/');
98 98
 	}
99 99
 
100 100
 	/**
Please login to merge, or discard this patch.
Indentation   +298 added lines, -298 removed lines patch added patch discarded remove patch
@@ -36,302 +36,302 @@
 block discarded – undo
36 36
  * Jail to a subdirectory of the wrapped cache
37 37
  */
38 38
 class CacheJail extends CacheWrapper {
39
-	/**
40
-	 * @var string
41
-	 */
42
-	protected $root;
43
-
44
-	/**
45
-	 * @param \OCP\Files\Cache\ICache $cache
46
-	 * @param string $root
47
-	 */
48
-	public function __construct($cache, $root) {
49
-		parent::__construct($cache);
50
-		$this->root = $root;
51
-	}
52
-
53
-	protected function getRoot() {
54
-		return $this->root;
55
-	}
56
-
57
-	protected function getSourcePath($path) {
58
-		if ($path === '') {
59
-			return $this->getRoot();
60
-		} else {
61
-			return $this->getRoot() . '/' . ltrim($path, '/');
62
-		}
63
-	}
64
-
65
-	/**
66
-	 * @param string $path
67
-	 * @return null|string the jailed path or null if the path is outside the jail
68
-	 */
69
-	protected function getJailedPath($path) {
70
-		if ($this->getRoot() === '') {
71
-			return $path;
72
-		}
73
-		$rootLength = strlen($this->getRoot()) + 1;
74
-		if ($path === $this->getRoot()) {
75
-			return '';
76
-		} else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') {
77
-			return substr($path, $rootLength);
78
-		} else {
79
-			return null;
80
-		}
81
-	}
82
-
83
-	/**
84
-	 * @param ICacheEntry|array $entry
85
-	 * @return array
86
-	 */
87
-	protected function formatCacheEntry($entry) {
88
-		if (isset($entry['path'])) {
89
-			$entry['path'] = $this->getJailedPath($entry['path']);
90
-		}
91
-		return $entry;
92
-	}
93
-
94
-	protected function filterCacheEntry($entry) {
95
-		$rootLength = strlen($this->getRoot()) + 1;
96
-		return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/');
97
-	}
98
-
99
-	/**
100
-	 * get the stored metadata of a file or folder
101
-	 *
102
-	 * @param string /int $file
103
-	 * @return ICacheEntry|false
104
-	 */
105
-	public function get($file) {
106
-		if (is_string($file) or $file == '') {
107
-			$file = $this->getSourcePath($file);
108
-		}
109
-		return parent::get($file);
110
-	}
111
-
112
-	/**
113
-	 * insert meta data for a new file or folder
114
-	 *
115
-	 * @param string $file
116
-	 * @param array $data
117
-	 *
118
-	 * @return int file id
119
-	 * @throws \RuntimeException
120
-	 */
121
-	public function insert($file, array $data) {
122
-		return $this->getCache()->insert($this->getSourcePath($file), $data);
123
-	}
124
-
125
-	/**
126
-	 * update the metadata in the cache
127
-	 *
128
-	 * @param int $id
129
-	 * @param array $data
130
-	 */
131
-	public function update($id, array $data) {
132
-		$this->getCache()->update($id, $data);
133
-	}
134
-
135
-	/**
136
-	 * get the file id for a file
137
-	 *
138
-	 * @param string $file
139
-	 * @return int
140
-	 */
141
-	public function getId($file) {
142
-		return $this->getCache()->getId($this->getSourcePath($file));
143
-	}
144
-
145
-	/**
146
-	 * get the id of the parent folder of a file
147
-	 *
148
-	 * @param string $file
149
-	 * @return int
150
-	 */
151
-	public function getParentId($file) {
152
-		return $this->getCache()->getParentId($this->getSourcePath($file));
153
-	}
154
-
155
-	/**
156
-	 * check if a file is available in the cache
157
-	 *
158
-	 * @param string $file
159
-	 * @return bool
160
-	 */
161
-	public function inCache($file) {
162
-		return $this->getCache()->inCache($this->getSourcePath($file));
163
-	}
164
-
165
-	/**
166
-	 * remove a file or folder from the cache
167
-	 *
168
-	 * @param string $file
169
-	 */
170
-	public function remove($file) {
171
-		$this->getCache()->remove($this->getSourcePath($file));
172
-	}
173
-
174
-	/**
175
-	 * Move a file or folder in the cache
176
-	 *
177
-	 * @param string $source
178
-	 * @param string $target
179
-	 */
180
-	public function move($source, $target) {
181
-		$this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
182
-	}
183
-
184
-	/**
185
-	 * Get the storage id and path needed for a move
186
-	 *
187
-	 * @param string $path
188
-	 * @return array [$storageId, $internalPath]
189
-	 */
190
-	protected function getMoveInfo($path) {
191
-		return [$this->getNumericStorageId(), $this->getSourcePath($path)];
192
-	}
193
-
194
-	/**
195
-	 * remove all entries for files that are stored on the storage from the cache
196
-	 */
197
-	public function clear() {
198
-		$this->getCache()->remove($this->getRoot());
199
-	}
200
-
201
-	/**
202
-	 * @param string $file
203
-	 *
204
-	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
205
-	 */
206
-	public function getStatus($file) {
207
-		return $this->getCache()->getStatus($this->getSourcePath($file));
208
-	}
209
-
210
-	private function formatSearchResults($results) {
211
-		$results = array_filter($results, array($this, 'filterCacheEntry'));
212
-		$results = array_values($results);
213
-		return array_map(array($this, 'formatCacheEntry'), $results);
214
-	}
215
-
216
-	/**
217
-	 * search for files matching $pattern
218
-	 *
219
-	 * @param string $pattern
220
-	 * @return array an array of file data
221
-	 */
222
-	public function search($pattern) {
223
-		$results = $this->getCache()->search($pattern);
224
-		return $this->formatSearchResults($results);
225
-	}
226
-
227
-	/**
228
-	 * search for files by mimetype
229
-	 *
230
-	 * @param string $mimetype
231
-	 * @return array
232
-	 */
233
-	public function searchByMime($mimetype) {
234
-		$results = $this->getCache()->searchByMime($mimetype);
235
-		return $this->formatSearchResults($results);
236
-	}
237
-
238
-	public function searchQuery(ISearchQuery $query) {
239
-		$results = $this->getCache()->searchQuery($query);
240
-		return $this->formatSearchResults($results);
241
-	}
242
-
243
-	/**
244
-	 * search for files by mimetype
245
-	 *
246
-	 * @param string|int $tag name or tag id
247
-	 * @param string $userId owner of the tags
248
-	 * @return array
249
-	 */
250
-	public function searchByTag($tag, $userId) {
251
-		$results = $this->getCache()->searchByTag($tag, $userId);
252
-		return $this->formatSearchResults($results);
253
-	}
254
-
255
-	/**
256
-	 * update the folder size and the size of all parent folders
257
-	 *
258
-	 * @param string|boolean $path
259
-	 * @param array $data (optional) meta data of the folder
260
-	 */
261
-	public function correctFolderSize($path, $data = null) {
262
-		if ($this->getCache() instanceof Cache) {
263
-			$this->getCache()->correctFolderSize($this->getSourcePath($path), $data);
264
-		}
265
-	}
266
-
267
-	/**
268
-	 * get the size of a folder and set it in the cache
269
-	 *
270
-	 * @param string $path
271
-	 * @param array $entry (optional) meta data of the folder
272
-	 * @return int
273
-	 */
274
-	public function calculateFolderSize($path, $entry = null) {
275
-		if ($this->getCache() instanceof Cache) {
276
-			return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
277
-		} else {
278
-			return 0;
279
-		}
280
-
281
-	}
282
-
283
-	/**
284
-	 * get all file ids on the files on the storage
285
-	 *
286
-	 * @return int[]
287
-	 */
288
-	public function getAll() {
289
-		// not supported
290
-		return array();
291
-	}
292
-
293
-	/**
294
-	 * find a folder in the cache which has not been fully scanned
295
-	 *
296
-	 * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
297
-	 * use the one with the highest id gives the best result with the background scanner, since that is most
298
-	 * likely the folder where we stopped scanning previously
299
-	 *
300
-	 * @return string|bool the path of the folder or false when no folder matched
301
-	 */
302
-	public function getIncomplete() {
303
-		// not supported
304
-		return false;
305
-	}
306
-
307
-	/**
308
-	 * get the path of a file on this storage by it's id
309
-	 *
310
-	 * @param int $id
311
-	 * @return string|null
312
-	 */
313
-	public function getPathById($id) {
314
-		$path = $this->getCache()->getPathById($id);
315
-		if ($path === null) {
316
-			return null;
317
-		}
318
-
319
-		return $this->getJailedPath($path);
320
-	}
321
-
322
-	/**
323
-	 * Move a file or folder in the cache
324
-	 *
325
-	 * Note that this should make sure the entries are removed from the source cache
326
-	 *
327
-	 * @param \OCP\Files\Cache\ICache $sourceCache
328
-	 * @param string $sourcePath
329
-	 * @param string $targetPath
330
-	 */
331
-	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
332
-		if ($sourceCache === $this) {
333
-			return $this->move($sourcePath, $targetPath);
334
-		}
335
-		return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
336
-	}
39
+    /**
40
+     * @var string
41
+     */
42
+    protected $root;
43
+
44
+    /**
45
+     * @param \OCP\Files\Cache\ICache $cache
46
+     * @param string $root
47
+     */
48
+    public function __construct($cache, $root) {
49
+        parent::__construct($cache);
50
+        $this->root = $root;
51
+    }
52
+
53
+    protected function getRoot() {
54
+        return $this->root;
55
+    }
56
+
57
+    protected function getSourcePath($path) {
58
+        if ($path === '') {
59
+            return $this->getRoot();
60
+        } else {
61
+            return $this->getRoot() . '/' . ltrim($path, '/');
62
+        }
63
+    }
64
+
65
+    /**
66
+     * @param string $path
67
+     * @return null|string the jailed path or null if the path is outside the jail
68
+     */
69
+    protected function getJailedPath($path) {
70
+        if ($this->getRoot() === '') {
71
+            return $path;
72
+        }
73
+        $rootLength = strlen($this->getRoot()) + 1;
74
+        if ($path === $this->getRoot()) {
75
+            return '';
76
+        } else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') {
77
+            return substr($path, $rootLength);
78
+        } else {
79
+            return null;
80
+        }
81
+    }
82
+
83
+    /**
84
+     * @param ICacheEntry|array $entry
85
+     * @return array
86
+     */
87
+    protected function formatCacheEntry($entry) {
88
+        if (isset($entry['path'])) {
89
+            $entry['path'] = $this->getJailedPath($entry['path']);
90
+        }
91
+        return $entry;
92
+    }
93
+
94
+    protected function filterCacheEntry($entry) {
95
+        $rootLength = strlen($this->getRoot()) + 1;
96
+        return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/');
97
+    }
98
+
99
+    /**
100
+     * get the stored metadata of a file or folder
101
+     *
102
+     * @param string /int $file
103
+     * @return ICacheEntry|false
104
+     */
105
+    public function get($file) {
106
+        if (is_string($file) or $file == '') {
107
+            $file = $this->getSourcePath($file);
108
+        }
109
+        return parent::get($file);
110
+    }
111
+
112
+    /**
113
+     * insert meta data for a new file or folder
114
+     *
115
+     * @param string $file
116
+     * @param array $data
117
+     *
118
+     * @return int file id
119
+     * @throws \RuntimeException
120
+     */
121
+    public function insert($file, array $data) {
122
+        return $this->getCache()->insert($this->getSourcePath($file), $data);
123
+    }
124
+
125
+    /**
126
+     * update the metadata in the cache
127
+     *
128
+     * @param int $id
129
+     * @param array $data
130
+     */
131
+    public function update($id, array $data) {
132
+        $this->getCache()->update($id, $data);
133
+    }
134
+
135
+    /**
136
+     * get the file id for a file
137
+     *
138
+     * @param string $file
139
+     * @return int
140
+     */
141
+    public function getId($file) {
142
+        return $this->getCache()->getId($this->getSourcePath($file));
143
+    }
144
+
145
+    /**
146
+     * get the id of the parent folder of a file
147
+     *
148
+     * @param string $file
149
+     * @return int
150
+     */
151
+    public function getParentId($file) {
152
+        return $this->getCache()->getParentId($this->getSourcePath($file));
153
+    }
154
+
155
+    /**
156
+     * check if a file is available in the cache
157
+     *
158
+     * @param string $file
159
+     * @return bool
160
+     */
161
+    public function inCache($file) {
162
+        return $this->getCache()->inCache($this->getSourcePath($file));
163
+    }
164
+
165
+    /**
166
+     * remove a file or folder from the cache
167
+     *
168
+     * @param string $file
169
+     */
170
+    public function remove($file) {
171
+        $this->getCache()->remove($this->getSourcePath($file));
172
+    }
173
+
174
+    /**
175
+     * Move a file or folder in the cache
176
+     *
177
+     * @param string $source
178
+     * @param string $target
179
+     */
180
+    public function move($source, $target) {
181
+        $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target));
182
+    }
183
+
184
+    /**
185
+     * Get the storage id and path needed for a move
186
+     *
187
+     * @param string $path
188
+     * @return array [$storageId, $internalPath]
189
+     */
190
+    protected function getMoveInfo($path) {
191
+        return [$this->getNumericStorageId(), $this->getSourcePath($path)];
192
+    }
193
+
194
+    /**
195
+     * remove all entries for files that are stored on the storage from the cache
196
+     */
197
+    public function clear() {
198
+        $this->getCache()->remove($this->getRoot());
199
+    }
200
+
201
+    /**
202
+     * @param string $file
203
+     *
204
+     * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
205
+     */
206
+    public function getStatus($file) {
207
+        return $this->getCache()->getStatus($this->getSourcePath($file));
208
+    }
209
+
210
+    private function formatSearchResults($results) {
211
+        $results = array_filter($results, array($this, 'filterCacheEntry'));
212
+        $results = array_values($results);
213
+        return array_map(array($this, 'formatCacheEntry'), $results);
214
+    }
215
+
216
+    /**
217
+     * search for files matching $pattern
218
+     *
219
+     * @param string $pattern
220
+     * @return array an array of file data
221
+     */
222
+    public function search($pattern) {
223
+        $results = $this->getCache()->search($pattern);
224
+        return $this->formatSearchResults($results);
225
+    }
226
+
227
+    /**
228
+     * search for files by mimetype
229
+     *
230
+     * @param string $mimetype
231
+     * @return array
232
+     */
233
+    public function searchByMime($mimetype) {
234
+        $results = $this->getCache()->searchByMime($mimetype);
235
+        return $this->formatSearchResults($results);
236
+    }
237
+
238
+    public function searchQuery(ISearchQuery $query) {
239
+        $results = $this->getCache()->searchQuery($query);
240
+        return $this->formatSearchResults($results);
241
+    }
242
+
243
+    /**
244
+     * search for files by mimetype
245
+     *
246
+     * @param string|int $tag name or tag id
247
+     * @param string $userId owner of the tags
248
+     * @return array
249
+     */
250
+    public function searchByTag($tag, $userId) {
251
+        $results = $this->getCache()->searchByTag($tag, $userId);
252
+        return $this->formatSearchResults($results);
253
+    }
254
+
255
+    /**
256
+     * update the folder size and the size of all parent folders
257
+     *
258
+     * @param string|boolean $path
259
+     * @param array $data (optional) meta data of the folder
260
+     */
261
+    public function correctFolderSize($path, $data = null) {
262
+        if ($this->getCache() instanceof Cache) {
263
+            $this->getCache()->correctFolderSize($this->getSourcePath($path), $data);
264
+        }
265
+    }
266
+
267
+    /**
268
+     * get the size of a folder and set it in the cache
269
+     *
270
+     * @param string $path
271
+     * @param array $entry (optional) meta data of the folder
272
+     * @return int
273
+     */
274
+    public function calculateFolderSize($path, $entry = null) {
275
+        if ($this->getCache() instanceof Cache) {
276
+            return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry);
277
+        } else {
278
+            return 0;
279
+        }
280
+
281
+    }
282
+
283
+    /**
284
+     * get all file ids on the files on the storage
285
+     *
286
+     * @return int[]
287
+     */
288
+    public function getAll() {
289
+        // not supported
290
+        return array();
291
+    }
292
+
293
+    /**
294
+     * find a folder in the cache which has not been fully scanned
295
+     *
296
+     * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
297
+     * use the one with the highest id gives the best result with the background scanner, since that is most
298
+     * likely the folder where we stopped scanning previously
299
+     *
300
+     * @return string|bool the path of the folder or false when no folder matched
301
+     */
302
+    public function getIncomplete() {
303
+        // not supported
304
+        return false;
305
+    }
306
+
307
+    /**
308
+     * get the path of a file on this storage by it's id
309
+     *
310
+     * @param int $id
311
+     * @return string|null
312
+     */
313
+    public function getPathById($id) {
314
+        $path = $this->getCache()->getPathById($id);
315
+        if ($path === null) {
316
+            return null;
317
+        }
318
+
319
+        return $this->getJailedPath($path);
320
+    }
321
+
322
+    /**
323
+     * Move a file or folder in the cache
324
+     *
325
+     * Note that this should make sure the entries are removed from the source cache
326
+     *
327
+     * @param \OCP\Files\Cache\ICache $sourceCache
328
+     * @param string $sourcePath
329
+     * @param string $targetPath
330
+     */
331
+    public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
332
+        if ($sourceCache === $this) {
333
+            return $this->move($sourcePath, $targetPath);
334
+        }
335
+        return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath));
336
+    }
337 337
 }
Please login to merge, or discard this patch.