Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Encryption often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Encryption, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Encryption extends Wrapper { |
||
| 47 | |||
| 48 | use LocalTempFileTrait; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $mountPoint; |
||
| 52 | |||
| 53 | /** @var \OC\Encryption\Util */ |
||
| 54 | private $util; |
||
| 55 | |||
| 56 | /** @var \OCP\Encryption\IManager */ |
||
| 57 | private $encryptionManager; |
||
| 58 | |||
| 59 | /** @var \OCP\ILogger */ |
||
| 60 | private $logger; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $uid; |
||
| 64 | |||
| 65 | /** @var array */ |
||
| 66 | protected $unencryptedSize; |
||
| 67 | |||
| 68 | /** @var \OCP\Encryption\IFile */ |
||
| 69 | private $fileHelper; |
||
| 70 | |||
| 71 | /** @var IMountPoint */ |
||
| 72 | private $mount; |
||
| 73 | |||
| 74 | /** @var IStorage */ |
||
| 75 | private $keyStorage; |
||
| 76 | |||
| 77 | /** @var Update */ |
||
| 78 | private $update; |
||
| 79 | |||
| 80 | /** @var Manager */ |
||
| 81 | private $mountManager; |
||
| 82 | |||
| 83 | /** @var array remember for which path we execute the repair step to avoid recursions */ |
||
| 84 | private $fixUnencryptedSizeOf = []; |
||
| 85 | |||
| 86 | /** @var ArrayCache */ |
||
| 87 | private $arrayCache; |
||
| 88 | |||
| 89 | /** @var array which has information of sourcePath during rename operation */ |
||
| 90 | private $sourcePath; |
||
| 91 | |||
| 92 | private static $disableWriteEncryption = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $parameters |
||
| 96 | * @param IManager $encryptionManager |
||
| 97 | * @param Util $util |
||
| 98 | * @param ILogger $logger |
||
| 99 | * @param IFile $fileHelper |
||
| 100 | * @param string $uid |
||
| 101 | * @param IStorage $keyStorage |
||
| 102 | * @param Update $update |
||
| 103 | * @param Manager $mountManager |
||
| 104 | * @param ArrayCache $arrayCache |
||
| 105 | */ |
||
| 106 | public function __construct( |
||
| 107 | $parameters, |
||
| 108 | IManager $encryptionManager = null, |
||
| 109 | Util $util = null, |
||
| 110 | ILogger $logger = null, |
||
| 111 | IFile $fileHelper = null, |
||
| 112 | $uid = null, |
||
| 113 | IStorage $keyStorage = null, |
||
| 114 | Update $update = null, |
||
| 115 | Manager $mountManager = null, |
||
| 116 | ArrayCache $arrayCache = null |
||
| 117 | ) { |
||
| 118 | |||
| 119 | $this->mountPoint = $parameters['mountPoint']; |
||
| 120 | $this->mount = $parameters['mount']; |
||
| 121 | $this->encryptionManager = $encryptionManager; |
||
| 122 | $this->util = $util; |
||
| 123 | $this->logger = $logger; |
||
| 124 | $this->uid = $uid; |
||
| 125 | $this->fileHelper = $fileHelper; |
||
| 126 | $this->keyStorage = $keyStorage; |
||
| 127 | $this->unencryptedSize = []; |
||
| 128 | $this->update = $update; |
||
| 129 | $this->mountManager = $mountManager; |
||
| 130 | $this->arrayCache = $arrayCache; |
||
| 131 | parent::__construct($parameters); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * see http://php.net/manual/en/function.filesize.php |
||
| 136 | * The result for filesize when called on a folder is required to be 0 |
||
| 137 | * |
||
| 138 | * @param string $path |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | public function filesize($path) { |
||
| 142 | $fullPath = $this->getFullPath($path); |
||
| 143 | |||
| 144 | /** @var CacheEntry $info */ |
||
| 145 | $info = $this->getCache()->get($path); |
||
| 146 | if (isset($this->unencryptedSize[$fullPath])) { |
||
| 147 | $size = $this->unencryptedSize[$fullPath]; |
||
| 148 | // update file cache |
||
| 149 | if ($info instanceof ICacheEntry) { |
||
| 150 | $info = $info->getData(); |
||
| 151 | $info['encrypted'] = $info['encryptedVersion']; |
||
| 152 | } else { |
||
| 153 | if (!is_array($info)) { |
||
| 154 | $info = []; |
||
| 155 | } |
||
| 156 | $info['encrypted'] = true; |
||
| 157 | } |
||
| 158 | |||
| 159 | $info['size'] = $size; |
||
| 160 | $this->getCache()->put($path, $info); |
||
| 161 | |||
| 162 | return $size; |
||
| 163 | } |
||
| 164 | |||
| 165 | if (isset($info['fileid']) && $info['encrypted']) { |
||
| 166 | return $this->verifyUnencryptedSize($path, $info['size']); |
||
|
|
|||
| 167 | } |
||
| 168 | |||
| 169 | return $this->storage->filesize($path); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $path |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function getMetaData($path) { |
||
| 177 | $data = $this->storage->getMetaData($path); |
||
| 178 | if (is_null($data)) { |
||
| 179 | return null; |
||
| 180 | } |
||
| 181 | $fullPath = $this->getFullPath($path); |
||
| 182 | $info = $this->getCache()->get($path); |
||
| 183 | |||
| 184 | if (isset($this->unencryptedSize[$fullPath])) { |
||
| 185 | $data['encrypted'] = true; |
||
| 186 | $data['size'] = $this->unencryptedSize[$fullPath]; |
||
| 187 | } else { |
||
| 188 | if (isset($info['fileid']) && $info['encrypted']) { |
||
| 189 | $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
||
| 190 | $data['encrypted'] = true; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
||
| 195 | $data['encryptedVersion'] = $info['encryptedVersion']; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $data; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * see http://php.net/manual/en/function.file_get_contents.php |
||
| 203 | * |
||
| 204 | * @param string $path |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function file_get_contents($path) { |
||
| 208 | |||
| 209 | if ($this->encryptionManager->isEnabled() !== false) { |
||
| 210 | $encryptionModule = $this->getEncryptionModule($path); |
||
| 211 | |||
| 212 | if ($encryptionModule) { |
||
| 213 | $handle = $this->fopen($path, "r"); |
||
| 214 | if (!$handle) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | $data = stream_get_contents($handle); |
||
| 218 | fclose($handle); |
||
| 219 | return $data; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | return $this->storage->file_get_contents($path); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * see http://php.net/manual/en/function.file_put_contents.php |
||
| 227 | * |
||
| 228 | * @param string $path |
||
| 229 | * @param string $data |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function file_put_contents($path, $data) { |
||
| 233 | // file put content will always be translated to a stream write |
||
| 234 | $handle = $this->fopen($path, 'w'); |
||
| 235 | if (is_resource($handle)) { |
||
| 236 | $written = fwrite($handle, $data); |
||
| 237 | fclose($handle); |
||
| 238 | return $written; |
||
| 239 | } |
||
| 240 | |||
| 241 | return false; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * see http://php.net/manual/en/function.unlink.php |
||
| 246 | * |
||
| 247 | * @param string $path |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function unlink($path) { |
||
| 251 | $fullPath = $this->getFullPath($path); |
||
| 252 | if ($this->util->isExcluded($fullPath)) { |
||
| 253 | return $this->storage->unlink($path); |
||
| 254 | } |
||
| 255 | |||
| 256 | $encryptionModule = ($this->encryptionManager->isEnabled()) ? $this->getEncryptionModule($path) : ""; |
||
| 257 | if ($encryptionModule) { |
||
| 258 | $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this->storage->unlink($path); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * see http://php.net/manual/en/function.rename.php |
||
| 266 | * |
||
| 267 | * @param string $path1 |
||
| 268 | * @param string $path2 |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function rename($path1, $path2) { |
||
| 272 | |||
| 273 | $result = $this->storage->rename($path1, $path2); |
||
| 274 | |||
| 275 | if ($result && |
||
| 276 | // versions always use the keys from the original file, so we can skip |
||
| 277 | // this step for versions |
||
| 278 | $this->isVersion($path2) === false && |
||
| 279 | $this->encryptionManager->isEnabled()) { |
||
| 280 | $source = $this->getFullPath($path1); |
||
| 281 | if (!$this->util->isExcluded($source)) { |
||
| 282 | $target = $this->getFullPath($path2); |
||
| 283 | if (isset($this->unencryptedSize[$source])) { |
||
| 284 | $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
||
| 285 | } |
||
| 286 | $this->keyStorage->renameKeys($source, $target); |
||
| 287 | $module = $this->getEncryptionModule($path2); |
||
| 288 | if ($module) { |
||
| 289 | $module->update($target, $this->uid, []); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return $result; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * see http://php.net/manual/en/function.rmdir.php |
||
| 299 | * |
||
| 300 | * @param string $path |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function rmdir($path) { |
||
| 304 | $result = $this->storage->rmdir($path); |
||
| 305 | $fullPath = $this->getFullPath($path); |
||
| 306 | if ($result && |
||
| 307 | $this->util->isExcluded($fullPath) === false && |
||
| 308 | $this->encryptionManager->isEnabled() |
||
| 309 | ) { |
||
| 310 | $this->keyStorage->deleteAllFileKeys($fullPath); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $result; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * check if a file can be read |
||
| 318 | * |
||
| 319 | * @param string $path |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function isReadable($path) { |
||
| 323 | |||
| 324 | $isReadable = true; |
||
| 325 | |||
| 326 | $metaData = $this->getMetaData($path); |
||
| 327 | if ( |
||
| 328 | !$this->is_dir($path) && |
||
| 329 | isset($metaData['encrypted']) && |
||
| 330 | $metaData['encrypted'] === true |
||
| 331 | ) { |
||
| 332 | $fullPath = $this->getFullPath($path); |
||
| 333 | $module = $this->getEncryptionModule($path); |
||
| 334 | $isReadable = $module->isReadable($fullPath, $this->uid); |
||
| 335 | } |
||
| 336 | |||
| 337 | return $this->storage->isReadable($path) && $isReadable; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * see http://php.net/manual/en/function.copy.php |
||
| 342 | * |
||
| 343 | * @param string $path1 |
||
| 344 | * @param string $path2 |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function copy($path1, $path2) { |
||
| 348 | |||
| 349 | $source = $this->getFullPath($path1); |
||
| 350 | |||
| 351 | if ($this->util->isExcluded($source)) { |
||
| 352 | return $this->storage->copy($path1, $path2); |
||
| 353 | } |
||
| 354 | |||
| 355 | // need to stream copy file by file in case we copy between a encrypted |
||
| 356 | // and a unencrypted storage |
||
| 357 | $this->unlink($path2); |
||
| 358 | $result = $this->copyFromStorage($this, $path1, $path2); |
||
| 359 | |||
| 360 | return $result; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * see http://php.net/manual/en/function.fopen.php |
||
| 365 | * |
||
| 366 | * @param string $path |
||
| 367 | * @param string $mode |
||
| 368 | * @return resource|bool |
||
| 369 | * @throws GenericEncryptionException |
||
| 370 | * @throws ModuleDoesNotExistsException |
||
| 371 | */ |
||
| 372 | public function fopen($path, $mode) { |
||
| 373 | |||
| 374 | // check if the file is stored in the array cache, this means that we |
||
| 375 | // copy a file over to the versions folder, in this case we don't want to |
||
| 376 | // decrypt it |
||
| 377 | if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
||
| 378 | $this->arrayCache->remove('encryption_copy_version_' . $path); |
||
| 379 | return $this->storage->fopen($path, $mode); |
||
| 380 | } |
||
| 381 | |||
| 382 | $encryptionEnabled = $this->encryptionManager->isEnabled(); |
||
| 383 | $shouldEncrypt = false; |
||
| 384 | $encryptionModule = null; |
||
| 385 | $header = $this->getHeader($path); |
||
| 386 | $signed = (isset($header['signed']) && $header['signed'] === 'true') ? true : false; |
||
| 387 | $fullPath = $this->getFullPath($path); |
||
| 388 | $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
||
| 389 | |||
| 390 | if ($this->util->isExcluded($fullPath) === false) { |
||
| 391 | |||
| 392 | $size = $unencryptedSize = 0; |
||
| 393 | $realFile = $this->util->stripPartialFileExtension($path); |
||
| 394 | $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
||
| 395 | $targetIsEncrypted = false; |
||
| 396 | if ($targetExists) { |
||
| 397 | // in case the file exists we require the explicit module as |
||
| 398 | // specified in the file header - otherwise we need to fail hard to |
||
| 399 | // prevent data loss on client side |
||
| 400 | if (!empty($encryptionModuleId)) { |
||
| 401 | $targetIsEncrypted = true; |
||
| 402 | $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
||
| 403 | } |
||
| 404 | |||
| 405 | if ($this->file_exists($path)) { |
||
| 406 | $size = $this->storage->filesize($path); |
||
| 407 | $unencryptedSize = $this->filesize($path); |
||
| 408 | } else { |
||
| 409 | $size = $unencryptedSize = 0; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | try { |
||
| 414 | |||
| 415 | if ( |
||
| 416 | $mode === 'w' |
||
| 417 | || $mode === 'w+' |
||
| 418 | || $mode === 'wb' |
||
| 419 | || $mode === 'wb+' |
||
| 420 | ) { |
||
| 421 | // don't overwrite encrypted files if encryption is not enabled |
||
| 422 | if ($targetIsEncrypted && $encryptionEnabled === false) { |
||
| 423 | throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled'); |
||
| 424 | } |
||
| 425 | if ($encryptionEnabled) { |
||
| 426 | // if $encryptionModuleId is empty, the default module will be used |
||
| 427 | $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
||
| 428 | $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
||
| 429 | $signed = true; |
||
| 430 | } |
||
| 431 | } else { |
||
| 432 | $info = $this->getCache()->get($path); |
||
| 433 | // only get encryption module if we found one in the header |
||
| 434 | // or if file should be encrypted according to the file cache |
||
| 435 | if (!empty($encryptionModuleId)) { |
||
| 436 | $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
||
| 437 | $shouldEncrypt = true; |
||
| 438 | } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
||
| 439 | // we come from a old installation. No header and/or no module defined |
||
| 440 | // but the file is encrypted. In this case we need to use the |
||
| 441 | // OC_DEFAULT_MODULE to read the file |
||
| 442 | $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
||
| 443 | $shouldEncrypt = true; |
||
| 444 | $targetIsEncrypted = true; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | } catch (ModuleDoesNotExistsException $e) { |
||
| 448 | $this->logger->warning('Encryption module "' . $encryptionModuleId . |
||
| 449 | '" not found, file will be stored unencrypted (' . $e->getMessage() . ')'); |
||
| 450 | } |
||
| 451 | |||
| 452 | // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
||
| 453 | if (!$encryptionEnabled || !$this->mount->getOption('encrypt', true)) { |
||
| 454 | if (!$targetExists || !$targetIsEncrypted) { |
||
| 455 | $shouldEncrypt = false; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | if ($shouldEncrypt === true && $encryptionModule !== null) { |
||
| 460 | /** |
||
| 461 | * The check of $disableWriteEncryption, required to get the file in the decrypted state. |
||
| 462 | * It will help us get the normal file handler. And hence we can re-encrypt |
||
| 463 | * the file when necessary, later. The true/false of $getDecryptedFile decides whether |
||
| 464 | * to keep the file decrypted or not. The intention is to get the data decrypt |
||
| 465 | * for write mode. |
||
| 466 | */ |
||
| 467 | if (self::$disableWriteEncryption && ($mode !== 'r')) { |
||
| 468 | return $this->getWrapperStorage()->fopen($path, $mode); |
||
| 469 | } |
||
| 470 | |||
| 471 | $headerSize = $this->getHeaderSize($path); |
||
| 472 | $source = $this->storage->fopen($path, $mode); |
||
| 473 | if (!is_resource($source)) { |
||
| 474 | return false; |
||
| 475 | } |
||
| 476 | |||
| 477 | if (isset($this->sourcePath[$path])) { |
||
| 478 | $sourceFileOfRename = $this->sourcePath[$path]; |
||
| 479 | } else { |
||
| 480 | $sourceFileOfRename = null; |
||
| 481 | } |
||
| 482 | $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
||
| 483 | $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
||
| 484 | $size, $unencryptedSize, $headerSize, $signed, $sourceFileOfRename); |
||
| 485 | unset($this->sourcePath[$path]); |
||
| 486 | |||
| 487 | return $handle; |
||
| 488 | } |
||
| 489 | |||
| 490 | } |
||
| 491 | |||
| 492 | return $this->storage->fopen($path, $mode); |
||
| 493 | } |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * perform some plausibility checks if the the unencrypted size is correct. |
||
| 498 | * If not, we calculate the correct unencrypted size and return it |
||
| 499 | * |
||
| 500 | * @param string $path internal path relative to the storage root |
||
| 501 | * @param int $unencryptedSize size of the unencrypted file |
||
| 502 | * |
||
| 503 | * @return int unencrypted size |
||
| 504 | */ |
||
| 505 | protected function verifyUnencryptedSize($path, $unencryptedSize) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * calculate the unencrypted size |
||
| 532 | * |
||
| 533 | * @param string $path internal path relative to the storage root |
||
| 534 | * @param int $size size of the physical file |
||
| 535 | * @param int $unencryptedSize size of the unencrypted file |
||
| 536 | * |
||
| 537 | * @return int calculated unencrypted size |
||
| 538 | */ |
||
| 539 | protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @param Storage $sourceStorage |
||
| 619 | * @param string $sourceInternalPath |
||
| 620 | * @param string $targetInternalPath |
||
| 621 | * @param bool $preserveMtime |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | View Code Duplication | public function moveFromStorage(Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Set the flag to true, so that the file would be |
||
| 652 | * in the decrypted state. |
||
| 653 | * |
||
| 654 | * @param $isDisabled bool |
||
| 655 | */ |
||
| 656 | public static function setDisableWriteEncryption($isDisabled) { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @param Storage $sourceStorage |
||
| 662 | * @param string $sourceInternalPath |
||
| 663 | * @param string $targetInternalPath |
||
| 664 | * @param bool $preserveMtime |
||
| 665 | * @param bool $isRename |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | public function copyFromStorage(Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Update the encrypted cache version in the database |
||
| 681 | * |
||
| 682 | * @param Storage $sourceStorage |
||
| 683 | * @param string $sourceInternalPath |
||
| 684 | * @param string $targetInternalPath |
||
| 685 | * @param bool $isRename |
||
| 686 | */ |
||
| 687 | private function updateEncryptedVersion(Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename) { |
||
| 688 | $isEncrypted = $this->encryptionManager->isEnabled() && $this->mount->getOption('encrypt', true) ? 1 : 0; |
||
| 689 | $cacheInformation = [ |
||
| 690 | 'encrypted' => (bool)$isEncrypted, |
||
| 691 | ]; |
||
| 692 | if($isEncrypted === 1) { |
||
| 693 | $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
||
| 694 | |||
| 695 | // In case of a move operation from an unencrypted to an encrypted |
||
| 696 | // storage the old encrypted version would stay with "0" while the |
||
| 697 | // correct value would be "1". Thus we manually set the value to "1" |
||
| 698 | // for those cases. |
||
| 699 | // See also https://github.com/owncloud/core/issues/23078 |
||
| 700 | if($encryptedVersion === 0) { |
||
| 701 | $encryptedVersion = 1; |
||
| 702 | } |
||
| 703 | |||
| 704 | $cacheInformation['encryptedVersion'] = $encryptedVersion; |
||
| 705 | } |
||
| 706 | |||
| 707 | // in case of a rename we need to manipulate the source cache because |
||
| 708 | // this information will be kept for the new target |
||
| 709 | if ($isRename) { |
||
| 710 | /* |
||
| 711 | * Rename is a process of creating a new file. Here we try to use the |
||
| 712 | * incremented version of source file, for the destination file. |
||
| 713 | */ |
||
| 714 | $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
||
| 715 | if ($this->encryptionManager->isEnabled()) { |
||
| 716 | $cacheInformation['encryptedVersion'] = $encryptedVersion + 1; |
||
| 717 | } |
||
| 718 | $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
||
| 719 | } else { |
||
| 720 | $this->getCache()->put($targetInternalPath, $cacheInformation); |
||
| 721 | } |
||
| 722 | } |
||
| 723 | |||
| 724 | /** |
||
| 725 | * copy file between two storages |
||
| 726 | * |
||
| 727 | * @param Storage $sourceStorage |
||
| 728 | * @param string $sourceInternalPath |
||
| 729 | * @param string $targetInternalPath |
||
| 730 | * @param bool $preserveMtime |
||
| 731 | * @param bool $isRename |
||
| 732 | * @return bool |
||
| 733 | * @throws \Exception |
||
| 734 | */ |
||
| 735 | private function copyBetweenStorage(Storage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
||
| 736 | // for versions we have nothing to do, because versions should always use the |
||
| 737 | // key from the original file. Just create a 1:1 copy and done |
||
| 738 | if ($this->isVersion($targetInternalPath) || |
||
| 739 | $this->isVersion($sourceInternalPath)) { |
||
| 740 | // remember that we try to create a version so that we can detect it during |
||
| 741 | // fopen($sourceInternalPath) and by-pass the encryption in order to |
||
| 742 | // create a 1:1 copy of the file |
||
| 743 | $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
||
| 744 | $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
| 745 | $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
||
| 746 | if ($result) { |
||
| 747 | $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
||
| 748 | // make sure that we update the unencrypted size for the version |
||
| 749 | if (isset($info['encrypted']) && $info['encrypted'] === true) { |
||
| 750 | $this->updateUnencryptedSize( |
||
| 751 | $this->getFullPath($targetInternalPath), |
||
| 752 | $info['size'] |
||
| 753 | ); |
||
| 754 | } |
||
| 755 | $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename); |
||
| 756 | } |
||
| 757 | return $result; |
||
| 758 | } |
||
| 759 | |||
| 760 | // first copy the keys that we reuse the existing file key on the target location |
||
| 761 | // and don't create a new one which would break versions for example. |
||
| 762 | $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
||
| 763 | if (count($mount) === 1) { |
||
| 764 | $mountPoint = $mount[0]->getMountPoint(); |
||
| 765 | $source = $mountPoint . '/' . $sourceInternalPath; |
||
| 766 | $target = $this->getFullPath($targetInternalPath); |
||
| 767 | $this->copyKeys($source, $target); |
||
| 768 | } else { |
||
| 769 | $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
||
| 770 | } |
||
| 771 | |||
| 772 | if ($sourceStorage->is_dir($sourceInternalPath)) { |
||
| 773 | $dh = $sourceStorage->opendir($sourceInternalPath); |
||
| 774 | $result = $this->mkdir($targetInternalPath); |
||
| 775 | View Code Duplication | if (is_resource($dh)) { |
|
| 776 | while ($result and ($file = readdir($dh)) !== false) { |
||
| 777 | if (!Filesystem::isIgnoredDir($file)) { |
||
| 778 | $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
||
| 779 | } |
||
| 780 | } |
||
| 781 | } |
||
| 782 | } else { |
||
| 783 | try { |
||
| 784 | $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
||
| 785 | if ($isRename && (count($mount) === 1)) { |
||
| 786 | $sourceStorageMountPoint = $mount[0]->getMountPoint(); |
||
| 787 | $this->sourcePath[$targetInternalPath] = $sourceStorageMountPoint . '/' . $sourceInternalPath; |
||
| 788 | } else { |
||
| 789 | unset($this->sourcePath[$targetInternalPath]); |
||
| 790 | } |
||
| 791 | $target = $this->fopen($targetInternalPath, 'w'); |
||
| 792 | list(, $result) = \OC_Helper::streamCopy($source, $target); |
||
| 793 | fclose($source); |
||
| 794 | fclose($target); |
||
| 795 | } catch (\Exception $e) { |
||
| 796 | Encryption::setDisableWriteEncryption(false); |
||
| 797 | fclose($source); |
||
| 798 | fclose($target); |
||
| 799 | throw $e; |
||
| 800 | } |
||
| 801 | if($result) { |
||
| 802 | if ($preserveMtime) { |
||
| 803 | $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
||
| 804 | } |
||
| 805 | $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename); |
||
| 806 | } else { |
||
| 807 | // delete partially written target file |
||
| 808 | $this->unlink($targetInternalPath); |
||
| 809 | // delete cache entry that was created by fopen |
||
| 810 | $this->getCache()->remove($targetInternalPath); |
||
| 811 | } |
||
| 812 | } |
||
| 813 | return (bool)$result; |
||
| 814 | |||
| 815 | } |
||
| 816 | |||
| 817 | /** |
||
| 818 | * get the path to a local version of the file. |
||
| 819 | * The local version of the file can be temporary and doesn't have to be persistent across requests |
||
| 820 | * |
||
| 821 | * @param string $path |
||
| 822 | * @return string |
||
| 823 | */ |
||
| 824 | public function getLocalFile($path) { |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Returns the wrapped storage's value for isLocal() |
||
| 836 | * |
||
| 837 | * @return bool wrapped storage's isLocal() value |
||
| 838 | */ |
||
| 839 | public function isLocal() { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * see http://php.net/manual/en/function.stat.php |
||
| 848 | * only the following keys are required in the result: size and mtime |
||
| 849 | * |
||
| 850 | * @param string $path |
||
| 851 | * @return array |
||
| 852 | */ |
||
| 853 | public function stat($path) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * see http://php.net/manual/en/function.hash.php |
||
| 863 | * |
||
| 864 | * @param string $type |
||
| 865 | * @param string $path |
||
| 866 | * @param bool $raw |
||
| 867 | * @return string |
||
| 868 | */ |
||
| 869 | View Code Duplication | public function hash($type, $path, $raw = false) { |
|
| 876 | |||
| 877 | /** |
||
| 878 | * return full path, including mount point |
||
| 879 | * |
||
| 880 | * @param string $path relative to mount point |
||
| 881 | * @return string full path including mount point |
||
| 882 | */ |
||
| 883 | protected function getFullPath($path) { |
||
| 886 | |||
| 887 | /** |
||
| 888 | * read first block of encrypted file, typically this will contain the |
||
| 889 | * encryption header |
||
| 890 | * |
||
| 891 | * @param string|resource $path |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | protected function readFirstBlock($path) { |
||
| 908 | |||
| 909 | /** |
||
| 910 | * return header size of given file |
||
| 911 | * |
||
| 912 | * @param string|resource $path |
||
| 913 | * @return int |
||
| 914 | */ |
||
| 915 | protected function getHeaderSize($path) { |
||
| 931 | |||
| 932 | /** |
||
| 933 | * parse raw header to array |
||
| 934 | * |
||
| 935 | * @param string $rawHeader |
||
| 936 | * @return array |
||
| 937 | */ |
||
| 938 | protected function parseRawHeader($rawHeader) { |
||
| 959 | |||
| 960 | /** |
||
| 961 | * read header from file |
||
| 962 | * |
||
| 963 | * @param string|resource $path |
||
| 964 | * @return array |
||
| 965 | */ |
||
| 966 | protected function getHeader($path) { |
||
| 997 | |||
| 998 | /** |
||
| 999 | * read encryption module needed to read/write the file located at $path |
||
| 1000 | * |
||
| 1001 | * @param string $path |
||
| 1002 | * @return null|\OCP\Encryption\IEncryptionModule |
||
| 1003 | * @throws ModuleDoesNotExistsException |
||
| 1004 | * @throws \Exception |
||
| 1005 | */ |
||
| 1006 | protected function getEncryptionModule($path) { |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * @param string $path |
||
| 1023 | * @param int $unencryptedSize |
||
| 1024 | */ |
||
| 1025 | public function updateUnencryptedSize($path, $unencryptedSize) { |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * copy keys to new location |
||
| 1031 | * |
||
| 1032 | * @param string $source path relative to data/ |
||
| 1033 | * @param string $target path relative to data/ |
||
| 1034 | * @return bool |
||
| 1035 | */ |
||
| 1036 | protected function copyKeys($source, $target) { |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * |
||
| 1046 | * delete file keys of the file |
||
| 1047 | * |
||
| 1048 | * @param $path path of the file key to delete |
||
| 1049 | * @return bool |
||
| 1050 | */ |
||
| 1051 | protected function deleteAllFileKeys($path) { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * check if path points to a files version |
||
| 1058 | * |
||
| 1059 | * @param $path |
||
| 1060 | * @return bool |
||
| 1061 | */ |
||
| 1062 | protected function isVersion($path) { |
||
| 1066 | |||
| 1067 | } |
||
| 1068 |