Completed
Pull Request — master (#4329)
by Robin
14:38
created
lib/private/Files/Storage/Local.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
 		if ($this->datadir === '/') {
59 59
 			$this->realDataDir = $this->datadir;
60 60
 		} else {
61
-			$this->realDataDir = rtrim(realpath($this->datadir), '/') . '/';
61
+			$this->realDataDir = rtrim(realpath($this->datadir), '/').'/';
62 62
 		}
63 63
 		if (substr($this->datadir, -1) !== '/') {
64 64
 			$this->datadir .= '/';
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
 	}
71 71
 
72 72
 	public function getId() {
73
-		return 'local::' . $this->datadir;
73
+		return 'local::'.$this->datadir;
74 74
 	}
75 75
 
76 76
 	public function mkdir($path) {
@@ -228,17 +228,17 @@  discard block
 block discarded – undo
228 228
 		$dstParent = dirname($path2);
229 229
 
230 230
 		if (!$this->isUpdatable($srcParent)) {
231
-			\OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, \OCP\Util::ERROR);
231
+			\OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : '.$srcParent, \OCP\Util::ERROR);
232 232
 			return false;
233 233
 		}
234 234
 
235 235
 		if (!$this->isUpdatable($dstParent)) {
236
-			\OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, \OCP\Util::ERROR);
236
+			\OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : '.$dstParent, \OCP\Util::ERROR);
237 237
 			return false;
238 238
 		}
239 239
 
240 240
 		if (!$this->file_exists($path1)) {
241
-			\OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, \OCP\Util::ERROR);
241
+			\OCP\Util::writeLog('core', 'unable to rename, file does not exists : '.$path1, \OCP\Util::ERROR);
242 242
 			return false;
243 243
 		}
244 244
 
@@ -319,13 +319,13 @@  discard block
 block discarded – undo
319 319
 		foreach (scandir($physicalDir) as $item) {
320 320
 			if (\OC\Files\Filesystem::isIgnoredDir($item))
321 321
 				continue;
322
-			$physicalItem = $physicalDir . '/' . $item;
322
+			$physicalItem = $physicalDir.'/'.$item;
323 323
 
324 324
 			if (strstr(strtolower($item), strtolower($query)) !== false) {
325
-				$files[] = $dir . '/' . $item;
325
+				$files[] = $dir.'/'.$item;
326 326
 			}
327 327
 			if (is_dir($physicalItem)) {
328
-				$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
328
+				$files = array_merge($files, $this->searchInDir($query, $dir.'/'.$item));
329 329
 			}
330 330
 		}
331 331
 		return $files;
@@ -354,7 +354,7 @@  discard block
 block discarded – undo
354 354
 	 * @throws ForbiddenException
355 355
 	 */
356 356
 	public function getSourcePath($path) {
357
-		$fullPath = $this->datadir . $path;
357
+		$fullPath = $this->datadir.$path;
358 358
 		if ($this->allowSymlinks || $path === '') {
359 359
 			return $fullPath;
360 360
 		}
@@ -365,7 +365,7 @@  discard block
 block discarded – undo
365 365
 			$realPath = realpath($pathToResolve);
366 366
 		}
367 367
 		if ($realPath) {
368
-			$realPath = $realPath . '/';
368
+			$realPath = $realPath.'/';
369 369
 		}
370 370
 		if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
371 371
 			return $fullPath;
@@ -391,9 +391,9 @@  discard block
 block discarded – undo
391 391
 		if ($this->is_file($path)) {
392 392
 			$stat = $this->stat($path);
393 393
 			return md5(
394
-				$stat['mtime'] .
395
-				$stat['ino'] .
396
-				$stat['dev'] .
394
+				$stat['mtime'].
395
+				$stat['ino'].
396
+				$stat['dev'].
397 397
 				$stat['size']
398 398
 			);
399 399
 		} else {
Please login to merge, or discard this patch.
Indentation   +402 added lines, -402 removed lines patch added patch discarded remove patch
@@ -42,406 +42,406 @@
 block discarded – undo
42 42
  * for local filestore, we only have to map the paths
43 43
  */
44 44
 class Local extends \OC\Files\Storage\Common {
45
-	protected $datadir;
46
-
47
-	protected $dataDirLength;
48
-
49
-	protected $allowSymlinks = false;
50
-
51
-	protected $realDataDir;
52
-
53
-	public function __construct($arguments) {
54
-		if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
55
-			throw new \InvalidArgumentException('No data directory set for local storage');
56
-		}
57
-		$this->datadir = $arguments['datadir'];
58
-		// some crazy code uses a local storage on root...
59
-		if ($this->datadir === '/') {
60
-			$this->realDataDir = $this->datadir;
61
-		} else {
62
-			$this->realDataDir = rtrim(realpath($this->datadir), '/') . '/';
63
-		}
64
-		if (substr($this->datadir, -1) !== '/') {
65
-			$this->datadir .= '/';
66
-		}
67
-		$this->dataDirLength = strlen($this->realDataDir);
68
-	}
69
-
70
-	public function __destruct() {
71
-	}
72
-
73
-	public function getId() {
74
-		return 'local::' . $this->datadir;
75
-	}
76
-
77
-	public function mkdir($path) {
78
-		return @mkdir($this->getSourcePath($path), 0777, true);
79
-	}
80
-
81
-	public function rmdir($path) {
82
-		if (!$this->isDeletable($path)) {
83
-			return false;
84
-		}
85
-		try {
86
-			$it = new \RecursiveIteratorIterator(
87
-				new \RecursiveDirectoryIterator($this->getSourcePath($path)),
88
-				\RecursiveIteratorIterator::CHILD_FIRST
89
-			);
90
-			/**
91
-			 * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
92
-			 * This bug is fixed in PHP 5.5.9 or before
93
-			 * See #8376
94
-			 */
95
-			$it->rewind();
96
-			while ($it->valid()) {
97
-				/**
98
-				 * @var \SplFileInfo $file
99
-				 */
100
-				$file = $it->current();
101
-				if (in_array($file->getBasename(), array('.', '..'))) {
102
-					$it->next();
103
-					continue;
104
-				} elseif ($file->isDir()) {
105
-					rmdir($file->getPathname());
106
-				} elseif ($file->isFile() || $file->isLink()) {
107
-					unlink($file->getPathname());
108
-				}
109
-				$it->next();
110
-			}
111
-			return rmdir($this->getSourcePath($path));
112
-		} catch (\UnexpectedValueException $e) {
113
-			return false;
114
-		}
115
-	}
116
-
117
-	public function opendir($path) {
118
-		return opendir($this->getSourcePath($path));
119
-	}
120
-
121
-	public function is_dir($path) {
122
-		if (substr($path, -1) == '/') {
123
-			$path = substr($path, 0, -1);
124
-		}
125
-		return is_dir($this->getSourcePath($path));
126
-	}
127
-
128
-	public function is_file($path) {
129
-		return is_file($this->getSourcePath($path));
130
-	}
131
-
132
-	public function stat($path) {
133
-		clearstatcache();
134
-		$fullPath = $this->getSourcePath($path);
135
-		$statResult = stat($fullPath);
136
-		if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
137
-			$filesize = $this->filesize($path);
138
-			$statResult['size'] = $filesize;
139
-			$statResult[7] = $filesize;
140
-		}
141
-		return $statResult;
142
-	}
143
-
144
-	public function filetype($path) {
145
-		$filetype = filetype($this->getSourcePath($path));
146
-		if ($filetype == 'link') {
147
-			$filetype = filetype(realpath($this->getSourcePath($path)));
148
-		}
149
-		return $filetype;
150
-	}
151
-
152
-	public function filesize($path) {
153
-		if ($this->is_dir($path)) {
154
-			return 0;
155
-		}
156
-		$fullPath = $this->getSourcePath($path);
157
-		if (PHP_INT_SIZE === 4) {
158
-			$helper = new \OC\LargeFileHelper;
159
-			return $helper->getFileSize($fullPath);
160
-		}
161
-		return filesize($fullPath);
162
-	}
163
-
164
-	public function isReadable($path) {
165
-		return is_readable($this->getSourcePath($path));
166
-	}
167
-
168
-	public function isUpdatable($path) {
169
-		return is_writable($this->getSourcePath($path));
170
-	}
171
-
172
-	public function file_exists($path) {
173
-		return file_exists($this->getSourcePath($path));
174
-	}
175
-
176
-	public function filemtime($path) {
177
-		$fullPath = $this->getSourcePath($path);
178
-		clearstatcache($fullPath);
179
-		if (!$this->file_exists($path)) {
180
-			return false;
181
-		}
182
-		if (PHP_INT_SIZE === 4) {
183
-			$helper = new \OC\LargeFileHelper();
184
-			return $helper->getFileMtime($fullPath);
185
-		}
186
-		return filemtime($fullPath);
187
-	}
188
-
189
-	public function touch($path, $mtime = null) {
190
-		// sets the modification time of the file to the given value.
191
-		// If mtime is nil the current time is set.
192
-		// note that the access time of the file always changes to the current time.
193
-		if ($this->file_exists($path) and !$this->isUpdatable($path)) {
194
-			return false;
195
-		}
196
-		if (!is_null($mtime)) {
197
-			$result = touch($this->getSourcePath($path), $mtime);
198
-		} else {
199
-			$result = touch($this->getSourcePath($path));
200
-		}
201
-		if ($result) {
202
-			clearstatcache(true, $this->getSourcePath($path));
203
-		}
204
-
205
-		return $result;
206
-	}
207
-
208
-	public function file_get_contents($path) {
209
-		return file_get_contents($this->getSourcePath($path));
210
-	}
211
-
212
-	public function file_put_contents($path, $data) {
213
-		return file_put_contents($this->getSourcePath($path), $data);
214
-	}
215
-
216
-	public function unlink($path) {
217
-		if ($this->is_dir($path)) {
218
-			return $this->rmdir($path);
219
-		} else if ($this->is_file($path)) {
220
-			return unlink($this->getSourcePath($path));
221
-		} else {
222
-			return false;
223
-		}
224
-
225
-	}
226
-
227
-	public function rename($path1, $path2) {
228
-		$srcParent = dirname($path1);
229
-		$dstParent = dirname($path2);
230
-
231
-		if (!$this->isUpdatable($srcParent)) {
232
-			\OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, \OCP\Util::ERROR);
233
-			return false;
234
-		}
235
-
236
-		if (!$this->isUpdatable($dstParent)) {
237
-			\OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, \OCP\Util::ERROR);
238
-			return false;
239
-		}
240
-
241
-		if (!$this->file_exists($path1)) {
242
-			\OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, \OCP\Util::ERROR);
243
-			return false;
244
-		}
245
-
246
-		if ($this->is_dir($path2)) {
247
-			$this->rmdir($path2);
248
-		} else if ($this->is_file($path2)) {
249
-			$this->unlink($path2);
250
-		}
251
-
252
-		if ($this->is_dir($path1)) {
253
-			// we can't move folders across devices, use copy instead
254
-			$stat1 = stat(dirname($this->getSourcePath($path1)));
255
-			$stat2 = stat(dirname($this->getSourcePath($path2)));
256
-			if ($stat1['dev'] !== $stat2['dev']) {
257
-				$result = $this->copy($path1, $path2);
258
-				if ($result) {
259
-					$result &= $this->rmdir($path1);
260
-				}
261
-				return $result;
262
-			}
263
-		}
264
-
265
-		return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
266
-	}
267
-
268
-	public function copy($path1, $path2) {
269
-		if ($this->is_dir($path1)) {
270
-			return parent::copy($path1, $path2);
271
-		} else {
272
-			return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
273
-		}
274
-	}
275
-
276
-	public function fopen($path, $mode) {
277
-		return fopen($this->getSourcePath($path), $mode);
278
-	}
279
-
280
-	public function hash($type, $path, $raw = false) {
281
-		return hash_file($type, $this->getSourcePath($path), $raw);
282
-	}
283
-
284
-	public function free_space($path) {
285
-		$sourcePath = $this->getSourcePath($path);
286
-		// using !is_dir because $sourcePath might be a part file or
287
-		// non-existing file, so we'd still want to use the parent dir
288
-		// in such cases
289
-		if (!is_dir($sourcePath)) {
290
-			// disk_free_space doesn't work on files
291
-			$sourcePath = dirname($sourcePath);
292
-		}
293
-		$space = @disk_free_space($sourcePath);
294
-		if ($space === false || is_null($space)) {
295
-			return \OCP\Files\FileInfo::SPACE_UNKNOWN;
296
-		}
297
-		return $space;
298
-	}
299
-
300
-	public function search($query) {
301
-		return $this->searchInDir($query);
302
-	}
303
-
304
-	public function getLocalFile($path) {
305
-		return $this->getSourcePath($path);
306
-	}
307
-
308
-	public function getLocalFolder($path) {
309
-		return $this->getSourcePath($path);
310
-	}
311
-
312
-	/**
313
-	 * @param string $query
314
-	 * @param string $dir
315
-	 * @return array
316
-	 */
317
-	protected function searchInDir($query, $dir = '') {
318
-		$files = array();
319
-		$physicalDir = $this->getSourcePath($dir);
320
-		foreach (scandir($physicalDir) as $item) {
321
-			if (\OC\Files\Filesystem::isIgnoredDir($item))
322
-				continue;
323
-			$physicalItem = $physicalDir . '/' . $item;
324
-
325
-			if (strstr(strtolower($item), strtolower($query)) !== false) {
326
-				$files[] = $dir . '/' . $item;
327
-			}
328
-			if (is_dir($physicalItem)) {
329
-				$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
330
-			}
331
-		}
332
-		return $files;
333
-	}
334
-
335
-	/**
336
-	 * check if a file or folder has been updated since $time
337
-	 *
338
-	 * @param string $path
339
-	 * @param int $time
340
-	 * @return bool
341
-	 */
342
-	public function hasUpdated($path, $time) {
343
-		if ($this->file_exists($path)) {
344
-			return $this->filemtime($path) > $time;
345
-		} else {
346
-			return true;
347
-		}
348
-	}
349
-
350
-	/**
351
-	 * Get the source path (on disk) of a given path
352
-	 *
353
-	 * @param string $path
354
-	 * @return string
355
-	 * @throws ForbiddenException
356
-	 */
357
-	public function getSourcePath($path) {
358
-		$fullPath = $this->datadir . $path;
359
-		if ($this->allowSymlinks || $path === '') {
360
-			return $fullPath;
361
-		}
362
-		$pathToResolve = $fullPath;
363
-		$realPath = realpath($pathToResolve);
364
-		while ($realPath === false) { // for non existing files check the parent directory
365
-			$pathToResolve = dirname($pathToResolve);
366
-			$realPath = realpath($pathToResolve);
367
-		}
368
-		if ($realPath) {
369
-			$realPath = $realPath . '/';
370
-		}
371
-		if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
372
-			return $fullPath;
373
-		}
374
-
375
-		\OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", \OCP\Util::ERROR);
376
-		throw new ForbiddenException('Following symlinks is not allowed', false);
377
-	}
378
-
379
-	/**
380
-	 * {@inheritdoc}
381
-	 */
382
-	public function isLocal() {
383
-		return true;
384
-	}
385
-
386
-	/**
387
-	 * get the ETag for a file or folder
388
-	 *
389
-	 * @param string $path
390
-	 * @return string
391
-	 */
392
-	public function getETag($path) {
393
-		if ($this->is_file($path)) {
394
-			$stat = $this->stat($path);
395
-			return md5(
396
-				$stat['mtime'] .
397
-				$stat['ino'] .
398
-				$stat['dev'] .
399
-				$stat['size']
400
-			);
401
-		} else {
402
-			return parent::getETag($path);
403
-		}
404
-	}
405
-
406
-	/**
407
-	 * @param \OCP\Files\Storage $sourceStorage
408
-	 * @param string $sourceInternalPath
409
-	 * @param string $targetInternalPath
410
-	 * @return bool
411
-	 */
412
-	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
413
-		if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
414
-			/**
415
-			 * @var \OC\Files\Storage\Local $sourceStorage
416
-			 */
417
-			$rootStorage = new Local(['datadir' => '/']);
418
-			return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
419
-		} else {
420
-			return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
421
-		}
422
-	}
423
-
424
-	/**
425
-	 * @param \OCP\Files\Storage $sourceStorage
426
-	 * @param string $sourceInternalPath
427
-	 * @param string $targetInternalPath
428
-	 * @return bool
429
-	 */
430
-	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
431
-		if ($sourceStorage->instanceOfStorage(Local::class)) {
432
-			if ($sourceStorage->instanceOfStorage(Jail::class)) {
433
-				/**
434
-				 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
435
-				 */
436
-				$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
437
-			}
438
-			/**
439
-			 * @var \OC\Files\Storage\Local $sourceStorage
440
-			 */
441
-			$rootStorage = new Local(['datadir' => '/']);
442
-			return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
443
-		} else {
444
-			return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
445
-		}
446
-	}
45
+    protected $datadir;
46
+
47
+    protected $dataDirLength;
48
+
49
+    protected $allowSymlinks = false;
50
+
51
+    protected $realDataDir;
52
+
53
+    public function __construct($arguments) {
54
+        if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
55
+            throw new \InvalidArgumentException('No data directory set for local storage');
56
+        }
57
+        $this->datadir = $arguments['datadir'];
58
+        // some crazy code uses a local storage on root...
59
+        if ($this->datadir === '/') {
60
+            $this->realDataDir = $this->datadir;
61
+        } else {
62
+            $this->realDataDir = rtrim(realpath($this->datadir), '/') . '/';
63
+        }
64
+        if (substr($this->datadir, -1) !== '/') {
65
+            $this->datadir .= '/';
66
+        }
67
+        $this->dataDirLength = strlen($this->realDataDir);
68
+    }
69
+
70
+    public function __destruct() {
71
+    }
72
+
73
+    public function getId() {
74
+        return 'local::' . $this->datadir;
75
+    }
76
+
77
+    public function mkdir($path) {
78
+        return @mkdir($this->getSourcePath($path), 0777, true);
79
+    }
80
+
81
+    public function rmdir($path) {
82
+        if (!$this->isDeletable($path)) {
83
+            return false;
84
+        }
85
+        try {
86
+            $it = new \RecursiveIteratorIterator(
87
+                new \RecursiveDirectoryIterator($this->getSourcePath($path)),
88
+                \RecursiveIteratorIterator::CHILD_FIRST
89
+            );
90
+            /**
91
+             * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
92
+             * This bug is fixed in PHP 5.5.9 or before
93
+             * See #8376
94
+             */
95
+            $it->rewind();
96
+            while ($it->valid()) {
97
+                /**
98
+                 * @var \SplFileInfo $file
99
+                 */
100
+                $file = $it->current();
101
+                if (in_array($file->getBasename(), array('.', '..'))) {
102
+                    $it->next();
103
+                    continue;
104
+                } elseif ($file->isDir()) {
105
+                    rmdir($file->getPathname());
106
+                } elseif ($file->isFile() || $file->isLink()) {
107
+                    unlink($file->getPathname());
108
+                }
109
+                $it->next();
110
+            }
111
+            return rmdir($this->getSourcePath($path));
112
+        } catch (\UnexpectedValueException $e) {
113
+            return false;
114
+        }
115
+    }
116
+
117
+    public function opendir($path) {
118
+        return opendir($this->getSourcePath($path));
119
+    }
120
+
121
+    public function is_dir($path) {
122
+        if (substr($path, -1) == '/') {
123
+            $path = substr($path, 0, -1);
124
+        }
125
+        return is_dir($this->getSourcePath($path));
126
+    }
127
+
128
+    public function is_file($path) {
129
+        return is_file($this->getSourcePath($path));
130
+    }
131
+
132
+    public function stat($path) {
133
+        clearstatcache();
134
+        $fullPath = $this->getSourcePath($path);
135
+        $statResult = stat($fullPath);
136
+        if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
137
+            $filesize = $this->filesize($path);
138
+            $statResult['size'] = $filesize;
139
+            $statResult[7] = $filesize;
140
+        }
141
+        return $statResult;
142
+    }
143
+
144
+    public function filetype($path) {
145
+        $filetype = filetype($this->getSourcePath($path));
146
+        if ($filetype == 'link') {
147
+            $filetype = filetype(realpath($this->getSourcePath($path)));
148
+        }
149
+        return $filetype;
150
+    }
151
+
152
+    public function filesize($path) {
153
+        if ($this->is_dir($path)) {
154
+            return 0;
155
+        }
156
+        $fullPath = $this->getSourcePath($path);
157
+        if (PHP_INT_SIZE === 4) {
158
+            $helper = new \OC\LargeFileHelper;
159
+            return $helper->getFileSize($fullPath);
160
+        }
161
+        return filesize($fullPath);
162
+    }
163
+
164
+    public function isReadable($path) {
165
+        return is_readable($this->getSourcePath($path));
166
+    }
167
+
168
+    public function isUpdatable($path) {
169
+        return is_writable($this->getSourcePath($path));
170
+    }
171
+
172
+    public function file_exists($path) {
173
+        return file_exists($this->getSourcePath($path));
174
+    }
175
+
176
+    public function filemtime($path) {
177
+        $fullPath = $this->getSourcePath($path);
178
+        clearstatcache($fullPath);
179
+        if (!$this->file_exists($path)) {
180
+            return false;
181
+        }
182
+        if (PHP_INT_SIZE === 4) {
183
+            $helper = new \OC\LargeFileHelper();
184
+            return $helper->getFileMtime($fullPath);
185
+        }
186
+        return filemtime($fullPath);
187
+    }
188
+
189
+    public function touch($path, $mtime = null) {
190
+        // sets the modification time of the file to the given value.
191
+        // If mtime is nil the current time is set.
192
+        // note that the access time of the file always changes to the current time.
193
+        if ($this->file_exists($path) and !$this->isUpdatable($path)) {
194
+            return false;
195
+        }
196
+        if (!is_null($mtime)) {
197
+            $result = touch($this->getSourcePath($path), $mtime);
198
+        } else {
199
+            $result = touch($this->getSourcePath($path));
200
+        }
201
+        if ($result) {
202
+            clearstatcache(true, $this->getSourcePath($path));
203
+        }
204
+
205
+        return $result;
206
+    }
207
+
208
+    public function file_get_contents($path) {
209
+        return file_get_contents($this->getSourcePath($path));
210
+    }
211
+
212
+    public function file_put_contents($path, $data) {
213
+        return file_put_contents($this->getSourcePath($path), $data);
214
+    }
215
+
216
+    public function unlink($path) {
217
+        if ($this->is_dir($path)) {
218
+            return $this->rmdir($path);
219
+        } else if ($this->is_file($path)) {
220
+            return unlink($this->getSourcePath($path));
221
+        } else {
222
+            return false;
223
+        }
224
+
225
+    }
226
+
227
+    public function rename($path1, $path2) {
228
+        $srcParent = dirname($path1);
229
+        $dstParent = dirname($path2);
230
+
231
+        if (!$this->isUpdatable($srcParent)) {
232
+            \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, \OCP\Util::ERROR);
233
+            return false;
234
+        }
235
+
236
+        if (!$this->isUpdatable($dstParent)) {
237
+            \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, \OCP\Util::ERROR);
238
+            return false;
239
+        }
240
+
241
+        if (!$this->file_exists($path1)) {
242
+            \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, \OCP\Util::ERROR);
243
+            return false;
244
+        }
245
+
246
+        if ($this->is_dir($path2)) {
247
+            $this->rmdir($path2);
248
+        } else if ($this->is_file($path2)) {
249
+            $this->unlink($path2);
250
+        }
251
+
252
+        if ($this->is_dir($path1)) {
253
+            // we can't move folders across devices, use copy instead
254
+            $stat1 = stat(dirname($this->getSourcePath($path1)));
255
+            $stat2 = stat(dirname($this->getSourcePath($path2)));
256
+            if ($stat1['dev'] !== $stat2['dev']) {
257
+                $result = $this->copy($path1, $path2);
258
+                if ($result) {
259
+                    $result &= $this->rmdir($path1);
260
+                }
261
+                return $result;
262
+            }
263
+        }
264
+
265
+        return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
266
+    }
267
+
268
+    public function copy($path1, $path2) {
269
+        if ($this->is_dir($path1)) {
270
+            return parent::copy($path1, $path2);
271
+        } else {
272
+            return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
273
+        }
274
+    }
275
+
276
+    public function fopen($path, $mode) {
277
+        return fopen($this->getSourcePath($path), $mode);
278
+    }
279
+
280
+    public function hash($type, $path, $raw = false) {
281
+        return hash_file($type, $this->getSourcePath($path), $raw);
282
+    }
283
+
284
+    public function free_space($path) {
285
+        $sourcePath = $this->getSourcePath($path);
286
+        // using !is_dir because $sourcePath might be a part file or
287
+        // non-existing file, so we'd still want to use the parent dir
288
+        // in such cases
289
+        if (!is_dir($sourcePath)) {
290
+            // disk_free_space doesn't work on files
291
+            $sourcePath = dirname($sourcePath);
292
+        }
293
+        $space = @disk_free_space($sourcePath);
294
+        if ($space === false || is_null($space)) {
295
+            return \OCP\Files\FileInfo::SPACE_UNKNOWN;
296
+        }
297
+        return $space;
298
+    }
299
+
300
+    public function search($query) {
301
+        return $this->searchInDir($query);
302
+    }
303
+
304
+    public function getLocalFile($path) {
305
+        return $this->getSourcePath($path);
306
+    }
307
+
308
+    public function getLocalFolder($path) {
309
+        return $this->getSourcePath($path);
310
+    }
311
+
312
+    /**
313
+     * @param string $query
314
+     * @param string $dir
315
+     * @return array
316
+     */
317
+    protected function searchInDir($query, $dir = '') {
318
+        $files = array();
319
+        $physicalDir = $this->getSourcePath($dir);
320
+        foreach (scandir($physicalDir) as $item) {
321
+            if (\OC\Files\Filesystem::isIgnoredDir($item))
322
+                continue;
323
+            $physicalItem = $physicalDir . '/' . $item;
324
+
325
+            if (strstr(strtolower($item), strtolower($query)) !== false) {
326
+                $files[] = $dir . '/' . $item;
327
+            }
328
+            if (is_dir($physicalItem)) {
329
+                $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
330
+            }
331
+        }
332
+        return $files;
333
+    }
334
+
335
+    /**
336
+     * check if a file or folder has been updated since $time
337
+     *
338
+     * @param string $path
339
+     * @param int $time
340
+     * @return bool
341
+     */
342
+    public function hasUpdated($path, $time) {
343
+        if ($this->file_exists($path)) {
344
+            return $this->filemtime($path) > $time;
345
+        } else {
346
+            return true;
347
+        }
348
+    }
349
+
350
+    /**
351
+     * Get the source path (on disk) of a given path
352
+     *
353
+     * @param string $path
354
+     * @return string
355
+     * @throws ForbiddenException
356
+     */
357
+    public function getSourcePath($path) {
358
+        $fullPath = $this->datadir . $path;
359
+        if ($this->allowSymlinks || $path === '') {
360
+            return $fullPath;
361
+        }
362
+        $pathToResolve = $fullPath;
363
+        $realPath = realpath($pathToResolve);
364
+        while ($realPath === false) { // for non existing files check the parent directory
365
+            $pathToResolve = dirname($pathToResolve);
366
+            $realPath = realpath($pathToResolve);
367
+        }
368
+        if ($realPath) {
369
+            $realPath = $realPath . '/';
370
+        }
371
+        if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
372
+            return $fullPath;
373
+        }
374
+
375
+        \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", \OCP\Util::ERROR);
376
+        throw new ForbiddenException('Following symlinks is not allowed', false);
377
+    }
378
+
379
+    /**
380
+     * {@inheritdoc}
381
+     */
382
+    public function isLocal() {
383
+        return true;
384
+    }
385
+
386
+    /**
387
+     * get the ETag for a file or folder
388
+     *
389
+     * @param string $path
390
+     * @return string
391
+     */
392
+    public function getETag($path) {
393
+        if ($this->is_file($path)) {
394
+            $stat = $this->stat($path);
395
+            return md5(
396
+                $stat['mtime'] .
397
+                $stat['ino'] .
398
+                $stat['dev'] .
399
+                $stat['size']
400
+            );
401
+        } else {
402
+            return parent::getETag($path);
403
+        }
404
+    }
405
+
406
+    /**
407
+     * @param \OCP\Files\Storage $sourceStorage
408
+     * @param string $sourceInternalPath
409
+     * @param string $targetInternalPath
410
+     * @return bool
411
+     */
412
+    public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
413
+        if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
414
+            /**
415
+             * @var \OC\Files\Storage\Local $sourceStorage
416
+             */
417
+            $rootStorage = new Local(['datadir' => '/']);
418
+            return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
419
+        } else {
420
+            return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
421
+        }
422
+    }
423
+
424
+    /**
425
+     * @param \OCP\Files\Storage $sourceStorage
426
+     * @param string $sourceInternalPath
427
+     * @param string $targetInternalPath
428
+     * @return bool
429
+     */
430
+    public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
431
+        if ($sourceStorage->instanceOfStorage(Local::class)) {
432
+            if ($sourceStorage->instanceOfStorage(Jail::class)) {
433
+                /**
434
+                 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
435
+                 */
436
+                $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
437
+            }
438
+            /**
439
+             * @var \OC\Files\Storage\Local $sourceStorage
440
+             */
441
+            $rootStorage = new Local(['datadir' => '/']);
442
+            return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
443
+        } else {
444
+            return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
445
+        }
446
+    }
447 447
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/Scanner.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -31,52 +31,52 @@
 block discarded – undo
31 31
  * Scanner for SharedStorage
32 32
  */
33 33
 class Scanner extends \OC\Files\Cache\Scanner {
34
-	/**
35
-	 * @var \OCA\Files_Sharing\SharedStorage $storage
36
-	 */
37
-	protected $storage;
34
+    /**
35
+     * @var \OCA\Files_Sharing\SharedStorage $storage
36
+     */
37
+    protected $storage;
38 38
 
39
-	private $sourceScanner;
39
+    private $sourceScanner;
40 40
 
41
-	/**
42
-	 * Returns metadata from the shared storage, but
43
-	 * with permissions from the source storage.
44
-	 *
45
-	 * @param string $path path of the file for which to retrieve metadata
46
-	 *
47
-	 * @return array an array of metadata of the file
48
-	 */
49
-	public function getData($path) {
50
-		$data = parent::getData($path);
51
-		if ($data === null) {
52
-			return null;
53
-		}
54
-		$internalPath = $this->storage->getUnjailedPath($path);
55
-		$data['permissions'] = $this->storage->getSourceStorage()->getPermissions($internalPath);
56
-		return $data;
57
-	}
41
+    /**
42
+     * Returns metadata from the shared storage, but
43
+     * with permissions from the source storage.
44
+     *
45
+     * @param string $path path of the file for which to retrieve metadata
46
+     *
47
+     * @return array an array of metadata of the file
48
+     */
49
+    public function getData($path) {
50
+        $data = parent::getData($path);
51
+        if ($data === null) {
52
+            return null;
53
+        }
54
+        $internalPath = $this->storage->getUnjailedPath($path);
55
+        $data['permissions'] = $this->storage->getSourceStorage()->getPermissions($internalPath);
56
+        return $data;
57
+    }
58 58
 
59
-	private function getSourceScanner() {
60
-		if ($this->sourceScanner) {
61
-			return $this->sourceScanner;
62
-		}
63
-		if ($this->storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
64
-			/** @var \OC\Files\Storage\Storage $storage */
65
-			list($storage) = $this->storage->resolvePath('');
66
-			$this->sourceScanner = $storage->getScanner();
67
-			return $this->sourceScanner;
68
-		} else {
69
-			return null;
70
-		}
71
-	}
59
+    private function getSourceScanner() {
60
+        if ($this->sourceScanner) {
61
+            return $this->sourceScanner;
62
+        }
63
+        if ($this->storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
64
+            /** @var \OC\Files\Storage\Storage $storage */
65
+            list($storage) = $this->storage->resolvePath('');
66
+            $this->sourceScanner = $storage->getScanner();
67
+            return $this->sourceScanner;
68
+        } else {
69
+            return null;
70
+        }
71
+    }
72 72
 
73
-	public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) {
74
-		$sourceScanner = $this->getSourceScanner();
75
-		if ($sourceScanner instanceof NoopScanner) {
76
-			return [];
77
-		} else {
78
-			return parent::scanFile($file, $reuseExisting, $parentId, $cacheData, $lock);
79
-		}
80
-	}
73
+    public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) {
74
+        $sourceScanner = $this->getSourceScanner();
75
+        if ($sourceScanner instanceof NoopScanner) {
76
+            return [];
77
+        } else {
78
+            return parent::scanFile($file, $reuseExisting, $parentId, $cacheData, $lock);
79
+        }
80
+    }
81 81
 }
82 82
 
Please login to merge, or discard this patch.
apps/files_sharing/lib/SharedStorage.php 2 patches
Indentation   +450 added lines, -450 removed lines patch added patch discarded remove patch
@@ -47,454 +47,454 @@
 block discarded – undo
47 47
  */
