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 |
||
| 84 | class View { |
||
| 85 | use EventEmitterTrait; |
||
| 86 | /** @var string */ |
||
| 87 | private $fakeRoot = ''; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \OCP\Lock\ILockingProvider |
||
| 91 | */ |
||
| 92 | private $lockingProvider; |
||
| 93 | |||
| 94 | /** @var bool */ |
||
| 95 | private $lockingEnabled; |
||
| 96 | |||
| 97 | /** @var bool */ |
||
| 98 | private $updaterEnabled = true; |
||
| 99 | |||
| 100 | /** @var \OC\User\Manager */ |
||
| 101 | private $userManager; |
||
| 102 | |||
| 103 | /** @var \OCP\ILogger */ |
||
| 104 | private $logger; |
||
| 105 | |||
| 106 | private $eventDispatcher; |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $root |
||
| 111 | * @throws \Exception If $root contains an invalid path |
||
| 112 | */ |
||
| 113 | public function __construct($root = '') { |
||
| 114 | if (is_null($root)) { |
||
| 115 | throw new \InvalidArgumentException('Root can\'t be null'); |
||
| 116 | } |
||
| 117 | if (!Filesystem::isValidPath($root)) { |
||
| 118 | throw new \Exception(); |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->fakeRoot = $root; |
||
| 122 | $this->lockingProvider = \OC::$server->getLockingProvider(); |
||
| 123 | $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
||
| 124 | $this->userManager = \OC::$server->getUserManager(); |
||
| 125 | $this->logger = \OC::$server->getLogger(); |
||
| 126 | $this->eventDispatcher = \OC::$server->getEventDispatcher(); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getAbsolutePath($path = '/') { |
||
| 130 | if ($path === null) { |
||
| 131 | return null; |
||
| 132 | } |
||
| 133 | $this->assertPathLength($path); |
||
| 134 | if ($path === '') { |
||
| 135 | $path = '/'; |
||
| 136 | } |
||
| 137 | if ($path[0] !== '/') { |
||
| 138 | $path = '/' . $path; |
||
| 139 | } |
||
| 140 | return $this->fakeRoot . $path; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * change the root to a fake root |
||
| 145 | * |
||
| 146 | * @param string $fakeRoot |
||
| 147 | * @return boolean|null |
||
| 148 | */ |
||
| 149 | public function chroot($fakeRoot) { |
||
| 150 | if (!$fakeRoot == '') { |
||
| 151 | if ($fakeRoot[0] !== '/') { |
||
| 152 | $fakeRoot = '/' . $fakeRoot; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | $this->fakeRoot = $fakeRoot; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * get the fake root |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getRoot() { |
||
| 164 | return $this->fakeRoot; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * get path relative to the root of the view |
||
| 169 | * |
||
| 170 | * @param string $path |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getRelativePath($path) { |
||
| 174 | $this->assertPathLength($path); |
||
| 175 | if ($this->fakeRoot == '') { |
||
| 176 | return $path; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
||
| 180 | return '/'; |
||
| 181 | } |
||
| 182 | |||
| 183 | // missing slashes can cause wrong matches! |
||
| 184 | $root = rtrim($this->fakeRoot, '/') . '/'; |
||
| 185 | |||
| 186 | if (strpos($path, $root) !== 0) { |
||
| 187 | return null; |
||
| 188 | } else { |
||
| 189 | $path = substr($path, strlen($this->fakeRoot)); |
||
| 190 | if (strlen($path) === 0) { |
||
| 191 | return '/'; |
||
| 192 | } else { |
||
| 193 | return $path; |
||
| 194 | } |
||
| 195 | } |
||
| 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 string |
||
| 206 | */ |
||
| 207 | public function getMountPoint($path) { |
||
| 208 | return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * get the mountpoint of the storage object for a path |
||
| 213 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 214 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 215 | * and does not take the chroot into account ) |
||
| 216 | * |
||
| 217 | * @param string $path |
||
| 218 | * @return \OCP\Files\Mount\IMountPoint |
||
| 219 | */ |
||
| 220 | public function getMount($path) { |
||
| 221 | return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * resolve a path to a storage and internal path |
||
| 226 | * |
||
| 227 | * @param string $path |
||
| 228 | * @return array an array consisting of the storage and the internal path |
||
| 229 | */ |
||
| 230 | public function resolvePath($path) { |
||
| 231 | $a = $this->getAbsolutePath($path); |
||
| 232 | $p = Filesystem::normalizePath($a); |
||
| 233 | return Filesystem::resolvePath($p); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * return the path to a local version of the file |
||
| 238 | * we need this because we can't know if a file is stored local or not from |
||
| 239 | * outside the filestorage and for some purposes a local file is needed |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function getLocalFile($path) { |
|
| 245 | $parent = substr($path, 0, strrpos($path, '/')); |
||
| 246 | $path = $this->getAbsolutePath($path); |
||
| 247 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
| 248 | if (Filesystem::isValidPath($parent) and $storage) { |
||
| 249 | return $storage->getLocalFile($internalPath); |
||
| 250 | } else { |
||
| 251 | return null; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $path |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function getLocalFolder($path) { |
|
| 260 | $parent = substr($path, 0, strrpos($path, '/')); |
||
| 261 | $path = $this->getAbsolutePath($path); |
||
| 262 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
| 263 | if (Filesystem::isValidPath($parent) and $storage) { |
||
| 264 | return $storage->getLocalFolder($internalPath); |
||
| 265 | } else { |
||
| 266 | return null; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * the following functions operate with arguments and return values identical |
||
| 272 | * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
||
| 273 | * for \OC\Files\Storage\Storage via basicOperation(). |
||
| 274 | */ |
||
| 275 | public function mkdir($path) { |
||
| 276 | return $this->emittingCall(function () use (&$path) { |
||
| 277 | $result = $this->basicOperation('mkdir', $path, ['create', 'write']); |
||
| 278 | return $result; |
||
| 279 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'create'); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * remove mount point |
||
| 284 | * |
||
| 285 | * @param \OC\Files\Mount\MoveableMount $mount |
||
| 286 | * @param string $path relative to data/ |
||
| 287 | * @return boolean |
||
| 288 | */ |
||
| 289 | protected function removeMount($mount, $path) { |
||
| 290 | if ($mount instanceof MoveableMount) { |
||
| 291 | // cut of /user/files to get the relative path to data/user/files |
||
| 292 | $pathParts = explode('/', $path, 4); |
||
| 293 | $relPath = '/' . $pathParts[3]; |
||
| 294 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 295 | \OC_Hook::emit( |
||
| 296 | Filesystem::CLASSNAME, "umount", |
||
| 297 | [Filesystem::signal_param_path => $relPath] |
||
| 298 | ); |
||
| 299 | $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 300 | $result = $mount->removeMount(); |
||
| 301 | $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 302 | if ($result) { |
||
| 303 | \OC_Hook::emit( |
||
| 304 | Filesystem::CLASSNAME, "post_umount", |
||
| 305 | [Filesystem::signal_param_path => $relPath] |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 309 | return $result; |
||
| 310 | } else { |
||
| 311 | // do not allow deleting the storage's root / the mount point |
||
| 312 | // because for some storages it might delete the whole contents |
||
| 313 | // but isn't supposed to work that way |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | public function disableCacheUpdate() { |
||
| 319 | $this->updaterEnabled = false; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function enableCacheUpdate() { |
||
| 323 | $this->updaterEnabled = true; |
||
| 324 | } |
||
| 325 | |||
| 326 | protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
||
| 327 | if ($this->updaterEnabled) { |
||
| 328 | if (is_null($time)) { |
||
| 329 | $time = time(); |
||
| 330 | } |
||
| 331 | $storage->getUpdater()->update($internalPath, $time); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | protected function removeUpdate(Storage $storage, $internalPath) { |
||
| 336 | if ($this->updaterEnabled) { |
||
| 337 | $storage->getUpdater()->remove($internalPath); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 342 | if ($this->updaterEnabled) { |
||
| 343 | $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $path |
||
| 349 | * @return bool|mixed |
||
| 350 | */ |
||
| 351 | public function rmdir($path) { |
||
| 352 | return $this->emittingCall(function () use (&$path) { |
||
| 353 | $absolutePath = $this->getAbsolutePath($path); |
||
| 354 | $mount = Filesystem::getMountManager()->find($absolutePath); |
||
| 355 | if ($mount->getInternalPath($absolutePath) === '') { |
||
| 356 | return $this->removeMount($mount, $absolutePath); |
||
|
|
|||
| 357 | } |
||
| 358 | if ($this->is_dir($path)) { |
||
| 359 | |||
| 360 | $result = $this->basicOperation('rmdir', $path, ['delete']); |
||
| 361 | } else { |
||
| 362 | $result = false; |
||
| 363 | } |
||
| 364 | |||
| 365 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 366 | $storage = $mount->getStorage(); |
||
| 367 | $internalPath = $mount->getInternalPath($absolutePath); |
||
| 368 | $storage->getUpdater()->remove($internalPath); |
||
| 369 | } |
||
| 370 | |||
| 371 | return $result; |
||
| 372 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'delete'); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $path |
||
| 377 | * @return resource |
||
| 378 | */ |
||
| 379 | public function opendir($path) { |
||
| 380 | return $this->basicOperation('opendir', $path, ['read']); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $path |
||
| 385 | * @return bool|mixed |
||
| 386 | */ |
||
| 387 | public function is_dir($path) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $path |
||
| 396 | * @return bool|mixed |
||
| 397 | */ |
||
| 398 | public function is_file($path) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $path |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | public function stat($path) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $path |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | public function filetype($path) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $path |
||
| 423 | * @return mixed |
||
| 424 | */ |
||
| 425 | public function filesize($path) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $path |
||
| 431 | * @return bool|mixed |
||
| 432 | * @throws \OCP\Files\InvalidPathException |
||
| 433 | */ |
||
| 434 | public function readfile($path) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $path |
||
| 452 | * @param int $from |
||
| 453 | * @param int $to |
||
| 454 | * @return bool|mixed |
||
| 455 | * @throws \OCP\Files\InvalidPathException |
||
| 456 | * @throws \OCP\Files\UnseekableException |
||
| 457 | */ |
||
| 458 | public function readfilePart($path, $from, $to) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $path |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | public function isCreatable($path) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $path |
||
| 493 | * @return mixed |
||
| 494 | */ |
||
| 495 | public function isReadable($path) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $path |
||
| 501 | * @return mixed |
||
| 502 | */ |
||
| 503 | public function isUpdatable($path) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $path |
||
| 509 | * @return bool|mixed |
||
| 510 | */ |
||
| 511 | public function isDeletable($path) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $path |
||
| 522 | * @return mixed |
||
| 523 | */ |
||
| 524 | public function isSharable($path) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string $path |
||
| 530 | * @return bool|mixed |
||
| 531 | */ |
||
| 532 | public function file_exists($path) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param string $path |
||
| 541 | * @return mixed |
||
| 542 | */ |
||
| 543 | public function filemtime($path) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $path |
||
| 549 | * @param int|string $mtime |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public function touch($path, $mtime = null) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $path |
||
| 581 | * @return mixed |
||
| 582 | */ |
||
| 583 | public function file_get_contents($path) { |
||
| 584 | return $this->emittingCall(function () use (&$path) { |
||
| 585 | return $this->basicOperation('file_get_contents', $path, ['read']); |
||
| 586 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'read'); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param bool $exists |
||
| 591 | * @param string $path |
||
| 592 | * @param bool $run |
||
| 593 | */ |
||
| 594 | protected function emit_file_hooks_pre($exists, $path, &$run) { |
||
| 595 | $event = new GenericEvent(null); |
||
| 596 | if (!$exists) { |
||
| 597 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
||
| 598 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 599 | Filesystem::signal_param_run => &$run, |
||
| 600 | ]); |
||
| 601 | View Code Duplication | if ($run) { |
|
| 602 | $event->setArgument('run', $run); |
||
| 603 | $this->eventDispatcher->dispatch('file.beforeCreate', $event); |
||
| 604 | if ($event->getArgument('run') === false) { |
||
| 605 | $run = $event->getArgument('run'); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | } else { |
||
| 609 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
||
| 610 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 611 | Filesystem::signal_param_run => &$run, |
||
| 612 | ]); |
||
| 613 | View Code Duplication | if ($run) { |
|
| 614 | $event->setArgument('run', $run); |
||
| 615 | $this->eventDispatcher->dispatch('file.beforeUpdate', $event); |
||
| 616 | if ($event->getArgument('run') === false) { |
||
| 617 | $run = $event->getArgument('run'); |
||
| 618 | } |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
||
| 623 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 624 | Filesystem::signal_param_run => &$run, |
||
| 625 | ]); |
||
| 626 | View Code Duplication | if ($run) { |
|
| 627 | $event->setArgument('run', $run); |
||
| 628 | $this->eventDispatcher->dispatch('file.beforeWrite', $event); |
||
| 629 | if ($event->getArgument('run') === false) { |
||
| 630 | $run = $event->getArgument('run'); |
||
| 631 | } |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param bool $exists |
||
| 637 | * @param string $path |
||
| 638 | */ |
||
| 639 | protected function emit_file_hooks_post($exists, $path) { |
||
| 640 | // A post event so no before event args required |
||
| 641 | return $this->emittingCall(function () use (&$exists, &$path){ |
||
| 642 | View Code Duplication | if (!$exists) { |
|
| 643 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
||
| 644 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 645 | ]); |
||
| 646 | |||
| 647 | } else { |
||
| 648 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
||
| 649 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 650 | ]); |
||
| 651 | } |
||
| 652 | \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
||
| 653 | Filesystem::signal_param_path => $this->getHookPath($path), |
||
| 654 | ]); |
||
| 655 | }, ['before' => ['path' => $path], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'create'); |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param string $path |
||
| 660 | * @param mixed $data |
||
| 661 | * @return bool|mixed |
||
| 662 | * @throws \Exception |
||
| 663 | */ |
||
| 664 | public function file_put_contents($path, $data) { |
||
| 665 | return $this->emittingCall(function () use (&$path, &$data) { |
||
| 666 | if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
||
| 667 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 668 | if (Filesystem::isValidPath($path) |
||
| 669 | and !Filesystem::isForbiddenFileOrDir($path) |
||
| 670 | ) { |
||
| 671 | $path = $this->getRelativePath($absolutePath); |
||
| 672 | |||
| 673 | $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 674 | |||
| 675 | $exists = $this->file_exists($path); |
||
| 676 | $run = true; |
||
| 677 | if ($this->shouldEmitHooks($path)) { |
||
| 678 | $this->emit_file_hooks_pre($exists, $path, $run); |
||
| 679 | } |
||
| 680 | if (!$run) { |
||
| 681 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 682 | return false; |
||
| 683 | } |
||
| 684 | |||
| 685 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 686 | |||
| 687 | /** @var \OC\Files\Storage\Storage $storage */ |
||
| 688 | list($storage, $internalPath) = $this->resolvePath($path); |
||
| 689 | $target = $storage->fopen($internalPath, 'w'); |
||
| 690 | if ($target) { |
||
| 691 | list (, $result) = \OC_Helper::streamCopy($data, $target); |
||
| 692 | fclose($target); |
||
| 693 | fclose($data); |
||
| 694 | |||
| 695 | $this->writeUpdate($storage, $internalPath); |
||
| 696 | |||
| 697 | $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
||
| 698 | |||
| 699 | if ($this->shouldEmitHooks($path) && $result !== false) { |
||
| 700 | $this->emit_file_hooks_post($exists, $path); |
||
| 701 | } |
||
| 702 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 703 | return $result; |
||
| 704 | } else { |
||
| 705 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 706 | return false; |
||
| 707 | } |
||
| 708 | } else { |
||
| 709 | return false; |
||
| 710 | } |
||
| 711 | } else { |
||
| 712 | $hooks = ($this->file_exists($path)) ? ['update', 'write'] : ['create', 'write']; |
||
| 713 | return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
||
| 714 | } |
||
| 715 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'update'); |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @param string $path |
||
| 720 | * @return bool|mixed |
||
| 721 | */ |
||
| 722 | public function unlink($path) { |
||
| 723 | return $this->emittingCall(function () use (&$path) { |
||
| 724 | if ($path === '' || $path === '/') { |
||
| 725 | // do not allow deleting the root |
||
| 726 | return false; |
||
| 727 | } |
||
| 728 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
| 729 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 730 | $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
||
| 731 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
||
| 732 | return $this->removeMount($mount, $absolutePath); |
||
| 733 | } |
||
| 734 | if ($this->is_dir($path)) { |
||
| 735 | $result = $this->basicOperation('rmdir', $path, array('delete')); |
||
| 736 | } else { |
||
| 737 | $result = $this->basicOperation('unlink', $path, array('delete')); |
||
| 738 | } |
||
| 739 | |||
| 740 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 741 | $storage = $mount->getStorage(); |
||
| 742 | $internalPath = $mount->getInternalPath($absolutePath); |
||
| 743 | $storage->getUpdater()->remove($internalPath); |
||
| 744 | return true; |
||
| 745 | } else { |
||
| 746 | return $result; |
||
| 747 | } |
||
| 748 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'delete'); |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param string $directory |
||
| 753 | * @return bool|mixed |
||
| 754 | */ |
||
| 755 | public function deleteAll($directory) { |
||
| 756 | return $this->rmdir($directory); |
||
| 757 | } |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Rename/move a file or folder from the source path to target path. |
||
| 761 | * |
||
| 762 | * @param string $path1 source path |
||
| 763 | * @param string $path2 target path |
||
| 764 | * |
||
| 765 | * @return bool|mixed |
||
| 766 | */ |
||
| 767 | public function rename($path1, $path2) { |
||
| 768 | return $this->emittingCall(function () use (&$path1, &$path2) { |
||
| 769 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
| 770 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
| 771 | $result = false; |
||
| 772 | if ( |
||
| 773 | Filesystem::isValidPath($path2) |
||
| 774 | and Filesystem::isValidPath($path1) |
||
| 775 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
| 776 | ) { |
||
| 777 | $path1 = $this->getRelativePath($absolutePath1); |
||
| 778 | $path2 = $this->getRelativePath($absolutePath2); |
||
| 779 | $exists = $this->file_exists($path2); |
||
| 780 | |||
| 781 | if ($path1 == null or $path2 == null) { |
||
| 782 | return false; |
||
| 783 | } |
||
| 784 | |||
| 785 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 786 | try { |
||
| 787 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 788 | } catch (LockedException $e) { |
||
| 789 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED); |
||
| 790 | throw $e; |
||
| 791 | } |
||
| 792 | |||
| 793 | $run = true; |
||
| 794 | if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
||
| 795 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
| 796 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
| 797 | } elseif ($this->shouldEmitHooks($path1)) { |
||
| 798 | \OC_Hook::emit( |
||
| 799 | Filesystem::CLASSNAME, Filesystem::signal_rename, |
||
| 800 | [ |
||
| 801 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 802 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
| 803 | Filesystem::signal_param_run => &$run |
||
| 804 | ] |
||
| 805 | ); |
||
| 806 | } |
||
| 807 | if ($run) { |
||
| 808 | $this->verifyPath(dirname($path2), basename($path2)); |
||
| 809 | |||
| 810 | $manager = Filesystem::getMountManager(); |
||
| 811 | $mount1 = $this->getMount($path1); |
||
| 812 | $mount2 = $this->getMount($path2); |
||
| 813 | $storage1 = $mount1->getStorage(); |
||
| 814 | $storage2 = $mount2->getStorage(); |
||
| 815 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
| 816 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
| 817 | |||
| 818 | $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 819 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 820 | |||
| 821 | if ($internalPath1 === '' and $mount1 instanceof MoveableMount) { |
||
| 822 | if ($this->canMove($mount1, $absolutePath2)) { |
||
| 823 | /** |
||
| 824 | * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
||
| 825 | */ |
||
| 826 | $sourceMountPoint = $mount1->getMountPoint(); |
||
| 827 | $result = $mount1->moveMount($absolutePath2); |
||
| 828 | $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
||
| 829 | } else { |
||
| 830 | $result = false; |
||
| 831 | } |
||
| 832 | // moving a file/folder within the same mount point |
||
| 833 | } elseif ($storage1 === $storage2) { |
||
| 834 | if ($storage1) { |
||
| 835 | $result = $storage1->rename($internalPath1, $internalPath2); |
||
| 836 | } else { |
||
| 837 | $result = false; |
||
| 838 | } |
||
| 839 | // moving a file/folder between storages (from $storage1 to $storage2) |
||
| 840 | } else { |
||
| 841 | $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
||
| 842 | } |
||
| 843 | |||
| 844 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
| 845 | // if it was a rename from a part file to a regular file it was a write and not a rename operation |
||
| 846 | |||
| 847 | $this->writeUpdate($storage2, $internalPath2); |
||
| 848 | } else if ($result) { |
||
| 849 | if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
||
| 850 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
||
| 851 | } |
||
| 852 | } |
||
| 853 | |||
| 854 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 855 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 856 | |||
| 857 | if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
||
| 858 | if ($this->shouldEmitHooks()) { |
||
| 859 | $this->emit_file_hooks_post($exists, $path2); |
||
| 860 | } |
||
| 861 | } elseif ($result) { |
||
| 862 | if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
||
| 863 | \OC_Hook::emit( |
||
| 864 | Filesystem::CLASSNAME, |
||
| 865 | Filesystem::signal_post_rename, |
||
| 866 | [ |
||
| 867 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 868 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
| 869 | ] |
||
| 870 | ); |
||
| 871 | } |
||
| 872 | } |
||
| 873 | } |
||
| 874 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
||
| 875 | $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
||
| 876 | } |
||
| 877 | |||
| 878 | return $result; |
||
| 879 | }, [ |
||
| 880 | 'before' => ['oldpath' => $this->getAbsolutePath($path1), |
||
| 881 | 'newpath' => $this->getAbsolutePath($path2)], |
||
| 882 | 'after' => ['oldpath' => $this->getAbsolutePath($path1), |
||
| 883 | 'newpath' => $this->getAbsolutePath($path2)] |
||
| 884 | ], 'file', 'rename'); |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Copy a file/folder from the source path to target path |
||
| 889 | * |
||
| 890 | * @param string $path1 source path |
||
| 891 | * @param string $path2 target path |
||
| 892 | * @param bool $preserveMtime whether to preserve mtime on the copy |
||
| 893 | * |
||
| 894 | * @return bool|mixed |
||
| 895 | */ |
||
| 896 | public function copy($path1, $path2, $preserveMtime = false) { |
||
| 897 | return $this->emittingCall(function () use (&$path1, &$path2, &$preserveMtime) { |
||
| 898 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
||
| 899 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
||
| 900 | $result = false; |
||
| 901 | if ( |
||
| 902 | Filesystem::isValidPath($path2) |
||
| 903 | and Filesystem::isValidPath($path1) |
||
| 904 | and !Filesystem::isForbiddenFileOrDir($path2) |
||
| 905 | ) { |
||
| 906 | $path1 = $this->getRelativePath($absolutePath1); |
||
| 907 | $path2 = $this->getRelativePath($absolutePath2); |
||
| 908 | |||
| 909 | if ($path1 == null or $path2 == null) { |
||
| 910 | return false; |
||
| 911 | } |
||
| 912 | $run = true; |
||
| 913 | |||
| 914 | $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
||
| 915 | $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
||
| 916 | $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
||
| 917 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
| 918 | |||
| 919 | try { |
||
| 920 | |||
| 921 | $exists = $this->file_exists($path2); |
||
| 922 | View Code Duplication | if ($this->shouldEmitHooks()) { |
|
| 923 | \OC_Hook::emit( |
||
| 924 | Filesystem::CLASSNAME, |
||
| 925 | Filesystem::signal_copy, |
||
| 926 | [ |
||
| 927 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 928 | Filesystem::signal_param_newpath => $this->getHookPath($path2), |
||
| 929 | Filesystem::signal_param_run => &$run |
||
| 930 | ] |
||
| 931 | ); |
||
| 932 | $this->emit_file_hooks_pre($exists, $path2, $run); |
||
| 933 | } |
||
| 934 | if ($run) { |
||
| 935 | $mount1 = $this->getMount($path1); |
||
| 936 | $mount2 = $this->getMount($path2); |
||
| 937 | $storage1 = $mount1->getStorage(); |
||
| 938 | $internalPath1 = $mount1->getInternalPath($absolutePath1); |
||
| 939 | $storage2 = $mount2->getStorage(); |
||
| 940 | $internalPath2 = $mount2->getInternalPath($absolutePath2); |
||
| 941 | |||
| 942 | $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 943 | $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
||
| 944 | |||
| 945 | if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
||
| 946 | if ($storage1) { |
||
| 947 | $result = $storage1->copy($internalPath1, $internalPath2); |
||
| 948 | } else { |
||
| 949 | $result = false; |
||
| 950 | } |
||
| 951 | } else { |
||
| 952 | $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
||
| 953 | } |
||
| 954 | |||
| 955 | $this->writeUpdate($storage2, $internalPath2); |
||
| 956 | |||
| 957 | $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
||
| 958 | $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
||
| 959 | |||
| 960 | View Code Duplication | if ($this->shouldEmitHooks() && $result !== false) { |
|
| 961 | \OC_Hook::emit( |
||
| 962 | Filesystem::CLASSNAME, |
||
| 963 | Filesystem::signal_post_copy, |
||
| 964 | [ |
||
| 965 | Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
||
| 966 | Filesystem::signal_param_newpath => $this->getHookPath($path2) |
||
| 967 | ] |
||
| 968 | ); |
||
| 969 | $this->emit_file_hooks_post($exists, $path2); |
||
| 970 | } |
||
| 971 | |||
| 972 | } |
||
| 973 | } catch (\Exception $e) { |
||
| 974 | $this->unlockFile($path2, $lockTypePath2); |
||
| 975 | $this->unlockFile($path1, $lockTypePath1); |
||
| 976 | throw $e; |
||
| 977 | } |
||
| 978 | |||
| 979 | $this->unlockFile($path2, $lockTypePath2); |
||
| 980 | $this->unlockFile($path1, $lockTypePath1); |
||
| 981 | |||
| 982 | } |
||
| 983 | |||
| 984 | return $result; |
||
| 985 | }, ['before' => [ |
||
| 986 | 'oldpath' => $this->getAbsolutePath($path1), |
||
| 987 | 'newpath' => $this->getAbsolutePath($path2)], |
||
| 988 | 'after' => [ |
||
| 989 | 'oldpath' => $this->getAbsolutePath($path1), |
||
| 990 | 'newpath' => $this->getAbsolutePath($path2) |
||
| 991 | ]], 'file', 'copy'); |
||
| 992 | } |
||
| 993 | |||
| 994 | /** |
||
| 995 | * @param string $path |
||
| 996 | * @param string $mode |
||
| 997 | * @return resource |
||
| 998 | */ |
||
| 999 | public function fopen($path, $mode) { |
||
| 1000 | $hooks = []; |
||
| 1001 | switch ($mode) { |
||
| 1002 | case 'r': |
||
| 1003 | case 'rb': |
||
| 1004 | $hooks[] = 'read'; |
||
| 1005 | break; |
||
| 1006 | case 'r+': |
||
| 1007 | case 'rb+': |
||
| 1008 | case 'w+': |
||
| 1009 | case 'wb+': |
||
| 1010 | case 'x+': |
||
| 1011 | case 'xb+': |
||
| 1012 | case 'a+': |
||
| 1013 | case 'ab+': |
||
| 1014 | $hooks[] = 'read'; |
||
| 1015 | $hooks[] = 'write'; |
||
| 1016 | break; |
||
| 1017 | case 'w': |
||
| 1018 | case 'wb': |
||
| 1019 | case 'x': |
||
| 1020 | case 'xb': |
||
| 1021 | case 'a': |
||
| 1022 | case 'ab': |
||
| 1023 | $hooks[] = 'write'; |
||
| 1024 | break; |
||
| 1025 | default: |
||
| 1026 | Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, Util::ERROR); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | return $this->basicOperation('fopen', $path, $hooks, $mode); |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @param string $path |
||
| 1034 | * @return bool|string |
||
| 1035 | * @throws \OCP\Files\InvalidPathException |
||
| 1036 | */ |
||
| 1037 | public function toTmpFile($path) { |
||
| 1038 | $this->assertPathLength($path); |
||
| 1039 | if (Filesystem::isValidPath($path)) { |
||
| 1040 | $source = $this->fopen($path, 'r'); |
||
| 1041 | if ($source) { |
||
| 1042 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
||
| 1043 | $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
||
| 1044 | file_put_contents($tmpFile, $source); |
||
| 1045 | return $tmpFile; |
||
| 1046 | } else { |
||
| 1047 | return false; |
||
| 1048 | } |
||
| 1049 | } else { |
||
| 1050 | return false; |
||
| 1051 | } |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * @param string $tmpFile |
||
| 1056 | * @param string $path |
||
| 1057 | * @return bool|mixed |
||
| 1058 | * @throws \OCP\Files\InvalidPathException |
||
| 1059 | */ |
||
| 1060 | public function fromTmpFile($tmpFile, $path) { |
||
| 1061 | $this->assertPathLength($path); |
||
| 1062 | if (Filesystem::isValidPath($path)) { |
||
| 1063 | |||
| 1064 | // Get directory that the file is going into |
||
| 1065 | $filePath = dirname($path); |
||
| 1066 | |||
| 1067 | // Create the directories if any |
||
| 1068 | if (!$this->file_exists($filePath)) { |
||
| 1069 | $result = $this->createParentDirectories($filePath); |
||
| 1070 | if($result === false) { |
||
| 1071 | return false; |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | $source = fopen($tmpFile, 'r'); |
||
| 1076 | if ($source) { |
||
| 1077 | $result = $this->file_put_contents($path, $source); |
||
| 1078 | // $this->file_put_contents() might have already closed |
||
| 1079 | // the resource, so we check it, before trying to close it |
||
| 1080 | // to avoid messages in the error log. |
||
| 1081 | if (is_resource($source)) { |
||
| 1082 | fclose($source); |
||
| 1083 | } |
||
| 1084 | unlink($tmpFile); |
||
| 1085 | return $result; |
||
| 1086 | } else { |
||
| 1087 | return false; |
||
| 1088 | } |
||
| 1089 | } else { |
||
| 1090 | return false; |
||
| 1091 | } |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * @param string $path |
||
| 1097 | * @return mixed |
||
| 1098 | * @throws \OCP\Files\InvalidPathException |
||
| 1099 | */ |
||
| 1100 | public function getMimeType($path) { |
||
| 1101 | $this->assertPathLength($path); |
||
| 1102 | return $this->basicOperation('getMimeType', $path); |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * @param string $type |
||
| 1107 | * @param string $path |
||
| 1108 | * @param bool $raw |
||
| 1109 | * @return bool|null|string |
||
| 1110 | */ |
||
| 1111 | public function hash($type, $path, $raw = false) { |
||
| 1112 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
| 1113 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 1114 | if (Filesystem::isValidPath($path)) { |
||
| 1115 | $path = $this->getRelativePath($absolutePath); |
||
| 1116 | if ($path == null) { |
||
| 1117 | return false; |
||
| 1118 | } |
||
| 1119 | if ($this->shouldEmitHooks($path)) { |
||
| 1120 | \OC_Hook::emit( |
||
| 1121 | Filesystem::CLASSNAME, |
||
| 1122 | Filesystem::signal_read, |
||
| 1123 | [Filesystem::signal_param_path => $this->getHookPath($path)] |
||
| 1124 | ); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
||
| 1128 | if ($storage) { |
||
| 1129 | $result = $storage->hash($type, $internalPath, $raw); |
||
| 1130 | return $result; |
||
| 1131 | } |
||
| 1132 | } |
||
| 1133 | return null; |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * @param string $path |
||
| 1138 | * @return mixed |
||
| 1139 | * @throws \OCP\Files\InvalidPathException |
||
| 1140 | */ |
||
| 1141 | public function free_space($path = '/') { |
||
| 1142 | $this->assertPathLength($path); |
||
| 1143 | return $this->basicOperation('free_space', $path); |
||
| 1144 | } |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
||
| 1148 | * |
||
| 1149 | * @param string $operation |
||
| 1150 | * @param string $path |
||
| 1151 | * @param array $hooks (optional) |
||
| 1152 | * @param mixed $extraParam (optional) |
||
| 1153 | * @return mixed |
||
| 1154 | * @throws \Exception |
||
| 1155 | * |
||
| 1156 | * This method takes requests for basic filesystem functions (e.g. reading & writing |
||
| 1157 | * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
||
| 1158 | * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
||
| 1159 | */ |
||
| 1160 | private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
||
| 1161 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
||
| 1162 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 1163 | if (Filesystem::isValidPath($path) |
||
| 1164 | and !Filesystem::isForbiddenFileOrDir($path) |
||
| 1165 | ) { |
||
| 1166 | $path = $this->getRelativePath($absolutePath); |
||
| 1167 | if ($path == null) { |
||
| 1168 | return false; |
||
| 1169 | } |
||
| 1170 | |||
| 1171 | if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
||
| 1172 | // always a shared lock during pre-hooks so the hook can read the file |
||
| 1173 | $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | $run = $this->runHooks($hooks, $path); |
||
| 1177 | /** @var \OC\Files\Storage\Storage $storage */ |
||
| 1178 | list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
||
| 1179 | if ($run and $storage) { |
||
| 1180 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
||
| 1181 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 1182 | } |
||
| 1183 | try { |
||
| 1184 | if (!is_null($extraParam)) { |
||
| 1185 | $result = $storage->$operation($internalPath, $extraParam); |
||
| 1186 | } else { |
||
| 1187 | $result = $storage->$operation($internalPath); |
||
| 1188 | } |
||
| 1189 | } catch (\Exception $e) { |
||
| 1190 | View Code Duplication | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1191 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 1192 | } else if (in_array('read', $hooks)) { |
||
| 1193 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 1194 | } |
||
| 1195 | throw $e; |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | if (in_array('delete', $hooks) and $result) { |
||
| 1199 | $this->removeUpdate($storage, $internalPath); |
||
| 1200 | } |
||
| 1201 | if (in_array('write', $hooks) and $operation !== 'fopen') { |
||
| 1202 | $this->writeUpdate($storage, $internalPath); |
||
| 1203 | } |
||
| 1204 | if (in_array('touch', $hooks)) { |
||
| 1205 | $this->writeUpdate($storage, $internalPath, $extraParam); |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
||
| 1209 | $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | $unlockLater = false; |
||
| 1213 | if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
||
| 1214 | $unlockLater = true; |
||
| 1215 | // make sure our unlocking callback will still be called if connection is aborted |
||
| 1216 | ignore_user_abort(true); |
||
| 1217 | $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
||
| 1218 | View Code Duplication | if (in_array('write', $hooks)) { |
|
| 1219 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
||
| 1220 | } else if (in_array('read', $hooks)) { |
||
| 1221 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 1222 | } |
||
| 1223 | }); |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | if ($this->shouldEmitHooks($path) && $result !== false) { |
||
| 1227 | if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
||
| 1228 | $this->runHooks($hooks, $path, true); |
||
| 1229 | } |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | View Code Duplication | if (!$unlockLater |
|
| 1233 | && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
||
| 1234 | ) { |
||
| 1235 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 1236 | } |
||
| 1237 | return $result; |
||
| 1238 | } else { |
||
| 1239 | $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
||
| 1240 | } |
||
| 1241 | } |
||
| 1242 | return null; |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * get the path relative to the default root for hook usage |
||
| 1247 | * |
||
| 1248 | * @param string $path |
||
| 1249 | * @return string |
||
| 1250 | */ |
||
| 1251 | private function getHookPath($path) { |
||
| 1252 | if (!Filesystem::getView()) { |
||
| 1253 | return $path; |
||
| 1254 | } |
||
| 1255 | return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | private function shouldEmitHooks($path = '') { |
||
| 1259 | if ($path && Cache\Scanner::isPartialFile($path)) { |
||
| 1260 | return false; |
||
| 1261 | } |
||
| 1262 | if (!Filesystem::$loaded) { |
||
| 1263 | return false; |
||
| 1264 | } |
||
| 1265 | $defaultRoot = Filesystem::getRoot(); |
||
| 1266 | if ($defaultRoot === null) { |
||
| 1267 | return false; |
||
| 1268 | } |
||
| 1269 | if ($this->fakeRoot === $defaultRoot) { |
||
| 1270 | return true; |
||
| 1271 | } |
||
| 1272 | $fullPath = $this->getAbsolutePath($path); |
||
| 1273 | |||
| 1274 | if ($fullPath === $defaultRoot) { |
||
| 1275 | return true; |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | /** |
||
| 1282 | * @param string[] $hooks |
||
| 1283 | * @param string $path |
||
| 1284 | * @param bool $post |
||
| 1285 | * @return bool |
||
| 1286 | */ |
||
| 1287 | private function runHooks($hooks, $path, $post = false) { |
||
| 1288 | if (empty($hooks)) { |
||
| 1289 | return true; |
||
| 1290 | } |
||
| 1291 | $relativePath = $path; |
||
| 1292 | $path = $this->getHookPath($path); |
||
| 1293 | $prefix = ($post) ? 'post_' : ''; |
||
| 1294 | $run = true; |
||
| 1295 | if ($this->shouldEmitHooks($relativePath)) { |
||
| 1296 | foreach ($hooks as $hook) { |
||
| 1297 | if ($hook != 'read') { |
||
| 1298 | \OC_Hook::emit( |
||
| 1299 | Filesystem::CLASSNAME, |
||
| 1300 | $prefix . $hook, |
||
| 1301 | [ |
||
| 1302 | Filesystem::signal_param_run => &$run, |
||
| 1303 | Filesystem::signal_param_path => $path |
||
| 1304 | ] |
||
| 1305 | ); |
||
| 1306 | } elseif (!$post) { |
||
| 1307 | \OC_Hook::emit( |
||
| 1308 | Filesystem::CLASSNAME, |
||
| 1309 | $prefix . $hook, |
||
| 1310 | [ |
||
| 1311 | Filesystem::signal_param_path => $path |
||
| 1312 | ] |
||
| 1313 | ); |
||
| 1314 | } |
||
| 1315 | } |
||
| 1316 | } |
||
| 1317 | return $run; |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * check if a file or folder has been updated since $time |
||
| 1322 | * |
||
| 1323 | * @param string $path |
||
| 1324 | * @param int $time |
||
| 1325 | * @return bool |
||
| 1326 | */ |
||
| 1327 | public function hasUpdated($path, $time) { |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @param string $ownerId |
||
| 1333 | * @return IUser |
||
| 1334 | */ |
||
| 1335 | private function getUserObjectForOwner($ownerId) { |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Get file info from cache |
||
| 1346 | * |
||
| 1347 | * If the file is not in cached it will be scanned |
||
| 1348 | * If the file has changed on storage the cache will be updated |
||
| 1349 | * |
||
| 1350 | * @param \OC\Files\Storage\Storage $storage |
||
| 1351 | * @param string $internalPath |
||
| 1352 | * @param string $relativePath |
||
| 1353 | * @return array|bool |
||
| 1354 | */ |
||
| 1355 | private function getCacheEntry($storage, $internalPath, $relativePath) { |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * get the filesystem info |
||
| 1388 | * |
||
| 1389 | * @param string $path |
||
| 1390 | * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
||
| 1391 | * 'ext' to add only ext storage mount point sizes. Defaults to true. |
||
| 1392 | * defaults to true |
||
| 1393 | * @return \OC\Files\FileInfo|false False if file does not exist |
||
| 1394 | */ |
||
| 1395 | public function getFileInfo($path, $includeMountPoints = true) { |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * get the content of a directory |
||
| 1451 | * |
||
| 1452 | * @param string $directory path under datadirectory |
||
| 1453 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 1454 | * @return FileInfo[] |
||
| 1455 | */ |
||
| 1456 | public function getDirectoryContent($directory, $mimetype_filter = '') { |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * change file metadata |
||
| 1586 | * |
||
| 1587 | * @param string $path |
||
| 1588 | * @param array|\OCP\Files\FileInfo $data |
||
| 1589 | * @return int |
||
| 1590 | * |
||
| 1591 | * returns the fileid of the updated file |
||
| 1592 | */ |
||
| 1593 | public function putFileInfo($path, $data) { |
||
| 1617 | |||
| 1618 | /** |
||
| 1619 | * search for files with the name matching $query |
||
| 1620 | * |
||
| 1621 | * @param string $query |
||
| 1622 | * @return FileInfo[] |
||
| 1623 | */ |
||
| 1624 | public function search($query) { |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * search for files with the name matching $query |
||
| 1630 | * |
||
| 1631 | * @param string $query |
||
| 1632 | * @return FileInfo[] |
||
| 1633 | */ |
||
| 1634 | public function searchRaw($query) { |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * search for files by mimetype |
||
| 1640 | * |
||
| 1641 | * @param string $mimetype |
||
| 1642 | * @return FileInfo[] |
||
| 1643 | */ |
||
| 1644 | public function searchByMime($mimetype) { |
||
| 1647 | |||
| 1648 | /** |
||
| 1649 | * search for files by tag |
||
| 1650 | * |
||
| 1651 | * @param string|int $tag name or tag id |
||
| 1652 | * @param string $userId owner of the tags |
||
| 1653 | * @return FileInfo[] |
||
| 1654 | */ |
||
| 1655 | public function searchByTag($tag, $userId) { |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * @param string $method cache method |
||
| 1661 | * @param array $args |
||
| 1662 | * @return FileInfo[] |
||
| 1663 | */ |
||
| 1664 | private function searchCommon($method, $args) { |
||
| 1708 | |||
| 1709 | /** |
||
| 1710 | * Get the owner for a file or folder |
||
| 1711 | * |
||
| 1712 | * @param string $path |
||
| 1713 | * @return string the user id of the owner |
||
| 1714 | * @throws NotFoundException |
||
| 1715 | */ |
||
| 1716 | public function getOwner($path) { |
||
| 1723 | |||
| 1724 | /** |
||
| 1725 | * get the ETag for a file or folder |
||
| 1726 | * |
||
| 1727 | * @param string $path |
||
| 1728 | * @return string |
||
| 1729 | */ |
||
| 1730 | public function getETag($path) { |
||
| 1742 | |||
| 1743 | /** |
||
| 1744 | * Get the path of a file by id, relative to the view |
||
| 1745 | * |
||
| 1746 | * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
||
| 1747 | * |
||
| 1748 | * @param int $id |
||
| 1749 | * @param bool $includeShares whether to recurse into shared mounts |
||
| 1750 | * @throws NotFoundException |
||
| 1751 | * @return string |
||
| 1752 | */ |
||
| 1753 | public function getPath($id, $includeShares = true) { |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * @param string $path |
||
| 1786 | * @throws InvalidPathException |
||
| 1787 | */ |
||
| 1788 | private function assertPathLength($path) { |
||
| 1797 | |||
| 1798 | /** |
||
| 1799 | * check if it is allowed to move a mount point to a given target. |
||
| 1800 | * It is not allowed to move a mount point into a different mount point or |
||
| 1801 | * into an already shared folder |
||
| 1802 | * |
||
| 1803 | * @param MoveableMount $mount1 moveable mount |
||
| 1804 | * @param string $target absolute target path |
||
| 1805 | * @return boolean |
||
| 1806 | */ |
||
| 1807 | private function canMove(MoveableMount $mount1, $target) { |
||
| 1819 | |||
| 1820 | /** |
||
| 1821 | * Get a fileinfo object for files that are ignored in the cache (part files) |
||
| 1822 | * |
||
| 1823 | * @param string $path |
||
| 1824 | * @return \OCP\Files\FileInfo |
||
| 1825 | */ |
||
| 1826 | private function getPartFileInfo($path) { |
||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * @param string $path |
||
| 1852 | * @param string $fileName |
||
| 1853 | * @throws InvalidPathException |
||
| 1854 | */ |
||
| 1855 | public function verifyPath($path, $fileName) { |
||
| 1899 | |||
| 1900 | /** |
||
| 1901 | * get all parent folders of $path |
||
| 1902 | * |
||
| 1903 | * @param string $path |
||
| 1904 | * @return string[] |
||
| 1905 | */ |
||
| 1906 | private function getParents($path) { |
||
| 1926 | |||
| 1927 | /** |
||
| 1928 | * Returns the mount point for which to lock |
||
| 1929 | * |
||
| 1930 | * @param string $absolutePath absolute path |
||
| 1931 | * @param bool $useParentMount true to return parent mount instead of whatever |
||
| 1932 | * is mounted directly on the given path, false otherwise |
||
| 1933 | * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
||
| 1934 | */ |
||
| 1935 | private function getMountForLock($absolutePath, $useParentMount = false) { |
||
| 1953 | |||
| 1954 | /** |
||
| 1955 | * Lock the given path |
||
| 1956 | * |
||
| 1957 | * @param string $path the path of the file to lock, relative to the view |
||
| 1958 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1959 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1960 | * |
||
| 1961 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1962 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 1963 | */ |
||
| 1964 | View Code Duplication | private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1993 | |||
| 1994 | /** |
||
| 1995 | * Change the lock type |
||
| 1996 | * |
||
| 1997 | * @param string $path the path of the file to lock, relative to the view |
||
| 1998 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1999 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2000 | * |
||
| 2001 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2002 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 2003 | */ |
||
| 2004 | View Code Duplication | public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 2034 | |||
| 2035 | /** |
||
| 2036 | * Unlock the given path |
||
| 2037 | * |
||
| 2038 | * @param string $path the path of the file to unlock, relative to the view |
||
| 2039 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2040 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2041 | * |
||
| 2042 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2043 | */ |
||
| 2044 | private function unlockPath($path, $type, $lockMountPoint = false) { |
||
| 2065 | |||
| 2066 | /** |
||
| 2067 | * Lock a path and all its parents up to the root of the view |
||
| 2068 | * |
||
| 2069 | * @param string $path the path of the file to lock relative to the view |
||
| 2070 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2071 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2072 | * |
||
| 2073 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2074 | */ |
||
| 2075 | View Code Duplication | public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2091 | |||
| 2092 | /** |
||
| 2093 | * Unlock a path and all its parents up to the root of the view |
||
| 2094 | * |
||
| 2095 | * @param string $path the path of the file to lock relative to the view |
||
| 2096 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2097 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2098 | * |
||
| 2099 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2100 | */ |
||
| 2101 | View Code Duplication | public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2117 | |||
| 2118 | /** |
||
| 2119 | * Only lock files in data/user/files/ |
||
| 2120 | * |
||
| 2121 | * @param string $path Absolute path to the file/folder we try to (un)lock |
||
| 2122 | * @return bool |
||
| 2123 | */ |
||
| 2124 | protected function shouldLockFile($path) { |
||
| 2135 | |||
| 2136 | /** |
||
| 2137 | * Shortens the given absolute path to be relative to |
||
| 2138 | * "$user/files". |
||
| 2139 | * |
||
| 2140 | * @param string $absolutePath absolute path which is under "files" |
||
| 2141 | * |
||
| 2142 | * @return string path relative to "files" with trimmed slashes or null |
||
| 2143 | * if the path was NOT relative to files |
||
| 2144 | * |
||
| 2145 | * @throws \InvalidArgumentException if the given path was not under "files" |
||
| 2146 | * @since 8.1.0 |
||
| 2147 | */ |
||
| 2148 | public function getPathRelativeToFiles($absolutePath) { |
||
| 2160 | |||
| 2161 | /** |
||
| 2162 | * @param string $filename |
||
| 2163 | * @return array |
||
| 2164 | * @throws \OC\User\NoUserException |
||
| 2165 | * @throws NotFoundException |
||
| 2166 | */ |
||
| 2167 | public function getUidAndFilename($filename) { |
||
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Creates parent non-existing folders |
||
| 2187 | * |
||
| 2188 | * @param string $filePath |
||
| 2189 | * @return bool |
||
| 2190 | */ |
||
| 2191 | private function createParentDirectories($filePath) { |
||
| 2202 | } |
||
| 2203 |
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: