Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like View often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
82 | class View { |
||
83 | /** @var string */ |
||
84 | private $fakeRoot = ''; |
||
85 | |||
86 | /** |
||
87 | * @var \OCP\Lock\ILockingProvider |
||
88 | */ |
||
89 | private $lockingProvider; |
||
90 | |||
91 | private $lockingEnabled; |
||
92 | |||
93 | private $updaterEnabled = true; |
||
94 | |||
95 | private $userManager; |
||
96 | |||
97 | |||
98 | /** |
||
99 | * @param string $root |
||
100 | * @throws \Exception If $root contains an invalid path |
||
101 | */ |
||
102 | public function __construct($root = '') { |
||
103 | if (is_null($root)) { |
||
104 | throw new \InvalidArgumentException('Root can\'t be null'); |
||
105 | } |
||
106 | if (!Filesystem::isValidPath($root)) { |
||
107 | throw new \Exception(); |
||
108 | } |
||
109 | |||
110 | $this->fakeRoot = $root; |
||
111 | $this->lockingProvider = \OC::$server->getLockingProvider(); |
||
112 | $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
||
113 | $this->userManager = \OC::$server->getUserManager(); |
||
114 | } |
||
115 | |||
116 | public function getAbsolutePath($path = '/') { |
||
117 | if ($path === null) { |
||
118 | return null; |
||
119 | } |
||
120 | $this->assertPathLength($path); |
||
121 | if ($path === '') { |
||
122 | $path = '/'; |
||
123 | } |
||
124 | if ($path[0] !== '/') { |
||
125 | $path = '/' . $path; |
||
126 | } |
||
127 | return $this->fakeRoot . $path; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * change the root to a fake root |
||
132 | * |
||
133 | * @param string $fakeRoot |
||
134 | * @return boolean|null |
||
135 | */ |
||
136 | public function chroot($fakeRoot) { |
||
137 | if (!$fakeRoot == '') { |
||
138 | if ($fakeRoot[0] !== '/') { |
||
139 | $fakeRoot = '/' . $fakeRoot; |
||
140 | } |
||
141 | } |
||
142 | $this->fakeRoot = $fakeRoot; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * get the fake root |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getRoot() { |
||
151 | return $this->fakeRoot; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * get path relative to the root of the view |
||
156 | * |
||
157 | * @param string $path |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getRelativePath($path) { |
||
161 | $this->assertPathLength($path); |
||
162 | if ($this->fakeRoot == '') { |
||
163 | return $path; |
||
164 | } |
||
165 | |||
166 | if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
||
167 | return '/'; |
||
168 | } |
||
169 | |||
170 | // missing slashes can cause wrong matches! |
||
171 | $root = rtrim($this->fakeRoot, '/') . '/'; |
||
172 | |||
173 | if (strpos($path, $root) !== 0) { |
||
174 | return null; |
||
175 | } else { |
||
176 | $path = substr($path, strlen($this->fakeRoot)); |
||
177 | if (strlen($path) === 0) { |
||
178 | return '/'; |
||
179 | } else { |
||
180 | return $path; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * get the mountpoint of the storage object for a path |
||
187 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
188 | * returned mountpoint is relative to the absolute root of the filesystem |
||
189 | * and does not take the chroot into account ) |
||
190 | * |
||
191 | * @param string $path |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getMountPoint($path) { |
||
195 | return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * get the mountpoint of the storage object for a path |
||
200 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
201 | * returned mountpoint is relative to the absolute root of the filesystem |
||
202 | * and does not take the chroot into account ) |
||
203 | * |
||
204 | * @param string $path |
||
205 | * @return \OCP\Files\Mount\IMountPoint |
||
206 | */ |
||
207 | public function getMount($path) { |
||
208 | return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * resolve a path to a storage and internal path |
||
213 | * |
||
214 | * @param string $path |
||
215 | * @return array an array consisting of the storage and the internal path |
||
216 | */ |
||
217 | public function resolvePath($path) { |
||
218 | $a = $this->getAbsolutePath($path); |
||
219 | $p = Filesystem::normalizePath($a); |
||
220 | return Filesystem::resolvePath($p); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * return the path to a local version of the file |
||
225 | * we need this because we can't know if a file is stored local or not from |
||
226 | * outside the filestorage and for some purposes a local file is needed |
||
227 | * |
||
228 | * @param string $path |
||
229 | * @return string |
||
230 | */ |
||
231 | View Code Duplication | public function getLocalFile($path) { |
|
232 | $parent = substr($path, 0, strrpos($path, '/')); |
||
233 | $path = $this->getAbsolutePath($path); |
||
234 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
235 | if (Filesystem::isValidPath($parent) and $storage) { |
||
236 | return $storage->getLocalFile($internalPath); |
||
237 | } else { |
||
238 | return null; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string $path |
||
244 | * @return string |
||
245 | */ |
||
246 | View Code Duplication | public function getLocalFolder($path) { |
|
247 | $parent = substr($path, 0, strrpos($path, '/')); |
||
248 | $path = $this->getAbsolutePath($path); |
||
249 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
250 | if (Filesystem::isValidPath($parent) and $storage) { |
||
251 | return $storage->getLocalFolder($internalPath); |
||
252 | } else { |
||
253 | return null; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * the following functions operate with arguments and return values identical |
||
259 | * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
||
260 | * for \OC\Files\Storage\Storage via basicOperation(). |
||
261 | */ |
||
262 | public function mkdir($path) { |
||
263 | return $this->basicOperation('mkdir', $path, ['create', 'write']); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * remove mount point |
||
268 | * |
||
269 | * @param \OC\Files\Mount\MoveableMount $mount |
||
270 | * @param string $path relative to data/ |
||
271 | * @return boolean |
||
272 | */ |
||
273 | protected function removeMount($mount, $path) { |
||
274 | if ($mount instanceof MoveableMount) { |
||
275 | // cut of /user/files to get the relative path to data/user/files |
||
276 | $pathParts = explode('/', $path, 4); |
||
277 | $relPath = '/' . $pathParts[3]; |
||
278 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
279 | \OC_Hook::emit( |
||
280 | Filesystem::CLASSNAME, "umount", |
||
281 | [Filesystem::signal_param_path => $relPath] |
||
282 | ); |
||
283 | $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
284 | $result = $mount->removeMount(); |
||
285 | $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
||
286 | if ($result) { |
||
287 | \OC_Hook::emit( |
||
288 | Filesystem::CLASSNAME, "post_umount", |
||
289 | [Filesystem::signal_param_path => $relPath] |
||
290 | ); |
||
291 | } |
||
292 | $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
293 | return $result; |
||
294 | } else { |
||
295 | // do not allow deleting the storage's root / the mount point |
||
296 | // because for some storages it might delete the whole contents |
||
297 | // but isn't supposed to work that way |
||
298 | return false; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | public function disableCacheUpdate() { |
||
303 | $this->updaterEnabled = false; |
||
304 | } |
||
305 | |||
306 | public function enableCacheUpdate() { |
||
307 | $this->updaterEnabled = true; |
||
308 | } |
||
309 | |||
310 | protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
||
311 | if ($this->updaterEnabled) { |
||
312 | if (is_null($time)) { |
||
313 | $time = time(); |
||
314 | } |
||
315 | $storage->getUpdater()->update($internalPath, $time); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | protected function removeUpdate(Storage $storage, $internalPath) { |
||
320 | if ($this->updaterEnabled) { |
||
321 | $storage->getUpdater()->remove($internalPath); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
||
326 | if ($this->updaterEnabled) { |
||
327 | $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param string $path |
||
333 | * @return bool|mixed |
||
334 | */ |
||
335 | public function rmdir($path) { |
||
336 | $absolutePath = $this->getAbsolutePath($path); |
||
337 | $mount = Filesystem::getMountManager()->find($absolutePath); |
||
338 | if ($mount->getInternalPath($absolutePath) === '') { |
||
339 | return $this->removeMount($mount, $absolutePath); |
||
|
|||
340 | } |
||
341 | if ($this->is_dir($path)) { |
||
342 | $result = $this->basicOperation('rmdir', $path, ['delete']); |
||
343 | } else { |
||
344 | $result = false; |
||
345 | } |
||
346 | |||
347 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
348 | $storage = $mount->getStorage(); |
||
349 | $internalPath = $mount->getInternalPath($absolutePath); |
||
350 | $storage->getUpdater()->remove($internalPath); |
||
351 | } |
||
352 | return $result; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param string $path |
||
357 | * @return resource |
||
358 | */ |
||
359 | public function opendir($path) { |
||
360 | return $this->basicOperation('opendir', $path, ['read']); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $path |
||
365 | * @return bool|mixed |
||
366 | */ |
||
367 | public function is_dir($path) { |
||
373 | |||
374 | /** |
||
375 | * @param string $path |
||
376 | * @return bool|mixed |
||
377 | */ |
||
378 | public function is_file($path) { |
||
384 | |||
385 | /** |
||
386 | * @param string $path |
||
387 | * @return mixed |
||
388 | */ |
||
389 | public function stat($path) { |
||
392 | |||
393 | /** |
||
394 | * @param string $path |
||
395 | * @return mixed |
||
396 | */ |
||
397 | public function filetype($path) { |
||
400 | |||
401 | /** |
||
402 | * @param string $path |
||
403 | * @return mixed |
||
404 | */ |
||
405 | public function filesize($path) { |
||
408 | |||
409 | /** |
||
410 | * @param string $path |
||
411 | * @return bool|mixed |
||
412 | * @throws \OCP\Files\InvalidPathException |
||
413 | */ |
||
414 | public function readfile($path) { |
||
429 | |||
430 | /** |
||
431 | * @param string $path |
||
432 | * @param int $from |
||
433 | * @param int $to |
||
434 | * @return bool|mixed |
||
435 | * @throws \OCP\Files\InvalidPathException |
||
436 | * @throws \OCP\Files\UnseekableException |
||
437 | */ |
||
438 | public function readfilePart($path, $from, $to) { |
||
439 | $this->assertPathLength($path); |
||
440 | @ob_end_clean(); |
||
441 | $handle = $this->fopen($path, 'rb'); |
||
442 | if ($handle) { |
||
443 | if (fseek($handle, $from) === 0) { |
||
444 | $chunkSize = 8192; // 8 kB chunks |
||
445 | $end = $to + 1; |
||
446 | while (!feof($handle) && ftell($handle) < $end) { |
||
447 | $len = $end - ftell($handle); |
||
448 | if ($len > $chunkSize) { |
||
449 | $len = $chunkSize; |
||
450 | } |
||
451 | echo fread($handle, $len); |
||
452 | flush(); |
||
453 | } |
||
454 | $size = ftell($handle) - $from; |
||
455 | return $size; |
||
456 | } |
||
457 | |||
458 | throw new \OCP\Files\UnseekableException('fseek error'); |
||
459 | } |
||
460 | return false; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param string $path |
||
465 | * @return mixed |
||
466 | */ |
||
467 | public function isCreatable($path) { |
||
470 | |||
471 | /** |
||
472 | * @param string $path |
||
473 | * @return mixed |
||
474 | */ |
||
475 | public function isReadable($path) { |
||
478 | |||
479 | /** |
||
480 | * @param string $path |
||
481 | * @return mixed |
||
482 | */ |
||
483 | public function isUpdatable($path) { |
||
486 | |||
487 | /** |
||
488 | * @param string $path |
||
489 | * @return bool|mixed |
||
490 | */ |
||
491 | public function isDeletable($path) { |
||
499 | |||
500 | /** |
||
501 | * @param string $path |
||
502 | * @return mixed |
||
503 | */ |
||
504 | public function isSharable($path) { |
||
507 | |||
508 | /** |
||
509 | * @param string $path |
||
510 | * @return bool|mixed |
||
511 | */ |
||
512 | public function file_exists($path) { |
||
518 | |||
519 | /** |
||
520 | * @param string $path |
||
521 | * @return mixed |
||
522 | */ |
||
523 | public function filemtime($path) { |
||
526 | |||
527 | /** |
||
528 | * @param string $path |
||
529 | * @param int|string $mtime |
||
530 | * @return bool |
||
531 | */ |
||
532 | public function touch($path, $mtime = null) { |
||
558 | |||
559 | /** |
||
560 | * @param string $path |
||
561 | * @return mixed |
||
562 | */ |
||
563 | public function file_get_contents($path) { |
||
566 | |||
567 | /** |
||
568 | * @param bool $exists |
||
569 | * @param string $path |
||
570 | * @param bool $run |
||
571 | */ |
||
572 | protected function emit_file_hooks_pre($exists, $path, &$run) { |
||
589 | |||
590 | /** |
||
591 | * @param bool $exists |
||
592 | * @param string $path |
||
593 | */ |
||
594 | protected function emit_file_hooks_post($exists, $path) { |
||
608 | |||
609 | /** |
||
610 | * @param string $path |
||
611 | * @param mixed $data |
||
612 | * @return bool|mixed |
||
613 | * @throws \Exception |
||
614 | */ |
||
615 | public function file_put_contents($path, $data) { |
||
666 | |||
667 | /** |
||
668 | * @param string $path |
||
669 | * @return bool|mixed |
||
670 | */ |
||
671 | public function unlink($path) { |
||
672 | if ($path === '' || $path === '/') { |
||
673 | // do not allow deleting the root |
||
674 | return false; |
||
675 | } |
||
676 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
677 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
678 | $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
||
679 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
||
680 | return $this->removeMount($mount, $absolutePath); |
||
681 | } |
||
682 | if ($this->is_dir($path)) { |
||
683 | $result = $this->basicOperation('rmdir', $path, array('delete')); |
||
684 | } else { |
||
685 | $result = $this->basicOperation('unlink', $path, array('delete')); |
||
686 | } |
||
687 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
688 | $storage = $mount->getStorage(); |
||
689 | $internalPath = $mount->getInternalPath($absolutePath); |
||
690 | $storage->getUpdater()->remove($internalPath); |
||
691 | return true; |
||
692 | } else { |
||
693 | return $result; |
||
694 | } |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * @param string $directory |
||
699 | * @return bool|mixed |
||
700 | */ |
||
701 | public function deleteAll($directory) { |
||
704 | |||
705 | /** |
||
706 | * Rename/move a file or folder from the source path to target path. |
||
707 | * |
||
708 | * @param string $path1 source path |
||
709 | * @param string $path2 target path |
||
710 | * |
||
711 | * @return bool|mixed |
||
712 | */ |
||
713 | public function rename($path1, $path2) { |
||
714 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
715 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
716 | $result = false; |
||
717 | if ( |
||
718 | Filesystem::isValidPath($path2) |
||
719 | and Filesystem::isValidPath($path1) |
||
720 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
721 | ) { |
||
722 | $path1 = $this->getRelativePath($absolutePath1); |
||
723 | $path2 = $this->getRelativePath($absolutePath2); |
||
724 | $exists = $this->file_exists($path2); |
||
725 | |||
726 | if ($path1 == null or $path2 == null) { |
||
727 | return false; |
||
728 | } |
||
729 | |||
730 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
731 | try { |
||
732 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
733 | } catch (LockedException $e) { |
||
734 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED); |
||
735 | throw $e; |
||
736 | } |
||
737 | |||
738 | $run = true; |
||
739 | if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
||
740 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
741 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
742 | } elseif ($this->shouldEmitHooks($path1)) { |
||
743 | \OC_Hook::emit( |
||
744 | Filesystem::CLASSNAME, Filesystem::signal_rename, |
||
745 | [ |
||
746 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
747 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
748 | Filesystem::signal_param_run => &$run |
||
749 | ] |
||
750 | ); |
||
751 | } |
||
752 | if ($run) { |
||
753 | $this->verifyPath(dirname($path2), basename($path2)); |
||
754 | |||
755 | $manager = Filesystem::getMountManager(); |
||
756 | $mount1 = $this->getMount($path1); |
||
757 | $mount2 = $this->getMount($path2); |
||
758 | $storage1 = $mount1->getStorage(); |
||
759 | $storage2 = $mount2->getStorage(); |
||
760 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
761 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
762 | |||
763 | $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
764 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
765 | |||
766 | if ($internalPath1 === '' and $mount1 instanceof MoveableMount) { |
||
767 | if ($this->canMove($mount1, $absolutePath2)) { |
||
768 | /** |
||
769 | * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
||
770 | */ |
||
771 | $sourceMountPoint = $mount1->getMountPoint(); |
||
772 | $result = $mount1->moveMount($absolutePath2); |
||
773 | $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
||
774 | } else { |
||
775 | $result = false; |
||
776 | } |
||
777 | // moving a file/folder within the same mount point |
||
778 | } elseif ($storage1 === $storage2) { |
||
779 | if ($storage1) { |
||
780 | $result = $storage1->rename($internalPath1, $internalPath2); |
||
781 | } else { |
||
782 | $result = false; |
||
783 | } |
||
784 | // moving a file/folder between storages (from $storage1 to $storage2) |
||
785 | } else { |
||
786 | $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
||
787 | } |
||
788 | |||
789 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
790 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
791 | |||
792 | $this->writeUpdate($storage2, $internalPath2); |
||
793 | } else if ($result) { |
||
794 | if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
||
795 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
||
796 | } |
||
797 | } |
||
798 | |||
799 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
||
800 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
||
801 | |||
802 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
803 | if ($this->shouldEmitHooks()) { |
||
804 | $this->emit_file_hooks_post($exists, $path2); |
||
805 | } |
||
806 | } elseif ($result) { |
||
807 | if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
||
808 | \OC_Hook::emit( |
||
809 | Filesystem::CLASSNAME, |
||
810 | Filesystem::signal_post_rename, |
||
811 | [ |
||
812 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
813 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
814 | ] |
||
815 | ); |
||
816 | } |
||
817 | } |
||
818 | } |
||
819 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
820 | $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
821 | } |
||
822 | return $result; |
||
823 | } |
||
824 | |||
825 | /** |
||
826 | * Copy a file/folder from the source path to target path |
||
827 | * |
||
828 | * @param string $path1 source path |
||
829 | * @param string $path2 target path |
||
830 | * @param bool $preserveMtime whether to preserve mtime on the copy |
||
831 | * |
||
832 | * @return bool|mixed |
||
833 | */ |
||
834 | public function copy($path1, $path2, $preserveMtime = false) { |
||
835 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
836 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
837 | $result = false; |
||
838 | if ( |
||
839 | Filesystem::isValidPath($path2) |
||
840 | and Filesystem::isValidPath($path1) |
||
841 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
842 | ) { |
||
843 | $path1 = $this->getRelativePath($absolutePath1); |
||
844 | $path2 = $this->getRelativePath($absolutePath2); |
||
845 | |||
846 | if ($path1 == null or $path2 == null) { |
||
847 | return false; |
||
848 | } |
||
849 | $run = true; |
||
850 | |||
851 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
||
852 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
||
853 | $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
||
854 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
855 | |||
856 | try { |
||
857 | |||
858 | $exists = $this->file_exists($path2); |
||
859 | View Code Duplication | if ($this->shouldEmitHooks()) { |
|
860 | \OC_Hook::emit( |
||
861 | Filesystem::CLASSNAME, |
||
862 | Filesystem::signal_copy, |
||
863 | [ |
||
864 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
865 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
866 | Filesystem::signal_param_run => &$run |
||
867 | ] |
||
868 | ); |
||
869 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
870 | } |
||
871 | if ($run) { |
||
872 | $mount1 = $this->getMount($path1); |
||
873 | $mount2 = $this->getMount($path2); |
||
874 | $storage1 = $mount1->getStorage(); |
||
875 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
876 | $storage2 = $mount2->getStorage(); |
||
877 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
878 | |||
879 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
||
880 | $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
||
881 | |||
882 | if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
||
883 | if ($storage1) { |
||
884 | $result = $storage1->copy($internalPath1, $internalPath2); |
||
885 | } else { |
||
886 | $result = false; |
||
887 | } |
||
888 | } else { |
||
889 | $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
||
890 | } |
||
891 | |||
892 | $this->writeUpdate($storage2, $internalPath2); |
||
893 | |||
894 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
||
895 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
896 | |||
897 | View Code Duplication | if ($this->shouldEmitHooks() && $result !== false) { |
|
898 | \OC_Hook::emit( |
||
899 | Filesystem::CLASSNAME, |
||
900 | Filesystem::signal_post_copy, |
||
901 | [ |
||
902 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
903 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
904 | ] |
||
905 | ); |
||
906 | $this->emit_file_hooks_post($exists, $path2); |
||
907 | } |
||
908 | |||
909 | } |
||
910 | } catch (\Exception $e) { |
||
911 | $this->unlockFile($path2, $lockTypePath2); |
||
912 | $this->unlockFile($path1, $lockTypePath1); |
||
913 | throw $e; |
||
914 | } |
||
915 | |||
916 | $this->unlockFile($path2, $lockTypePath2); |
||
917 | $this->unlockFile($path1, $lockTypePath1); |
||
918 | |||
919 | } |
||
920 | return $result; |
||
921 | } |
||
922 | |||
923 | /** |
||
924 | * @param string $path |
||
925 | * @param string $mode |
||
926 | * @return resource |
||
927 | */ |
||
928 | public function fopen($path, $mode) { |
||
960 | |||
961 | /** |
||
962 | * @param string $path |
||
963 | * @return bool|string |
||
964 | * @throws \OCP\Files\InvalidPathException |
||
965 | */ |
||
966 | public function toTmpFile($path) { |
||
982 | |||
983 | /** |
||
984 | * @param string $tmpFile |
||
985 | * @param string $path |
||
986 | * @return bool|mixed |
||
987 | * @throws \OCP\Files\InvalidPathException |
||
988 | */ |
||
989 | public function fromTmpFile($tmpFile, $path) { |
||
990 | $this->assertPathLength($path); |
||
991 | if (Filesystem::isValidPath($path)) { |
||
992 | |||
993 | // Get directory that the file is going into |
||
994 | $filePath = dirname($path); |
||
995 | |||
996 | // Create the directories if any |
||
997 | if (!$this->file_exists($filePath)) { |
||
998 | $result = $this->createParentDirectories($filePath); |
||
999 | if($result === false) { |
||
1000 | return false; |
||
1001 | } |
||
1002 | } |
||
1003 | |||
1004 | $source = fopen($tmpFile, 'r'); |
||
1005 | if ($source) { |
||
1006 | $result = $this->file_put_contents($path, $source); |
||
1007 | // $this->file_put_contents() might have already closed |
||
1008 | // the resource, so we check it, before trying to close it |
||
1009 | // to avoid messages in the error log. |
||
1010 | if (is_resource($source)) { |
||
1011 | fclose($source); |
||
1012 | } |
||
1013 | unlink($tmpFile); |
||
1014 | return $result; |
||
1015 | } else { |
||
1016 | return false; |
||
1017 | } |
||
1018 | } else { |
||
1019 | return false; |
||
1020 | } |
||
1021 | } |
||
1022 | |||
1023 | |||
1024 | /** |
||
1025 | * @param string $path |
||
1026 | * @return mixed |
||
1027 | * @throws \OCP\Files\InvalidPathException |
||
1028 | */ |
||
1029 | public function getMimeType($path) { |
||
1033 | |||
1034 | /** |
||
1035 | * @param string $type |
||
1036 | * @param string $path |
||
1037 | * @param bool $raw |
||
1038 | * @return bool|null|string |
||
1039 | */ |
||
1040 | public function hash($type, $path, $raw = false) { |
||
1063 | |||
1064 | /** |
||
1065 | * @param string $path |
||
1066 | * @return mixed |
||
1067 | * @throws \OCP\Files\InvalidPathException |
||
1068 | */ |
||
1069 | public function free_space($path = '/') { |
||
1073 | |||
1074 | /** |
||
1075 | * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
||
1076 | * |
||
1077 | * @param string $operation |
||
1078 | * @param string $path |
||
1079 | * @param array $hooks (optional) |
||
1080 | * @param mixed $extraParam (optional) |
||
1081 | * @return mixed |
||
1082 | * @throws \Exception |
||
1083 | * |
||
1084 | * This method takes requests for basic filesystem functions (e.g. reading & writing |
||
1085 | * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
||
1086 | * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
||
1087 | */ |
||
1088 | private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
||
1089 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
1090 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
1091 | if (Filesystem::isValidPath($path) |
||
1092 | and !Filesystem::isForbiddenFileOrDir($path) |
||
1093 | ) { |
||
1094 | $path = $this->getRelativePath($absolutePath); |
||
1095 | if ($path == null) { |
||
1096 | return false; |
||
1097 | } |
||
1098 | |||
1099 | if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
||
1100 | // always a shared lock during pre-hooks so the hook can read the file |
||
1101 | $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
||
1102 | } |
||
1103 | |||
1104 | $run = $this->runHooks($hooks, $path); |
||
1105 | /** @var \OC\Files\Storage\Storage $storage */ |
||
1106 | list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
||
1107 | if ($run and $storage) { |
||
1108 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
||
1109 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
1110 | } |
||
1111 | try { |
||
1112 | if (!is_null($extraParam)) { |
||
1113 | $result = $storage->$operation($internalPath, $extraParam); |
||
1114 | } else { |
||
1115 | $result = $storage->$operation($internalPath); |
||
1116 | } |
||
1117 | } catch (\Exception $e) { |
||
1118 | View Code Duplication | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1119 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
1120 | } else if (in_array('read', $hooks)) { |
||
1121 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
1122 | } |
||
1123 | throw $e; |
||
1124 | } |
||
1125 | |||
1126 | if (in_array('delete', $hooks) and $result) { |
||
1127 | $this->removeUpdate($storage, $internalPath); |
||
1128 | } |
||
1129 | if (in_array('write', $hooks) and $operation !== 'fopen') { |
||
1130 | $this->writeUpdate($storage, $internalPath); |
||
1131 | } |
||
1132 | if (in_array('touch', $hooks)) { |
||
1133 | $this->writeUpdate($storage, $internalPath, $extraParam); |
||
1134 | } |
||
1135 | |||
1136 | if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
||
1137 | $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
||
1138 | } |
||
1139 | |||
1140 | $unlockLater = false; |
||
1141 | if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
||
1142 | $unlockLater = true; |
||
1143 | // make sure our unlocking callback will still be called if connection is aborted |
||
1144 | ignore_user_abort(true); |
||
1145 | $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
||
1146 | View Code Duplication | if (in_array('write', $hooks)) { |
|
1147 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
1148 | } else if (in_array('read', $hooks)) { |
||
1149 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
1150 | } |
||
1151 | }); |
||
1152 | } |
||
1153 | |||
1154 | if ($this->shouldEmitHooks($path) && $result !== false) { |
||
1155 | if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
||
1156 | $this->runHooks($hooks, $path, true); |
||
1157 | } |
||
1158 | } |
||
1159 | |||
1160 | View Code Duplication | if (!$unlockLater |
|
1161 | && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
||
1162 | ) { |
||
1163 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
1164 | } |
||
1165 | return $result; |
||
1166 | } else { |
||
1167 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
1168 | } |
||
1169 | } |
||
1170 | return null; |
||
1171 | } |
||
1172 | |||
1173 | /** |
||
1174 | * get the path relative to the default root for hook usage |
||
1175 | * |
||
1176 | * @param string $path |
||
1177 | * @return string |
||
1178 | */ |
||
1179 | private function getHookPath($path) { |
||
1180 | if (!Filesystem::getView()) { |
||
1181 | return $path; |
||
1182 | } |
||
1183 | return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
||
1184 | } |
||
1185 | |||
1186 | private function shouldEmitHooks($path = '') { |
||
1187 | if ($path && Cache\Scanner::isPartialFile($path)) { |
||
1188 | return false; |
||
1189 | } |
||
1190 | if (!Filesystem::$loaded) { |
||
1191 | return false; |
||
1192 | } |
||
1193 | $defaultRoot = Filesystem::getRoot(); |
||
1194 | if ($defaultRoot === null) { |
||
1195 | return false; |
||
1196 | } |
||
1197 | if ($this->fakeRoot === $defaultRoot) { |
||
1198 | return true; |
||
1199 | } |
||
1200 | $fullPath = $this->getAbsolutePath($path); |
||
1201 | |||
1202 | if ($fullPath === $defaultRoot) { |
||
1203 | return true; |
||
1204 | } |
||
1205 | |||
1206 | return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * @param string[] $hooks |
||
1211 | * @param string $path |
||
1212 | * @param bool $post |
||
1213 | * @return bool |
||
1214 | */ |
||
1215 | private function runHooks($hooks, $path, $post = false) { |
||
1216 | $relativePath = $path; |
||
1217 | $path = $this->getHookPath($path); |
||
1218 | $prefix = ($post) ? 'post_' : ''; |
||
1219 | $run = true; |
||
1220 | if ($this->shouldEmitHooks($relativePath)) { |
||
1221 | foreach ($hooks as $hook) { |
||
1222 | if ($hook != 'read') { |
||
1223 | \OC_Hook::emit( |
||
1224 | Filesystem::CLASSNAME, |
||
1225 | $prefix . $hook, |
||
1226 | [ |
||
1227 | Filesystem::signal_param_run => &$run, |
||
1228 | Filesystem::signal_param_path => $path |
||
1229 | ] |
||
1230 | ); |
||
1231 | } elseif (!$post) { |
||
1232 | \OC_Hook::emit( |
||
1233 | Filesystem::CLASSNAME, |
||
1234 | $prefix . $hook, |
||
1235 | [ |
||
1236 | Filesystem::signal_param_path => $path |
||
1237 | ] |
||
1238 | ); |
||
1239 | } |
||
1240 | } |
||
1241 | } |
||
1242 | return $run; |
||
1243 | } |
||
1244 | |||
1245 | /** |
||
1246 | * check if a file or folder has been updated since $time |
||
1247 | * |
||
1248 | * @param string $path |
||
1249 | * @param int $time |
||
1250 | * @return bool |
||
1251 | */ |
||
1252 | public function hasUpdated($path, $time) { |
||
1253 | return $this->basicOperation('hasUpdated', $path, [], $time); |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * @param string $ownerId |
||
1258 | * @return IUser |
||
1259 | */ |
||
1260 | private function getUserObjectForOwner($ownerId) { |
||
1261 | $owner = $this->userManager->get($ownerId); |
||
1262 | if (!$owner instanceof IUser) { |
||
1263 | return new RemoteUser($ownerId); |
||
1264 | } |
||
1265 | |||
1266 | return $owner; |
||
1267 | } |
||
1268 | |||
1269 | /** |
||
1270 | * Get file info from cache |
||
1271 | * |
||
1272 | * If the file is not in cached it will be scanned |
||
1273 | * If the file has changed on storage the cache will be updated |
||
1274 | * |
||
1275 | * @param \OC\Files\Storage\Storage $storage |
||
1276 | * @param string $internalPath |
||
1277 | * @param string $relativePath |
||
1278 | * @return array|bool |
||
1279 | */ |
||
1280 | private function getCacheEntry($storage, $internalPath, $relativePath) { |
||
1281 | $cache = $storage->getCache($internalPath); |
||
1282 | $data = $cache->get($internalPath); |
||
1283 | $watcher = $storage->getWatcher($internalPath); |
||
1284 | |||
1285 | try { |
||
1286 | // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
||
1287 | if (!$data || $data['size'] === -1) { |
||
1288 | $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
||
1289 | if (!$storage->file_exists($internalPath)) { |
||
1290 | $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
||
1291 | return false; |
||
1292 | } |
||
1293 | $scanner = $storage->getScanner($internalPath); |
||
1294 | $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
||
1295 | $data = $cache->get($internalPath); |
||
1296 | $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
||
1297 | } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
||
1298 | $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
||
1299 | $watcher->update($internalPath, $data); |
||
1300 | $storage->getPropagator()->propagateChange($internalPath, time()); |
||
1301 | $data = $cache->get($internalPath); |
||
1302 | $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
||
1303 | } |
||
1304 | } catch (LockedException $e) { |
||
1305 | // if the file is locked we just use the old cache info |
||
1306 | } |
||
1307 | |||
1308 | return $data; |
||
1309 | } |
||
1310 | |||
1311 | /** |
||
1312 | * get the filesystem info |
||
1313 | * |
||
1314 | * @param string $path |
||
1315 | * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
||
1316 | * 'ext' to add only ext storage mount point sizes. Defaults to true. |
||
1317 | * defaults to true |
||
1318 | * @return \OC\Files\FileInfo|false False if file does not exist |
||
1319 | */ |
||
1320 | public function getFileInfo($path, $includeMountPoints = true) { |
||
1321 | $this->assertPathLength($path); |
||
1322 | if (!Filesystem::isValidPath($path)) { |
||
1323 | return false; |
||
1324 | } |
||
1325 | if (Cache\Scanner::isPartialFile($path)) { |
||
1326 | return $this->getPartFileInfo($path); |
||
1327 | } |
||
1328 | $relativePath = $path; |
||
1329 | $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
||
1330 | |||
1331 | $mount = Filesystem::getMountManager()->find($path); |
||
1332 | $storage = $mount->getStorage(); |
||
1333 | $internalPath = $mount->getInternalPath($path); |
||
1334 | if ($storage) { |
||
1335 | $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
||
1336 | |||
1337 | if (!$data instanceof ICacheEntry) { |
||
1338 | return false; |
||
1339 | } |
||
1340 | |||
1341 | if ($mount instanceof MoveableMount && $internalPath === '') { |
||
1342 | $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
||
1343 | } |
||
1344 | |||
1345 | $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
||
1346 | $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
||
1347 | |||
1348 | if ($data and isset($data['fileid'])) { |
||
1349 | if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
||
1350 | //add the sizes of other mount points to the folder |
||
1351 | $extOnly = ($includeMountPoints === 'ext'); |
||
1352 | $mounts = Filesystem::getMountManager()->findIn($path); |
||
1353 | foreach ($mounts as $mount) { |
||
1354 | $subStorage = $mount->getStorage(); |
||
1355 | if ($subStorage) { |
||
1356 | // exclude shared storage ? |
||
1357 | if ($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage) { |
||
1358 | continue; |
||
1359 | } |
||
1360 | $subCache = $subStorage->getCache(''); |
||
1361 | $rootEntry = $subCache->get(''); |
||
1362 | $info->addSubEntry($rootEntry, $mount->getMountPoint()); |
||
1363 | } |
||
1364 | } |
||
1365 | } |
||
1366 | } |
||
1367 | |||
1368 | return $info; |
||
1369 | } |
||
1370 | |||
1371 | return false; |
||
1372 | } |
||
1373 | |||
1374 | /** |
||
1375 | * get the content of a directory |
||
1376 | * |
||
1377 | * @param string $directory path under datadirectory |
||
1378 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
1379 | * @return FileInfo[] |
||
1380 | */ |
||
1381 | public function getDirectoryContent($directory, $mimetype_filter = '') { |
||
1382 | $this->assertPathLength($directory); |
||
1383 | if (!Filesystem::isValidPath($directory)) { |
||
1384 | return []; |
||
1385 | } |
||
1386 | $path = $this->getAbsolutePath($directory); |
||
1387 | $path = Filesystem::normalizePath($path); |
||
1388 | $mount = $this->getMount($directory); |
||
1389 | $storage = $mount->getStorage(); |
||
1390 | $internalPath = $mount->getInternalPath($path); |
||
1391 | if ($storage) { |
||
1392 | $cache = $storage->getCache($internalPath); |
||
1393 | $user = \OC_User::getUser(); |
||
1394 | |||
1395 | $data = $this->getCacheEntry($storage, $internalPath, $directory); |
||
1396 | |||
1397 | if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
||
1398 | return []; |
||
1399 | } |
||
1400 | |||
1401 | $folderId = $data['fileid']; |
||
1402 | $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
||
1403 | |||
1404 | $sharingDisabled = Util::isSharingDisabledForUser(); |
||
1405 | /** |
||
1406 | * @var \OC\Files\FileInfo[] $files |
||
1407 | */ |
||
1408 | $files = array_filter($contents, function(ICacheEntry $content) { |
||
1409 | return (!\OC\Files\Filesystem::isForbiddenFileOrDir($content['path'])); |
||
1410 | }); |
||
1411 | $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
||
1412 | if ($sharingDisabled) { |
||
1413 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
||
1414 | } |
||
1415 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
||
1416 | return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
||
1417 | }, $files); |
||
1418 | |||
1419 | //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
||
1420 | $mounts = Filesystem::getMountManager()->findIn($path); |
||
1421 | $dirLength = strlen($path); |
||
1422 | foreach ($mounts as $mount) { |
||
1423 | $mountPoint = $mount->getMountPoint(); |
||
1424 | $subStorage = $mount->getStorage(); |
||
1425 | if ($subStorage) { |
||
1426 | $subCache = $subStorage->getCache(''); |
||
1427 | |||
1428 | $rootEntry = $subCache->get(''); |
||
1429 | if (!$rootEntry) { |
||
1430 | $subScanner = $subStorage->getScanner(''); |
||
1431 | try { |
||
1432 | $subScanner->scanFile(''); |
||
1433 | } catch (\OCP\Files\StorageNotAvailableException $e) { |
||
1434 | continue; |
||
1435 | } catch (\OCP\Files\StorageInvalidException $e) { |
||
1436 | continue; |
||
1437 | } catch (\Exception $e) { |
||
1438 | // sometimes when the storage is not available it can be any exception |
||
1439 | Util::writeLog( |
||
1440 | 'core', |
||
1441 | 'Exception while scanning storage "' . $subStorage->getId() . '": ' . |
||
1442 | get_class($e) . ': ' . $e->getMessage(), |
||
1443 | Util::ERROR |
||
1444 | ); |
||
1445 | continue; |
||
1446 | } |
||
1447 | $rootEntry = $subCache->get(''); |
||
1448 | } |
||
1449 | |||
1450 | if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
||
1451 | $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
||
1452 | if ($pos = strpos($relativePath, '/')) { |
||
1453 | //mountpoint inside subfolder add size to the correct folder |
||
1454 | $entryName = substr($relativePath, 0, $pos); |
||
1455 | foreach ($files as &$entry) { |
||
1456 | if ($entry->getName() === $entryName) { |
||
1457 | $entry->addSubEntry($rootEntry, $mountPoint); |
||
1458 | } |
||
1459 | } |
||
1460 | } else { //mountpoint in this folder, add an entry for it |
||
1461 | $rootEntry['name'] = $relativePath; |
||
1462 | $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
||
1463 | $permissions = $rootEntry['permissions']; |
||
1464 | // do not allow renaming/deleting the mount point if they are not shared files/folders |
||
1465 | // for shared files/folders we use the permissions given by the owner |
||
1466 | if ($mount instanceof MoveableMount) { |
||
1467 | $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
||
1468 | } else { |
||
1469 | $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
||
1470 | } |
||
1471 | |||
1472 | //remove any existing entry with the same name |
||
1473 | foreach ($files as $i => $file) { |
||
1474 | if ($file['name'] === $rootEntry['name']) { |
||
1475 | unset($files[$i]); |
||
1476 | break; |
||
1477 | } |
||
1478 | } |
||
1479 | $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
||
1480 | |||
1481 | // if sharing was disabled for the user we remove the share permissions |
||
1482 | if (Util::isSharingDisabledForUser()) { |
||
1483 | $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
||
1484 | } |
||
1485 | |||
1486 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
||
1487 | $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
||
1488 | } |
||
1489 | } |
||
1490 | } |
||
1491 | } |
||
1492 | |||
1493 | if ($mimetype_filter) { |
||
1494 | $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
||
1495 | if (strpos($mimetype_filter, '/')) { |
||
1496 | return $file->getMimetype() === $mimetype_filter; |
||
1497 | } else { |
||
1498 | return $file->getMimePart() === $mimetype_filter; |
||
1499 | } |
||
1500 | }); |
||
1501 | } |
||
1502 | |||
1503 | return $files; |
||
1504 | } else { |
||
1505 | return []; |
||
1506 | } |
||
1507 | } |
||
1508 | |||
1509 | /** |
||
1510 | * change file metadata |
||
1511 | * |
||
1512 | * @param string $path |
||
1513 | * @param array|\OCP\Files\FileInfo $data |
||
1514 | * @return int |
||
1515 | * |
||
1516 | * returns the fileid of the updated file |
||
1517 | */ |
||
1518 | public function putFileInfo($path, $data) { |
||
1542 | |||
1543 | /** |
||
1544 | * search for files with the name matching $query |
||
1545 | * |
||
1546 | * @param string $query |
||
1547 | * @return FileInfo[] |
||
1548 | */ |
||
1549 | public function search($query) { |
||
1552 | |||
1553 | /** |
||
1554 | * search for files with the name matching $query |
||
1555 | * |
||
1556 | * @param string $query |
||
1557 | * @return FileInfo[] |
||
1558 | */ |
||
1559 | public function searchRaw($query) { |
||
1562 | |||
1563 | /** |
||
1564 | * search for files by mimetype |
||
1565 | * |
||
1566 | * @param string $mimetype |
||
1567 | * @return FileInfo[] |
||
1568 | */ |
||
1569 | public function searchByMime($mimetype) { |
||
1572 | |||
1573 | /** |
||
1574 | * search for files by tag |
||
1575 | * |
||
1576 | * @param string|int $tag name or tag id |
||
1577 | * @param string $userId owner of the tags |
||
1578 | * @return FileInfo[] |
||
1579 | */ |
||
1580 | public function searchByTag($tag, $userId) { |
||
1583 | |||
1584 | /** |
||
1585 | * @param string $method cache method |
||
1586 | * @param array $args |
||
1587 | * @return FileInfo[] |
||
1588 | */ |
||
1589 | private function searchCommon($method, $args) { |
||
1633 | |||
1634 | /** |
||
1635 | * Get the owner for a file or folder |
||
1636 | * |
||
1637 | * @param string $path |
||
1638 | * @return string the user id of the owner |
||
1639 | * @throws NotFoundException |
||
1640 | */ |
||
1641 | public function getOwner($path) { |
||
1648 | |||
1649 | /** |
||
1650 | * get the ETag for a file or folder |
||
1651 | * |
||
1652 | * @param string $path |
||
1653 | * @return string |
||
1654 | */ |
||
1655 | public function getETag($path) { |
||
1667 | |||
1668 | /** |
||
1669 | * Get the path of a file by id, relative to the view |
||
1670 | * |
||
1671 | * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
||
1672 | * |
||
1673 | * @param int $id |
||
1674 | * @param bool $includeShares whether to recurse into shared mounts |
||
1675 | * @throws NotFoundException |
||
1676 | * @return string |
||
1677 | */ |
||
1678 | public function getPath($id, $includeShares = true) { |
||
1679 | $id = (int)$id; |
||
1680 | $manager = Filesystem::getMountManager(); |
||
1681 | $mounts = $manager->findIn($this->fakeRoot); |
||
1682 | $mounts[] = $manager->find($this->fakeRoot); |
||
1683 | // reverse the array so we start with the storage this view is in |
||
1684 | // which is the most likely to contain the file we're looking for |
||
1685 | $mounts = array_reverse($mounts); |
||
1686 | foreach ($mounts as $mount) { |
||
1687 | /** |
||
1688 | * @var \OC\Files\Mount\MountPoint $mount |
||
1689 | */ |
||
1690 | if (!$includeShares && $mount instanceof SharedMount) { |
||
1691 | // prevent potential infinite loop when instantiating shared storages |
||
1692 | // recursively |
||
1693 | continue; |
||
1694 | } |
||
1695 | if ($mount->getStorage()) { |
||
1696 | $cache = $mount->getStorage()->getCache(); |
||
1697 | $internalPath = $cache->getPathById($id); |
||
1698 | if (is_string($internalPath)) { |
||
1699 | $fullPath = $mount->getMountPoint() . $internalPath; |
||
1700 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
||
1701 | return $path; |
||
1702 | } |
||
1703 | } |
||
1704 | } |
||
1705 | } |
||
1706 | throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
||
1707 | } |
||
1708 | |||
1709 | /** |
||
1710 | * @param string $path |
||
1711 | * @throws InvalidPathException |
||
1712 | */ |
||
1713 | private function assertPathLength($path) { |
||
1722 | |||
1723 | /** |
||
1724 | * check if it is allowed to move a mount point to a given target. |
||
1725 | * It is not allowed to move a mount point into a different mount point or |
||
1726 | * into an already shared folder |
||
1727 | * |
||
1728 | * @param MoveableMount $mount1 moveable mount |
||
1729 | * @param string $target absolute target path |
||
1730 | * @return boolean |
||
1731 | */ |
||
1732 | private function canMove(MoveableMount $mount1, $target) { |
||
1733 | |||
1734 | list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
||
1735 | if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
||
1736 | Util::writeLog('files', |
||
1737 | 'It is not allowed to move one mount point into another one', |
||
1738 | Util::DEBUG); |
||
1739 | return false; |
||
1740 | } |
||
1741 | |||
1742 | return $mount1->isTargetAllowed($target); |
||
1743 | } |
||
1744 | |||
1745 | /** |
||
1746 | * Get a fileinfo object for files that are ignored in the cache (part files) |
||
1747 | * |
||
1748 | * @param string $path |
||
1749 | * @return \OCP\Files\FileInfo |
||
1750 | */ |
||
1751 | private function getPartFileInfo($path) { |
||
1752 | $mount = $this->getMount($path); |
||
1753 | $storage = $mount->getStorage(); |
||
1754 | $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
||
1755 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
||
1756 | return new FileInfo( |
||
1757 | $this->getAbsolutePath($path), |
||
1758 | $storage, |
||
1759 | $internalPath, |
||
1760 | [ |
||
1761 | 'fileid' => null, |
||
1762 | 'mimetype' => $storage->getMimeType($internalPath), |
||
1763 | 'name' => basename($path), |
||
1764 | 'etag' => null, |
||
1765 | 'size' => $storage->filesize($internalPath), |
||
1766 | 'mtime' => $storage->filemtime($internalPath), |
||
1767 | 'encrypted' => false, |
||
1768 | 'permissions' => \OCP\Constants::PERMISSION_ALL |
||
1769 | ], |
||
1770 | $mount, |
||
1771 | $owner |
||
1772 | ); |
||
1773 | } |
||
1774 | |||
1775 | /** |
||
1776 | * @param string $path |
||
1777 | * @param string $fileName |
||
1778 | * @throws InvalidPathException |
||
1779 | */ |
||
1780 | public function verifyPath($path, $fileName) { |
||
1781 | |||
1782 | $l10n = \OC::$server->getL10N('lib'); |
||
1783 | |||
1784 | // verify empty and dot files |
||
1785 | $trimmed = trim($fileName); |
||
1786 | if ($trimmed === '') { |
||
1787 | throw new InvalidPathException($l10n->t('Empty filename is not allowed')); |
||
1788 | } |
||
1789 | if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
||
1790 | throw new InvalidPathException($l10n->t('Dot files are not allowed')); |
||
1791 | } |
||
1792 | |||
1793 | $matches = []; |
||
1794 | |||
1795 | if (preg_match('/' . FileInfo::BLACKLIST_FILES_REGEX . '/', $fileName) !== 0) { |
||
1796 | throw new InvalidPathException( |
||
1797 | "Can`t upload files with extension {$matches[0]} because these extensions are reserved for internal use." |
||
1798 | ); |
||
1799 | } |
||
1800 | |||
1801 | if (!\OC::$server->getDatabaseConnection()->allows4ByteCharacters()) { |
||
1802 | // verify database - e.g. mysql only 3-byte chars |
||
1803 | if (preg_match('%(?: |
||
1804 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
||
1805 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
||
1806 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
||
1807 | )%xs', $fileName)) { |
||
1808 | throw new InvalidPathException($l10n->t('4-byte characters are not supported in file names')); |
||
1809 | } |
||
1810 | } |
||
1811 | |||
1812 | try { |
||
1813 | /** @type \OCP\Files\Storage $storage */ |
||
1814 | list($storage, $internalPath) = $this->resolvePath($path); |
||
1815 | $storage->verifyPath($internalPath, $fileName); |
||
1816 | } catch (ReservedWordException $ex) { |
||
1817 | throw new InvalidPathException($l10n->t('File name is a reserved word')); |
||
1818 | } catch (InvalidCharacterInPathException $ex) { |
||
1819 | throw new InvalidPathException($l10n->t('File name contains at least one invalid character')); |
||
1820 | } catch (FileNameTooLongException $ex) { |
||
1821 | throw new InvalidPathException($l10n->t('File name is too long')); |
||
1822 | } |
||
1823 | } |
||
1824 | |||
1825 | /** |
||
1826 | * get all parent folders of $path |
||
1827 | * |
||
1828 | * @param string $path |
||
1829 | * @return string[] |
||
1830 | */ |
||
1831 | private function getParents($path) { |
||
1851 | |||
1852 | /** |
||
1853 | * Returns the mount point for which to lock |
||
1854 | * |
||
1855 | * @param string $absolutePath absolute path |
||
1856 | * @param bool $useParentMount true to return parent mount instead of whatever |
||
1857 | * is mounted directly on the given path, false otherwise |
||
1858 | * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
||
1859 | */ |
||
1860 | private function getMountForLock($absolutePath, $useParentMount = false) { |
||
1878 | |||
1879 | /** |
||
1880 | * Lock the given path |
||
1881 | * |
||
1882 | * @param string $path the path of the file to lock, relative to the view |
||
1883 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
1884 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
1885 | * |
||
1886 | * @return bool False if the path is excluded from locking, true otherwise |
||
1887 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
1888 | */ |
||
1889 | View Code Duplication | private function lockPath($path, $type, $lockMountPoint = false) { |
|
1918 | |||
1919 | /** |
||
1920 | * Change the lock type |
||
1921 | * |
||
1922 | * @param string $path the path of the file to lock, relative to the view |
||
1923 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
1924 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
1925 | * |
||
1926 | * @return bool False if the path is excluded from locking, true otherwise |
||
1927 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
1928 | */ |
||
1929 | View Code Duplication | public function changeLock($path, $type, $lockMountPoint = false) { |
|
1959 | |||
1960 | /** |
||
1961 | * Unlock the given path |
||
1962 | * |
||
1963 | * @param string $path the path of the file to unlock, relative to the view |
||
1964 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
1965 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
1966 | * |
||
1967 | * @return bool False if the path is excluded from locking, true otherwise |
||
1968 | */ |
||
1969 | private function unlockPath($path, $type, $lockMountPoint = false) { |
||
1990 | |||
1991 | /** |
||
1992 | * Lock a path and all its parents up to the root of the view |
||
1993 | * |
||
1994 | * @param string $path the path of the file to lock relative to the view |
||
1995 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
1996 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
1997 | * |
||
1998 | * @return bool False if the path is excluded from locking, true otherwise |
||
1999 | */ |
||
2000 | View Code Duplication | public function lockFile($path, $type, $lockMountPoint = false) { |
|
2016 | |||
2017 | /** |
||
2018 | * Unlock a path and all its parents up to the root of the view |
||
2019 | * |
||
2020 | * @param string $path the path of the file to lock relative to the view |
||
2021 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
2022 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
2023 | * |
||
2024 | * @return bool False if the path is excluded from locking, true otherwise |
||
2025 | */ |
||
2026 | View Code Duplication | public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2042 | |||
2043 | /** |
||
2044 | * Only lock files in data/user/files/ |
||
2045 | * |
||
2046 | * @param string $path Absolute path to the file/folder we try to (un)lock |
||
2047 | * @return bool |
||
2048 | */ |
||
2049 | protected function shouldLockFile($path) { |
||
2060 | |||
2061 | /** |
||
2062 | * Shortens the given absolute path to be relative to |
||
2063 | * "$user/files". |
||
2064 | * |
||
2065 | * @param string $absolutePath absolute path which is under "files" |
||
2066 | * |
||
2067 | * @return string path relative to "files" with trimmed slashes or null |
||
2068 | * if the path was NOT relative to files |
||
2069 | * |
||
2070 | * @throws \InvalidArgumentException if the given path was not under "files" |
||
2071 | * @since 8.1.0 |
||
2072 | */ |
||
2073 | public function getPathRelativeToFiles($absolutePath) { |
||
2085 | |||
2086 | /** |
||
2087 | * @param string $filename |
||
2088 | * @return array |
||
2089 | * @throws \OC\User\NoUserException |
||
2090 | * @throws NotFoundException |
||
2091 | */ |
||
2092 | public function getUidAndFilename($filename) { |
||
2109 | |||
2110 | /** |
||
2111 | * Creates parent non-existing folders |
||
2112 | * |
||
2113 | * @param string $filePath |
||
2114 | * @return bool |
||
2115 | */ |
||
2116 | private function createParentDirectories($filePath) { |
||
2127 | } |
||
2128 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: