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 | private static $ignorePartFile = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $root |
||
| 112 | * @throws \Exception If $root contains an invalid path |
||
| 113 | */ |
||
| 114 | public function __construct($root = '') { |
||
| 115 | if ($root === null) { |
||
| 116 | throw new \InvalidArgumentException('Root can\'t be null'); |
||
| 117 | } |
||
| 118 | if (!Filesystem::isValidPath($root)) { |
||
| 119 | throw new \Exception(); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->fakeRoot = $root; |
||
| 123 | $this->lockingProvider = \OC::$server->getLockingProvider(); |
||
| 124 | $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
||
| 125 | $this->userManager = \OC::$server->getUserManager(); |
||
| 126 | $this->logger = \OC::$server->getLogger(); |
||
| 127 | $this->eventDispatcher = \OC::$server->getEventDispatcher(); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getAbsolutePath($path = '/') { |
||
| 131 | if ($path === null) { |
||
| 132 | return null; |
||
| 133 | } |
||
| 134 | $this->assertPathLength($path); |
||
| 135 | if ($path === '') { |
||
| 136 | $path = '/'; |
||
| 137 | } |
||
| 138 | if ($path[0] !== '/') { |
||
| 139 | $path = '/' . $path; |
||
| 140 | } |
||
| 141 | return $this->fakeRoot . $path; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * change the root to a fake root |
||
| 146 | * |
||
| 147 | * @param string $fakeRoot |
||
| 148 | * @return boolean|null |
||
| 149 | */ |
||
| 150 | public function chroot($fakeRoot) { |
||
| 151 | if (!$fakeRoot == '') { |
||
| 152 | if ($fakeRoot[0] !== '/') { |
||
| 153 | $fakeRoot = '/' . $fakeRoot; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | $this->fakeRoot = $fakeRoot; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * get the fake root |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getRoot() { |
||
| 165 | return $this->fakeRoot; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * get path relative to the root of the view |
||
| 170 | * |
||
| 171 | * @param string $path |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getRelativePath($path) { |
||
| 175 | $this->assertPathLength($path); |
||
| 176 | if ($this->fakeRoot == '') { |
||
| 177 | return $path; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (\rtrim($path, '/') === \rtrim($this->fakeRoot, '/')) { |
||
| 181 | return '/'; |
||
| 182 | } |
||
| 183 | |||
| 184 | // missing slashes can cause wrong matches! |
||
| 185 | $root = \rtrim($this->fakeRoot, '/') . '/'; |
||
| 186 | |||
| 187 | if (\strpos($path, $root) !== 0) { |
||
| 188 | return null; |
||
| 189 | } else { |
||
| 190 | $path = \substr($path, \strlen($this->fakeRoot)); |
||
| 191 | if (\strlen($path) === 0) { |
||
| 192 | return '/'; |
||
| 193 | } else { |
||
| 194 | return $path; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * get the mountpoint of the storage object for a path |
||
| 201 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 202 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 203 | * and does not take the chroot into account ) |
||
| 204 | * |
||
| 205 | * @param string $path |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getMountPoint($path) { |
||
| 209 | return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * get the mountpoint of the storage object for a path |
||
| 214 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 215 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 216 | * and does not take the chroot into account ) |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * @return \OCP\Files\Mount\IMountPoint |
||
| 220 | */ |
||
| 221 | public function getMount($path) { |
||
| 222 | return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * resolve a path to a storage and internal path |
||
| 227 | * |
||
| 228 | * @param string $path |
||
| 229 | * @return array an array consisting of the storage and the internal path |
||
| 230 | */ |
||
| 231 | public function resolvePath($path) { |
||
| 232 | $a = $this->getAbsolutePath($path); |
||
| 233 | $p = Filesystem::normalizePath($a); |
||
| 234 | return Filesystem::resolvePath($p); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * return the path to a local version of the file |
||
| 239 | * we need this because we can't know if a file is stored local or not from |
||
| 240 | * outside the filestorage and for some purposes a local file is needed |
||
| 241 | * |
||
| 242 | * @param string $path |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function getLocalFile($path) { |
|
| 246 | $parent = \substr($path, 0, \strrpos($path, '/')); |
||
| 247 | $path = $this->getAbsolutePath($path); |
||
| 248 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
| 249 | if (Filesystem::isValidPath($parent) and $storage) { |
||
| 250 | return $storage->getLocalFile($internalPath); |
||
| 251 | } else { |
||
| 252 | return null; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $path |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | View Code Duplication | public function getLocalFolder($path) { |
|
| 261 | $parent = \substr($path, 0, \strrpos($path, '/')); |
||
| 262 | $path = $this->getAbsolutePath($path); |
||
| 263 | list($storage, $internalPath) = Filesystem::resolvePath($path); |
||
| 264 | if (Filesystem::isValidPath($parent) and $storage) { |
||
| 265 | return $storage->getLocalFolder($internalPath); |
||
| 266 | } else { |
||
| 267 | return null; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * the following functions operate with arguments and return values identical |
||
| 273 | * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
||
| 274 | * for \OC\Files\Storage\Storage via basicOperation(). |
||
| 275 | */ |
||
| 276 | public function mkdir($path) { |
||
| 277 | return $this->emittingCall(function () use (&$path) { |
||
| 278 | $result = $this->basicOperation('mkdir', $path, ['create', 'write']); |
||
| 279 | return $result; |
||
| 280 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'create'); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * remove mount point |
||
| 285 | * |
||
| 286 | * @param \OC\Files\Mount\MoveableMount $mount |
||
| 287 | * @param string $path relative to data/ |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | protected function removeMount($mount, $path) { |
||
| 291 | if ($mount instanceof MoveableMount) { |
||
| 292 | // cut of /user/files to get the relative path to data/user/files |
||
| 293 | $pathParts = \explode('/', $path, 4); |
||
| 294 | $relPath = '/' . $pathParts[3]; |
||
| 295 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 296 | \OC_Hook::emit( |
||
| 297 | Filesystem::CLASSNAME, "umount", |
||
| 298 | [Filesystem::signal_param_path => $relPath] |
||
| 299 | ); |
||
| 300 | $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
||
| 301 | $result = $mount->removeMount(); |
||
| 302 | $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 303 | if ($result) { |
||
| 304 | \OC_Hook::emit( |
||
| 305 | Filesystem::CLASSNAME, "post_umount", |
||
| 306 | [Filesystem::signal_param_path => $relPath] |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
||
| 310 | return $result; |
||
| 311 | } else { |
||
| 312 | // do not allow deleting the storage's root / the mount point |
||
| 313 | // because for some storages it might delete the whole contents |
||
| 314 | // but isn't supposed to work that way |
||
| 315 | return false; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | public function disableCacheUpdate() { |
||
| 320 | $this->updaterEnabled = false; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function enableCacheUpdate() { |
||
| 324 | $this->updaterEnabled = true; |
||
| 325 | } |
||
| 326 | |||
| 327 | protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
||
| 328 | if ($this->updaterEnabled) { |
||
| 329 | if ($time === null) { |
||
| 330 | $time = \time(); |
||
| 331 | } |
||
| 332 | $storage->getUpdater()->update($internalPath, $time); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | protected function removeUpdate(Storage $storage, $internalPath) { |
||
| 337 | if ($this->updaterEnabled) { |
||
| 338 | $storage->getUpdater()->remove($internalPath); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 343 | if ($this->updaterEnabled) { |
||
| 344 | $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $path |
||
| 350 | * @return bool|mixed |
||
| 351 | */ |
||
| 352 | public function rmdir($path) { |
||
| 353 | return $this->emittingCall(function () use (&$path) { |
||
| 354 | $absolutePath = $this->getAbsolutePath($path); |
||
| 355 | $mount = Filesystem::getMountManager()->find($absolutePath); |
||
| 356 | if ($mount->getInternalPath($absolutePath) === '') { |
||
| 357 | return $this->removeMount($mount, $absolutePath); |
||
|
|
|||
| 358 | } |
||
| 359 | if ($this->is_dir($path)) { |
||
| 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) { |
||
| 388 | if ($path == '/') { |
||
| 389 | return true; |
||
| 390 | } |
||
| 391 | return $this->basicOperation('is_dir', $path); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $path |
||
| 396 | * @return bool|mixed |
||
| 397 | */ |
||
| 398 | public function is_file($path) { |
||
| 399 | if ($path == '/') { |
||
| 400 | return false; |
||
| 401 | } |
||
| 402 | return $this->basicOperation('is_file', $path); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $path |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | public function stat($path) { |
||
| 410 | return $this->basicOperation('stat', $path); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $path |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | public function filetype($path) { |
||
| 418 | return $this->basicOperation('filetype', $path); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $path |
||
| 423 | * @return mixed |
||
| 424 | */ |
||
| 425 | public function filesize($path) { |
||
| 426 | return $this->basicOperation('filesize', $path); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $path |
||
| 431 | * @return bool|mixed |
||
| 432 | * @throws \OCP\Files\InvalidPathException |
||
| 433 | */ |
||
| 434 | public function readfile($path) { |
||
| 435 | $this->assertPathLength($path); |
||
| 436 | @\ob_end_clean(); |
||
| 437 | $handle = $this->fopen($path, 'rb'); |
||
| 438 | if ($handle) { |
||
| 439 | $chunkSize = 8192; // 8 kB chunks |
||
| 440 | while (!\feof($handle)) { |
||
| 441 | echo \fread($handle, $chunkSize); |
||
| 442 | \flush(); |
||
| 443 | } |
||
| 444 | $size = $this->filesize($path); |
||
| 445 | return $size; |
||
| 446 | } |
||
| 447 | return false; |
||
| 448 | } |
||
| 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) { |
||
| 459 | $this->assertPathLength($path); |
||
| 460 | @\ob_end_clean(); |
||
| 461 | $handle = $this->fopen($path, 'rb'); |
||
| 462 | if ($handle) { |
||
| 463 | if (\fseek($handle, $from) === 0) { |
||
| 464 | $chunkSize = 8192; // 8 kB chunks |
||
| 465 | $end = $to + 1; |
||
| 466 | while (!\feof($handle) && \ftell($handle) < $end) { |
||
| 467 | $len = $end - \ftell($handle); |
||
| 468 | if ($len > $chunkSize) { |
||
| 469 | $len = $chunkSize; |
||
| 470 | } |
||
| 471 | echo \fread($handle, $len); |
||
| 472 | \flush(); |
||
| 473 | } |
||
| 474 | $size = \ftell($handle) - $from; |
||
| 475 | return $size; |
||
| 476 | } |
||
| 477 | |||
| 478 | throw new \OCP\Files\UnseekableException('fseek error'); |
||
| 479 | } |
||
| 480 | return false; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $path |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | public function isCreatable($path) { |
||
| 488 | return $this->basicOperation('isCreatable', $path); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $path |
||
| 493 | * @return mixed |
||
| 494 | */ |
||
| 495 | public function isReadable($path) { |
||
| 496 | return $this->basicOperation('isReadable', $path); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $path |
||
| 501 | * @return mixed |
||
| 502 | */ |
||
| 503 | public function isUpdatable($path) { |
||
| 504 | return $this->basicOperation('isUpdatable', $path); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $path |
||
| 509 | * @return bool|mixed |
||
| 510 | */ |
||
| 511 | public function isDeletable($path) { |
||
| 512 | $absolutePath = $this->getAbsolutePath($path); |
||
| 513 | $mount = Filesystem::getMountManager()->find($absolutePath); |
||
| 514 | if ($mount->getInternalPath($absolutePath) === '') { |
||
| 515 | return $mount instanceof MoveableMount; |
||
| 516 | } |
||
| 517 | return $this->basicOperation('isDeletable', $path); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $path |
||
| 522 | * @return mixed |
||
| 523 | */ |
||
| 524 | public function isSharable($path) { |
||
| 525 | return $this->basicOperation('isSharable', $path); |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string $path |
||
| 530 | * @return bool|mixed |
||
| 531 | */ |
||
| 532 | public function file_exists($path) { |
||
| 533 | if ($path == '/') { |
||
| 534 | return true; |
||
| 535 | } |
||
| 536 | return $this->basicOperation('file_exists', $path); |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param string $path |
||
| 541 | * @return mixed |
||
| 542 | */ |
||
| 543 | public function filemtime($path) { |
||
| 544 | return $this->basicOperation('filemtime', $path); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $path |
||
| 549 | * @param int|string $mtime |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public function touch($path, $mtime = null) { |
||
| 553 | if ($mtime !== null and !\is_numeric($mtime)) { |
||
| 554 | $mtime = \strtotime($mtime); |
||
| 555 | } |
||
| 556 | |||
| 557 | $hooks = ['touch']; |
||
| 558 | |||
| 559 | if (!$this->file_exists($path)) { |
||
| 560 | $hooks[] = 'create'; |
||
| 561 | $hooks[] = 'write'; |
||
| 562 | } |
||
| 563 | $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
||
| 564 | if (!$result) { |
||
| 565 | // If create file fails because of permissions on external storage like SMB folders, |
||
| 566 | // check file exists and return false if not. |
||
| 567 | if (!$this->file_exists($path)) { |
||
| 568 | return false; |
||
| 569 | } |
||
| 570 | if ($mtime === null) { |
||
| 571 | $mtime = \time(); |
||
| 572 | } |
||
| 573 | //if native touch fails, we emulate it by changing the mtime in the cache |
||
| 574 | $this->putFileInfo($path, ['mtime' => $mtime]); |
||
| 575 | } |
||
| 576 | return true; |
||
| 577 | } |
||
| 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) { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @param string $path |
||
| 659 | * @param mixed $data |
||
| 660 | * @return bool|mixed |
||
| 661 | * @throws \Exception |
||
| 662 | */ |
||
| 663 | public function file_put_contents($path, $data) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @param string $path |
||
| 719 | * @return bool|mixed |
||
| 720 | */ |
||
| 721 | public function unlink($path) { |
||
| 722 | return $this->emittingCall(function () use (&$path) { |
||
| 723 | if ($path === '' || $path === '/') { |
||
| 724 | // do not allow deleting the root |
||
| 725 | return false; |
||
| 726 | } |
||
| 727 | $postFix = (\substr($path, -1, 1) === '/') ? '/' : ''; |
||
| 728 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
||
| 729 | $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
||
| 730 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
||
| 731 | return $this->removeMount($mount, $absolutePath); |
||
| 732 | } |
||
| 733 | if ($this->is_dir($path)) { |
||
| 734 | $result = $this->basicOperation('rmdir', $path, ['delete']); |
||
| 735 | } else { |
||
| 736 | $result = $this->basicOperation('unlink', $path, ['delete']); |
||
| 737 | } |
||
| 738 | |||
| 739 | View Code Duplication | if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 740 | $storage = $mount->getStorage(); |
||
| 741 | $internalPath = $mount->getInternalPath($absolutePath); |
||
| 742 | $storage->getUpdater()->remove($internalPath); |
||
| 743 | return true; |
||
| 744 | } else { |
||
| 745 | return $result; |
||
| 746 | } |
||
| 747 | }, ['before' => ['path' => $this->getAbsolutePath($path)], 'after' => ['path' => $this->getAbsolutePath($path)]], 'file', 'delete'); |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * @param string $directory |
||
| 752 | * @return bool|mixed |
||
| 753 | */ |
||
| 754 | public function deleteAll($directory) { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Rename/move a file or folder from the source path to target path. |
||
| 760 | * |
||
| 761 | * @param string $path1 source path |
||
| 762 | * @param string $path2 target path |
||
| 763 | * |
||
| 764 | * @return bool|mixed |
||
| 765 | */ |
||
| 766 | public function rename($path1, $path2) { |
||
| 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 | |||
| 897 | public function copy($path1, $path2, $preserveMtime = false) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * @param string $path |
||
| 996 | * @param string $mode |
||
| 997 | * @return resource |
||
| 998 | */ |
||
| 999 | public function fopen($path, $mode) { |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @param string $path |
||
| 1034 | * @return bool|string |
||
| 1035 | * @throws \OCP\Files\InvalidPathException |
||
| 1036 | */ |
||
| 1037 | public function toTmpFile($path) { |
||
| 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) { |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @param string $path |
||
| 1096 | * @return mixed |
||
| 1097 | * @throws \OCP\Files\InvalidPathException |
||
| 1098 | */ |
||
| 1099 | public function getMimeType($path) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @param string $type |
||
| 1106 | * @param string $path |
||
| 1107 | * @param bool $raw |
||
| 1108 | * @return bool|null|string |
||
| 1109 | */ |
||
| 1110 | public function hash($type, $path, $raw = false) { |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * @param string $path |
||
| 1137 | * @return mixed |
||
| 1138 | * @throws \OCP\Files\InvalidPathException |
||
| 1139 | */ |
||
| 1140 | public function free_space($path = '/') { |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
||
| 1147 | * |
||
| 1148 | * @param string $operation |
||
| 1149 | * @param string $path |
||
| 1150 | * @param array $hooks (optional) |
||
| 1151 | * @param mixed $extraParam (optional) |
||
| 1152 | * @return mixed |
||
| 1153 | * @throws \Exception |
||
| 1154 | * |
||
| 1155 | * This method takes requests for basic filesystem functions (e.g. reading & writing |
||
| 1156 | * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
||
| 1157 | * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
||
| 1158 | */ |
||
| 1159 | private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * get the path relative to the default root for hook usage |
||
| 1246 | * |
||
| 1247 | * @param string $path |
||
| 1248 | * @return string |
||
| 1249 | */ |
||
| 1250 | private function getHookPath($path) { |
||
| 1256 | |||
| 1257 | private function shouldEmitHooks($path = '') { |
||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * @param string[] $hooks |
||
| 1282 | * @param string $path |
||
| 1283 | * @param bool $post |
||
| 1284 | * @return bool |
||
| 1285 | */ |
||
| 1286 | private function runHooks($hooks, $path, $post = false) { |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * check if a file or folder has been updated since $time |
||
| 1321 | * |
||
| 1322 | * @param string $path |
||
| 1323 | * @param int $time |
||
| 1324 | * @return bool |
||
| 1325 | */ |
||
| 1326 | public function hasUpdated($path, $time) { |
||
| 1329 | |||
| 1330 | /** |
||
| 1331 | * @param string $ownerId |
||
| 1332 | * @return IUser |
||
| 1333 | */ |
||
| 1334 | private function getUserObjectForOwner($ownerId) { |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Get file info from cache |
||
| 1345 | * |
||
| 1346 | * If the file is not in cached it will be scanned |
||
| 1347 | * If the file has changed on storage the cache will be updated |
||
| 1348 | * |
||
| 1349 | * @param \OC\Files\Storage\Storage $storage |
||
| 1350 | * @param string $internalPath |
||
| 1351 | * @param string $relativePath |
||
| 1352 | * @return array|bool |
||
| 1353 | */ |
||
| 1354 | private function getCacheEntry($storage, $internalPath, $relativePath) { |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * get the filesystem info |
||
| 1387 | * |
||
| 1388 | * @param string $path |
||
| 1389 | * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
||
| 1390 | * 'ext' to add only ext storage mount point sizes. Defaults to true. |
||
| 1391 | * defaults to true |
||
| 1392 | * @return \OC\Files\FileInfo|false False if file does not exist |
||
| 1393 | */ |
||
| 1394 | public function getFileInfo($path, $includeMountPoints = true) { |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * get the content of a directory |
||
| 1450 | * |
||
| 1451 | * @param string $directory path under datadirectory |
||
| 1452 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 1453 | * @return FileInfo[] |
||
| 1454 | */ |
||
| 1455 | public function getDirectoryContent($directory, $mimetype_filter = '') { |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * change file metadata |
||
| 1585 | * |
||
| 1586 | * @param string $path |
||
| 1587 | * @param array|\OCP\Files\FileInfo $data |
||
| 1588 | * @return int |
||
| 1589 | * |
||
| 1590 | * returns the fileid of the updated file |
||
| 1591 | */ |
||
| 1592 | public function putFileInfo($path, $data) { |
||
| 1616 | |||
| 1617 | /** |
||
| 1618 | * search for files with the name matching $query |
||
| 1619 | * |
||
| 1620 | * @param string $query |
||
| 1621 | * @return FileInfo[] |
||
| 1622 | */ |
||
| 1623 | public function search($query) { |
||
| 1626 | |||
| 1627 | /** |
||
| 1628 | * search for files with the name matching $query |
||
| 1629 | * |
||
| 1630 | * @param string $query |
||
| 1631 | * @return FileInfo[] |
||
| 1632 | */ |
||
| 1633 | public function searchRaw($query) { |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * search for files by mimetype |
||
| 1639 | * |
||
| 1640 | * @param string $mimetype |
||
| 1641 | * @return FileInfo[] |
||
| 1642 | */ |
||
| 1643 | public function searchByMime($mimetype) { |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * search for files by tag |
||
| 1649 | * |
||
| 1650 | * @param string|int $tag name or tag id |
||
| 1651 | * @param string $userId owner of the tags |
||
| 1652 | * @return FileInfo[] |
||
| 1653 | */ |
||
| 1654 | public function searchByTag($tag, $userId) { |
||
| 1657 | |||
| 1658 | /** |
||
| 1659 | * @param string $method cache method |
||
| 1660 | * @param array $args |
||
| 1661 | * @return FileInfo[] |
||
| 1662 | */ |
||
| 1663 | private function searchCommon($method, $args) { |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Get the owner for a file or folder |
||
| 1710 | * |
||
| 1711 | * @param string $path |
||
| 1712 | * @return string the user id of the owner |
||
| 1713 | * @throws NotFoundException |
||
| 1714 | */ |
||
| 1715 | public function getOwner($path) { |
||
| 1722 | |||
| 1723 | /** |
||
| 1724 | * get the ETag for a file or folder |
||
| 1725 | * |
||
| 1726 | * @param string $path |
||
| 1727 | * @return string |
||
| 1728 | */ |
||
| 1729 | public function getETag($path) { |
||
| 1741 | |||
| 1742 | /** |
||
| 1743 | * Get the path of a file by id, relative to the view |
||
| 1744 | * |
||
| 1745 | * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
||
| 1746 | * |
||
| 1747 | * @param int $id |
||
| 1748 | * @param bool $includeShares whether to recurse into shared mounts |
||
| 1749 | * @throws NotFoundException |
||
| 1750 | * @return string |
||
| 1751 | */ |
||
| 1752 | public function getPath($id, $includeShares = true) { |
||
| 1782 | |||
| 1783 | /** |
||
| 1784 | * @param string $path |
||
| 1785 | * @throws InvalidPathException |
||
| 1786 | */ |
||
| 1787 | private function assertPathLength($path) { |
||
| 1796 | |||
| 1797 | /** |
||
| 1798 | * check if it is allowed to move a mount point to a given target. |
||
| 1799 | * It is not allowed to move a mount point into a different mount point or |
||
| 1800 | * into an already shared folder |
||
| 1801 | * |
||
| 1802 | * @param MoveableMount $mount1 moveable mount |
||
| 1803 | * @param string $target absolute target path |
||
| 1804 | * @return boolean |
||
| 1805 | */ |
||
| 1806 | private function canMove(MoveableMount $mount1, $target) { |
||
| 1817 | |||
| 1818 | /** |
||
| 1819 | * Get a fileinfo object for files that are ignored in the cache (part files) |
||
| 1820 | * |
||
| 1821 | * @param string $path |
||
| 1822 | * @return \OCP\Files\FileInfo |
||
| 1823 | */ |
||
| 1824 | private function getPartFileInfo($path) { |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * @param string $path |
||
| 1850 | * @param string $fileName |
||
| 1851 | * @throws InvalidPathException |
||
| 1852 | */ |
||
| 1853 | public function verifyPath($path, $fileName) { |
||
| 1896 | |||
| 1897 | /** |
||
| 1898 | * get all parent folders of $path |
||
| 1899 | * |
||
| 1900 | * @param string $path |
||
| 1901 | * @return string[] |
||
| 1902 | */ |
||
| 1903 | private function getParents($path) { |
||
| 1923 | |||
| 1924 | /** |
||
| 1925 | * Returns the mount point for which to lock |
||
| 1926 | * |
||
| 1927 | * @param string $absolutePath absolute path |
||
| 1928 | * @param bool $useParentMount true to return parent mount instead of whatever |
||
| 1929 | * is mounted directly on the given path, false otherwise |
||
| 1930 | * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
||
| 1931 | */ |
||
| 1932 | private function getMountForLock($absolutePath, $useParentMount = false) { |
||
| 1950 | |||
| 1951 | /** |
||
| 1952 | * Lock the given path |
||
| 1953 | * |
||
| 1954 | * @param string $path the path of the file to lock, relative to the view |
||
| 1955 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1956 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1957 | * |
||
| 1958 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1959 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 1960 | */ |
||
| 1961 | View Code Duplication | private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Change the lock type |
||
| 1993 | * |
||
| 1994 | * @param string $path the path of the file to lock, relative to the view |
||
| 1995 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 1996 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 1997 | * |
||
| 1998 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 1999 | * @throws \OCP\Lock\LockedException if the path is already locked |
||
| 2000 | */ |
||
| 2001 | View Code Duplication | public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 2031 | |||
| 2032 | /** |
||
| 2033 | * Unlock the given path |
||
| 2034 | * |
||
| 2035 | * @param string $path the path of the file to unlock, relative to the view |
||
| 2036 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2037 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2038 | * |
||
| 2039 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2040 | */ |
||
| 2041 | private function unlockPath($path, $type, $lockMountPoint = false) { |
||
| 2062 | |||
| 2063 | /** |
||
| 2064 | * Lock a path and all its parents up to the root of the view |
||
| 2065 | * |
||
| 2066 | * @param string $path the path of the file to lock relative to the view |
||
| 2067 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2068 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2069 | * |
||
| 2070 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2071 | */ |
||
| 2072 | View Code Duplication | public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2088 | |||
| 2089 | /** |
||
| 2090 | * Unlock a path and all its parents up to the root of the view |
||
| 2091 | * |
||
| 2092 | * @param string $path the path of the file to lock relative to the view |
||
| 2093 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 2094 | * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
||
| 2095 | * |
||
| 2096 | * @return bool False if the path is excluded from locking, true otherwise |
||
| 2097 | */ |
||
| 2098 | View Code Duplication | public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2114 | |||
| 2115 | /** |
||
| 2116 | * Only lock files in data/user/files/ |
||
| 2117 | * |
||
| 2118 | * @param string $path Absolute path to the file/folder we try to (un)lock |
||
| 2119 | * @return bool |
||
| 2120 | */ |
||
| 2121 | protected function shouldLockFile($path) { |
||
| 2132 | |||
| 2133 | /** |
||
| 2134 | * Shortens the given absolute path to be relative to |
||
| 2135 | * "$user/files". |
||
| 2136 | * |
||
| 2137 | * @param string $absolutePath absolute path which is under "files" |
||
| 2138 | * |
||
| 2139 | * @return string path relative to "files" with trimmed slashes or null |
||
| 2140 | * if the path was NOT relative to files |
||
| 2141 | * |
||
| 2142 | * @throws \InvalidArgumentException if the given path was not under "files" |
||
| 2143 | * @since 8.1.0 |
||
| 2144 | */ |
||
| 2145 | public function getPathRelativeToFiles($absolutePath) { |
||
| 2157 | |||
| 2158 | /** |
||
| 2159 | * @param string $filename |
||
| 2160 | * @return array |
||
| 2161 | * @throws \OC\User\NoUserException |
||
| 2162 | * @throws NotFoundException |
||
| 2163 | */ |
||
| 2164 | public function getUidAndFilename($filename) { |
||
| 2181 | |||
| 2182 | /** |
||
| 2183 | * Creates parent non-existing folders |
||
| 2184 | * |
||
| 2185 | * @param string $filePath |
||
| 2186 | * @return bool |
||
| 2187 | */ |
||
| 2188 | private function createParentDirectories($filePath) { |
||
| 2199 | |||
| 2200 | /** |
||
| 2201 | * User can create part files example to a call for rename(), in effect |
||
| 2202 | * it might not be a part file. So for better control in such cases this |
||
| 2203 | * method would help to let the method in rename() to know if it is a |
||
| 2204 | * part file. |
||
| 2205 | * |
||
| 2206 | * @param bool $isIgnored |
||
| 2207 | */ |
||
| 2208 | public static function setIgnorePartFile($isIgnored) { |
||
| 2211 | } |
||
| 2212 |
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: