@@ -80,2090 +80,2090 @@ |
||
| 80 | 80 | * \OC\Files\Storage\Storage object |
| 81 | 81 | */ |
| 82 | 82 | class View { |
| 83 | - /** @var string */ |
|
| 84 | - private $fakeRoot = ''; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @var \OCP\Lock\ILockingProvider |
|
| 88 | - */ |
|
| 89 | - protected $lockingProvider; |
|
| 90 | - |
|
| 91 | - private $lockingEnabled; |
|
| 92 | - |
|
| 93 | - private $updaterEnabled = true; |
|
| 94 | - |
|
| 95 | - /** @var \OC\User\Manager */ |
|
| 96 | - private $userManager; |
|
| 97 | - |
|
| 98 | - /** @var \OCP\ILogger */ |
|
| 99 | - private $logger; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param string $root |
|
| 103 | - * @throws \Exception If $root contains an invalid path |
|
| 104 | - */ |
|
| 105 | - public function __construct($root = '') { |
|
| 106 | - if (is_null($root)) { |
|
| 107 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 108 | - } |
|
| 109 | - if (!Filesystem::isValidPath($root)) { |
|
| 110 | - throw new \Exception(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->fakeRoot = $root; |
|
| 114 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 115 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 116 | - $this->userManager = \OC::$server->getUserManager(); |
|
| 117 | - $this->logger = \OC::$server->getLogger(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function getAbsolutePath($path = '/') { |
|
| 121 | - if ($path === null) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - $this->assertPathLength($path); |
|
| 125 | - if ($path === '') { |
|
| 126 | - $path = '/'; |
|
| 127 | - } |
|
| 128 | - if ($path[0] !== '/') { |
|
| 129 | - $path = '/' . $path; |
|
| 130 | - } |
|
| 131 | - return $this->fakeRoot . $path; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * change the root to a fake root |
|
| 136 | - * |
|
| 137 | - * @param string $fakeRoot |
|
| 138 | - * @return boolean|null |
|
| 139 | - */ |
|
| 140 | - public function chroot($fakeRoot) { |
|
| 141 | - if (!$fakeRoot == '') { |
|
| 142 | - if ($fakeRoot[0] !== '/') { |
|
| 143 | - $fakeRoot = '/' . $fakeRoot; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - $this->fakeRoot = $fakeRoot; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * get the fake root |
|
| 151 | - * |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getRoot() { |
|
| 155 | - return $this->fakeRoot; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * get path relative to the root of the view |
|
| 160 | - * |
|
| 161 | - * @param string $path |
|
| 162 | - * @return string |
|
| 163 | - */ |
|
| 164 | - public function getRelativePath($path) { |
|
| 165 | - $this->assertPathLength($path); |
|
| 166 | - if ($this->fakeRoot == '') { |
|
| 167 | - return $path; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 171 | - return '/'; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // missing slashes can cause wrong matches! |
|
| 175 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 176 | - |
|
| 177 | - if (strpos($path, $root) !== 0) { |
|
| 178 | - return null; |
|
| 179 | - } else { |
|
| 180 | - $path = substr($path, strlen($this->fakeRoot)); |
|
| 181 | - if (strlen($path) === 0) { |
|
| 182 | - return '/'; |
|
| 183 | - } else { |
|
| 184 | - return $path; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * get the mountpoint of the storage object for a path |
|
| 191 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 192 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 193 | - * and does not take the chroot into account ) |
|
| 194 | - * |
|
| 195 | - * @param string $path |
|
| 196 | - * @return string |
|
| 197 | - */ |
|
| 198 | - public function getMountPoint($path) { |
|
| 199 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * get the mountpoint of the storage object for a path |
|
| 204 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 205 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 206 | - * and does not take the chroot into account ) |
|
| 207 | - * |
|
| 208 | - * @param string $path |
|
| 209 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 210 | - */ |
|
| 211 | - public function getMount($path) { |
|
| 212 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * resolve a path to a storage and internal path |
|
| 217 | - * |
|
| 218 | - * @param string $path |
|
| 219 | - * @return array an array consisting of the storage and the internal path |
|
| 220 | - */ |
|
| 221 | - public function resolvePath($path) { |
|
| 222 | - $a = $this->getAbsolutePath($path); |
|
| 223 | - $p = Filesystem::normalizePath($a); |
|
| 224 | - return Filesystem::resolvePath($p); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * return the path to a local version of the file |
|
| 229 | - * we need this because we can't know if a file is stored local or not from |
|
| 230 | - * outside the filestorage and for some purposes a local file is needed |
|
| 231 | - * |
|
| 232 | - * @param string $path |
|
| 233 | - * @return string |
|
| 234 | - */ |
|
| 235 | - public function getLocalFile($path) { |
|
| 236 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 237 | - $path = $this->getAbsolutePath($path); |
|
| 238 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 239 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 240 | - return $storage->getLocalFile($internalPath); |
|
| 241 | - } else { |
|
| 242 | - return null; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param string $path |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function getLocalFolder($path) { |
|
| 251 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 252 | - $path = $this->getAbsolutePath($path); |
|
| 253 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 254 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 255 | - return $storage->getLocalFolder($internalPath); |
|
| 256 | - } else { |
|
| 257 | - return null; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * the following functions operate with arguments and return values identical |
|
| 263 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 264 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 265 | - */ |
|
| 266 | - public function mkdir($path) { |
|
| 267 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * remove mount point |
|
| 272 | - * |
|
| 273 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
| 274 | - * @param string $path relative to data/ |
|
| 275 | - * @return boolean |
|
| 276 | - */ |
|
| 277 | - protected function removeMount($mount, $path) { |
|
| 278 | - if ($mount instanceof MoveableMount) { |
|
| 279 | - // cut of /user/files to get the relative path to data/user/files |
|
| 280 | - $pathParts = explode('/', $path, 4); |
|
| 281 | - $relPath = '/' . $pathParts[3]; |
|
| 282 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 283 | - \OC_Hook::emit( |
|
| 284 | - Filesystem::CLASSNAME, "umount", |
|
| 285 | - array(Filesystem::signal_param_path => $relPath) |
|
| 286 | - ); |
|
| 287 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 288 | - $result = $mount->removeMount(); |
|
| 289 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 290 | - if ($result) { |
|
| 291 | - \OC_Hook::emit( |
|
| 292 | - Filesystem::CLASSNAME, "post_umount", |
|
| 293 | - array(Filesystem::signal_param_path => $relPath) |
|
| 294 | - ); |
|
| 295 | - } |
|
| 296 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 297 | - return $result; |
|
| 298 | - } else { |
|
| 299 | - // do not allow deleting the storage's root / the mount point |
|
| 300 | - // because for some storages it might delete the whole contents |
|
| 301 | - // but isn't supposed to work that way |
|
| 302 | - return false; |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - public function disableCacheUpdate() { |
|
| 307 | - $this->updaterEnabled = false; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - public function enableCacheUpdate() { |
|
| 311 | - $this->updaterEnabled = true; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 315 | - if ($this->updaterEnabled) { |
|
| 316 | - if (is_null($time)) { |
|
| 317 | - $time = time(); |
|
| 318 | - } |
|
| 319 | - $storage->getUpdater()->update($internalPath, $time); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 324 | - if ($this->updaterEnabled) { |
|
| 325 | - $storage->getUpdater()->remove($internalPath); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 330 | - if ($this->updaterEnabled) { |
|
| 331 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @param string $path |
|
| 337 | - * @return bool|mixed |
|
| 338 | - */ |
|
| 339 | - public function rmdir($path) { |
|
| 340 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 341 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 342 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 343 | - return $this->removeMount($mount, $absolutePath); |
|
| 344 | - } |
|
| 345 | - if ($this->is_dir($path)) { |
|
| 346 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 347 | - } else { |
|
| 348 | - $result = false; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 352 | - $storage = $mount->getStorage(); |
|
| 353 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 354 | - $storage->getUpdater()->remove($internalPath); |
|
| 355 | - } |
|
| 356 | - return $result; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @param string $path |
|
| 361 | - * @return resource |
|
| 362 | - */ |
|
| 363 | - public function opendir($path) { |
|
| 364 | - return $this->basicOperation('opendir', $path, array('read')); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * @param string $path |
|
| 369 | - * @return bool|mixed |
|
| 370 | - */ |
|
| 371 | - public function is_dir($path) { |
|
| 372 | - if ($path == '/') { |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - return $this->basicOperation('is_dir', $path); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * @param string $path |
|
| 380 | - * @return bool|mixed |
|
| 381 | - */ |
|
| 382 | - public function is_file($path) { |
|
| 383 | - if ($path == '/') { |
|
| 384 | - return false; |
|
| 385 | - } |
|
| 386 | - return $this->basicOperation('is_file', $path); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * @param string $path |
|
| 391 | - * @return mixed |
|
| 392 | - */ |
|
| 393 | - public function stat($path) { |
|
| 394 | - return $this->basicOperation('stat', $path); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * @param string $path |
|
| 399 | - * @return mixed |
|
| 400 | - */ |
|
| 401 | - public function filetype($path) { |
|
| 402 | - return $this->basicOperation('filetype', $path); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * @param string $path |
|
| 407 | - * @return mixed |
|
| 408 | - */ |
|
| 409 | - public function filesize($path) { |
|
| 410 | - return $this->basicOperation('filesize', $path); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * @param string $path |
|
| 415 | - * @return bool|mixed |
|
| 416 | - * @throws \OCP\Files\InvalidPathException |
|
| 417 | - */ |
|
| 418 | - public function readfile($path) { |
|
| 419 | - $this->assertPathLength($path); |
|
| 420 | - @ob_end_clean(); |
|
| 421 | - $handle = $this->fopen($path, 'rb'); |
|
| 422 | - if ($handle) { |
|
| 423 | - $chunkSize = 8192; // 8 kB chunks |
|
| 424 | - while (!feof($handle)) { |
|
| 425 | - echo fread($handle, $chunkSize); |
|
| 426 | - flush(); |
|
| 427 | - } |
|
| 428 | - fclose($handle); |
|
| 429 | - $size = $this->filesize($path); |
|
| 430 | - return $size; |
|
| 431 | - } |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @param string $path |
|
| 437 | - * @param int $from |
|
| 438 | - * @param int $to |
|
| 439 | - * @return bool|mixed |
|
| 440 | - * @throws \OCP\Files\InvalidPathException |
|
| 441 | - * @throws \OCP\Files\UnseekableException |
|
| 442 | - */ |
|
| 443 | - public function readfilePart($path, $from, $to) { |
|
| 444 | - $this->assertPathLength($path); |
|
| 445 | - @ob_end_clean(); |
|
| 446 | - $handle = $this->fopen($path, 'rb'); |
|
| 447 | - if ($handle) { |
|
| 448 | - $chunkSize = 8192; // 8 kB chunks |
|
| 449 | - $startReading = true; |
|
| 450 | - |
|
| 451 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 452 | - // forward file handle via chunked fread because fseek seem to have failed |
|
| 453 | - |
|
| 454 | - $end = $from + 1; |
|
| 455 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 456 | - $len = $from - ftell($handle); |
|
| 457 | - if ($len > $chunkSize) { |
|
| 458 | - $len = $chunkSize; |
|
| 459 | - } |
|
| 460 | - $result = fread($handle, $len); |
|
| 461 | - |
|
| 462 | - if ($result === false) { |
|
| 463 | - $startReading = false; |
|
| 464 | - break; |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if ($startReading) { |
|
| 470 | - $end = $to + 1; |
|
| 471 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 472 | - $len = $end - ftell($handle); |
|
| 473 | - if ($len > $chunkSize) { |
|
| 474 | - $len = $chunkSize; |
|
| 475 | - } |
|
| 476 | - echo fread($handle, $len); |
|
| 477 | - flush(); |
|
| 478 | - } |
|
| 479 | - $size = ftell($handle) - $from; |
|
| 480 | - return $size; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 484 | - } |
|
| 485 | - return false; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * @param string $path |
|
| 490 | - * @return mixed |
|
| 491 | - */ |
|
| 492 | - public function isCreatable($path) { |
|
| 493 | - return $this->basicOperation('isCreatable', $path); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * @param string $path |
|
| 498 | - * @return mixed |
|
| 499 | - */ |
|
| 500 | - public function isReadable($path) { |
|
| 501 | - return $this->basicOperation('isReadable', $path); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * @param string $path |
|
| 506 | - * @return mixed |
|
| 507 | - */ |
|
| 508 | - public function isUpdatable($path) { |
|
| 509 | - return $this->basicOperation('isUpdatable', $path); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * @param string $path |
|
| 514 | - * @return bool|mixed |
|
| 515 | - */ |
|
| 516 | - public function isDeletable($path) { |
|
| 517 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 518 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 519 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 520 | - return $mount instanceof MoveableMount; |
|
| 521 | - } |
|
| 522 | - return $this->basicOperation('isDeletable', $path); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * @param string $path |
|
| 527 | - * @return mixed |
|
| 528 | - */ |
|
| 529 | - public function isSharable($path) { |
|
| 530 | - return $this->basicOperation('isSharable', $path); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * @param string $path |
|
| 535 | - * @return bool|mixed |
|
| 536 | - */ |
|
| 537 | - public function file_exists($path) { |
|
| 538 | - if ($path == '/') { |
|
| 539 | - return true; |
|
| 540 | - } |
|
| 541 | - return $this->basicOperation('file_exists', $path); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * @param string $path |
|
| 546 | - * @return mixed |
|
| 547 | - */ |
|
| 548 | - public function filemtime($path) { |
|
| 549 | - return $this->basicOperation('filemtime', $path); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * @param string $path |
|
| 554 | - * @param int|string $mtime |
|
| 555 | - * @return bool |
|
| 556 | - */ |
|
| 557 | - public function touch($path, $mtime = null) { |
|
| 558 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 559 | - $mtime = strtotime($mtime); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - $hooks = array('touch'); |
|
| 563 | - |
|
| 564 | - if (!$this->file_exists($path)) { |
|
| 565 | - $hooks[] = 'create'; |
|
| 566 | - $hooks[] = 'write'; |
|
| 567 | - } |
|
| 568 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 569 | - if (!$result) { |
|
| 570 | - // If create file fails because of permissions on external storage like SMB folders, |
|
| 571 | - // check file exists and return false if not. |
|
| 572 | - if (!$this->file_exists($path)) { |
|
| 573 | - return false; |
|
| 574 | - } |
|
| 575 | - if (is_null($mtime)) { |
|
| 576 | - $mtime = time(); |
|
| 577 | - } |
|
| 578 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 579 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 580 | - } |
|
| 581 | - return true; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * @param string $path |
|
| 586 | - * @return mixed |
|
| 587 | - */ |
|
| 588 | - public function file_get_contents($path) { |
|
| 589 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * @param bool $exists |
|
| 594 | - * @param string $path |
|
| 595 | - * @param bool $run |
|
| 596 | - */ |
|
| 597 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 598 | - if (!$exists) { |
|
| 599 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 600 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 601 | - Filesystem::signal_param_run => &$run, |
|
| 602 | - )); |
|
| 603 | - } else { |
|
| 604 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 605 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 606 | - Filesystem::signal_param_run => &$run, |
|
| 607 | - )); |
|
| 608 | - } |
|
| 609 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 610 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 611 | - Filesystem::signal_param_run => &$run, |
|
| 612 | - )); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * @param bool $exists |
|
| 617 | - * @param string $path |
|
| 618 | - */ |
|
| 619 | - protected function emit_file_hooks_post($exists, $path) { |
|
| 620 | - if (!$exists) { |
|
| 621 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 622 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 623 | - )); |
|
| 624 | - } else { |
|
| 625 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 626 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 627 | - )); |
|
| 628 | - } |
|
| 629 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 630 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 631 | - )); |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * @param string $path |
|
| 636 | - * @param mixed $data |
|
| 637 | - * @return bool|mixed |
|
| 638 | - * @throws \Exception |
|
| 639 | - */ |
|
| 640 | - public function file_put_contents($path, $data) { |
|
| 641 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 642 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 643 | - if (Filesystem::isValidPath($path) |
|
| 644 | - and !Filesystem::isFileBlacklisted($path) |
|
| 645 | - ) { |
|
| 646 | - $path = $this->getRelativePath($absolutePath); |
|
| 647 | - |
|
| 648 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 649 | - |
|
| 650 | - $exists = $this->file_exists($path); |
|
| 651 | - $run = true; |
|
| 652 | - if ($this->shouldEmitHooks($path)) { |
|
| 653 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 654 | - } |
|
| 655 | - if (!$run) { |
|
| 656 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 657 | - return false; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 661 | - |
|
| 662 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 663 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 664 | - $target = $storage->fopen($internalPath, 'w'); |
|
| 665 | - if ($target) { |
|
| 666 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 667 | - fclose($target); |
|
| 668 | - fclose($data); |
|
| 669 | - |
|
| 670 | - $this->writeUpdate($storage, $internalPath); |
|
| 671 | - |
|
| 672 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 673 | - |
|
| 674 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 675 | - $this->emit_file_hooks_post($exists, $path); |
|
| 676 | - } |
|
| 677 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 678 | - return $result; |
|
| 679 | - } else { |
|
| 680 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 681 | - return false; |
|
| 682 | - } |
|
| 683 | - } else { |
|
| 684 | - return false; |
|
| 685 | - } |
|
| 686 | - } else { |
|
| 687 | - $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); |
|
| 688 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 689 | - } |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * @param string $path |
|
| 694 | - * @return bool|mixed |
|
| 695 | - */ |
|
| 696 | - public function unlink($path) { |
|
| 697 | - if ($path === '' || $path === '/') { |
|
| 698 | - // do not allow deleting the root |
|
| 699 | - return false; |
|
| 700 | - } |
|
| 701 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 702 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 703 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 704 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 705 | - return $this->removeMount($mount, $absolutePath); |
|
| 706 | - } |
|
| 707 | - if ($this->is_dir($path)) { |
|
| 708 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 709 | - } else { |
|
| 710 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 711 | - } |
|
| 712 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 713 | - $storage = $mount->getStorage(); |
|
| 714 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 715 | - $storage->getUpdater()->remove($internalPath); |
|
| 716 | - return true; |
|
| 717 | - } else { |
|
| 718 | - return $result; |
|
| 719 | - } |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * @param string $directory |
|
| 724 | - * @return bool|mixed |
|
| 725 | - */ |
|
| 726 | - public function deleteAll($directory) { |
|
| 727 | - return $this->rmdir($directory); |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Rename/move a file or folder from the source path to target path. |
|
| 732 | - * |
|
| 733 | - * @param string $path1 source path |
|
| 734 | - * @param string $path2 target path |
|
| 735 | - * |
|
| 736 | - * @return bool|mixed |
|
| 737 | - */ |
|
| 738 | - public function rename($path1, $path2) { |
|
| 739 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 740 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 741 | - $result = false; |
|
| 742 | - if ( |
|
| 743 | - Filesystem::isValidPath($path2) |
|
| 744 | - and Filesystem::isValidPath($path1) |
|
| 745 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 746 | - ) { |
|
| 747 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 748 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 749 | - $exists = $this->file_exists($path2); |
|
| 750 | - |
|
| 751 | - if ($path1 == null or $path2 == null) { |
|
| 752 | - return false; |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 756 | - try { |
|
| 757 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 758 | - |
|
| 759 | - $run = true; |
|
| 760 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 761 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 762 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 763 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
| 764 | - \OC_Hook::emit( |
|
| 765 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 766 | - array( |
|
| 767 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 768 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 769 | - Filesystem::signal_param_run => &$run |
|
| 770 | - ) |
|
| 771 | - ); |
|
| 772 | - } |
|
| 773 | - if ($run) { |
|
| 774 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
| 775 | - |
|
| 776 | - $manager = Filesystem::getMountManager(); |
|
| 777 | - $mount1 = $this->getMount($path1); |
|
| 778 | - $mount2 = $this->getMount($path2); |
|
| 779 | - $storage1 = $mount1->getStorage(); |
|
| 780 | - $storage2 = $mount2->getStorage(); |
|
| 781 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 782 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 783 | - |
|
| 784 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 785 | - try { |
|
| 786 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 787 | - |
|
| 788 | - if ($internalPath1 === '') { |
|
| 789 | - if ($mount1 instanceof MoveableMount) { |
|
| 790 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
| 791 | - /** |
|
| 792 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 793 | - */ |
|
| 794 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
| 795 | - $result = $mount1->moveMount($absolutePath2); |
|
| 796 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 797 | - } else { |
|
| 798 | - $result = false; |
|
| 799 | - } |
|
| 800 | - } else { |
|
| 801 | - $result = false; |
|
| 802 | - } |
|
| 803 | - // moving a file/folder within the same mount point |
|
| 804 | - } elseif ($storage1 === $storage2) { |
|
| 805 | - if ($storage1) { |
|
| 806 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 807 | - } else { |
|
| 808 | - $result = false; |
|
| 809 | - } |
|
| 810 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 811 | - } else { |
|
| 812 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 816 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 817 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 818 | - } else if ($result) { |
|
| 819 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 820 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 821 | - } |
|
| 822 | - } |
|
| 823 | - } catch(\Exception $e) { |
|
| 824 | - throw $e; |
|
| 825 | - } finally { |
|
| 826 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 827 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 831 | - if ($this->shouldEmitHooks()) { |
|
| 832 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 833 | - } |
|
| 834 | - } elseif ($result) { |
|
| 835 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 836 | - \OC_Hook::emit( |
|
| 837 | - Filesystem::CLASSNAME, |
|
| 838 | - Filesystem::signal_post_rename, |
|
| 839 | - array( |
|
| 840 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 841 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 842 | - ) |
|
| 843 | - ); |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - } catch(\Exception $e) { |
|
| 848 | - throw $e; |
|
| 849 | - } finally { |
|
| 850 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 851 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 852 | - } |
|
| 853 | - } |
|
| 854 | - return $result; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Copy a file/folder from the source path to target path |
|
| 859 | - * |
|
| 860 | - * @param string $path1 source path |
|
| 861 | - * @param string $path2 target path |
|
| 862 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 863 | - * |
|
| 864 | - * @return bool|mixed |
|
| 865 | - */ |
|
| 866 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
| 867 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 868 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 869 | - $result = false; |
|
| 870 | - if ( |
|
| 871 | - Filesystem::isValidPath($path2) |
|
| 872 | - and Filesystem::isValidPath($path1) |
|
| 873 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 874 | - ) { |
|
| 875 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 876 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 877 | - |
|
| 878 | - if ($path1 == null or $path2 == null) { |
|
| 879 | - return false; |
|
| 880 | - } |
|
| 881 | - $run = true; |
|
| 882 | - |
|
| 883 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 884 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 885 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 886 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 887 | - |
|
| 888 | - try { |
|
| 889 | - |
|
| 890 | - $exists = $this->file_exists($path2); |
|
| 891 | - if ($this->shouldEmitHooks()) { |
|
| 892 | - \OC_Hook::emit( |
|
| 893 | - Filesystem::CLASSNAME, |
|
| 894 | - Filesystem::signal_copy, |
|
| 895 | - array( |
|
| 896 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 897 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 898 | - Filesystem::signal_param_run => &$run |
|
| 899 | - ) |
|
| 900 | - ); |
|
| 901 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 902 | - } |
|
| 903 | - if ($run) { |
|
| 904 | - $mount1 = $this->getMount($path1); |
|
| 905 | - $mount2 = $this->getMount($path2); |
|
| 906 | - $storage1 = $mount1->getStorage(); |
|
| 907 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 908 | - $storage2 = $mount2->getStorage(); |
|
| 909 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 910 | - |
|
| 911 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 912 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 913 | - |
|
| 914 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 915 | - if ($storage1) { |
|
| 916 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 917 | - } else { |
|
| 918 | - $result = false; |
|
| 919 | - } |
|
| 920 | - } else { |
|
| 921 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 925 | - |
|
| 926 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 927 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 928 | - |
|
| 929 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
| 930 | - \OC_Hook::emit( |
|
| 931 | - Filesystem::CLASSNAME, |
|
| 932 | - Filesystem::signal_post_copy, |
|
| 933 | - array( |
|
| 934 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 935 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 936 | - ) |
|
| 937 | - ); |
|
| 938 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - } |
|
| 942 | - } catch (\Exception $e) { |
|
| 943 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 944 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 945 | - throw $e; |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 949 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 950 | - |
|
| 951 | - } |
|
| 952 | - return $result; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * @param string $path |
|
| 957 | - * @param string $mode 'r' or 'w' |
|
| 958 | - * @return resource |
|
| 959 | - */ |
|
| 960 | - public function fopen($path, $mode) { |
|
| 961 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 962 | - $hooks = array(); |
|
| 963 | - switch ($mode) { |
|
| 964 | - case 'r': |
|
| 965 | - $hooks[] = 'read'; |
|
| 966 | - break; |
|
| 967 | - case 'r+': |
|
| 968 | - case 'w+': |
|
| 969 | - case 'x+': |
|
| 970 | - case 'a+': |
|
| 971 | - $hooks[] = 'read'; |
|
| 972 | - $hooks[] = 'write'; |
|
| 973 | - break; |
|
| 974 | - case 'w': |
|
| 975 | - case 'x': |
|
| 976 | - case 'a': |
|
| 977 | - $hooks[] = 'write'; |
|
| 978 | - break; |
|
| 979 | - default: |
|
| 980 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - if ($mode !== 'r' && $mode !== 'w') { |
|
| 984 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * @param string $path |
|
| 992 | - * @return bool|string |
|
| 993 | - * @throws \OCP\Files\InvalidPathException |
|
| 994 | - */ |
|
| 995 | - public function toTmpFile($path) { |
|
| 996 | - $this->assertPathLength($path); |
|
| 997 | - if (Filesystem::isValidPath($path)) { |
|
| 998 | - $source = $this->fopen($path, 'r'); |
|
| 999 | - if ($source) { |
|
| 1000 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1001 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1002 | - file_put_contents($tmpFile, $source); |
|
| 1003 | - return $tmpFile; |
|
| 1004 | - } else { |
|
| 1005 | - return false; |
|
| 1006 | - } |
|
| 1007 | - } else { |
|
| 1008 | - return false; |
|
| 1009 | - } |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - /** |
|
| 1013 | - * @param string $tmpFile |
|
| 1014 | - * @param string $path |
|
| 1015 | - * @return bool|mixed |
|
| 1016 | - * @throws \OCP\Files\InvalidPathException |
|
| 1017 | - */ |
|
| 1018 | - public function fromTmpFile($tmpFile, $path) { |
|
| 1019 | - $this->assertPathLength($path); |
|
| 1020 | - if (Filesystem::isValidPath($path)) { |
|
| 1021 | - |
|
| 1022 | - // Get directory that the file is going into |
|
| 1023 | - $filePath = dirname($path); |
|
| 1024 | - |
|
| 1025 | - // Create the directories if any |
|
| 1026 | - if (!$this->file_exists($filePath)) { |
|
| 1027 | - $result = $this->createParentDirectories($filePath); |
|
| 1028 | - if ($result === false) { |
|
| 1029 | - return false; |
|
| 1030 | - } |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - $source = fopen($tmpFile, 'r'); |
|
| 1034 | - if ($source) { |
|
| 1035 | - $result = $this->file_put_contents($path, $source); |
|
| 1036 | - // $this->file_put_contents() might have already closed |
|
| 1037 | - // the resource, so we check it, before trying to close it |
|
| 1038 | - // to avoid messages in the error log. |
|
| 1039 | - if (is_resource($source)) { |
|
| 1040 | - fclose($source); |
|
| 1041 | - } |
|
| 1042 | - unlink($tmpFile); |
|
| 1043 | - return $result; |
|
| 1044 | - } else { |
|
| 1045 | - return false; |
|
| 1046 | - } |
|
| 1047 | - } else { |
|
| 1048 | - return false; |
|
| 1049 | - } |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - |
|
| 1053 | - /** |
|
| 1054 | - * @param string $path |
|
| 1055 | - * @return mixed |
|
| 1056 | - * @throws \OCP\Files\InvalidPathException |
|
| 1057 | - */ |
|
| 1058 | - public function getMimeType($path) { |
|
| 1059 | - $this->assertPathLength($path); |
|
| 1060 | - return $this->basicOperation('getMimeType', $path); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - /** |
|
| 1064 | - * @param string $type |
|
| 1065 | - * @param string $path |
|
| 1066 | - * @param bool $raw |
|
| 1067 | - * @return bool|null|string |
|
| 1068 | - */ |
|
| 1069 | - public function hash($type, $path, $raw = false) { |
|
| 1070 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 1071 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1072 | - if (Filesystem::isValidPath($path)) { |
|
| 1073 | - $path = $this->getRelativePath($absolutePath); |
|
| 1074 | - if ($path == null) { |
|
| 1075 | - return false; |
|
| 1076 | - } |
|
| 1077 | - if ($this->shouldEmitHooks($path)) { |
|
| 1078 | - \OC_Hook::emit( |
|
| 1079 | - Filesystem::CLASSNAME, |
|
| 1080 | - Filesystem::signal_read, |
|
| 1081 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1082 | - ); |
|
| 1083 | - } |
|
| 1084 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1085 | - if ($storage) { |
|
| 1086 | - $result = $storage->hash($type, $internalPath, $raw); |
|
| 1087 | - return $result; |
|
| 1088 | - } |
|
| 1089 | - } |
|
| 1090 | - return null; |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - /** |
|
| 1094 | - * @param string $path |
|
| 1095 | - * @return mixed |
|
| 1096 | - * @throws \OCP\Files\InvalidPathException |
|
| 1097 | - */ |
|
| 1098 | - public function free_space($path = '/') { |
|
| 1099 | - $this->assertPathLength($path); |
|
| 1100 | - $result = $this->basicOperation('free_space', $path); |
|
| 1101 | - if ($result === null) { |
|
| 1102 | - throw new InvalidPathException(); |
|
| 1103 | - } |
|
| 1104 | - return $result; |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - /** |
|
| 1108 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1109 | - * |
|
| 1110 | - * @param string $operation |
|
| 1111 | - * @param string $path |
|
| 1112 | - * @param array $hooks (optional) |
|
| 1113 | - * @param mixed $extraParam (optional) |
|
| 1114 | - * @return mixed |
|
| 1115 | - * @throws \Exception |
|
| 1116 | - * |
|
| 1117 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1118 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1119 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1120 | - */ |
|
| 1121 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1122 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 1123 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1124 | - if (Filesystem::isValidPath($path) |
|
| 1125 | - and !Filesystem::isFileBlacklisted($path) |
|
| 1126 | - ) { |
|
| 1127 | - $path = $this->getRelativePath($absolutePath); |
|
| 1128 | - if ($path == null) { |
|
| 1129 | - return false; |
|
| 1130 | - } |
|
| 1131 | - |
|
| 1132 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1133 | - // always a shared lock during pre-hooks so the hook can read the file |
|
| 1134 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - $run = $this->runHooks($hooks, $path); |
|
| 1138 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1139 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1140 | - if ($run and $storage) { |
|
| 1141 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1142 | - try { |
|
| 1143 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1144 | - } catch (LockedException $e) { |
|
| 1145 | - // release the shared lock we acquired before quiting |
|
| 1146 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1147 | - throw $e; |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - try { |
|
| 1151 | - if (!is_null($extraParam)) { |
|
| 1152 | - $result = $storage->$operation($internalPath, $extraParam); |
|
| 1153 | - } else { |
|
| 1154 | - $result = $storage->$operation($internalPath); |
|
| 1155 | - } |
|
| 1156 | - } catch (\Exception $e) { |
|
| 1157 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1158 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1159 | - } else if (in_array('read', $hooks)) { |
|
| 1160 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1161 | - } |
|
| 1162 | - throw $e; |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - if ($result && in_array('delete', $hooks) and $result) { |
|
| 1166 | - $this->removeUpdate($storage, $internalPath); |
|
| 1167 | - } |
|
| 1168 | - if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1169 | - $this->writeUpdate($storage, $internalPath); |
|
| 1170 | - } |
|
| 1171 | - if ($result && in_array('touch', $hooks)) { |
|
| 1172 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1176 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - $unlockLater = false; |
|
| 1180 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1181 | - $unlockLater = true; |
|
| 1182 | - // make sure our unlocking callback will still be called if connection is aborted |
|
| 1183 | - ignore_user_abort(true); |
|
| 1184 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1185 | - if (in_array('write', $hooks)) { |
|
| 1186 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1187 | - } else if (in_array('read', $hooks)) { |
|
| 1188 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1189 | - } |
|
| 1190 | - }); |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1194 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1195 | - $this->runHooks($hooks, $path, true); |
|
| 1196 | - } |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - if (!$unlockLater |
|
| 1200 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1201 | - ) { |
|
| 1202 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1203 | - } |
|
| 1204 | - return $result; |
|
| 1205 | - } else { |
|
| 1206 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1207 | - } |
|
| 1208 | - } |
|
| 1209 | - return null; |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - /** |
|
| 1213 | - * get the path relative to the default root for hook usage |
|
| 1214 | - * |
|
| 1215 | - * @param string $path |
|
| 1216 | - * @return string |
|
| 1217 | - */ |
|
| 1218 | - private function getHookPath($path) { |
|
| 1219 | - if (!Filesystem::getView()) { |
|
| 1220 | - return $path; |
|
| 1221 | - } |
|
| 1222 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - private function shouldEmitHooks($path = '') { |
|
| 1226 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1227 | - return false; |
|
| 1228 | - } |
|
| 1229 | - if (!Filesystem::$loaded) { |
|
| 1230 | - return false; |
|
| 1231 | - } |
|
| 1232 | - $defaultRoot = Filesystem::getRoot(); |
|
| 1233 | - if ($defaultRoot === null) { |
|
| 1234 | - return false; |
|
| 1235 | - } |
|
| 1236 | - if ($this->fakeRoot === $defaultRoot) { |
|
| 1237 | - return true; |
|
| 1238 | - } |
|
| 1239 | - $fullPath = $this->getAbsolutePath($path); |
|
| 1240 | - |
|
| 1241 | - if ($fullPath === $defaultRoot) { |
|
| 1242 | - return true; |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1246 | - } |
|
| 1247 | - |
|
| 1248 | - /** |
|
| 1249 | - * @param string[] $hooks |
|
| 1250 | - * @param string $path |
|
| 1251 | - * @param bool $post |
|
| 1252 | - * @return bool |
|
| 1253 | - */ |
|
| 1254 | - private function runHooks($hooks, $path, $post = false) { |
|
| 1255 | - $relativePath = $path; |
|
| 1256 | - $path = $this->getHookPath($path); |
|
| 1257 | - $prefix = ($post) ? 'post_' : ''; |
|
| 1258 | - $run = true; |
|
| 1259 | - if ($this->shouldEmitHooks($relativePath)) { |
|
| 1260 | - foreach ($hooks as $hook) { |
|
| 1261 | - if ($hook != 'read') { |
|
| 1262 | - \OC_Hook::emit( |
|
| 1263 | - Filesystem::CLASSNAME, |
|
| 1264 | - $prefix . $hook, |
|
| 1265 | - array( |
|
| 1266 | - Filesystem::signal_param_run => &$run, |
|
| 1267 | - Filesystem::signal_param_path => $path |
|
| 1268 | - ) |
|
| 1269 | - ); |
|
| 1270 | - } elseif (!$post) { |
|
| 1271 | - \OC_Hook::emit( |
|
| 1272 | - Filesystem::CLASSNAME, |
|
| 1273 | - $prefix . $hook, |
|
| 1274 | - array( |
|
| 1275 | - Filesystem::signal_param_path => $path |
|
| 1276 | - ) |
|
| 1277 | - ); |
|
| 1278 | - } |
|
| 1279 | - } |
|
| 1280 | - } |
|
| 1281 | - return $run; |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - /** |
|
| 1285 | - * check if a file or folder has been updated since $time |
|
| 1286 | - * |
|
| 1287 | - * @param string $path |
|
| 1288 | - * @param int $time |
|
| 1289 | - * @return bool |
|
| 1290 | - */ |
|
| 1291 | - public function hasUpdated($path, $time) { |
|
| 1292 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - /** |
|
| 1296 | - * @param string $ownerId |
|
| 1297 | - * @return \OC\User\User |
|
| 1298 | - */ |
|
| 1299 | - private function getUserObjectForOwner($ownerId) { |
|
| 1300 | - $owner = $this->userManager->get($ownerId); |
|
| 1301 | - if ($owner instanceof IUser) { |
|
| 1302 | - return $owner; |
|
| 1303 | - } else { |
|
| 1304 | - return new User($ownerId, null); |
|
| 1305 | - } |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - /** |
|
| 1309 | - * Get file info from cache |
|
| 1310 | - * |
|
| 1311 | - * If the file is not in cached it will be scanned |
|
| 1312 | - * If the file has changed on storage the cache will be updated |
|
| 1313 | - * |
|
| 1314 | - * @param \OC\Files\Storage\Storage $storage |
|
| 1315 | - * @param string $internalPath |
|
| 1316 | - * @param string $relativePath |
|
| 1317 | - * @return ICacheEntry|bool |
|
| 1318 | - */ |
|
| 1319 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1320 | - $cache = $storage->getCache($internalPath); |
|
| 1321 | - $data = $cache->get($internalPath); |
|
| 1322 | - $watcher = $storage->getWatcher($internalPath); |
|
| 1323 | - |
|
| 1324 | - try { |
|
| 1325 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1326 | - if (!$data || $data['size'] === -1) { |
|
| 1327 | - if (!$storage->file_exists($internalPath)) { |
|
| 1328 | - return false; |
|
| 1329 | - } |
|
| 1330 | - // don't need to get a lock here since the scanner does it's own locking |
|
| 1331 | - $scanner = $storage->getScanner($internalPath); |
|
| 1332 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1333 | - $data = $cache->get($internalPath); |
|
| 1334 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1335 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1336 | - $watcher->update($internalPath, $data); |
|
| 1337 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1338 | - $data = $cache->get($internalPath); |
|
| 1339 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1340 | - } |
|
| 1341 | - } catch (LockedException $e) { |
|
| 1342 | - // if the file is locked we just use the old cache info |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - return $data; |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - /** |
|
| 1349 | - * get the filesystem info |
|
| 1350 | - * |
|
| 1351 | - * @param string $path |
|
| 1352 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1353 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1354 | - * defaults to true |
|
| 1355 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1356 | - */ |
|
| 1357 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1358 | - $this->assertPathLength($path); |
|
| 1359 | - if (!Filesystem::isValidPath($path)) { |
|
| 1360 | - return false; |
|
| 1361 | - } |
|
| 1362 | - if (Cache\Scanner::isPartialFile($path)) { |
|
| 1363 | - return $this->getPartFileInfo($path); |
|
| 1364 | - } |
|
| 1365 | - $relativePath = $path; |
|
| 1366 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1367 | - |
|
| 1368 | - $mount = Filesystem::getMountManager()->find($path); |
|
| 1369 | - if (!$mount) { |
|
| 1370 | - return false; |
|
| 1371 | - } |
|
| 1372 | - $storage = $mount->getStorage(); |
|
| 1373 | - $internalPath = $mount->getInternalPath($path); |
|
| 1374 | - if ($storage) { |
|
| 1375 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1376 | - |
|
| 1377 | - if (!$data instanceof ICacheEntry) { |
|
| 1378 | - return false; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1382 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1383 | - } |
|
| 1384 | - |
|
| 1385 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1386 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1387 | - |
|
| 1388 | - if ($data and isset($data['fileid'])) { |
|
| 1389 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1390 | - //add the sizes of other mount points to the folder |
|
| 1391 | - $extOnly = ($includeMountPoints === 'ext'); |
|
| 1392 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1393 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1394 | - $subStorage = $mount->getStorage(); |
|
| 1395 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1396 | - })); |
|
| 1397 | - } |
|
| 1398 | - } |
|
| 1399 | - |
|
| 1400 | - return $info; |
|
| 1401 | - } |
|
| 1402 | - |
|
| 1403 | - return false; |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - /** |
|
| 1407 | - * get the content of a directory |
|
| 1408 | - * |
|
| 1409 | - * @param string $directory path under datadirectory |
|
| 1410 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1411 | - * @return FileInfo[] |
|
| 1412 | - */ |
|
| 1413 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1414 | - $this->assertPathLength($directory); |
|
| 1415 | - if (!Filesystem::isValidPath($directory)) { |
|
| 1416 | - return []; |
|
| 1417 | - } |
|
| 1418 | - $path = $this->getAbsolutePath($directory); |
|
| 1419 | - $path = Filesystem::normalizePath($path); |
|
| 1420 | - $mount = $this->getMount($directory); |
|
| 1421 | - if (!$mount) { |
|
| 1422 | - return []; |
|
| 1423 | - } |
|
| 1424 | - $storage = $mount->getStorage(); |
|
| 1425 | - $internalPath = $mount->getInternalPath($path); |
|
| 1426 | - if ($storage) { |
|
| 1427 | - $cache = $storage->getCache($internalPath); |
|
| 1428 | - $user = \OC_User::getUser(); |
|
| 1429 | - |
|
| 1430 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1431 | - |
|
| 1432 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1433 | - return []; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - $folderId = $data['fileid']; |
|
| 1437 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1438 | - |
|
| 1439 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1440 | - |
|
| 1441 | - $fileNames = array_map(function(ICacheEntry $content) { |
|
| 1442 | - return $content->getName(); |
|
| 1443 | - }, $contents); |
|
| 1444 | - /** |
|
| 1445 | - * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1446 | - */ |
|
| 1447 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1448 | - if ($sharingDisabled) { |
|
| 1449 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1450 | - } |
|
| 1451 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1452 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1453 | - }, $contents); |
|
| 1454 | - $files = array_combine($fileNames, $fileInfos); |
|
| 1455 | - |
|
| 1456 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1457 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1458 | - $dirLength = strlen($path); |
|
| 1459 | - foreach ($mounts as $mount) { |
|
| 1460 | - $mountPoint = $mount->getMountPoint(); |
|
| 1461 | - $subStorage = $mount->getStorage(); |
|
| 1462 | - if ($subStorage) { |
|
| 1463 | - $subCache = $subStorage->getCache(''); |
|
| 1464 | - |
|
| 1465 | - $rootEntry = $subCache->get(''); |
|
| 1466 | - if (!$rootEntry) { |
|
| 1467 | - $subScanner = $subStorage->getScanner(''); |
|
| 1468 | - try { |
|
| 1469 | - $subScanner->scanFile(''); |
|
| 1470 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1471 | - continue; |
|
| 1472 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1473 | - continue; |
|
| 1474 | - } catch (\Exception $e) { |
|
| 1475 | - // sometimes when the storage is not available it can be any exception |
|
| 1476 | - \OCP\Util::writeLog( |
|
| 1477 | - 'core', |
|
| 1478 | - 'Exception while scanning storage "' . $subStorage->getId() . '": ' . |
|
| 1479 | - get_class($e) . ': ' . $e->getMessage(), |
|
| 1480 | - \OCP\Util::ERROR |
|
| 1481 | - ); |
|
| 1482 | - continue; |
|
| 1483 | - } |
|
| 1484 | - $rootEntry = $subCache->get(''); |
|
| 1485 | - } |
|
| 1486 | - |
|
| 1487 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1488 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1489 | - if ($pos = strpos($relativePath, '/')) { |
|
| 1490 | - //mountpoint inside subfolder add size to the correct folder |
|
| 1491 | - $entryName = substr($relativePath, 0, $pos); |
|
| 1492 | - foreach ($files as &$entry) { |
|
| 1493 | - if ($entry->getName() === $entryName) { |
|
| 1494 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1495 | - } |
|
| 1496 | - } |
|
| 1497 | - } else { //mountpoint in this folder, add an entry for it |
|
| 1498 | - $rootEntry['name'] = $relativePath; |
|
| 1499 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1500 | - $permissions = $rootEntry['permissions']; |
|
| 1501 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1502 | - // for shared files/folders we use the permissions given by the owner |
|
| 1503 | - if ($mount instanceof MoveableMount) { |
|
| 1504 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1505 | - } else { |
|
| 1506 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1507 | - } |
|
| 1508 | - |
|
| 1509 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1510 | - |
|
| 1511 | - // if sharing was disabled for the user we remove the share permissions |
|
| 1512 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1513 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1514 | - } |
|
| 1515 | - |
|
| 1516 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1517 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1518 | - } |
|
| 1519 | - } |
|
| 1520 | - } |
|
| 1521 | - } |
|
| 1522 | - |
|
| 1523 | - if ($mimetype_filter) { |
|
| 1524 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1525 | - if (strpos($mimetype_filter, '/')) { |
|
| 1526 | - return $file->getMimetype() === $mimetype_filter; |
|
| 1527 | - } else { |
|
| 1528 | - return $file->getMimePart() === $mimetype_filter; |
|
| 1529 | - } |
|
| 1530 | - }); |
|
| 1531 | - } |
|
| 1532 | - |
|
| 1533 | - return array_values($files); |
|
| 1534 | - } else { |
|
| 1535 | - return []; |
|
| 1536 | - } |
|
| 1537 | - } |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * change file metadata |
|
| 1541 | - * |
|
| 1542 | - * @param string $path |
|
| 1543 | - * @param array|\OCP\Files\FileInfo $data |
|
| 1544 | - * @return int |
|
| 1545 | - * |
|
| 1546 | - * returns the fileid of the updated file |
|
| 1547 | - */ |
|
| 1548 | - public function putFileInfo($path, $data) { |
|
| 1549 | - $this->assertPathLength($path); |
|
| 1550 | - if ($data instanceof FileInfo) { |
|
| 1551 | - $data = $data->getData(); |
|
| 1552 | - } |
|
| 1553 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1554 | - /** |
|
| 1555 | - * @var \OC\Files\Storage\Storage $storage |
|
| 1556 | - * @var string $internalPath |
|
| 1557 | - */ |
|
| 1558 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1559 | - if ($storage) { |
|
| 1560 | - $cache = $storage->getCache($path); |
|
| 1561 | - |
|
| 1562 | - if (!$cache->inCache($internalPath)) { |
|
| 1563 | - $scanner = $storage->getScanner($internalPath); |
|
| 1564 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1565 | - } |
|
| 1566 | - |
|
| 1567 | - return $cache->put($internalPath, $data); |
|
| 1568 | - } else { |
|
| 1569 | - return -1; |
|
| 1570 | - } |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - /** |
|
| 1574 | - * search for files with the name matching $query |
|
| 1575 | - * |
|
| 1576 | - * @param string $query |
|
| 1577 | - * @return FileInfo[] |
|
| 1578 | - */ |
|
| 1579 | - public function search($query) { |
|
| 1580 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1581 | - } |
|
| 1582 | - |
|
| 1583 | - /** |
|
| 1584 | - * search for files with the name matching $query |
|
| 1585 | - * |
|
| 1586 | - * @param string $query |
|
| 1587 | - * @return FileInfo[] |
|
| 1588 | - */ |
|
| 1589 | - public function searchRaw($query) { |
|
| 1590 | - return $this->searchCommon('search', array($query)); |
|
| 1591 | - } |
|
| 1592 | - |
|
| 1593 | - /** |
|
| 1594 | - * search for files by mimetype |
|
| 1595 | - * |
|
| 1596 | - * @param string $mimetype |
|
| 1597 | - * @return FileInfo[] |
|
| 1598 | - */ |
|
| 1599 | - public function searchByMime($mimetype) { |
|
| 1600 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1601 | - } |
|
| 1602 | - |
|
| 1603 | - /** |
|
| 1604 | - * search for files by tag |
|
| 1605 | - * |
|
| 1606 | - * @param string|int $tag name or tag id |
|
| 1607 | - * @param string $userId owner of the tags |
|
| 1608 | - * @return FileInfo[] |
|
| 1609 | - */ |
|
| 1610 | - public function searchByTag($tag, $userId) { |
|
| 1611 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1612 | - } |
|
| 1613 | - |
|
| 1614 | - /** |
|
| 1615 | - * @param string $method cache method |
|
| 1616 | - * @param array $args |
|
| 1617 | - * @return FileInfo[] |
|
| 1618 | - */ |
|
| 1619 | - private function searchCommon($method, $args) { |
|
| 1620 | - $files = array(); |
|
| 1621 | - $rootLength = strlen($this->fakeRoot); |
|
| 1622 | - |
|
| 1623 | - $mount = $this->getMount(''); |
|
| 1624 | - $mountPoint = $mount->getMountPoint(); |
|
| 1625 | - $storage = $mount->getStorage(); |
|
| 1626 | - if ($storage) { |
|
| 1627 | - $cache = $storage->getCache(''); |
|
| 1628 | - |
|
| 1629 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1630 | - foreach ($results as $result) { |
|
| 1631 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1632 | - $internalPath = $result['path']; |
|
| 1633 | - $path = $mountPoint . $result['path']; |
|
| 1634 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1635 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1636 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1637 | - } |
|
| 1638 | - } |
|
| 1639 | - |
|
| 1640 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1641 | - foreach ($mounts as $mount) { |
|
| 1642 | - $mountPoint = $mount->getMountPoint(); |
|
| 1643 | - $storage = $mount->getStorage(); |
|
| 1644 | - if ($storage) { |
|
| 1645 | - $cache = $storage->getCache(''); |
|
| 1646 | - |
|
| 1647 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1648 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1649 | - if ($results) { |
|
| 1650 | - foreach ($results as $result) { |
|
| 1651 | - $internalPath = $result['path']; |
|
| 1652 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1653 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1654 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1655 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1656 | - } |
|
| 1657 | - } |
|
| 1658 | - } |
|
| 1659 | - } |
|
| 1660 | - } |
|
| 1661 | - return $files; |
|
| 1662 | - } |
|
| 1663 | - |
|
| 1664 | - /** |
|
| 1665 | - * Get the owner for a file or folder |
|
| 1666 | - * |
|
| 1667 | - * @param string $path |
|
| 1668 | - * @return string the user id of the owner |
|
| 1669 | - * @throws NotFoundException |
|
| 1670 | - */ |
|
| 1671 | - public function getOwner($path) { |
|
| 1672 | - $info = $this->getFileInfo($path); |
|
| 1673 | - if (!$info) { |
|
| 1674 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1675 | - } |
|
| 1676 | - return $info->getOwner()->getUID(); |
|
| 1677 | - } |
|
| 1678 | - |
|
| 1679 | - /** |
|
| 1680 | - * get the ETag for a file or folder |
|
| 1681 | - * |
|
| 1682 | - * @param string $path |
|
| 1683 | - * @return string |
|
| 1684 | - */ |
|
| 1685 | - public function getETag($path) { |
|
| 1686 | - /** |
|
| 1687 | - * @var Storage\Storage $storage |
|
| 1688 | - * @var string $internalPath |
|
| 1689 | - */ |
|
| 1690 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1691 | - if ($storage) { |
|
| 1692 | - return $storage->getETag($internalPath); |
|
| 1693 | - } else { |
|
| 1694 | - return null; |
|
| 1695 | - } |
|
| 1696 | - } |
|
| 1697 | - |
|
| 1698 | - /** |
|
| 1699 | - * Get the path of a file by id, relative to the view |
|
| 1700 | - * |
|
| 1701 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1702 | - * |
|
| 1703 | - * @param int $id |
|
| 1704 | - * @throws NotFoundException |
|
| 1705 | - * @return string |
|
| 1706 | - */ |
|
| 1707 | - public function getPath($id) { |
|
| 1708 | - $id = (int)$id; |
|
| 1709 | - $manager = Filesystem::getMountManager(); |
|
| 1710 | - $mounts = $manager->findIn($this->fakeRoot); |
|
| 1711 | - $mounts[] = $manager->find($this->fakeRoot); |
|
| 1712 | - // reverse the array so we start with the storage this view is in |
|
| 1713 | - // which is the most likely to contain the file we're looking for |
|
| 1714 | - $mounts = array_reverse($mounts); |
|
| 1715 | - foreach ($mounts as $mount) { |
|
| 1716 | - /** |
|
| 1717 | - * @var \OC\Files\Mount\MountPoint $mount |
|
| 1718 | - */ |
|
| 1719 | - if ($mount->getStorage()) { |
|
| 1720 | - $cache = $mount->getStorage()->getCache(); |
|
| 1721 | - $internalPath = $cache->getPathById($id); |
|
| 1722 | - if (is_string($internalPath)) { |
|
| 1723 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1724 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1725 | - return $path; |
|
| 1726 | - } |
|
| 1727 | - } |
|
| 1728 | - } |
|
| 1729 | - } |
|
| 1730 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1731 | - } |
|
| 1732 | - |
|
| 1733 | - /** |
|
| 1734 | - * @param string $path |
|
| 1735 | - * @throws InvalidPathException |
|
| 1736 | - */ |
|
| 1737 | - private function assertPathLength($path) { |
|
| 1738 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1739 | - // Check for the string length - performed using isset() instead of strlen() |
|
| 1740 | - // because isset() is about 5x-40x faster. |
|
| 1741 | - if (isset($path[$maxLen])) { |
|
| 1742 | - $pathLen = strlen($path); |
|
| 1743 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1744 | - } |
|
| 1745 | - } |
|
| 1746 | - |
|
| 1747 | - /** |
|
| 1748 | - * check if it is allowed to move a mount point to a given target. |
|
| 1749 | - * It is not allowed to move a mount point into a different mount point or |
|
| 1750 | - * into an already shared folder |
|
| 1751 | - * |
|
| 1752 | - * @param string $target path |
|
| 1753 | - * @return boolean |
|
| 1754 | - */ |
|
| 1755 | - private function isTargetAllowed($target) { |
|
| 1756 | - |
|
| 1757 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1758 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1759 | - \OCP\Util::writeLog('files', |
|
| 1760 | - 'It is not allowed to move one mount point into another one', |
|
| 1761 | - \OCP\Util::DEBUG); |
|
| 1762 | - return false; |
|
| 1763 | - } |
|
| 1764 | - |
|
| 1765 | - // note: cannot use the view because the target is already locked |
|
| 1766 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1767 | - if ($fileId === -1) { |
|
| 1768 | - // target might not exist, need to check parent instead |
|
| 1769 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1770 | - } |
|
| 1771 | - |
|
| 1772 | - // check if any of the parents were shared by the current owner (include collections) |
|
| 1773 | - $shares = \OCP\Share::getItemShared( |
|
| 1774 | - 'folder', |
|
| 1775 | - $fileId, |
|
| 1776 | - \OCP\Share::FORMAT_NONE, |
|
| 1777 | - null, |
|
| 1778 | - true |
|
| 1779 | - ); |
|
| 1780 | - |
|
| 1781 | - if (count($shares) > 0) { |
|
| 1782 | - \OCP\Util::writeLog('files', |
|
| 1783 | - 'It is not allowed to move one mount point into a shared folder', |
|
| 1784 | - \OCP\Util::DEBUG); |
|
| 1785 | - return false; |
|
| 1786 | - } |
|
| 1787 | - |
|
| 1788 | - return true; |
|
| 1789 | - } |
|
| 1790 | - |
|
| 1791 | - /** |
|
| 1792 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1793 | - * |
|
| 1794 | - * @param string $path |
|
| 1795 | - * @return \OCP\Files\FileInfo |
|
| 1796 | - */ |
|
| 1797 | - private function getPartFileInfo($path) { |
|
| 1798 | - $mount = $this->getMount($path); |
|
| 1799 | - $storage = $mount->getStorage(); |
|
| 1800 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1801 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1802 | - return new FileInfo( |
|
| 1803 | - $this->getAbsolutePath($path), |
|
| 1804 | - $storage, |
|
| 1805 | - $internalPath, |
|
| 1806 | - [ |
|
| 1807 | - 'fileid' => null, |
|
| 1808 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1809 | - 'name' => basename($path), |
|
| 1810 | - 'etag' => null, |
|
| 1811 | - 'size' => $storage->filesize($internalPath), |
|
| 1812 | - 'mtime' => $storage->filemtime($internalPath), |
|
| 1813 | - 'encrypted' => false, |
|
| 1814 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1815 | - ], |
|
| 1816 | - $mount, |
|
| 1817 | - $owner |
|
| 1818 | - ); |
|
| 1819 | - } |
|
| 1820 | - |
|
| 1821 | - /** |
|
| 1822 | - * @param string $path |
|
| 1823 | - * @param string $fileName |
|
| 1824 | - * @throws InvalidPathException |
|
| 1825 | - */ |
|
| 1826 | - public function verifyPath($path, $fileName) { |
|
| 1827 | - try { |
|
| 1828 | - /** @type \OCP\Files\Storage $storage */ |
|
| 1829 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1830 | - $storage->verifyPath($internalPath, $fileName); |
|
| 1831 | - } catch (ReservedWordException $ex) { |
|
| 1832 | - $l = \OC::$server->getL10N('lib'); |
|
| 1833 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1834 | - } catch (InvalidCharacterInPathException $ex) { |
|
| 1835 | - $l = \OC::$server->getL10N('lib'); |
|
| 1836 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1837 | - } catch (FileNameTooLongException $ex) { |
|
| 1838 | - $l = \OC::$server->getL10N('lib'); |
|
| 1839 | - throw new InvalidPathException($l->t('File name is too long')); |
|
| 1840 | - } catch (InvalidDirectoryException $ex) { |
|
| 1841 | - $l = \OC::$server->getL10N('lib'); |
|
| 1842 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1843 | - } catch (EmptyFileNameException $ex) { |
|
| 1844 | - $l = \OC::$server->getL10N('lib'); |
|
| 1845 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1846 | - } |
|
| 1847 | - } |
|
| 1848 | - |
|
| 1849 | - /** |
|
| 1850 | - * get all parent folders of $path |
|
| 1851 | - * |
|
| 1852 | - * @param string $path |
|
| 1853 | - * @return string[] |
|
| 1854 | - */ |
|
| 1855 | - private function getParents($path) { |
|
| 1856 | - $path = trim($path, '/'); |
|
| 1857 | - if (!$path) { |
|
| 1858 | - return []; |
|
| 1859 | - } |
|
| 1860 | - |
|
| 1861 | - $parts = explode('/', $path); |
|
| 1862 | - |
|
| 1863 | - // remove the single file |
|
| 1864 | - array_pop($parts); |
|
| 1865 | - $result = array('/'); |
|
| 1866 | - $resultPath = ''; |
|
| 1867 | - foreach ($parts as $part) { |
|
| 1868 | - if ($part) { |
|
| 1869 | - $resultPath .= '/' . $part; |
|
| 1870 | - $result[] = $resultPath; |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - return $result; |
|
| 1874 | - } |
|
| 1875 | - |
|
| 1876 | - /** |
|
| 1877 | - * Returns the mount point for which to lock |
|
| 1878 | - * |
|
| 1879 | - * @param string $absolutePath absolute path |
|
| 1880 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1881 | - * is mounted directly on the given path, false otherwise |
|
| 1882 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1883 | - */ |
|
| 1884 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1885 | - $results = []; |
|
| 1886 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1887 | - if (!$mount) { |
|
| 1888 | - return $results; |
|
| 1889 | - } |
|
| 1890 | - |
|
| 1891 | - if ($useParentMount) { |
|
| 1892 | - // find out if something is mounted directly on the path |
|
| 1893 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1894 | - if ($internalPath === '') { |
|
| 1895 | - // resolve the parent mount instead |
|
| 1896 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1897 | - } |
|
| 1898 | - } |
|
| 1899 | - |
|
| 1900 | - return $mount; |
|
| 1901 | - } |
|
| 1902 | - |
|
| 1903 | - /** |
|
| 1904 | - * Lock the given path |
|
| 1905 | - * |
|
| 1906 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1907 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1908 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1909 | - * |
|
| 1910 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1911 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1912 | - */ |
|
| 1913 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1914 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1915 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1916 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1917 | - return false; |
|
| 1918 | - } |
|
| 1919 | - |
|
| 1920 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1921 | - if ($mount) { |
|
| 1922 | - try { |
|
| 1923 | - $storage = $mount->getStorage(); |
|
| 1924 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1925 | - $storage->acquireLock( |
|
| 1926 | - $mount->getInternalPath($absolutePath), |
|
| 1927 | - $type, |
|
| 1928 | - $this->lockingProvider |
|
| 1929 | - ); |
|
| 1930 | - } |
|
| 1931 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1932 | - // rethrow with the a human-readable path |
|
| 1933 | - throw new \OCP\Lock\LockedException( |
|
| 1934 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1935 | - $e |
|
| 1936 | - ); |
|
| 1937 | - } |
|
| 1938 | - } |
|
| 1939 | - |
|
| 1940 | - return true; |
|
| 1941 | - } |
|
| 1942 | - |
|
| 1943 | - /** |
|
| 1944 | - * Change the lock type |
|
| 1945 | - * |
|
| 1946 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1947 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1948 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1949 | - * |
|
| 1950 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1951 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1952 | - */ |
|
| 1953 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1954 | - $path = Filesystem::normalizePath($path); |
|
| 1955 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1956 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1957 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1958 | - return false; |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1962 | - if ($mount) { |
|
| 1963 | - try { |
|
| 1964 | - $storage = $mount->getStorage(); |
|
| 1965 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1966 | - $storage->changeLock( |
|
| 1967 | - $mount->getInternalPath($absolutePath), |
|
| 1968 | - $type, |
|
| 1969 | - $this->lockingProvider |
|
| 1970 | - ); |
|
| 1971 | - } |
|
| 1972 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1973 | - try { |
|
| 1974 | - // rethrow with the a human-readable path |
|
| 1975 | - throw new \OCP\Lock\LockedException( |
|
| 1976 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1977 | - $e |
|
| 1978 | - ); |
|
| 1979 | - } catch (\InvalidArgumentException $e) { |
|
| 1980 | - throw new \OCP\Lock\LockedException( |
|
| 1981 | - $absolutePath, |
|
| 1982 | - $e |
|
| 1983 | - ); |
|
| 1984 | - } |
|
| 1985 | - } |
|
| 1986 | - } |
|
| 1987 | - |
|
| 1988 | - return true; |
|
| 1989 | - } |
|
| 1990 | - |
|
| 1991 | - /** |
|
| 1992 | - * Unlock the given path |
|
| 1993 | - * |
|
| 1994 | - * @param string $path the path of the file to unlock, relative to the view |
|
| 1995 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1996 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1997 | - * |
|
| 1998 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1999 | - */ |
|
| 2000 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2001 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2002 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2003 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2004 | - return false; |
|
| 2005 | - } |
|
| 2006 | - |
|
| 2007 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2008 | - if ($mount) { |
|
| 2009 | - $storage = $mount->getStorage(); |
|
| 2010 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2011 | - $storage->releaseLock( |
|
| 2012 | - $mount->getInternalPath($absolutePath), |
|
| 2013 | - $type, |
|
| 2014 | - $this->lockingProvider |
|
| 2015 | - ); |
|
| 2016 | - } |
|
| 2017 | - } |
|
| 2018 | - |
|
| 2019 | - return true; |
|
| 2020 | - } |
|
| 2021 | - |
|
| 2022 | - /** |
|
| 2023 | - * Lock a path and all its parents up to the root of the view |
|
| 2024 | - * |
|
| 2025 | - * @param string $path the path of the file to lock relative to the view |
|
| 2026 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2027 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2028 | - * |
|
| 2029 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2030 | - */ |
|
| 2031 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2032 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2033 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2034 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2035 | - return false; |
|
| 2036 | - } |
|
| 2037 | - |
|
| 2038 | - $this->lockPath($path, $type, $lockMountPoint); |
|
| 2039 | - |
|
| 2040 | - $parents = $this->getParents($path); |
|
| 2041 | - foreach ($parents as $parent) { |
|
| 2042 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2043 | - } |
|
| 2044 | - |
|
| 2045 | - return true; |
|
| 2046 | - } |
|
| 2047 | - |
|
| 2048 | - /** |
|
| 2049 | - * Unlock a path and all its parents up to the root of the view |
|
| 2050 | - * |
|
| 2051 | - * @param string $path the path of the file to lock relative to the view |
|
| 2052 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2053 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2054 | - * |
|
| 2055 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2056 | - */ |
|
| 2057 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2058 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2059 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2060 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2061 | - return false; |
|
| 2062 | - } |
|
| 2063 | - |
|
| 2064 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2065 | - |
|
| 2066 | - $parents = $this->getParents($path); |
|
| 2067 | - foreach ($parents as $parent) { |
|
| 2068 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2069 | - } |
|
| 2070 | - |
|
| 2071 | - return true; |
|
| 2072 | - } |
|
| 2073 | - |
|
| 2074 | - /** |
|
| 2075 | - * Only lock files in data/user/files/ |
|
| 2076 | - * |
|
| 2077 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2078 | - * @return bool |
|
| 2079 | - */ |
|
| 2080 | - protected function shouldLockFile($path) { |
|
| 2081 | - $path = Filesystem::normalizePath($path); |
|
| 2082 | - |
|
| 2083 | - $pathSegments = explode('/', $path); |
|
| 2084 | - if (isset($pathSegments[2])) { |
|
| 2085 | - // E.g.: /username/files/path-to-file |
|
| 2086 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2087 | - } |
|
| 2088 | - |
|
| 2089 | - return strpos($path, '/appdata_') !== 0; |
|
| 2090 | - } |
|
| 2091 | - |
|
| 2092 | - /** |
|
| 2093 | - * Shortens the given absolute path to be relative to |
|
| 2094 | - * "$user/files". |
|
| 2095 | - * |
|
| 2096 | - * @param string $absolutePath absolute path which is under "files" |
|
| 2097 | - * |
|
| 2098 | - * @return string path relative to "files" with trimmed slashes or null |
|
| 2099 | - * if the path was NOT relative to files |
|
| 2100 | - * |
|
| 2101 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2102 | - * @since 8.1.0 |
|
| 2103 | - */ |
|
| 2104 | - public function getPathRelativeToFiles($absolutePath) { |
|
| 2105 | - $path = Filesystem::normalizePath($absolutePath); |
|
| 2106 | - $parts = explode('/', trim($path, '/'), 3); |
|
| 2107 | - // "$user", "files", "path/to/dir" |
|
| 2108 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2109 | - $this->logger->error( |
|
| 2110 | - '$absolutePath must be relative to "files", value is "%s"', |
|
| 2111 | - [ |
|
| 2112 | - $absolutePath |
|
| 2113 | - ] |
|
| 2114 | - ); |
|
| 2115 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2116 | - } |
|
| 2117 | - if (isset($parts[2])) { |
|
| 2118 | - return $parts[2]; |
|
| 2119 | - } |
|
| 2120 | - return ''; |
|
| 2121 | - } |
|
| 2122 | - |
|
| 2123 | - /** |
|
| 2124 | - * @param string $filename |
|
| 2125 | - * @return array |
|
| 2126 | - * @throws \OC\User\NoUserException |
|
| 2127 | - * @throws NotFoundException |
|
| 2128 | - */ |
|
| 2129 | - public function getUidAndFilename($filename) { |
|
| 2130 | - $info = $this->getFileInfo($filename); |
|
| 2131 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2132 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2133 | - } |
|
| 2134 | - $uid = $info->getOwner()->getUID(); |
|
| 2135 | - if ($uid != \OCP\User::getUser()) { |
|
| 2136 | - Filesystem::initMountPoints($uid); |
|
| 2137 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2138 | - try { |
|
| 2139 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 2140 | - } catch (NotFoundException $e) { |
|
| 2141 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2142 | - } |
|
| 2143 | - } |
|
| 2144 | - return [$uid, $filename]; |
|
| 2145 | - } |
|
| 2146 | - |
|
| 2147 | - /** |
|
| 2148 | - * Creates parent non-existing folders |
|
| 2149 | - * |
|
| 2150 | - * @param string $filePath |
|
| 2151 | - * @return bool |
|
| 2152 | - */ |
|
| 2153 | - private function createParentDirectories($filePath) { |
|
| 2154 | - $directoryParts = explode('/', $filePath); |
|
| 2155 | - $directoryParts = array_filter($directoryParts); |
|
| 2156 | - foreach ($directoryParts as $key => $part) { |
|
| 2157 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2158 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2159 | - if ($this->is_file($currentPath)) { |
|
| 2160 | - return false; |
|
| 2161 | - } |
|
| 2162 | - if (!$this->file_exists($currentPath)) { |
|
| 2163 | - $this->mkdir($currentPath); |
|
| 2164 | - } |
|
| 2165 | - } |
|
| 2166 | - |
|
| 2167 | - return true; |
|
| 2168 | - } |
|
| 83 | + /** @var string */ |
|
| 84 | + private $fakeRoot = ''; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @var \OCP\Lock\ILockingProvider |
|
| 88 | + */ |
|
| 89 | + protected $lockingProvider; |
|
| 90 | + |
|
| 91 | + private $lockingEnabled; |
|
| 92 | + |
|
| 93 | + private $updaterEnabled = true; |
|
| 94 | + |
|
| 95 | + /** @var \OC\User\Manager */ |
|
| 96 | + private $userManager; |
|
| 97 | + |
|
| 98 | + /** @var \OCP\ILogger */ |
|
| 99 | + private $logger; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param string $root |
|
| 103 | + * @throws \Exception If $root contains an invalid path |
|
| 104 | + */ |
|
| 105 | + public function __construct($root = '') { |
|
| 106 | + if (is_null($root)) { |
|
| 107 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 108 | + } |
|
| 109 | + if (!Filesystem::isValidPath($root)) { |
|
| 110 | + throw new \Exception(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->fakeRoot = $root; |
|
| 114 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 115 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 116 | + $this->userManager = \OC::$server->getUserManager(); |
|
| 117 | + $this->logger = \OC::$server->getLogger(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function getAbsolutePath($path = '/') { |
|
| 121 | + if ($path === null) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + $this->assertPathLength($path); |
|
| 125 | + if ($path === '') { |
|
| 126 | + $path = '/'; |
|
| 127 | + } |
|
| 128 | + if ($path[0] !== '/') { |
|
| 129 | + $path = '/' . $path; |
|
| 130 | + } |
|
| 131 | + return $this->fakeRoot . $path; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * change the root to a fake root |
|
| 136 | + * |
|
| 137 | + * @param string $fakeRoot |
|
| 138 | + * @return boolean|null |
|
| 139 | + */ |
|
| 140 | + public function chroot($fakeRoot) { |
|
| 141 | + if (!$fakeRoot == '') { |
|
| 142 | + if ($fakeRoot[0] !== '/') { |
|
| 143 | + $fakeRoot = '/' . $fakeRoot; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + $this->fakeRoot = $fakeRoot; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * get the fake root |
|
| 151 | + * |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getRoot() { |
|
| 155 | + return $this->fakeRoot; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * get path relative to the root of the view |
|
| 160 | + * |
|
| 161 | + * @param string $path |
|
| 162 | + * @return string |
|
| 163 | + */ |
|
| 164 | + public function getRelativePath($path) { |
|
| 165 | + $this->assertPathLength($path); |
|
| 166 | + if ($this->fakeRoot == '') { |
|
| 167 | + return $path; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 171 | + return '/'; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // missing slashes can cause wrong matches! |
|
| 175 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 176 | + |
|
| 177 | + if (strpos($path, $root) !== 0) { |
|
| 178 | + return null; |
|
| 179 | + } else { |
|
| 180 | + $path = substr($path, strlen($this->fakeRoot)); |
|
| 181 | + if (strlen($path) === 0) { |
|
| 182 | + return '/'; |
|
| 183 | + } else { |
|
| 184 | + return $path; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * get the mountpoint of the storage object for a path |
|
| 191 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 192 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 193 | + * and does not take the chroot into account ) |
|
| 194 | + * |
|
| 195 | + * @param string $path |
|
| 196 | + * @return string |
|
| 197 | + */ |
|
| 198 | + public function getMountPoint($path) { |
|
| 199 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * get the mountpoint of the storage object for a path |
|
| 204 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 205 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 206 | + * and does not take the chroot into account ) |
|
| 207 | + * |
|
| 208 | + * @param string $path |
|
| 209 | + * @return \OCP\Files\Mount\IMountPoint |
|
| 210 | + */ |
|
| 211 | + public function getMount($path) { |
|
| 212 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * resolve a path to a storage and internal path |
|
| 217 | + * |
|
| 218 | + * @param string $path |
|
| 219 | + * @return array an array consisting of the storage and the internal path |
|
| 220 | + */ |
|
| 221 | + public function resolvePath($path) { |
|
| 222 | + $a = $this->getAbsolutePath($path); |
|
| 223 | + $p = Filesystem::normalizePath($a); |
|
| 224 | + return Filesystem::resolvePath($p); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * return the path to a local version of the file |
|
| 229 | + * we need this because we can't know if a file is stored local or not from |
|
| 230 | + * outside the filestorage and for some purposes a local file is needed |
|
| 231 | + * |
|
| 232 | + * @param string $path |
|
| 233 | + * @return string |
|
| 234 | + */ |
|
| 235 | + public function getLocalFile($path) { |
|
| 236 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 237 | + $path = $this->getAbsolutePath($path); |
|
| 238 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 239 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 240 | + return $storage->getLocalFile($internalPath); |
|
| 241 | + } else { |
|
| 242 | + return null; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param string $path |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function getLocalFolder($path) { |
|
| 251 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 252 | + $path = $this->getAbsolutePath($path); |
|
| 253 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 254 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 255 | + return $storage->getLocalFolder($internalPath); |
|
| 256 | + } else { |
|
| 257 | + return null; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * the following functions operate with arguments and return values identical |
|
| 263 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 264 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 265 | + */ |
|
| 266 | + public function mkdir($path) { |
|
| 267 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * remove mount point |
|
| 272 | + * |
|
| 273 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
| 274 | + * @param string $path relative to data/ |
|
| 275 | + * @return boolean |
|
| 276 | + */ |
|
| 277 | + protected function removeMount($mount, $path) { |
|
| 278 | + if ($mount instanceof MoveableMount) { |
|
| 279 | + // cut of /user/files to get the relative path to data/user/files |
|
| 280 | + $pathParts = explode('/', $path, 4); |
|
| 281 | + $relPath = '/' . $pathParts[3]; |
|
| 282 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 283 | + \OC_Hook::emit( |
|
| 284 | + Filesystem::CLASSNAME, "umount", |
|
| 285 | + array(Filesystem::signal_param_path => $relPath) |
|
| 286 | + ); |
|
| 287 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 288 | + $result = $mount->removeMount(); |
|
| 289 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 290 | + if ($result) { |
|
| 291 | + \OC_Hook::emit( |
|
| 292 | + Filesystem::CLASSNAME, "post_umount", |
|
| 293 | + array(Filesystem::signal_param_path => $relPath) |
|
| 294 | + ); |
|
| 295 | + } |
|
| 296 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 297 | + return $result; |
|
| 298 | + } else { |
|
| 299 | + // do not allow deleting the storage's root / the mount point |
|
| 300 | + // because for some storages it might delete the whole contents |
|
| 301 | + // but isn't supposed to work that way |
|
| 302 | + return false; |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + public function disableCacheUpdate() { |
|
| 307 | + $this->updaterEnabled = false; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + public function enableCacheUpdate() { |
|
| 311 | + $this->updaterEnabled = true; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 315 | + if ($this->updaterEnabled) { |
|
| 316 | + if (is_null($time)) { |
|
| 317 | + $time = time(); |
|
| 318 | + } |
|
| 319 | + $storage->getUpdater()->update($internalPath, $time); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 324 | + if ($this->updaterEnabled) { |
|
| 325 | + $storage->getUpdater()->remove($internalPath); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 330 | + if ($this->updaterEnabled) { |
|
| 331 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @param string $path |
|
| 337 | + * @return bool|mixed |
|
| 338 | + */ |
|
| 339 | + public function rmdir($path) { |
|
| 340 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 341 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 342 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 343 | + return $this->removeMount($mount, $absolutePath); |
|
| 344 | + } |
|
| 345 | + if ($this->is_dir($path)) { |
|
| 346 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 347 | + } else { |
|
| 348 | + $result = false; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 352 | + $storage = $mount->getStorage(); |
|
| 353 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 354 | + $storage->getUpdater()->remove($internalPath); |
|
| 355 | + } |
|
| 356 | + return $result; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @param string $path |
|
| 361 | + * @return resource |
|
| 362 | + */ |
|
| 363 | + public function opendir($path) { |
|
| 364 | + return $this->basicOperation('opendir', $path, array('read')); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * @param string $path |
|
| 369 | + * @return bool|mixed |
|
| 370 | + */ |
|
| 371 | + public function is_dir($path) { |
|
| 372 | + if ($path == '/') { |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + return $this->basicOperation('is_dir', $path); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * @param string $path |
|
| 380 | + * @return bool|mixed |
|
| 381 | + */ |
|
| 382 | + public function is_file($path) { |
|
| 383 | + if ($path == '/') { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + return $this->basicOperation('is_file', $path); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * @param string $path |
|
| 391 | + * @return mixed |
|
| 392 | + */ |
|
| 393 | + public function stat($path) { |
|
| 394 | + return $this->basicOperation('stat', $path); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * @param string $path |
|
| 399 | + * @return mixed |
|
| 400 | + */ |
|
| 401 | + public function filetype($path) { |
|
| 402 | + return $this->basicOperation('filetype', $path); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * @param string $path |
|
| 407 | + * @return mixed |
|
| 408 | + */ |
|
| 409 | + public function filesize($path) { |
|
| 410 | + return $this->basicOperation('filesize', $path); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * @param string $path |
|
| 415 | + * @return bool|mixed |
|
| 416 | + * @throws \OCP\Files\InvalidPathException |
|
| 417 | + */ |
|
| 418 | + public function readfile($path) { |
|
| 419 | + $this->assertPathLength($path); |
|
| 420 | + @ob_end_clean(); |
|
| 421 | + $handle = $this->fopen($path, 'rb'); |
|
| 422 | + if ($handle) { |
|
| 423 | + $chunkSize = 8192; // 8 kB chunks |
|
| 424 | + while (!feof($handle)) { |
|
| 425 | + echo fread($handle, $chunkSize); |
|
| 426 | + flush(); |
|
| 427 | + } |
|
| 428 | + fclose($handle); |
|
| 429 | + $size = $this->filesize($path); |
|
| 430 | + return $size; |
|
| 431 | + } |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @param string $path |
|
| 437 | + * @param int $from |
|
| 438 | + * @param int $to |
|
| 439 | + * @return bool|mixed |
|
| 440 | + * @throws \OCP\Files\InvalidPathException |
|
| 441 | + * @throws \OCP\Files\UnseekableException |
|
| 442 | + */ |
|
| 443 | + public function readfilePart($path, $from, $to) { |
|
| 444 | + $this->assertPathLength($path); |
|
| 445 | + @ob_end_clean(); |
|
| 446 | + $handle = $this->fopen($path, 'rb'); |
|
| 447 | + if ($handle) { |
|
| 448 | + $chunkSize = 8192; // 8 kB chunks |
|
| 449 | + $startReading = true; |
|
| 450 | + |
|
| 451 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 452 | + // forward file handle via chunked fread because fseek seem to have failed |
|
| 453 | + |
|
| 454 | + $end = $from + 1; |
|
| 455 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 456 | + $len = $from - ftell($handle); |
|
| 457 | + if ($len > $chunkSize) { |
|
| 458 | + $len = $chunkSize; |
|
| 459 | + } |
|
| 460 | + $result = fread($handle, $len); |
|
| 461 | + |
|
| 462 | + if ($result === false) { |
|
| 463 | + $startReading = false; |
|
| 464 | + break; |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if ($startReading) { |
|
| 470 | + $end = $to + 1; |
|
| 471 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 472 | + $len = $end - ftell($handle); |
|
| 473 | + if ($len > $chunkSize) { |
|
| 474 | + $len = $chunkSize; |
|
| 475 | + } |
|
| 476 | + echo fread($handle, $len); |
|
| 477 | + flush(); |
|
| 478 | + } |
|
| 479 | + $size = ftell($handle) - $from; |
|
| 480 | + return $size; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 484 | + } |
|
| 485 | + return false; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * @param string $path |
|
| 490 | + * @return mixed |
|
| 491 | + */ |
|
| 492 | + public function isCreatable($path) { |
|
| 493 | + return $this->basicOperation('isCreatable', $path); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * @param string $path |
|
| 498 | + * @return mixed |
|
| 499 | + */ |
|
| 500 | + public function isReadable($path) { |
|
| 501 | + return $this->basicOperation('isReadable', $path); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * @param string $path |
|
| 506 | + * @return mixed |
|
| 507 | + */ |
|
| 508 | + public function isUpdatable($path) { |
|
| 509 | + return $this->basicOperation('isUpdatable', $path); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * @param string $path |
|
| 514 | + * @return bool|mixed |
|
| 515 | + */ |
|
| 516 | + public function isDeletable($path) { |
|
| 517 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 518 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 519 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 520 | + return $mount instanceof MoveableMount; |
|
| 521 | + } |
|
| 522 | + return $this->basicOperation('isDeletable', $path); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * @param string $path |
|
| 527 | + * @return mixed |
|
| 528 | + */ |
|
| 529 | + public function isSharable($path) { |
|
| 530 | + return $this->basicOperation('isSharable', $path); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * @param string $path |
|
| 535 | + * @return bool|mixed |
|
| 536 | + */ |
|
| 537 | + public function file_exists($path) { |
|
| 538 | + if ($path == '/') { |
|
| 539 | + return true; |
|
| 540 | + } |
|
| 541 | + return $this->basicOperation('file_exists', $path); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * @param string $path |
|
| 546 | + * @return mixed |
|
| 547 | + */ |
|
| 548 | + public function filemtime($path) { |
|
| 549 | + return $this->basicOperation('filemtime', $path); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * @param string $path |
|
| 554 | + * @param int|string $mtime |
|
| 555 | + * @return bool |
|
| 556 | + */ |
|
| 557 | + public function touch($path, $mtime = null) { |
|
| 558 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 559 | + $mtime = strtotime($mtime); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + $hooks = array('touch'); |
|
| 563 | + |
|
| 564 | + if (!$this->file_exists($path)) { |
|
| 565 | + $hooks[] = 'create'; |
|
| 566 | + $hooks[] = 'write'; |
|
| 567 | + } |
|
| 568 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 569 | + if (!$result) { |
|
| 570 | + // If create file fails because of permissions on external storage like SMB folders, |
|
| 571 | + // check file exists and return false if not. |
|
| 572 | + if (!$this->file_exists($path)) { |
|
| 573 | + return false; |
|
| 574 | + } |
|
| 575 | + if (is_null($mtime)) { |
|
| 576 | + $mtime = time(); |
|
| 577 | + } |
|
| 578 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 579 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 580 | + } |
|
| 581 | + return true; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * @param string $path |
|
| 586 | + * @return mixed |
|
| 587 | + */ |
|
| 588 | + public function file_get_contents($path) { |
|
| 589 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * @param bool $exists |
|
| 594 | + * @param string $path |
|
| 595 | + * @param bool $run |
|
| 596 | + */ |
|
| 597 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 598 | + if (!$exists) { |
|
| 599 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 600 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 601 | + Filesystem::signal_param_run => &$run, |
|
| 602 | + )); |
|
| 603 | + } else { |
|
| 604 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 605 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 606 | + Filesystem::signal_param_run => &$run, |
|
| 607 | + )); |
|
| 608 | + } |
|
| 609 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 610 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 611 | + Filesystem::signal_param_run => &$run, |
|
| 612 | + )); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * @param bool $exists |
|
| 617 | + * @param string $path |
|
| 618 | + */ |
|
| 619 | + protected function emit_file_hooks_post($exists, $path) { |
|
| 620 | + if (!$exists) { |
|
| 621 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 622 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 623 | + )); |
|
| 624 | + } else { |
|
| 625 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 626 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 627 | + )); |
|
| 628 | + } |
|
| 629 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 630 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 631 | + )); |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * @param string $path |
|
| 636 | + * @param mixed $data |
|
| 637 | + * @return bool|mixed |
|
| 638 | + * @throws \Exception |
|
| 639 | + */ |
|
| 640 | + public function file_put_contents($path, $data) { |
|
| 641 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 642 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 643 | + if (Filesystem::isValidPath($path) |
|
| 644 | + and !Filesystem::isFileBlacklisted($path) |
|
| 645 | + ) { |
|
| 646 | + $path = $this->getRelativePath($absolutePath); |
|
| 647 | + |
|
| 648 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 649 | + |
|
| 650 | + $exists = $this->file_exists($path); |
|
| 651 | + $run = true; |
|
| 652 | + if ($this->shouldEmitHooks($path)) { |
|
| 653 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 654 | + } |
|
| 655 | + if (!$run) { |
|
| 656 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 657 | + return false; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 661 | + |
|
| 662 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 663 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 664 | + $target = $storage->fopen($internalPath, 'w'); |
|
| 665 | + if ($target) { |
|
| 666 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 667 | + fclose($target); |
|
| 668 | + fclose($data); |
|
| 669 | + |
|
| 670 | + $this->writeUpdate($storage, $internalPath); |
|
| 671 | + |
|
| 672 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 673 | + |
|
| 674 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 675 | + $this->emit_file_hooks_post($exists, $path); |
|
| 676 | + } |
|
| 677 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 678 | + return $result; |
|
| 679 | + } else { |
|
| 680 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 681 | + return false; |
|
| 682 | + } |
|
| 683 | + } else { |
|
| 684 | + return false; |
|
| 685 | + } |
|
| 686 | + } else { |
|
| 687 | + $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); |
|
| 688 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 689 | + } |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * @param string $path |
|
| 694 | + * @return bool|mixed |
|
| 695 | + */ |
|
| 696 | + public function unlink($path) { |
|
| 697 | + if ($path === '' || $path === '/') { |
|
| 698 | + // do not allow deleting the root |
|
| 699 | + return false; |
|
| 700 | + } |
|
| 701 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 702 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 703 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 704 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 705 | + return $this->removeMount($mount, $absolutePath); |
|
| 706 | + } |
|
| 707 | + if ($this->is_dir($path)) { |
|
| 708 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 709 | + } else { |
|
| 710 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 711 | + } |
|
| 712 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 713 | + $storage = $mount->getStorage(); |
|
| 714 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 715 | + $storage->getUpdater()->remove($internalPath); |
|
| 716 | + return true; |
|
| 717 | + } else { |
|
| 718 | + return $result; |
|
| 719 | + } |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * @param string $directory |
|
| 724 | + * @return bool|mixed |
|
| 725 | + */ |
|
| 726 | + public function deleteAll($directory) { |
|
| 727 | + return $this->rmdir($directory); |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Rename/move a file or folder from the source path to target path. |
|
| 732 | + * |
|
| 733 | + * @param string $path1 source path |
|
| 734 | + * @param string $path2 target path |
|
| 735 | + * |
|
| 736 | + * @return bool|mixed |
|
| 737 | + */ |
|
| 738 | + public function rename($path1, $path2) { |
|
| 739 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 740 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 741 | + $result = false; |
|
| 742 | + if ( |
|
| 743 | + Filesystem::isValidPath($path2) |
|
| 744 | + and Filesystem::isValidPath($path1) |
|
| 745 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 746 | + ) { |
|
| 747 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 748 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 749 | + $exists = $this->file_exists($path2); |
|
| 750 | + |
|
| 751 | + if ($path1 == null or $path2 == null) { |
|
| 752 | + return false; |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 756 | + try { |
|
| 757 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 758 | + |
|
| 759 | + $run = true; |
|
| 760 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 761 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 762 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 763 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
| 764 | + \OC_Hook::emit( |
|
| 765 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 766 | + array( |
|
| 767 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 768 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 769 | + Filesystem::signal_param_run => &$run |
|
| 770 | + ) |
|
| 771 | + ); |
|
| 772 | + } |
|
| 773 | + if ($run) { |
|
| 774 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
| 775 | + |
|
| 776 | + $manager = Filesystem::getMountManager(); |
|
| 777 | + $mount1 = $this->getMount($path1); |
|
| 778 | + $mount2 = $this->getMount($path2); |
|
| 779 | + $storage1 = $mount1->getStorage(); |
|
| 780 | + $storage2 = $mount2->getStorage(); |
|
| 781 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 782 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 783 | + |
|
| 784 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 785 | + try { |
|
| 786 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 787 | + |
|
| 788 | + if ($internalPath1 === '') { |
|
| 789 | + if ($mount1 instanceof MoveableMount) { |
|
| 790 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
| 791 | + /** |
|
| 792 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 793 | + */ |
|
| 794 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
| 795 | + $result = $mount1->moveMount($absolutePath2); |
|
| 796 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 797 | + } else { |
|
| 798 | + $result = false; |
|
| 799 | + } |
|
| 800 | + } else { |
|
| 801 | + $result = false; |
|
| 802 | + } |
|
| 803 | + // moving a file/folder within the same mount point |
|
| 804 | + } elseif ($storage1 === $storage2) { |
|
| 805 | + if ($storage1) { |
|
| 806 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 807 | + } else { |
|
| 808 | + $result = false; |
|
| 809 | + } |
|
| 810 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 811 | + } else { |
|
| 812 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 816 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 817 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 818 | + } else if ($result) { |
|
| 819 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 820 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 821 | + } |
|
| 822 | + } |
|
| 823 | + } catch(\Exception $e) { |
|
| 824 | + throw $e; |
|
| 825 | + } finally { |
|
| 826 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 827 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 831 | + if ($this->shouldEmitHooks()) { |
|
| 832 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 833 | + } |
|
| 834 | + } elseif ($result) { |
|
| 835 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 836 | + \OC_Hook::emit( |
|
| 837 | + Filesystem::CLASSNAME, |
|
| 838 | + Filesystem::signal_post_rename, |
|
| 839 | + array( |
|
| 840 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 841 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 842 | + ) |
|
| 843 | + ); |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + } catch(\Exception $e) { |
|
| 848 | + throw $e; |
|
| 849 | + } finally { |
|
| 850 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 851 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 852 | + } |
|
| 853 | + } |
|
| 854 | + return $result; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * Copy a file/folder from the source path to target path |
|
| 859 | + * |
|
| 860 | + * @param string $path1 source path |
|
| 861 | + * @param string $path2 target path |
|
| 862 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 863 | + * |
|
| 864 | + * @return bool|mixed |
|
| 865 | + */ |
|
| 866 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
| 867 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 868 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 869 | + $result = false; |
|
| 870 | + if ( |
|
| 871 | + Filesystem::isValidPath($path2) |
|
| 872 | + and Filesystem::isValidPath($path1) |
|
| 873 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 874 | + ) { |
|
| 875 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 876 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 877 | + |
|
| 878 | + if ($path1 == null or $path2 == null) { |
|
| 879 | + return false; |
|
| 880 | + } |
|
| 881 | + $run = true; |
|
| 882 | + |
|
| 883 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 884 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 885 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 886 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 887 | + |
|
| 888 | + try { |
|
| 889 | + |
|
| 890 | + $exists = $this->file_exists($path2); |
|
| 891 | + if ($this->shouldEmitHooks()) { |
|
| 892 | + \OC_Hook::emit( |
|
| 893 | + Filesystem::CLASSNAME, |
|
| 894 | + Filesystem::signal_copy, |
|
| 895 | + array( |
|
| 896 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 897 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 898 | + Filesystem::signal_param_run => &$run |
|
| 899 | + ) |
|
| 900 | + ); |
|
| 901 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 902 | + } |
|
| 903 | + if ($run) { |
|
| 904 | + $mount1 = $this->getMount($path1); |
|
| 905 | + $mount2 = $this->getMount($path2); |
|
| 906 | + $storage1 = $mount1->getStorage(); |
|
| 907 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 908 | + $storage2 = $mount2->getStorage(); |
|
| 909 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 910 | + |
|
| 911 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 912 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 913 | + |
|
| 914 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 915 | + if ($storage1) { |
|
| 916 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 917 | + } else { |
|
| 918 | + $result = false; |
|
| 919 | + } |
|
| 920 | + } else { |
|
| 921 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 925 | + |
|
| 926 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 927 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 928 | + |
|
| 929 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
| 930 | + \OC_Hook::emit( |
|
| 931 | + Filesystem::CLASSNAME, |
|
| 932 | + Filesystem::signal_post_copy, |
|
| 933 | + array( |
|
| 934 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 935 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 936 | + ) |
|
| 937 | + ); |
|
| 938 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + } |
|
| 942 | + } catch (\Exception $e) { |
|
| 943 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 944 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 945 | + throw $e; |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 949 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 950 | + |
|
| 951 | + } |
|
| 952 | + return $result; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * @param string $path |
|
| 957 | + * @param string $mode 'r' or 'w' |
|
| 958 | + * @return resource |
|
| 959 | + */ |
|
| 960 | + public function fopen($path, $mode) { |
|
| 961 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 962 | + $hooks = array(); |
|
| 963 | + switch ($mode) { |
|
| 964 | + case 'r': |
|
| 965 | + $hooks[] = 'read'; |
|
| 966 | + break; |
|
| 967 | + case 'r+': |
|
| 968 | + case 'w+': |
|
| 969 | + case 'x+': |
|
| 970 | + case 'a+': |
|
| 971 | + $hooks[] = 'read'; |
|
| 972 | + $hooks[] = 'write'; |
|
| 973 | + break; |
|
| 974 | + case 'w': |
|
| 975 | + case 'x': |
|
| 976 | + case 'a': |
|
| 977 | + $hooks[] = 'write'; |
|
| 978 | + break; |
|
| 979 | + default: |
|
| 980 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + if ($mode !== 'r' && $mode !== 'w') { |
|
| 984 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * @param string $path |
|
| 992 | + * @return bool|string |
|
| 993 | + * @throws \OCP\Files\InvalidPathException |
|
| 994 | + */ |
|
| 995 | + public function toTmpFile($path) { |
|
| 996 | + $this->assertPathLength($path); |
|
| 997 | + if (Filesystem::isValidPath($path)) { |
|
| 998 | + $source = $this->fopen($path, 'r'); |
|
| 999 | + if ($source) { |
|
| 1000 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1001 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1002 | + file_put_contents($tmpFile, $source); |
|
| 1003 | + return $tmpFile; |
|
| 1004 | + } else { |
|
| 1005 | + return false; |
|
| 1006 | + } |
|
| 1007 | + } else { |
|
| 1008 | + return false; |
|
| 1009 | + } |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + /** |
|
| 1013 | + * @param string $tmpFile |
|
| 1014 | + * @param string $path |
|
| 1015 | + * @return bool|mixed |
|
| 1016 | + * @throws \OCP\Files\InvalidPathException |
|
| 1017 | + */ |
|
| 1018 | + public function fromTmpFile($tmpFile, $path) { |
|
| 1019 | + $this->assertPathLength($path); |
|
| 1020 | + if (Filesystem::isValidPath($path)) { |
|
| 1021 | + |
|
| 1022 | + // Get directory that the file is going into |
|
| 1023 | + $filePath = dirname($path); |
|
| 1024 | + |
|
| 1025 | + // Create the directories if any |
|
| 1026 | + if (!$this->file_exists($filePath)) { |
|
| 1027 | + $result = $this->createParentDirectories($filePath); |
|
| 1028 | + if ($result === false) { |
|
| 1029 | + return false; |
|
| 1030 | + } |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + $source = fopen($tmpFile, 'r'); |
|
| 1034 | + if ($source) { |
|
| 1035 | + $result = $this->file_put_contents($path, $source); |
|
| 1036 | + // $this->file_put_contents() might have already closed |
|
| 1037 | + // the resource, so we check it, before trying to close it |
|
| 1038 | + // to avoid messages in the error log. |
|
| 1039 | + if (is_resource($source)) { |
|
| 1040 | + fclose($source); |
|
| 1041 | + } |
|
| 1042 | + unlink($tmpFile); |
|
| 1043 | + return $result; |
|
| 1044 | + } else { |
|
| 1045 | + return false; |
|
| 1046 | + } |
|
| 1047 | + } else { |
|
| 1048 | + return false; |
|
| 1049 | + } |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + |
|
| 1053 | + /** |
|
| 1054 | + * @param string $path |
|
| 1055 | + * @return mixed |
|
| 1056 | + * @throws \OCP\Files\InvalidPathException |
|
| 1057 | + */ |
|
| 1058 | + public function getMimeType($path) { |
|
| 1059 | + $this->assertPathLength($path); |
|
| 1060 | + return $this->basicOperation('getMimeType', $path); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + /** |
|
| 1064 | + * @param string $type |
|
| 1065 | + * @param string $path |
|
| 1066 | + * @param bool $raw |
|
| 1067 | + * @return bool|null|string |
|
| 1068 | + */ |
|
| 1069 | + public function hash($type, $path, $raw = false) { |
|
| 1070 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 1071 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1072 | + if (Filesystem::isValidPath($path)) { |
|
| 1073 | + $path = $this->getRelativePath($absolutePath); |
|
| 1074 | + if ($path == null) { |
|
| 1075 | + return false; |
|
| 1076 | + } |
|
| 1077 | + if ($this->shouldEmitHooks($path)) { |
|
| 1078 | + \OC_Hook::emit( |
|
| 1079 | + Filesystem::CLASSNAME, |
|
| 1080 | + Filesystem::signal_read, |
|
| 1081 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1082 | + ); |
|
| 1083 | + } |
|
| 1084 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1085 | + if ($storage) { |
|
| 1086 | + $result = $storage->hash($type, $internalPath, $raw); |
|
| 1087 | + return $result; |
|
| 1088 | + } |
|
| 1089 | + } |
|
| 1090 | + return null; |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + /** |
|
| 1094 | + * @param string $path |
|
| 1095 | + * @return mixed |
|
| 1096 | + * @throws \OCP\Files\InvalidPathException |
|
| 1097 | + */ |
|
| 1098 | + public function free_space($path = '/') { |
|
| 1099 | + $this->assertPathLength($path); |
|
| 1100 | + $result = $this->basicOperation('free_space', $path); |
|
| 1101 | + if ($result === null) { |
|
| 1102 | + throw new InvalidPathException(); |
|
| 1103 | + } |
|
| 1104 | + return $result; |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + /** |
|
| 1108 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1109 | + * |
|
| 1110 | + * @param string $operation |
|
| 1111 | + * @param string $path |
|
| 1112 | + * @param array $hooks (optional) |
|
| 1113 | + * @param mixed $extraParam (optional) |
|
| 1114 | + * @return mixed |
|
| 1115 | + * @throws \Exception |
|
| 1116 | + * |
|
| 1117 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1118 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1119 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1120 | + */ |
|
| 1121 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1122 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
|
| 1123 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1124 | + if (Filesystem::isValidPath($path) |
|
| 1125 | + and !Filesystem::isFileBlacklisted($path) |
|
| 1126 | + ) { |
|
| 1127 | + $path = $this->getRelativePath($absolutePath); |
|
| 1128 | + if ($path == null) { |
|
| 1129 | + return false; |
|
| 1130 | + } |
|
| 1131 | + |
|
| 1132 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1133 | + // always a shared lock during pre-hooks so the hook can read the file |
|
| 1134 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + $run = $this->runHooks($hooks, $path); |
|
| 1138 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1139 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1140 | + if ($run and $storage) { |
|
| 1141 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1142 | + try { |
|
| 1143 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1144 | + } catch (LockedException $e) { |
|
| 1145 | + // release the shared lock we acquired before quiting |
|
| 1146 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1147 | + throw $e; |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + try { |
|
| 1151 | + if (!is_null($extraParam)) { |
|
| 1152 | + $result = $storage->$operation($internalPath, $extraParam); |
|
| 1153 | + } else { |
|
| 1154 | + $result = $storage->$operation($internalPath); |
|
| 1155 | + } |
|
| 1156 | + } catch (\Exception $e) { |
|
| 1157 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1158 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1159 | + } else if (in_array('read', $hooks)) { |
|
| 1160 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1161 | + } |
|
| 1162 | + throw $e; |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + if ($result && in_array('delete', $hooks) and $result) { |
|
| 1166 | + $this->removeUpdate($storage, $internalPath); |
|
| 1167 | + } |
|
| 1168 | + if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1169 | + $this->writeUpdate($storage, $internalPath); |
|
| 1170 | + } |
|
| 1171 | + if ($result && in_array('touch', $hooks)) { |
|
| 1172 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1176 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + $unlockLater = false; |
|
| 1180 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1181 | + $unlockLater = true; |
|
| 1182 | + // make sure our unlocking callback will still be called if connection is aborted |
|
| 1183 | + ignore_user_abort(true); |
|
| 1184 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1185 | + if (in_array('write', $hooks)) { |
|
| 1186 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1187 | + } else if (in_array('read', $hooks)) { |
|
| 1188 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1189 | + } |
|
| 1190 | + }); |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1194 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1195 | + $this->runHooks($hooks, $path, true); |
|
| 1196 | + } |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + if (!$unlockLater |
|
| 1200 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1201 | + ) { |
|
| 1202 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1203 | + } |
|
| 1204 | + return $result; |
|
| 1205 | + } else { |
|
| 1206 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1207 | + } |
|
| 1208 | + } |
|
| 1209 | + return null; |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + /** |
|
| 1213 | + * get the path relative to the default root for hook usage |
|
| 1214 | + * |
|
| 1215 | + * @param string $path |
|
| 1216 | + * @return string |
|
| 1217 | + */ |
|
| 1218 | + private function getHookPath($path) { |
|
| 1219 | + if (!Filesystem::getView()) { |
|
| 1220 | + return $path; |
|
| 1221 | + } |
|
| 1222 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + private function shouldEmitHooks($path = '') { |
|
| 1226 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1227 | + return false; |
|
| 1228 | + } |
|
| 1229 | + if (!Filesystem::$loaded) { |
|
| 1230 | + return false; |
|
| 1231 | + } |
|
| 1232 | + $defaultRoot = Filesystem::getRoot(); |
|
| 1233 | + if ($defaultRoot === null) { |
|
| 1234 | + return false; |
|
| 1235 | + } |
|
| 1236 | + if ($this->fakeRoot === $defaultRoot) { |
|
| 1237 | + return true; |
|
| 1238 | + } |
|
| 1239 | + $fullPath = $this->getAbsolutePath($path); |
|
| 1240 | + |
|
| 1241 | + if ($fullPath === $defaultRoot) { |
|
| 1242 | + return true; |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + /** |
|
| 1249 | + * @param string[] $hooks |
|
| 1250 | + * @param string $path |
|
| 1251 | + * @param bool $post |
|
| 1252 | + * @return bool |
|
| 1253 | + */ |
|
| 1254 | + private function runHooks($hooks, $path, $post = false) { |
|
| 1255 | + $relativePath = $path; |
|
| 1256 | + $path = $this->getHookPath($path); |
|
| 1257 | + $prefix = ($post) ? 'post_' : ''; |
|
| 1258 | + $run = true; |
|
| 1259 | + if ($this->shouldEmitHooks($relativePath)) { |
|
| 1260 | + foreach ($hooks as $hook) { |
|
| 1261 | + if ($hook != 'read') { |
|
| 1262 | + \OC_Hook::emit( |
|
| 1263 | + Filesystem::CLASSNAME, |
|
| 1264 | + $prefix . $hook, |
|
| 1265 | + array( |
|
| 1266 | + Filesystem::signal_param_run => &$run, |
|
| 1267 | + Filesystem::signal_param_path => $path |
|
| 1268 | + ) |
|
| 1269 | + ); |
|
| 1270 | + } elseif (!$post) { |
|
| 1271 | + \OC_Hook::emit( |
|
| 1272 | + Filesystem::CLASSNAME, |
|
| 1273 | + $prefix . $hook, |
|
| 1274 | + array( |
|
| 1275 | + Filesystem::signal_param_path => $path |
|
| 1276 | + ) |
|
| 1277 | + ); |
|
| 1278 | + } |
|
| 1279 | + } |
|
| 1280 | + } |
|
| 1281 | + return $run; |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + /** |
|
| 1285 | + * check if a file or folder has been updated since $time |
|
| 1286 | + * |
|
| 1287 | + * @param string $path |
|
| 1288 | + * @param int $time |
|
| 1289 | + * @return bool |
|
| 1290 | + */ |
|
| 1291 | + public function hasUpdated($path, $time) { |
|
| 1292 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + /** |
|
| 1296 | + * @param string $ownerId |
|
| 1297 | + * @return \OC\User\User |
|
| 1298 | + */ |
|
| 1299 | + private function getUserObjectForOwner($ownerId) { |
|
| 1300 | + $owner = $this->userManager->get($ownerId); |
|
| 1301 | + if ($owner instanceof IUser) { |
|
| 1302 | + return $owner; |
|
| 1303 | + } else { |
|
| 1304 | + return new User($ownerId, null); |
|
| 1305 | + } |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + /** |
|
| 1309 | + * Get file info from cache |
|
| 1310 | + * |
|
| 1311 | + * If the file is not in cached it will be scanned |
|
| 1312 | + * If the file has changed on storage the cache will be updated |
|
| 1313 | + * |
|
| 1314 | + * @param \OC\Files\Storage\Storage $storage |
|
| 1315 | + * @param string $internalPath |
|
| 1316 | + * @param string $relativePath |
|
| 1317 | + * @return ICacheEntry|bool |
|
| 1318 | + */ |
|
| 1319 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1320 | + $cache = $storage->getCache($internalPath); |
|
| 1321 | + $data = $cache->get($internalPath); |
|
| 1322 | + $watcher = $storage->getWatcher($internalPath); |
|
| 1323 | + |
|
| 1324 | + try { |
|
| 1325 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1326 | + if (!$data || $data['size'] === -1) { |
|
| 1327 | + if (!$storage->file_exists($internalPath)) { |
|
| 1328 | + return false; |
|
| 1329 | + } |
|
| 1330 | + // don't need to get a lock here since the scanner does it's own locking |
|
| 1331 | + $scanner = $storage->getScanner($internalPath); |
|
| 1332 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1333 | + $data = $cache->get($internalPath); |
|
| 1334 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1335 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1336 | + $watcher->update($internalPath, $data); |
|
| 1337 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1338 | + $data = $cache->get($internalPath); |
|
| 1339 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1340 | + } |
|
| 1341 | + } catch (LockedException $e) { |
|
| 1342 | + // if the file is locked we just use the old cache info |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + return $data; |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + /** |
|
| 1349 | + * get the filesystem info |
|
| 1350 | + * |
|
| 1351 | + * @param string $path |
|
| 1352 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1353 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1354 | + * defaults to true |
|
| 1355 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1356 | + */ |
|
| 1357 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1358 | + $this->assertPathLength($path); |
|
| 1359 | + if (!Filesystem::isValidPath($path)) { |
|
| 1360 | + return false; |
|
| 1361 | + } |
|
| 1362 | + if (Cache\Scanner::isPartialFile($path)) { |
|
| 1363 | + return $this->getPartFileInfo($path); |
|
| 1364 | + } |
|
| 1365 | + $relativePath = $path; |
|
| 1366 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1367 | + |
|
| 1368 | + $mount = Filesystem::getMountManager()->find($path); |
|
| 1369 | + if (!$mount) { |
|
| 1370 | + return false; |
|
| 1371 | + } |
|
| 1372 | + $storage = $mount->getStorage(); |
|
| 1373 | + $internalPath = $mount->getInternalPath($path); |
|
| 1374 | + if ($storage) { |
|
| 1375 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1376 | + |
|
| 1377 | + if (!$data instanceof ICacheEntry) { |
|
| 1378 | + return false; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1382 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1383 | + } |
|
| 1384 | + |
|
| 1385 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1386 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1387 | + |
|
| 1388 | + if ($data and isset($data['fileid'])) { |
|
| 1389 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1390 | + //add the sizes of other mount points to the folder |
|
| 1391 | + $extOnly = ($includeMountPoints === 'ext'); |
|
| 1392 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1393 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1394 | + $subStorage = $mount->getStorage(); |
|
| 1395 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1396 | + })); |
|
| 1397 | + } |
|
| 1398 | + } |
|
| 1399 | + |
|
| 1400 | + return $info; |
|
| 1401 | + } |
|
| 1402 | + |
|
| 1403 | + return false; |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + /** |
|
| 1407 | + * get the content of a directory |
|
| 1408 | + * |
|
| 1409 | + * @param string $directory path under datadirectory |
|
| 1410 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1411 | + * @return FileInfo[] |
|
| 1412 | + */ |
|
| 1413 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1414 | + $this->assertPathLength($directory); |
|
| 1415 | + if (!Filesystem::isValidPath($directory)) { |
|
| 1416 | + return []; |
|
| 1417 | + } |
|
| 1418 | + $path = $this->getAbsolutePath($directory); |
|
| 1419 | + $path = Filesystem::normalizePath($path); |
|
| 1420 | + $mount = $this->getMount($directory); |
|
| 1421 | + if (!$mount) { |
|
| 1422 | + return []; |
|
| 1423 | + } |
|
| 1424 | + $storage = $mount->getStorage(); |
|
| 1425 | + $internalPath = $mount->getInternalPath($path); |
|
| 1426 | + if ($storage) { |
|
| 1427 | + $cache = $storage->getCache($internalPath); |
|
| 1428 | + $user = \OC_User::getUser(); |
|
| 1429 | + |
|
| 1430 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1431 | + |
|
| 1432 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1433 | + return []; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + $folderId = $data['fileid']; |
|
| 1437 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1438 | + |
|
| 1439 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1440 | + |
|
| 1441 | + $fileNames = array_map(function(ICacheEntry $content) { |
|
| 1442 | + return $content->getName(); |
|
| 1443 | + }, $contents); |
|
| 1444 | + /** |
|
| 1445 | + * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1446 | + */ |
|
| 1447 | + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1448 | + if ($sharingDisabled) { |
|
| 1449 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1450 | + } |
|
| 1451 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1452 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1453 | + }, $contents); |
|
| 1454 | + $files = array_combine($fileNames, $fileInfos); |
|
| 1455 | + |
|
| 1456 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1457 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1458 | + $dirLength = strlen($path); |
|
| 1459 | + foreach ($mounts as $mount) { |
|
| 1460 | + $mountPoint = $mount->getMountPoint(); |
|
| 1461 | + $subStorage = $mount->getStorage(); |
|
| 1462 | + if ($subStorage) { |
|
| 1463 | + $subCache = $subStorage->getCache(''); |
|
| 1464 | + |
|
| 1465 | + $rootEntry = $subCache->get(''); |
|
| 1466 | + if (!$rootEntry) { |
|
| 1467 | + $subScanner = $subStorage->getScanner(''); |
|
| 1468 | + try { |
|
| 1469 | + $subScanner->scanFile(''); |
|
| 1470 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1471 | + continue; |
|
| 1472 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1473 | + continue; |
|
| 1474 | + } catch (\Exception $e) { |
|
| 1475 | + // sometimes when the storage is not available it can be any exception |
|
| 1476 | + \OCP\Util::writeLog( |
|
| 1477 | + 'core', |
|
| 1478 | + 'Exception while scanning storage "' . $subStorage->getId() . '": ' . |
|
| 1479 | + get_class($e) . ': ' . $e->getMessage(), |
|
| 1480 | + \OCP\Util::ERROR |
|
| 1481 | + ); |
|
| 1482 | + continue; |
|
| 1483 | + } |
|
| 1484 | + $rootEntry = $subCache->get(''); |
|
| 1485 | + } |
|
| 1486 | + |
|
| 1487 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1488 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1489 | + if ($pos = strpos($relativePath, '/')) { |
|
| 1490 | + //mountpoint inside subfolder add size to the correct folder |
|
| 1491 | + $entryName = substr($relativePath, 0, $pos); |
|
| 1492 | + foreach ($files as &$entry) { |
|
| 1493 | + if ($entry->getName() === $entryName) { |
|
| 1494 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1495 | + } |
|
| 1496 | + } |
|
| 1497 | + } else { //mountpoint in this folder, add an entry for it |
|
| 1498 | + $rootEntry['name'] = $relativePath; |
|
| 1499 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1500 | + $permissions = $rootEntry['permissions']; |
|
| 1501 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1502 | + // for shared files/folders we use the permissions given by the owner |
|
| 1503 | + if ($mount instanceof MoveableMount) { |
|
| 1504 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1505 | + } else { |
|
| 1506 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1507 | + } |
|
| 1508 | + |
|
| 1509 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1510 | + |
|
| 1511 | + // if sharing was disabled for the user we remove the share permissions |
|
| 1512 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1513 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1514 | + } |
|
| 1515 | + |
|
| 1516 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1517 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1518 | + } |
|
| 1519 | + } |
|
| 1520 | + } |
|
| 1521 | + } |
|
| 1522 | + |
|
| 1523 | + if ($mimetype_filter) { |
|
| 1524 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1525 | + if (strpos($mimetype_filter, '/')) { |
|
| 1526 | + return $file->getMimetype() === $mimetype_filter; |
|
| 1527 | + } else { |
|
| 1528 | + return $file->getMimePart() === $mimetype_filter; |
|
| 1529 | + } |
|
| 1530 | + }); |
|
| 1531 | + } |
|
| 1532 | + |
|
| 1533 | + return array_values($files); |
|
| 1534 | + } else { |
|
| 1535 | + return []; |
|
| 1536 | + } |
|
| 1537 | + } |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * change file metadata |
|
| 1541 | + * |
|
| 1542 | + * @param string $path |
|
| 1543 | + * @param array|\OCP\Files\FileInfo $data |
|
| 1544 | + * @return int |
|
| 1545 | + * |
|
| 1546 | + * returns the fileid of the updated file |
|
| 1547 | + */ |
|
| 1548 | + public function putFileInfo($path, $data) { |
|
| 1549 | + $this->assertPathLength($path); |
|
| 1550 | + if ($data instanceof FileInfo) { |
|
| 1551 | + $data = $data->getData(); |
|
| 1552 | + } |
|
| 1553 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1554 | + /** |
|
| 1555 | + * @var \OC\Files\Storage\Storage $storage |
|
| 1556 | + * @var string $internalPath |
|
| 1557 | + */ |
|
| 1558 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1559 | + if ($storage) { |
|
| 1560 | + $cache = $storage->getCache($path); |
|
| 1561 | + |
|
| 1562 | + if (!$cache->inCache($internalPath)) { |
|
| 1563 | + $scanner = $storage->getScanner($internalPath); |
|
| 1564 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1565 | + } |
|
| 1566 | + |
|
| 1567 | + return $cache->put($internalPath, $data); |
|
| 1568 | + } else { |
|
| 1569 | + return -1; |
|
| 1570 | + } |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + /** |
|
| 1574 | + * search for files with the name matching $query |
|
| 1575 | + * |
|
| 1576 | + * @param string $query |
|
| 1577 | + * @return FileInfo[] |
|
| 1578 | + */ |
|
| 1579 | + public function search($query) { |
|
| 1580 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1581 | + } |
|
| 1582 | + |
|
| 1583 | + /** |
|
| 1584 | + * search for files with the name matching $query |
|
| 1585 | + * |
|
| 1586 | + * @param string $query |
|
| 1587 | + * @return FileInfo[] |
|
| 1588 | + */ |
|
| 1589 | + public function searchRaw($query) { |
|
| 1590 | + return $this->searchCommon('search', array($query)); |
|
| 1591 | + } |
|
| 1592 | + |
|
| 1593 | + /** |
|
| 1594 | + * search for files by mimetype |
|
| 1595 | + * |
|
| 1596 | + * @param string $mimetype |
|
| 1597 | + * @return FileInfo[] |
|
| 1598 | + */ |
|
| 1599 | + public function searchByMime($mimetype) { |
|
| 1600 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1601 | + } |
|
| 1602 | + |
|
| 1603 | + /** |
|
| 1604 | + * search for files by tag |
|
| 1605 | + * |
|
| 1606 | + * @param string|int $tag name or tag id |
|
| 1607 | + * @param string $userId owner of the tags |
|
| 1608 | + * @return FileInfo[] |
|
| 1609 | + */ |
|
| 1610 | + public function searchByTag($tag, $userId) { |
|
| 1611 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1612 | + } |
|
| 1613 | + |
|
| 1614 | + /** |
|
| 1615 | + * @param string $method cache method |
|
| 1616 | + * @param array $args |
|
| 1617 | + * @return FileInfo[] |
|
| 1618 | + */ |
|
| 1619 | + private function searchCommon($method, $args) { |
|
| 1620 | + $files = array(); |
|
| 1621 | + $rootLength = strlen($this->fakeRoot); |
|
| 1622 | + |
|
| 1623 | + $mount = $this->getMount(''); |
|
| 1624 | + $mountPoint = $mount->getMountPoint(); |
|
| 1625 | + $storage = $mount->getStorage(); |
|
| 1626 | + if ($storage) { |
|
| 1627 | + $cache = $storage->getCache(''); |
|
| 1628 | + |
|
| 1629 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1630 | + foreach ($results as $result) { |
|
| 1631 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1632 | + $internalPath = $result['path']; |
|
| 1633 | + $path = $mountPoint . $result['path']; |
|
| 1634 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1635 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1636 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1637 | + } |
|
| 1638 | + } |
|
| 1639 | + |
|
| 1640 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1641 | + foreach ($mounts as $mount) { |
|
| 1642 | + $mountPoint = $mount->getMountPoint(); |
|
| 1643 | + $storage = $mount->getStorage(); |
|
| 1644 | + if ($storage) { |
|
| 1645 | + $cache = $storage->getCache(''); |
|
| 1646 | + |
|
| 1647 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1648 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1649 | + if ($results) { |
|
| 1650 | + foreach ($results as $result) { |
|
| 1651 | + $internalPath = $result['path']; |
|
| 1652 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1653 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1654 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1655 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1656 | + } |
|
| 1657 | + } |
|
| 1658 | + } |
|
| 1659 | + } |
|
| 1660 | + } |
|
| 1661 | + return $files; |
|
| 1662 | + } |
|
| 1663 | + |
|
| 1664 | + /** |
|
| 1665 | + * Get the owner for a file or folder |
|
| 1666 | + * |
|
| 1667 | + * @param string $path |
|
| 1668 | + * @return string the user id of the owner |
|
| 1669 | + * @throws NotFoundException |
|
| 1670 | + */ |
|
| 1671 | + public function getOwner($path) { |
|
| 1672 | + $info = $this->getFileInfo($path); |
|
| 1673 | + if (!$info) { |
|
| 1674 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1675 | + } |
|
| 1676 | + return $info->getOwner()->getUID(); |
|
| 1677 | + } |
|
| 1678 | + |
|
| 1679 | + /** |
|
| 1680 | + * get the ETag for a file or folder |
|
| 1681 | + * |
|
| 1682 | + * @param string $path |
|
| 1683 | + * @return string |
|
| 1684 | + */ |
|
| 1685 | + public function getETag($path) { |
|
| 1686 | + /** |
|
| 1687 | + * @var Storage\Storage $storage |
|
| 1688 | + * @var string $internalPath |
|
| 1689 | + */ |
|
| 1690 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1691 | + if ($storage) { |
|
| 1692 | + return $storage->getETag($internalPath); |
|
| 1693 | + } else { |
|
| 1694 | + return null; |
|
| 1695 | + } |
|
| 1696 | + } |
|
| 1697 | + |
|
| 1698 | + /** |
|
| 1699 | + * Get the path of a file by id, relative to the view |
|
| 1700 | + * |
|
| 1701 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1702 | + * |
|
| 1703 | + * @param int $id |
|
| 1704 | + * @throws NotFoundException |
|
| 1705 | + * @return string |
|
| 1706 | + */ |
|
| 1707 | + public function getPath($id) { |
|
| 1708 | + $id = (int)$id; |
|
| 1709 | + $manager = Filesystem::getMountManager(); |
|
| 1710 | + $mounts = $manager->findIn($this->fakeRoot); |
|
| 1711 | + $mounts[] = $manager->find($this->fakeRoot); |
|
| 1712 | + // reverse the array so we start with the storage this view is in |
|
| 1713 | + // which is the most likely to contain the file we're looking for |
|
| 1714 | + $mounts = array_reverse($mounts); |
|
| 1715 | + foreach ($mounts as $mount) { |
|
| 1716 | + /** |
|
| 1717 | + * @var \OC\Files\Mount\MountPoint $mount |
|
| 1718 | + */ |
|
| 1719 | + if ($mount->getStorage()) { |
|
| 1720 | + $cache = $mount->getStorage()->getCache(); |
|
| 1721 | + $internalPath = $cache->getPathById($id); |
|
| 1722 | + if (is_string($internalPath)) { |
|
| 1723 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1724 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1725 | + return $path; |
|
| 1726 | + } |
|
| 1727 | + } |
|
| 1728 | + } |
|
| 1729 | + } |
|
| 1730 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1731 | + } |
|
| 1732 | + |
|
| 1733 | + /** |
|
| 1734 | + * @param string $path |
|
| 1735 | + * @throws InvalidPathException |
|
| 1736 | + */ |
|
| 1737 | + private function assertPathLength($path) { |
|
| 1738 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1739 | + // Check for the string length - performed using isset() instead of strlen() |
|
| 1740 | + // because isset() is about 5x-40x faster. |
|
| 1741 | + if (isset($path[$maxLen])) { |
|
| 1742 | + $pathLen = strlen($path); |
|
| 1743 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1744 | + } |
|
| 1745 | + } |
|
| 1746 | + |
|
| 1747 | + /** |
|
| 1748 | + * check if it is allowed to move a mount point to a given target. |
|
| 1749 | + * It is not allowed to move a mount point into a different mount point or |
|
| 1750 | + * into an already shared folder |
|
| 1751 | + * |
|
| 1752 | + * @param string $target path |
|
| 1753 | + * @return boolean |
|
| 1754 | + */ |
|
| 1755 | + private function isTargetAllowed($target) { |
|
| 1756 | + |
|
| 1757 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1758 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1759 | + \OCP\Util::writeLog('files', |
|
| 1760 | + 'It is not allowed to move one mount point into another one', |
|
| 1761 | + \OCP\Util::DEBUG); |
|
| 1762 | + return false; |
|
| 1763 | + } |
|
| 1764 | + |
|
| 1765 | + // note: cannot use the view because the target is already locked |
|
| 1766 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1767 | + if ($fileId === -1) { |
|
| 1768 | + // target might not exist, need to check parent instead |
|
| 1769 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1770 | + } |
|
| 1771 | + |
|
| 1772 | + // check if any of the parents were shared by the current owner (include collections) |
|
| 1773 | + $shares = \OCP\Share::getItemShared( |
|
| 1774 | + 'folder', |
|
| 1775 | + $fileId, |
|
| 1776 | + \OCP\Share::FORMAT_NONE, |
|
| 1777 | + null, |
|
| 1778 | + true |
|
| 1779 | + ); |
|
| 1780 | + |
|
| 1781 | + if (count($shares) > 0) { |
|
| 1782 | + \OCP\Util::writeLog('files', |
|
| 1783 | + 'It is not allowed to move one mount point into a shared folder', |
|
| 1784 | + \OCP\Util::DEBUG); |
|
| 1785 | + return false; |
|
| 1786 | + } |
|
| 1787 | + |
|
| 1788 | + return true; |
|
| 1789 | + } |
|
| 1790 | + |
|
| 1791 | + /** |
|
| 1792 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1793 | + * |
|
| 1794 | + * @param string $path |
|
| 1795 | + * @return \OCP\Files\FileInfo |
|
| 1796 | + */ |
|
| 1797 | + private function getPartFileInfo($path) { |
|
| 1798 | + $mount = $this->getMount($path); |
|
| 1799 | + $storage = $mount->getStorage(); |
|
| 1800 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1801 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1802 | + return new FileInfo( |
|
| 1803 | + $this->getAbsolutePath($path), |
|
| 1804 | + $storage, |
|
| 1805 | + $internalPath, |
|
| 1806 | + [ |
|
| 1807 | + 'fileid' => null, |
|
| 1808 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1809 | + 'name' => basename($path), |
|
| 1810 | + 'etag' => null, |
|
| 1811 | + 'size' => $storage->filesize($internalPath), |
|
| 1812 | + 'mtime' => $storage->filemtime($internalPath), |
|
| 1813 | + 'encrypted' => false, |
|
| 1814 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1815 | + ], |
|
| 1816 | + $mount, |
|
| 1817 | + $owner |
|
| 1818 | + ); |
|
| 1819 | + } |
|
| 1820 | + |
|
| 1821 | + /** |
|
| 1822 | + * @param string $path |
|
| 1823 | + * @param string $fileName |
|
| 1824 | + * @throws InvalidPathException |
|
| 1825 | + */ |
|
| 1826 | + public function verifyPath($path, $fileName) { |
|
| 1827 | + try { |
|
| 1828 | + /** @type \OCP\Files\Storage $storage */ |
|
| 1829 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1830 | + $storage->verifyPath($internalPath, $fileName); |
|
| 1831 | + } catch (ReservedWordException $ex) { |
|
| 1832 | + $l = \OC::$server->getL10N('lib'); |
|
| 1833 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1834 | + } catch (InvalidCharacterInPathException $ex) { |
|
| 1835 | + $l = \OC::$server->getL10N('lib'); |
|
| 1836 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1837 | + } catch (FileNameTooLongException $ex) { |
|
| 1838 | + $l = \OC::$server->getL10N('lib'); |
|
| 1839 | + throw new InvalidPathException($l->t('File name is too long')); |
|
| 1840 | + } catch (InvalidDirectoryException $ex) { |
|
| 1841 | + $l = \OC::$server->getL10N('lib'); |
|
| 1842 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1843 | + } catch (EmptyFileNameException $ex) { |
|
| 1844 | + $l = \OC::$server->getL10N('lib'); |
|
| 1845 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1846 | + } |
|
| 1847 | + } |
|
| 1848 | + |
|
| 1849 | + /** |
|
| 1850 | + * get all parent folders of $path |
|
| 1851 | + * |
|
| 1852 | + * @param string $path |
|
| 1853 | + * @return string[] |
|
| 1854 | + */ |
|
| 1855 | + private function getParents($path) { |
|
| 1856 | + $path = trim($path, '/'); |
|
| 1857 | + if (!$path) { |
|
| 1858 | + return []; |
|
| 1859 | + } |
|
| 1860 | + |
|
| 1861 | + $parts = explode('/', $path); |
|
| 1862 | + |
|
| 1863 | + // remove the single file |
|
| 1864 | + array_pop($parts); |
|
| 1865 | + $result = array('/'); |
|
| 1866 | + $resultPath = ''; |
|
| 1867 | + foreach ($parts as $part) { |
|
| 1868 | + if ($part) { |
|
| 1869 | + $resultPath .= '/' . $part; |
|
| 1870 | + $result[] = $resultPath; |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + return $result; |
|
| 1874 | + } |
|
| 1875 | + |
|
| 1876 | + /** |
|
| 1877 | + * Returns the mount point for which to lock |
|
| 1878 | + * |
|
| 1879 | + * @param string $absolutePath absolute path |
|
| 1880 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1881 | + * is mounted directly on the given path, false otherwise |
|
| 1882 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1883 | + */ |
|
| 1884 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1885 | + $results = []; |
|
| 1886 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1887 | + if (!$mount) { |
|
| 1888 | + return $results; |
|
| 1889 | + } |
|
| 1890 | + |
|
| 1891 | + if ($useParentMount) { |
|
| 1892 | + // find out if something is mounted directly on the path |
|
| 1893 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1894 | + if ($internalPath === '') { |
|
| 1895 | + // resolve the parent mount instead |
|
| 1896 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1897 | + } |
|
| 1898 | + } |
|
| 1899 | + |
|
| 1900 | + return $mount; |
|
| 1901 | + } |
|
| 1902 | + |
|
| 1903 | + /** |
|
| 1904 | + * Lock the given path |
|
| 1905 | + * |
|
| 1906 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1907 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1908 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1909 | + * |
|
| 1910 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1911 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1912 | + */ |
|
| 1913 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1914 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1915 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1916 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1917 | + return false; |
|
| 1918 | + } |
|
| 1919 | + |
|
| 1920 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1921 | + if ($mount) { |
|
| 1922 | + try { |
|
| 1923 | + $storage = $mount->getStorage(); |
|
| 1924 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1925 | + $storage->acquireLock( |
|
| 1926 | + $mount->getInternalPath($absolutePath), |
|
| 1927 | + $type, |
|
| 1928 | + $this->lockingProvider |
|
| 1929 | + ); |
|
| 1930 | + } |
|
| 1931 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1932 | + // rethrow with the a human-readable path |
|
| 1933 | + throw new \OCP\Lock\LockedException( |
|
| 1934 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1935 | + $e |
|
| 1936 | + ); |
|
| 1937 | + } |
|
| 1938 | + } |
|
| 1939 | + |
|
| 1940 | + return true; |
|
| 1941 | + } |
|
| 1942 | + |
|
| 1943 | + /** |
|
| 1944 | + * Change the lock type |
|
| 1945 | + * |
|
| 1946 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1947 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1948 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1949 | + * |
|
| 1950 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1951 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1952 | + */ |
|
| 1953 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1954 | + $path = Filesystem::normalizePath($path); |
|
| 1955 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1956 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1957 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1958 | + return false; |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1962 | + if ($mount) { |
|
| 1963 | + try { |
|
| 1964 | + $storage = $mount->getStorage(); |
|
| 1965 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1966 | + $storage->changeLock( |
|
| 1967 | + $mount->getInternalPath($absolutePath), |
|
| 1968 | + $type, |
|
| 1969 | + $this->lockingProvider |
|
| 1970 | + ); |
|
| 1971 | + } |
|
| 1972 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1973 | + try { |
|
| 1974 | + // rethrow with the a human-readable path |
|
| 1975 | + throw new \OCP\Lock\LockedException( |
|
| 1976 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1977 | + $e |
|
| 1978 | + ); |
|
| 1979 | + } catch (\InvalidArgumentException $e) { |
|
| 1980 | + throw new \OCP\Lock\LockedException( |
|
| 1981 | + $absolutePath, |
|
| 1982 | + $e |
|
| 1983 | + ); |
|
| 1984 | + } |
|
| 1985 | + } |
|
| 1986 | + } |
|
| 1987 | + |
|
| 1988 | + return true; |
|
| 1989 | + } |
|
| 1990 | + |
|
| 1991 | + /** |
|
| 1992 | + * Unlock the given path |
|
| 1993 | + * |
|
| 1994 | + * @param string $path the path of the file to unlock, relative to the view |
|
| 1995 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1996 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1997 | + * |
|
| 1998 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1999 | + */ |
|
| 2000 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2001 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2002 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2003 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2004 | + return false; |
|
| 2005 | + } |
|
| 2006 | + |
|
| 2007 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2008 | + if ($mount) { |
|
| 2009 | + $storage = $mount->getStorage(); |
|
| 2010 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2011 | + $storage->releaseLock( |
|
| 2012 | + $mount->getInternalPath($absolutePath), |
|
| 2013 | + $type, |
|
| 2014 | + $this->lockingProvider |
|
| 2015 | + ); |
|
| 2016 | + } |
|
| 2017 | + } |
|
| 2018 | + |
|
| 2019 | + return true; |
|
| 2020 | + } |
|
| 2021 | + |
|
| 2022 | + /** |
|
| 2023 | + * Lock a path and all its parents up to the root of the view |
|
| 2024 | + * |
|
| 2025 | + * @param string $path the path of the file to lock relative to the view |
|
| 2026 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2027 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2028 | + * |
|
| 2029 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2030 | + */ |
|
| 2031 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2032 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2033 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2034 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2035 | + return false; |
|
| 2036 | + } |
|
| 2037 | + |
|
| 2038 | + $this->lockPath($path, $type, $lockMountPoint); |
|
| 2039 | + |
|
| 2040 | + $parents = $this->getParents($path); |
|
| 2041 | + foreach ($parents as $parent) { |
|
| 2042 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2043 | + } |
|
| 2044 | + |
|
| 2045 | + return true; |
|
| 2046 | + } |
|
| 2047 | + |
|
| 2048 | + /** |
|
| 2049 | + * Unlock a path and all its parents up to the root of the view |
|
| 2050 | + * |
|
| 2051 | + * @param string $path the path of the file to lock relative to the view |
|
| 2052 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2053 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2054 | + * |
|
| 2055 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2056 | + */ |
|
| 2057 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2058 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2059 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2060 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2061 | + return false; |
|
| 2062 | + } |
|
| 2063 | + |
|
| 2064 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2065 | + |
|
| 2066 | + $parents = $this->getParents($path); |
|
| 2067 | + foreach ($parents as $parent) { |
|
| 2068 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2069 | + } |
|
| 2070 | + |
|
| 2071 | + return true; |
|
| 2072 | + } |
|
| 2073 | + |
|
| 2074 | + /** |
|
| 2075 | + * Only lock files in data/user/files/ |
|
| 2076 | + * |
|
| 2077 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2078 | + * @return bool |
|
| 2079 | + */ |
|
| 2080 | + protected function shouldLockFile($path) { |
|
| 2081 | + $path = Filesystem::normalizePath($path); |
|
| 2082 | + |
|
| 2083 | + $pathSegments = explode('/', $path); |
|
| 2084 | + if (isset($pathSegments[2])) { |
|
| 2085 | + // E.g.: /username/files/path-to-file |
|
| 2086 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2087 | + } |
|
| 2088 | + |
|
| 2089 | + return strpos($path, '/appdata_') !== 0; |
|
| 2090 | + } |
|
| 2091 | + |
|
| 2092 | + /** |
|
| 2093 | + * Shortens the given absolute path to be relative to |
|
| 2094 | + * "$user/files". |
|
| 2095 | + * |
|
| 2096 | + * @param string $absolutePath absolute path which is under "files" |
|
| 2097 | + * |
|
| 2098 | + * @return string path relative to "files" with trimmed slashes or null |
|
| 2099 | + * if the path was NOT relative to files |
|
| 2100 | + * |
|
| 2101 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2102 | + * @since 8.1.0 |
|
| 2103 | + */ |
|
| 2104 | + public function getPathRelativeToFiles($absolutePath) { |
|
| 2105 | + $path = Filesystem::normalizePath($absolutePath); |
|
| 2106 | + $parts = explode('/', trim($path, '/'), 3); |
|
| 2107 | + // "$user", "files", "path/to/dir" |
|
| 2108 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2109 | + $this->logger->error( |
|
| 2110 | + '$absolutePath must be relative to "files", value is "%s"', |
|
| 2111 | + [ |
|
| 2112 | + $absolutePath |
|
| 2113 | + ] |
|
| 2114 | + ); |
|
| 2115 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2116 | + } |
|
| 2117 | + if (isset($parts[2])) { |
|
| 2118 | + return $parts[2]; |
|
| 2119 | + } |
|
| 2120 | + return ''; |
|
| 2121 | + } |
|
| 2122 | + |
|
| 2123 | + /** |
|
| 2124 | + * @param string $filename |
|
| 2125 | + * @return array |
|
| 2126 | + * @throws \OC\User\NoUserException |
|
| 2127 | + * @throws NotFoundException |
|
| 2128 | + */ |
|
| 2129 | + public function getUidAndFilename($filename) { |
|
| 2130 | + $info = $this->getFileInfo($filename); |
|
| 2131 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2132 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2133 | + } |
|
| 2134 | + $uid = $info->getOwner()->getUID(); |
|
| 2135 | + if ($uid != \OCP\User::getUser()) { |
|
| 2136 | + Filesystem::initMountPoints($uid); |
|
| 2137 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 2138 | + try { |
|
| 2139 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 2140 | + } catch (NotFoundException $e) { |
|
| 2141 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2142 | + } |
|
| 2143 | + } |
|
| 2144 | + return [$uid, $filename]; |
|
| 2145 | + } |
|
| 2146 | + |
|
| 2147 | + /** |
|
| 2148 | + * Creates parent non-existing folders |
|
| 2149 | + * |
|
| 2150 | + * @param string $filePath |
|
| 2151 | + * @return bool |
|
| 2152 | + */ |
|
| 2153 | + private function createParentDirectories($filePath) { |
|
| 2154 | + $directoryParts = explode('/', $filePath); |
|
| 2155 | + $directoryParts = array_filter($directoryParts); |
|
| 2156 | + foreach ($directoryParts as $key => $part) { |
|
| 2157 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2158 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2159 | + if ($this->is_file($currentPath)) { |
|
| 2160 | + return false; |
|
| 2161 | + } |
|
| 2162 | + if (!$this->file_exists($currentPath)) { |
|
| 2163 | + $this->mkdir($currentPath); |
|
| 2164 | + } |
|
| 2165 | + } |
|
| 2166 | + |
|
| 2167 | + return true; |
|
| 2168 | + } |
|
| 2169 | 2169 | } |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | $path = '/'; |
| 127 | 127 | } |
| 128 | 128 | if ($path[0] !== '/') { |
| 129 | - $path = '/' . $path; |
|
| 129 | + $path = '/'.$path; |
|
| 130 | 130 | } |
| 131 | - return $this->fakeRoot . $path; |
|
| 131 | + return $this->fakeRoot.$path; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | public function chroot($fakeRoot) { |
| 141 | 141 | if (!$fakeRoot == '') { |
| 142 | 142 | if ($fakeRoot[0] !== '/') { |
| 143 | - $fakeRoot = '/' . $fakeRoot; |
|
| 143 | + $fakeRoot = '/'.$fakeRoot; |
|
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | 146 | $this->fakeRoot = $fakeRoot; |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // missing slashes can cause wrong matches! |
| 175 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 175 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
| 176 | 176 | |
| 177 | 177 | if (strpos($path, $root) !== 0) { |
| 178 | 178 | return null; |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | if ($mount instanceof MoveableMount) { |
| 279 | 279 | // cut of /user/files to get the relative path to data/user/files |
| 280 | 280 | $pathParts = explode('/', $path, 4); |
| 281 | - $relPath = '/' . $pathParts[3]; |
|
| 281 | + $relPath = '/'.$pathParts[3]; |
|
| 282 | 282 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
| 283 | 283 | \OC_Hook::emit( |
| 284 | 284 | Filesystem::CLASSNAME, "umount", |
@@ -700,7 +700,7 @@ discard block |
||
| 700 | 700 | } |
| 701 | 701 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; |
| 702 | 702 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
| 703 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 703 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
| 704 | 704 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
| 705 | 705 | return $this->removeMount($mount, $absolutePath); |
| 706 | 706 | } |
@@ -820,7 +820,7 @@ discard block |
||
| 820 | 820 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
| 821 | 821 | } |
| 822 | 822 | } |
| 823 | - } catch(\Exception $e) { |
|
| 823 | + } catch (\Exception $e) { |
|
| 824 | 824 | throw $e; |
| 825 | 825 | } finally { |
| 826 | 826 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -844,7 +844,7 @@ discard block |
||
| 844 | 844 | } |
| 845 | 845 | } |
| 846 | 846 | } |
| 847 | - } catch(\Exception $e) { |
|
| 847 | + } catch (\Exception $e) { |
|
| 848 | 848 | throw $e; |
| 849 | 849 | } finally { |
| 850 | 850 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -977,7 +977,7 @@ discard block |
||
| 977 | 977 | $hooks[] = 'write'; |
| 978 | 978 | break; |
| 979 | 979 | default: |
| 980 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 980 | + \OCP\Util::writeLog('core', 'invalid mode ('.$mode.') for '.$path, \OCP\Util::ERROR); |
|
| 981 | 981 | } |
| 982 | 982 | |
| 983 | 983 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1081,7 +1081,7 @@ discard block |
||
| 1081 | 1081 | array(Filesystem::signal_param_path => $this->getHookPath($path)) |
| 1082 | 1082 | ); |
| 1083 | 1083 | } |
| 1084 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1084 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1085 | 1085 | if ($storage) { |
| 1086 | 1086 | $result = $storage->hash($type, $internalPath, $raw); |
| 1087 | 1087 | return $result; |
@@ -1136,7 +1136,7 @@ discard block |
||
| 1136 | 1136 | |
| 1137 | 1137 | $run = $this->runHooks($hooks, $path); |
| 1138 | 1138 | /** @var \OC\Files\Storage\Storage $storage */ |
| 1139 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1139 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1140 | 1140 | if ($run and $storage) { |
| 1141 | 1141 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
| 1142 | 1142 | try { |
@@ -1181,7 +1181,7 @@ discard block |
||
| 1181 | 1181 | $unlockLater = true; |
| 1182 | 1182 | // make sure our unlocking callback will still be called if connection is aborted |
| 1183 | 1183 | ignore_user_abort(true); |
| 1184 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1184 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
| 1185 | 1185 | if (in_array('write', $hooks)) { |
| 1186 | 1186 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
| 1187 | 1187 | } else if (in_array('read', $hooks)) { |
@@ -1242,7 +1242,7 @@ discard block |
||
| 1242 | 1242 | return true; |
| 1243 | 1243 | } |
| 1244 | 1244 | |
| 1245 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1245 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
| 1246 | 1246 | } |
| 1247 | 1247 | |
| 1248 | 1248 | /** |
@@ -1261,7 +1261,7 @@ discard block |
||
| 1261 | 1261 | if ($hook != 'read') { |
| 1262 | 1262 | \OC_Hook::emit( |
| 1263 | 1263 | Filesystem::CLASSNAME, |
| 1264 | - $prefix . $hook, |
|
| 1264 | + $prefix.$hook, |
|
| 1265 | 1265 | array( |
| 1266 | 1266 | Filesystem::signal_param_run => &$run, |
| 1267 | 1267 | Filesystem::signal_param_path => $path |
@@ -1270,7 +1270,7 @@ discard block |
||
| 1270 | 1270 | } elseif (!$post) { |
| 1271 | 1271 | \OC_Hook::emit( |
| 1272 | 1272 | Filesystem::CLASSNAME, |
| 1273 | - $prefix . $hook, |
|
| 1273 | + $prefix.$hook, |
|
| 1274 | 1274 | array( |
| 1275 | 1275 | Filesystem::signal_param_path => $path |
| 1276 | 1276 | ) |
@@ -1363,7 +1363,7 @@ discard block |
||
| 1363 | 1363 | return $this->getPartFileInfo($path); |
| 1364 | 1364 | } |
| 1365 | 1365 | $relativePath = $path; |
| 1366 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1366 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1367 | 1367 | |
| 1368 | 1368 | $mount = Filesystem::getMountManager()->find($path); |
| 1369 | 1369 | if (!$mount) { |
@@ -1390,7 +1390,7 @@ discard block |
||
| 1390 | 1390 | //add the sizes of other mount points to the folder |
| 1391 | 1391 | $extOnly = ($includeMountPoints === 'ext'); |
| 1392 | 1392 | $mounts = Filesystem::getMountManager()->findIn($path); |
| 1393 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1393 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
| 1394 | 1394 | $subStorage = $mount->getStorage(); |
| 1395 | 1395 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
| 1396 | 1396 | })); |
@@ -1444,12 +1444,12 @@ discard block |
||
| 1444 | 1444 | /** |
| 1445 | 1445 | * @var \OC\Files\FileInfo[] $fileInfos |
| 1446 | 1446 | */ |
| 1447 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1447 | + $fileInfos = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1448 | 1448 | if ($sharingDisabled) { |
| 1449 | 1449 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
| 1450 | 1450 | } |
| 1451 | 1451 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
| 1452 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1452 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1453 | 1453 | }, $contents); |
| 1454 | 1454 | $files = array_combine($fileNames, $fileInfos); |
| 1455 | 1455 | |
@@ -1475,8 +1475,8 @@ discard block |
||
| 1475 | 1475 | // sometimes when the storage is not available it can be any exception |
| 1476 | 1476 | \OCP\Util::writeLog( |
| 1477 | 1477 | 'core', |
| 1478 | - 'Exception while scanning storage "' . $subStorage->getId() . '": ' . |
|
| 1479 | - get_class($e) . ': ' . $e->getMessage(), |
|
| 1478 | + 'Exception while scanning storage "'.$subStorage->getId().'": '. |
|
| 1479 | + get_class($e).': '.$e->getMessage(), |
|
| 1480 | 1480 | \OCP\Util::ERROR |
| 1481 | 1481 | ); |
| 1482 | 1482 | continue; |
@@ -1506,7 +1506,7 @@ discard block |
||
| 1506 | 1506 | $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
| 1507 | 1507 | } |
| 1508 | 1508 | |
| 1509 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1509 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1510 | 1510 | |
| 1511 | 1511 | // if sharing was disabled for the user we remove the share permissions |
| 1512 | 1512 | if (\OCP\Util::isSharingDisabledForUser()) { |
@@ -1514,14 +1514,14 @@ discard block |
||
| 1514 | 1514 | } |
| 1515 | 1515 | |
| 1516 | 1516 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
| 1517 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1517 | + $files[$rootEntry->getName()] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1518 | 1518 | } |
| 1519 | 1519 | } |
| 1520 | 1520 | } |
| 1521 | 1521 | } |
| 1522 | 1522 | |
| 1523 | 1523 | if ($mimetype_filter) { |
| 1524 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1524 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
| 1525 | 1525 | if (strpos($mimetype_filter, '/')) { |
| 1526 | 1526 | return $file->getMimetype() === $mimetype_filter; |
| 1527 | 1527 | } else { |
@@ -1550,7 +1550,7 @@ discard block |
||
| 1550 | 1550 | if ($data instanceof FileInfo) { |
| 1551 | 1551 | $data = $data->getData(); |
| 1552 | 1552 | } |
| 1553 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1553 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1554 | 1554 | /** |
| 1555 | 1555 | * @var \OC\Files\Storage\Storage $storage |
| 1556 | 1556 | * @var string $internalPath |
@@ -1577,7 +1577,7 @@ discard block |
||
| 1577 | 1577 | * @return FileInfo[] |
| 1578 | 1578 | */ |
| 1579 | 1579 | public function search($query) { |
| 1580 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1580 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
| 1581 | 1581 | } |
| 1582 | 1582 | |
| 1583 | 1583 | /** |
@@ -1628,10 +1628,10 @@ discard block |
||
| 1628 | 1628 | |
| 1629 | 1629 | $results = call_user_func_array(array($cache, $method), $args); |
| 1630 | 1630 | foreach ($results as $result) { |
| 1631 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1631 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
| 1632 | 1632 | $internalPath = $result['path']; |
| 1633 | - $path = $mountPoint . $result['path']; |
|
| 1634 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1633 | + $path = $mountPoint.$result['path']; |
|
| 1634 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
| 1635 | 1635 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1636 | 1636 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1637 | 1637 | } |
@@ -1649,8 +1649,8 @@ discard block |
||
| 1649 | 1649 | if ($results) { |
| 1650 | 1650 | foreach ($results as $result) { |
| 1651 | 1651 | $internalPath = $result['path']; |
| 1652 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1653 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1652 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
| 1653 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
| 1654 | 1654 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1655 | 1655 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1656 | 1656 | } |
@@ -1671,7 +1671,7 @@ discard block |
||
| 1671 | 1671 | public function getOwner($path) { |
| 1672 | 1672 | $info = $this->getFileInfo($path); |
| 1673 | 1673 | if (!$info) { |
| 1674 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1674 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
| 1675 | 1675 | } |
| 1676 | 1676 | return $info->getOwner()->getUID(); |
| 1677 | 1677 | } |
@@ -1705,7 +1705,7 @@ discard block |
||
| 1705 | 1705 | * @return string |
| 1706 | 1706 | */ |
| 1707 | 1707 | public function getPath($id) { |
| 1708 | - $id = (int)$id; |
|
| 1708 | + $id = (int) $id; |
|
| 1709 | 1709 | $manager = Filesystem::getMountManager(); |
| 1710 | 1710 | $mounts = $manager->findIn($this->fakeRoot); |
| 1711 | 1711 | $mounts[] = $manager->find($this->fakeRoot); |
@@ -1720,7 +1720,7 @@ discard block |
||
| 1720 | 1720 | $cache = $mount->getStorage()->getCache(); |
| 1721 | 1721 | $internalPath = $cache->getPathById($id); |
| 1722 | 1722 | if (is_string($internalPath)) { |
| 1723 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1723 | + $fullPath = $mount->getMountPoint().$internalPath; |
|
| 1724 | 1724 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
| 1725 | 1725 | return $path; |
| 1726 | 1726 | } |
@@ -1763,10 +1763,10 @@ discard block |
||
| 1763 | 1763 | } |
| 1764 | 1764 | |
| 1765 | 1765 | // note: cannot use the view because the target is already locked |
| 1766 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1766 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); |
|
| 1767 | 1767 | if ($fileId === -1) { |
| 1768 | 1768 | // target might not exist, need to check parent instead |
| 1769 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1769 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1770 | 1770 | } |
| 1771 | 1771 | |
| 1772 | 1772 | // check if any of the parents were shared by the current owner (include collections) |
@@ -1866,7 +1866,7 @@ discard block |
||
| 1866 | 1866 | $resultPath = ''; |
| 1867 | 1867 | foreach ($parts as $part) { |
| 1868 | 1868 | if ($part) { |
| 1869 | - $resultPath .= '/' . $part; |
|
| 1869 | + $resultPath .= '/'.$part; |
|
| 1870 | 1870 | $result[] = $resultPath; |
| 1871 | 1871 | } |
| 1872 | 1872 | } |
@@ -2129,16 +2129,16 @@ discard block |
||
| 2129 | 2129 | public function getUidAndFilename($filename) { |
| 2130 | 2130 | $info = $this->getFileInfo($filename); |
| 2131 | 2131 | if (!$info instanceof \OCP\Files\FileInfo) { |
| 2132 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2132 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
| 2133 | 2133 | } |
| 2134 | 2134 | $uid = $info->getOwner()->getUID(); |
| 2135 | 2135 | if ($uid != \OCP\User::getUser()) { |
| 2136 | 2136 | Filesystem::initMountPoints($uid); |
| 2137 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2137 | + $ownerView = new View('/'.$uid.'/files'); |
|
| 2138 | 2138 | try { |
| 2139 | 2139 | $filename = $ownerView->getPath($info['fileid']); |
| 2140 | 2140 | } catch (NotFoundException $e) { |
| 2141 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2141 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
| 2142 | 2142 | } |
| 2143 | 2143 | } |
| 2144 | 2144 | return [$uid, $filename]; |
@@ -2155,7 +2155,7 @@ discard block |
||
| 2155 | 2155 | $directoryParts = array_filter($directoryParts); |
| 2156 | 2156 | foreach ($directoryParts as $key => $part) { |
| 2157 | 2157 | $currentPathElements = array_slice($directoryParts, 0, $key); |
| 2158 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2158 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
| 2159 | 2159 | if ($this->is_file($currentPath)) { |
| 2160 | 2160 | return false; |
| 2161 | 2161 | } |