48 48
 class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage {
49 49
 
50
-	/** @var \OCP\Share\IShare */
51
-	private $superShare;
52
-
53
-	/** @var \OCP\Share\IShare[] */
54
-	private $groupedShares;
55
-
56
-	/**
57
-	 * @var \OC\Files\View
58
-	 */
59
-	private $ownerView;
60
-
61
-	private $initialized = false;
62
-
63
-	/**
64
-	 * @var ICacheEntry
65
-	 */
66
-	private $sourceRootInfo;
67
-
68
-	/** @var string */
69
-	private $user;
70
-
71
-	/**
72
-	 * @var \OCP\ILogger
73
-	 */
74
-	private $logger;
75
-
76
-	/** @var  IStorage */
77
-	private $nonMaskedStorage;
78
-
79
-	private $options;
80
-
81
-	public function __construct($arguments) {
82
-		$this->ownerView = $arguments['ownerView'];
83
-		$this->logger = \OC::$server->getLogger();
84
-
85
-		$this->superShare = $arguments['superShare'];
86
-		$this->groupedShares = $arguments['groupedShares'];
87
-
88
-		$this->user = $arguments['user'];
89
-
90
-		parent::__construct([
91
-			'storage' => null,
92
-			'root' => null,
93
-		]);
94
-	}
95
-
96
-	/**
97
-	 * @return ICacheEntry
98
-	 */
99
-	private function getSourceRootInfo() {
100
-		if (is_null($this->sourceRootInfo)) {
101
-			if (is_null($this->superShare->getNodeCacheEntry())) {
102
-				$this->init();
103
-				$this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
104
-			} else {
105
-				$this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
106
-			}
107
-		}
108
-		return $this->sourceRootInfo;
109
-	}
110
-
111
-	private function init() {
112
-		if ($this->initialized) {
113
-			return;
114
-		}
115
-		$this->initialized = true;
116
-		try {
117
-			Filesystem::initMountPoints($this->superShare->getShareOwner());
118
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
119
-			list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
120
-			$this->storage = new PermissionsMask([
121
-				'storage' => $this->nonMaskedStorage,
122
-				'mask' => $this->superShare->getPermissions()
123
-			]);
124
-		} catch (NotFoundException $e) {
125
-			// original file not accessible or deleted, set FailedStorage
126
-			$this->storage = new FailedStorage(['exception' => $e]);
127
-			$this->cache = new FailedCache();
128
-			$this->rootPath = '';
129
-		} catch (NoUserException $e) {
130
-			// sharer user deleted, set FailedStorage
131
-			$this->storage = new FailedStorage(['exception' => $e]);
132
-			$this->cache = new FailedCache();
133
-			$this->rootPath = '';
134
-		} catch (\Exception $e) {
135
-			$this->storage = new FailedStorage(['exception' => $e]);
136
-			$this->cache = new FailedCache();
137
-			$this->rootPath = '';
138
-			$this->logger->logException($e);
139
-		}
140
-
141
-		if (!$this->nonMaskedStorage) {
142
-			$this->nonMaskedStorage = $this->storage;
143
-		}
144
-	}
145
-
146
-	/**
147
-	 * @inheritdoc
148
-	 */
149
-	public function instanceOfStorage($class) {
150
-		if ($class === '\OC\Files\Storage\Common') {
151
-			return true;
152
-		}
153
-		if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
154
-			return false;
155
-		}
156
-		return parent::instanceOfStorage($class);
157
-	}
158
-
159
-	/**
160
-	 * @return string
161
-	 */
162
-	public function getShareId() {
163
-		return $this->superShare->getId();
164
-	}
165
-
166
-	private function isValid() {
167
-		return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
168
-	}
169
-
170
-	/**
171
-	 * get id of the mount point
172
-	 *
173
-	 * @return string
174
-	 */
175
-	public function getId() {
176
-		return 'shared::' . $this->getMountPoint();
177
-	}
178
-
179
-	/**
180
-	 * Get the permissions granted for a shared file
181
-	 *
182
-	 * @param string $target Shared target file path
183
-	 * @return int CRUDS permissions granted
184
-	 */
185
-	public function getPermissions($target = '') {
186
-		if (!$this->isValid()) {
187
-			return 0;
188
-		}
189
-		$permissions = $this->superShare->getPermissions();
190
-		// part files and the mount point always have delete permissions
191
-		if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
192
-			$permissions |= \OCP\Constants::PERMISSION_DELETE;
193
-		}
194
-
195
-		if (\OCP\Util::isSharingDisabledForUser()) {
196
-			$permissions &= ~\OCP\Constants::PERMISSION_SHARE;
197
-		}
198
-
199
-		return $permissions;
200
-	}
201
-
202
-	public function isCreatable($path) {
203
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
204
-	}
205
-
206
-	public function isReadable($path) {
207
-		if (!$this->isValid()) {
208
-			return false;
209
-		}
210
-		if (!$this->file_exists($path)) {
211
-			return false;
212
-		}
213
-		/** @var IStorage $storage */
214
-		/** @var string $internalPath */
215
-		list($storage, $internalPath) = $this->resolvePath($path);
216
-		return $storage->isReadable($internalPath);
217
-	}
218
-
219
-	public function isUpdatable($path) {
220
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
221
-	}
222
-
223
-	public function isDeletable($path) {
224
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
225
-	}
226
-
227
-	public function isSharable($path) {
228
-		if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
229
-			return false;
230
-		}
231
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
232
-	}
233
-
234
-	public function fopen($path, $mode) {
235
-		if ($source = $this->getUnjailedPath($path)) {
236
-			switch ($mode) {
237
-				case 'r+':
238
-				case 'rb+':
239
-				case 'w+':
240
-				case 'wb+':
241
-				case 'x+':
242
-				case 'xb+':
243
-				case 'a+':
244
-				case 'ab+':
245
-				case 'w':
246
-				case 'wb':
247
-				case 'x':
248
-				case 'xb':
249
-				case 'a':
250
-				case 'ab':
251
-					$creatable = $this->isCreatable($path);
252
-					$updatable = $this->isUpdatable($path);
253
-					// if neither permissions given, no need to continue
254
-					if (!$creatable && !$updatable) {
255
-						return false;
256
-					}
257
-
258
-					$exists = $this->file_exists($path);
259
-					// if a file exists, updatable permissions are required
260
-					if ($exists && !$updatable) {
261
-						return false;
262
-					}
263
-
264
-					// part file is allowed if !$creatable but the final file is $updatable
265
-					if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
266
-						if (!$exists && !$creatable) {
267
-							return false;
268
-						}
269
-					}
270
-			}
271
-			$info = array(
272
-				'target' => $this->getMountPoint() . $path,
273
-				'source' => $source,
274
-				'mode' => $mode,
275
-			);
276
-			\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
277
-			return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
278
-		}
279
-		return false;
280
-	}
281
-
282
-	/**
283
-	 * see http://php.net/manual/en/function.rename.php
284
-	 *
285
-	 * @param string $path1
286
-	 * @param string $path2
287
-	 * @return bool
288
-	 */
289
-	public function rename($path1, $path2) {
290
-		$this->init();
291
-		$isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
292
-		$targetExists = $this->file_exists($path2);
293
-		$sameFodler = dirname($path1) === dirname($path2);
294
-
295
-		if ($targetExists || ($sameFodler && !$isPartFile)) {
296
-			if (!$this->isUpdatable('')) {
297
-				return false;
298
-			}
299
-		} else {
300
-			if (!$this->isCreatable('')) {
301
-				return false;
302
-			}
303
-		}
304
-
305
-		return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
306
-	}
307
-
308
-	/**
309
-	 * return mount point of share, relative to data/user/files
310
-	 *
311
-	 * @return string
312
-	 */
313
-	public function getMountPoint() {
314
-		return $this->superShare->getTarget();
315
-	}
316
-
317
-	/**
318
-	 * @param string $path
319
-	 */
320
-	public function setMountPoint($path) {
321
-		$this->superShare->setTarget($path);
322
-
323
-		foreach ($this->groupedShares as $share) {
324
-			$share->setTarget($path);
325
-		}
326
-	}
327
-
328
-	/**
329
-	 * get the user who shared the file
330
-	 *
331
-	 * @return string
332
-	 */
333
-	public function getSharedFrom() {
334
-		return $this->superShare->getShareOwner();
335
-	}
336
-
337
-	/**
338
-	 * @return \OCP\Share\IShare
339
-	 */
340
-	public function getShare() {
341
-		return $this->superShare;
342
-	}
343
-
344
-	/**
345
-	 * return share type, can be "file" or "folder"
346
-	 *
347
-	 * @return string
348
-	 */
349
-	public function getItemType() {
350
-		return $this->superShare->getNodeType();
351
-	}
352
-
353
-	/**
354
-	 * @param string $path
355
-	 * @param null $storage
356
-	 * @return Cache
357
-	 */
358
-	public function getCache($path = '', $storage = null) {
359
-		if ($this->cache) {
360
-			return $this->cache;
361
-		}
362
-		if (!$storage) {
363
-			$storage = $this;
364
-		}
365
-		if ($this->storage instanceof FailedStorage) {
366
-			return new FailedCache();
367
-		}
368
-		$this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare);
369
-		return $this->cache;
370
-	}
371
-
372
-	public function getScanner($path = '', $storage = null) {
373
-		if (!$storage) {
374
-			$storage = $this;
375
-		}
376
-		return new \OCA\Files_Sharing\Scanner($storage);
377
-	}
378
-
379
-	public function getPropagator($storage = null) {
380
-		if (isset($this->propagator)) {
381
-			return $this->propagator;
382
-		}
383
-
384
-		if (!$storage) {
385
-			$storage = $this;
386
-		}
387
-		$this->propagator = new \OCA\Files_Sharing\SharedPropagator($storage, \OC::$server->getDatabaseConnection());
388
-		return $this->propagator;
389
-	}
390
-
391
-	public function getOwner($path) {
392
-		return $this->superShare->getShareOwner();
393
-	}
394
-
395
-	/**
396
-	 * unshare complete storage, also the grouped shares
397
-	 *
398
-	 * @return bool
399
-	 */
400
-	public function unshareStorage() {
401
-		foreach ($this->groupedShares as $share) {
402
-			\OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
403
-		}
404
-		return true;
405
-	}
406
-
407
-	/**
408
-	 * @param string $path
409
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
410
-	 * @param \OCP\Lock\ILockingProvider $provider
411
-	 * @throws \OCP\Lock\LockedException
412
-	 */
413
-	public function acquireLock($path, $type, ILockingProvider $provider) {
414
-		/** @var \OCP\Files\Storage $targetStorage */
415
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
416
-		$targetStorage->acquireLock($targetInternalPath, $type, $provider);
417
-		// lock the parent folders of the owner when locking the share as recipient
418
-		if ($path === '') {
419
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
420
-			$this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
421
-		}
422
-	}
423
-
424
-	/**
425
-	 * @param string $path
426
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
427
-	 * @param \OCP\Lock\ILockingProvider $provider
428
-	 */
429
-	public function releaseLock($path, $type, ILockingProvider $provider) {
430
-		/** @var \OCP\Files\Storage $targetStorage */
431
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
432
-		$targetStorage->releaseLock($targetInternalPath, $type, $provider);
433
-		// unlock the parent folders of the owner when unlocking the share as recipient
434
-		if ($path === '') {
435
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
436
-			$this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
437
-		}
438
-	}
439
-
440
-	/**
441
-	 * @param string $path
442
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
443
-	 * @param \OCP\Lock\ILockingProvider $provider
444
-	 */
445
-	public function changeLock($path, $type, ILockingProvider $provider) {
446
-		/** @var \OCP\Files\Storage $targetStorage */
447
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
448
-		$targetStorage->changeLock($targetInternalPath, $type, $provider);
449
-	}
450
-
451
-	/**
452
-	 * @return array [ available, last_checked ]
453
-	 */
454
-	public function getAvailability() {
455
-		// shares do not participate in availability logic
456
-		return [
457
-			'available' => true,
458
-			'last_checked' => 0
459
-		];
460
-	}
461
-
462
-	/**
463
-	 * @param bool $available
464
-	 */
465
-	public function setAvailability($available) {
466
-		// shares do not participate in availability logic
467
-	}
468
-
469
-	public function getSourceStorage() {
470
-		$this->init();
471
-		return $this->nonMaskedStorage;
472
-	}
473
-
474
-	public function getWrapperStorage() {
475
-		$this->init();
476
-		return $this->storage;
477
-	}
478
-
479
-	public function file_get_contents($path) {
480
-		$info = [
481
-			'target' => $this->getMountPoint() . '/' . $path,
482
-			'source' => $this->getUnjailedPath($path),
483
-		];
484
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
485
-		return parent::file_get_contents($path);
486
-	}
487
-
488
-	public function file_put_contents($path, $data) {
489
-		$info = [
490
-			'target' => $this->getMountPoint() . '/' . $path,
491
-			'source' => $this->getUnjailedPath($path),
492
-		];
493
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
494
-		return parent::file_put_contents($path, $data);
495
-	}
496
-
497
-	public function setMountOptions(array $options) {
498
-		$this->mountOptions = $options;
499
-	}
50
+    /** @var \OCP\Share\IShare */
51
+    private $superShare;
52
+
53
+    /** @var \OCP\Share\IShare[] */
54
+    private $groupedShares;
55
+
56
+    /**
57
+     * @var \OC\Files\View
58
+     */
59
+    private $ownerView;
60
+
61
+    private $initialized = false;
62
+
63
+    /**
64
+     * @var ICacheEntry
65
+     */
66
+    private $sourceRootInfo;
67
+
68
+    /** @var string */
69
+    private $user;
70
+
71
+    /**
72
+     * @var \OCP\ILogger
73
+     */
74
+    private $logger;
75
+
76
+    /** @var  IStorage */
77
+    private $nonMaskedStorage;
78
+
79
+    private $options;
80
+
81
+    public function __construct($arguments) {
82
+        $this->ownerView = $arguments['ownerView'];
83
+        $this->logger = \OC::$server->getLogger();
84
+
85
+        $this->superShare = $arguments['superShare'];
86
+        $this->groupedShares = $arguments['groupedShares'];
87
+
88
+        $this->user = $arguments['user'];
89
+
90
+        parent::__construct([
91
+            'storage' => null,
92
+            'root' => null,
93
+        ]);
94
+    }
95
+
96
+    /**
97
+     * @return ICacheEntry
98
+     */
99
+    private function getSourceRootInfo() {
100
+        if (is_null($this->sourceRootInfo)) {
101
+            if (is_null($this->superShare->getNodeCacheEntry())) {
102
+                $this->init();
103
+                $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
104
+            } else {
105
+                $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
106
+            }
107
+        }
108
+        return $this->sourceRootInfo;
109
+    }
110
+
111
+    private function init() {
112
+        if ($this->initialized) {
113
+            return;
114
+        }
115
+        $this->initialized = true;
116
+        try {
117
+            Filesystem::initMountPoints($this->superShare->getShareOwner());
118
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
119
+            list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
120
+            $this->storage = new PermissionsMask([
121
+                'storage' => $this->nonMaskedStorage,
122
+                'mask' => $this->superShare->getPermissions()
123
+            ]);
124
+        } catch (NotFoundException $e) {
125
+            // original file not accessible or deleted, set FailedStorage
126
+            $this->storage = new FailedStorage(['exception' => $e]);
127
+            $this->cache = new FailedCache();
128
+            $this->rootPath = '';
129
+        } catch (NoUserException $e) {
130
+            // sharer user deleted, set FailedStorage
131
+            $this->storage = new FailedStorage(['exception' => $e]);
132
+            $this->cache = new FailedCache();
133
+            $this->rootPath = '';
134
+        } catch (\Exception $e) {
135
+            $this->storage = new FailedStorage(['exception' => $e]);
136
+            $this->cache = new FailedCache();
137
+            $this->rootPath = '';
138
+            $this->logger->logException($e);
139
+        }
140
+
141
+        if (!$this->nonMaskedStorage) {
142
+            $this->nonMaskedStorage = $this->storage;
143
+        }
144
+    }
145
+
146
+    /**
147
+     * @inheritdoc
148
+     */
149
+    public function instanceOfStorage($class) {
150
+        if ($class === '\OC\Files\Storage\Common') {
151
+            return true;
152
+        }
153
+        if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
154
+            return false;
155
+        }
156
+        return parent::instanceOfStorage($class);
157
+    }
158
+
159
+    /**
160
+     * @return string
161
+     */
162
+    public function getShareId() {
163
+        return $this->superShare->getId();
164
+    }
165
+
166
+    private function isValid() {
167
+        return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
168
+    }
169
+
170
+    /**
171
+     * get id of the mount point
172
+     *
173
+     * @return string
174
+     */
175
+    public function getId() {
176
+        return 'shared::' . $this->getMountPoint();
177
+    }
178
+
179
+    /**
180
+     * Get the permissions granted for a shared file
181
+     *
182
+     * @param string $target Shared target file path
183
+     * @return int CRUDS permissions granted
184
+     */
185
+    public function getPermissions($target = '') {
186
+        if (!$this->isValid()) {
187
+            return 0;
188
+        }
189
+        $permissions = $this->superShare->getPermissions();
190
+        // part files and the mount point always have delete permissions
191
+        if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
192
+            $permissions |= \OCP\Constants::PERMISSION_DELETE;
193
+        }
194
+
195
+        if (\OCP\Util::isSharingDisabledForUser()) {
196
+            $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
197
+        }
198
+
199
+        return $permissions;
200
+    }
201
+
202
+    public function isCreatable($path) {
203
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
204
+    }
205
+
206
+    public function isReadable($path) {
207
+        if (!$this->isValid()) {
208
+            return false;
209
+        }
210
+        if (!$this->file_exists($path)) {
211
+            return false;
212
+        }
213
+        /** @var IStorage $storage */
214
+        /** @var string $internalPath */
215
+        list($storage, $internalPath) = $this->resolvePath($path);
216
+        return $storage->isReadable($internalPath);
217
+    }
218
+
219
+    public function isUpdatable($path) {
220
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
221
+    }
222
+
223
+    public function isDeletable($path) {
224
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
225
+    }
226
+
227
+    public function isSharable($path) {
228
+        if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
229
+            return false;
230
+        }
231
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
232
+    }
233
+
234
+    public function fopen($path, $mode) {
235
+        if ($source = $this->getUnjailedPath($path)) {
236
+            switch ($mode) {
237
+                case 'r+':
238
+                case 'rb+':
239
+                case 'w+':
240
+                case 'wb+':
241
+                case 'x+':
242
+                case 'xb+':
243
+                case 'a+':
244
+                case 'ab+':
245
+                case 'w':
246
+                case 'wb':
247
+                case 'x':
248
+                case 'xb':
249
+                case 'a':
250
+                case 'ab':
251
+                    $creatable = $this->isCreatable($path);
252
+                    $updatable = $this->isUpdatable($path);
253
+                    // if neither permissions given, no need to continue
254
+                    if (!$creatable && !$updatable) {
255
+                        return false;
256
+                    }
257
+
258
+                    $exists = $this->file_exists($path);
259
+                    // if a file exists, updatable permissions are required
260
+                    if ($exists && !$updatable) {
261
+                        return false;
262
+                    }
263
+
264
+                    // part file is allowed if !$creatable but the final file is $updatable
265
+                    if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
266
+                        if (!$exists && !$creatable) {
267
+                            return false;
268
+                        }
269
+                    }
270
+            }
271
+            $info = array(
272
+                'target' => $this->getMountPoint() . $path,
273
+                'source' => $source,
274
+                'mode' => $mode,
275
+            );
276
+            \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
277
+            return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
278
+        }
279
+        return false;
280
+    }
281
+
282
+    /**
283
+     * see http://php.net/manual/en/function.rename.php
284
+     *
285
+     * @param string $path1
286
+     * @param string $path2
287
+     * @return bool
288
+     */
289
+    public function rename($path1, $path2) {
290
+        $this->init();
291
+        $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
292
+        $targetExists = $this->file_exists($path2);
293
+        $sameFodler = dirname($path1) === dirname($path2);
294
+
295
+        if ($targetExists || ($sameFodler && !$isPartFile)) {
296
+            if (!$this->isUpdatable('')) {
297
+                return false;
298
+            }
299
+        } else {
300
+            if (!$this->isCreatable('')) {
301
+                return false;
302
+            }
303
+        }
304
+
305
+        return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
306
+    }
307
+
308
+    /**
309
+     * return mount point of share, relative to data/user/files
310
+     *
311
+     * @return string
312
+     */
313
+    public function getMountPoint() {
314
+        return $this->superShare->getTarget();
315
+    }
316
+
317
+    /**
318
+     * @param string $path
319
+     */
320
+    public function setMountPoint($path) {
321
+        $this->superShare->setTarget($path);
322
+
323
+        foreach ($this->groupedShares as $share) {
324
+            $share->setTarget($path);
325
+        }
326
+    }
327
+
328
+    /**
329
+     * get the user who shared the file
330
+     *
331
+     * @return string
332
+     */
333
+    public function getSharedFrom() {
334
+        return $this->superShare->getShareOwner();
335
+    }
336
+
337
+    /**
338
+     * @return \OCP\Share\IShare
339
+     */
340
+    public function getShare() {
341
+        return $this->superShare;
342
+    }
343
+
344
+    /**
345
+     * return share type, can be "file" or "folder"
346
+     *
347
+     * @return string
348
+     */
349
+    public function getItemType() {
350
+        return $this->superShare->getNodeType();
351
+    }
352
+
353
+    /**
354
+     * @param string $path
355
+     * @param null $storage
356
+     * @return Cache
357
+     */
358
+    public function getCache($path = '', $storage = null) {
359
+        if ($this->cache) {
360
+            return $this->cache;
361
+        }
362
+        if (!$storage) {
363
+            $storage = $this;
364
+        }
365
+        if ($this->storage instanceof FailedStorage) {
366
+            return new FailedCache();
367
+        }
368
+        $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare);
369
+        return $this->cache;
370
+    }
371
+
372
+    public function getScanner($path = '', $storage = null) {
373
+        if (!$storage) {
374
+            $storage = $this;
375
+        }
376
+        return new \OCA\Files_Sharing\Scanner($storage);
377
+    }
378
+
379
+    public function getPropagator($storage = null) {
380
+        if (isset($this->propagator)) {
381
+            return $this->propagator;
382
+        }
383
+
384
+        if (!$storage) {
385
+            $storage = $this;
386
+        }
387
+        $this->propagator = new \OCA\Files_Sharing\SharedPropagator($storage, \OC::$server->getDatabaseConnection());
388
+        return $this->propagator;
389
+    }
390
+
391
+    public function getOwner($path) {
392
+        return $this->superShare->getShareOwner();
393
+    }
394
+
395
+    /**
396
+     * unshare complete storage, also the grouped shares
397
+     *
398
+     * @return bool
399
+     */
400
+    public function unshareStorage() {
401
+        foreach ($this->groupedShares as $share) {
402
+            \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
403
+        }
404
+        return true;
405
+    }
406
+
407
+    /**
408
+     * @param string $path
409
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
410
+     * @param \OCP\Lock\ILockingProvider $provider
411
+     * @throws \OCP\Lock\LockedException
412
+     */
413
+    public function acquireLock($path, $type, ILockingProvider $provider) {
414
+        /** @var \OCP\Files\Storage $targetStorage */
415
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
416
+        $targetStorage->acquireLock($targetInternalPath, $type, $provider);
417
+        // lock the parent folders of the owner when locking the share as recipient
418
+        if ($path === '') {
419
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
420
+            $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
421
+        }
422
+    }
423
+
424
+    /**
425
+     * @param string $path
426
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
427
+     * @param \OCP\Lock\ILockingProvider $provider
428
+     */
429
+    public function releaseLock($path, $type, ILockingProvider $provider) {
430
+        /** @var \OCP\Files\Storage $targetStorage */
431
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
432
+        $targetStorage->releaseLock($targetInternalPath, $type, $provider);
433
+        // unlock the parent folders of the owner when unlocking the share as recipient
434
+        if ($path === '') {
435
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
436
+            $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
437
+        }
438
+    }
439
+
440
+    /**
441
+     * @param string $path
442
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
443
+     * @param \OCP\Lock\ILockingProvider $provider
444
+     */
445
+    public function changeLock($path, $type, ILockingProvider $provider) {
446
+        /** @var \OCP\Files\Storage $targetStorage */
447
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
448
+        $targetStorage->changeLock($targetInternalPath, $type, $provider);
449
+    }
450
+
451
+    /**
452
+     * @return array [ available, last_checked ]
453
+     */
454
+    public function getAvailability() {
455
+        // shares do not participate in availability logic
456
+        return [
457
+            'available' => true,
458
+            'last_checked' => 0
459
+        ];
460
+    }
461
+
462
+    /**
463
+     * @param bool $available
464
+     */
465
+    public function setAvailability($available) {
466
+        // shares do not participate in availability logic
467
+    }
468
+
469
+    public function getSourceStorage() {
470
+        $this->init();
471
+        return $this->nonMaskedStorage;
472
+    }
473
+
474
+    public function getWrapperStorage() {
475
+        $this->init();
476
+        return $this->storage;
477
+    }
478
+
479
+    public function file_get_contents($path) {
480
+        $info = [
481
+            'target' => $this->getMountPoint() . '/' . $path,
482
+            'source' => $this->getUnjailedPath($path),
483
+        ];
484
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
485
+        return parent::file_get_contents($path);
486
+    }
487
+
488
+    public function file_put_contents($path, $data) {
489
+        $info = [
490
+            'target' => $this->getMountPoint() . '/' . $path,
491
+            'source' => $this->getUnjailedPath($path),
492
+        ];
493
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
494
+        return parent::file_put_contents($path, $data);
495
+    }
496
+
497
+    public function setMountOptions(array $options) {
498
+        $this->mountOptions = $options;
499
+    }
500 500
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
 	 * @return string
