Passed
Push — master ( 23080f...533cb6 )
by Roeland
10:44 queued 11s
created
lib/private/Files/Storage/Local.php 1 patch
Indentation   +504 added lines, -504 removed lines patch added patch discarded remove patch
@@ -51,508 +51,508 @@
 block discarded – undo
51 51
  * for local filestore, we only have to map the paths
52 52
  */
53 53
 class Local extends \OC\Files\Storage\Common {
54
-	protected $datadir;
55
-
56
-	protected $dataDirLength;
57
-
58
-	protected $allowSymlinks = false;
59
-
60
-	protected $realDataDir;
61
-
62
-	public function __construct($arguments) {
63
-		if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
64
-			throw new \InvalidArgumentException('No data directory set for local storage');
65
-		}
66
-		$this->datadir = str_replace('//', '/', $arguments['datadir']);
67
-		// some crazy code uses a local storage on root...
68
-		if ($this->datadir === '/') {
69
-			$this->realDataDir = $this->datadir;
70
-		} else {
71
-			$realPath = realpath($this->datadir) ?: $this->datadir;
72
-			$this->realDataDir = rtrim($realPath, '/') . '/';
73
-		}
74
-		if (substr($this->datadir, -1) !== '/') {
75
-			$this->datadir .= '/';
76
-		}
77
-		$this->dataDirLength = strlen($this->realDataDir);
78
-	}
79
-
80
-	public function __destruct() {
81
-	}
82
-
83
-	public function getId() {
84
-		return 'local::' . $this->datadir;
85
-	}
86
-
87
-	public function mkdir($path) {
88
-		return @mkdir($this->getSourcePath($path), 0777, true);
89
-	}
90
-
91
-	public function rmdir($path) {
92
-		if (!$this->isDeletable($path)) {
93
-			return false;
94
-		}
95
-		try {
96
-			$it = new \RecursiveIteratorIterator(
97
-				new \RecursiveDirectoryIterator($this->getSourcePath($path)),
98
-				\RecursiveIteratorIterator::CHILD_FIRST
99
-			);
100
-			/**
101
-			 * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
102
-			 * This bug is fixed in PHP 5.5.9 or before
103
-			 * See #8376
104
-			 */
105
-			$it->rewind();
106
-			while ($it->valid()) {
107
-				/**
108
-				 * @var \SplFileInfo $file
109
-				 */
110
-				$file = $it->current();
111
-				if (in_array($file->getBasename(), ['.', '..'])) {
112
-					$it->next();
113
-					continue;
114
-				} elseif ($file->isDir()) {
115
-					rmdir($file->getPathname());
116
-				} elseif ($file->isFile() || $file->isLink()) {
117
-					unlink($file->getPathname());
118
-				}
119
-				$it->next();
120
-			}
121
-			return rmdir($this->getSourcePath($path));
122
-		} catch (\UnexpectedValueException $e) {
123
-			return false;
124
-		}
125
-	}
126
-
127
-	public function opendir($path) {
128
-		return opendir($this->getSourcePath($path));
129
-	}
130
-
131
-	public function is_dir($path) {
132
-		if (substr($path, -1) == '/') {
133
-			$path = substr($path, 0, -1);
134
-		}
135
-		return is_dir($this->getSourcePath($path));
136
-	}
137
-
138
-	public function is_file($path) {
139
-		return is_file($this->getSourcePath($path));
140
-	}
141
-
142
-	public function stat($path) {
143
-		clearstatcache();
144
-		$fullPath = $this->getSourcePath($path);
145
-		$statResult = stat($fullPath);
146
-		if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
147
-			$filesize = $this->filesize($path);
148
-			$statResult['size'] = $filesize;
149
-			$statResult[7] = $filesize;
150
-		}
151
-		return $statResult;
152
-	}
153
-
154
-	/**
155
-	 * @inheritdoc
156
-	 */
157
-	public function getMetaData($path) {
158
-		$fullPath = $this->getSourcePath($path);
159
-		clearstatcache();
160
-		$stat = @stat($fullPath);
161
-		if (!$stat) {
162
-			return null;
163
-		}
164
-
165
-		$permissions = Constants::PERMISSION_SHARE;
166
-		$statPermissions = $stat['mode'];
167
-		$isDir = ($statPermissions & 0x4000) === 0x4000;
168
-		if ($statPermissions & 0x0100) {
169
-			$permissions += Constants::PERMISSION_READ;
170
-		}
171
-		if ($statPermissions & 0x0080) {
172
-			$permissions += Constants::PERMISSION_UPDATE;
173
-			if ($isDir) {
174
-				$permissions += Constants::PERMISSION_CREATE;
175
-			}
176
-		}
177
-
178
-		if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
179
-			$parent = dirname($fullPath);
180
-			if (is_writable($parent)) {
181
-				$permissions += Constants::PERMISSION_DELETE;
182
-			}
183
-		}
184
-
185
-		$data = [];
186
-		$data['mimetype'] = $isDir ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($path);
187
-		$data['mtime'] = $stat['mtime'];
188
-		if ($data['mtime'] === false) {
189
-			$data['mtime'] = time();
190
-		}
191
-		if ($isDir) {
192
-			$data['size'] = -1; //unknown
193
-		} else {
194
-			$data['size'] = $stat['size'];
195
-		}
196
-		$data['etag'] = $this->calculateEtag($path, $stat);
197
-		$data['storage_mtime'] = $data['mtime'];
198
-		$data['permissions'] = $permissions;
199
-		$data['name'] = basename($path);
200
-
201
-		return $data;
202
-	}
203
-
204
-	public function filetype($path) {
205
-		$filetype = filetype($this->getSourcePath($path));
206
-		if ($filetype == 'link') {
207
-			$filetype = filetype(realpath($this->getSourcePath($path)));
208
-		}
209
-		return $filetype;
210
-	}
211
-
212
-	public function filesize($path) {
213
-		if ($this->is_dir($path)) {
214
-			return 0;
215
-		}
216
-		$fullPath = $this->getSourcePath($path);
217
-		if (PHP_INT_SIZE === 4) {
218
-			$helper = new \OC\LargeFileHelper;
219
-			return $helper->getFileSize($fullPath);
220
-		}
221
-		return filesize($fullPath);
222
-	}
223
-
224
-	public function isReadable($path) {
225
-		return is_readable($this->getSourcePath($path));
226
-	}
227
-
228
-	public function isUpdatable($path) {
229
-		return is_writable($this->getSourcePath($path));
230
-	}
231
-
232
-	public function file_exists($path) {
233
-		return file_exists($this->getSourcePath($path));
234
-	}
235
-
236
-	public function filemtime($path) {
237
-		$fullPath = $this->getSourcePath($path);
238
-		clearstatcache(true, $fullPath);
239
-		if (!$this->file_exists($path)) {
240
-			return false;
241
-		}
242
-		if (PHP_INT_SIZE === 4) {
243
-			$helper = new \OC\LargeFileHelper();
244
-			return $helper->getFileMtime($fullPath);
245
-		}
246
-		return filemtime($fullPath);
247
-	}
248
-
249
-	public function touch($path, $mtime = null) {
250
-		// sets the modification time of the file to the given value.
251
-		// If mtime is nil the current time is set.
252
-		// note that the access time of the file always changes to the current time.
253
-		if ($this->file_exists($path) and !$this->isUpdatable($path)) {
254
-			return false;
255
-		}
256
-		if (!is_null($mtime)) {
257
-			$result = @touch($this->getSourcePath($path), $mtime);
258
-		} else {
259
-			$result = @touch($this->getSourcePath($path));
260
-		}
261
-		if ($result) {
262
-			clearstatcache(true, $this->getSourcePath($path));
263
-		}
264
-
265
-		return $result;
266
-	}
267
-
268
-	public function file_get_contents($path) {
269
-		return file_get_contents($this->getSourcePath($path));
270
-	}
271
-
272
-	public function file_put_contents($path, $data) {
273
-		return file_put_contents($this->getSourcePath($path), $data);
274
-	}
275
-
276
-	public function unlink($path) {
277
-		if ($this->is_dir($path)) {
278
-			return $this->rmdir($path);
279
-		} elseif ($this->is_file($path)) {
280
-			return unlink($this->getSourcePath($path));
281
-		} else {
282
-			return false;
283
-		}
284
-	}
285
-
286
-	private function treeContainsBlacklistedFile(string $path): bool {
287
-		$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
288
-		foreach ($iterator as $file) {
289
-			/** @var \SplFileInfo $file */
290
-			if (Filesystem::isFileBlacklisted($file->getBasename())) {
291
-				return true;
292
-			}
293
-		}
294
-
295
-		return false;
296
-	}
297
-
298
-	public function rename($path1, $path2) {
299
-		$srcParent = dirname($path1);
300
-		$dstParent = dirname($path2);
301
-
302
-		if (!$this->isUpdatable($srcParent)) {
303
-			\OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, ILogger::ERROR);
304
-			return false;
305
-		}
306
-
307
-		if (!$this->isUpdatable($dstParent)) {
308
-			\OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, ILogger::ERROR);
309
-			return false;
310
-		}
311
-
312
-		if (!$this->file_exists($path1)) {
313
-			\OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, ILogger::ERROR);
314
-			return false;
315
-		}
316
-
317
-		if ($this->is_dir($path2)) {
318
-			$this->rmdir($path2);
319
-		} elseif ($this->is_file($path2)) {
320
-			$this->unlink($path2);
321
-		}
322
-
323
-		if ($this->is_dir($path1)) {
324
-			// we can't move folders across devices, use copy instead
325
-			$stat1 = stat(dirname($this->getSourcePath($path1)));
326
-			$stat2 = stat(dirname($this->getSourcePath($path2)));
327
-			if ($stat1['dev'] !== $stat2['dev']) {
328
-				$result = $this->copy($path1, $path2);
329
-				if ($result) {
330
-					$result &= $this->rmdir($path1);
331
-				}
332
-				return $result;
333
-			}
334
-
335
-			if ($this->treeContainsBlacklistedFile($this->getSourcePath($path1))) {
336
-				throw new ForbiddenException('Invalid path', false);
337
-			}
338
-		}
339
-
340
-		return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
341
-	}
342
-
343
-	public function copy($path1, $path2) {
344
-		if ($this->is_dir($path1)) {
345
-			return parent::copy($path1, $path2);
346
-		} else {
347
-			return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
348
-		}
349
-	}
350
-
351
-	public function fopen($path, $mode) {
352
-		return fopen($this->getSourcePath($path), $mode);
353
-	}
354
-
355
-	public function hash($type, $path, $raw = false) {
356
-		return hash_file($type, $this->getSourcePath($path), $raw);
357
-	}
358
-
359
-	public function free_space($path) {
360
-		$sourcePath = $this->getSourcePath($path);
361
-		// using !is_dir because $sourcePath might be a part file or
362
-		// non-existing file, so we'd still want to use the parent dir
363
-		// in such cases
364
-		if (!is_dir($sourcePath)) {
365
-			// disk_free_space doesn't work on files
366
-			$sourcePath = dirname($sourcePath);
367
-		}
368
-		$space = @disk_free_space($sourcePath);
369
-		if ($space === false || is_null($space)) {
370
-			return \OCP\Files\FileInfo::SPACE_UNKNOWN;
371
-		}
372
-		return $space;
373
-	}
374
-
375
-	public function search($query) {
376
-		return $this->searchInDir($query);
377
-	}
378
-
379
-	public function getLocalFile($path) {
380
-		return $this->getSourcePath($path);
381
-	}
382
-
383
-	public function getLocalFolder($path) {
384
-		return $this->getSourcePath($path);
385
-	}
386
-
387
-	/**
388
-	 * @param string $query
389
-	 * @param string $dir
390
-	 * @return array
391
-	 */
392
-	protected function searchInDir($query, $dir = '') {
393
-		$files = [];
394
-		$physicalDir = $this->getSourcePath($dir);
395
-		foreach (scandir($physicalDir) as $item) {
396
-			if (\OC\Files\Filesystem::isIgnoredDir($item)) {
397
-				continue;
398
-			}
399
-			$physicalItem = $physicalDir . '/' . $item;
400
-
401
-			if (strstr(strtolower($item), strtolower($query)) !== false) {
402
-				$files[] = $dir . '/' . $item;
403
-			}
404
-			if (is_dir($physicalItem)) {
405
-				$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
406
-			}
407
-		}
408
-		return $files;
409
-	}
410
-
411
-	/**
412
-	 * check if a file or folder has been updated since $time
413
-	 *
414
-	 * @param string $path
415
-	 * @param int $time
416
-	 * @return bool
417
-	 */
418
-	public function hasUpdated($path, $time) {
419
-		if ($this->file_exists($path)) {
420
-			return $this->filemtime($path) > $time;
421
-		} else {
422
-			return true;
423
-		}
424
-	}
425
-
426
-	/**
427
-	 * Get the source path (on disk) of a given path
428
-	 *
429
-	 * @param string $path
430
-	 * @return string
431
-	 * @throws ForbiddenException
432
-	 */
433
-	public function getSourcePath($path) {
434
-		if (Filesystem::isFileBlacklisted($path)) {
435
-			throw new ForbiddenException('Invalid path', false);
436
-		}
437
-
438
-		$fullPath = $this->datadir . $path;
439
-		$currentPath = $path;
440
-		if ($this->allowSymlinks || $currentPath === '') {
441
-			return $fullPath;
442
-		}
443
-		$pathToResolve = $fullPath;
444
-		$realPath = realpath($pathToResolve);
445
-		while ($realPath === false) { // for non existing files check the parent directory
446
-			$currentPath = dirname($currentPath);
447
-			if ($currentPath === '' || $currentPath === '.') {
448
-				return $fullPath;
449
-			}
450
-			$realPath = realpath($this->datadir . $currentPath);
451
-		}
452
-		if ($realPath) {
453
-			$realPath = $realPath . '/';
454
-		}
455
-		if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
456
-			return $fullPath;
457
-		}
458
-
459
-		\OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ILogger::ERROR);
460
-		throw new ForbiddenException('Following symlinks is not allowed', false);
461
-	}
462
-
463
-	/**
464
-	 * {@inheritdoc}
465
-	 */
466
-	public function isLocal() {
467
-		return true;
468
-	}
469
-
470
-	/**
471
-	 * get the ETag for a file or folder
472
-	 *
473
-	 * @param string $path
474
-	 * @return string
475
-	 */
476
-	public function getETag($path) {
477
-		return $this->calculateEtag($path, $this->stat($path));
478
-	}
479
-
480
-	private function calculateEtag(string $path, array $stat): string {
481
-		if ($stat['mode'] & 0x4000) { // is_dir
482
-			return parent::getETag($path);
483
-		} else {
484
-			if ($stat === false) {
485
-				return md5('');
486
-			}
487
-
488
-			$toHash = '';
489
-			if (isset($stat['mtime'])) {
490
-				$toHash .= $stat['mtime'];
491
-			}
492
-			if (isset($stat['ino'])) {
493
-				$toHash .= $stat['ino'];
494
-			}
495
-			if (isset($stat['dev'])) {
496
-				$toHash .= $stat['dev'];
497
-			}
498
-			if (isset($stat['size'])) {
499
-				$toHash .= $stat['size'];
500
-			}
501
-
502
-			return md5($toHash);
503
-		}
504
-	}
505
-
506
-	/**
507
-	 * @param IStorage $sourceStorage
508
-	 * @param string $sourceInternalPath
509
-	 * @param string $targetInternalPath
510
-	 * @param bool $preserveMtime
511
-	 * @return bool
512
-	 */
513
-	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
514
-		if ($sourceStorage->instanceOfStorage(Local::class)) {
515
-			if ($sourceStorage->instanceOfStorage(Jail::class)) {
516
-				/**
517
-				 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
518
-				 */
519
-				$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
520
-			}
521
-			/**
522
-			 * @var \OC\Files\Storage\Local $sourceStorage
523
-			 */
524
-			$rootStorage = new Local(['datadir' => '/']);
525
-			return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
526
-		} else {
527
-			return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
528
-		}
529
-	}
530
-
531
-	/**
532
-	 * @param IStorage $sourceStorage
533
-	 * @param string $sourceInternalPath
534
-	 * @param string $targetInternalPath
535
-	 * @return bool
536
-	 */
537
-	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
538
-		if ($sourceStorage->instanceOfStorage(Local::class)) {
539
-			if ($sourceStorage->instanceOfStorage(Jail::class)) {
540
-				/**
541
-				 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
542
-				 */
543
-				$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
544
-			}
545
-			/**
546
-			 * @var \OC\Files\Storage\Local $sourceStorage
547
-			 */
548
-			$rootStorage = new Local(['datadir' => '/']);
549
-			return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
550
-		} else {
551
-			return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
552
-		}
553
-	}
554
-
555
-	public function writeStream(string $path, $stream, int $size = null): int {
556
-		return (int)file_put_contents($this->getSourcePath($path), $stream);
557
-	}
54
+    protected $datadir;
55
+
56
+    protected $dataDirLength;
57
+
58
+    protected $allowSymlinks = false;
59
+
60
+    protected $realDataDir;
61
+
62
+    public function __construct($arguments) {
63
+        if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
64
+            throw new \InvalidArgumentException('No data directory set for local storage');
65
+        }
66
+        $this->datadir = str_replace('//', '/', $arguments['datadir']);
67
+        // some crazy code uses a local storage on root...
68
+        if ($this->datadir === '/') {
69
+            $this->realDataDir = $this->datadir;
70
+        } else {
71
+            $realPath = realpath($this->datadir) ?: $this->datadir;
72
+            $this->realDataDir = rtrim($realPath, '/') . '/';
73
+        }
74
+        if (substr($this->datadir, -1) !== '/') {
75
+            $this->datadir .= '/';
76
+        }
77
+        $this->dataDirLength = strlen($this->realDataDir);
78
+    }
79
+
80
+    public function __destruct() {
81
+    }
82
+
83
+    public function getId() {
84
+        return 'local::' . $this->datadir;
85
+    }
86
+
87
+    public function mkdir($path) {
88
+        return @mkdir($this->getSourcePath($path), 0777, true);
89
+    }
90
+
91
+    public function rmdir($path) {
92
+        if (!$this->isDeletable($path)) {
93
+            return false;
94
+        }
95
+        try {
96
+            $it = new \RecursiveIteratorIterator(
97
+                new \RecursiveDirectoryIterator($this->getSourcePath($path)),
98
+                \RecursiveIteratorIterator::CHILD_FIRST
99
+            );
100
+            /**
101
+             * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
102
+             * This bug is fixed in PHP 5.5.9 or before
103
+             * See #8376
104
+             */
105
+            $it->rewind();
106
+            while ($it->valid()) {
107
+                /**
108
+                 * @var \SplFileInfo $file
109
+                 */
110
+                $file = $it->current();
111
+                if (in_array($file->getBasename(), ['.', '..'])) {
112
+                    $it->next();
113
+                    continue;
114
+                } elseif ($file->isDir()) {
115
+                    rmdir($file->getPathname());
116
+                } elseif ($file->isFile() || $file->isLink()) {
117
+                    unlink($file->getPathname());
118
+                }
119
+                $it->next();
120
+            }
121
+            return rmdir($this->getSourcePath($path));
122
+        } catch (\UnexpectedValueException $e) {
123
+            return false;
124
+        }
125
+    }
126
+
127
+    public function opendir($path) {
128
+        return opendir($this->getSourcePath($path));
129
+    }
130
+
131
+    public function is_dir($path) {
132
+        if (substr($path, -1) == '/') {
133
+            $path = substr($path, 0, -1);
134
+        }
135
+        return is_dir($this->getSourcePath($path));
136
+    }
137
+
138
+    public function is_file($path) {
139
+        return is_file($this->getSourcePath($path));
140
+    }
141
+
142
+    public function stat($path) {
143
+        clearstatcache();
144
+        $fullPath = $this->getSourcePath($path);
145
+        $statResult = stat($fullPath);
146
+        if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
147
+            $filesize = $this->filesize($path);
148
+            $statResult['size'] = $filesize;
149
+            $statResult[7] = $filesize;
150
+        }
151
+        return $statResult;
152
+    }
153
+
154
+    /**
155
+     * @inheritdoc
156
+     */
157
+    public function getMetaData($path) {
158
+        $fullPath = $this->getSourcePath($path);
159
+        clearstatcache();
160
+        $stat = @stat($fullPath);
161
+        if (!$stat) {
162
+            return null;
163
+        }
164
+
165
+        $permissions = Constants::PERMISSION_SHARE;
166
+        $statPermissions = $stat['mode'];
167
+        $isDir = ($statPermissions & 0x4000) === 0x4000;
168
+        if ($statPermissions & 0x0100) {
169
+            $permissions += Constants::PERMISSION_READ;
170
+        }
171
+        if ($statPermissions & 0x0080) {
172
+            $permissions += Constants::PERMISSION_UPDATE;
173
+            if ($isDir) {
174
+                $permissions += Constants::PERMISSION_CREATE;
175
+            }
176
+        }
177
+
178
+        if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
179
+            $parent = dirname($fullPath);
180
+            if (is_writable($parent)) {
181
+                $permissions += Constants::PERMISSION_DELETE;
182
+            }
183
+        }
184
+
185
+        $data = [];
186
+        $data['mimetype'] = $isDir ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($path);
187
+        $data['mtime'] = $stat['mtime'];
188
+        if ($data['mtime'] === false) {
189
+            $data['mtime'] = time();
190
+        }
191
+        if ($isDir) {
192
+            $data['size'] = -1; //unknown
193
+        } else {
194
+            $data['size'] = $stat['size'];
195
+        }
196
+        $data['etag'] = $this->calculateEtag($path, $stat);
197
+        $data['storage_mtime'] = $data['mtime'];
198
+        $data['permissions'] = $permissions;
199
+        $data['name'] = basename($path);
200
+
201
+        return $data;
202
+    }
203
+
204
+    public function filetype($path) {
205
+        $filetype = filetype($this->getSourcePath($path));
206
+        if ($filetype == 'link') {
207
+            $filetype = filetype(realpath($this->getSourcePath($path)));
208
+        }
209
+        return $filetype;
210
+    }
211
+
212
+    public function filesize($path) {
213
+        if ($this->is_dir($path)) {
214
+            return 0;
215
+        }
216
+        $fullPath = $this->getSourcePath($path);
217
+        if (PHP_INT_SIZE === 4) {
218
+            $helper = new \OC\LargeFileHelper;
219
+            return $helper->getFileSize($fullPath);
220
+        }
221
+        return filesize($fullPath);
222
+    }
223
+
224
+    public function isReadable($path) {
225
+        return is_readable($this->getSourcePath($path));
226
+    }
227
+
228
+    public function isUpdatable($path) {
229
+        return is_writable($this->getSourcePath($path));
230
+    }
231
+
232
+    public function file_exists($path) {
233
+        return file_exists($this->getSourcePath($path));
234
+    }
235
+
236
+    public function filemtime($path) {
237
+        $fullPath = $this->getSourcePath($path);
238
+        clearstatcache(true, $fullPath);
239
+        if (!$this->file_exists($path)) {
240
+            return false;
241
+        }
242
+        if (PHP_INT_SIZE === 4) {
243
+            $helper = new \OC\LargeFileHelper();
244
+            return $helper->getFileMtime($fullPath);
245
+        }
246
+        return filemtime($fullPath);
247
+    }
248
+
249
+    public function touch($path, $mtime = null) {
250
+        // sets the modification time of the file to the given value.
251
+        // If mtime is nil the current time is set.
252
+        // note that the access time of the file always changes to the current time.
253
+        if ($this->file_exists($path) and !$this->isUpdatable($path)) {
254
+            return false;
255
+        }
256
+        if (!is_null($mtime)) {
257
+            $result = @touch($this->getSourcePath($path), $mtime);
258
+        } else {
259
+            $result = @touch($this->getSourcePath($path));
260
+        }
261
+        if ($result) {
262
+            clearstatcache(true, $this->getSourcePath($path));
263
+        }
264
+
265
+        return $result;
266
+    }
267
+
268
+    public function file_get_contents($path) {
269
+        return file_get_contents($this->getSourcePath($path));
270
+    }
271
+
272
+    public function file_put_contents($path, $data) {
273
+        return file_put_contents($this->getSourcePath($path), $data);
274
+    }
275
+
276
+    public function unlink($path) {
277
+        if ($this->is_dir($path)) {
278
+            return $this->rmdir($path);
279
+        } elseif ($this->is_file($path)) {
280
+            return unlink($this->getSourcePath($path));
281
+        } else {
282
+            return false;
283
+        }
284
+    }
285
+
286
+    private function treeContainsBlacklistedFile(string $path): bool {
287
+        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
288
+        foreach ($iterator as $file) {
289
+            /** @var \SplFileInfo $file */
290
+            if (Filesystem::isFileBlacklisted($file->getBasename())) {
291
+                return true;
292
+            }
293
+        }
294
+
295
+        return false;
296
+    }
297
+
298
+    public function rename($path1, $path2) {
299
+        $srcParent = dirname($path1);
300
+        $dstParent = dirname($path2);
301
+
302
+        if (!$this->isUpdatable($srcParent)) {
303
+            \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, ILogger::ERROR);
304
+            return false;
305
+        }
306
+
307
+        if (!$this->isUpdatable($dstParent)) {
308
+            \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, ILogger::ERROR);
309
+            return false;
310
+        }
311
+
312
+        if (!$this->file_exists($path1)) {
313
+            \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, ILogger::ERROR);
314
+            return false;
315
+        }
316
+
317
+        if ($this->is_dir($path2)) {
318
+            $this->rmdir($path2);
319
+        } elseif ($this->is_file($path2)) {
320
+            $this->unlink($path2);
321
+        }
322
+
323
+        if ($this->is_dir($path1)) {
324
+            // we can't move folders across devices, use copy instead
325
+            $stat1 = stat(dirname($this->getSourcePath($path1)));
326
+            $stat2 = stat(dirname($this->getSourcePath($path2)));
327
+            if ($stat1['dev'] !== $stat2['dev']) {
328
+                $result = $this->copy($path1, $path2);
329
+                if ($result) {
330
+                    $result &= $this->rmdir($path1);
331
+                }
332
+                return $result;
333
+            }
334
+
335
+            if ($this->treeContainsBlacklistedFile($this->getSourcePath($path1))) {
336
+                throw new ForbiddenException('Invalid path', false);
337
+            }
338
+        }
339
+
340
+        return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
341
+    }
342
+
343
+    public function copy($path1, $path2) {
344
+        if ($this->is_dir($path1)) {
345
+            return parent::copy($path1, $path2);
346
+        } else {
347
+            return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
348
+        }
349
+    }
350
+
351
+    public function fopen($path, $mode) {
352
+        return fopen($this->getSourcePath($path), $mode);
353
+    }
354
+
355
+    public function hash($type, $path, $raw = false) {
356
+        return hash_file($type, $this->getSourcePath($path), $raw);
357
+    }
358
+
359
+    public function free_space($path) {
360
+        $sourcePath = $this->getSourcePath($path);
361
+        // using !is_dir because $sourcePath might be a part file or
362
+        // non-existing file, so we'd still want to use the parent dir
363
+        // in such cases
364
+        if (!is_dir($sourcePath)) {
365
+            // disk_free_space doesn't work on files
366
+            $sourcePath = dirname($sourcePath);
367
+        }
368
+        $space = @disk_free_space($sourcePath);
369
+        if ($space === false || is_null($space)) {
370
+            return \OCP\Files\FileInfo::SPACE_UNKNOWN;
371
+        }
372
+        return $space;
373
+    }
374
+
375
+    public function search($query) {
376
+        return $this->searchInDir($query);
377
+    }
378
+
379
+    public function getLocalFile($path) {
380
+        return $this->getSourcePath($path);
381
+    }
382
+
383
+    public function getLocalFolder($path) {
384
+        return $this->getSourcePath($path);
385
+    }
386
+
387
+    /**
388
+     * @param string $query
389
+     * @param string $dir
390
+     * @return array
391
+     */
392
+    protected function searchInDir($query, $dir = '') {
393
+        $files = [];
394
+        $physicalDir = $this->getSourcePath($dir);
395
+        foreach (scandir($physicalDir) as $item) {
396
+            if (\OC\Files\Filesystem::isIgnoredDir($item)) {
397
+                continue;
398
+            }
399
+            $physicalItem = $physicalDir . '/' . $item;
400
+
401
+            if (strstr(strtolower($item), strtolower($query)) !== false) {
402
+                $files[] = $dir . '/' . $item;
403
+            }
404
+            if (is_dir($physicalItem)) {
405
+                $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
406
+            }
407
+        }
408
+        return $files;
409
+    }
410
+
411
+    /**
412
+     * check if a file or folder has been updated since $time
413
+     *
414
+     * @param string $path
415
+     * @param int $time
416
+     * @return bool
417
+     */
418
+    public function hasUpdated($path, $time) {
419
+        if ($this->file_exists($path)) {
420
+            return $this->filemtime($path) > $time;
421
+        } else {
422
+            return true;
423
+        }
424
+    }
425
+
426
+    /**
427
+     * Get the source path (on disk) of a given path
428
+     *
429
+     * @param string $path
430
+     * @return string
431
+     * @throws ForbiddenException
432
+     */
433
+    public function getSourcePath($path) {
434
+        if (Filesystem::isFileBlacklisted($path)) {
435
+            throw new ForbiddenException('Invalid path', false);
436
+        }
437
+
438
+        $fullPath = $this->datadir . $path;
439
+        $currentPath = $path;
440
+        if ($this->allowSymlinks || $currentPath === '') {
441
+            return $fullPath;
442
+        }
443
+        $pathToResolve = $fullPath;
444
+        $realPath = realpath($pathToResolve);
445
+        while ($realPath === false) { // for non existing files check the parent directory
446
+            $currentPath = dirname($currentPath);
447
+            if ($currentPath === '' || $currentPath === '.') {
448
+                return $fullPath;
449
+            }
450
+            $realPath = realpath($this->datadir . $currentPath);
451
+        }
452
+        if ($realPath) {
453
+            $realPath = $realPath . '/';
454
+        }
455
+        if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
456
+            return $fullPath;
457
+        }
458
+
459
+        \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", ILogger::ERROR);
460
+        throw new ForbiddenException('Following symlinks is not allowed', false);
461
+    }
462
+
463
+    /**
464
+     * {@inheritdoc}
465
+     */
466
+    public function isLocal() {
467
+        return true;
468
+    }
469
+
470
+    /**
471
+     * get the ETag for a file or folder
472
+     *
473
+     * @param string $path
474
+     * @return string
475
+     */
476
+    public function getETag($path) {
477
+        return $this->calculateEtag($path, $this->stat($path));
478
+    }
479
+
480
+    private function calculateEtag(string $path, array $stat): string {
481
+        if ($stat['mode'] & 0x4000) { // is_dir
482
+            return parent::getETag($path);
483
+        } else {
484
+            if ($stat === false) {
485
+                return md5('');
486
+            }
487
+
488
+            $toHash = '';
489
+            if (isset($stat['mtime'])) {
490
+                $toHash .= $stat['mtime'];
491
+            }
492
+            if (isset($stat['ino'])) {
493
+                $toHash .= $stat['ino'];
494
+            }
495
+            if (isset($stat['dev'])) {
496
+                $toHash .= $stat['dev'];
497
+            }
498
+            if (isset($stat['size'])) {
499
+                $toHash .= $stat['size'];
500
+            }
501
+
502
+            return md5($toHash);
503
+        }
504
+    }
505
+
506
+    /**
507
+     * @param IStorage $sourceStorage
508
+     * @param string $sourceInternalPath
509
+     * @param string $targetInternalPath
510
+     * @param bool $preserveMtime
511
+     * @return bool
512
+     */
513
+    public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) {
514
+        if ($sourceStorage->instanceOfStorage(Local::class)) {
515
+            if ($sourceStorage->instanceOfStorage(Jail::class)) {
516
+                /**
517
+                 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
518
+                 */
519
+                $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
520
+            }
521
+            /**
522
+             * @var \OC\Files\Storage\Local $sourceStorage
523
+             */
524
+            $rootStorage = new Local(['datadir' => '/']);
525
+            return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
526
+        } else {
527
+            return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
528
+        }
529
+    }
530
+
531
+    /**
532
+     * @param IStorage $sourceStorage
533
+     * @param string $sourceInternalPath
534
+     * @param string $targetInternalPath
535
+     * @return bool
536
+     */
537
+    public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
538
+        if ($sourceStorage->instanceOfStorage(Local::class)) {
539
+            if ($sourceStorage->instanceOfStorage(Jail::class)) {
540
+                /**
541
+                 * @var \OC\Files\Storage\Wrapper\Jail $sourceStorage
542
+                 */
543
+                $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
544
+            }
545
+            /**
546
+             * @var \OC\Files\Storage\Local $sourceStorage
547
+             */
548
+            $rootStorage = new Local(['datadir' => '/']);
549
+            return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
550
+        } else {
551
+            return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
552
+        }
553
+    }
554
+
555
+    public function writeStream(string $path, $stream, int $size = null): int {
556
+        return (int)file_put_contents($this->getSourcePath($path), $stream);
557
+    }
558 558
 }
Please login to merge, or discard this patch.