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 |
||
| 81 | class View { |
||
| 82 | /** @var string */ |
||
| 83 | private $fakeRoot = ''; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \OCP\Lock\ILockingProvider |
||
| 87 | */ |
||
| 88 | private $lockingProvider; |
||
| 89 | |||
| 90 | private $lockingEnabled; |
||
| 91 | |||
| 92 | private $updaterEnabled = true; |
||
| 93 | |||
| 94 | private $userManager; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $root |
||
| 98 | * @throws \Exception If $root contains an invalid path |
||
| 99 | */ |
||
| 100 | public function __construct($root = '') { |
||
| 113 | |||
| 114 | public function getAbsolutePath($path = '/') { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * change the root to a fake root |
||
| 130 | * |
||
| 131 | * @param string $fakeRoot |
||
| 132 | * @return boolean|null |
||
| 133 | */ |
||
| 134 | public function chroot($fakeRoot) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * get the fake root |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getRoot() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * get path relative to the root of the view |
||
| 154 | * |
||
| 155 | * @param string $path |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getRelativePath($path) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * get the mountpoint of the storage object for a path |
||
| 185 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 186 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 187 | * and does not take the chroot into account ) |
||
| 188 | * |
||
| 189 | * @param string $path |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getMountPoint($path) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * get the mountpoint of the storage object for a path |
||
| 198 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 199 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 200 | * and does not take the chroot into account ) |
||
| 201 | * |
||
| 202 | * @param string $path |
||
| 203 | * @return \OCP\Files\Mount\IMountPoint |
||
| 204 | */ |
||
| 205 | public function getMount($path) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * resolve a path to a storage and internal path |
||
| 211 | * |
||
| 212 | * @param string $path |
||
| 213 | * @return array an array consisting of the storage and the internal path |
||
| 214 | */ |
||
| 215 | public function resolvePath($path) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * return the path to a local version of the file |
||
| 223 | * we need this because we can't know if a file is stored local or not from |
||
| 224 | * outside the filestorage and for some purposes a local file is needed |
||
| 225 | * |
||
| 226 | * @param string $path |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | View Code Duplication | public function getLocalFile($path) { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $path |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function getLocalFolder($path) { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * the following functions operate with arguments and return values identical |
||
| 257 | * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
||
| 258 | * for \OC\Files\Storage\Storage via basicOperation(). |
||
| 259 | */ |
||
| 260 | public function mkdir($path) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * remove mount point |
||
| 266 | * |
||
| 267 | * @param \OC\Files\Mount\MoveableMount $mount |
||
| 268 | * @param string $path relative to data/ |
||
| 269 | * @return boolean |
||
| 270 | */ |
||
| 271 | protected function removeMount($mount, $path) { |
||
| 299 | |||
| 300 | public function disableCacheUpdate() { |
||
| 303 | |||
| 304 | public function enableCacheUpdate() { |
||
| 307 | |||
| 308 | protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
||
| 316 | |||
| 317 | protected function removeUpdate(Storage $storage, $internalPath) { |
||
| 322 | |||
| 323 | protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $path |
||
| 331 | * @return bool|mixed |
||
| 332 | */ |
||
| 333 | public function rmdir($path) { |
||
| 334 | $absolutePath = $this->getAbsolutePath($path); |
||
| 335 | $mount = Filesystem::getMountManager()->find($absolutePath); |
||
| 336 | if ($mount->getInternalPath($absolutePath) === '') { |
||
| 337 | return $this->removeMount($mount, $absolutePath); |
||
|
|
|||
| 338 | } |
||
| 339 | if ($this->is_dir($path)) { |
||
| 340 | $result = $this->basicOperation('rmdir', $path, ['delete']); |
||
| 341 | } else { |
||
| 342 | $result = false; |
||
| 343 | } |
||
| 344 | |||
| 345 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 346 | $storage = $mount->getStorage(); |
||
| 347 | $internalPath = $mount->getInternalPath($absolutePath); |
||
| 348 | $storage->getUpdater()->remove($internalPath); |
||
| 349 | } |
||
| 350 | return $result; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $path |
||
| 355 | * @return resource |
||
| 356 | */ |
||
| 357 | public function opendir($path) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param $handle |
||
| 363 | * @return mixed |
||
| 364 | */ |
||
| 365 | public function readdir($handle) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $path |
||
| 372 | * @return bool|mixed |
||
| 373 | */ |
||
| 374 | public function is_dir($path) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $path |
||
| 383 | * @return bool|mixed |
||
| 384 | */ |
||
| 385 | public function is_file($path) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $path |
||
| 394 | * @return mixed |
||
| 395 | */ |
||
| 396 | public function stat($path) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $path |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | public function filetype($path) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $path |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | public function filesize($path) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $path |
||
| 418 | * @return bool|mixed |
||
| 419 | * @throws \OCP\Files\InvalidPathException |
||
| 420 | */ |
||
| 421 | public function readfile($path) { |
||
| 422 | $this->assertPathLength($path); |
||
| 423 | @ob_end_clean(); |
||
| 424 | $handle = $this->fopen($path, 'rb'); |
||
| 425 | if ($handle) { |
||
| 426 | $chunkSize = 8192; // 8 kB chunks |
||
| 427 | while (!feof($handle)) { |
||
| 428 | echo fread($handle, $chunkSize); |
||
| 429 | flush(); |
||
| 430 | } |
||
| 431 | $size = $this->filesize($path); |
||
| 432 | return $size; |
||
| 433 | } |
||
| 434 | return false; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $path |
||
| 439 | * @param int $from |
||
| 440 | * @param int $to |
||
| 441 | * @return bool|mixed |
||
| 442 | * @throws \OCP\Files\InvalidPathException |
||
| 443 | * @throws \OCP\Files\UnseekableException |
||
| 444 | */ |
||
| 445 | public function readfilePart($path, $from, $to) { |
||
| 446 | $this->assertPathLength($path); |
||
| 447 | @ob_end_clean(); |
||
| 448 | $handle = $this->fopen($path, 'rb'); |
||
| 449 | if ($handle) { |
||
| 450 | if (fseek($handle, $from) === 0) { |
||
| 451 | $chunkSize = 8192; // 8 kB chunks |
||
| 452 | $end = $to + 1; |
||
| 453 | while (!feof($handle) && ftell($handle) < $end) { |
||
| 454 | $len = $end - ftell($handle); |
||
| 455 | if ($len > $chunkSize) { |
||
| 456 | $len = $chunkSize; |
||
| 457 | } |
||
| 458 | echo fread($handle, $len); |
||
| 459 | flush(); |
||
| 460 | } |
||
| 461 | $size = ftell($handle) - $from; |
||
| 462 | return $size; |
||
| 463 | } |
||
| 464 | |||
| 465 | throw new \OCP\Files\UnseekableException('fseek error'); |
||
| 466 | } |
||
| 467 | return false; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $path |
||
| 472 | * @return mixed |
||
| 473 | */ |
||
| 474 | public function isCreatable($path) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $path |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function isReadable($path) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $path |
||
| 488 | * @return mixed |
||
| 489 | */ |
||
| 490 | public function isUpdatable($path) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $path |
||
| 496 | * @return bool|mixed |
||
| 497 | */ |
||
| 498 | public function isDeletable($path) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $path |
||
| 509 | * @return mixed |
||
| 510 | */ |
||
| 511 | public function isSharable($path) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param string $path |
||
| 517 | * @return bool|mixed |
||
| 518 | */ |
||
| 519 | public function file_exists($path) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param string $path |
||
| 528 | * @return mixed |
||
| 529 | */ |
||
| 530 | public function filemtime($path) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $path |
||
| 536 | * @param int|string $mtime |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | public function touch($path, $mtime = null) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param string $path |
||
| 568 | * @return mixed |
||
| 569 | */ |
||
| 570 | public function file_get_contents($path) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param bool $exists |
||
| 576 | * @param string $path |
||
| 577 | * @param bool $run |
||
| 578 | */ |
||
| 579 | protected function emit_file_hooks_pre($exists, $path, &$run) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @param bool $exists |
||
| 599 | * @param string $path |
||
| 600 | */ |
||
| 601 | protected function emit_file_hooks_post($exists, $path) { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param string $path |
||
| 618 | * @param mixed $data |
||
| 619 | * @return bool|mixed |
||
| 620 | * @throws \Exception |
||
| 621 | */ |
||
| 622 | public function file_put_contents($path, $data) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @param string $path |
||
| 676 | * @return bool|mixed |
||
| 677 | */ |
||
| 678 | public function unlink($path) { |
||
| 679 | if ($path === '' || $path === '/') { |
||
| 680 | // do not allow deleting the root |
||
| 681 | return false; |
||
| 682 | } |
||
| 683 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
| 684 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 685 | $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
||
| 686 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
||
| 687 | return $this->removeMount($mount, $absolutePath); |
||
| 688 | } |
||
| 689 | if ($this->is_dir($path)) { |
||
| 690 | $result = $this->basicOperation('rmdir', $path, array('delete')); |
||
| 691 | } else { |
||
| 692 | $result = $this->basicOperation('unlink', $path, array('delete')); |
||
| 693 | } |
||
| 694 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 695 | $storage = $mount->getStorage(); |
||
| 696 | $internalPath = $mount->getInternalPath($absolutePath); |
||
| 697 | $storage->getUpdater()->remove($internalPath); |
||
| 698 | return true; |
||
| 699 | } else { |
||
| 700 | return $result; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $directory |
||
| 706 | * @return bool|mixed |
||
| 707 | */ |
||
| 708 | public function deleteAll($directory) { |
||
| 709 | return $this->rmdir($directory); |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Rename/move a file or folder from the source path to target path. |
||
| 714 | * |
||
| 715 | * @param string $path1 source path |
||
| 716 | * @param string $path2 target path |
||
| 717 | * |
||
| 718 | * @return bool|mixed |
||
| 719 | */ |
||
| 720 | public function rename($path1, $path2) { |
||
| 721 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
| 722 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
| 723 | $result = false; |
||
| 724 | if ( |
||
| 725 | Filesystem::isValidPath($path2) |
||
| 726 | and Filesystem::isValidPath($path1) |
||
| 727 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
| 728 | ) { |
||
| 729 | $path1 = $this->getRelativePath($absolutePath1); |
||
| 730 | $path2 = $this->getRelativePath($absolutePath2); |
||
| 731 | $exists = $this->file_exists($path2); |
||
| 732 | |||
| 733 | if ($path1 == null or $path2 == null) { |
||
| 734 | return false; |
||
| 735 | } |
||
| 736 | |||
| 737 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 738 | try { |
||
| 739 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 740 | } catch (LockedException $e) { |
||
| 741 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED); |
||
| 742 | throw $e; |
||
| 743 | } |
||
| 744 | |||
| 745 | $run = true; |
||
| 746 | if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
||
| 747 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
| 748 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
| 749 | } elseif ($this->shouldEmitHooks($path1)) { |
||
| 750 | \OC_Hook::emit( |
||
| 751 | Filesystem::CLASSNAME, Filesystem::signal_rename, |
||
| 752 | [ |
||
| 753 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 754 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
| 755 | Filesystem::signal_param_run => &$run |
||
| 756 | ] |
||
| 757 | ); |
||
| 758 | } |
||
| 759 | if ($run) { |
||
| 760 | $this->verifyPath(dirname($path2), basename($path2)); |
||
| 761 | |||
| 762 | $manager = Filesystem::getMountManager(); |
||
| 763 | $mount1 = $this->getMount($path1); |
||
| 764 | $mount2 = $this->getMount($path2); |
||
| 765 | $storage1 = $mount1->getStorage(); |
||
| 766 | $storage2 = $mount2->getStorage(); |
||
| 767 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
| 768 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
| 769 | |||
| 770 | $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 771 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 772 | |||
| 773 | if ($internalPath1 === '' and $mount1 instanceof MoveableMount) { |
||
| 774 | if ($this->canMove($mount1, $absolutePath2)) { |
||
| 775 | /** |
||
| 776 | * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
||
| 777 | */ |
||
| 778 | $sourceMountPoint = $mount1->getMountPoint(); |
||
| 779 | $result = $mount1->moveMount($absolutePath2); |
||
| 780 | $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
||
| 781 | } else { |
||
| 782 | $result = false; |
||
| 783 | } |
||
| 784 | // moving a file/folder within the same mount point |
||
| 785 | } elseif ($storage1 === $storage2) { |
||
| 786 | if ($storage1) { |
||
| 787 | $result = $storage1->rename($internalPath1, $internalPath2); |
||
| 788 | } else { |
||
| 789 | $result = false; |
||
| 790 | } |
||
| 791 | // moving a file/folder between storages (from $storage1 to $storage2) |
||
| 792 | } else { |
||
| 793 | $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
||
| 794 | } |
||
| 795 | |||
| 796 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
| 797 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
| 798 | |||
| 799 | $this->writeUpdate($storage2, $internalPath2); |
||
| 800 | } else if ($result) { |
||
| 801 | if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
||
| 802 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
||
| 803 | } |
||
| 804 | } |
||
| 805 | |||
| 806 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 807 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 808 | |||
| 809 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
| 810 | if ($this->shouldEmitHooks()) { |
||
| 811 | $this->emit_file_hooks_post($exists, $path2); |
||
| 812 | } |
||
| 813 | } elseif ($result) { |
||
| 814 | if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
||
| 815 | \OC_Hook::emit( |
||
| 816 | Filesystem::CLASSNAME, |
||
| 817 | Filesystem::signal_post_rename, |
||
| 818 | [ |
||
| 819 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 820 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
| 821 | ] |
||
| 822 | ); |
||
| 823 | } |
||
| 824 | } |
||
| 825 | } |
||
| 826 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 827 | $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 828 | } |
||
| 829 | return $result; |
||
| 830 | } |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Copy a file/folder from the source path to target path |
||
| 834 | * |
||
| 835 | * @param string $path1 source path |
||
| 836 | * @param string $path2 target path |
||
| 837 | * @param bool $preserveMtime whether to preserve mtime on the copy |
||
| 838 | * |
||
| 839 | * @return bool|mixed |
||
| 840 | */ |
||
| 841 | public function copy($path1, $path2, $preserveMtime = false) { |
||
| 842 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
| 843 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
| 844 | $result = false; |
||
| 845 | if ( |
||
| 846 | Filesystem::isValidPath($path2) |
||
| 847 | and Filesystem::isValidPath($path1) |
||
| 848 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
| 849 | ) { |
||
| 850 | $path1 = $this->getRelativePath($absolutePath1); |
||
| 851 | $path2 = $this->getRelativePath($absolutePath2); |
||
| 852 | |||
| 853 | if ($path1 == null or $path2 == null) { |
||
| 854 | return false; |
||
| 855 | } |
||
| 856 | $run = true; |
||
| 857 | |||
| 858 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
||
| 859 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
||
| 860 | $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
||
| 861 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
| 862 | |||
| 863 | try { |
||
| 864 | |||
| 865 | $exists = $this->file_exists($path2); |
||
| 866 | View Code Duplication | if ($this->shouldEmitHooks()) { |
|
| 867 | \OC_Hook::emit( |
||
| 868 | Filesystem::CLASSNAME, |
||
| 869 | Filesystem::signal_copy, |
||
| 870 | [ |
||
| 871 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 872 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
| 873 | Filesystem::signal_param_run => &$run |
||
| 874 | ] |
||
| 875 | ); |
||
| 876 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
| 877 | } |
||
| 878 | if ($run) { |
||
| 879 | $mount1 = $this->getMount($path1); |
||
| 880 | $mount2 = $this->getMount($path2); |
||
| 881 | $storage1 = $mount1->getStorage(); |
||
| 882 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
| 883 | $storage2 = $mount2->getStorage(); |
||
| 884 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
| 885 | |||
| 886 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 887 | $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
||
| 888 | |||
| 889 | if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
||
| 890 | if ($storage1) { |
||
| 891 | $result = $storage1->copy($internalPath1, $internalPath2); |
||
| 892 | } else { |
||
| 893 | $result = false; |
||
| 894 | } |
||
| 895 | } else { |
||
| 896 | $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
||
| 897 | } |
||
| 898 | |||
| 899 | $this->writeUpdate($storage2, $internalPath2); |
||
| 900 | |||
| 901 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
||
| 902 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
| 903 | |||
| 904 | View Code Duplication | if ($this->shouldEmitHooks() && $result !== false) { |
|
| 905 | \OC_Hook::emit( |
||
| 906 | Filesystem::CLASSNAME, |
||
| 907 | Filesystem::signal_post_copy, |
||
| 908 | [ |
||
| 909 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 910 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
| 911 | ] |
||
| 912 | ); |
||
| 913 | $this->emit_file_hooks_post($exists, $path2); |
||
| 914 | } |
||
| 915 | |||
| 916 | } |
||
| 917 | } catch (\Exception $e) { |
||
| 918 | $this->unlockFile($path2, $lockTypePath2); |
||
| 919 | $this->unlockFile($path1, $lockTypePath1); |
||
| 920 | throw $e; |
||
| 921 | } |
||
| 922 | |||
| 923 | $this->unlockFile($path2, $lockTypePath2); |
||
| 924 | $this->unlockFile($path1, $lockTypePath1); |
||
| 925 | |||
| 926 | } |
||
| 927 | return $result; |
||
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * @param string $path |
||
| 932 | * @param string $mode |
||
| 933 | * @return resource |
||
| 934 | */ |
||
| 935 | public function fopen($path, $mode) { |
||
| 967 | |||
| 968 | /** |
||
| 969 | * @param string $path |
||
| 970 | * @return bool|string |
||
| 971 | * @throws \OCP\Files\InvalidPathException |
||
| 972 | */ |
||
| 973 | public function toTmpFile($path) { |
||
| 989 | |||
| 990 | /** |
||
| 991 | * @param string $tmpFile |
||
| 992 | * @param string $path |
||
| 993 | * @return bool|mixed |
||
| 994 | * @throws \OCP\Files\InvalidPathException |
||
| 995 | */ |
||
| 996 | public function fromTmpFile($tmpFile, $path) { |
||
| 1029 | |||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * @param string $path |
||
| 1033 | * @return mixed |
||
| 1034 | * @throws \OCP\Files\InvalidPathException |
||
| 1035 | */ |
||
| 1036 | public function getMimeType($path) { |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * @param string $type |
||
| 1043 | * @param string $path |
||
| 1044 | * @param bool $raw |
||
| 1045 | * @return bool|null|string |
||
| 1046 | */ |
||
| 1047 | public function hash($type, $path, $raw = false) { |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * @param string $path |
||
| 1073 | * @return mixed |
||
| 1074 | * @throws \OCP\Files\InvalidPathException |
||
| 1075 | */ |
||
| 1076 | public function free_space($path = '/') { |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
||
| 1083 | * |
||
| 1084 | * @param string $operation |
||
| 1085 | * @param string $path |
||
| 1086 | * @param array $hooks (optional) |
||
| 1087 | * @param mixed $extraParam (optional) |
||
| 1088 | * @return mixed |
||
| 1089 | * @throws \Exception |
||
| 1090 | * |
||
| 1091 | * This method takes requests for basic filesystem functions (e.g. reading & writing |
||
| 1092 | * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
||
| 1093 | * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
||
| 1094 | */ |
||
| 1095 | private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * get the path relative to the default root for hook usage |
||
| 1182 | * |
||
| 1183 | * @param string $path |
||
| 1184 | * @return string |
||
| 1185 | */ |
||
| 1186 | private function getHookPath($path) { |
||
| 1192 | |||
| 1193 | private function shouldEmitHooks($path = '') { |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * @param string[] $hooks |
||
| 1218 | * @param string $path |
||
| 1219 | * @param bool $post |
||
| 1220 | * @return bool |
||
| 1221 | */ |
||
| 1222 | private function runHooks($hooks, $path, $post = false) { |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * check if a file or folder has been updated since $time |
||
| 1254 | * |
||
| 1255 | * @param string $path |
||
| 1256 | * @param int $time |
||
| 1257 | * @return bool |
||
| 1258 | */ |
||
| 1259 | public function hasUpdated($path, $time) { |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * @param string $ownerId |
||
| 1265 | * @return \OC\User\User |
||
| 1266 | */ |
||
| 1267 | private function getUserObjectForOwner($ownerId) { |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Get file info from cache |
||
| 1278 | * |
||
| 1279 | * If the file is not in cached it will be scanned |
||
| 1280 | * If the file has changed on storage the cache will be updated |
||
| 1281 | * |
||
| 1282 | * @param \OC\Files\Storage\Storage $storage |
||
| 1283 | * @param string $internalPath |
||
| 1284 | * @param string $relativePath |
||
| 1285 | * @return array|bool |
||
| 1286 | */ |
||
| 1287 | private function getCacheEntry($storage, $internalPath, $relativePath) { |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * get the filesystem info |
||
| 1320 | * |
||
| 1321 | * @param string $path |
||
| 1322 | * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
||
| 1323 | * 'ext' to add only ext storage mount point sizes. Defaults to true. |
||
| 1324 | * defaults to true |
||
| 1325 | * @return \OC\Files\FileInfo|false False if file does not exist |
||
| 1326 | */ |
||
| 1327 | public function getFileInfo($path, $includeMountPoints = true) { |
||
| 1380 | |||
| 1381 | /** |
||
| 1382 | * get the content of a directory |
||
| 1383 | * |
||
| 1384 | * @param string $directory path under datadirectory |
||
| 1385 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 1386 | * @return FileInfo[] |
||
| 1387 | */ |
||
| 1388 | public function getDirectoryContent($directory, $mimetype_filter = '') { |
||
| 1515 | |||
| 1516 | /** |
||
| 1517 | * change file metadata |
||
| 1518 | * |
||
| 1519 | * @param string $path |
||
| 1520 | * @param array|\OCP\Files\FileInfo $data |
||
| 1521 | * @return int |
||
| 1522 | * |
||
| 1523 | * returns the fileid of the updated file |
||
| 1524 | */ |
||
| 1525 | public function putFileInfo($path, $data) { |
||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * search for files with the name matching $query |
||
| 1552 | * |
||
| 1553 | * @param string $query |
||
| 1554 | * @return FileInfo[] |
||
| 1555 | */ |
||
| 1556 | public function search($query) { |
||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * search for files with the name matching $query |
||
| 1562 | * |
||
| 1563 | * @param string $query |
||
| 1564 | * @return FileInfo[] |
||
| 1565 | */ |
||
| 1566 | public function searchRaw($query) { |
||
| 1569 | |||
| 1570 | /** |
||
| 1571 | * search for files by mimetype |
||
| 1572 | * |
||
| 1573 | * @param string $mimetype |
||
| 1574 | * @return FileInfo[] |
||
| 1575 | */ |
||
| 1576 | public function searchByMime($mimetype) { |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * search for files by tag |
||
| 1582 | * |
||
| 1583 | * @param string|int $tag name or tag id |
||
| 1584 | * @param string $userId owner of the tags |
||
| 1585 | * @return FileInfo[] |
||
| 1586 | */ |
||
| 1587 | public function searchByTag($tag, $userId) { |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * @param string $method cache method |
||
| 1593 | * @param array $args |
||
| 1594 | * @return FileInfo[] |
||
| 1595 | */ |
||
| 1596 | private function searchCommon($method, $args) { |
||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * Get the owner for a file or folder |
||
| 1643 | * |
||
| 1644 | * @param string $path |
||
| 1645 | * @return string the user id of the owner |
||
| 1646 | * @throws NotFoundException |
||
| 1647 | */ |
||
| 1648 | public function getOwner($path) { |
||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * get the ETag for a file or folder |
||
| 1658 | * |
||
| 1659 | * @param string $path |
||
| 1660 | * @return string |
||
| 1661 | */ |
||
| 1662 | public function getETag($path) { |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Get the path of a file by id, relative to the view |
||
| 1677 | * |
||
| 1678 | * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
||
| 1679 | * |
||
| 1680 | * @param int $id |
||
| 1681 | * @param bool $includeShares whether to recurse into shared mounts |
||
| 1682 | * @throws NotFoundException |
||
| 1683 | * @return string |
||
| 1684 | */ |
||
| 1685 | public function getPath($id, $includeShares = true) { |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * @param string $path |
||
| 1718 | * @throws InvalidPathException |
||
| 1719 | */ |
||
| 1720 | private function assertPathLength($path) { |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * check if it is allowed to move a mount point to a given target. |
||
| 1732 | * It is not allowed to move a mount point into a different mount point or |
||
| 1733 | * into an already shared folder |
||
| 1734 | * |
||
| 1735 | * @param MoveableMount $mount1 moveable mount |
||
| 1736 | * @param string $target absolute target path |
||
| 1737 | * @return boolean |
||
| 1738 | */ |
||
| 1739 | private function canMove(MoveableMount $mount1, $target) { |
||
| 1751 | |||
| 1752 | /** |
||
| 1753 | * Get a fileinfo object for files that are ignored in the cache (part files) |
||
| 1754 | * |
||
| 1755 | * @param string $path |
||
| 1756 | * @return \OCP\Files\FileInfo |
||
| 1757 | */ |
||
| 1758 | private function getPartFileInfo($path) { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * @param string $path |
||
| 1784 | * @param string $fileName |
||
| 1785 | * @throws InvalidPathException |
||
| 1786 | */ |
||
| 1787 | public function verifyPath($path, $fileName) { |
||
| 1823 | |||
| 1824 | /** |
||
| 1825 | * get all parent folders of $path |
||
| 1826 | * |
||
| 1827 | * @param string $path |
||
| 1828 | * @return string[] |
||
| 1829 | */ |
||
| 1830 | private function getParents($path) { |
||
| 1850 | |||
| 1851 | /** |
||
| 1852 | * Returns the mount point for which to lock |
||
| 1853 | * |
||
| 1854 | * @param string $absolutePath absolute path |
||
| 1855 | * @param bool $useParentMount true to return parent mount instead of whatever |
||
| 1856 | * is mounted directly on the given path, false otherwise |
||
| 1857 | * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
||
| 1858 | */ |
||
| 1859 | private function getMountForLock($absolutePath, $useParentMount = false) { |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Lock the given path |
||
| 1880 | * |
||
| 1881 | * @param string $path the path of the file to lock, relative to the view |
||
| 1882 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1883 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1884 | * |
||
| 1885 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1886 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 1887 | */ |
||
| 1888 | View Code Duplication | private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1917 | |||
| 1918 | /** |
||
| 1919 | * Change the lock type |
||
| 1920 | * |
||
| 1921 | * @param string $path the path of the file to lock, relative to the view |
||
| 1922 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1923 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1924 | * |
||
| 1925 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1926 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 1927 | */ |
||
| 1928 | View Code Duplication | public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Unlock the given path |
||
| 1961 | * |
||
| 1962 | * @param string $path the path of the file to unlock, relative to the view |
||
| 1963 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1964 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1965 | * |
||
| 1966 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1967 | */ |
||
| 1968 | private function unlockPath($path, $type, $lockMountPoint = false) { |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * Lock a path and all its parents up to the root of the view |
||
| 1992 | * |
||
| 1993 | * @param string $path the path of the file to lock relative to the view |
||
| 1994 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1995 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1996 | * |
||
| 1997 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1998 | */ |
||
| 1999 | View Code Duplication | public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2015 | |||
| 2016 | /** |
||
| 2017 | * Unlock a path and all its parents up to the root of the view |
||
| 2018 | * |
||
| 2019 | * @param string $path the path of the file to lock relative to the view |
||
| 2020 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2021 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2022 | * |
||
| 2023 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2024 | */ |
||
| 2025 | View Code Duplication | public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2041 | |||
| 2042 | /** |
||
| 2043 | * Only lock files in data/user/files/ |
||
| 2044 | * |
||
| 2045 | * @param string $path Absolute path to the file/folder we try to (un)lock |
||
| 2046 | * @return bool |
||
| 2047 | */ |
||
| 2048 | protected function shouldLockFile($path) { |
||
| 2059 | |||
| 2060 | /** |
||
| 2061 | * Shortens the given absolute path to be relative to |
||
| 2062 | * "$user/files". |
||
| 2063 | * |
||
| 2064 | * @param string $absolutePath absolute path which is under "files" |
||
| 2065 | * |
||
| 2066 | * @return string path relative to "files" with trimmed slashes or null |
||
| 2067 | * if the path was NOT relative to files |
||
| 2068 | * |
||
| 2069 | * @throws \InvalidArgumentException if the given path was not under "files" |
||
| 2070 | * @since 8.1.0 |
||
| 2071 | */ |
||
| 2072 | public function getPathRelativeToFiles($absolutePath) { |
||
| 2084 | |||
| 2085 | /** |
||
| 2086 | * @param string $filename |
||
| 2087 | * @return array |
||
| 2088 | * @throws \OC\User\NoUserException |
||
| 2089 | * @throws NotFoundException |
||
| 2090 | */ |
||
| 2091 | public function getUidAndFilename($filename) { |
||
| 2108 | |||
| 2109 | /** |
||
| 2110 | * Creates parent non-existing folders |
||
| 2111 | * |
||
| 2112 | * @param string $filePath |
||
| 2113 | * @return bool |
||
| 2114 | */ |
||
| 2115 | private function createParentDirectories($filePath) { |
||
| 2126 | } |
||
| 2127 |
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: