| Total Complexity | 160 |
| Total Lines | 808 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Common often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Common, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 79 | abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { |
||
| 80 | use LocalTempFileTrait; |
||
| 81 | |||
| 82 | protected $cache; |
||
| 83 | protected $scanner; |
||
| 84 | protected $watcher; |
||
| 85 | protected $propagator; |
||
| 86 | protected $storageCache; |
||
| 87 | protected $updater; |
||
| 88 | |||
| 89 | protected $mountOptions = []; |
||
| 90 | protected $owner = null; |
||
| 91 | |||
| 92 | private $shouldLogLocks = null; |
||
| 93 | private $logger; |
||
| 94 | |||
| 95 | public function __construct($parameters) { |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Remove a file or folder |
||
| 100 | * |
||
| 101 | * @param string $path |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | protected function remove($path) { |
||
| 105 | if ($this->is_dir($path)) { |
||
| 106 | return $this->rmdir($path); |
||
| 107 | } elseif ($this->is_file($path)) { |
||
| 108 | return $this->unlink($path); |
||
| 109 | } else { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function is_dir($path) { |
||
| 115 | return $this->filetype($path) === 'dir'; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function is_file($path) { |
||
| 119 | return $this->filetype($path) === 'file'; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function filesize($path) { |
||
| 123 | if ($this->is_dir($path)) { |
||
| 124 | return 0; //by definition |
||
| 125 | } else { |
||
| 126 | $stat = $this->stat($path); |
||
| 127 | if (isset($stat['size'])) { |
||
| 128 | return $stat['size']; |
||
| 129 | } else { |
||
| 130 | return 0; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function isReadable($path) { |
||
| 136 | // at least check whether it exists |
||
| 137 | // subclasses might want to implement this more thoroughly |
||
| 138 | return $this->file_exists($path); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function isUpdatable($path) { |
||
| 142 | // at least check whether it exists |
||
| 143 | // subclasses might want to implement this more thoroughly |
||
| 144 | // a non-existing file/folder isn't updatable |
||
| 145 | return $this->file_exists($path); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function isCreatable($path) { |
||
| 149 | if ($this->is_dir($path) && $this->isUpdatable($path)) { |
||
| 150 | return true; |
||
| 151 | } |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function isDeletable($path) { |
||
| 156 | if ($path === '' || $path === '/') { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | $parent = dirname($path); |
||
| 160 | return $this->isUpdatable($parent) && $this->isUpdatable($path); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function isSharable($path) { |
||
| 164 | return $this->isReadable($path); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getPermissions($path) { |
||
| 168 | $permissions = 0; |
||
| 169 | if ($this->isCreatable($path)) { |
||
| 170 | $permissions |= \OCP\Constants::PERMISSION_CREATE; |
||
| 171 | } |
||
| 172 | if ($this->isReadable($path)) { |
||
| 173 | $permissions |= \OCP\Constants::PERMISSION_READ; |
||
| 174 | } |
||
| 175 | if ($this->isUpdatable($path)) { |
||
| 176 | $permissions |= \OCP\Constants::PERMISSION_UPDATE; |
||
| 177 | } |
||
| 178 | if ($this->isDeletable($path)) { |
||
| 179 | $permissions |= \OCP\Constants::PERMISSION_DELETE; |
||
| 180 | } |
||
| 181 | if ($this->isSharable($path)) { |
||
| 182 | $permissions |= \OCP\Constants::PERMISSION_SHARE; |
||
| 183 | } |
||
| 184 | return $permissions; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function filemtime($path) { |
||
| 188 | $stat = $this->stat($path); |
||
| 189 | if (isset($stat['mtime']) && $stat['mtime'] > 0) { |
||
| 190 | return $stat['mtime']; |
||
| 191 | } else { |
||
| 192 | return 0; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | public function file_get_contents($path) { |
||
| 204 | } |
||
| 205 | |||
| 206 | public function file_put_contents($path, $data) { |
||
| 207 | $handle = $this->fopen($path, "w"); |
||
| 208 | $this->removeCachedFile($path); |
||
| 209 | $count = fwrite($handle, $data); |
||
| 210 | fclose($handle); |
||
| 211 | return $count; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function rename($path1, $path2) { |
||
| 215 | $this->remove($path2); |
||
| 216 | |||
| 217 | $this->removeCachedFile($path1); |
||
| 218 | return $this->copy($path1, $path2) and $this->remove($path1); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function copy($path1, $path2) { |
||
| 222 | if ($this->is_dir($path1)) { |
||
| 223 | $this->remove($path2); |
||
| 224 | $dir = $this->opendir($path1); |
||
| 225 | $this->mkdir($path2); |
||
| 226 | while ($file = readdir($dir)) { |
||
| 227 | if (!Filesystem::isIgnoredDir($file)) { |
||
| 228 | if (!$this->copy($path1 . '/' . $file, $path2 . '/' . $file)) { |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | closedir($dir); |
||
| 234 | return true; |
||
| 235 | } else { |
||
| 236 | $source = $this->fopen($path1, 'r'); |
||
| 237 | $target = $this->fopen($path2, 'w'); |
||
| 238 | [, $result] = \OC_Helper::streamCopy($source, $target); |
||
| 239 | if (!$result) { |
||
| 240 | \OC::$server->getLogger()->warning("Failed to write data while copying $path1 to $path2"); |
||
| 241 | } |
||
| 242 | $this->removeCachedFile($path2); |
||
| 243 | return $result; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | public function getMimeType($path) { |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | public function hash($type, $path, $raw = false) { |
||
| 263 | } |
||
| 264 | |||
| 265 | public function search($query) { |
||
| 266 | return $this->searchInDir($query); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function getLocalFile($path) { |
||
| 270 | return $this->getCachedFile($path); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $path |
||
| 275 | * @param string $target |
||
| 276 | */ |
||
| 277 | private function addLocalFolder($path, $target) { |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $query |
||
| 296 | * @param string $dir |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | protected function searchInDir($query, $dir = '') { |
||
| 300 | $files = []; |
||
| 301 | $dh = $this->opendir($dir); |
||
| 302 | if (is_resource($dh)) { |
||
| 303 | while (($item = readdir($dh)) !== false) { |
||
| 304 | if (\OC\Files\Filesystem::isIgnoredDir($item)) { |
||
| 305 | continue; |
||
| 306 | } |
||
| 307 | if (strstr(strtolower($item), strtolower($query)) !== false) { |
||
| 308 | $files[] = $dir . '/' . $item; |
||
| 309 | } |
||
| 310 | if ($this->is_dir($dir . '/' . $item)) { |
||
| 311 | $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item)); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | closedir($dh); |
||
| 316 | return $files; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * check if a file or folder has been updated since $time |
||
| 321 | * |
||
| 322 | * The method is only used to check if the cache needs to be updated. Storage backends that don't support checking |
||
| 323 | * the mtime should always return false here. As a result storage implementations that always return false expect |
||
| 324 | * exclusive access to the backend and will not pick up files that have been added in a way that circumvents |
||
| 325 | * ownClouds filesystem. |
||
| 326 | * |
||
| 327 | * @param string $path |
||
| 328 | * @param int $time |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function hasUpdated($path, $time) { |
||
| 332 | return $this->filemtime($path) > $time; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function getCache($path = '', $storage = null) { |
||
| 336 | if (!$storage) { |
||
| 337 | $storage = $this; |
||
| 338 | } |
||
| 339 | if (!isset($storage->cache)) { |
||
| 340 | $storage->cache = new Cache($storage); |
||
| 341 | } |
||
| 342 | return $storage->cache; |
||
| 343 | } |
||
| 344 | |||
| 345 | public function getScanner($path = '', $storage = null) { |
||
| 346 | if (!$storage) { |
||
| 347 | $storage = $this; |
||
| 348 | } |
||
| 349 | if (!isset($storage->scanner)) { |
||
| 350 | $storage->scanner = new Scanner($storage); |
||
| 351 | } |
||
| 352 | return $storage->scanner; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function getWatcher($path = '', $storage = null) { |
||
| 356 | if (!$storage) { |
||
| 357 | $storage = $this; |
||
| 358 | } |
||
| 359 | if (!isset($this->watcher)) { |
||
| 360 | $this->watcher = new Watcher($storage); |
||
| 361 | $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); |
||
| 362 | $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); |
||
| 363 | } |
||
| 364 | return $this->watcher; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * get a propagator instance for the cache |
||
| 369 | * |
||
| 370 | * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher |
||
| 371 | * @return \OC\Files\Cache\Propagator |
||
| 372 | */ |
||
| 373 | public function getPropagator($storage = null) { |
||
| 374 | if (!$storage) { |
||
| 375 | $storage = $this; |
||
| 376 | } |
||
| 377 | if (!isset($storage->propagator)) { |
||
| 378 | $config = \OC::$server->getSystemConfig(); |
||
| 379 | $storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getValue('instanceid')]); |
||
| 380 | } |
||
| 381 | return $storage->propagator; |
||
| 382 | } |
||
| 383 | |||
| 384 | public function getUpdater($storage = null) { |
||
| 385 | if (!$storage) { |
||
| 386 | $storage = $this; |
||
| 387 | } |
||
| 388 | if (!isset($storage->updater)) { |
||
| 389 | $storage->updater = new Updater($storage); |
||
| 390 | } |
||
| 391 | return $storage->updater; |
||
| 392 | } |
||
| 393 | |||
| 394 | public function getStorageCache($storage = null) { |
||
| 395 | if (!$storage) { |
||
| 396 | $storage = $this; |
||
| 397 | } |
||
| 398 | if (!isset($this->storageCache)) { |
||
| 399 | $this->storageCache = new \OC\Files\Cache\Storage($storage); |
||
| 400 | } |
||
| 401 | return $this->storageCache; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * get the owner of a path |
||
| 406 | * |
||
| 407 | * @param string $path The path to get the owner |
||
| 408 | * @return string|false uid or false |
||
| 409 | */ |
||
| 410 | public function getOwner($path) { |
||
| 411 | if ($this->owner === null) { |
||
| 412 | $this->owner = \OC_User::getUser(); |
||
| 413 | } |
||
| 414 | |||
| 415 | return $this->owner; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * get the ETag for a file or folder |
||
| 420 | * |
||
| 421 | * @param string $path |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getETag($path) { |
||
| 425 | return uniqid(); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * clean a path, i.e. remove all redundant '.' and '..' |
||
| 430 | * making sure that it can't point to higher than '/' |
||
| 431 | * |
||
| 432 | * @param string $path The path to clean |
||
| 433 | * @return string cleaned path |
||
| 434 | */ |
||
| 435 | public function cleanPath($path) { |
||
| 436 | if (strlen($path) == 0 or $path[0] != '/') { |
||
| 437 | $path = '/' . $path; |
||
| 438 | } |
||
| 439 | |||
| 440 | $output = []; |
||
| 441 | foreach (explode('/', $path) as $chunk) { |
||
| 442 | if ($chunk == '..') { |
||
| 443 | array_pop($output); |
||
| 444 | } elseif ($chunk == '.') { |
||
| 445 | } else { |
||
| 446 | $output[] = $chunk; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | return implode('/', $output); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Test a storage for availability |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function test() { |
||
| 458 | try { |
||
| 459 | if ($this->stat('')) { |
||
| 460 | return true; |
||
| 461 | } |
||
| 462 | \OC::$server->getLogger()->info("External storage not available: stat() failed"); |
||
| 463 | return false; |
||
| 464 | } catch (\Exception $e) { |
||
| 465 | \OC::$server->getLogger()->warning("External storage not available: " . $e->getMessage()); |
||
| 466 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
||
| 467 | return false; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * get the free space in the storage |
||
| 473 | * |
||
| 474 | * @param string $path |
||
| 475 | * @return int|false |
||
| 476 | */ |
||
| 477 | public function free_space($path) { |
||
| 478 | return \OCP\Files\FileInfo::SPACE_UNKNOWN; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function isLocal() { |
||
| 485 | // the common implementation returns a temporary file by |
||
| 486 | // default, which is not local |
||
| 487 | return false; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class |
||
| 492 | * |
||
| 493 | * @param string $class |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | public function instanceOfStorage($class) { |
||
| 497 | if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') { |
||
| 498 | // FIXME Temporary fix to keep existing checks working |
||
| 499 | $class = '\OCA\Files_Sharing\SharedStorage'; |
||
| 500 | } |
||
| 501 | return is_a($this, $class); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * A custom storage implementation can return an url for direct download of a give file. |
||
| 506 | * |
||
| 507 | * For now the returned array can hold the parameter url - in future more attributes might follow. |
||
| 508 | * |
||
| 509 | * @param string $path |
||
| 510 | * @return array|false |
||
| 511 | */ |
||
| 512 | public function getDirectDownload($path) { |
||
| 513 | return []; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @inheritdoc |
||
| 518 | * @throws InvalidPathException |
||
| 519 | */ |
||
| 520 | public function verifyPath($path, $fileName) { |
||
| 521 | |||
| 522 | // verify empty and dot files |
||
| 523 | $trimmed = trim($fileName); |
||
| 524 | if ($trimmed === '') { |
||
| 525 | throw new EmptyFileNameException(); |
||
| 526 | } |
||
| 527 | |||
| 528 | if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { |
||
| 529 | throw new InvalidDirectoryException(); |
||
| 530 | } |
||
| 531 | |||
| 532 | if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) { |
||
| 533 | // verify database - e.g. mysql only 3-byte chars |
||
| 534 | if (preg_match('%(?: |
||
| 535 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
||
| 536 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
||
| 537 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
||
| 538 | )%xs', $fileName)) { |
||
| 539 | throw new InvalidCharacterInPathException(); |
||
| 540 | } |
||
| 541 | } |
||
| 542 | |||
| 543 | // 255 characters is the limit on common file systems (ext/xfs) |
||
| 544 | // oc_filecache has a 250 char length limit for the filename |
||
| 545 | if (isset($fileName[250])) { |
||
| 546 | throw new FileNameTooLongException(); |
||
| 547 | } |
||
| 548 | |||
| 549 | // NOTE: $path will remain unverified for now |
||
| 550 | $this->verifyPosixPath($fileName); |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $fileName |
||
| 555 | * @throws InvalidPathException |
||
| 556 | */ |
||
| 557 | protected function verifyPosixPath($fileName) { |
||
| 558 | $fileName = trim($fileName); |
||
| 559 | $this->scanForInvalidCharacters($fileName, "\\/"); |
||
| 560 | $reservedNames = ['*']; |
||
| 561 | if (in_array($fileName, $reservedNames)) { |
||
| 562 | throw new ReservedWordException(); |
||
| 563 | } |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param string $fileName |
||
| 568 | * @param string $invalidChars |
||
| 569 | * @throws InvalidPathException |
||
| 570 | */ |
||
| 571 | private function scanForInvalidCharacters($fileName, $invalidChars) { |
||
| 572 | foreach (str_split($invalidChars) as $char) { |
||
| 573 | if (strpos($fileName, $char) !== false) { |
||
| 574 | throw new InvalidCharacterInPathException(); |
||
| 575 | } |
||
| 576 | } |
||
| 577 | |||
| 578 | $sanitizedFileName = filter_var($fileName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
||
| 579 | if ($sanitizedFileName !== $fileName) { |
||
| 580 | throw new InvalidCharacterInPathException(); |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param array $options |
||
| 586 | */ |
||
| 587 | public function setMountOptions(array $options) { |
||
| 588 | $this->mountOptions = $options; |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param string $name |
||
| 593 | * @param mixed $default |
||
| 594 | * @return mixed |
||
| 595 | */ |
||
| 596 | public function getMountOption($name, $default = null) { |
||
| 597 | return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param IStorage $sourceStorage |
||
| 602 | * @param string $sourceInternalPath |
||
| 603 | * @param string $targetInternalPath |
||
| 604 | * @param bool $preserveMtime |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
||
| 608 | if ($sourceStorage === $this) { |
||
| 609 | return $this->copy($sourceInternalPath, $targetInternalPath); |
||
| 610 | } |
||
| 611 | |||
| 612 | if ($sourceStorage->is_dir($sourceInternalPath)) { |
||
| 613 | $dh = $sourceStorage->opendir($sourceInternalPath); |
||
| 614 | $result = $this->mkdir($targetInternalPath); |
||
| 615 | if (is_resource($dh)) { |
||
| 616 | while ($result and ($file = readdir($dh)) !== false) { |
||
| 617 | if (!Filesystem::isIgnoredDir($file)) { |
||
| 618 | $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); |
||
| 619 | } |
||
| 620 | } |
||
| 621 | } |
||
| 622 | } else { |
||
| 623 | $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
||
| 624 | $result = false; |
||
| 625 | if ($source) { |
||
| 626 | try { |
||
| 627 | $this->writeStream($targetInternalPath, $source); |
||
| 628 | $result = true; |
||
| 629 | } catch (\Exception $e) { |
||
| 630 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN, 'message' => 'Failed to copy stream to storage']); |
||
| 631 | } |
||
| 632 | } |
||
| 633 | |||
| 634 | if ($result and $preserveMtime) { |
||
| 635 | $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
||
| 636 | } |
||
| 637 | |||
| 638 | if (!$result) { |
||
| 639 | // delete partially written target file |
||
| 640 | $this->unlink($targetInternalPath); |
||
| 641 | // delete cache entry that was created by fopen |
||
| 642 | $this->getCache()->remove($targetInternalPath); |
||
| 643 | } |
||
| 644 | } |
||
| 645 | return (bool)$result; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Check if a storage is the same as the current one, including wrapped storages |
||
| 650 | * |
||
| 651 | * @param IStorage $storage |
||
| 652 | * @return bool |
||
| 653 | */ |
||
| 654 | private function isSameStorage(IStorage $storage): bool { |
||
| 655 | while ($storage->instanceOfStorage(Wrapper::class)) { |
||
| 656 | /** |
||
| 657 | * @var Wrapper $sourceStorage |
||
| 658 | */ |
||
| 659 | $storage = $storage->getWrapperStorage(); |
||
| 660 | } |
||
| 661 | |||
| 662 | return $storage === $this; |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @param IStorage $sourceStorage |
||
| 667 | * @param string $sourceInternalPath |
||
| 668 | * @param string $targetInternalPath |
||
| 669 | * @return bool |
||
| 670 | */ |
||
| 671 | public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
||
| 672 | if ($this->isSameStorage($sourceStorage)) { |
||
| 673 | // resolve any jailed paths |
||
| 674 | while ($sourceStorage->instanceOfStorage(Jail::class)) { |
||
| 675 | /** |
||
| 676 | * @var Jail $sourceStorage |
||
| 677 | */ |
||
| 678 | $sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath); |
||
| 679 | $sourceStorage = $sourceStorage->getUnjailedStorage(); |
||
| 680 | } |
||
| 681 | |||
| 682 | return $this->rename($sourceInternalPath, $targetInternalPath); |
||
| 683 | } |
||
| 684 | |||
| 685 | if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
||
| 686 | return false; |
||
| 687 | } |
||
| 688 | |||
| 689 | $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); |
||
| 690 | if ($result) { |
||
| 691 | if ($sourceStorage->is_dir($sourceInternalPath)) { |
||
| 692 | $result &= $sourceStorage->rmdir($sourceInternalPath); |
||
| 693 | } else { |
||
| 694 | $result &= $sourceStorage->unlink($sourceInternalPath); |
||
| 695 | } |
||
| 696 | } |
||
| 697 | return $result; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @inheritdoc |
||
| 702 | */ |
||
| 703 | public function getMetaData($path) { |
||
| 704 | $permissions = $this->getPermissions($path); |
||
| 705 | if (!$permissions & \OCP\Constants::PERMISSION_READ) { |
||
| 706 | //can't read, nothing we can do |
||
| 707 | return null; |
||
| 708 | } |
||
| 709 | |||
| 710 | $data = []; |
||
| 711 | $data['mimetype'] = $this->getMimeType($path); |
||
| 712 | $data['mtime'] = $this->filemtime($path); |
||
| 713 | if ($data['mtime'] === false) { |
||
| 714 | $data['mtime'] = time(); |
||
| 715 | } |
||
| 716 | if ($data['mimetype'] == 'httpd/unix-directory') { |
||
| 717 | $data['size'] = -1; //unknown |
||
| 718 | } else { |
||
| 719 | $data['size'] = $this->filesize($path); |
||
| 720 | } |
||
| 721 | $data['etag'] = $this->getETag($path); |
||
| 722 | $data['storage_mtime'] = $data['mtime']; |
||
| 723 | $data['permissions'] = $permissions; |
||
| 724 | $data['name'] = basename($path); |
||
| 725 | |||
| 726 | return $data; |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * @param string $path |
||
| 731 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 732 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 733 | * @throws \OCP\Lock\LockedException |
||
| 734 | */ |
||
| 735 | public function acquireLock($path, $type, ILockingProvider $provider) { |
||
| 736 | $logger = $this->getLockLogger(); |
||
| 737 | if ($logger) { |
||
| 738 | $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
||
| 739 | $logger->info( |
||
| 740 | sprintf( |
||
| 741 | 'acquire %s lock on "%s" on storage "%s"', |
||
| 742 | $typeString, |
||
| 743 | $path, |
||
| 744 | $this->getId() |
||
| 745 | ), |
||
| 746 | [ |
||
| 747 | 'app' => 'locking', |
||
| 748 | ] |
||
| 749 | ); |
||
| 750 | } |
||
| 751 | try { |
||
| 752 | $provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type, $this->getId() . '::' . $path); |
||
| 753 | } catch (LockedException $e) { |
||
| 754 | if ($logger) { |
||
| 755 | $logger->logException($e, ['level' => ILogger::INFO]); |
||
| 756 | } |
||
| 757 | throw $e; |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @param string $path |
||
| 763 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 764 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 765 | * @throws \OCP\Lock\LockedException |
||
| 766 | */ |
||
| 767 | public function releaseLock($path, $type, ILockingProvider $provider) { |
||
| 790 | } |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @param string $path |
||
| 795 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 796 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 797 | * @throws \OCP\Lock\LockedException |
||
| 798 | */ |
||
| 799 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 800 | $logger = $this->getLockLogger(); |
||
| 801 | if ($logger) { |
||
| 802 | $typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive'; |
||
| 803 | $logger->info( |
||
| 804 | sprintf( |
||
| 805 | 'change lock on "%s" to %s on storage "%s"', |
||
| 806 | $path, |
||
| 807 | $typeString, |
||
| 808 | $this->getId() |
||
| 809 | ), |
||
| 810 | [ |
||
| 811 | 'app' => 'locking', |
||
| 812 | ] |
||
| 813 | ); |
||
| 814 | } |
||
| 815 | try { |
||
| 816 | $provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type); |
||
| 817 | } catch (LockedException $e) { |
||
| 818 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::INFO]); |
||
| 819 | throw $e; |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | private function getLockLogger() { |
||
| 824 | if (is_null($this->shouldLogLocks)) { |
||
| 825 | $this->shouldLogLocks = \OC::$server->getConfig()->getSystemValue('filelocking.debug', false); |
||
| 826 | $this->logger = $this->shouldLogLocks ? \OC::$server->getLogger() : null; |
||
| 827 | } |
||
| 828 | return $this->logger; |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * @return array [ available, last_checked ] |
||
| 833 | */ |
||
| 834 | public function getAvailability() { |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @param bool $isAvailable |
||
| 840 | */ |
||
| 841 | public function setAvailability($isAvailable) { |
||
| 842 | $this->getStorageCache()->setAvailability($isAvailable); |
||
| 843 | } |
||
| 844 | |||
| 845 | /** |
||
| 846 | * @return bool |
||
| 847 | */ |
||
| 848 | public function needsPartFile() { |
||
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * fallback implementation |
||
| 854 | * |
||
| 855 | * @param string $path |
||
| 856 | * @param resource $stream |
||
| 857 | * @param int $size |
||
| 858 | * @return int |
||
| 859 | */ |
||
| 860 | public function writeStream(string $path, $stream, int $size = null): int { |
||
| 861 | $target = $this->fopen($path, 'w'); |
||
| 862 | if (!$target) { |
||
| 863 | throw new GenericFileException("Failed to open $path for writing"); |
||
| 864 | } |
||
| 865 | try { |
||
| 866 | [$count, $result] = \OC_Helper::streamCopy($stream, $target); |
||
| 867 | if (!$result) { |
||
| 868 | throw new GenericFileException("Failed to copy stream"); |
||
| 869 | } |
||
| 870 | } finally { |
||
| 871 | fclose($target); |
||
| 872 | fclose($stream); |
||
| 873 | } |
||
| 874 | return $count; |
||
| 875 | } |
||
| 876 | |||
| 877 | public function getDirectoryContent($directory): \Traversable { |
||
| 887 | } |
||
| 888 | } |
||
| 889 | } |
||
| 890 | } |
||
| 891 | } |
||
| 892 | } |
||
| 893 |