174 174
 	 */
175 175
 	public function getId() {
176
-		return 'shared::' . $this->getMountPoint();
176
+		return 'shared::'.$this->getMountPoint();
177 177
 	}
178 178
 
179 179
 	/**
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 					}
270 270
 			}
271 271
 			$info = array(
272
-				'target' => $this->getMountPoint() . $path,
272
+				'target' => $this->getMountPoint().$path,
273 273
 				'source' => $source,
274 274
 				'mode' => $mode,
275 275
 			);
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
 
479 479
 	public function file_get_contents($path) {
480 480
 		$info = [
481
-			'target' => $this->getMountPoint() . '/' . $path,
481
+			'target' => $this->getMountPoint().'/'.$path,
482 482
 			'source' => $this->getUnjailedPath($path),
483 483
 		];
484 484
 		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
@@ -487,7 +487,7 @@  discard block
 block discarded – undo
487 487
 
488 488
 	public function file_put_contents($path, $data) {
489 489
 		$info = [
490
-			'target' => $this->getMountPoint() . '/' . $path,
490
+			'target' => $this->getMountPoint().'/'.$path,
491 491
 			'source' => $this->getUnjailedPath($path),
492 492
 		];
493 493
 		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
Please login to merge, or discard this patch.
lib/private/Files/Storage/Wrapper/Jail.php 1 patch
Indentation   +455 added lines, -455 removed lines patch added patch discarded remove patch
@@ -33,459 +33,459 @@
 block discarded – undo
33 33
  * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
34 34
  */
35 35
 class Jail extends Wrapper {
36
-	/**
37
-	 * @var string
38
-	 */
39
-	protected $rootPath;
40
-
41
-	/**
42
-	 * @param array $arguments ['storage' => $storage, 'mask' => $root]
43
-	 *
44
-	 * $storage: The storage that will be wrapper
45
-	 * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
46
-	 */
47
-	public function __construct($arguments) {
48
-		parent::__construct($arguments);
49
-		$this->rootPath = $arguments['root'];
50
-	}
51
-
52
-	public function getUnjailedPath($path) {
53
-		if ($path === '') {
54
-			return $this->rootPath;
55
-		} else {
56
-			return $this->rootPath . '/' . $path;
57
-		}
58
-	}
59
-
60
-	public function getId() {
61
-		return parent::getId();
62
-	}
63
-
64
-	/**
65
-	 * see http://php.net/manual/en/function.mkdir.php
66
-	 *
67
-	 * @param string $path
68
-	 * @return bool
69
-	 */
70
-	public function mkdir($path) {
71
-		return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
72
-	}
73
-
74
-	/**
75
-	 * see http://php.net/manual/en/function.rmdir.php
76
-	 *
77
-	 * @param string $path
78
-	 * @return bool
79
-	 */
80
-	public function rmdir($path) {
81
-		return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
82
-	}
83
-
84
-	/**
85
-	 * see http://php.net/manual/en/function.opendir.php
86
-	 *
87
-	 * @param string $path
88
-	 * @return resource
89
-	 */
90
-	public function opendir($path) {
91
-		return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
92
-	}
93
-
94
-	/**
95
-	 * see http://php.net/manual/en/function.is_dir.php
96
-	 *
97
-	 * @param string $path
98
-	 * @return bool
99
-	 */
100
-	public function is_dir($path) {
101
-		return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
102
-	}
103
-
104
-	/**
105
-	 * see http://php.net/manual/en/function.is_file.php
106
-	 *
107
-	 * @param string $path
108
-	 * @return bool
109
-	 */
110
-	public function is_file($path) {
111
-		return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
112
-	}
113
-
114
-	/**
115
-	 * see http://php.net/manual/en/function.stat.php
116
-	 * only the following keys are required in the result: size and mtime
117
-	 *
118
-	 * @param string $path
119
-	 * @return array
120
-	 */
121
-	public function stat($path) {
122
-		return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
123
-	}
124
-
125
-	/**
126
-	 * see http://php.net/manual/en/function.filetype.php
127
-	 *
128
-	 * @param string $path
129
-	 * @return bool
130
-	 */
131
-	public function filetype($path) {
132
-		return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
133
-	}
134
-
135
-	/**
136
-	 * see http://php.net/manual/en/function.filesize.php
137
-	 * The result for filesize when called on a folder is required to be 0
138
-	 *
139
-	 * @param string $path
140
-	 * @return int
141
-	 */
142
-	public function filesize($path) {
143
-		return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
144
-	}
145
-
146
-	/**
147
-	 * check if a file can be created in $path
148
-	 *
149
-	 * @param string $path
150
-	 * @return bool
151
-	 */
152
-	public function isCreatable($path) {
153
-		return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
154
-	}
155
-
156
-	/**
157
-	 * check if a file can be read
158
-	 *
159
-	 * @param string $path
160
-	 * @return bool
161
-	 */
162
-	public function isReadable($path) {
163
-		return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
164
-	}
165
-
166
-	/**
167
-	 * check if a file can be written to
168
-	 *
169
-	 * @param string $path
170
-	 * @return bool
171
-	 */
172
-	public function isUpdatable($path) {
173
-		return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
174
-	}
175
-
176
-	/**
177
-	 * check if a file can be deleted
178
-	 *
179
-	 * @param string $path
180
-	 * @return bool
181
-	 */
182
-	public function isDeletable($path) {
183
-		return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
184
-	}
185
-
186
-	/**
187
-	 * check if a file can be shared
188
-	 *
189
-	 * @param string $path
190
-	 * @return bool
191
-	 */
192
-	public function isSharable($path) {
193
-		return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
194
-	}
195
-
196
-	/**
197
-	 * get the full permissions of a path.
198
-	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
199
-	 *
200
-	 * @param string $path
201
-	 * @return int
202
-	 */
203
-	public function getPermissions($path) {
204
-		return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
205
-	}
206
-
207
-	/**
208
-	 * see http://php.net/manual/en/function.file_exists.php
209
-	 *
210
-	 * @param string $path
211
-	 * @return bool
212
-	 */
213
-	public function file_exists($path) {
214
-		return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
215
-	}
216
-
217
-	/**
218
-	 * see http://php.net/manual/en/function.filemtime.php
219
-	 *
220
-	 * @param string $path
221
-	 * @return int
222
-	 */
223
-	public function filemtime($path) {
224
-		return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
225
-	}
226
-
227
-	/**
228
-	 * see http://php.net/manual/en/function.file_get_contents.php
229
-	 *
230
-	 * @param string $path
231
-	 * @return string
232
-	 */
233
-	public function file_get_contents($path) {
234
-		return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
235
-	}
236
-
237
-	/**
238
-	 * see http://php.net/manual/en/function.file_put_contents.php
239
-	 *
240
-	 * @param string $path
241
-	 * @param string $data
242
-	 * @return bool
243
-	 */
244
-	public function file_put_contents($path, $data) {
245
-		return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
246
-	}
247
-
248
-	/**
249
-	 * see http://php.net/manual/en/function.unlink.php
250
-	 *
251
-	 * @param string $path
252
-	 * @return bool
253
-	 */
254
-	public function unlink($path) {
255
-		return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
256
-	}
257
-
258
-	/**
259
-	 * see http://php.net/manual/en/function.rename.php
260
-	 *
261
-	 * @param string $path1
262
-	 * @param string $path2
263
-	 * @return bool
264
-	 */
265
-	public function rename($path1, $path2) {
266
-		return $this->getWrapperStorage()->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
267
-	}
268
-
269
-	/**
270
-	 * see http://php.net/manual/en/function.copy.php
271
-	 *
272
-	 * @param string $path1
273
-	 * @param string $path2
274
-	 * @return bool
275
-	 */
276
-	public function copy($path1, $path2) {
277
-		return $this->getWrapperStorage()->copy($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
278
-	}
279
-
280
-	/**
281
-	 * see http://php.net/manual/en/function.fopen.php
282
-	 *
283
-	 * @param string $path
284
-	 * @param string $mode
285
-	 * @return resource
286
-	 */
287
-	public function fopen($path, $mode) {
288
-		return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
289
-	}
290
-
291
-	/**
292
-	 * get the mimetype for a file or folder
293
-	 * The mimetype for a folder is required to be "httpd/unix-directory"
294
-	 *
295
-	 * @param string $path
296
-	 * @return string
297
-	 */
298
-	public function getMimeType($path) {
299
-		return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
300
-	}
301
-
302
-	/**
303
-	 * see http://php.net/manual/en/function.hash.php
304
-	 *
305
-	 * @param string $type
306
-	 * @param string $path
307
-	 * @param bool $raw
308
-	 * @return string
309
-	 */
310
-	public function hash($type, $path, $raw = false) {
311
-		return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
312
-	}
313
-
314
-	/**
315
-	 * see http://php.net/manual/en/function.free_space.php
316
-	 *
317
-	 * @param string $path
318
-	 * @return int
319
-	 */
320
-	public function free_space($path) {
321
-		return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
322
-	}
323
-
324
-	/**
325
-	 * search for occurrences of $query in file names
326
-	 *
327
-	 * @param string $query
328
-	 * @return array
329
-	 */
330
-	public function search($query) {
331
-		return $this->getWrapperStorage()->search($query);
332
-	}
333
-
334
-	/**
335
-	 * see http://php.net/manual/en/function.touch.php
336
-	 * If the backend does not support the operation, false should be returned
337
-	 *
338
-	 * @param string $path
339
-	 * @param int $mtime
340
-	 * @return bool
341
-	 */
342
-	public function touch($path, $mtime = null) {
343
-		return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
344
-	}
345
-
346
-	/**
347
-	 * get the path to a local version of the file.
348
-	 * The local version of the file can be temporary and doesn't have to be persistent across requests
349
-	 *
350
-	 * @param string $path
351
-	 * @return string
352
-	 */
353
-	public function getLocalFile($path) {
354
-		return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
355
-	}
356
-
357
-	/**
358
-	 * check if a file or folder has been updated since $time
359
-	 *
360
-	 * @param string $path
361
-	 * @param int $time
362
-	 * @return bool
363
-	 *
364
-	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
365
-	 * returning true for other changes in the folder is optional
366
-	 */
367
-	public function hasUpdated($path, $time) {
368
-		return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
369
-	}
370
-
371
-	/**
372
-	 * get a cache instance for the storage
373
-	 *
374
-	 * @param string $path
375
-	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
376
-	 * @return \OC\Files\Cache\Cache
377
-	 */
378
-	public function getCache($path = '', $storage = null) {
379
-		if (!$storage) {
380
-			$storage = $this->getWrapperStorage();
381
-		}
382
-		$sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path), $storage);
383
-		return new CacheJail($sourceCache, $this->rootPath);
384
-	}
385
-
386
-	/**
387
-	 * get the user id of the owner of a file or folder
388
-	 *
389
-	 * @param string $path
390
-	 * @return string
391
-	 */
392
-	public function getOwner($path) {
393
-		return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
394
-	}
395
-
396
-	/**
397
-	 * get a watcher instance for the cache
398
-	 *
399
-	 * @param string $path
400
-	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
401
-	 * @return \OC\Files\Cache\Watcher
402
-	 */
403
-	public function getWatcher($path = '', $storage = null) {
404
-		if (!$storage) {
405
-			$storage = $this;
406
-		}
407
-		return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage);
408
-	}
409
-
410
-	/**
411
-	 * get the ETag for a file or folder
412
-	 *
413
-	 * @param string $path
414
-	 * @return string
415
-	 */
416
-	public function getETag($path) {
417
-		return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
418
-	}
419
-
420
-	/**
421
-	 * @param string $path
422
-	 * @return array
423
-	 */
424
-	public function getMetaData($path) {
425
-		return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
426
-	}
427
-
428
-	/**
429
-	 * @param string $path
430
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
431
-	 * @param \OCP\Lock\ILockingProvider $provider
432
-	 * @throws \OCP\Lock\LockedException
433
-	 */
434
-	public function acquireLock($path, $type, ILockingProvider $provider) {
435
-		$this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
436
-	}
437
-
438
-	/**
439
-	 * @param string $path
440
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
441
-	 * @param \OCP\Lock\ILockingProvider $provider
442
-	 */
443
-	public function releaseLock($path, $type, ILockingProvider $provider) {
444
-		$this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
445
-	}
446
-
447
-	/**
448
-	 * @param string $path
449
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
450
-	 * @param \OCP\Lock\ILockingProvider $provider
451
-	 */
452
-	public function changeLock($path, $type, ILockingProvider $provider) {
453
-		$this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
454
-	}
455
-
456
-	/**
457
-	 * Resolve the path for the source of the share
458
-	 *
459
-	 * @param string $path
460
-	 * @return array
461
-	 */
462
-	public function resolvePath($path) {
463
-		return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
464
-	}
465
-
466
-	/**
467
-	 * @param \OCP\Files\Storage $sourceStorage
468
-	 * @param string $sourceInternalPath
469
-	 * @param string $targetInternalPath
470
-	 * @return bool
471
-	 */
472
-	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
473
-		if ($sourceStorage === $this) {
474
-			return $this->copy($sourceInternalPath, $targetInternalPath);
475
-		}
476
-		return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
477
-	}
478
-
479
-	/**
480
-	 * @param \OCP\Files\Storage $sourceStorage
481
-	 * @param string $sourceInternalPath
482
-	 * @param string $targetInternalPath
483
-	 * @return bool
484
-	 */
485
-	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
486
-		if ($sourceStorage === $this) {
487
-			return $this->rename($sourceInternalPath, $targetInternalPath);
488
-		}
489
-		return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
490
-	}
36
+    /**
37
+     * @var string
38
+     */
39
+    protected $rootPath;
40
+
41
+    /**
42
+     * @param array $arguments ['storage' => $storage, 'mask' => $root]
43
+     *
44
+     * $storage: The storage that will be wrapper
45
+     * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
46
+     */
47
+    public function __construct($arguments) {
48
+        parent::__construct($arguments);
49
+        $this->rootPath = $arguments['root'];
50
+    }
51
+
52
+    public function getUnjailedPath($path) {
53
+        if ($path === '') {
54
+            return $this->rootPath;
55
+        } else {
56
+            return $this->rootPath . '/' . $path;
57
+        }
58
+    }
59
+
60
+    public function getId() {
61
+        return parent::getId();
62
+    }
63
+
64
+    /**
65
+     * see http://php.net/manual/en/function.mkdir.php
66
+     *
67
+     * @param string $path
68
+     * @return bool
69
+     */
70
+    public function mkdir($path) {
71
+        return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
72
+    }
73
+
74
+    /**
75
+     * see http://php.net/manual/en/function.rmdir.php
76
+     *
77
+     * @param string $path
78
+     * @return bool
79
+     */
80
+    public function rmdir($path) {
81
+        return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
82
+    }
83
+
84
+    /**
85
+     * see http://php.net/manual/en/function.opendir.php
86
+     *
87
+     * @param string $path
88
+     * @return resource
89
+     */
90
+    public function opendir($path) {
91
+        return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
92
+    }
93
+
94
+    /**
95
+     * see http://php.net/manual/en/function.is_dir.php
96
+     *
97
+     * @param string $path
98
+     * @return bool
99
+     */
100
+    public function is_dir($path) {
101
+        return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
102
+    }
103
+
104
+    /**
105
+     * see http://php.net/manual/en/function.is_file.php
106
+     *
107
+     * @param string $path
108
+     * @return bool
109
+     */
110
+    public function is_file($path) {
111
+        return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
112
+    }
113
+
114
+    /**
115
+     * see http://php.net/manual/en/function.stat.php
116
+     * only the following keys are required in the result: size and mtime
117
+     *
118
+     * @param string $path
119
+     * @return array
120
+     */
121
+    public function stat($path) {
122
+        return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
123
+    }
124
+
125
+    /**
126
+     * see http://php.net/manual/en/function.filetype.php
127
+     *
128
+     * @param string $path
129
+     * @return bool
130
+     */
131
+    public function filetype($path) {
132
+        return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
133
+    }
134
+
135
+    /**
136
+     * see http://php.net/manual/en/function.filesize.php
137
+     * The result for filesize when called on a folder is required to be 0
138
+     *
139
+     * @param string $path
140
+     * @return int
141
+     */
142
+    public function filesize($path) {
143
+        return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
144
+    }
145
+
146
+    /**
147
+     * check if a file can be created in $path
148
+     *
149
+     * @param string $path
150
+     * @return bool
151
+     */
152
+    public function isCreatable($path) {
153
+        return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
154
+    }
155
+
156
+    /**
157
+     * check if a file can be read
158
+     *
159
+     * @param string $path
160
+     * @return bool
161
+     */
162
+    public function isReadable($path) {
163
+        return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
164
+    }
165
+
166
+    /**
167
+     * check if a file can be written to
168
+     *
169
+     * @param string $path
170
+     * @return bool
171
+     */
172
+    public function isUpdatable($path) {
173
+        return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
174
+    }
175
+
176
+    /**
177
+     * check if a file can be deleted
178
+     *
179
+     * @param string $path
180
+     * @return bool
181
+     */
182
+    public function isDeletable($path) {
183
+        return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
184
+    }
185
+
186
+    /**
187
+     * check if a file can be shared
188
+     *
189
+     * @param string $path
190
+     * @return bool
191
+     */
192
+    public function isSharable($path) {
193
+        return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
194
+    }
195
+
196
+    /**
197
+     * get the full permissions of a path.
198
+     * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
199
+     *
200
+     * @param string $path
201
+     * @return int
202
+     */
203
+    public function getPermissions($path) {
204
+        return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
205
+    }
206
+
207
+    /**
208
+     * see http://php.net/manual/en/function.file_exists.php
209
+     *
210
+     * @param string $path
211
+     * @return bool
212
+     */
213
+    public function file_exists($path) {
214
+        return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
215
+    }
216
+
217
+    /**
218
+     * see http://php.net/manual/en/function.filemtime.php
219
+     *
220
+     * @param string $path
221
+     * @return int
222
+     */
223
+    public function filemtime($path) {
224
+        return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
225
+    }
226
+
227
+    /**
228
+     * see http://php.net/manual/en/function.file_get_contents.php
229
+     *
230
+     * @param string $path
231
+     * @return string
232
+     */
233
+    public function file_get_contents($path) {
234
+        return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
235
+    }
236
+
237
+    /**
238
+     * see http://php.net/manual/en/function.file_put_contents.php
239
+     *
240
+     * @param string $path
241
+     * @param string $data
242
+     * @return bool
243
+     */
244
+    public function file_put_contents($path, $data) {
245
+        return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
246
+    }
247
+
248
+    /**
249
+     * see http://php.net/manual/en/function.unlink.php
250
+     *
251
+     * @param string $path
252
+     * @return bool
253
+     */
254
+    public function unlink($path) {
255
+        return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
256
+    }
257
+
258
+    /**
259
+     * see http://php.net/manual/en/function.rename.php
260
+     *
261
+     * @param string $path1
262
+     * @param string $path2
263
+     * @return bool
264
+     */
265
+    public function rename($path1, $path2) {
266
+        return $this->getWrapperStorage()->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
267
+    }
268
+
269
+    /**
270
+     * see http://php.net/manual/en/function.copy.php
271
+     *
272
+     * @param string $path1
273
+     * @param string $path2
274
+     * @return bool
275
+     */
276
+    public function copy($path1, $path2) {
277
+        return $this->getWrapperStorage()->copy($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
278
+    }
279
+
280
+    /**
281
+     * see http://php.net/manual/en/function.fopen.php
282
+     *
283
+     * @param string $path
284
+     * @param string $mode
285
+     * @return resource
286
+     */
287
+    public function fopen($path, $mode) {
288
+        return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
289
+    }
290
+
291
+    /**
292
+     * get the mimetype for a file or folder
293
+     * The mimetype for a folder is required to be "httpd/unix-directory"
294
+     *
295
+     * @param string $path
296
+     * @return string
297
+     */
298
+    public function getMimeType($path) {
299
+        return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
300
+    }
301
+
302
+    /**
303
+     * see http://php.net/manual/en/function.hash.php
304
+     *
305
+     * @param string $type
306
+     * @param string $path
307
+     * @param bool $raw
308
+     * @return string
309
+     */
310
+    public function hash($type, $path, $raw = false) {
311
+        return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
312
+    }
313
+
314
+    /**
315
+     * see http://php.net/manual/en/function.free_space.php
316
+     *
317
+     * @param string $path
318
+     * @return int
319
+     */
320
+    public function free_space($path) {
321
+        return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
322
+    }
323
+
324
+    /**
325
+     * search for occurrences of $query in file names
326
+     *
327
+     * @param string $query
328
+     * @return array
329
+     */
330
+    public function search($query) {
331
+        return $this->getWrapperStorage()->search($query);
332
+    }
333
+
334
+    /**
335
+     * see http://php.net/manual/en/function.touch.php
336
+     * If the backend does not support the operation, false should be returned
337
+     *
338
+     * @param string $path
339
+     * @param int $mtime
340
+     * @return bool
341
+     */
342
+    public function touch($path, $mtime = null) {
343
+        return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
344
+    }
345
+
346
+    /**
347
+     * get the path to a local version of the file.
348
+     * The local version of the file can be temporary and doesn't have to be persistent across requests
349
+     *
350
+     * @param string $path
351
+     * @return string
352
+     */
353
+    public function getLocalFile($path) {
354
+        return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
355
+    }
356
+
357
+    /**
358
+     * check if a file or folder has been updated since $time
359
+     *
360
+     * @param string $path
361
+     * @param int $time
362
+     * @return bool
363
+     *
364
+     * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
365
+     * returning true for other changes in the folder is optional
366
+     */
367
+    public function hasUpdated($path, $time) {
368
+        return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
369
+    }
370
+
371
+    /**
372
+     * get a cache instance for the storage
373
+     *
374
+     * @param string $path
375
+     * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
376
+     * @return \OC\Files\Cache\Cache
377
+     */
378
+    public function getCache($path = '', $storage = null) {
379
+        if (!$storage) {
380
+            $storage = $this->getWrapperStorage();
381
+        }
382
+        $sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path), $storage);
383
+        return new CacheJail($sourceCache, $this->rootPath);
384
+    }
385
+
386
+    /**
387
+     * get the user id of the owner of a file or folder
388
+     *
389
+     * @param string $path
390
+     * @return string
391
+     */
392
+    public function getOwner($path) {
393
+        return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
394
+    }
395
+
396
+    /**
397
+     * get a watcher instance for the cache
398
+     *
399
+     * @param string $path
400
+     * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
401
+     * @return \OC\Files\Cache\Watcher
402
+     */
403
+    public function getWatcher($path = '', $storage = null) {
404
+        if (!$storage) {
405
+            $storage = $this;
406
+        }
407
+        return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage);
408
+    }
409
+
410
+    /**
411
+     * get the ETag for a file or folder
412
+     *
413
+     * @param string $path
414
+     * @return string
415
+     */
416
+    public function getETag($path) {
417
+        return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
418
+    }
419
+
420
+    /**
421
+     * @param string $path
422
+     * @return array
423
+     */
424
+    public function getMetaData($path) {
425
+        return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
426
+    }
427
+
428
+    /**
429
+     * @param string $path
430
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
431
+     * @param \OCP\Lock\ILockingProvider $provider
432
+     * @throws \OCP\Lock\LockedException
433
+     */
434
+    public function acquireLock($path, $type, ILockingProvider $provider) {
435
+        $this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
436
+    }
437
+
438
+    /**
439
+     * @param string $path
440
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
441
+     * @param \OCP\Lock\ILockingProvider $provider
442
+     */
443
+    public function releaseLock($path, $type, ILockingProvider $provider) {
444
+        $this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
445
+    }
446
+
447
+    /**
448
+     * @param string $path
449
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
450
+     * @param \OCP\Lock\ILockingProvider $provider
451
+     */
452
+    public function changeLock($path, $type, ILockingProvider $provider) {
453
+        $this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
454
+    }
455
+
456
+    /**
457
+     * Resolve the path for the source of the share
458
+     *
459
+     * @param string $path
460
+     * @return array
461
+     */
462
+    public function resolvePath($path) {
463
+        return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
464
+    }
465
+
466
+    /**
467
+     * @param \OCP\Files\Storage $sourceStorage
468
+     * @param string $sourceInternalPath
469
+     * @param string $targetInternalPath
470
+     * @return bool
471
+     */
472
+    public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
473
+        if ($sourceStorage === $this) {
474
+            return $this->copy($sourceInternalPath, $targetInternalPath);
475
+        }
476
+        return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
477
+    }
478
+
479
+    /**
480
+     * @param \OCP\Files\Storage $sourceStorage
481
+     * @param string $sourceInternalPath
482
+     * @param string $targetInternalPath
483
+     * @return bool
484
+     */
485
+    public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
486
+        if ($sourceStorage === $this) {
487
+            return $this->rename($sourceInternalPath, $targetInternalPath);
488
+        }
489
+        return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
490
+    }
491 491
 }
Please login to merge, or discard this patch.
lib/private/Files/Node/Folder.php 1 patch
Indentation   +390 added lines, -390 removed lines patch added patch discarded remove patch
@@ -36,394 +36,394 @@
 block discarded – undo
36 36
 use OCP\Files\Search\ISearchOperator;
37 37
 
38 38
 class Folder extends Node implements \OCP\Files\Folder {
39
-	/**
40
-	 * Creates a Folder that represents a non-existing path
41
-	 *
42
-	 * @param string $path path
43
-	 * @return string non-existing node class
44
-	 */
45
-	protected function createNonExistingNode($path) {
46
-		return new NonExistingFolder($this->root, $this->view, $path);
47
-	}
48
-
49
-	/**
50
-	 * @param string $path path relative to the folder
51
-	 * @return string
52
-	 * @throws \OCP\Files\NotPermittedException
53
-	 */
54
-	public function getFullPath($path) {
55
-		if (!$this->isValidPath($path)) {
56
-			throw new NotPermittedException('Invalid path');
57
-		}
58
-		return $this->path . $this->normalizePath($path);
59
-	}
60
-
61
-	/**
62
-	 * @param string $path
63
-	 * @return string
64
-	 */
65
-	public function getRelativePath($path) {
66
-		if ($this->path === '' or $this->path === '/') {
67
-			return $this->normalizePath($path);
68
-		}
69
-		if ($path === $this->path) {
70
-			return '/';
71
-		} else if (strpos($path, $this->path . '/') !== 0) {
72
-			return null;
73
-		} else {
74
-			$path = substr($path, strlen($this->path));
75
-			return $this->normalizePath($path);
76
-		}
77
-	}
78
-
79
-	/**
80
-	 * check if a node is a (grand-)child of the folder
81
-	 *
82
-	 * @param \OC\Files\Node\Node $node
83
-	 * @return bool
84
-	 */
85
-	public function isSubNode($node) {
86
-		return strpos($node->getPath(), $this->path . '/') === 0;
87
-	}
88
-
89
-	/**
90
-	 * get the content of this directory
91
-	 *
92
-	 * @throws \OCP\Files\NotFoundException
93
-	 * @return Node[]
94
-	 */
95
-	public function getDirectoryListing() {
96
-		$folderContent = $this->view->getDirectoryContent($this->path);
97
-
98
-		return array_map(function (FileInfo $info) {
99
-			if ($info->getMimetype() === 'httpd/unix-directory') {
100
-				return new Folder($this->root, $this->view, $info->getPath(), $info);
101
-			} else {
102
-				return new File($this->root, $this->view, $info->getPath(), $info);
103
-			}
104
-		}, $folderContent);
105
-	}
106
-
107
-	/**
108
-	 * @param string $path
109
-	 * @param FileInfo $info
110
-	 * @return File|Folder
111
-	 */
112
-	protected function createNode($path, FileInfo $info = null) {
113
-		if (is_null($info)) {
114
-			$isDir = $this->view->is_dir($path);
115
-		} else {
116
-			$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
117
-		}
118
-		if ($isDir) {
119
-			return new Folder($this->root, $this->view, $path, $info);
120
-		} else {
121
-			return new File($this->root, $this->view, $path, $info);
122
-		}
123
-	}
124
-
125
-	/**
126
-	 * Get the node at $path
127
-	 *
128
-	 * @param string $path
129
-	 * @return \OC\Files\Node\Node
130
-	 * @throws \OCP\Files\NotFoundException
131
-	 */
132
-	public function get($path) {
133
-		return $this->root->get($this->getFullPath($path));
134
-	}
135
-
136
-	/**
137
-	 * @param string $path
138
-	 * @return bool
139
-	 */
140
-	public function nodeExists($path) {
141
-		try {
142
-			$this->get($path);
143
-			return true;
144
-		} catch (NotFoundException $e) {
145
-			return false;
146
-		}
147
-	}
148
-
149
-	/**
150
-	 * @param string $path
151
-	 * @return \OC\Files\Node\Folder
152
-	 * @throws \OCP\Files\NotPermittedException
153
-	 */
154
-	public function newFolder($path) {
155
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
156
-			$fullPath = $this->getFullPath($path);
157
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
158
-			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
159
-			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
160
-			$this->view->mkdir($fullPath);
161
-			$node = new Folder($this->root, $this->view, $fullPath);
162
-			$this->root->emit('\OC\Files', 'postWrite', array($node));
163
-			$this->root->emit('\OC\Files', 'postCreate', array($node));
164
-			return $node;
165
-		} else {
166
-			throw new NotPermittedException('No create permission for folder');
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * @param string $path
172
-	 * @return \OC\Files\Node\File
173
-	 * @throws \OCP\Files\NotPermittedException
174
-	 */
175
-	public function newFile($path) {
176
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
177
-			$fullPath = $this->getFullPath($path);
178
-			$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
179
-			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
180
-			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
181
-			$this->view->touch($fullPath);
182
-			$node = new File($this->root, $this->view, $fullPath);
183
-			$this->root->emit('\OC\Files', 'postWrite', array($node));
184
-			$this->root->emit('\OC\Files', 'postCreate', array($node));
185
-			return $node;
186
-		} else {
187
-			throw new NotPermittedException('No create permission for path');
188
-		}
189
-	}
190
-
191
-	/**
192
-	 * search for files with the name matching $query
193
-	 *
194
-	 * @param string|ISearchOperator $query
195
-	 * @return \OC\Files\Node\Node[]
196
-	 */
197
-	public function search($query) {
198
-		if (is_string($query)) {
199
-			return $this->searchCommon('search', array('%' . $query . '%'));
200
-		} else {
201
-			return $this->searchCommon('searchQuery', array($query));
202
-		}
203
-	}
204
-
205
-	/**
206
-	 * search for files by mimetype
207
-	 *
208
-	 * @param string $mimetype
209
-	 * @return Node[]
210
-	 */
211
-	public function searchByMime($mimetype) {
212
-		return $this->searchCommon('searchByMime', array($mimetype));
213
-	}
214
-
215
-	/**
216
-	 * search for files by tag
217
-	 *
218
-	 * @param string|int $tag name or tag id
219
-	 * @param string $userId owner of the tags
220
-	 * @return Node[]
221
-	 */
222
-	public function searchByTag($tag, $userId) {
223
-		return $this->searchCommon('searchByTag', array($tag, $userId));
224
-	}
225
-
226
-	/**
227
-	 * @param string $method cache method
228
-	 * @param array $args call args
229
-	 * @return \OC\Files\Node\Node[]
230
-	 */
231
-	private function searchCommon($method, $args) {
232
-		$files = array();
233
-		$rootLength = strlen($this->path);
234
-		$mount = $this->root->getMount($this->path);
235
-		$storage = $mount->getStorage();
236
-		$internalPath = $mount->getInternalPath($this->path);
237
-		$internalPath = rtrim($internalPath, '/');
238
-		if ($internalPath !== '') {
239
-			$internalPath = $internalPath . '/';
240
-		}
241
-		$internalRootLength = strlen($internalPath);
242
-
243
-		$cache = $storage->getCache('');
244
-
245
-		$results = call_user_func_array(array($cache, $method), $args);
246
-		foreach ($results as $result) {
247
-			if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
248
-				$result['internalPath'] = $result['path'];
249
-				$result['path'] = substr($result['path'], $internalRootLength);
250
-				$result['storage'] = $storage;
251
-				$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
252
-			}
253
-		}
254
-
255
-		$mounts = $this->root->getMountsIn($this->path);
256
-		foreach ($mounts as $mount) {
257
-			$storage = $mount->getStorage();
258
-			if ($storage) {
259
-				$cache = $storage->getCache('');
260
-
261
-				$relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
262
-				$results = call_user_func_array(array($cache, $method), $args);
263
-				foreach ($results as $result) {
264
-					$result['internalPath'] = $result['path'];
265
-					$result['path'] = $relativeMountPoint . $result['path'];
266
-					$result['storage'] = $storage;
267
-					$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
-				}
269
-			}
270
-		}
271
-
272
-		return array_map(function (FileInfo $file) {
273
-			return $this->createNode($file->getPath(), $file);
274
-		}, $files);
275
-	}
276
-
277
-	/**
278
-	 * @param int $id
279
-	 * @return \OC\Files\Node\Node[]
280
-	 */
281
-	public function getById($id) {
282
-		$mountCache = $this->root->getUserMountCache();
283
-		$mountsContainingFile = $mountCache->getMountsForFileId((int)$id);
284
-		$mounts = $this->root->getMountsIn($this->path);
285
-		$mounts[] = $this->root->getMount($this->path);
286
-		/** @var IMountPoint[] $folderMounts */
287
-		$folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
288
-			return $mountPoint->getMountPoint();
289
-		}, $mounts), $mounts);
290
-
291
-		/** @var ICachedMountInfo[] $mountsContainingFile */
292
-		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
293
-			return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
294
-		}));
295
-
296
-		if (count($mountsContainingFile) === 0) {
297
-			return [];
298
-		}
299
-
300
-		// we only need to get the cache info once, since all mounts we found point to the same storage
301
-
302
-		$mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()];
303
-		$cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
304
-		if (!$cacheEntry) {
305
-			return [];
306
-		}
307
-		// cache jails will hide the "true" internal path
308
-		$internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
309
-
310
-		$nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) {
311
-			$mount = $folderMounts[$cachedMountInfo->getMountPoint()];
312
-			$pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
313
-			$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
314
-			$absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount;
315
-			return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
316
-				$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
317
-				\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
318
-			));
319
-		}, $mountsContainingFile);
320
-
321
-		return array_filter($nodes, function (Node $node) {
322
-			return $this->getRelativePath($node->getPath());
323
-		});
324
-	}
325
-
326
-	public function getFreeSpace() {
327
-		return $this->view->free_space($this->path);
328
-	}
329
-
330
-	public function delete() {
331
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
332
-			$this->sendHooks(array('preDelete'));
333
-			$fileInfo = $this->getFileInfo();
334
-			$this->view->rmdir($this->path);
335
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
336
-			$this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
337
-			$this->exists = false;
338
-		} else {
339
-			throw new NotPermittedException('No delete permission for path');
340
-		}
341
-	}
342
-
343
-	/**
344
-	 * Add a suffix to the name in case the file exists
345
-	 *
346
-	 * @param string $name
347
-	 * @return string
348
-	 * @throws NotPermittedException
349
-	 */
350
-	public function getNonExistingName($name) {
351
-		$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
352
-		return trim($this->getRelativePath($uniqueName), '/');
353
-	}
354
-
355
-	/**
356
-	 * @param int $limit
357
-	 * @param int $offset
358
-	 * @return \OCP\Files\Node[]
359
-	 */
360
-	public function getRecent($limit, $offset = 0) {
361
-		$mimetypeLoader = \OC::$server->getMimeTypeLoader();
362
-		$mounts = $this->root->getMountsIn($this->path);
363
-		$mounts[] = $this->getMountPoint();
364
-
365
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
366
-			return $mount->getStorage();
367
-		});
368
-		$storageIds = array_map(function (IMountPoint $mount) {
369
-			return $mount->getStorage()->getCache()->getNumericStorageId();
370
-		}, $mounts);
371
-		/** @var IMountPoint[] $mountMap */
372
-		$mountMap = array_combine($storageIds, $mounts);
373
-		$folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
374
-
375
-		//todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc)
376
-
377
-		$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
378
-		$query = $builder
379
-			->select('f.*')
380
-			->from('filecache', 'f')
381
-			->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
382
-			->andWhere($builder->expr()->orX(
383
-			// handle non empty folders separate
384
-				$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
385
-				$builder->expr()->eq('f.size', new Literal(0))
386
-			))
387
-			->orderBy('f.mtime', 'DESC')
388
-			->setMaxResults($limit)
389
-			->setFirstResult($offset);
390
-
391
-		$result = $query->execute()->fetchAll();
392
-
393
-		$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
394
-			$mount = $mountMap[$entry['storage']];
395
-			$entry['internalPath'] = $entry['path'];
396
-			$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
397
-			$entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
398
-			$path = $this->getAbsolutePath($mount, $entry['path']);
399
-			if (is_null($path)) {
400
-				return null;
401
-			}
402
-			$fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
403
-			return $this->root->createNode($fileInfo->getPath(), $fileInfo);
404
-		}, $result));
405
-
406
-		return array_values(array_filter($files, function (Node $node) {
407
-			$relative = $this->getRelativePath($node->getPath());
408
-			return $relative !== null && $relative !== '/';
409
-		}));
410
-	}
411
-
412
-	private function getAbsolutePath(IMountPoint $mount, $path) {
413
-		$storage = $mount->getStorage();
414
-		if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
415
-			/** @var \OC\Files\Storage\Wrapper\Jail $storage */
416
-			$jailRoot = $storage->getUnjailedPath('');
417
-			$rootLength = strlen($jailRoot) + 1;
418
-			if ($path === $jailRoot) {
419
-				return $mount->getMountPoint();
420
-			} else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
421
-				return $mount->getMountPoint() . substr($path, $rootLength);
422
-			} else {
423
-				return null;
424
-			}
425
-		} else {
426
-			return $mount->getMountPoint() . $path;
427
-		}
428
-	}
39
+    /**
40
+     * Creates a Folder that represents a non-existing path
41
+     *
42
+     * @param string $path path
43
+     * @return string non-existing node class
44
+     */
45
+    protected function createNonExistingNode($path) {
46
+        return new NonExistingFolder($this->root, $this->view, $path);
47
+    }
48
+
49
+    /**
50
+     * @param string $path path relative to the folder
51
+     * @return string
52
+     * @throws \OCP\Files\NotPermittedException
53
+     */
54
+    public function getFullPath($path) {
55
+        if (!$this->isValidPath($path)) {
56
+            throw new NotPermittedException('Invalid path');
57
+        }
58
+        return $this->path . $this->normalizePath($path);
59
+    }
60
+
61
+    /**
62
+     * @param string $path
63
+     * @return string
64
+     */
65
+    public function getRelativePath($path) {
66
+        if ($this->path === '' or $this->path === '/') {
67
+            return $this->normalizePath($path);
68
+        }
69
+        if ($path === $this->path) {
70
+            return '/';
71
+        } else if (strpos($path, $this->path . '/') !== 0) {
72
+            return null;
73
+        } else {
74
+            $path = substr($path, strlen($this->path));
75
+            return $this->normalizePath($path);
76
+        }
77
+    }
78
+
79
+    /**
80
+     * check if a node is a (grand-)child of the folder
81
+     *
82
+     * @param \OC\Files\Node\Node $node
83
+     * @return bool
84
+     */
85
+    public function isSubNode($node) {
86
+        return strpos($node->getPath(), $this->path . '/') === 0;
87
+    }
88
+
89
+    /**
90
+     * get the content of this directory
91
+     *
92
+     * @throws \OCP\Files\NotFoundException
93
+     * @return Node[]
94
+     */
95
+    public function getDirectoryListing() {
96
+        $folderContent = $this->view->getDirectoryContent($this->path);
97
+
98
+        return array_map(function (FileInfo $info) {
99
+            if ($info->getMimetype() === 'httpd/unix-directory') {
100
+                return new Folder($this->root, $this->view, $info->getPath(), $info);
101
+            } else {
102
+                return new File($this->root, $this->view, $info->getPath(), $info);
103
+            }
104
+        }, $folderContent);
105
+    }
106
+
107
+    /**
108
+     * @param string $path
109
+     * @param FileInfo $info
110
+     * @return File|Folder
111
+     */
112
+    protected function createNode($path, FileInfo $info = null) {
113
+        if (is_null($info)) {
114
+            $isDir = $this->view->is_dir($path);
115
+        } else {
116
+            $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
117
+        }
118
+        if ($isDir) {
119
+            return new Folder($this->root, $this->view, $path, $info);
120
+        } else {
121
+            return new File($this->root, $this->view, $path, $info);
122
+        }
123
+    }
124
+
125
+    /**
126
+     * Get the node at $path
127
+     *
128
+     * @param string $path
129
+     * @return \OC\Files\Node\Node
130
+     * @throws \OCP\Files\NotFoundException
131
+     */
132
+    public function get($path) {
133
+        return $this->root->get($this->getFullPath($path));
134
+    }
135
+
136
+    /**
137
+     * @param string $path
138
+     * @return bool
139
+     */
140
+    public function nodeExists($path) {
141
+        try {
142
+            $this->get($path);
143
+            return true;
144
+        } catch (NotFoundException $e) {
145
+            return false;
146
+        }
147
+    }
148
+
149
+    /**
150
+     * @param string $path
151
+     * @return \OC\Files\Node\Folder
152
+     * @throws \OCP\Files\NotPermittedException
153
+     */
154
+    public function newFolder($path) {
155
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
156
+            $fullPath = $this->getFullPath($path);
157
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
158
+            $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
159
+            $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
160
+            $this->view->mkdir($fullPath);
161
+            $node = new Folder($this->root, $this->view, $fullPath);
162
+            $this->root->emit('\OC\Files', 'postWrite', array($node));
163
+            $this->root->emit('\OC\Files', 'postCreate', array($node));
164
+            return $node;
165
+        } else {
166
+            throw new NotPermittedException('No create permission for folder');
167
+        }
168
+    }
169
+
170
+    /**
171
+     * @param string $path
172
+     * @return \OC\Files\Node\File
173
+     * @throws \OCP\Files\NotPermittedException
174
+     */
175
+    public function newFile($path) {
176
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
177
+            $fullPath = $this->getFullPath($path);
178
+            $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
179
+            $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
180
+            $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
181
+            $this->view->touch($fullPath);
182
+            $node = new File($this->root, $this->view, $fullPath);
183
+            $this->root->emit('\OC\Files', 'postWrite', array($node));
184
+            $this->root->emit('\OC\Files', 'postCreate', array($node));
185
+            return $node;
186
+        } else {
187
+            throw new NotPermittedException('No create permission for path');
188
+        }
189
+    }
190
+
191
+    /**
192
+     * search for files with the name matching $query
193
+     *
194
+     * @param string|ISearchOperator $query
195
+     * @return \OC\Files\Node\Node[]
196
+     */
197
+    public function search($query) {
198
+        if (is_string($query)) {
199
+            return $this->searchCommon('search', array('%' . $query . '%'));
200
+        } else {
201
+            return $this->searchCommon('searchQuery', array($query));
202
+        }
203
+    }
204
+
205
+    /**
206
+     * search for files by mimetype
207
+     *
208
+     * @param string $mimetype
209
+     * @return Node[]
210
+     */
211
+    public function searchByMime($mimetype) {
212
+        return $this->searchCommon('searchByMime', array($mimetype));
213
+    }
214
+
215
+    /**
216
+     * search for files by tag
217
+     *
218
+     * @param string|int $tag name or tag id
219
+     * @param string $userId owner of the tags
220
+     * @return Node[]
221
+     */
222
+    public function searchByTag($tag, $userId) {
223
+        return $this->searchCommon('searchByTag', array($tag, $userId));
224
+    }
225
+
226
+    /**
227
+     * @param string $method cache method
228
+     * @param array $args call args
229
+     * @return \OC\Files\Node\Node[]
230
+     */
231
+    private function searchCommon($method, $args) {
232
+        $files = array();
233
+        $rootLength = strlen($this->path);
234
+        $mount = $this->root->getMount($this->path);
235
+        $storage = $mount->getStorage();
236
+        $internalPath = $mount->getInternalPath($this->path);
237
+        $internalPath = rtrim($internalPath, '/');
238
+        if ($internalPath !== '') {
239
+            $internalPath = $internalPath . '/';
240
+        }
241
+        $internalRootLength = strlen($internalPath);
242
+
243
+        $cache = $storage->getCache('');
244
+
245
+        $results = call_user_func_array(array($cache, $method), $args);
246
+        foreach ($results as $result) {
247
+            if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
248
+                $result['internalPath'] = $result['path'];
249
+                $result['path'] = substr($result['path'], $internalRootLength);
250
+                $result['storage'] = $storage;
251
+                $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
252
+            }
253
+        }
254
+
255
+        $mounts = $this->root->getMountsIn($this->path);
256
+        foreach ($mounts as $mount) {
257
+            $storage = $mount->getStorage();
258
+            if ($storage) {
259
+                $cache = $storage->getCache('');
260
+
261
+                $relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
262
+                $results = call_user_func_array(array($cache, $method), $args);
263
+                foreach ($results as $result) {
264
+                    $result['internalPath'] = $result['path'];
265
+                    $result['path'] = $relativeMountPoint . $result['path'];
266
+                    $result['storage'] = $storage;
267
+                    $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
+                }
269
+            }
270
+        }
271
+
272
+        return array_map(function (FileInfo $file) {
273
+            return $this->createNode($file->getPath(), $file);
274
+        }, $files);
275
+    }
276
+
277
+    /**
278
+     * @param int $id
279
+     * @return \OC\Files\Node\Node[]
280
+     */
281
+    public function getById($id) {
282
+        $mountCache = $this->root->getUserMountCache();
283
+        $mountsContainingFile = $mountCache->getMountsForFileId((int)$id);
284
+        $mounts = $this->root->getMountsIn($this->path);
285
+        $mounts[] = $this->root->getMount($this->path);
286
+        /** @var IMountPoint[] $folderMounts */
287
+        $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
288
+            return $mountPoint->getMountPoint();
289
+        }, $mounts), $mounts);
290
+
291
+        /** @var ICachedMountInfo[] $mountsContainingFile */
292
+        $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
293
+            return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
294
+        }));
295
+
296
+        if (count($mountsContainingFile) === 0) {
297
+            return [];
298
+        }
299
+
300
+        // we only need to get the cache info once, since all mounts we found point to the same storage
301
+
302
+        $mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()];
303
+        $cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
304
+        if (!$cacheEntry) {
305
+            return [];
306
+        }
307
+        // cache jails will hide the "true" internal path
308
+        $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
309
+
310
+        $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) {
311
+            $mount = $folderMounts[$cachedMountInfo->getMountPoint()];
312
+            $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
313
+            $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
314
+            $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount;
315
+            return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
316
+                $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
317
+                \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
318
+            ));
319
+        }, $mountsContainingFile);
320
+
321
+        return array_filter($nodes, function (Node $node) {
322
+            return $this->getRelativePath($node->getPath());
323
+        });
324
+    }
325
+
326
+    public function getFreeSpace() {
327
+        return $this->view->free_space($this->path);
328
+    }
329
+
330
+    public function delete() {
331
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
332
+            $this->sendHooks(array('preDelete'));
333
+            $fileInfo = $this->getFileInfo();
334
+            $this->view->rmdir($this->path);
335
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
336
+            $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
337
+            $this->exists = false;
338
+        } else {
339
+            throw new NotPermittedException('No delete permission for path');
340
+        }
341
+    }
342
+
343
+    /**
344
+     * Add a suffix to the name in case the file exists
345
+     *
346
+     * @param string $name
347
+     * @return string
348
+     * @throws NotPermittedException
349
+     */
350
+    public function getNonExistingName($name) {
351
+        $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
352
+        return trim($this->getRelativePath($uniqueName), '/');
353
+    }
354
+
355
+    /**
356
+     * @param int $limit
357
+     * @param int $offset
358
+     * @return \OCP\Files\Node[]
359
+     */
360
+    public function getRecent($limit, $offset = 0) {
361
+        $mimetypeLoader = \OC::$server->getMimeTypeLoader();
362
+        $mounts = $this->root->getMountsIn($this->path);
363
+        $mounts[] = $this->getMountPoint();
364
+
365
+        $mounts = array_filter($mounts, function (IMountPoint $mount) {
366
+            return $mount->getStorage();
367
+        });
368
+        $storageIds = array_map(function (IMountPoint $mount) {
369
+            return $mount->getStorage()->getCache()->getNumericStorageId();
370
+        }, $mounts);
371
+        /** @var IMountPoint[] $mountMap */
372
+        $mountMap = array_combine($storageIds, $mounts);
373
+        $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
374
+
375
+        //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc)
376
+
377
+        $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
378
+        $query = $builder
379
+            ->select('f.*')
380
+            ->from('filecache', 'f')
381
+            ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
382
+            ->andWhere($builder->expr()->orX(
383
+            // handle non empty folders separate
384
+                $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
385
+                $builder->expr()->eq('f.size', new Literal(0))
386
+            ))
387
+            ->orderBy('f.mtime', 'DESC')
388
+            ->setMaxResults($limit)
389
+            ->setFirstResult($offset);
390
+
391
+        $result = $query->execute()->fetchAll();
392
+
393
+        $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
394
+            $mount = $mountMap[$entry['storage']];
395
+            $entry['internalPath'] = $entry['path'];
396
+            $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
397
+            $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
398
+            $path = $this->getAbsolutePath($mount, $entry['path']);
399
+            if (is_null($path)) {
400
+                return null;
401
+            }
402
+            $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
403
+            return $this->root->createNode($fileInfo->getPath(), $fileInfo);
404
+        }, $result));
405
+
406
+        return array_values(array_filter($files, function (Node $node) {
407
+            $relative = $this->getRelativePath($node->getPath());
408
+            return $relative !== null && $relative !== '/';
409
+        }));
410
+    }
411
+
412
+    private function getAbsolutePath(IMountPoint $mount, $path) {
413
+        $storage = $mount->getStorage();
414
+        if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
415
+            /** @var \OC\Files\Storage\Wrapper\Jail $storage */
416
+            $jailRoot = $storage->getUnjailedPath('');
417
+            $rootLength = strlen($jailRoot) + 1;
418
+            if ($path === $jailRoot) {
419
+                return $mount->getMountPoint();
420
+            } else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
421
+                return $mount->getMountPoint() . substr($path, $rootLength);
422
+            } else {
423
+                return null;
424
+            }
425
+        } else {
426
+            return $mount->getMountPoint() . $path;
427
+        }
428
+    }
429 429
 }
Please login to merge, or discard this patch.