@@ -76,685 +76,685 @@ |
||
| 76 | 76 | use Sabre\DAV\IFile; |
| 77 | 77 | |
| 78 | 78 | class File extends Node implements IFile { |
| 79 | - protected $request; |
|
| 80 | - |
|
| 81 | - protected IL10N $l10n; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Sets up the node, expects a full path name |
|
| 85 | - * |
|
| 86 | - * @param \OC\Files\View $view |
|
| 87 | - * @param \OCP\Files\FileInfo $info |
|
| 88 | - * @param \OCP\Share\IManager $shareManager |
|
| 89 | - * @param \OC\AppFramework\Http\Request $request |
|
| 90 | - */ |
|
| 91 | - public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
|
| 92 | - parent::__construct($view, $info, $shareManager); |
|
| 93 | - |
|
| 94 | - // Querying IL10N directly results in a dependency loop |
|
| 95 | - /** @var IL10NFactory $l10nFactory */ |
|
| 96 | - $l10nFactory = \OC::$server->get(IL10NFactory::class); |
|
| 97 | - $this->l10n = $l10nFactory->get(Application::APP_ID); |
|
| 98 | - |
|
| 99 | - if (isset($request)) { |
|
| 100 | - $this->request = $request; |
|
| 101 | - } else { |
|
| 102 | - $this->request = \OC::$server->getRequest(); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Updates the data |
|
| 108 | - * |
|
| 109 | - * The data argument is a readable stream resource. |
|
| 110 | - * |
|
| 111 | - * After a successful put operation, you may choose to return an ETag. The |
|
| 112 | - * etag must always be surrounded by double-quotes. These quotes must |
|
| 113 | - * appear in the actual string you're returning. |
|
| 114 | - * |
|
| 115 | - * Clients may use the ETag from a PUT request to later on make sure that |
|
| 116 | - * when they update the file, the contents haven't changed in the mean |
|
| 117 | - * time. |
|
| 118 | - * |
|
| 119 | - * If you don't plan to store the file byte-by-byte, and you return a |
|
| 120 | - * different object on a subsequent GET you are strongly recommended to not |
|
| 121 | - * return an ETag, and just return null. |
|
| 122 | - * |
|
| 123 | - * @param resource $data |
|
| 124 | - * |
|
| 125 | - * @throws Forbidden |
|
| 126 | - * @throws UnsupportedMediaType |
|
| 127 | - * @throws BadRequest |
|
| 128 | - * @throws Exception |
|
| 129 | - * @throws EntityTooLarge |
|
| 130 | - * @throws ServiceUnavailable |
|
| 131 | - * @throws FileLocked |
|
| 132 | - * @return string|null |
|
| 133 | - */ |
|
| 134 | - public function put($data) { |
|
| 135 | - try { |
|
| 136 | - $exists = $this->fileView->file_exists($this->path); |
|
| 137 | - if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 138 | - throw new Forbidden(); |
|
| 139 | - } |
|
| 140 | - } catch (StorageNotAvailableException $e) { |
|
| 141 | - throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()])); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // verify path of the target |
|
| 145 | - $this->verifyPath(); |
|
| 146 | - |
|
| 147 | - // chunked handling |
|
| 148 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 149 | - try { |
|
| 150 | - return $this->createFileChunked($data); |
|
| 151 | - } catch (\Exception $e) { |
|
| 152 | - $this->convertToSabreException($e); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** @var Storage $partStorage */ |
|
| 157 | - [$partStorage] = $this->fileView->resolvePath($this->path); |
|
| 158 | - $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
|
| 159 | - |
|
| 160 | - $view = \OC\Files\Filesystem::getView(); |
|
| 161 | - |
|
| 162 | - if ($needsPartFile) { |
|
| 163 | - // mark file as partial while uploading (ignored by the scanner) |
|
| 164 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 165 | - |
|
| 166 | - if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) { |
|
| 167 | - $needsPartFile = false; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - if (!$needsPartFile) { |
|
| 171 | - // upload file directly as the final path |
|
| 172 | - $partFilePath = $this->path; |
|
| 173 | - |
|
| 174 | - if ($view && !$this->emitPreHooks($exists)) { |
|
| 175 | - throw new Exception($this->l10n->t('Could not write to final file, canceled by hook')); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 180 | - /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 181 | - [$partStorage, $internalPartPath] = $this->fileView->resolvePath($partFilePath); |
|
| 182 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 183 | - [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
| 184 | - try { |
|
| 185 | - if (!$needsPartFile) { |
|
| 186 | - try { |
|
| 187 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 188 | - } catch (LockedException $e) { |
|
| 189 | - // during very large uploads, the shared lock we got at the start might have been expired |
|
| 190 | - // meaning that the above lock can fail not just only because somebody else got a shared lock |
|
| 191 | - // or because there is no existing shared lock to make exclusive |
|
| 192 | - // |
|
| 193 | - // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
| 194 | - // lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
| 195 | - // the entire operation will be safe |
|
| 196 | - |
|
| 197 | - try { |
|
| 198 | - $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 199 | - } catch (LockedException $ex) { |
|
| 200 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - if (!is_resource($data)) { |
|
| 206 | - $tmpData = fopen('php://temp', 'r+'); |
|
| 207 | - if ($data !== null) { |
|
| 208 | - fwrite($tmpData, $data); |
|
| 209 | - rewind($tmpData); |
|
| 210 | - } |
|
| 211 | - $data = $tmpData; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $data = HashWrapper::wrap($data, 'md5', function ($hash) { |
|
| 215 | - $this->header('X-Hash-MD5: ' . $hash); |
|
| 216 | - }); |
|
| 217 | - $data = HashWrapper::wrap($data, 'sha1', function ($hash) { |
|
| 218 | - $this->header('X-Hash-SHA1: ' . $hash); |
|
| 219 | - }); |
|
| 220 | - $data = HashWrapper::wrap($data, 'sha256', function ($hash) { |
|
| 221 | - $this->header('X-Hash-SHA256: ' . $hash); |
|
| 222 | - }); |
|
| 223 | - |
|
| 224 | - if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { |
|
| 225 | - $isEOF = false; |
|
| 226 | - $wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) { |
|
| 227 | - $isEOF = feof($stream); |
|
| 228 | - }); |
|
| 229 | - |
|
| 230 | - $result = true; |
|
| 231 | - $count = -1; |
|
| 232 | - try { |
|
| 233 | - $count = $partStorage->writeStream($internalPartPath, $wrappedData); |
|
| 234 | - } catch (GenericFileException $e) { |
|
| 235 | - $result = false; |
|
| 236 | - } catch (BadGateway $e) { |
|
| 237 | - throw $e; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - if ($result === false) { |
|
| 242 | - $result = $isEOF; |
|
| 243 | - if (is_resource($wrappedData)) { |
|
| 244 | - $result = feof($wrappedData); |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - } else { |
|
| 248 | - $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 249 | - if ($target === false) { |
|
| 250 | - \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
|
| 251 | - // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 252 | - throw new Exception($this->l10n->t('Could not write file contents')); |
|
| 253 | - } |
|
| 254 | - [$count, $result] = \OC_Helper::streamCopy($data, $target); |
|
| 255 | - fclose($target); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ($result === false) { |
|
| 259 | - $expected = -1; |
|
| 260 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 261 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 262 | - } |
|
| 263 | - if ($expected !== "0") { |
|
| 264 | - throw new Exception( |
|
| 265 | - $this->l10n->t( |
|
| 266 | - 'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)', |
|
| 267 | - [ |
|
| 268 | - $this->l10n->n('%n byte', '%n bytes', $count), |
|
| 269 | - $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 270 | - ], |
|
| 271 | - ) |
|
| 272 | - ); |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // if content length is sent by client: |
|
| 277 | - // double check if the file was fully received |
|
| 278 | - // compare expected and actual size |
|
| 279 | - if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 280 | - $expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
| 281 | - if ($count !== $expected) { |
|
| 282 | - throw new BadRequest( |
|
| 283 | - $this->l10n->t( |
|
| 284 | - 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
|
| 285 | - [ |
|
| 286 | - $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 287 | - $this->l10n->n('%n byte', '%n bytes', $count), |
|
| 288 | - ], |
|
| 289 | - ) |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - } catch (\Exception $e) { |
|
| 294 | - $context = []; |
|
| 295 | - |
|
| 296 | - if ($e instanceof LockedException) { |
|
| 297 | - $context['level'] = ILogger::DEBUG; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - \OC::$server->getLogger()->logException($e, $context); |
|
| 301 | - if ($needsPartFile) { |
|
| 302 | - $partStorage->unlink($internalPartPath); |
|
| 303 | - } |
|
| 304 | - $this->convertToSabreException($e); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - try { |
|
| 308 | - if ($needsPartFile) { |
|
| 309 | - if ($view && !$this->emitPreHooks($exists)) { |
|
| 310 | - $partStorage->unlink($internalPartPath); |
|
| 311 | - throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook')); |
|
| 312 | - } |
|
| 313 | - try { |
|
| 314 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 315 | - } catch (LockedException $e) { |
|
| 316 | - // during very large uploads, the shared lock we got at the start might have been expired |
|
| 317 | - // meaning that the above lock can fail not just only because somebody else got a shared lock |
|
| 318 | - // or because there is no existing shared lock to make exclusive |
|
| 319 | - // |
|
| 320 | - // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
| 321 | - // lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
| 322 | - // the entire operation will be safe |
|
| 323 | - |
|
| 324 | - try { |
|
| 325 | - $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 326 | - } catch (LockedException $ex) { |
|
| 327 | - if ($needsPartFile) { |
|
| 328 | - $partStorage->unlink($internalPartPath); |
|
| 329 | - } |
|
| 330 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // rename to correct path |
|
| 335 | - try { |
|
| 336 | - $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 337 | - $fileExists = $storage->file_exists($internalPath); |
|
| 338 | - if ($renameOkay === false || $fileExists === false) { |
|
| 339 | - \OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']); |
|
| 340 | - throw new Exception($this->l10n->t('Could not rename part file to final file')); |
|
| 341 | - } |
|
| 342 | - } catch (ForbiddenException $ex) { |
|
| 343 | - if (!$ex->getRetry()) { |
|
| 344 | - $partStorage->unlink($internalPartPath); |
|
| 345 | - } |
|
| 346 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 347 | - } catch (\Exception $e) { |
|
| 348 | - $partStorage->unlink($internalPartPath); |
|
| 349 | - $this->convertToSabreException($e); |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 354 | - $storage->getUpdater()->update($internalPath); |
|
| 355 | - |
|
| 356 | - try { |
|
| 357 | - $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 358 | - } catch (LockedException $e) { |
|
| 359 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - // allow sync clients to send the mtime along in a header |
|
| 363 | - if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
| 364 | - $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
| 365 | - if ($this->fileView->touch($this->path, $mtime)) { |
|
| 366 | - $this->header('X-OC-MTime: accepted'); |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $fileInfoUpdate = [ |
|
| 371 | - 'upload_time' => time() |
|
| 372 | - ]; |
|
| 373 | - |
|
| 374 | - // allow sync clients to send the creation time along in a header |
|
| 375 | - if (isset($this->request->server['HTTP_X_OC_CTIME'])) { |
|
| 376 | - $ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']); |
|
| 377 | - $fileInfoUpdate['creation_time'] = $ctime; |
|
| 378 | - $this->header('X-OC-CTime: accepted'); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - $this->fileView->putFileInfo($this->path, $fileInfoUpdate); |
|
| 382 | - |
|
| 383 | - if ($view) { |
|
| 384 | - $this->emitPostHooks($exists); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - $this->refreshInfo(); |
|
| 388 | - |
|
| 389 | - if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
| 390 | - $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
| 391 | - $this->setChecksum($checksum); |
|
| 392 | - } elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 393 | - $this->setChecksum(''); |
|
| 394 | - } |
|
| 395 | - } catch (StorageNotAvailableException $e) { |
|
| 396 | - throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return '"' . $this->info->getEtag() . '"'; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - private function getPartFileBasePath($path) { |
|
| 403 | - $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 404 | - if ($partFileInStorage) { |
|
| 405 | - return $path; |
|
| 406 | - } else { |
|
| 407 | - return md5($path); // will place it in the root of the view with a unique name |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * @param string $path |
|
| 413 | - */ |
|
| 414 | - private function emitPreHooks($exists, $path = null) { |
|
| 415 | - if (is_null($path)) { |
|
| 416 | - $path = $this->path; |
|
| 417 | - } |
|
| 418 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 419 | - $run = true; |
|
| 420 | - |
|
| 421 | - if (!$exists) { |
|
| 422 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, [ |
|
| 423 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 424 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 425 | - ]); |
|
| 426 | - } else { |
|
| 427 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, [ |
|
| 428 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 429 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 430 | - ]); |
|
| 431 | - } |
|
| 432 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, [ |
|
| 433 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 434 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 435 | - ]); |
|
| 436 | - return $run; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * @param string $path |
|
| 441 | - */ |
|
| 442 | - private function emitPostHooks($exists, $path = null) { |
|
| 443 | - if (is_null($path)) { |
|
| 444 | - $path = $this->path; |
|
| 445 | - } |
|
| 446 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 447 | - if (!$exists) { |
|
| 448 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, [ |
|
| 449 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 450 | - ]); |
|
| 451 | - } else { |
|
| 452 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, [ |
|
| 453 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 454 | - ]); |
|
| 455 | - } |
|
| 456 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, [ |
|
| 457 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 458 | - ]); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Returns the data |
|
| 463 | - * |
|
| 464 | - * @return resource |
|
| 465 | - * @throws Forbidden |
|
| 466 | - * @throws ServiceUnavailable |
|
| 467 | - */ |
|
| 468 | - public function get() { |
|
| 469 | - //throw exception if encryption is disabled but files are still encrypted |
|
| 470 | - try { |
|
| 471 | - if (!$this->info->isReadable()) { |
|
| 472 | - // do a if the file did not exist |
|
| 473 | - throw new NotFound(); |
|
| 474 | - } |
|
| 475 | - try { |
|
| 476 | - $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 477 | - } catch (\Exception $e) { |
|
| 478 | - $this->convertToSabreException($e); |
|
| 479 | - } |
|
| 480 | - if ($res === false) { |
|
| 481 | - throw new ServiceUnavailable($this->l10n->t('Could not open file')); |
|
| 482 | - } |
|
| 483 | - return $res; |
|
| 484 | - } catch (GenericEncryptionException $e) { |
|
| 485 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 486 | - throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()])); |
|
| 487 | - } catch (StorageNotAvailableException $e) { |
|
| 488 | - throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()])); |
|
| 489 | - } catch (ForbiddenException $ex) { |
|
| 490 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 491 | - } catch (LockedException $e) { |
|
| 492 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Delete the current file |
|
| 498 | - * |
|
| 499 | - * @throws Forbidden |
|
| 500 | - * @throws ServiceUnavailable |
|
| 501 | - */ |
|
| 502 | - public function delete() { |
|
| 503 | - if (!$this->info->isDeletable()) { |
|
| 504 | - throw new Forbidden(); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - try { |
|
| 508 | - if (!$this->fileView->unlink($this->path)) { |
|
| 509 | - // assume it wasn't possible to delete due to permissions |
|
| 510 | - throw new Forbidden(); |
|
| 511 | - } |
|
| 512 | - } catch (StorageNotAvailableException $e) { |
|
| 513 | - throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()])); |
|
| 514 | - } catch (ForbiddenException $ex) { |
|
| 515 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 516 | - } catch (LockedException $e) { |
|
| 517 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Returns the mime-type for a file |
|
| 523 | - * |
|
| 524 | - * If null is returned, we'll assume application/octet-stream |
|
| 525 | - * |
|
| 526 | - * @return string |
|
| 527 | - */ |
|
| 528 | - public function getContentType() { |
|
| 529 | - $mimeType = $this->info->getMimetype(); |
|
| 530 | - |
|
| 531 | - // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 532 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 533 | - return $mimeType; |
|
| 534 | - } |
|
| 535 | - return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * @return array|bool |
|
| 540 | - */ |
|
| 541 | - public function getDirectDownload() { |
|
| 542 | - if (\OCP\App::isEnabled('encryption')) { |
|
| 543 | - return []; |
|
| 544 | - } |
|
| 545 | - /** @var \OCP\Files\Storage $storage */ |
|
| 546 | - [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
| 547 | - if (is_null($storage)) { |
|
| 548 | - return []; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - return $storage->getDirectDownload($internalPath); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * @param resource $data |
|
| 556 | - * @return null|string |
|
| 557 | - * @throws Exception |
|
| 558 | - * @throws BadRequest |
|
| 559 | - * @throws NotImplemented |
|
| 560 | - * @throws ServiceUnavailable |
|
| 561 | - */ |
|
| 562 | - private function createFileChunked($data) { |
|
| 563 | - [$path, $name] = \Sabre\Uri\split($this->path); |
|
| 564 | - |
|
| 565 | - $info = \OC_FileChunking::decodeName($name); |
|
| 566 | - if (empty($info)) { |
|
| 567 | - throw new NotImplemented($this->l10n->t('Invalid chunk name')); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - $chunk_handler = new \OC_FileChunking($info); |
|
| 571 | - $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 572 | - |
|
| 573 | - //detect aborted upload |
|
| 574 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 575 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 576 | - $expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
| 577 | - if ($bytesWritten !== $expected) { |
|
| 578 | - $chunk_handler->remove($info['index']); |
|
| 579 | - throw new BadRequest( |
|
| 580 | - $this->l10n->t( |
|
| 581 | - 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
|
| 582 | - [ |
|
| 583 | - $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 584 | - $this->l10n->n('%n byte', '%n bytes', $bytesWritten), |
|
| 585 | - ], |
|
| 586 | - ) |
|
| 587 | - ); |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - if ($chunk_handler->isComplete()) { |
|
| 593 | - /** @var Storage $storage */ |
|
| 594 | - [$storage,] = $this->fileView->resolvePath($path); |
|
| 595 | - $needsPartFile = $storage->needsPartFile(); |
|
| 596 | - $partFile = null; |
|
| 597 | - |
|
| 598 | - $targetPath = $path . '/' . $info['name']; |
|
| 599 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 600 | - [$targetStorage, $targetInternalPath] = $this->fileView->resolvePath($targetPath); |
|
| 601 | - |
|
| 602 | - $exists = $this->fileView->file_exists($targetPath); |
|
| 603 | - |
|
| 604 | - try { |
|
| 605 | - $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 606 | - |
|
| 607 | - $this->emitPreHooks($exists, $targetPath); |
|
| 608 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 609 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 610 | - [$targetStorage, $targetInternalPath] = $this->fileView->resolvePath($targetPath); |
|
| 611 | - |
|
| 612 | - if ($needsPartFile) { |
|
| 613 | - // we first assembly the target file as a part file |
|
| 614 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 615 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 616 | - [$partStorage, $partInternalPath] = $this->fileView->resolvePath($partFile); |
|
| 617 | - |
|
| 618 | - |
|
| 619 | - $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 620 | - |
|
| 621 | - // here is the final atomic rename |
|
| 622 | - $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 623 | - $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 624 | - if ($renameOkay === false || $fileExists === false) { |
|
| 625 | - \OC::$server->getLogger()->error('\OC\Files\Filesystem::rename() failed', ['app' => 'webdav']); |
|
| 626 | - // only delete if an error occurred and the target file was already created |
|
| 627 | - if ($fileExists) { |
|
| 628 | - // set to null to avoid double-deletion when handling exception |
|
| 629 | - // stray part file |
|
| 630 | - $partFile = null; |
|
| 631 | - $targetStorage->unlink($targetInternalPath); |
|
| 632 | - } |
|
| 633 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 634 | - throw new Exception($this->l10n->t('Could not rename part file assembled from chunks')); |
|
| 635 | - } |
|
| 636 | - } else { |
|
| 637 | - // assemble directly into the final file |
|
| 638 | - $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - // allow sync clients to send the mtime along in a header |
|
| 642 | - if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
| 643 | - $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
| 644 | - if ($targetStorage->touch($targetInternalPath, $mtime)) { |
|
| 645 | - $this->header('X-OC-MTime: accepted'); |
|
| 646 | - } |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 650 | - $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 651 | - |
|
| 652 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 653 | - |
|
| 654 | - $this->emitPostHooks($exists, $targetPath); |
|
| 655 | - |
|
| 656 | - // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 657 | - $info = $this->fileView->getFileInfo($targetPath); |
|
| 658 | - |
|
| 659 | - if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
| 660 | - $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
| 661 | - $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 662 | - } elseif ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 663 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 667 | - |
|
| 668 | - return $info->getEtag(); |
|
| 669 | - } catch (\Exception $e) { |
|
| 670 | - if ($partFile !== null) { |
|
| 671 | - $targetStorage->unlink($targetInternalPath); |
|
| 672 | - } |
|
| 673 | - $this->convertToSabreException($e); |
|
| 674 | - } |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - return null; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Convert the given exception to a SabreException instance |
|
| 682 | - * |
|
| 683 | - * @param \Exception $e |
|
| 684 | - * |
|
| 685 | - * @throws \Sabre\DAV\Exception |
|
| 686 | - */ |
|
| 687 | - private function convertToSabreException(\Exception $e) { |
|
| 688 | - if ($e instanceof \Sabre\DAV\Exception) { |
|
| 689 | - throw $e; |
|
| 690 | - } |
|
| 691 | - if ($e instanceof NotPermittedException) { |
|
| 692 | - // a more general case - due to whatever reason the content could not be written |
|
| 693 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 694 | - } |
|
| 695 | - if ($e instanceof ForbiddenException) { |
|
| 696 | - // the path for the file was forbidden |
|
| 697 | - throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 698 | - } |
|
| 699 | - if ($e instanceof EntityTooLargeException) { |
|
| 700 | - // the file is too big to be stored |
|
| 701 | - throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 702 | - } |
|
| 703 | - if ($e instanceof InvalidContentException) { |
|
| 704 | - // the file content is not permitted |
|
| 705 | - throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 706 | - } |
|
| 707 | - if ($e instanceof InvalidPathException) { |
|
| 708 | - // the path for the file was not valid |
|
| 709 | - // TODO: find proper http status code for this case |
|
| 710 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 711 | - } |
|
| 712 | - if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 713 | - // the file is currently being written to by another process |
|
| 714 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 715 | - } |
|
| 716 | - if ($e instanceof GenericEncryptionException) { |
|
| 717 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 718 | - throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e); |
|
| 719 | - } |
|
| 720 | - if ($e instanceof StorageNotAvailableException) { |
|
| 721 | - throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e); |
|
| 722 | - } |
|
| 723 | - if ($e instanceof NotFoundException) { |
|
| 724 | - throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Get the checksum for this file |
|
| 732 | - * |
|
| 733 | - * @return string|null |
|
| 734 | - */ |
|
| 735 | - public function getChecksum() { |
|
| 736 | - if (!$this->info) { |
|
| 737 | - return null; |
|
| 738 | - } |
|
| 739 | - return $this->info->getChecksum(); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - public function setChecksum(string $checksum) { |
|
| 743 | - $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 744 | - $this->refreshInfo(); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - protected function header($string) { |
|
| 748 | - if (!\OC::$CLI) { |
|
| 749 | - \header($string); |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - public function hash(string $type) { |
|
| 754 | - return $this->fileView->hash($type, $this->path); |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - public function getNode(): \OCP\Files\File { |
|
| 758 | - return $this->node; |
|
| 759 | - } |
|
| 79 | + protected $request; |
|
| 80 | + |
|
| 81 | + protected IL10N $l10n; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Sets up the node, expects a full path name |
|
| 85 | + * |
|
| 86 | + * @param \OC\Files\View $view |
|
| 87 | + * @param \OCP\Files\FileInfo $info |
|
| 88 | + * @param \OCP\Share\IManager $shareManager |
|
| 89 | + * @param \OC\AppFramework\Http\Request $request |
|
| 90 | + */ |
|
| 91 | + public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
|
| 92 | + parent::__construct($view, $info, $shareManager); |
|
| 93 | + |
|
| 94 | + // Querying IL10N directly results in a dependency loop |
|
| 95 | + /** @var IL10NFactory $l10nFactory */ |
|
| 96 | + $l10nFactory = \OC::$server->get(IL10NFactory::class); |
|
| 97 | + $this->l10n = $l10nFactory->get(Application::APP_ID); |
|
| 98 | + |
|
| 99 | + if (isset($request)) { |
|
| 100 | + $this->request = $request; |
|
| 101 | + } else { |
|
| 102 | + $this->request = \OC::$server->getRequest(); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Updates the data |
|
| 108 | + * |
|
| 109 | + * The data argument is a readable stream resource. |
|
| 110 | + * |
|
| 111 | + * After a successful put operation, you may choose to return an ETag. The |
|
| 112 | + * etag must always be surrounded by double-quotes. These quotes must |
|
| 113 | + * appear in the actual string you're returning. |
|
| 114 | + * |
|
| 115 | + * Clients may use the ETag from a PUT request to later on make sure that |
|
| 116 | + * when they update the file, the contents haven't changed in the mean |
|
| 117 | + * time. |
|
| 118 | + * |
|
| 119 | + * If you don't plan to store the file byte-by-byte, and you return a |
|
| 120 | + * different object on a subsequent GET you are strongly recommended to not |
|
| 121 | + * return an ETag, and just return null. |
|
| 122 | + * |
|
| 123 | + * @param resource $data |
|
| 124 | + * |
|
| 125 | + * @throws Forbidden |
|
| 126 | + * @throws UnsupportedMediaType |
|
| 127 | + * @throws BadRequest |
|
| 128 | + * @throws Exception |
|
| 129 | + * @throws EntityTooLarge |
|
| 130 | + * @throws ServiceUnavailable |
|
| 131 | + * @throws FileLocked |
|
| 132 | + * @return string|null |
|
| 133 | + */ |
|
| 134 | + public function put($data) { |
|
| 135 | + try { |
|
| 136 | + $exists = $this->fileView->file_exists($this->path); |
|
| 137 | + if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 138 | + throw new Forbidden(); |
|
| 139 | + } |
|
| 140 | + } catch (StorageNotAvailableException $e) { |
|
| 141 | + throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()])); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // verify path of the target |
|
| 145 | + $this->verifyPath(); |
|
| 146 | + |
|
| 147 | + // chunked handling |
|
| 148 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 149 | + try { |
|
| 150 | + return $this->createFileChunked($data); |
|
| 151 | + } catch (\Exception $e) { |
|
| 152 | + $this->convertToSabreException($e); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** @var Storage $partStorage */ |
|
| 157 | + [$partStorage] = $this->fileView->resolvePath($this->path); |
|
| 158 | + $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
|
| 159 | + |
|
| 160 | + $view = \OC\Files\Filesystem::getView(); |
|
| 161 | + |
|
| 162 | + if ($needsPartFile) { |
|
| 163 | + // mark file as partial while uploading (ignored by the scanner) |
|
| 164 | + $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 165 | + |
|
| 166 | + if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) { |
|
| 167 | + $needsPartFile = false; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + if (!$needsPartFile) { |
|
| 171 | + // upload file directly as the final path |
|
| 172 | + $partFilePath = $this->path; |
|
| 173 | + |
|
| 174 | + if ($view && !$this->emitPreHooks($exists)) { |
|
| 175 | + throw new Exception($this->l10n->t('Could not write to final file, canceled by hook')); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 180 | + /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 181 | + [$partStorage, $internalPartPath] = $this->fileView->resolvePath($partFilePath); |
|
| 182 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 183 | + [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
| 184 | + try { |
|
| 185 | + if (!$needsPartFile) { |
|
| 186 | + try { |
|
| 187 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 188 | + } catch (LockedException $e) { |
|
| 189 | + // during very large uploads, the shared lock we got at the start might have been expired |
|
| 190 | + // meaning that the above lock can fail not just only because somebody else got a shared lock |
|
| 191 | + // or because there is no existing shared lock to make exclusive |
|
| 192 | + // |
|
| 193 | + // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
| 194 | + // lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
| 195 | + // the entire operation will be safe |
|
| 196 | + |
|
| 197 | + try { |
|
| 198 | + $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 199 | + } catch (LockedException $ex) { |
|
| 200 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + if (!is_resource($data)) { |
|
| 206 | + $tmpData = fopen('php://temp', 'r+'); |
|
| 207 | + if ($data !== null) { |
|
| 208 | + fwrite($tmpData, $data); |
|
| 209 | + rewind($tmpData); |
|
| 210 | + } |
|
| 211 | + $data = $tmpData; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $data = HashWrapper::wrap($data, 'md5', function ($hash) { |
|
| 215 | + $this->header('X-Hash-MD5: ' . $hash); |
|
| 216 | + }); |
|
| 217 | + $data = HashWrapper::wrap($data, 'sha1', function ($hash) { |
|
| 218 | + $this->header('X-Hash-SHA1: ' . $hash); |
|
| 219 | + }); |
|
| 220 | + $data = HashWrapper::wrap($data, 'sha256', function ($hash) { |
|
| 221 | + $this->header('X-Hash-SHA256: ' . $hash); |
|
| 222 | + }); |
|
| 223 | + |
|
| 224 | + if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { |
|
| 225 | + $isEOF = false; |
|
| 226 | + $wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) { |
|
| 227 | + $isEOF = feof($stream); |
|
| 228 | + }); |
|
| 229 | + |
|
| 230 | + $result = true; |
|
| 231 | + $count = -1; |
|
| 232 | + try { |
|
| 233 | + $count = $partStorage->writeStream($internalPartPath, $wrappedData); |
|
| 234 | + } catch (GenericFileException $e) { |
|
| 235 | + $result = false; |
|
| 236 | + } catch (BadGateway $e) { |
|
| 237 | + throw $e; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + if ($result === false) { |
|
| 242 | + $result = $isEOF; |
|
| 243 | + if (is_resource($wrappedData)) { |
|
| 244 | + $result = feof($wrappedData); |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + } else { |
|
| 248 | + $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 249 | + if ($target === false) { |
|
| 250 | + \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
|
| 251 | + // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 252 | + throw new Exception($this->l10n->t('Could not write file contents')); |
|
| 253 | + } |
|
| 254 | + [$count, $result] = \OC_Helper::streamCopy($data, $target); |
|
| 255 | + fclose($target); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ($result === false) { |
|
| 259 | + $expected = -1; |
|
| 260 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 261 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 262 | + } |
|
| 263 | + if ($expected !== "0") { |
|
| 264 | + throw new Exception( |
|
| 265 | + $this->l10n->t( |
|
| 266 | + 'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)', |
|
| 267 | + [ |
|
| 268 | + $this->l10n->n('%n byte', '%n bytes', $count), |
|
| 269 | + $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 270 | + ], |
|
| 271 | + ) |
|
| 272 | + ); |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // if content length is sent by client: |
|
| 277 | + // double check if the file was fully received |
|
| 278 | + // compare expected and actual size |
|
| 279 | + if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 280 | + $expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
| 281 | + if ($count !== $expected) { |
|
| 282 | + throw new BadRequest( |
|
| 283 | + $this->l10n->t( |
|
| 284 | + 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
|
| 285 | + [ |
|
| 286 | + $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 287 | + $this->l10n->n('%n byte', '%n bytes', $count), |
|
| 288 | + ], |
|
| 289 | + ) |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + } catch (\Exception $e) { |
|
| 294 | + $context = []; |
|
| 295 | + |
|
| 296 | + if ($e instanceof LockedException) { |
|
| 297 | + $context['level'] = ILogger::DEBUG; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + \OC::$server->getLogger()->logException($e, $context); |
|
| 301 | + if ($needsPartFile) { |
|
| 302 | + $partStorage->unlink($internalPartPath); |
|
| 303 | + } |
|
| 304 | + $this->convertToSabreException($e); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + try { |
|
| 308 | + if ($needsPartFile) { |
|
| 309 | + if ($view && !$this->emitPreHooks($exists)) { |
|
| 310 | + $partStorage->unlink($internalPartPath); |
|
| 311 | + throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook')); |
|
| 312 | + } |
|
| 313 | + try { |
|
| 314 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 315 | + } catch (LockedException $e) { |
|
| 316 | + // during very large uploads, the shared lock we got at the start might have been expired |
|
| 317 | + // meaning that the above lock can fail not just only because somebody else got a shared lock |
|
| 318 | + // or because there is no existing shared lock to make exclusive |
|
| 319 | + // |
|
| 320 | + // Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
| 321 | + // lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
| 322 | + // the entire operation will be safe |
|
| 323 | + |
|
| 324 | + try { |
|
| 325 | + $this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 326 | + } catch (LockedException $ex) { |
|
| 327 | + if ($needsPartFile) { |
|
| 328 | + $partStorage->unlink($internalPartPath); |
|
| 329 | + } |
|
| 330 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // rename to correct path |
|
| 335 | + try { |
|
| 336 | + $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 337 | + $fileExists = $storage->file_exists($internalPath); |
|
| 338 | + if ($renameOkay === false || $fileExists === false) { |
|
| 339 | + \OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']); |
|
| 340 | + throw new Exception($this->l10n->t('Could not rename part file to final file')); |
|
| 341 | + } |
|
| 342 | + } catch (ForbiddenException $ex) { |
|
| 343 | + if (!$ex->getRetry()) { |
|
| 344 | + $partStorage->unlink($internalPartPath); |
|
| 345 | + } |
|
| 346 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 347 | + } catch (\Exception $e) { |
|
| 348 | + $partStorage->unlink($internalPartPath); |
|
| 349 | + $this->convertToSabreException($e); |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 354 | + $storage->getUpdater()->update($internalPath); |
|
| 355 | + |
|
| 356 | + try { |
|
| 357 | + $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 358 | + } catch (LockedException $e) { |
|
| 359 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + // allow sync clients to send the mtime along in a header |
|
| 363 | + if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
| 364 | + $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
| 365 | + if ($this->fileView->touch($this->path, $mtime)) { |
|
| 366 | + $this->header('X-OC-MTime: accepted'); |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $fileInfoUpdate = [ |
|
| 371 | + 'upload_time' => time() |
|
| 372 | + ]; |
|
| 373 | + |
|
| 374 | + // allow sync clients to send the creation time along in a header |
|
| 375 | + if (isset($this->request->server['HTTP_X_OC_CTIME'])) { |
|
| 376 | + $ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']); |
|
| 377 | + $fileInfoUpdate['creation_time'] = $ctime; |
|
| 378 | + $this->header('X-OC-CTime: accepted'); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + $this->fileView->putFileInfo($this->path, $fileInfoUpdate); |
|
| 382 | + |
|
| 383 | + if ($view) { |
|
| 384 | + $this->emitPostHooks($exists); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + $this->refreshInfo(); |
|
| 388 | + |
|
| 389 | + if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
| 390 | + $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
| 391 | + $this->setChecksum($checksum); |
|
| 392 | + } elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 393 | + $this->setChecksum(''); |
|
| 394 | + } |
|
| 395 | + } catch (StorageNotAvailableException $e) { |
|
| 396 | + throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return '"' . $this->info->getEtag() . '"'; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + private function getPartFileBasePath($path) { |
|
| 403 | + $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 404 | + if ($partFileInStorage) { |
|
| 405 | + return $path; |
|
| 406 | + } else { |
|
| 407 | + return md5($path); // will place it in the root of the view with a unique name |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * @param string $path |
|
| 413 | + */ |
|
| 414 | + private function emitPreHooks($exists, $path = null) { |
|
| 415 | + if (is_null($path)) { |
|
| 416 | + $path = $this->path; |
|
| 417 | + } |
|
| 418 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 419 | + $run = true; |
|
| 420 | + |
|
| 421 | + if (!$exists) { |
|
| 422 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, [ |
|
| 423 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 424 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 425 | + ]); |
|
| 426 | + } else { |
|
| 427 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, [ |
|
| 428 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 429 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 430 | + ]); |
|
| 431 | + } |
|
| 432 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, [ |
|
| 433 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 434 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 435 | + ]); |
|
| 436 | + return $run; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * @param string $path |
|
| 441 | + */ |
|
| 442 | + private function emitPostHooks($exists, $path = null) { |
|
| 443 | + if (is_null($path)) { |
|
| 444 | + $path = $this->path; |
|
| 445 | + } |
|
| 446 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 447 | + if (!$exists) { |
|
| 448 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, [ |
|
| 449 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 450 | + ]); |
|
| 451 | + } else { |
|
| 452 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, [ |
|
| 453 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 454 | + ]); |
|
| 455 | + } |
|
| 456 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, [ |
|
| 457 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 458 | + ]); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Returns the data |
|
| 463 | + * |
|
| 464 | + * @return resource |
|
| 465 | + * @throws Forbidden |
|
| 466 | + * @throws ServiceUnavailable |
|
| 467 | + */ |
|
| 468 | + public function get() { |
|
| 469 | + //throw exception if encryption is disabled but files are still encrypted |
|
| 470 | + try { |
|
| 471 | + if (!$this->info->isReadable()) { |
|
| 472 | + // do a if the file did not exist |
|
| 473 | + throw new NotFound(); |
|
| 474 | + } |
|
| 475 | + try { |
|
| 476 | + $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 477 | + } catch (\Exception $e) { |
|
| 478 | + $this->convertToSabreException($e); |
|
| 479 | + } |
|
| 480 | + if ($res === false) { |
|
| 481 | + throw new ServiceUnavailable($this->l10n->t('Could not open file')); |
|
| 482 | + } |
|
| 483 | + return $res; |
|
| 484 | + } catch (GenericEncryptionException $e) { |
|
| 485 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 486 | + throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()])); |
|
| 487 | + } catch (StorageNotAvailableException $e) { |
|
| 488 | + throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()])); |
|
| 489 | + } catch (ForbiddenException $ex) { |
|
| 490 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 491 | + } catch (LockedException $e) { |
|
| 492 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Delete the current file |
|
| 498 | + * |
|
| 499 | + * @throws Forbidden |
|
| 500 | + * @throws ServiceUnavailable |
|
| 501 | + */ |
|
| 502 | + public function delete() { |
|
| 503 | + if (!$this->info->isDeletable()) { |
|
| 504 | + throw new Forbidden(); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + try { |
|
| 508 | + if (!$this->fileView->unlink($this->path)) { |
|
| 509 | + // assume it wasn't possible to delete due to permissions |
|
| 510 | + throw new Forbidden(); |
|
| 511 | + } |
|
| 512 | + } catch (StorageNotAvailableException $e) { |
|
| 513 | + throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()])); |
|
| 514 | + } catch (ForbiddenException $ex) { |
|
| 515 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 516 | + } catch (LockedException $e) { |
|
| 517 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Returns the mime-type for a file |
|
| 523 | + * |
|
| 524 | + * If null is returned, we'll assume application/octet-stream |
|
| 525 | + * |
|
| 526 | + * @return string |
|
| 527 | + */ |
|
| 528 | + public function getContentType() { |
|
| 529 | + $mimeType = $this->info->getMimetype(); |
|
| 530 | + |
|
| 531 | + // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 532 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 533 | + return $mimeType; |
|
| 534 | + } |
|
| 535 | + return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * @return array|bool |
|
| 540 | + */ |
|
| 541 | + public function getDirectDownload() { |
|
| 542 | + if (\OCP\App::isEnabled('encryption')) { |
|
| 543 | + return []; |
|
| 544 | + } |
|
| 545 | + /** @var \OCP\Files\Storage $storage */ |
|
| 546 | + [$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
| 547 | + if (is_null($storage)) { |
|
| 548 | + return []; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + return $storage->getDirectDownload($internalPath); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * @param resource $data |
|
| 556 | + * @return null|string |
|
| 557 | + * @throws Exception |
|
| 558 | + * @throws BadRequest |
|
| 559 | + * @throws NotImplemented |
|
| 560 | + * @throws ServiceUnavailable |
|
| 561 | + */ |
|
| 562 | + private function createFileChunked($data) { |
|
| 563 | + [$path, $name] = \Sabre\Uri\split($this->path); |
|
| 564 | + |
|
| 565 | + $info = \OC_FileChunking::decodeName($name); |
|
| 566 | + if (empty($info)) { |
|
| 567 | + throw new NotImplemented($this->l10n->t('Invalid chunk name')); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + $chunk_handler = new \OC_FileChunking($info); |
|
| 571 | + $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 572 | + |
|
| 573 | + //detect aborted upload |
|
| 574 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 575 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 576 | + $expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
| 577 | + if ($bytesWritten !== $expected) { |
|
| 578 | + $chunk_handler->remove($info['index']); |
|
| 579 | + throw new BadRequest( |
|
| 580 | + $this->l10n->t( |
|
| 581 | + 'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
|
| 582 | + [ |
|
| 583 | + $this->l10n->n('%n byte', '%n bytes', $expected), |
|
| 584 | + $this->l10n->n('%n byte', '%n bytes', $bytesWritten), |
|
| 585 | + ], |
|
| 586 | + ) |
|
| 587 | + ); |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + if ($chunk_handler->isComplete()) { |
|
| 593 | + /** @var Storage $storage */ |
|
| 594 | + [$storage,] = $this->fileView->resolvePath($path); |
|
| 595 | + $needsPartFile = $storage->needsPartFile(); |
|
| 596 | + $partFile = null; |
|
| 597 | + |
|
| 598 | + $targetPath = $path . '/' . $info['name']; |
|
| 599 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 600 | + [$targetStorage, $targetInternalPath] = $this->fileView->resolvePath($targetPath); |
|
| 601 | + |
|
| 602 | + $exists = $this->fileView->file_exists($targetPath); |
|
| 603 | + |
|
| 604 | + try { |
|
| 605 | + $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 606 | + |
|
| 607 | + $this->emitPreHooks($exists, $targetPath); |
|
| 608 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 609 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 610 | + [$targetStorage, $targetInternalPath] = $this->fileView->resolvePath($targetPath); |
|
| 611 | + |
|
| 612 | + if ($needsPartFile) { |
|
| 613 | + // we first assembly the target file as a part file |
|
| 614 | + $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 615 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 616 | + [$partStorage, $partInternalPath] = $this->fileView->resolvePath($partFile); |
|
| 617 | + |
|
| 618 | + |
|
| 619 | + $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 620 | + |
|
| 621 | + // here is the final atomic rename |
|
| 622 | + $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 623 | + $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 624 | + if ($renameOkay === false || $fileExists === false) { |
|
| 625 | + \OC::$server->getLogger()->error('\OC\Files\Filesystem::rename() failed', ['app' => 'webdav']); |
|
| 626 | + // only delete if an error occurred and the target file was already created |
|
| 627 | + if ($fileExists) { |
|
| 628 | + // set to null to avoid double-deletion when handling exception |
|
| 629 | + // stray part file |
|
| 630 | + $partFile = null; |
|
| 631 | + $targetStorage->unlink($targetInternalPath); |
|
| 632 | + } |
|
| 633 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 634 | + throw new Exception($this->l10n->t('Could not rename part file assembled from chunks')); |
|
| 635 | + } |
|
| 636 | + } else { |
|
| 637 | + // assemble directly into the final file |
|
| 638 | + $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + // allow sync clients to send the mtime along in a header |
|
| 642 | + if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
| 643 | + $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
| 644 | + if ($targetStorage->touch($targetInternalPath, $mtime)) { |
|
| 645 | + $this->header('X-OC-MTime: accepted'); |
|
| 646 | + } |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 650 | + $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 651 | + |
|
| 652 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 653 | + |
|
| 654 | + $this->emitPostHooks($exists, $targetPath); |
|
| 655 | + |
|
| 656 | + // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 657 | + $info = $this->fileView->getFileInfo($targetPath); |
|
| 658 | + |
|
| 659 | + if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
| 660 | + $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
| 661 | + $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 662 | + } elseif ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 663 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 667 | + |
|
| 668 | + return $info->getEtag(); |
|
| 669 | + } catch (\Exception $e) { |
|
| 670 | + if ($partFile !== null) { |
|
| 671 | + $targetStorage->unlink($targetInternalPath); |
|
| 672 | + } |
|
| 673 | + $this->convertToSabreException($e); |
|
| 674 | + } |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + return null; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Convert the given exception to a SabreException instance |
|
| 682 | + * |
|
| 683 | + * @param \Exception $e |
|
| 684 | + * |
|
| 685 | + * @throws \Sabre\DAV\Exception |
|
| 686 | + */ |
|
| 687 | + private function convertToSabreException(\Exception $e) { |
|
| 688 | + if ($e instanceof \Sabre\DAV\Exception) { |
|
| 689 | + throw $e; |
|
| 690 | + } |
|
| 691 | + if ($e instanceof NotPermittedException) { |
|
| 692 | + // a more general case - due to whatever reason the content could not be written |
|
| 693 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 694 | + } |
|
| 695 | + if ($e instanceof ForbiddenException) { |
|
| 696 | + // the path for the file was forbidden |
|
| 697 | + throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 698 | + } |
|
| 699 | + if ($e instanceof EntityTooLargeException) { |
|
| 700 | + // the file is too big to be stored |
|
| 701 | + throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 702 | + } |
|
| 703 | + if ($e instanceof InvalidContentException) { |
|
| 704 | + // the file content is not permitted |
|
| 705 | + throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 706 | + } |
|
| 707 | + if ($e instanceof InvalidPathException) { |
|
| 708 | + // the path for the file was not valid |
|
| 709 | + // TODO: find proper http status code for this case |
|
| 710 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 711 | + } |
|
| 712 | + if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 713 | + // the file is currently being written to by another process |
|
| 714 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 715 | + } |
|
| 716 | + if ($e instanceof GenericEncryptionException) { |
|
| 717 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 718 | + throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e); |
|
| 719 | + } |
|
| 720 | + if ($e instanceof StorageNotAvailableException) { |
|
| 721 | + throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e); |
|
| 722 | + } |
|
| 723 | + if ($e instanceof NotFoundException) { |
|
| 724 | + throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Get the checksum for this file |
|
| 732 | + * |
|
| 733 | + * @return string|null |
|
| 734 | + */ |
|
| 735 | + public function getChecksum() { |
|
| 736 | + if (!$this->info) { |
|
| 737 | + return null; |
|
| 738 | + } |
|
| 739 | + return $this->info->getChecksum(); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + public function setChecksum(string $checksum) { |
|
| 743 | + $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 744 | + $this->refreshInfo(); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + protected function header($string) { |
|
| 748 | + if (!\OC::$CLI) { |
|
| 749 | + \header($string); |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + public function hash(string $type) { |
|
| 754 | + return $this->fileView->hash($type, $this->path); |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + public function getNode(): \OCP\Files\File { |
|
| 758 | + return $this->node; |
|
| 759 | + } |
|
| 760 | 760 | } |
@@ -49,386 +49,386 @@ |
||
| 49 | 49 | |
| 50 | 50 | abstract class Node implements \Sabre\DAV\INode { |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @var \OC\Files\View |
|
| 54 | - */ |
|
| 55 | - protected $fileView; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The path to the current node |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $path; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * node properties cache |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $property_cache = null; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var \OCP\Files\FileInfo |
|
| 73 | - */ |
|
| 74 | - protected $info; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var IManager |
|
| 78 | - */ |
|
| 79 | - protected $shareManager; |
|
| 80 | - |
|
| 81 | - protected \OCP\Files\Node $node; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Sets up the node, expects a full path name |
|
| 85 | - * |
|
| 86 | - * @param \OC\Files\View $view |
|
| 87 | - * @param \OCP\Files\FileInfo $info |
|
| 88 | - * @param IManager $shareManager |
|
| 89 | - */ |
|
| 90 | - public function __construct(View $view, FileInfo $info, IManager $shareManager = null) { |
|
| 91 | - $this->fileView = $view; |
|
| 92 | - $this->path = $this->fileView->getRelativePath($info->getPath()); |
|
| 93 | - $this->info = $info; |
|
| 94 | - if ($shareManager) { |
|
| 95 | - $this->shareManager = $shareManager; |
|
| 96 | - } else { |
|
| 97 | - $this->shareManager = \OC::$server->getShareManager(); |
|
| 98 | - } |
|
| 99 | - if ($info instanceof Folder || $info instanceof File) { |
|
| 100 | - $this->node = $info; |
|
| 101 | - } else { |
|
| 102 | - $root = \OC::$server->get(IRootFolder::class); |
|
| 103 | - if ($info->getType() === FileInfo::TYPE_FOLDER) { |
|
| 104 | - $this->node = new Folder($root, $view, $this->path, $info); |
|
| 105 | - } else { |
|
| 106 | - $this->node = new File($root, $view, $this->path, $info); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - protected function refreshInfo() { |
|
| 112 | - $this->info = $this->fileView->getFileInfo($this->path); |
|
| 113 | - $root = \OC::$server->get(IRootFolder::class); |
|
| 114 | - if ($this->info->getType() === FileInfo::TYPE_FOLDER) { |
|
| 115 | - $this->node = new Folder($root, $this->fileView, $this->path, $this->info); |
|
| 116 | - } else { |
|
| 117 | - $this->node = new File($root, $this->fileView, $this->path, $this->info); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Returns the name of the node |
|
| 123 | - * |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public function getName() { |
|
| 127 | - return $this->info->getName(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Returns the full path |
|
| 132 | - * |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function getPath() { |
|
| 136 | - return $this->path; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Renames the node |
|
| 141 | - * |
|
| 142 | - * @param string $name The new name |
|
| 143 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 144 | - * @throws \Sabre\DAV\Exception\Forbidden |
|
| 145 | - */ |
|
| 146 | - public function setName($name) { |
|
| 147 | - |
|
| 148 | - // rename is only allowed if the update privilege is granted |
|
| 149 | - if (!($this->info->isUpdateable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) { |
|
| 150 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - [$parentPath,] = \Sabre\Uri\split($this->path); |
|
| 154 | - [, $newName] = \Sabre\Uri\split($name); |
|
| 155 | - |
|
| 156 | - // verify path of the target |
|
| 157 | - $this->verifyPath(); |
|
| 158 | - |
|
| 159 | - $newPath = $parentPath . '/' . $newName; |
|
| 160 | - |
|
| 161 | - if (!$this->fileView->rename($this->path, $newPath)) { |
|
| 162 | - throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $this->path = $newPath; |
|
| 166 | - |
|
| 167 | - $this->refreshInfo(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - public function setPropertyCache($property_cache) { |
|
| 171 | - $this->property_cache = $property_cache; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Returns the last modification time, as a unix timestamp |
|
| 176 | - * |
|
| 177 | - * @return int timestamp as integer |
|
| 178 | - */ |
|
| 179 | - public function getLastModified() { |
|
| 180 | - $timestamp = $this->info->getMtime(); |
|
| 181 | - if (!empty($timestamp)) { |
|
| 182 | - return (int)$timestamp; |
|
| 183 | - } |
|
| 184 | - return $timestamp; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * sets the last modification time of the file (mtime) to the value given |
|
| 189 | - * in the second parameter or to now if the second param is empty. |
|
| 190 | - * Even if the modification time is set to a custom value the access time is set to now. |
|
| 191 | - */ |
|
| 192 | - public function touch($mtime) { |
|
| 193 | - $mtime = $this->sanitizeMtime($mtime); |
|
| 194 | - $this->fileView->touch($this->path, $mtime); |
|
| 195 | - $this->refreshInfo(); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Returns the ETag for a file |
|
| 200 | - * |
|
| 201 | - * An ETag is a unique identifier representing the current version of the |
|
| 202 | - * file. If the file changes, the ETag MUST change. The ETag is an |
|
| 203 | - * arbitrary string, but MUST be surrounded by double-quotes. |
|
| 204 | - * |
|
| 205 | - * Return null if the ETag can not effectively be determined |
|
| 206 | - * |
|
| 207 | - * @return string |
|
| 208 | - */ |
|
| 209 | - public function getETag() { |
|
| 210 | - return '"' . $this->info->getEtag() . '"'; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Sets the ETag |
|
| 215 | - * |
|
| 216 | - * @param string $etag |
|
| 217 | - * |
|
| 218 | - * @return int file id of updated file or -1 on failure |
|
| 219 | - */ |
|
| 220 | - public function setETag($etag) { |
|
| 221 | - return $this->fileView->putFileInfo($this->path, ['etag' => $etag]); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function setCreationTime(int $time) { |
|
| 225 | - return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - public function setUploadTime(int $time) { |
|
| 229 | - return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Returns the size of the node, in bytes |
|
| 234 | - * |
|
| 235 | - * @return integer |
|
| 236 | - */ |
|
| 237 | - public function getSize() { |
|
| 238 | - return $this->info->getSize(); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Returns the cache's file id |
|
| 243 | - * |
|
| 244 | - * @return int |
|
| 245 | - */ |
|
| 246 | - public function getId() { |
|
| 247 | - return $this->info->getId(); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @return string|null |
|
| 252 | - */ |
|
| 253 | - public function getFileId() { |
|
| 254 | - if ($this->info->getId()) { |
|
| 255 | - $instanceId = \OC_Util::getInstanceId(); |
|
| 256 | - $id = sprintf('%08d', $this->info->getId()); |
|
| 257 | - return $id . $instanceId; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return null; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @return integer |
|
| 265 | - */ |
|
| 266 | - public function getInternalFileId() { |
|
| 267 | - return $this->info->getId(); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $user |
|
| 272 | - * @return int |
|
| 273 | - */ |
|
| 274 | - public function getSharePermissions($user) { |
|
| 275 | - |
|
| 276 | - // check of we access a federated share |
|
| 277 | - if ($user !== null) { |
|
| 278 | - try { |
|
| 279 | - $share = $this->shareManager->getShareByToken($user); |
|
| 280 | - return $share->getPermissions(); |
|
| 281 | - } catch (ShareNotFound $e) { |
|
| 282 | - // ignore |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - try { |
|
| 287 | - $storage = $this->info->getStorage(); |
|
| 288 | - } catch (StorageNotAvailableException $e) { |
|
| 289 | - $storage = null; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
| 293 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 294 | - $permissions = (int)$storage->getShare()->getPermissions(); |
|
| 295 | - } else { |
|
| 296 | - $permissions = $this->info->getPermissions(); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /* |
|
| 52 | + /** |
|
| 53 | + * @var \OC\Files\View |
|
| 54 | + */ |
|
| 55 | + protected $fileView; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The path to the current node |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $path; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * node properties cache |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $property_cache = null; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var \OCP\Files\FileInfo |
|
| 73 | + */ |
|
| 74 | + protected $info; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var IManager |
|
| 78 | + */ |
|
| 79 | + protected $shareManager; |
|
| 80 | + |
|
| 81 | + protected \OCP\Files\Node $node; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Sets up the node, expects a full path name |
|
| 85 | + * |
|
| 86 | + * @param \OC\Files\View $view |
|
| 87 | + * @param \OCP\Files\FileInfo $info |
|
| 88 | + * @param IManager $shareManager |
|
| 89 | + */ |
|
| 90 | + public function __construct(View $view, FileInfo $info, IManager $shareManager = null) { |
|
| 91 | + $this->fileView = $view; |
|
| 92 | + $this->path = $this->fileView->getRelativePath($info->getPath()); |
|
| 93 | + $this->info = $info; |
|
| 94 | + if ($shareManager) { |
|
| 95 | + $this->shareManager = $shareManager; |
|
| 96 | + } else { |
|
| 97 | + $this->shareManager = \OC::$server->getShareManager(); |
|
| 98 | + } |
|
| 99 | + if ($info instanceof Folder || $info instanceof File) { |
|
| 100 | + $this->node = $info; |
|
| 101 | + } else { |
|
| 102 | + $root = \OC::$server->get(IRootFolder::class); |
|
| 103 | + if ($info->getType() === FileInfo::TYPE_FOLDER) { |
|
| 104 | + $this->node = new Folder($root, $view, $this->path, $info); |
|
| 105 | + } else { |
|
| 106 | + $this->node = new File($root, $view, $this->path, $info); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + protected function refreshInfo() { |
|
| 112 | + $this->info = $this->fileView->getFileInfo($this->path); |
|
| 113 | + $root = \OC::$server->get(IRootFolder::class); |
|
| 114 | + if ($this->info->getType() === FileInfo::TYPE_FOLDER) { |
|
| 115 | + $this->node = new Folder($root, $this->fileView, $this->path, $this->info); |
|
| 116 | + } else { |
|
| 117 | + $this->node = new File($root, $this->fileView, $this->path, $this->info); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Returns the name of the node |
|
| 123 | + * |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public function getName() { |
|
| 127 | + return $this->info->getName(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Returns the full path |
|
| 132 | + * |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function getPath() { |
|
| 136 | + return $this->path; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Renames the node |
|
| 141 | + * |
|
| 142 | + * @param string $name The new name |
|
| 143 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 144 | + * @throws \Sabre\DAV\Exception\Forbidden |
|
| 145 | + */ |
|
| 146 | + public function setName($name) { |
|
| 147 | + |
|
| 148 | + // rename is only allowed if the update privilege is granted |
|
| 149 | + if (!($this->info->isUpdateable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) { |
|
| 150 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + [$parentPath,] = \Sabre\Uri\split($this->path); |
|
| 154 | + [, $newName] = \Sabre\Uri\split($name); |
|
| 155 | + |
|
| 156 | + // verify path of the target |
|
| 157 | + $this->verifyPath(); |
|
| 158 | + |
|
| 159 | + $newPath = $parentPath . '/' . $newName; |
|
| 160 | + |
|
| 161 | + if (!$this->fileView->rename($this->path, $newPath)) { |
|
| 162 | + throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $this->path = $newPath; |
|
| 166 | + |
|
| 167 | + $this->refreshInfo(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + public function setPropertyCache($property_cache) { |
|
| 171 | + $this->property_cache = $property_cache; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Returns the last modification time, as a unix timestamp |
|
| 176 | + * |
|
| 177 | + * @return int timestamp as integer |
|
| 178 | + */ |
|
| 179 | + public function getLastModified() { |
|
| 180 | + $timestamp = $this->info->getMtime(); |
|
| 181 | + if (!empty($timestamp)) { |
|
| 182 | + return (int)$timestamp; |
|
| 183 | + } |
|
| 184 | + return $timestamp; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * sets the last modification time of the file (mtime) to the value given |
|
| 189 | + * in the second parameter or to now if the second param is empty. |
|
| 190 | + * Even if the modification time is set to a custom value the access time is set to now. |
|
| 191 | + */ |
|
| 192 | + public function touch($mtime) { |
|
| 193 | + $mtime = $this->sanitizeMtime($mtime); |
|
| 194 | + $this->fileView->touch($this->path, $mtime); |
|
| 195 | + $this->refreshInfo(); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Returns the ETag for a file |
|
| 200 | + * |
|
| 201 | + * An ETag is a unique identifier representing the current version of the |
|
| 202 | + * file. If the file changes, the ETag MUST change. The ETag is an |
|
| 203 | + * arbitrary string, but MUST be surrounded by double-quotes. |
|
| 204 | + * |
|
| 205 | + * Return null if the ETag can not effectively be determined |
|
| 206 | + * |
|
| 207 | + * @return string |
|
| 208 | + */ |
|
| 209 | + public function getETag() { |
|
| 210 | + return '"' . $this->info->getEtag() . '"'; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Sets the ETag |
|
| 215 | + * |
|
| 216 | + * @param string $etag |
|
| 217 | + * |
|
| 218 | + * @return int file id of updated file or -1 on failure |
|
| 219 | + */ |
|
| 220 | + public function setETag($etag) { |
|
| 221 | + return $this->fileView->putFileInfo($this->path, ['etag' => $etag]); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function setCreationTime(int $time) { |
|
| 225 | + return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + public function setUploadTime(int $time) { |
|
| 229 | + return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Returns the size of the node, in bytes |
|
| 234 | + * |
|
| 235 | + * @return integer |
|
| 236 | + */ |
|
| 237 | + public function getSize() { |
|
| 238 | + return $this->info->getSize(); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Returns the cache's file id |
|
| 243 | + * |
|
| 244 | + * @return int |
|
| 245 | + */ |
|
| 246 | + public function getId() { |
|
| 247 | + return $this->info->getId(); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @return string|null |
|
| 252 | + */ |
|
| 253 | + public function getFileId() { |
|
| 254 | + if ($this->info->getId()) { |
|
| 255 | + $instanceId = \OC_Util::getInstanceId(); |
|
| 256 | + $id = sprintf('%08d', $this->info->getId()); |
|
| 257 | + return $id . $instanceId; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return null; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @return integer |
|
| 265 | + */ |
|
| 266 | + public function getInternalFileId() { |
|
| 267 | + return $this->info->getId(); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $user |
|
| 272 | + * @return int |
|
| 273 | + */ |
|
| 274 | + public function getSharePermissions($user) { |
|
| 275 | + |
|
| 276 | + // check of we access a federated share |
|
| 277 | + if ($user !== null) { |
|
| 278 | + try { |
|
| 279 | + $share = $this->shareManager->getShareByToken($user); |
|
| 280 | + return $share->getPermissions(); |
|
| 281 | + } catch (ShareNotFound $e) { |
|
| 282 | + // ignore |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + try { |
|
| 287 | + $storage = $this->info->getStorage(); |
|
| 288 | + } catch (StorageNotAvailableException $e) { |
|
| 289 | + $storage = null; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
| 293 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
| 294 | + $permissions = (int)$storage->getShare()->getPermissions(); |
|
| 295 | + } else { |
|
| 296 | + $permissions = $this->info->getPermissions(); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /* |
|
| 300 | 300 | * We can always share non moveable mount points with DELETE and UPDATE |
| 301 | 301 | * Eventually we need to do this properly |
| 302 | 302 | */ |
| 303 | - $mountpoint = $this->info->getMountPoint(); |
|
| 304 | - if (!($mountpoint instanceof MoveableMount)) { |
|
| 305 | - $mountpointpath = $mountpoint->getMountPoint(); |
|
| 306 | - if (substr($mountpointpath, -1) === '/') { |
|
| 307 | - $mountpointpath = substr($mountpointpath, 0, -1); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) { |
|
| 311 | - $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /* |
|
| 303 | + $mountpoint = $this->info->getMountPoint(); |
|
| 304 | + if (!($mountpoint instanceof MoveableMount)) { |
|
| 305 | + $mountpointpath = $mountpoint->getMountPoint(); |
|
| 306 | + if (substr($mountpointpath, -1) === '/') { |
|
| 307 | + $mountpointpath = substr($mountpointpath, 0, -1); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) { |
|
| 311 | + $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /* |
|
| 316 | 316 | * Files can't have create or delete permissions |
| 317 | 317 | */ |
| 318 | - if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
|
| 319 | - $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - return $permissions; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * @param string $user |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - public function getNoteFromShare($user) { |
|
| 330 | - if ($user === null) { |
|
| 331 | - return ''; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $types = [ |
|
| 335 | - IShare::TYPE_USER, |
|
| 336 | - IShare::TYPE_GROUP, |
|
| 337 | - IShare::TYPE_CIRCLE, |
|
| 338 | - IShare::TYPE_ROOM |
|
| 339 | - ]; |
|
| 340 | - |
|
| 341 | - foreach ($types as $shareType) { |
|
| 342 | - $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1); |
|
| 343 | - foreach ($shares as $share) { |
|
| 344 | - $note = $share->getNote(); |
|
| 345 | - if ($share->getShareOwner() !== $user && !empty($note)) { |
|
| 346 | - return $note; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - return ''; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - public function getDavPermissions() { |
|
| 358 | - $p = ''; |
|
| 359 | - if ($this->info->isShared()) { |
|
| 360 | - $p .= 'S'; |
|
| 361 | - } |
|
| 362 | - if ($this->info->isShareable()) { |
|
| 363 | - $p .= 'R'; |
|
| 364 | - } |
|
| 365 | - if ($this->info->isMounted()) { |
|
| 366 | - $p .= 'M'; |
|
| 367 | - } |
|
| 368 | - if ($this->info->isReadable()) { |
|
| 369 | - $p .= 'G'; |
|
| 370 | - } |
|
| 371 | - if ($this->info->isDeletable()) { |
|
| 372 | - $p .= 'D'; |
|
| 373 | - } |
|
| 374 | - if ($this->info->isUpdateable()) { |
|
| 375 | - $p .= 'NV'; // Renameable, Moveable |
|
| 376 | - } |
|
| 377 | - if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
|
| 378 | - if ($this->info->isUpdateable()) { |
|
| 379 | - $p .= 'W'; |
|
| 380 | - } |
|
| 381 | - } else { |
|
| 382 | - if ($this->info->isCreatable()) { |
|
| 383 | - $p .= 'CK'; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - return $p; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - public function getOwner() { |
|
| 390 | - return $this->info->getOwner(); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - protected function verifyPath() { |
|
| 394 | - try { |
|
| 395 | - $fileName = basename($this->info->getPath()); |
|
| 396 | - $this->fileView->verifyPath($this->path, $fileName); |
|
| 397 | - } catch (\OCP\Files\InvalidPathException $ex) { |
|
| 398 | - throw new InvalidPath($ex->getMessage()); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 404 | - */ |
|
| 405 | - public function acquireLock($type) { |
|
| 406 | - $this->fileView->lockFile($this->path, $type); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 411 | - */ |
|
| 412 | - public function releaseLock($type) { |
|
| 413 | - $this->fileView->unlockFile($this->path, $type); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 418 | - */ |
|
| 419 | - public function changeLock($type) { |
|
| 420 | - $this->fileView->changeLock($this->path, $type); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - public function getFileInfo() { |
|
| 424 | - return $this->info; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - public function getNode(): \OCP\Files\Node { |
|
| 428 | - return $this->node; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - protected function sanitizeMtime($mtimeFromRequest) { |
|
| 432 | - return MtimeSanitizer::sanitizeMtime($mtimeFromRequest); |
|
| 433 | - } |
|
| 318 | + if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
|
| 319 | + $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + return $permissions; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * @param string $user |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + public function getNoteFromShare($user) { |
|
| 330 | + if ($user === null) { |
|
| 331 | + return ''; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $types = [ |
|
| 335 | + IShare::TYPE_USER, |
|
| 336 | + IShare::TYPE_GROUP, |
|
| 337 | + IShare::TYPE_CIRCLE, |
|
| 338 | + IShare::TYPE_ROOM |
|
| 339 | + ]; |
|
| 340 | + |
|
| 341 | + foreach ($types as $shareType) { |
|
| 342 | + $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1); |
|
| 343 | + foreach ($shares as $share) { |
|
| 344 | + $note = $share->getNote(); |
|
| 345 | + if ($share->getShareOwner() !== $user && !empty($note)) { |
|
| 346 | + return $note; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + return ''; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + public function getDavPermissions() { |
|
| 358 | + $p = ''; |
|
| 359 | + if ($this->info->isShared()) { |
|
| 360 | + $p .= 'S'; |
|
| 361 | + } |
|
| 362 | + if ($this->info->isShareable()) { |
|
| 363 | + $p .= 'R'; |
|
| 364 | + } |
|
| 365 | + if ($this->info->isMounted()) { |
|
| 366 | + $p .= 'M'; |
|
| 367 | + } |
|
| 368 | + if ($this->info->isReadable()) { |
|
| 369 | + $p .= 'G'; |
|
| 370 | + } |
|
| 371 | + if ($this->info->isDeletable()) { |
|
| 372 | + $p .= 'D'; |
|
| 373 | + } |
|
| 374 | + if ($this->info->isUpdateable()) { |
|
| 375 | + $p .= 'NV'; // Renameable, Moveable |
|
| 376 | + } |
|
| 377 | + if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
|
| 378 | + if ($this->info->isUpdateable()) { |
|
| 379 | + $p .= 'W'; |
|
| 380 | + } |
|
| 381 | + } else { |
|
| 382 | + if ($this->info->isCreatable()) { |
|
| 383 | + $p .= 'CK'; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + return $p; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + public function getOwner() { |
|
| 390 | + return $this->info->getOwner(); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + protected function verifyPath() { |
|
| 394 | + try { |
|
| 395 | + $fileName = basename($this->info->getPath()); |
|
| 396 | + $this->fileView->verifyPath($this->path, $fileName); |
|
| 397 | + } catch (\OCP\Files\InvalidPathException $ex) { |
|
| 398 | + throw new InvalidPath($ex->getMessage()); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 404 | + */ |
|
| 405 | + public function acquireLock($type) { |
|
| 406 | + $this->fileView->lockFile($this->path, $type); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 411 | + */ |
|
| 412 | + public function releaseLock($type) { |
|
| 413 | + $this->fileView->unlockFile($this->path, $type); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 418 | + */ |
|
| 419 | + public function changeLock($type) { |
|
| 420 | + $this->fileView->changeLock($this->path, $type); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + public function getFileInfo() { |
|
| 424 | + return $this->info; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + public function getNode(): \OCP\Files\Node { |
|
| 428 | + return $this->node; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + protected function sanitizeMtime($mtimeFromRequest) { |
|
| 432 | + return MtimeSanitizer::sanitizeMtime($mtimeFromRequest); |
|
| 433 | + } |
|
| 434 | 434 | } |
@@ -54,431 +54,431 @@ |
||
| 54 | 54 | |
| 55 | 55 | class Directory extends \OCA\DAV\Connector\Sabre\Node implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota, \Sabre\DAV\IMoveTarget, \Sabre\DAV\ICopyTarget { |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Cached directory content |
|
| 59 | - * |
|
| 60 | - * @var \OCP\Files\FileInfo[] |
|
| 61 | - */ |
|
| 62 | - private $dirContent; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Cached quota info |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - private $quotaInfo; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var ObjectTree|null |
|
| 73 | - */ |
|
| 74 | - private $tree; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Sets up the node, expects a full path name |
|
| 78 | - * |
|
| 79 | - * @param \OC\Files\View $view |
|
| 80 | - * @param \OCP\Files\FileInfo $info |
|
| 81 | - * @param ObjectTree|null $tree |
|
| 82 | - * @param \OCP\Share\IManager $shareManager |
|
| 83 | - */ |
|
| 84 | - public function __construct(View $view, FileInfo $info, $tree = null, $shareManager = null) { |
|
| 85 | - parent::__construct($view, $info, $shareManager); |
|
| 86 | - $this->tree = $tree; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Creates a new file in the directory |
|
| 91 | - * |
|
| 92 | - * Data will either be supplied as a stream resource, or in certain cases |
|
| 93 | - * as a string. Keep in mind that you may have to support either. |
|
| 94 | - * |
|
| 95 | - * After successful creation of the file, you may choose to return the ETag |
|
| 96 | - * of the new file here. |
|
| 97 | - * |
|
| 98 | - * The returned ETag must be surrounded by double-quotes (The quotes should |
|
| 99 | - * be part of the actual string). |
|
| 100 | - * |
|
| 101 | - * If you cannot accurately determine the ETag, you should not return it. |
|
| 102 | - * If you don't store the file exactly as-is (you're transforming it |
|
| 103 | - * somehow) you should also not return an ETag. |
|
| 104 | - * |
|
| 105 | - * This means that if a subsequent GET to this new file does not exactly |
|
| 106 | - * return the same contents of what was submitted here, you are strongly |
|
| 107 | - * recommended to omit the ETag. |
|
| 108 | - * |
|
| 109 | - * @param string $name Name of the file |
|
| 110 | - * @param resource|string $data Initial payload |
|
| 111 | - * @return null|string |
|
| 112 | - * @throws Exception\EntityTooLarge |
|
| 113 | - * @throws Exception\UnsupportedMediaType |
|
| 114 | - * @throws FileLocked |
|
| 115 | - * @throws InvalidPath |
|
| 116 | - * @throws \Sabre\DAV\Exception |
|
| 117 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 118 | - * @throws \Sabre\DAV\Exception\Forbidden |
|
| 119 | - * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 120 | - */ |
|
| 121 | - public function createFile($name, $data = null) { |
|
| 122 | - try { |
|
| 123 | - // for chunked upload also updating a existing file is a "createFile" |
|
| 124 | - // because we create all the chunks before re-assemble them to the existing file. |
|
| 125 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 126 | - |
|
| 127 | - // exit if we can't create a new file and we don't updatable existing file |
|
| 128 | - $chunkInfo = \OC_FileChunking::decodeName($name); |
|
| 129 | - if (!$this->fileView->isCreatable($this->path) && |
|
| 130 | - !$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name']) |
|
| 131 | - ) { |
|
| 132 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 133 | - } |
|
| 134 | - } else { |
|
| 135 | - // For non-chunked upload it is enough to check if we can create a new file |
|
| 136 | - if (!$this->fileView->isCreatable($this->path)) { |
|
| 137 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $this->fileView->verifyPath($this->path, $name); |
|
| 142 | - |
|
| 143 | - $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name; |
|
| 144 | - // in case the file already exists/overwriting |
|
| 145 | - $info = $this->fileView->getFileInfo($this->path . '/' . $name); |
|
| 146 | - if (!$info) { |
|
| 147 | - // use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete |
|
| 148 | - $info = new \OC\Files\FileInfo($path, null, null, [ |
|
| 149 | - 'type' => FileInfo::TYPE_FILE |
|
| 150 | - ], null); |
|
| 151 | - } |
|
| 152 | - $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info); |
|
| 153 | - |
|
| 154 | - // only allow 1 process to upload a file at once but still allow reading the file while writing the part file |
|
| 155 | - $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
| 156 | - $this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 157 | - |
|
| 158 | - $result = $node->put($data); |
|
| 159 | - |
|
| 160 | - $this->fileView->unlockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 161 | - $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
| 162 | - return $result; |
|
| 163 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 164 | - throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage(), $e->getCode(), $e); |
|
| 165 | - } catch (InvalidPathException $ex) { |
|
| 166 | - throw new InvalidPath($ex->getMessage(), false, $ex); |
|
| 167 | - } catch (ForbiddenException $ex) { |
|
| 168 | - throw new Forbidden($ex->getMessage(), $ex->getRetry(), $ex); |
|
| 169 | - } catch (LockedException $e) { |
|
| 170 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Creates a new subdirectory |
|
| 176 | - * |
|
| 177 | - * @param string $name |
|
| 178 | - * @throws FileLocked |
|
| 179 | - * @throws InvalidPath |
|
| 180 | - * @throws \Sabre\DAV\Exception\Forbidden |
|
| 181 | - * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 182 | - */ |
|
| 183 | - public function createDirectory($name) { |
|
| 184 | - try { |
|
| 185 | - if (!$this->info->isCreatable()) { |
|
| 186 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $this->fileView->verifyPath($this->path, $name); |
|
| 190 | - $newPath = $this->path . '/' . $name; |
|
| 191 | - if (!$this->fileView->mkdir($newPath)) { |
|
| 192 | - throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath); |
|
| 193 | - } |
|
| 194 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 195 | - throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
|
| 196 | - } catch (InvalidPathException $ex) { |
|
| 197 | - throw new InvalidPath($ex->getMessage()); |
|
| 198 | - } catch (ForbiddenException $ex) { |
|
| 199 | - throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 200 | - } catch (LockedException $e) { |
|
| 201 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Returns a specific child node, referenced by its name |
|
| 207 | - * |
|
| 208 | - * @param string $name |
|
| 209 | - * @param \OCP\Files\FileInfo $info |
|
| 210 | - * @return \Sabre\DAV\INode |
|
| 211 | - * @throws InvalidPath |
|
| 212 | - * @throws \Sabre\DAV\Exception\NotFound |
|
| 213 | - * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 214 | - */ |
|
| 215 | - public function getChild($name, $info = null) { |
|
| 216 | - if (!$this->info->isReadable()) { |
|
| 217 | - // avoid detecting files through this way |
|
| 218 | - throw new NotFound(); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $path = $this->path . '/' . $name; |
|
| 222 | - if (is_null($info)) { |
|
| 223 | - try { |
|
| 224 | - $this->fileView->verifyPath($this->path, $name); |
|
| 225 | - $info = $this->fileView->getFileInfo($path); |
|
| 226 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 227 | - throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
|
| 228 | - } catch (InvalidPathException $ex) { |
|
| 229 | - throw new InvalidPath($ex->getMessage()); |
|
| 230 | - } catch (ForbiddenException $e) { |
|
| 231 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - if (!$info) { |
|
| 236 | - throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located'); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if ($info->getMimeType() === FileInfo::MIMETYPE_FOLDER) { |
|
| 240 | - $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager); |
|
| 241 | - } else { |
|
| 242 | - $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager); |
|
| 243 | - } |
|
| 244 | - if ($this->tree) { |
|
| 245 | - $this->tree->cacheNode($node); |
|
| 246 | - } |
|
| 247 | - return $node; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Returns an array with all the child nodes |
|
| 252 | - * |
|
| 253 | - * @return \Sabre\DAV\INode[] |
|
| 254 | - * @throws \Sabre\DAV\Exception\Locked |
|
| 255 | - * @throws \OCA\DAV\Connector\Sabre\Exception\Forbidden |
|
| 256 | - */ |
|
| 257 | - public function getChildren() { |
|
| 258 | - if (!is_null($this->dirContent)) { |
|
| 259 | - return $this->dirContent; |
|
| 260 | - } |
|
| 261 | - try { |
|
| 262 | - if (!$this->info->isReadable()) { |
|
| 263 | - // return 403 instead of 404 because a 404 would make |
|
| 264 | - // the caller believe that the collection itself does not exist |
|
| 265 | - throw new Forbidden('No read permissions'); |
|
| 266 | - } |
|
| 267 | - $folderContent = $this->getNode()->getDirectoryListing(); |
|
| 268 | - } catch (LockedException $e) { |
|
| 269 | - throw new Locked(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $nodes = []; |
|
| 273 | - foreach ($folderContent as $info) { |
|
| 274 | - $node = $this->getChild($info->getName(), $info); |
|
| 275 | - $nodes[] = $node; |
|
| 276 | - } |
|
| 277 | - $this->dirContent = $nodes; |
|
| 278 | - return $this->dirContent; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Checks if a child exists. |
|
| 283 | - * |
|
| 284 | - * @param string $name |
|
| 285 | - * @return bool |
|
| 286 | - */ |
|
| 287 | - public function childExists($name) { |
|
| 288 | - // note: here we do NOT resolve the chunk file name to the real file name |
|
| 289 | - // to make sure we return false when checking for file existence with a chunk |
|
| 290 | - // file name. |
|
| 291 | - // This is to make sure that "createFile" is still triggered |
|
| 292 | - // (required old code) instead of "updateFile". |
|
| 293 | - // |
|
| 294 | - // TODO: resolve chunk file name here and implement "updateFile" |
|
| 295 | - $path = $this->path . '/' . $name; |
|
| 296 | - return $this->fileView->file_exists($path); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Deletes all files in this directory, and then itself |
|
| 301 | - * |
|
| 302 | - * @return void |
|
| 303 | - * @throws FileLocked |
|
| 304 | - * @throws \Sabre\DAV\Exception\Forbidden |
|
| 305 | - */ |
|
| 306 | - public function delete() { |
|
| 307 | - if ($this->path === '' || $this->path === '/' || !$this->info->isDeletable()) { |
|
| 308 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - try { |
|
| 312 | - if (!$this->fileView->rmdir($this->path)) { |
|
| 313 | - // assume it wasn't possible to remove due to permission issue |
|
| 314 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 315 | - } |
|
| 316 | - } catch (ForbiddenException $ex) { |
|
| 317 | - throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 318 | - } catch (LockedException $e) { |
|
| 319 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Returns available diskspace information |
|
| 325 | - * |
|
| 326 | - * @return array |
|
| 327 | - */ |
|
| 328 | - public function getQuotaInfo() { |
|
| 329 | - if ($this->quotaInfo) { |
|
| 330 | - return $this->quotaInfo; |
|
| 331 | - } |
|
| 332 | - try { |
|
| 333 | - $storageInfo = \OC_Helper::getStorageInfo($this->info->getPath(), $this->info, false); |
|
| 334 | - if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 335 | - $free = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
| 336 | - } else { |
|
| 337 | - $free = $storageInfo['free']; |
|
| 338 | - } |
|
| 339 | - $this->quotaInfo = [ |
|
| 340 | - $storageInfo['used'], |
|
| 341 | - $free |
|
| 342 | - ]; |
|
| 343 | - return $this->quotaInfo; |
|
| 344 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 345 | - return [0, 0]; |
|
| 346 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 347 | - return [0, 0]; |
|
| 348 | - } catch (NotPermittedException $e) { |
|
| 349 | - return [0, 0]; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Moves a node into this collection. |
|
| 355 | - * |
|
| 356 | - * It is up to the implementors to: |
|
| 357 | - * 1. Create the new resource. |
|
| 358 | - * 2. Remove the old resource. |
|
| 359 | - * 3. Transfer any properties or other data. |
|
| 360 | - * |
|
| 361 | - * Generally you should make very sure that your collection can easily move |
|
| 362 | - * the move. |
|
| 363 | - * |
|
| 364 | - * If you don't, just return false, which will trigger sabre/dav to handle |
|
| 365 | - * the move itself. If you return true from this function, the assumption |
|
| 366 | - * is that the move was successful. |
|
| 367 | - * |
|
| 368 | - * @param string $targetName New local file/collection name. |
|
| 369 | - * @param string $fullSourcePath Full path to source node |
|
| 370 | - * @param INode $sourceNode Source node itself |
|
| 371 | - * @return bool |
|
| 372 | - * @throws BadRequest |
|
| 373 | - * @throws ServiceUnavailable |
|
| 374 | - * @throws Forbidden |
|
| 375 | - * @throws FileLocked |
|
| 376 | - * @throws \Sabre\DAV\Exception\Forbidden |
|
| 377 | - */ |
|
| 378 | - public function moveInto($targetName, $fullSourcePath, INode $sourceNode) { |
|
| 379 | - if (!$sourceNode instanceof Node) { |
|
| 380 | - // it's a file of another kind, like FutureFile |
|
| 381 | - if ($sourceNode instanceof IFile) { |
|
| 382 | - // fallback to default copy+delete handling |
|
| 383 | - return false; |
|
| 384 | - } |
|
| 385 | - throw new BadRequest('Incompatible node types'); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - if (!$this->fileView) { |
|
| 389 | - throw new ServiceUnavailable('filesystem not setup'); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - $targetNodeExists = $this->childExists($targetName); |
|
| 396 | - |
|
| 397 | - // at getNodeForPath we also check the path for isForbiddenFileOrDir |
|
| 398 | - // with that we have covered both source and destination |
|
| 399 | - if ($sourceNode instanceof Directory && $targetNodeExists) { |
|
| 400 | - throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - [$sourceDir,] = \Sabre\Uri\split($sourceNode->getPath()); |
|
| 404 | - $destinationDir = $this->getPath(); |
|
| 405 | - |
|
| 406 | - $sourcePath = $sourceNode->getPath(); |
|
| 407 | - |
|
| 408 | - $isMovableMount = false; |
|
| 409 | - $sourceMount = \OC::$server->getMountManager()->find($this->fileView->getAbsolutePath($sourcePath)); |
|
| 410 | - $internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath)); |
|
| 411 | - if ($sourceMount instanceof MoveableMount && $internalPath === '') { |
|
| 412 | - $isMovableMount = true; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - try { |
|
| 416 | - $sameFolder = ($sourceDir === $destinationDir); |
|
| 417 | - // if we're overwriting or same folder |
|
| 418 | - if ($targetNodeExists || $sameFolder) { |
|
| 419 | - // note that renaming a share mount point is always allowed |
|
| 420 | - if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) { |
|
| 421 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 422 | - } |
|
| 423 | - } else { |
|
| 424 | - if (!$this->fileView->isCreatable($destinationDir)) { |
|
| 425 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if (!$sameFolder) { |
|
| 430 | - // moving to a different folder, source will be gone, like a deletion |
|
| 431 | - // note that moving a share mount point is always allowed |
|
| 432 | - if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) { |
|
| 433 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $fileName = basename($destinationPath); |
|
| 438 | - try { |
|
| 439 | - $this->fileView->verifyPath($destinationDir, $fileName); |
|
| 440 | - } catch (InvalidPathException $ex) { |
|
| 441 | - throw new InvalidPath($ex->getMessage()); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $renameOkay = $this->fileView->rename($sourcePath, $destinationPath); |
|
| 445 | - if (!$renameOkay) { |
|
| 446 | - throw new \Sabre\DAV\Exception\Forbidden(''); |
|
| 447 | - } |
|
| 448 | - } catch (StorageNotAvailableException $e) { |
|
| 449 | - throw new ServiceUnavailable($e->getMessage()); |
|
| 450 | - } catch (ForbiddenException $ex) { |
|
| 451 | - throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 452 | - } catch (LockedException $e) { |
|
| 453 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - return true; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - |
|
| 460 | - public function copyInto($targetName, $sourcePath, INode $sourceNode) { |
|
| 461 | - if ($sourceNode instanceof File || $sourceNode instanceof Directory) { |
|
| 462 | - $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 463 | - $sourcePath = $sourceNode->getPath(); |
|
| 464 | - |
|
| 465 | - if (!$this->fileView->isCreatable($this->getPath())) { |
|
| 466 | - throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - try { |
|
| 470 | - $this->fileView->verifyPath($this->getPath(), $targetName); |
|
| 471 | - } catch (InvalidPathException $ex) { |
|
| 472 | - throw new InvalidPath($ex->getMessage()); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - return $this->fileView->copy($sourcePath, $destinationPath); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - return false; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - public function getNode(): Folder { |
|
| 482 | - return $this->node; |
|
| 483 | - } |
|
| 57 | + /** |
|
| 58 | + * Cached directory content |
|
| 59 | + * |
|
| 60 | + * @var \OCP\Files\FileInfo[] |
|
| 61 | + */ |
|
| 62 | + private $dirContent; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Cached quota info |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + private $quotaInfo; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var ObjectTree|null |
|
| 73 | + */ |
|
| 74 | + private $tree; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Sets up the node, expects a full path name |
|
| 78 | + * |
|
| 79 | + * @param \OC\Files\View $view |
|
| 80 | + * @param \OCP\Files\FileInfo $info |
|
| 81 | + * @param ObjectTree|null $tree |
|
| 82 | + * @param \OCP\Share\IManager $shareManager |
|
| 83 | + */ |
|
| 84 | + public function __construct(View $view, FileInfo $info, $tree = null, $shareManager = null) { |
|
| 85 | + parent::__construct($view, $info, $shareManager); |
|
| 86 | + $this->tree = $tree; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Creates a new file in the directory |
|
| 91 | + * |
|
| 92 | + * Data will either be supplied as a stream resource, or in certain cases |
|
| 93 | + * as a string. Keep in mind that you may have to support either. |
|
| 94 | + * |
|
| 95 | + * After successful creation of the file, you may choose to return the ETag |
|
| 96 | + * of the new file here. |
|
| 97 | + * |
|
| 98 | + * The returned ETag must be surrounded by double-quotes (The quotes should |
|
| 99 | + * be part of the actual string). |
|
| 100 | + * |
|
| 101 | + * If you cannot accurately determine the ETag, you should not return it. |
|
| 102 | + * If you don't store the file exactly as-is (you're transforming it |
|
| 103 | + * somehow) you should also not return an ETag. |
|
| 104 | + * |
|
| 105 | + * This means that if a subsequent GET to this new file does not exactly |
|
| 106 | + * return the same contents of what was submitted here, you are strongly |
|
| 107 | + * recommended to omit the ETag. |
|
| 108 | + * |
|
| 109 | + * @param string $name Name of the file |
|
| 110 | + * @param resource|string $data Initial payload |
|
| 111 | + * @return null|string |
|
| 112 | + * @throws Exception\EntityTooLarge |
|
| 113 | + * @throws Exception\UnsupportedMediaType |
|
| 114 | + * @throws FileLocked |
|
| 115 | + * @throws InvalidPath |
|
| 116 | + * @throws \Sabre\DAV\Exception |
|
| 117 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 118 | + * @throws \Sabre\DAV\Exception\Forbidden |
|
| 119 | + * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 120 | + */ |
|
| 121 | + public function createFile($name, $data = null) { |
|
| 122 | + try { |
|
| 123 | + // for chunked upload also updating a existing file is a "createFile" |
|
| 124 | + // because we create all the chunks before re-assemble them to the existing file. |
|
| 125 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 126 | + |
|
| 127 | + // exit if we can't create a new file and we don't updatable existing file |
|
| 128 | + $chunkInfo = \OC_FileChunking::decodeName($name); |
|
| 129 | + if (!$this->fileView->isCreatable($this->path) && |
|
| 130 | + !$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name']) |
|
| 131 | + ) { |
|
| 132 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 133 | + } |
|
| 134 | + } else { |
|
| 135 | + // For non-chunked upload it is enough to check if we can create a new file |
|
| 136 | + if (!$this->fileView->isCreatable($this->path)) { |
|
| 137 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $this->fileView->verifyPath($this->path, $name); |
|
| 142 | + |
|
| 143 | + $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name; |
|
| 144 | + // in case the file already exists/overwriting |
|
| 145 | + $info = $this->fileView->getFileInfo($this->path . '/' . $name); |
|
| 146 | + if (!$info) { |
|
| 147 | + // use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete |
|
| 148 | + $info = new \OC\Files\FileInfo($path, null, null, [ |
|
| 149 | + 'type' => FileInfo::TYPE_FILE |
|
| 150 | + ], null); |
|
| 151 | + } |
|
| 152 | + $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info); |
|
| 153 | + |
|
| 154 | + // only allow 1 process to upload a file at once but still allow reading the file while writing the part file |
|
| 155 | + $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
| 156 | + $this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 157 | + |
|
| 158 | + $result = $node->put($data); |
|
| 159 | + |
|
| 160 | + $this->fileView->unlockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 161 | + $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
| 162 | + return $result; |
|
| 163 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 164 | + throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage(), $e->getCode(), $e); |
|
| 165 | + } catch (InvalidPathException $ex) { |
|
| 166 | + throw new InvalidPath($ex->getMessage(), false, $ex); |
|
| 167 | + } catch (ForbiddenException $ex) { |
|
| 168 | + throw new Forbidden($ex->getMessage(), $ex->getRetry(), $ex); |
|
| 169 | + } catch (LockedException $e) { |
|
| 170 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Creates a new subdirectory |
|
| 176 | + * |
|
| 177 | + * @param string $name |
|
| 178 | + * @throws FileLocked |
|
| 179 | + * @throws InvalidPath |
|
| 180 | + * @throws \Sabre\DAV\Exception\Forbidden |
|
| 181 | + * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 182 | + */ |
|
| 183 | + public function createDirectory($name) { |
|
| 184 | + try { |
|
| 185 | + if (!$this->info->isCreatable()) { |
|
| 186 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $this->fileView->verifyPath($this->path, $name); |
|
| 190 | + $newPath = $this->path . '/' . $name; |
|
| 191 | + if (!$this->fileView->mkdir($newPath)) { |
|
| 192 | + throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath); |
|
| 193 | + } |
|
| 194 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 195 | + throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
|
| 196 | + } catch (InvalidPathException $ex) { |
|
| 197 | + throw new InvalidPath($ex->getMessage()); |
|
| 198 | + } catch (ForbiddenException $ex) { |
|
| 199 | + throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 200 | + } catch (LockedException $e) { |
|
| 201 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Returns a specific child node, referenced by its name |
|
| 207 | + * |
|
| 208 | + * @param string $name |
|
| 209 | + * @param \OCP\Files\FileInfo $info |
|
| 210 | + * @return \Sabre\DAV\INode |
|
| 211 | + * @throws InvalidPath |
|
| 212 | + * @throws \Sabre\DAV\Exception\NotFound |
|
| 213 | + * @throws \Sabre\DAV\Exception\ServiceUnavailable |
|
| 214 | + */ |
|
| 215 | + public function getChild($name, $info = null) { |
|
| 216 | + if (!$this->info->isReadable()) { |
|
| 217 | + // avoid detecting files through this way |
|
| 218 | + throw new NotFound(); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $path = $this->path . '/' . $name; |
|
| 222 | + if (is_null($info)) { |
|
| 223 | + try { |
|
| 224 | + $this->fileView->verifyPath($this->path, $name); |
|
| 225 | + $info = $this->fileView->getFileInfo($path); |
|
| 226 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 227 | + throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
|
| 228 | + } catch (InvalidPathException $ex) { |
|
| 229 | + throw new InvalidPath($ex->getMessage()); |
|
| 230 | + } catch (ForbiddenException $e) { |
|
| 231 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + if (!$info) { |
|
| 236 | + throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located'); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if ($info->getMimeType() === FileInfo::MIMETYPE_FOLDER) { |
|
| 240 | + $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager); |
|
| 241 | + } else { |
|
| 242 | + $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager); |
|
| 243 | + } |
|
| 244 | + if ($this->tree) { |
|
| 245 | + $this->tree->cacheNode($node); |
|
| 246 | + } |
|
| 247 | + return $node; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Returns an array with all the child nodes |
|
| 252 | + * |
|
| 253 | + * @return \Sabre\DAV\INode[] |
|
| 254 | + * @throws \Sabre\DAV\Exception\Locked |
|
| 255 | + * @throws \OCA\DAV\Connector\Sabre\Exception\Forbidden |
|
| 256 | + */ |
|
| 257 | + public function getChildren() { |
|
| 258 | + if (!is_null($this->dirContent)) { |
|
| 259 | + return $this->dirContent; |
|
| 260 | + } |
|
| 261 | + try { |
|
| 262 | + if (!$this->info->isReadable()) { |
|
| 263 | + // return 403 instead of 404 because a 404 would make |
|
| 264 | + // the caller believe that the collection itself does not exist |
|
| 265 | + throw new Forbidden('No read permissions'); |
|
| 266 | + } |
|
| 267 | + $folderContent = $this->getNode()->getDirectoryListing(); |
|
| 268 | + } catch (LockedException $e) { |
|
| 269 | + throw new Locked(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $nodes = []; |
|
| 273 | + foreach ($folderContent as $info) { |
|
| 274 | + $node = $this->getChild($info->getName(), $info); |
|
| 275 | + $nodes[] = $node; |
|
| 276 | + } |
|
| 277 | + $this->dirContent = $nodes; |
|
| 278 | + return $this->dirContent; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Checks if a child exists. |
|
| 283 | + * |
|
| 284 | + * @param string $name |
|
| 285 | + * @return bool |
|
| 286 | + */ |
|
| 287 | + public function childExists($name) { |
|
| 288 | + // note: here we do NOT resolve the chunk file name to the real file name |
|
| 289 | + // to make sure we return false when checking for file existence with a chunk |
|
| 290 | + // file name. |
|
| 291 | + // This is to make sure that "createFile" is still triggered |
|
| 292 | + // (required old code) instead of "updateFile". |
|
| 293 | + // |
|
| 294 | + // TODO: resolve chunk file name here and implement "updateFile" |
|
| 295 | + $path = $this->path . '/' . $name; |
|
| 296 | + return $this->fileView->file_exists($path); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Deletes all files in this directory, and then itself |
|
| 301 | + * |
|
| 302 | + * @return void |
|
| 303 | + * @throws FileLocked |
|
| 304 | + * @throws \Sabre\DAV\Exception\Forbidden |
|
| 305 | + */ |
|
| 306 | + public function delete() { |
|
| 307 | + if ($this->path === '' || $this->path === '/' || !$this->info->isDeletable()) { |
|
| 308 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + try { |
|
| 312 | + if (!$this->fileView->rmdir($this->path)) { |
|
| 313 | + // assume it wasn't possible to remove due to permission issue |
|
| 314 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 315 | + } |
|
| 316 | + } catch (ForbiddenException $ex) { |
|
| 317 | + throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 318 | + } catch (LockedException $e) { |
|
| 319 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Returns available diskspace information |
|
| 325 | + * |
|
| 326 | + * @return array |
|
| 327 | + */ |
|
| 328 | + public function getQuotaInfo() { |
|
| 329 | + if ($this->quotaInfo) { |
|
| 330 | + return $this->quotaInfo; |
|
| 331 | + } |
|
| 332 | + try { |
|
| 333 | + $storageInfo = \OC_Helper::getStorageInfo($this->info->getPath(), $this->info, false); |
|
| 334 | + if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 335 | + $free = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
| 336 | + } else { |
|
| 337 | + $free = $storageInfo['free']; |
|
| 338 | + } |
|
| 339 | + $this->quotaInfo = [ |
|
| 340 | + $storageInfo['used'], |
|
| 341 | + $free |
|
| 342 | + ]; |
|
| 343 | + return $this->quotaInfo; |
|
| 344 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 345 | + return [0, 0]; |
|
| 346 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 347 | + return [0, 0]; |
|
| 348 | + } catch (NotPermittedException $e) { |
|
| 349 | + return [0, 0]; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Moves a node into this collection. |
|
| 355 | + * |
|
| 356 | + * It is up to the implementors to: |
|
| 357 | + * 1. Create the new resource. |
|
| 358 | + * 2. Remove the old resource. |
|
| 359 | + * 3. Transfer any properties or other data. |
|
| 360 | + * |
|
| 361 | + * Generally you should make very sure that your collection can easily move |
|
| 362 | + * the move. |
|
| 363 | + * |
|
| 364 | + * If you don't, just return false, which will trigger sabre/dav to handle |
|
| 365 | + * the move itself. If you return true from this function, the assumption |
|
| 366 | + * is that the move was successful. |
|
| 367 | + * |
|
| 368 | + * @param string $targetName New local file/collection name. |
|
| 369 | + * @param string $fullSourcePath Full path to source node |
|
| 370 | + * @param INode $sourceNode Source node itself |
|
| 371 | + * @return bool |
|
| 372 | + * @throws BadRequest |
|
| 373 | + * @throws ServiceUnavailable |
|
| 374 | + * @throws Forbidden |
|
| 375 | + * @throws FileLocked |
|
| 376 | + * @throws \Sabre\DAV\Exception\Forbidden |
|
| 377 | + */ |
|
| 378 | + public function moveInto($targetName, $fullSourcePath, INode $sourceNode) { |
|
| 379 | + if (!$sourceNode instanceof Node) { |
|
| 380 | + // it's a file of another kind, like FutureFile |
|
| 381 | + if ($sourceNode instanceof IFile) { |
|
| 382 | + // fallback to default copy+delete handling |
|
| 383 | + return false; |
|
| 384 | + } |
|
| 385 | + throw new BadRequest('Incompatible node types'); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + if (!$this->fileView) { |
|
| 389 | + throw new ServiceUnavailable('filesystem not setup'); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + $targetNodeExists = $this->childExists($targetName); |
|
| 396 | + |
|
| 397 | + // at getNodeForPath we also check the path for isForbiddenFileOrDir |
|
| 398 | + // with that we have covered both source and destination |
|
| 399 | + if ($sourceNode instanceof Directory && $targetNodeExists) { |
|
| 400 | + throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + [$sourceDir,] = \Sabre\Uri\split($sourceNode->getPath()); |
|
| 404 | + $destinationDir = $this->getPath(); |
|
| 405 | + |
|
| 406 | + $sourcePath = $sourceNode->getPath(); |
|
| 407 | + |
|
| 408 | + $isMovableMount = false; |
|
| 409 | + $sourceMount = \OC::$server->getMountManager()->find($this->fileView->getAbsolutePath($sourcePath)); |
|
| 410 | + $internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath)); |
|
| 411 | + if ($sourceMount instanceof MoveableMount && $internalPath === '') { |
|
| 412 | + $isMovableMount = true; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + try { |
|
| 416 | + $sameFolder = ($sourceDir === $destinationDir); |
|
| 417 | + // if we're overwriting or same folder |
|
| 418 | + if ($targetNodeExists || $sameFolder) { |
|
| 419 | + // note that renaming a share mount point is always allowed |
|
| 420 | + if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) { |
|
| 421 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 422 | + } |
|
| 423 | + } else { |
|
| 424 | + if (!$this->fileView->isCreatable($destinationDir)) { |
|
| 425 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if (!$sameFolder) { |
|
| 430 | + // moving to a different folder, source will be gone, like a deletion |
|
| 431 | + // note that moving a share mount point is always allowed |
|
| 432 | + if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) { |
|
| 433 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $fileName = basename($destinationPath); |
|
| 438 | + try { |
|
| 439 | + $this->fileView->verifyPath($destinationDir, $fileName); |
|
| 440 | + } catch (InvalidPathException $ex) { |
|
| 441 | + throw new InvalidPath($ex->getMessage()); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $renameOkay = $this->fileView->rename($sourcePath, $destinationPath); |
|
| 445 | + if (!$renameOkay) { |
|
| 446 | + throw new \Sabre\DAV\Exception\Forbidden(''); |
|
| 447 | + } |
|
| 448 | + } catch (StorageNotAvailableException $e) { |
|
| 449 | + throw new ServiceUnavailable($e->getMessage()); |
|
| 450 | + } catch (ForbiddenException $ex) { |
|
| 451 | + throw new Forbidden($ex->getMessage(), $ex->getRetry()); |
|
| 452 | + } catch (LockedException $e) { |
|
| 453 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + return true; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + |
|
| 460 | + public function copyInto($targetName, $sourcePath, INode $sourceNode) { |
|
| 461 | + if ($sourceNode instanceof File || $sourceNode instanceof Directory) { |
|
| 462 | + $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 463 | + $sourcePath = $sourceNode->getPath(); |
|
| 464 | + |
|
| 465 | + if (!$this->fileView->isCreatable($this->getPath())) { |
|
| 466 | + throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + try { |
|
| 470 | + $this->fileView->verifyPath($this->getPath(), $targetName); |
|
| 471 | + } catch (InvalidPathException $ex) { |
|
| 472 | + throw new InvalidPath($ex->getMessage()); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + return $this->fileView->copy($sourcePath, $destinationPath); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + return false; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + public function getNode(): Folder { |
|
| 482 | + return $this->node; |
|
| 483 | + } |
|
| 484 | 484 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | // exit if we can't create a new file and we don't updatable existing file |
| 128 | 128 | $chunkInfo = \OC_FileChunking::decodeName($name); |
| 129 | 129 | if (!$this->fileView->isCreatable($this->path) && |
| 130 | - !$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name']) |
|
| 130 | + !$this->fileView->isUpdatable($this->path.'/'.$chunkInfo['name']) |
|
| 131 | 131 | ) { |
| 132 | 132 | throw new \Sabre\DAV\Exception\Forbidden(); |
| 133 | 133 | } |
@@ -140,9 +140,9 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | $this->fileView->verifyPath($this->path, $name); |
| 142 | 142 | |
| 143 | - $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name; |
|
| 143 | + $path = $this->fileView->getAbsolutePath($this->path).'/'.$name; |
|
| 144 | 144 | // in case the file already exists/overwriting |
| 145 | - $info = $this->fileView->getFileInfo($this->path . '/' . $name); |
|
| 145 | + $info = $this->fileView->getFileInfo($this->path.'/'.$name); |
|
| 146 | 146 | if (!$info) { |
| 147 | 147 | // use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete |
| 148 | 148 | $info = new \OC\Files\FileInfo($path, null, null, [ |
@@ -153,11 +153,11 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | // only allow 1 process to upload a file at once but still allow reading the file while writing the part file |
| 155 | 155 | $node->acquireLock(ILockingProvider::LOCK_SHARED); |
| 156 | - $this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 156 | + $this->fileView->lockFile($path.'.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 157 | 157 | |
| 158 | 158 | $result = $node->put($data); |
| 159 | 159 | |
| 160 | - $this->fileView->unlockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 160 | + $this->fileView->unlockFile($path.'.upload.part', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 161 | 161 | $node->releaseLock(ILockingProvider::LOCK_SHARED); |
| 162 | 162 | return $result; |
| 163 | 163 | } catch (\OCP\Files\StorageNotAvailableException $e) { |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | $this->fileView->verifyPath($this->path, $name); |
| 190 | - $newPath = $this->path . '/' . $name; |
|
| 190 | + $newPath = $this->path.'/'.$name; |
|
| 191 | 191 | if (!$this->fileView->mkdir($newPath)) { |
| 192 | - throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath); |
|
| 192 | + throw new \Sabre\DAV\Exception\Forbidden('Could not create directory '.$newPath); |
|
| 193 | 193 | } |
| 194 | 194 | } catch (\OCP\Files\StorageNotAvailableException $e) { |
| 195 | 195 | throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | throw new NotFound(); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $path = $this->path . '/' . $name; |
|
| 221 | + $path = $this->path.'/'.$name; |
|
| 222 | 222 | if (is_null($info)) { |
| 223 | 223 | try { |
| 224 | 224 | $this->fileView->verifyPath($this->path, $name); |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | if (!$info) { |
| 236 | - throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located'); |
|
| 236 | + throw new \Sabre\DAV\Exception\NotFound('File with name '.$path.' could not be located'); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | if ($info->getMimeType() === FileInfo::MIMETYPE_FOLDER) { |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | // (required old code) instead of "updateFile". |
| 293 | 293 | // |
| 294 | 294 | // TODO: resolve chunk file name here and implement "updateFile" |
| 295 | - $path = $this->path . '/' . $name; |
|
| 295 | + $path = $this->path.'/'.$name; |
|
| 296 | 296 | return $this->fileView->file_exists($path); |
| 297 | 297 | } |
| 298 | 298 | |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | throw new ServiceUnavailable('filesystem not setup'); |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 392 | + $destinationPath = $this->getPath().'/'.$targetName; |
|
| 393 | 393 | |
| 394 | 394 | |
| 395 | 395 | $targetNodeExists = $this->childExists($targetName); |
@@ -397,10 +397,10 @@ discard block |
||
| 397 | 397 | // at getNodeForPath we also check the path for isForbiddenFileOrDir |
| 398 | 398 | // with that we have covered both source and destination |
| 399 | 399 | if ($sourceNode instanceof Directory && $targetNodeExists) { |
| 400 | - throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists'); |
|
| 400 | + throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory '.$sourceNode->getName().', target exists'); |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | - [$sourceDir,] = \Sabre\Uri\split($sourceNode->getPath()); |
|
| 403 | + [$sourceDir, ] = \Sabre\Uri\split($sourceNode->getPath()); |
|
| 404 | 404 | $destinationDir = $this->getPath(); |
| 405 | 405 | |
| 406 | 406 | $sourcePath = $sourceNode->getPath(); |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | |
| 460 | 460 | public function copyInto($targetName, $sourcePath, INode $sourceNode) { |
| 461 | 461 | if ($sourceNode instanceof File || $sourceNode instanceof Directory) { |
| 462 | - $destinationPath = $this->getPath() . '/' . $targetName; |
|
| 462 | + $destinationPath = $this->getPath().'/'.$targetName; |
|
| 463 | 463 | $sourcePath = $sourceNode->getPath(); |
| 464 | 464 | |
| 465 | 465 | if (!$this->fileView->isCreatable($this->getPath())) { |
@@ -39,190 +39,190 @@ |
||
| 39 | 39 | * Sabre Plugin to provide share-related properties |
| 40 | 40 | */ |
| 41 | 41 | class SharesPlugin extends \Sabre\DAV\ServerPlugin { |
| 42 | - public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 43 | - public const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
| 44 | - public const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types'; |
|
| 45 | - public const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Reference to main server object |
|
| 49 | - * |
|
| 50 | - * @var \Sabre\DAV\Server |
|
| 51 | - */ |
|
| 52 | - private $server; |
|
| 53 | - |
|
| 54 | - /** @var \OCP\Share\IManager */ |
|
| 55 | - private $shareManager; |
|
| 56 | - |
|
| 57 | - /** @var \Sabre\DAV\Tree */ |
|
| 58 | - private $tree; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - private $userId; |
|
| 62 | - |
|
| 63 | - /** @var \OCP\Files\Folder */ |
|
| 64 | - private $userFolder; |
|
| 65 | - |
|
| 66 | - /** @var IShare[][] */ |
|
| 67 | - private $cachedShares = []; |
|
| 68 | - |
|
| 69 | - /** @var string[] */ |
|
| 70 | - private $cachedFolders = []; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param \Sabre\DAV\Tree $tree tree |
|
| 74 | - * @param IUserSession $userSession user session |
|
| 75 | - * @param \OCP\Files\Folder $userFolder user home folder |
|
| 76 | - * @param \OCP\Share\IManager $shareManager share manager |
|
| 77 | - */ |
|
| 78 | - public function __construct( |
|
| 79 | - \Sabre\DAV\Tree $tree, |
|
| 80 | - IUserSession $userSession, |
|
| 81 | - \OCP\Files\Folder $userFolder, |
|
| 82 | - \OCP\Share\IManager $shareManager |
|
| 83 | - ) { |
|
| 84 | - $this->tree = $tree; |
|
| 85 | - $this->shareManager = $shareManager; |
|
| 86 | - $this->userFolder = $userFolder; |
|
| 87 | - $this->userId = $userSession->getUser()->getUID(); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * This initializes the plugin. |
|
| 92 | - * |
|
| 93 | - * This function is called by \Sabre\DAV\Server, after |
|
| 94 | - * addPlugin is called. |
|
| 95 | - * |
|
| 96 | - * This method should set up the required event subscriptions. |
|
| 97 | - * |
|
| 98 | - * @param \Sabre\DAV\Server $server |
|
| 99 | - */ |
|
| 100 | - public function initialize(\Sabre\DAV\Server $server) { |
|
| 101 | - $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 102 | - $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class; |
|
| 103 | - $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME; |
|
| 104 | - $server->protectedProperties[] = self::SHAREES_PROPERTYNAME; |
|
| 105 | - |
|
| 106 | - $this->server = $server; |
|
| 107 | - $this->server->on('propFind', [$this, 'handleGetProperties']); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param \OCP\Files\Node $node |
|
| 112 | - * @return IShare[] |
|
| 113 | - */ |
|
| 114 | - private function getShare(\OCP\Files\Node $node): array { |
|
| 115 | - $result = []; |
|
| 116 | - $requestedShareTypes = [ |
|
| 117 | - IShare::TYPE_USER, |
|
| 118 | - IShare::TYPE_GROUP, |
|
| 119 | - IShare::TYPE_LINK, |
|
| 120 | - IShare::TYPE_REMOTE, |
|
| 121 | - IShare::TYPE_EMAIL, |
|
| 122 | - IShare::TYPE_ROOM, |
|
| 123 | - IShare::TYPE_CIRCLE, |
|
| 124 | - IShare::TYPE_DECK, |
|
| 125 | - ]; |
|
| 126 | - foreach ($requestedShareTypes as $requestedShareType) { |
|
| 127 | - $shares = $this->shareManager->getSharesBy( |
|
| 128 | - $this->userId, |
|
| 129 | - $requestedShareType, |
|
| 130 | - $node, |
|
| 131 | - false, |
|
| 132 | - -1 |
|
| 133 | - ); |
|
| 134 | - foreach ($shares as $share) { |
|
| 135 | - $result[] = $share; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - return $result; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param Folder $node |
|
| 143 | - * @return IShare[][] |
|
| 144 | - */ |
|
| 145 | - private function getSharesFolder(Folder $node): array { |
|
| 146 | - return $this->shareManager->getSharesInFolder( |
|
| 147 | - $this->userId, |
|
| 148 | - $node, |
|
| 149 | - true |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param DavNode $sabreNode |
|
| 155 | - * @return IShare[] |
|
| 156 | - */ |
|
| 157 | - private function getShares(DavNode $sabreNode): array { |
|
| 158 | - if (isset($this->cachedShares[$sabreNode->getId()])) { |
|
| 159 | - $shares = $this->cachedShares[$sabreNode->getId()]; |
|
| 160 | - } else { |
|
| 161 | - [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath()); |
|
| 162 | - if ($parentPath === '') { |
|
| 163 | - $parentPath = '/'; |
|
| 164 | - } |
|
| 165 | - // if we already cached the folder this file is in we know there are no shares for this file |
|
| 166 | - if (array_search($parentPath, $this->cachedFolders) === false) { |
|
| 167 | - try { |
|
| 168 | - $node = $sabreNode->getNode(); |
|
| 169 | - } catch (NotFoundException $e) { |
|
| 170 | - return []; |
|
| 171 | - } |
|
| 172 | - $shares = $this->getShare($node); |
|
| 173 | - $this->cachedShares[$sabreNode->getId()] = $shares; |
|
| 174 | - } else { |
|
| 175 | - return []; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $shares; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Adds shares to propfind response |
|
| 184 | - * |
|
| 185 | - * @param PropFind $propFind propfind object |
|
| 186 | - * @param \Sabre\DAV\INode $sabreNode sabre node |
|
| 187 | - */ |
|
| 188 | - public function handleGetProperties( |
|
| 189 | - PropFind $propFind, |
|
| 190 | - \Sabre\DAV\INode $sabreNode |
|
| 191 | - ) { |
|
| 192 | - if (!($sabreNode instanceof DavNode)) { |
|
| 193 | - return; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // need prefetch ? |
|
| 197 | - if ($sabreNode instanceof Directory |
|
| 198 | - && $propFind->getDepth() !== 0 |
|
| 199 | - && ( |
|
| 200 | - !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) || |
|
| 201 | - !is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME)) |
|
| 202 | - ) |
|
| 203 | - ) { |
|
| 204 | - $folderNode = $sabreNode->getNode(); |
|
| 205 | - $this->cachedFolders[] = $sabreNode->getPath(); |
|
| 206 | - $childShares = $this->getSharesFolder($folderNode); |
|
| 207 | - foreach ($childShares as $id => $shares) { |
|
| 208 | - $this->cachedShares[$id] = $shares; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 213 | - $shares = $this->getShares($sabreNode); |
|
| 214 | - |
|
| 215 | - $shareTypes = array_unique(array_map(function (IShare $share) { |
|
| 216 | - return $share->getShareType(); |
|
| 217 | - }, $shares)); |
|
| 218 | - |
|
| 219 | - return new ShareTypeList($shareTypes); |
|
| 220 | - }); |
|
| 221 | - |
|
| 222 | - $propFind->handle(self::SHAREES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 223 | - $shares = $this->getShares($sabreNode); |
|
| 224 | - |
|
| 225 | - return new ShareeList($shares); |
|
| 226 | - }); |
|
| 227 | - } |
|
| 42 | + public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 43 | + public const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
| 44 | + public const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types'; |
|
| 45 | + public const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Reference to main server object |
|
| 49 | + * |
|
| 50 | + * @var \Sabre\DAV\Server |
|
| 51 | + */ |
|
| 52 | + private $server; |
|
| 53 | + |
|
| 54 | + /** @var \OCP\Share\IManager */ |
|
| 55 | + private $shareManager; |
|
| 56 | + |
|
| 57 | + /** @var \Sabre\DAV\Tree */ |
|
| 58 | + private $tree; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + private $userId; |
|
| 62 | + |
|
| 63 | + /** @var \OCP\Files\Folder */ |
|
| 64 | + private $userFolder; |
|
| 65 | + |
|
| 66 | + /** @var IShare[][] */ |
|
| 67 | + private $cachedShares = []; |
|
| 68 | + |
|
| 69 | + /** @var string[] */ |
|
| 70 | + private $cachedFolders = []; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param \Sabre\DAV\Tree $tree tree |
|
| 74 | + * @param IUserSession $userSession user session |
|
| 75 | + * @param \OCP\Files\Folder $userFolder user home folder |
|
| 76 | + * @param \OCP\Share\IManager $shareManager share manager |
|
| 77 | + */ |
|
| 78 | + public function __construct( |
|
| 79 | + \Sabre\DAV\Tree $tree, |
|
| 80 | + IUserSession $userSession, |
|
| 81 | + \OCP\Files\Folder $userFolder, |
|
| 82 | + \OCP\Share\IManager $shareManager |
|
| 83 | + ) { |
|
| 84 | + $this->tree = $tree; |
|
| 85 | + $this->shareManager = $shareManager; |
|
| 86 | + $this->userFolder = $userFolder; |
|
| 87 | + $this->userId = $userSession->getUser()->getUID(); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * This initializes the plugin. |
|
| 92 | + * |
|
| 93 | + * This function is called by \Sabre\DAV\Server, after |
|
| 94 | + * addPlugin is called. |
|
| 95 | + * |
|
| 96 | + * This method should set up the required event subscriptions. |
|
| 97 | + * |
|
| 98 | + * @param \Sabre\DAV\Server $server |
|
| 99 | + */ |
|
| 100 | + public function initialize(\Sabre\DAV\Server $server) { |
|
| 101 | + $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 102 | + $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class; |
|
| 103 | + $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME; |
|
| 104 | + $server->protectedProperties[] = self::SHAREES_PROPERTYNAME; |
|
| 105 | + |
|
| 106 | + $this->server = $server; |
|
| 107 | + $this->server->on('propFind', [$this, 'handleGetProperties']); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param \OCP\Files\Node $node |
|
| 112 | + * @return IShare[] |
|
| 113 | + */ |
|
| 114 | + private function getShare(\OCP\Files\Node $node): array { |
|
| 115 | + $result = []; |
|
| 116 | + $requestedShareTypes = [ |
|
| 117 | + IShare::TYPE_USER, |
|
| 118 | + IShare::TYPE_GROUP, |
|
| 119 | + IShare::TYPE_LINK, |
|
| 120 | + IShare::TYPE_REMOTE, |
|
| 121 | + IShare::TYPE_EMAIL, |
|
| 122 | + IShare::TYPE_ROOM, |
|
| 123 | + IShare::TYPE_CIRCLE, |
|
| 124 | + IShare::TYPE_DECK, |
|
| 125 | + ]; |
|
| 126 | + foreach ($requestedShareTypes as $requestedShareType) { |
|
| 127 | + $shares = $this->shareManager->getSharesBy( |
|
| 128 | + $this->userId, |
|
| 129 | + $requestedShareType, |
|
| 130 | + $node, |
|
| 131 | + false, |
|
| 132 | + -1 |
|
| 133 | + ); |
|
| 134 | + foreach ($shares as $share) { |
|
| 135 | + $result[] = $share; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + return $result; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param Folder $node |
|
| 143 | + * @return IShare[][] |
|
| 144 | + */ |
|
| 145 | + private function getSharesFolder(Folder $node): array { |
|
| 146 | + return $this->shareManager->getSharesInFolder( |
|
| 147 | + $this->userId, |
|
| 148 | + $node, |
|
| 149 | + true |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param DavNode $sabreNode |
|
| 155 | + * @return IShare[] |
|
| 156 | + */ |
|
| 157 | + private function getShares(DavNode $sabreNode): array { |
|
| 158 | + if (isset($this->cachedShares[$sabreNode->getId()])) { |
|
| 159 | + $shares = $this->cachedShares[$sabreNode->getId()]; |
|
| 160 | + } else { |
|
| 161 | + [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath()); |
|
| 162 | + if ($parentPath === '') { |
|
| 163 | + $parentPath = '/'; |
|
| 164 | + } |
|
| 165 | + // if we already cached the folder this file is in we know there are no shares for this file |
|
| 166 | + if (array_search($parentPath, $this->cachedFolders) === false) { |
|
| 167 | + try { |
|
| 168 | + $node = $sabreNode->getNode(); |
|
| 169 | + } catch (NotFoundException $e) { |
|
| 170 | + return []; |
|
| 171 | + } |
|
| 172 | + $shares = $this->getShare($node); |
|
| 173 | + $this->cachedShares[$sabreNode->getId()] = $shares; |
|
| 174 | + } else { |
|
| 175 | + return []; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $shares; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Adds shares to propfind response |
|
| 184 | + * |
|
| 185 | + * @param PropFind $propFind propfind object |
|
| 186 | + * @param \Sabre\DAV\INode $sabreNode sabre node |
|
| 187 | + */ |
|
| 188 | + public function handleGetProperties( |
|
| 189 | + PropFind $propFind, |
|
| 190 | + \Sabre\DAV\INode $sabreNode |
|
| 191 | + ) { |
|
| 192 | + if (!($sabreNode instanceof DavNode)) { |
|
| 193 | + return; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // need prefetch ? |
|
| 197 | + if ($sabreNode instanceof Directory |
|
| 198 | + && $propFind->getDepth() !== 0 |
|
| 199 | + && ( |
|
| 200 | + !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) || |
|
| 201 | + !is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME)) |
|
| 202 | + ) |
|
| 203 | + ) { |
|
| 204 | + $folderNode = $sabreNode->getNode(); |
|
| 205 | + $this->cachedFolders[] = $sabreNode->getPath(); |
|
| 206 | + $childShares = $this->getSharesFolder($folderNode); |
|
| 207 | + foreach ($childShares as $id => $shares) { |
|
| 208 | + $this->cachedShares[$id] = $shares; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 213 | + $shares = $this->getShares($sabreNode); |
|
| 214 | + |
|
| 215 | + $shareTypes = array_unique(array_map(function (IShare $share) { |
|
| 216 | + return $share->getShareType(); |
|
| 217 | + }, $shares)); |
|
| 218 | + |
|
| 219 | + return new ShareTypeList($shareTypes); |
|
| 220 | + }); |
|
| 221 | + |
|
| 222 | + $propFind->handle(self::SHAREES_PROPERTYNAME, function () use ($sabreNode) { |
|
| 223 | + $shares = $this->getShares($sabreNode); |
|
| 224 | + |
|
| 225 | + return new ShareeList($shares); |
|
| 226 | + }); |
|
| 227 | + } |
|
| 228 | 228 | } |
@@ -50,399 +50,399 @@ |
||
| 50 | 50 | use OCP\IUserManager; |
| 51 | 51 | |
| 52 | 52 | class Folder extends Node implements \OCP\Files\Folder { |
| 53 | - /** |
|
| 54 | - * Creates a Folder that represents a non-existing path |
|
| 55 | - * |
|
| 56 | - * @param string $path path |
|
| 57 | - * @return string non-existing node class |
|
| 58 | - */ |
|
| 59 | - protected function createNonExistingNode($path) { |
|
| 60 | - return new NonExistingFolder($this->root, $this->view, $path); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param string $path path relative to the folder |
|
| 65 | - * @return string |
|
| 66 | - * @throws \OCP\Files\NotPermittedException |
|
| 67 | - */ |
|
| 68 | - public function getFullPath($path) { |
|
| 69 | - if (!$this->isValidPath($path)) { |
|
| 70 | - throw new NotPermittedException('Invalid path'); |
|
| 71 | - } |
|
| 72 | - return $this->path . $this->normalizePath($path); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $path |
|
| 77 | - * @return string|null |
|
| 78 | - */ |
|
| 79 | - public function getRelativePath($path) { |
|
| 80 | - return PathHelper::getRelativePath($this->getPath(), $path); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * check if a node is a (grand-)child of the folder |
|
| 85 | - * |
|
| 86 | - * @param \OC\Files\Node\Node $node |
|
| 87 | - * @return bool |
|
| 88 | - */ |
|
| 89 | - public function isSubNode($node) { |
|
| 90 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * get the content of this directory |
|
| 95 | - * |
|
| 96 | - * @return Node[] |
|
| 97 | - * @throws \OCP\Files\NotFoundException |
|
| 98 | - */ |
|
| 99 | - public function getDirectoryListing() { |
|
| 100 | - $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo()); |
|
| 101 | - |
|
| 102 | - return array_map(function (FileInfo $info) { |
|
| 103 | - if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
|
| 104 | - return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 105 | - } else { |
|
| 106 | - return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 107 | - } |
|
| 108 | - }, $folderContent); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $path |
|
| 113 | - * @param FileInfo $info |
|
| 114 | - * @return File|Folder |
|
| 115 | - */ |
|
| 116 | - protected function createNode($path, FileInfo $info = null) { |
|
| 117 | - if (is_null($info)) { |
|
| 118 | - $isDir = $this->view->is_dir($path); |
|
| 119 | - } else { |
|
| 120 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 121 | - } |
|
| 122 | - if ($isDir) { |
|
| 123 | - return new Folder($this->root, $this->view, $path, $info); |
|
| 124 | - } else { |
|
| 125 | - return new File($this->root, $this->view, $path, $info); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the node at $path |
|
| 131 | - * |
|
| 132 | - * @param string $path |
|
| 133 | - * @return \OC\Files\Node\Node |
|
| 134 | - * @throws \OCP\Files\NotFoundException |
|
| 135 | - */ |
|
| 136 | - public function get($path) { |
|
| 137 | - return $this->root->get($this->getFullPath($path)); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $path |
|
| 142 | - * @return bool |
|
| 143 | - */ |
|
| 144 | - public function nodeExists($path) { |
|
| 145 | - try { |
|
| 146 | - $this->get($path); |
|
| 147 | - return true; |
|
| 148 | - } catch (NotFoundException $e) { |
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param string $path |
|
| 155 | - * @return \OC\Files\Node\Folder |
|
| 156 | - * @throws \OCP\Files\NotPermittedException |
|
| 157 | - */ |
|
| 158 | - public function newFolder($path) { |
|
| 159 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 160 | - $fullPath = $this->getFullPath($path); |
|
| 161 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 162 | - $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
| 163 | - if (!$this->view->mkdir($fullPath)) { |
|
| 164 | - throw new NotPermittedException('Could not create folder'); |
|
| 165 | - } |
|
| 166 | - $node = new Folder($this->root, $this->view, $fullPath); |
|
| 167 | - $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
| 168 | - return $node; |
|
| 169 | - } else { |
|
| 170 | - throw new NotPermittedException('No create permission for folder'); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param string $path |
|
| 176 | - * @param string | resource | null $content |
|
| 177 | - * @return \OC\Files\Node\File |
|
| 178 | - * @throws \OCP\Files\NotPermittedException |
|
| 179 | - */ |
|
| 180 | - public function newFile($path, $content = null) { |
|
| 181 | - if (empty($path)) { |
|
| 182 | - throw new NotPermittedException('Could not create as provided path is empty'); |
|
| 183 | - } |
|
| 184 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 185 | - $fullPath = $this->getFullPath($path); |
|
| 186 | - $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 187 | - $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
| 188 | - if ($content !== null) { |
|
| 189 | - $result = $this->view->file_put_contents($fullPath, $content); |
|
| 190 | - } else { |
|
| 191 | - $result = $this->view->touch($fullPath); |
|
| 192 | - } |
|
| 193 | - if ($result === false) { |
|
| 194 | - throw new NotPermittedException('Could not create path'); |
|
| 195 | - } |
|
| 196 | - $node = new File($this->root, $this->view, $fullPath); |
|
| 197 | - $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
| 198 | - return $node; |
|
| 199 | - } |
|
| 200 | - throw new NotPermittedException('No create permission for path'); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { |
|
| 204 | - if ($uid === null) { |
|
| 205 | - $user = null; |
|
| 206 | - } else { |
|
| 207 | - /** @var IUserManager $userManager */ |
|
| 208 | - $userManager = \OC::$server->query(IUserManager::class); |
|
| 209 | - $user = $userManager->get($uid); |
|
| 210 | - } |
|
| 211 | - return new SearchQuery($operator, 0, 0, [], $user); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * search for files with the name matching $query |
|
| 216 | - * |
|
| 217 | - * @param string|ISearchQuery $query |
|
| 218 | - * @return \OC\Files\Node\Node[] |
|
| 219 | - */ |
|
| 220 | - public function search($query) { |
|
| 221 | - if (is_string($query)) { |
|
| 222 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // search is handled by a single query covering all caches that this folder contains |
|
| 226 | - // this is done by collect |
|
| 227 | - |
|
| 228 | - $limitToHome = $query->limitToHome(); |
|
| 229 | - if ($limitToHome && count(explode('/', $this->path)) !== 3) { |
|
| 230 | - throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $rootLength = strlen($this->path); |
|
| 234 | - $mount = $this->root->getMount($this->path); |
|
| 235 | - $storage = $mount->getStorage(); |
|
| 236 | - $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | - |
|
| 238 | - // collect all caches for this folder, indexed by their mountpoint relative to this folder |
|
| 239 | - // and save the mount which is needed later to construct the FileInfo objects |
|
| 240 | - |
|
| 241 | - if ($internalPath !== '') { |
|
| 242 | - // a temporary CacheJail is used to handle filtering down the results to within this folder |
|
| 243 | - $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)]; |
|
| 244 | - } else { |
|
| 245 | - $caches = ['' => $storage->getCache('')]; |
|
| 246 | - } |
|
| 247 | - $mountByMountPoint = ['' => $mount]; |
|
| 248 | - |
|
| 249 | - if (!$limitToHome) { |
|
| 250 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 251 | - foreach ($mounts as $mount) { |
|
| 252 | - $storage = $mount->getStorage(); |
|
| 253 | - if ($storage) { |
|
| 254 | - $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
| 255 | - $caches[$relativeMountPoint] = $storage->getCache(''); |
|
| 256 | - $mountByMountPoint[$relativeMountPoint] = $mount; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** @var QuerySearchHelper $searchHelper */ |
|
| 262 | - $searchHelper = \OC::$server->get(QuerySearchHelper::class); |
|
| 263 | - $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
|
| 264 | - |
|
| 265 | - // loop trough all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
|
| 266 | - $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
| 267 | - $mount = $mountByMountPoint[$relativeMountPoint]; |
|
| 268 | - return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
| 269 | - return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
|
| 270 | - }, $results); |
|
| 271 | - }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
|
| 272 | - |
|
| 273 | - // don't include this folder in the results |
|
| 274 | - $files = array_filter($files, function (FileInfo $file) { |
|
| 275 | - return $file->getPath() !== $this->getPath(); |
|
| 276 | - }); |
|
| 277 | - |
|
| 278 | - // since results were returned per-cache, they are no longer fully sorted |
|
| 279 | - $order = $query->getOrder(); |
|
| 280 | - if ($order) { |
|
| 281 | - usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
| 282 | - foreach ($order as $orderField) { |
|
| 283 | - $cmp = $orderField->sortFileInfo($a, $b); |
|
| 284 | - if ($cmp !== 0) { |
|
| 285 | - return $cmp; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - return 0; |
|
| 289 | - }); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - return array_map(function (FileInfo $file) { |
|
| 293 | - return $this->createNode($file->getPath(), $file); |
|
| 294 | - }, $files); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
|
| 298 | - $cacheEntry['internalPath'] = $cacheEntry['path']; |
|
| 299 | - $cacheEntry['path'] = $appendRoot . $cacheEntry->getPath(); |
|
| 300 | - $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
| 301 | - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * search for files by mimetype |
|
| 306 | - * |
|
| 307 | - * @param string $mimetype |
|
| 308 | - * @return Node[] |
|
| 309 | - */ |
|
| 310 | - public function searchByMime($mimetype) { |
|
| 311 | - if (strpos($mimetype, '/') === false) { |
|
| 312 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
| 313 | - } else { |
|
| 314 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
|
| 315 | - } |
|
| 316 | - return $this->search($query); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * search for files by tag |
|
| 321 | - * |
|
| 322 | - * @param string|int $tag name or tag id |
|
| 323 | - * @param string $userId owner of the tags |
|
| 324 | - * @return Node[] |
|
| 325 | - */ |
|
| 326 | - public function searchByTag($tag, $userId) { |
|
| 327 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId); |
|
| 328 | - return $this->search($query); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @param int $id |
|
| 333 | - * @return \OC\Files\Node\Node[] |
|
| 334 | - */ |
|
| 335 | - public function getById($id) { |
|
| 336 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - protected function getAppDataDirectoryName(): string { |
|
| 340 | - $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
|
| 341 | - return 'appdata_' . $instanceId; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * In case the path we are currently in is inside the appdata_* folder, |
|
| 346 | - * the original getById method does not work, because it can only look inside |
|
| 347 | - * the user's mount points. But the user has no mount point for the root storage. |
|
| 348 | - * |
|
| 349 | - * So in that case we directly check the mount of the root if it contains |
|
| 350 | - * the id. If it does we check if the path is inside the path we are working |
|
| 351 | - * in. |
|
| 352 | - * |
|
| 353 | - * @param int $id |
|
| 354 | - * @return array |
|
| 355 | - */ |
|
| 356 | - protected function getByIdInRootMount(int $id): array { |
|
| 357 | - $mount = $this->root->getMount(''); |
|
| 358 | - $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id); |
|
| 359 | - if (!$cacheEntry) { |
|
| 360 | - return []; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
| 364 | - $currentPath = rtrim($this->path, '/') . '/'; |
|
| 365 | - |
|
| 366 | - if (strpos($absolutePath, $currentPath) !== 0) { |
|
| 367 | - return []; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return [$this->root->createNode( |
|
| 371 | - $absolutePath, new \OC\Files\FileInfo( |
|
| 372 | - $absolutePath, |
|
| 373 | - $mount->getStorage(), |
|
| 374 | - $cacheEntry->getPath(), |
|
| 375 | - $cacheEntry, |
|
| 376 | - $mount |
|
| 377 | - ))]; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - public function getFreeSpace() { |
|
| 381 | - return $this->view->free_space($this->path); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - public function delete() { |
|
| 385 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 386 | - $this->sendHooks(['preDelete']); |
|
| 387 | - $fileInfo = $this->getFileInfo(); |
|
| 388 | - $this->view->rmdir($this->path); |
|
| 389 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 390 | - $this->sendHooks(['postDelete'], [$nonExisting]); |
|
| 391 | - $this->exists = false; |
|
| 392 | - } else { |
|
| 393 | - throw new NotPermittedException('No delete permission for path'); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Add a suffix to the name in case the file exists |
|
| 399 | - * |
|
| 400 | - * @param string $name |
|
| 401 | - * @return string |
|
| 402 | - * @throws NotPermittedException |
|
| 403 | - */ |
|
| 404 | - public function getNonExistingName($name) { |
|
| 405 | - $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 406 | - return trim($this->getRelativePath($uniqueName), '/'); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * @param int $limit |
|
| 411 | - * @param int $offset |
|
| 412 | - * @return \OCP\Files\Node[] |
|
| 413 | - */ |
|
| 414 | - public function getRecent($limit, $offset = 0) { |
|
| 415 | - $query = new SearchQuery( |
|
| 416 | - new SearchBinaryOperator( |
|
| 417 | - // filter out non empty folders |
|
| 418 | - ISearchBinaryOperator::OPERATOR_OR, |
|
| 419 | - [ |
|
| 420 | - new SearchBinaryOperator( |
|
| 421 | - ISearchBinaryOperator::OPERATOR_NOT, |
|
| 422 | - [ |
|
| 423 | - new SearchComparison( |
|
| 424 | - ISearchComparison::COMPARE_EQUAL, |
|
| 425 | - 'mimetype', |
|
| 426 | - FileInfo::MIMETYPE_FOLDER |
|
| 427 | - ), |
|
| 428 | - ] |
|
| 429 | - ), |
|
| 430 | - new SearchComparison( |
|
| 431 | - ISearchComparison::COMPARE_EQUAL, |
|
| 432 | - 'size', |
|
| 433 | - 0 |
|
| 434 | - ), |
|
| 435 | - ] |
|
| 436 | - ), |
|
| 437 | - $limit, |
|
| 438 | - $offset, |
|
| 439 | - [ |
|
| 440 | - new SearchOrder( |
|
| 441 | - ISearchOrder::DIRECTION_DESCENDING, |
|
| 442 | - 'mtime' |
|
| 443 | - ), |
|
| 444 | - ] |
|
| 445 | - ); |
|
| 446 | - return $this->search($query); |
|
| 447 | - } |
|
| 53 | + /** |
|
| 54 | + * Creates a Folder that represents a non-existing path |
|
| 55 | + * |
|
| 56 | + * @param string $path path |
|
| 57 | + * @return string non-existing node class |
|
| 58 | + */ |
|
| 59 | + protected function createNonExistingNode($path) { |
|
| 60 | + return new NonExistingFolder($this->root, $this->view, $path); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param string $path path relative to the folder |
|
| 65 | + * @return string |
|
| 66 | + * @throws \OCP\Files\NotPermittedException |
|
| 67 | + */ |
|
| 68 | + public function getFullPath($path) { |
|
| 69 | + if (!$this->isValidPath($path)) { |
|
| 70 | + throw new NotPermittedException('Invalid path'); |
|
| 71 | + } |
|
| 72 | + return $this->path . $this->normalizePath($path); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $path |
|
| 77 | + * @return string|null |
|
| 78 | + */ |
|
| 79 | + public function getRelativePath($path) { |
|
| 80 | + return PathHelper::getRelativePath($this->getPath(), $path); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * check if a node is a (grand-)child of the folder |
|
| 85 | + * |
|
| 86 | + * @param \OC\Files\Node\Node $node |
|
| 87 | + * @return bool |
|
| 88 | + */ |
|
| 89 | + public function isSubNode($node) { |
|
| 90 | + return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * get the content of this directory |
|
| 95 | + * |
|
| 96 | + * @return Node[] |
|
| 97 | + * @throws \OCP\Files\NotFoundException |
|
| 98 | + */ |
|
| 99 | + public function getDirectoryListing() { |
|
| 100 | + $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo()); |
|
| 101 | + |
|
| 102 | + return array_map(function (FileInfo $info) { |
|
| 103 | + if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
|
| 104 | + return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 105 | + } else { |
|
| 106 | + return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 107 | + } |
|
| 108 | + }, $folderContent); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $path |
|
| 113 | + * @param FileInfo $info |
|
| 114 | + * @return File|Folder |
|
| 115 | + */ |
|
| 116 | + protected function createNode($path, FileInfo $info = null) { |
|
| 117 | + if (is_null($info)) { |
|
| 118 | + $isDir = $this->view->is_dir($path); |
|
| 119 | + } else { |
|
| 120 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 121 | + } |
|
| 122 | + if ($isDir) { |
|
| 123 | + return new Folder($this->root, $this->view, $path, $info); |
|
| 124 | + } else { |
|
| 125 | + return new File($this->root, $this->view, $path, $info); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the node at $path |
|
| 131 | + * |
|
| 132 | + * @param string $path |
|
| 133 | + * @return \OC\Files\Node\Node |
|
| 134 | + * @throws \OCP\Files\NotFoundException |
|
| 135 | + */ |
|
| 136 | + public function get($path) { |
|
| 137 | + return $this->root->get($this->getFullPath($path)); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $path |
|
| 142 | + * @return bool |
|
| 143 | + */ |
|
| 144 | + public function nodeExists($path) { |
|
| 145 | + try { |
|
| 146 | + $this->get($path); |
|
| 147 | + return true; |
|
| 148 | + } catch (NotFoundException $e) { |
|
| 149 | + return false; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param string $path |
|
| 155 | + * @return \OC\Files\Node\Folder |
|
| 156 | + * @throws \OCP\Files\NotPermittedException |
|
| 157 | + */ |
|
| 158 | + public function newFolder($path) { |
|
| 159 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 160 | + $fullPath = $this->getFullPath($path); |
|
| 161 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 162 | + $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
| 163 | + if (!$this->view->mkdir($fullPath)) { |
|
| 164 | + throw new NotPermittedException('Could not create folder'); |
|
| 165 | + } |
|
| 166 | + $node = new Folder($this->root, $this->view, $fullPath); |
|
| 167 | + $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
| 168 | + return $node; |
|
| 169 | + } else { |
|
| 170 | + throw new NotPermittedException('No create permission for folder'); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param string $path |
|
| 176 | + * @param string | resource | null $content |
|
| 177 | + * @return \OC\Files\Node\File |
|
| 178 | + * @throws \OCP\Files\NotPermittedException |
|
| 179 | + */ |
|
| 180 | + public function newFile($path, $content = null) { |
|
| 181 | + if (empty($path)) { |
|
| 182 | + throw new NotPermittedException('Could not create as provided path is empty'); |
|
| 183 | + } |
|
| 184 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 185 | + $fullPath = $this->getFullPath($path); |
|
| 186 | + $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 187 | + $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]); |
|
| 188 | + if ($content !== null) { |
|
| 189 | + $result = $this->view->file_put_contents($fullPath, $content); |
|
| 190 | + } else { |
|
| 191 | + $result = $this->view->touch($fullPath); |
|
| 192 | + } |
|
| 193 | + if ($result === false) { |
|
| 194 | + throw new NotPermittedException('Could not create path'); |
|
| 195 | + } |
|
| 196 | + $node = new File($this->root, $this->view, $fullPath); |
|
| 197 | + $this->sendHooks(['postWrite', 'postCreate'], [$node]); |
|
| 198 | + return $node; |
|
| 199 | + } |
|
| 200 | + throw new NotPermittedException('No create permission for path'); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { |
|
| 204 | + if ($uid === null) { |
|
| 205 | + $user = null; |
|
| 206 | + } else { |
|
| 207 | + /** @var IUserManager $userManager */ |
|
| 208 | + $userManager = \OC::$server->query(IUserManager::class); |
|
| 209 | + $user = $userManager->get($uid); |
|
| 210 | + } |
|
| 211 | + return new SearchQuery($operator, 0, 0, [], $user); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * search for files with the name matching $query |
|
| 216 | + * |
|
| 217 | + * @param string|ISearchQuery $query |
|
| 218 | + * @return \OC\Files\Node\Node[] |
|
| 219 | + */ |
|
| 220 | + public function search($query) { |
|
| 221 | + if (is_string($query)) { |
|
| 222 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // search is handled by a single query covering all caches that this folder contains |
|
| 226 | + // this is done by collect |
|
| 227 | + |
|
| 228 | + $limitToHome = $query->limitToHome(); |
|
| 229 | + if ($limitToHome && count(explode('/', $this->path)) !== 3) { |
|
| 230 | + throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $rootLength = strlen($this->path); |
|
| 234 | + $mount = $this->root->getMount($this->path); |
|
| 235 | + $storage = $mount->getStorage(); |
|
| 236 | + $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | + |
|
| 238 | + // collect all caches for this folder, indexed by their mountpoint relative to this folder |
|
| 239 | + // and save the mount which is needed later to construct the FileInfo objects |
|
| 240 | + |
|
| 241 | + if ($internalPath !== '') { |
|
| 242 | + // a temporary CacheJail is used to handle filtering down the results to within this folder |
|
| 243 | + $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)]; |
|
| 244 | + } else { |
|
| 245 | + $caches = ['' => $storage->getCache('')]; |
|
| 246 | + } |
|
| 247 | + $mountByMountPoint = ['' => $mount]; |
|
| 248 | + |
|
| 249 | + if (!$limitToHome) { |
|
| 250 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 251 | + foreach ($mounts as $mount) { |
|
| 252 | + $storage = $mount->getStorage(); |
|
| 253 | + if ($storage) { |
|
| 254 | + $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
| 255 | + $caches[$relativeMountPoint] = $storage->getCache(''); |
|
| 256 | + $mountByMountPoint[$relativeMountPoint] = $mount; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** @var QuerySearchHelper $searchHelper */ |
|
| 262 | + $searchHelper = \OC::$server->get(QuerySearchHelper::class); |
|
| 263 | + $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
|
| 264 | + |
|
| 265 | + // loop trough all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
|
| 266 | + $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
| 267 | + $mount = $mountByMountPoint[$relativeMountPoint]; |
|
| 268 | + return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
| 269 | + return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
|
| 270 | + }, $results); |
|
| 271 | + }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
|
| 272 | + |
|
| 273 | + // don't include this folder in the results |
|
| 274 | + $files = array_filter($files, function (FileInfo $file) { |
|
| 275 | + return $file->getPath() !== $this->getPath(); |
|
| 276 | + }); |
|
| 277 | + |
|
| 278 | + // since results were returned per-cache, they are no longer fully sorted |
|
| 279 | + $order = $query->getOrder(); |
|
| 280 | + if ($order) { |
|
| 281 | + usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
| 282 | + foreach ($order as $orderField) { |
|
| 283 | + $cmp = $orderField->sortFileInfo($a, $b); |
|
| 284 | + if ($cmp !== 0) { |
|
| 285 | + return $cmp; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + return 0; |
|
| 289 | + }); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + return array_map(function (FileInfo $file) { |
|
| 293 | + return $this->createNode($file->getPath(), $file); |
|
| 294 | + }, $files); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
|
| 298 | + $cacheEntry['internalPath'] = $cacheEntry['path']; |
|
| 299 | + $cacheEntry['path'] = $appendRoot . $cacheEntry->getPath(); |
|
| 300 | + $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
| 301 | + return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * search for files by mimetype |
|
| 306 | + * |
|
| 307 | + * @param string $mimetype |
|
| 308 | + * @return Node[] |
|
| 309 | + */ |
|
| 310 | + public function searchByMime($mimetype) { |
|
| 311 | + if (strpos($mimetype, '/') === false) { |
|
| 312 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
| 313 | + } else { |
|
| 314 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
|
| 315 | + } |
|
| 316 | + return $this->search($query); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * search for files by tag |
|
| 321 | + * |
|
| 322 | + * @param string|int $tag name or tag id |
|
| 323 | + * @param string $userId owner of the tags |
|
| 324 | + * @return Node[] |
|
| 325 | + */ |
|
| 326 | + public function searchByTag($tag, $userId) { |
|
| 327 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId); |
|
| 328 | + return $this->search($query); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @param int $id |
|
| 333 | + * @return \OC\Files\Node\Node[] |
|
| 334 | + */ |
|
| 335 | + public function getById($id) { |
|
| 336 | + return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + protected function getAppDataDirectoryName(): string { |
|
| 340 | + $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
|
| 341 | + return 'appdata_' . $instanceId; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * In case the path we are currently in is inside the appdata_* folder, |
|
| 346 | + * the original getById method does not work, because it can only look inside |
|
| 347 | + * the user's mount points. But the user has no mount point for the root storage. |
|
| 348 | + * |
|
| 349 | + * So in that case we directly check the mount of the root if it contains |
|
| 350 | + * the id. If it does we check if the path is inside the path we are working |
|
| 351 | + * in. |
|
| 352 | + * |
|
| 353 | + * @param int $id |
|
| 354 | + * @return array |
|
| 355 | + */ |
|
| 356 | + protected function getByIdInRootMount(int $id): array { |
|
| 357 | + $mount = $this->root->getMount(''); |
|
| 358 | + $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id); |
|
| 359 | + if (!$cacheEntry) { |
|
| 360 | + return []; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
| 364 | + $currentPath = rtrim($this->path, '/') . '/'; |
|
| 365 | + |
|
| 366 | + if (strpos($absolutePath, $currentPath) !== 0) { |
|
| 367 | + return []; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return [$this->root->createNode( |
|
| 371 | + $absolutePath, new \OC\Files\FileInfo( |
|
| 372 | + $absolutePath, |
|
| 373 | + $mount->getStorage(), |
|
| 374 | + $cacheEntry->getPath(), |
|
| 375 | + $cacheEntry, |
|
| 376 | + $mount |
|
| 377 | + ))]; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + public function getFreeSpace() { |
|
| 381 | + return $this->view->free_space($this->path); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + public function delete() { |
|
| 385 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 386 | + $this->sendHooks(['preDelete']); |
|
| 387 | + $fileInfo = $this->getFileInfo(); |
|
| 388 | + $this->view->rmdir($this->path); |
|
| 389 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 390 | + $this->sendHooks(['postDelete'], [$nonExisting]); |
|
| 391 | + $this->exists = false; |
|
| 392 | + } else { |
|
| 393 | + throw new NotPermittedException('No delete permission for path'); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Add a suffix to the name in case the file exists |
|
| 399 | + * |
|
| 400 | + * @param string $name |
|
| 401 | + * @return string |
|
| 402 | + * @throws NotPermittedException |
|
| 403 | + */ |
|
| 404 | + public function getNonExistingName($name) { |
|
| 405 | + $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 406 | + return trim($this->getRelativePath($uniqueName), '/'); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * @param int $limit |
|
| 411 | + * @param int $offset |
|
| 412 | + * @return \OCP\Files\Node[] |
|
| 413 | + */ |
|
| 414 | + public function getRecent($limit, $offset = 0) { |
|
| 415 | + $query = new SearchQuery( |
|
| 416 | + new SearchBinaryOperator( |
|
| 417 | + // filter out non empty folders |
|
| 418 | + ISearchBinaryOperator::OPERATOR_OR, |
|
| 419 | + [ |
|
| 420 | + new SearchBinaryOperator( |
|
| 421 | + ISearchBinaryOperator::OPERATOR_NOT, |
|
| 422 | + [ |
|
| 423 | + new SearchComparison( |
|
| 424 | + ISearchComparison::COMPARE_EQUAL, |
|
| 425 | + 'mimetype', |
|
| 426 | + FileInfo::MIMETYPE_FOLDER |
|
| 427 | + ), |
|
| 428 | + ] |
|
| 429 | + ), |
|
| 430 | + new SearchComparison( |
|
| 431 | + ISearchComparison::COMPARE_EQUAL, |
|
| 432 | + 'size', |
|
| 433 | + 0 |
|
| 434 | + ), |
|
| 435 | + ] |
|
| 436 | + ), |
|
| 437 | + $limit, |
|
| 438 | + $offset, |
|
| 439 | + [ |
|
| 440 | + new SearchOrder( |
|
| 441 | + ISearchOrder::DIRECTION_DESCENDING, |
|
| 442 | + 'mtime' |
|
| 443 | + ), |
|
| 444 | + ] |
|
| 445 | + ); |
|
| 446 | + return $this->search($query); |
|
| 447 | + } |
|
| 448 | 448 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | if (!$this->isValidPath($path)) { |
| 70 | 70 | throw new NotPermittedException('Invalid path'); |
| 71 | 71 | } |
| 72 | - return $this->path . $this->normalizePath($path); |
|
| 72 | + return $this->path.$this->normalizePath($path); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @return bool |
| 88 | 88 | */ |
| 89 | 89 | public function isSubNode($node) { |
| 90 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 90 | + return strpos($node->getPath(), $this->path.'/') === 0; |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | public function getDirectoryListing() { |
| 100 | 100 | $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo()); |
| 101 | 101 | |
| 102 | - return array_map(function (FileInfo $info) { |
|
| 102 | + return array_map(function(FileInfo $info) { |
|
| 103 | 103 | if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) { |
| 104 | 104 | return new Folder($this->root, $this->view, $info->getPath(), $info); |
| 105 | 105 | } else { |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | */ |
| 220 | 220 | public function search($query) { |
| 221 | 221 | if (is_string($query)) { |
| 222 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); |
|
| 222 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%'.$query.'%')); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | // search is handled by a single query covering all caches that this folder contains |
@@ -263,22 +263,22 @@ discard block |
||
| 263 | 263 | $resultsPerCache = $searchHelper->searchInCaches($query, $caches); |
| 264 | 264 | |
| 265 | 265 | // loop trough all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all |
| 266 | - $files = array_merge(...array_map(function (array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
| 266 | + $files = array_merge(...array_map(function(array $results, $relativeMountPoint) use ($mountByMountPoint) { |
|
| 267 | 267 | $mount = $mountByMountPoint[$relativeMountPoint]; |
| 268 | - return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
| 268 | + return array_map(function(ICacheEntry $result) use ($relativeMountPoint, $mount) { |
|
| 269 | 269 | return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result); |
| 270 | 270 | }, $results); |
| 271 | 271 | }, array_values($resultsPerCache), array_keys($resultsPerCache))); |
| 272 | 272 | |
| 273 | 273 | // don't include this folder in the results |
| 274 | - $files = array_filter($files, function (FileInfo $file) { |
|
| 274 | + $files = array_filter($files, function(FileInfo $file) { |
|
| 275 | 275 | return $file->getPath() !== $this->getPath(); |
| 276 | 276 | }); |
| 277 | 277 | |
| 278 | 278 | // since results were returned per-cache, they are no longer fully sorted |
| 279 | 279 | $order = $query->getOrder(); |
| 280 | 280 | if ($order) { |
| 281 | - usort($files, function (FileInfo $a, FileInfo $b) use ($order) { |
|
| 281 | + usort($files, function(FileInfo $a, FileInfo $b) use ($order) { |
|
| 282 | 282 | foreach ($order as $orderField) { |
| 283 | 283 | $cmp = $orderField->sortFileInfo($a, $b); |
| 284 | 284 | if ($cmp !== 0) { |
@@ -289,16 +289,16 @@ discard block |
||
| 289 | 289 | }); |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - return array_map(function (FileInfo $file) { |
|
| 292 | + return array_map(function(FileInfo $file) { |
|
| 293 | 293 | return $this->createNode($file->getPath(), $file); |
| 294 | 294 | }, $files); |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo { |
| 298 | 298 | $cacheEntry['internalPath'] = $cacheEntry['path']; |
| 299 | - $cacheEntry['path'] = $appendRoot . $cacheEntry->getPath(); |
|
| 300 | - $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; |
|
| 301 | - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
| 299 | + $cacheEntry['path'] = $appendRoot.$cacheEntry->getPath(); |
|
| 300 | + $subPath = $cacheEntry['path'] !== '' ? '/'.$cacheEntry['path'] : ''; |
|
| 301 | + return new \OC\Files\FileInfo($this->path.$subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | */ |
| 310 | 310 | public function searchByMime($mimetype) { |
| 311 | 311 | if (strpos($mimetype, '/') === false) { |
| 312 | - $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); |
|
| 312 | + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype.'/%')); |
|
| 313 | 313 | } else { |
| 314 | 314 | $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); |
| 315 | 315 | } |
@@ -333,12 +333,12 @@ discard block |
||
| 333 | 333 | * @return \OC\Files\Node\Node[] |
| 334 | 334 | */ |
| 335 | 335 | public function getById($id) { |
| 336 | - return $this->root->getByIdInPath((int)$id, $this->getPath()); |
|
| 336 | + return $this->root->getByIdInPath((int) $id, $this->getPath()); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | protected function getAppDataDirectoryName(): string { |
| 340 | 340 | $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid'); |
| 341 | - return 'appdata_' . $instanceId; |
|
| 341 | + return 'appdata_'.$instanceId; |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | /** |
@@ -360,8 +360,8 @@ discard block |
||
| 360 | 360 | return []; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/'); |
|
| 364 | - $currentPath = rtrim($this->path, '/') . '/'; |
|
| 363 | + $absolutePath = '/'.ltrim($cacheEntry->getPath(), '/'); |
|
| 364 | + $currentPath = rtrim($this->path, '/').'/'; |
|
| 365 | 365 | |
| 366 | 366 | if (strpos($absolutePath, $currentPath) !== 0) { |
| 367 | 367 | return []; |
@@ -37,384 +37,384 @@ |
||
| 37 | 37 | use OCP\IUser; |
| 38 | 38 | |
| 39 | 39 | class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess { |
| 40 | - /** |
|
| 41 | - * @var array $data |
|
| 42 | - */ |
|
| 43 | - private $data; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var string $path |
|
| 47 | - */ |
|
| 48 | - private $path; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var \OC\Files\Storage\Storage $storage |
|
| 52 | - */ |
|
| 53 | - private $storage; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var string $internalPath |
|
| 57 | - */ |
|
| 58 | - private $internalPath; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @var \OCP\Files\Mount\IMountPoint |
|
| 62 | - */ |
|
| 63 | - private $mount; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var IUser |
|
| 67 | - */ |
|
| 68 | - private $owner; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var string[] |
|
| 72 | - */ |
|
| 73 | - private $childEtags = []; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var IMountPoint[] |
|
| 77 | - */ |
|
| 78 | - private $subMounts = []; |
|
| 79 | - |
|
| 80 | - private $subMountsUsed = false; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * The size of the file/folder without any sub mount |
|
| 84 | - * |
|
| 85 | - * @var int |
|
| 86 | - */ |
|
| 87 | - private $rawSize = 0; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string|boolean $path |
|
| 91 | - * @param Storage\Storage $storage |
|
| 92 | - * @param string $internalPath |
|
| 93 | - * @param array|ICacheEntry $data |
|
| 94 | - * @param \OCP\Files\Mount\IMountPoint $mount |
|
| 95 | - * @param \OCP\IUser|null $owner |
|
| 96 | - */ |
|
| 97 | - public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) { |
|
| 98 | - $this->path = $path; |
|
| 99 | - $this->storage = $storage; |
|
| 100 | - $this->internalPath = $internalPath; |
|
| 101 | - $this->data = $data; |
|
| 102 | - $this->mount = $mount; |
|
| 103 | - $this->owner = $owner; |
|
| 104 | - $this->rawSize = $this->data['size'] ?? 0; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public function offsetSet($offset, $value): void { |
|
| 108 | - $this->data[$offset] = $value; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - public function offsetExists($offset): bool { |
|
| 112 | - return isset($this->data[$offset]); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - public function offsetUnset($offset): void { |
|
| 116 | - unset($this->data[$offset]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @return mixed |
|
| 121 | - */ |
|
| 122 | - #[\ReturnTypeWillChange] |
|
| 123 | - public function offsetGet($offset) { |
|
| 124 | - if ($offset === 'type') { |
|
| 125 | - return $this->getType(); |
|
| 126 | - } elseif ($offset === 'etag') { |
|
| 127 | - return $this->getEtag(); |
|
| 128 | - } elseif ($offset === 'size') { |
|
| 129 | - return $this->getSize(); |
|
| 130 | - } elseif ($offset === 'mtime') { |
|
| 131 | - return $this->getMTime(); |
|
| 132 | - } elseif ($offset === 'permissions') { |
|
| 133 | - return $this->getPermissions(); |
|
| 134 | - } elseif (isset($this->data[$offset])) { |
|
| 135 | - return $this->data[$offset]; |
|
| 136 | - } else { |
|
| 137 | - return null; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getPath() { |
|
| 145 | - return $this->path; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @return \OCP\Files\Storage |
|
| 150 | - */ |
|
| 151 | - public function getStorage() { |
|
| 152 | - return $this->storage; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function getInternalPath() { |
|
| 159 | - return $this->internalPath; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get FileInfo ID or null in case of part file |
|
| 164 | - * |
|
| 165 | - * @return int|null |
|
| 166 | - */ |
|
| 167 | - public function getId() { |
|
| 168 | - return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function getMimetype() { |
|
| 175 | - return $this->data['mimetype']; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function getMimePart() { |
|
| 182 | - return $this->data['mimepart']; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - public function getName() { |
|
| 189 | - return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath()); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @return string |
|
| 194 | - */ |
|
| 195 | - public function getEtag() { |
|
| 196 | - $this->updateEntryfromSubMounts(); |
|
| 197 | - if (count($this->childEtags) > 0) { |
|
| 198 | - $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags); |
|
| 199 | - return md5($combinedEtag); |
|
| 200 | - } else { |
|
| 201 | - return $this->data['etag']; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @return int |
|
| 207 | - */ |
|
| 208 | - public function getSize($includeMounts = true) { |
|
| 209 | - if ($includeMounts) { |
|
| 210 | - $this->updateEntryfromSubMounts(); |
|
| 211 | - return isset($this->data['size']) ? 0 + $this->data['size'] : 0; |
|
| 212 | - } else { |
|
| 213 | - return $this->rawSize; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @return int |
|
| 219 | - */ |
|
| 220 | - public function getMTime() { |
|
| 221 | - $this->updateEntryfromSubMounts(); |
|
| 222 | - return (int) $this->data['mtime']; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function isEncrypted() { |
|
| 229 | - return $this->data['encrypted']; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Return the currently version used for the HMAC in the encryption app |
|
| 234 | - * |
|
| 235 | - * @return int |
|
| 236 | - */ |
|
| 237 | - public function getEncryptedVersion() { |
|
| 238 | - return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @return int |
|
| 243 | - */ |
|
| 244 | - public function getPermissions() { |
|
| 245 | - $perms = (int) $this->data['permissions']; |
|
| 246 | - if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) { |
|
| 247 | - $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 248 | - } |
|
| 249 | - return $perms; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER |
|
| 254 | - */ |
|
| 255 | - public function getType() { |
|
| 256 | - if (!isset($this->data['type'])) { |
|
| 257 | - $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE; |
|
| 258 | - } |
|
| 259 | - return $this->data['type']; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - public function getData() { |
|
| 263 | - return $this->data; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @param int $permissions |
|
| 268 | - * @return bool |
|
| 269 | - */ |
|
| 270 | - protected function checkPermissions($permissions) { |
|
| 271 | - return ($this->getPermissions() & $permissions) === $permissions; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @return bool |
|
| 276 | - */ |
|
| 277 | - public function isReadable() { |
|
| 278 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_READ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @return bool |
|
| 283 | - */ |
|
| 284 | - public function isUpdateable() { |
|
| 285 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Check whether new files or folders can be created inside this folder |
|
| 290 | - * |
|
| 291 | - * @return bool |
|
| 292 | - */ |
|
| 293 | - public function isCreatable() { |
|
| 294 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @return bool |
|
| 299 | - */ |
|
| 300 | - public function isDeletable() { |
|
| 301 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @return bool |
|
| 306 | - */ |
|
| 307 | - public function isShareable() { |
|
| 308 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Check if a file or folder is shared |
|
| 313 | - * |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - public function isShared() { |
|
| 317 | - $sid = $this->getStorage()->getId(); |
|
| 318 | - if (!is_null($sid)) { |
|
| 319 | - $sid = explode(':', $sid); |
|
| 320 | - return ($sid[0] === 'shared'); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - public function isMounted() { |
|
| 327 | - $storage = $this->getStorage(); |
|
| 328 | - if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - $sid = $storage->getId(); |
|
| 332 | - if (!is_null($sid)) { |
|
| 333 | - $sid = explode(':', $sid); |
|
| 334 | - return ($sid[0] !== 'home' and $sid[0] !== 'shared'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Get the mountpoint the file belongs to |
|
| 342 | - * |
|
| 343 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 344 | - */ |
|
| 345 | - public function getMountPoint() { |
|
| 346 | - return $this->mount; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Get the owner of the file |
|
| 351 | - * |
|
| 352 | - * @return \OCP\IUser |
|
| 353 | - */ |
|
| 354 | - public function getOwner() { |
|
| 355 | - return $this->owner; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * @param IMountPoint[] $mounts |
|
| 360 | - */ |
|
| 361 | - public function setSubMounts(array $mounts) { |
|
| 362 | - $this->subMounts = $mounts; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - private function updateEntryfromSubMounts() { |
|
| 366 | - if ($this->subMountsUsed) { |
|
| 367 | - return; |
|
| 368 | - } |
|
| 369 | - $this->subMountsUsed = true; |
|
| 370 | - foreach ($this->subMounts as $mount) { |
|
| 371 | - $subStorage = $mount->getStorage(); |
|
| 372 | - if ($subStorage) { |
|
| 373 | - $subCache = $subStorage->getCache(''); |
|
| 374 | - $rootEntry = $subCache->get(''); |
|
| 375 | - $this->addSubEntry($rootEntry, $mount->getMountPoint()); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Add a cache entry which is the child of this folder |
|
| 382 | - * |
|
| 383 | - * Sets the size, etag and size to for cross-storage childs |
|
| 384 | - * |
|
| 385 | - * @param array|ICacheEntry $data cache entry for the child |
|
| 386 | - * @param string $entryPath full path of the child entry |
|
| 387 | - */ |
|
| 388 | - public function addSubEntry($data, $entryPath) { |
|
| 389 | - $this->data['size'] += isset($data['size']) ? $data['size'] : 0; |
|
| 390 | - if (isset($data['mtime'])) { |
|
| 391 | - $this->data['mtime'] = max($this->data['mtime'], $data['mtime']); |
|
| 392 | - } |
|
| 393 | - if (isset($data['etag'])) { |
|
| 394 | - // prefix the etag with the relative path of the subentry to propagate etag on mount moves |
|
| 395 | - $relativeEntryPath = substr($entryPath, strlen($this->getPath())); |
|
| 396 | - // attach the permissions to propagate etag on permision changes of submounts |
|
| 397 | - $permissions = isset($data['permissions']) ? $data['permissions'] : 0; |
|
| 398 | - $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions; |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @inheritdoc |
|
| 404 | - */ |
|
| 405 | - public function getChecksum() { |
|
| 406 | - return $this->data['checksum']; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - public function getExtension(): string { |
|
| 410 | - return pathinfo($this->getName(), PATHINFO_EXTENSION); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - public function getCreationTime(): int { |
|
| 414 | - return (int) $this->data['creation_time']; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - public function getUploadTime(): int { |
|
| 418 | - return (int) $this->data['upload_time']; |
|
| 419 | - } |
|
| 40 | + /** |
|
| 41 | + * @var array $data |
|
| 42 | + */ |
|
| 43 | + private $data; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var string $path |
|
| 47 | + */ |
|
| 48 | + private $path; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var \OC\Files\Storage\Storage $storage |
|
| 52 | + */ |
|
| 53 | + private $storage; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var string $internalPath |
|
| 57 | + */ |
|
| 58 | + private $internalPath; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @var \OCP\Files\Mount\IMountPoint |
|
| 62 | + */ |
|
| 63 | + private $mount; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var IUser |
|
| 67 | + */ |
|
| 68 | + private $owner; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var string[] |
|
| 72 | + */ |
|
| 73 | + private $childEtags = []; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var IMountPoint[] |
|
| 77 | + */ |
|
| 78 | + private $subMounts = []; |
|
| 79 | + |
|
| 80 | + private $subMountsUsed = false; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * The size of the file/folder without any sub mount |
|
| 84 | + * |
|
| 85 | + * @var int |
|
| 86 | + */ |
|
| 87 | + private $rawSize = 0; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string|boolean $path |
|
| 91 | + * @param Storage\Storage $storage |
|
| 92 | + * @param string $internalPath |
|
| 93 | + * @param array|ICacheEntry $data |
|
| 94 | + * @param \OCP\Files\Mount\IMountPoint $mount |
|
| 95 | + * @param \OCP\IUser|null $owner |
|
| 96 | + */ |
|
| 97 | + public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) { |
|
| 98 | + $this->path = $path; |
|
| 99 | + $this->storage = $storage; |
|
| 100 | + $this->internalPath = $internalPath; |
|
| 101 | + $this->data = $data; |
|
| 102 | + $this->mount = $mount; |
|
| 103 | + $this->owner = $owner; |
|
| 104 | + $this->rawSize = $this->data['size'] ?? 0; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public function offsetSet($offset, $value): void { |
|
| 108 | + $this->data[$offset] = $value; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + public function offsetExists($offset): bool { |
|
| 112 | + return isset($this->data[$offset]); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + public function offsetUnset($offset): void { |
|
| 116 | + unset($this->data[$offset]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @return mixed |
|
| 121 | + */ |
|
| 122 | + #[\ReturnTypeWillChange] |
|
| 123 | + public function offsetGet($offset) { |
|
| 124 | + if ($offset === 'type') { |
|
| 125 | + return $this->getType(); |
|
| 126 | + } elseif ($offset === 'etag') { |
|
| 127 | + return $this->getEtag(); |
|
| 128 | + } elseif ($offset === 'size') { |
|
| 129 | + return $this->getSize(); |
|
| 130 | + } elseif ($offset === 'mtime') { |
|
| 131 | + return $this->getMTime(); |
|
| 132 | + } elseif ($offset === 'permissions') { |
|
| 133 | + return $this->getPermissions(); |
|
| 134 | + } elseif (isset($this->data[$offset])) { |
|
| 135 | + return $this->data[$offset]; |
|
| 136 | + } else { |
|
| 137 | + return null; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getPath() { |
|
| 145 | + return $this->path; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @return \OCP\Files\Storage |
|
| 150 | + */ |
|
| 151 | + public function getStorage() { |
|
| 152 | + return $this->storage; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function getInternalPath() { |
|
| 159 | + return $this->internalPath; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get FileInfo ID or null in case of part file |
|
| 164 | + * |
|
| 165 | + * @return int|null |
|
| 166 | + */ |
|
| 167 | + public function getId() { |
|
| 168 | + return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function getMimetype() { |
|
| 175 | + return $this->data['mimetype']; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function getMimePart() { |
|
| 182 | + return $this->data['mimepart']; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + public function getName() { |
|
| 189 | + return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath()); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @return string |
|
| 194 | + */ |
|
| 195 | + public function getEtag() { |
|
| 196 | + $this->updateEntryfromSubMounts(); |
|
| 197 | + if (count($this->childEtags) > 0) { |
|
| 198 | + $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags); |
|
| 199 | + return md5($combinedEtag); |
|
| 200 | + } else { |
|
| 201 | + return $this->data['etag']; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @return int |
|
| 207 | + */ |
|
| 208 | + public function getSize($includeMounts = true) { |
|
| 209 | + if ($includeMounts) { |
|
| 210 | + $this->updateEntryfromSubMounts(); |
|
| 211 | + return isset($this->data['size']) ? 0 + $this->data['size'] : 0; |
|
| 212 | + } else { |
|
| 213 | + return $this->rawSize; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @return int |
|
| 219 | + */ |
|
| 220 | + public function getMTime() { |
|
| 221 | + $this->updateEntryfromSubMounts(); |
|
| 222 | + return (int) $this->data['mtime']; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function isEncrypted() { |
|
| 229 | + return $this->data['encrypted']; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Return the currently version used for the HMAC in the encryption app |
|
| 234 | + * |
|
| 235 | + * @return int |
|
| 236 | + */ |
|
| 237 | + public function getEncryptedVersion() { |
|
| 238 | + return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @return int |
|
| 243 | + */ |
|
| 244 | + public function getPermissions() { |
|
| 245 | + $perms = (int) $this->data['permissions']; |
|
| 246 | + if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) { |
|
| 247 | + $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 248 | + } |
|
| 249 | + return $perms; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER |
|
| 254 | + */ |
|
| 255 | + public function getType() { |
|
| 256 | + if (!isset($this->data['type'])) { |
|
| 257 | + $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE; |
|
| 258 | + } |
|
| 259 | + return $this->data['type']; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + public function getData() { |
|
| 263 | + return $this->data; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @param int $permissions |
|
| 268 | + * @return bool |
|
| 269 | + */ |
|
| 270 | + protected function checkPermissions($permissions) { |
|
| 271 | + return ($this->getPermissions() & $permissions) === $permissions; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @return bool |
|
| 276 | + */ |
|
| 277 | + public function isReadable() { |
|
| 278 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_READ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @return bool |
|
| 283 | + */ |
|
| 284 | + public function isUpdateable() { |
|
| 285 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Check whether new files or folders can be created inside this folder |
|
| 290 | + * |
|
| 291 | + * @return bool |
|
| 292 | + */ |
|
| 293 | + public function isCreatable() { |
|
| 294 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @return bool |
|
| 299 | + */ |
|
| 300 | + public function isDeletable() { |
|
| 301 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @return bool |
|
| 306 | + */ |
|
| 307 | + public function isShareable() { |
|
| 308 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Check if a file or folder is shared |
|
| 313 | + * |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + public function isShared() { |
|
| 317 | + $sid = $this->getStorage()->getId(); |
|
| 318 | + if (!is_null($sid)) { |
|
| 319 | + $sid = explode(':', $sid); |
|
| 320 | + return ($sid[0] === 'shared'); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + public function isMounted() { |
|
| 327 | + $storage = $this->getStorage(); |
|
| 328 | + if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + $sid = $storage->getId(); |
|
| 332 | + if (!is_null($sid)) { |
|
| 333 | + $sid = explode(':', $sid); |
|
| 334 | + return ($sid[0] !== 'home' and $sid[0] !== 'shared'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Get the mountpoint the file belongs to |
|
| 342 | + * |
|
| 343 | + * @return \OCP\Files\Mount\IMountPoint |
|
| 344 | + */ |
|
| 345 | + public function getMountPoint() { |
|
| 346 | + return $this->mount; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Get the owner of the file |
|
| 351 | + * |
|
| 352 | + * @return \OCP\IUser |
|
| 353 | + */ |
|
| 354 | + public function getOwner() { |
|
| 355 | + return $this->owner; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @param IMountPoint[] $mounts |
|
| 360 | + */ |
|
| 361 | + public function setSubMounts(array $mounts) { |
|
| 362 | + $this->subMounts = $mounts; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + private function updateEntryfromSubMounts() { |
|
| 366 | + if ($this->subMountsUsed) { |
|
| 367 | + return; |
|
| 368 | + } |
|
| 369 | + $this->subMountsUsed = true; |
|
| 370 | + foreach ($this->subMounts as $mount) { |
|
| 371 | + $subStorage = $mount->getStorage(); |
|
| 372 | + if ($subStorage) { |
|
| 373 | + $subCache = $subStorage->getCache(''); |
|
| 374 | + $rootEntry = $subCache->get(''); |
|
| 375 | + $this->addSubEntry($rootEntry, $mount->getMountPoint()); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Add a cache entry which is the child of this folder |
|
| 382 | + * |
|
| 383 | + * Sets the size, etag and size to for cross-storage childs |
|
| 384 | + * |
|
| 385 | + * @param array|ICacheEntry $data cache entry for the child |
|
| 386 | + * @param string $entryPath full path of the child entry |
|
| 387 | + */ |
|
| 388 | + public function addSubEntry($data, $entryPath) { |
|
| 389 | + $this->data['size'] += isset($data['size']) ? $data['size'] : 0; |
|
| 390 | + if (isset($data['mtime'])) { |
|
| 391 | + $this->data['mtime'] = max($this->data['mtime'], $data['mtime']); |
|
| 392 | + } |
|
| 393 | + if (isset($data['etag'])) { |
|
| 394 | + // prefix the etag with the relative path of the subentry to propagate etag on mount moves |
|
| 395 | + $relativeEntryPath = substr($entryPath, strlen($this->getPath())); |
|
| 396 | + // attach the permissions to propagate etag on permision changes of submounts |
|
| 397 | + $permissions = isset($data['permissions']) ? $data['permissions'] : 0; |
|
| 398 | + $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions; |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @inheritdoc |
|
| 404 | + */ |
|
| 405 | + public function getChecksum() { |
|
| 406 | + return $this->data['checksum']; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + public function getExtension(): string { |
|
| 410 | + return pathinfo($this->getName(), PATHINFO_EXTENSION); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + public function getCreationTime(): int { |
|
| 414 | + return (int) $this->data['creation_time']; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + public function getUploadTime(): int { |
|
| 418 | + return (int) $this->data['upload_time']; |
|
| 419 | + } |
|
| 420 | 420 | } |
@@ -84,2127 +84,2127 @@ |
||
| 84 | 84 | * \OC\Files\Storage\Storage object |
| 85 | 85 | */ |
| 86 | 86 | class View { |
| 87 | - /** @var string */ |
|
| 88 | - private $fakeRoot = ''; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var \OCP\Lock\ILockingProvider |
|
| 92 | - */ |
|
| 93 | - protected $lockingProvider; |
|
| 94 | - |
|
| 95 | - private $lockingEnabled; |
|
| 96 | - |
|
| 97 | - private $updaterEnabled = true; |
|
| 98 | - |
|
| 99 | - /** @var \OC\User\Manager */ |
|
| 100 | - private $userManager; |
|
| 101 | - |
|
| 102 | - /** @var \OCP\ILogger */ |
|
| 103 | - private $logger; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param string $root |
|
| 107 | - * @throws \Exception If $root contains an invalid path |
|
| 108 | - */ |
|
| 109 | - public function __construct($root = '') { |
|
| 110 | - if (is_null($root)) { |
|
| 111 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 112 | - } |
|
| 113 | - if (!Filesystem::isValidPath($root)) { |
|
| 114 | - throw new \Exception(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->fakeRoot = $root; |
|
| 118 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 119 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 120 | - $this->userManager = \OC::$server->getUserManager(); |
|
| 121 | - $this->logger = \OC::$server->getLogger(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - public function getAbsolutePath($path = '/') { |
|
| 125 | - if ($path === null) { |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - $this->assertPathLength($path); |
|
| 129 | - if ($path === '') { |
|
| 130 | - $path = '/'; |
|
| 131 | - } |
|
| 132 | - if ($path[0] !== '/') { |
|
| 133 | - $path = '/' . $path; |
|
| 134 | - } |
|
| 135 | - return $this->fakeRoot . $path; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * change the root to a fake root |
|
| 140 | - * |
|
| 141 | - * @param string $fakeRoot |
|
| 142 | - * @return boolean|null |
|
| 143 | - */ |
|
| 144 | - public function chroot($fakeRoot) { |
|
| 145 | - if (!$fakeRoot == '') { |
|
| 146 | - if ($fakeRoot[0] !== '/') { |
|
| 147 | - $fakeRoot = '/' . $fakeRoot; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - $this->fakeRoot = $fakeRoot; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * get the fake root |
|
| 155 | - * |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function getRoot() { |
|
| 159 | - return $this->fakeRoot; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * get path relative to the root of the view |
|
| 164 | - * |
|
| 165 | - * @param string $path |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - public function getRelativePath($path) { |
|
| 169 | - $this->assertPathLength($path); |
|
| 170 | - if ($this->fakeRoot == '') { |
|
| 171 | - return $path; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 175 | - return '/'; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // missing slashes can cause wrong matches! |
|
| 179 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 180 | - |
|
| 181 | - if (strpos($path, $root) !== 0) { |
|
| 182 | - return null; |
|
| 183 | - } else { |
|
| 184 | - $path = substr($path, strlen($this->fakeRoot)); |
|
| 185 | - if (strlen($path) === 0) { |
|
| 186 | - return '/'; |
|
| 187 | - } else { |
|
| 188 | - return $path; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * get the mountpoint of the storage object for a path |
|
| 195 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 196 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 197 | - * and does not take the chroot into account ) |
|
| 198 | - * |
|
| 199 | - * @param string $path |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function getMountPoint($path) { |
|
| 203 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * get the mountpoint of the storage object for a path |
|
| 208 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 209 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 210 | - * and does not take the chroot into account ) |
|
| 211 | - * |
|
| 212 | - * @param string $path |
|
| 213 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 214 | - */ |
|
| 215 | - public function getMount($path) { |
|
| 216 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * resolve a path to a storage and internal path |
|
| 221 | - * |
|
| 222 | - * @param string $path |
|
| 223 | - * @return array an array consisting of the storage and the internal path |
|
| 224 | - */ |
|
| 225 | - public function resolvePath($path) { |
|
| 226 | - $a = $this->getAbsolutePath($path); |
|
| 227 | - $p = Filesystem::normalizePath($a); |
|
| 228 | - return Filesystem::resolvePath($p); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * return the path to a local version of the file |
|
| 233 | - * we need this because we can't know if a file is stored local or not from |
|
| 234 | - * outside the filestorage and for some purposes a local file is needed |
|
| 235 | - * |
|
| 236 | - * @param string $path |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - public function getLocalFile($path) { |
|
| 240 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 241 | - $path = $this->getAbsolutePath($path); |
|
| 242 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 243 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 244 | - return $storage->getLocalFile($internalPath); |
|
| 245 | - } else { |
|
| 246 | - return null; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param string $path |
|
| 252 | - * @return string |
|
| 253 | - */ |
|
| 254 | - public function getLocalFolder($path) { |
|
| 255 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 256 | - $path = $this->getAbsolutePath($path); |
|
| 257 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 258 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 259 | - return $storage->getLocalFolder($internalPath); |
|
| 260 | - } else { |
|
| 261 | - return null; |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * the following functions operate with arguments and return values identical |
|
| 267 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 268 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 269 | - */ |
|
| 270 | - public function mkdir($path) { |
|
| 271 | - return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * remove mount point |
|
| 276 | - * |
|
| 277 | - * @param IMountPoint $mount |
|
| 278 | - * @param string $path relative to data/ |
|
| 279 | - * @return boolean |
|
| 280 | - */ |
|
| 281 | - protected function removeMount($mount, $path) { |
|
| 282 | - if ($mount instanceof MoveableMount) { |
|
| 283 | - // cut of /user/files to get the relative path to data/user/files |
|
| 284 | - $pathParts = explode('/', $path, 4); |
|
| 285 | - $relPath = '/' . $pathParts[3]; |
|
| 286 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 287 | - \OC_Hook::emit( |
|
| 288 | - Filesystem::CLASSNAME, "umount", |
|
| 289 | - [Filesystem::signal_param_path => $relPath] |
|
| 290 | - ); |
|
| 291 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 292 | - $result = $mount->removeMount(); |
|
| 293 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 294 | - if ($result) { |
|
| 295 | - \OC_Hook::emit( |
|
| 296 | - Filesystem::CLASSNAME, "post_umount", |
|
| 297 | - [Filesystem::signal_param_path => $relPath] |
|
| 298 | - ); |
|
| 299 | - } |
|
| 300 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 301 | - return $result; |
|
| 302 | - } else { |
|
| 303 | - // do not allow deleting the storage's root / the mount point |
|
| 304 | - // because for some storages it might delete the whole contents |
|
| 305 | - // but isn't supposed to work that way |
|
| 306 | - return false; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - public function disableCacheUpdate() { |
|
| 311 | - $this->updaterEnabled = false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - public function enableCacheUpdate() { |
|
| 315 | - $this->updaterEnabled = true; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 319 | - if ($this->updaterEnabled) { |
|
| 320 | - if (is_null($time)) { |
|
| 321 | - $time = time(); |
|
| 322 | - } |
|
| 323 | - $storage->getUpdater()->update($internalPath, $time); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 328 | - if ($this->updaterEnabled) { |
|
| 329 | - $storage->getUpdater()->remove($internalPath); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 334 | - if ($this->updaterEnabled) { |
|
| 335 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * @param string $path |
|
| 341 | - * @return bool|mixed |
|
| 342 | - */ |
|
| 343 | - public function rmdir($path) { |
|
| 344 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 345 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 346 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 347 | - return $this->removeMount($mount, $absolutePath); |
|
| 348 | - } |
|
| 349 | - if ($this->is_dir($path)) { |
|
| 350 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 351 | - } else { |
|
| 352 | - $result = false; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 356 | - $storage = $mount->getStorage(); |
|
| 357 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 358 | - $storage->getUpdater()->remove($internalPath); |
|
| 359 | - } |
|
| 360 | - return $result; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * @param string $path |
|
| 365 | - * @return resource |
|
| 366 | - */ |
|
| 367 | - public function opendir($path) { |
|
| 368 | - return $this->basicOperation('opendir', $path, ['read']); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @param string $path |
|
| 373 | - * @return bool|mixed |
|
| 374 | - */ |
|
| 375 | - public function is_dir($path) { |
|
| 376 | - if ($path == '/') { |
|
| 377 | - return true; |
|
| 378 | - } |
|
| 379 | - return $this->basicOperation('is_dir', $path); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * @param string $path |
|
| 384 | - * @return bool|mixed |
|
| 385 | - */ |
|
| 386 | - public function is_file($path) { |
|
| 387 | - if ($path == '/') { |
|
| 388 | - return false; |
|
| 389 | - } |
|
| 390 | - return $this->basicOperation('is_file', $path); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @param string $path |
|
| 395 | - * @return mixed |
|
| 396 | - */ |
|
| 397 | - public function stat($path) { |
|
| 398 | - return $this->basicOperation('stat', $path); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @param string $path |
|
| 403 | - * @return mixed |
|
| 404 | - */ |
|
| 405 | - public function filetype($path) { |
|
| 406 | - return $this->basicOperation('filetype', $path); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * @param string $path |
|
| 411 | - * @return mixed |
|
| 412 | - */ |
|
| 413 | - public function filesize($path) { |
|
| 414 | - return $this->basicOperation('filesize', $path); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * @param string $path |
|
| 419 | - * @return bool|mixed |
|
| 420 | - * @throws \OCP\Files\InvalidPathException |
|
| 421 | - */ |
|
| 422 | - public function readfile($path) { |
|
| 423 | - $this->assertPathLength($path); |
|
| 424 | - if (ob_get_level()) { |
|
| 425 | - ob_end_clean(); |
|
| 426 | - } |
|
| 427 | - $handle = $this->fopen($path, 'rb'); |
|
| 428 | - if ($handle) { |
|
| 429 | - $chunkSize = 524288; // 512 kB chunks |
|
| 430 | - while (!feof($handle)) { |
|
| 431 | - echo fread($handle, $chunkSize); |
|
| 432 | - flush(); |
|
| 433 | - } |
|
| 434 | - fclose($handle); |
|
| 435 | - return $this->filesize($path); |
|
| 436 | - } |
|
| 437 | - return false; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * @param string $path |
|
| 442 | - * @param int $from |
|
| 443 | - * @param int $to |
|
| 444 | - * @return bool|mixed |
|
| 445 | - * @throws \OCP\Files\InvalidPathException |
|
| 446 | - * @throws \OCP\Files\UnseekableException |
|
| 447 | - */ |
|
| 448 | - public function readfilePart($path, $from, $to) { |
|
| 449 | - $this->assertPathLength($path); |
|
| 450 | - if (ob_get_level()) { |
|
| 451 | - ob_end_clean(); |
|
| 452 | - } |
|
| 453 | - $handle = $this->fopen($path, 'rb'); |
|
| 454 | - if ($handle) { |
|
| 455 | - $chunkSize = 524288; // 512 kB chunks |
|
| 456 | - $startReading = true; |
|
| 457 | - |
|
| 458 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 459 | - // forward file handle via chunked fread because fseek seem to have failed |
|
| 460 | - |
|
| 461 | - $end = $from + 1; |
|
| 462 | - while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
| 463 | - $len = $from - ftell($handle); |
|
| 464 | - if ($len > $chunkSize) { |
|
| 465 | - $len = $chunkSize; |
|
| 466 | - } |
|
| 467 | - $result = fread($handle, $len); |
|
| 468 | - |
|
| 469 | - if ($result === false) { |
|
| 470 | - $startReading = false; |
|
| 471 | - break; |
|
| 472 | - } |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - if ($startReading) { |
|
| 477 | - $end = $to + 1; |
|
| 478 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 479 | - $len = $end - ftell($handle); |
|
| 480 | - if ($len > $chunkSize) { |
|
| 481 | - $len = $chunkSize; |
|
| 482 | - } |
|
| 483 | - echo fread($handle, $len); |
|
| 484 | - flush(); |
|
| 485 | - } |
|
| 486 | - return ftell($handle) - $from; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 490 | - } |
|
| 491 | - return false; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * @param string $path |
|
| 496 | - * @return mixed |
|
| 497 | - */ |
|
| 498 | - public function isCreatable($path) { |
|
| 499 | - return $this->basicOperation('isCreatable', $path); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * @param string $path |
|
| 504 | - * @return mixed |
|
| 505 | - */ |
|
| 506 | - public function isReadable($path) { |
|
| 507 | - return $this->basicOperation('isReadable', $path); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * @param string $path |
|
| 512 | - * @return mixed |
|
| 513 | - */ |
|
| 514 | - public function isUpdatable($path) { |
|
| 515 | - return $this->basicOperation('isUpdatable', $path); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * @param string $path |
|
| 520 | - * @return bool|mixed |
|
| 521 | - */ |
|
| 522 | - public function isDeletable($path) { |
|
| 523 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 524 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 525 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 526 | - return $mount instanceof MoveableMount; |
|
| 527 | - } |
|
| 528 | - return $this->basicOperation('isDeletable', $path); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * @param string $path |
|
| 533 | - * @return mixed |
|
| 534 | - */ |
|
| 535 | - public function isSharable($path) { |
|
| 536 | - return $this->basicOperation('isSharable', $path); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * @param string $path |
|
| 541 | - * @return bool|mixed |
|
| 542 | - */ |
|
| 543 | - public function file_exists($path) { |
|
| 544 | - if ($path == '/') { |
|
| 545 | - return true; |
|
| 546 | - } |
|
| 547 | - return $this->basicOperation('file_exists', $path); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * @param string $path |
|
| 552 | - * @return mixed |
|
| 553 | - */ |
|
| 554 | - public function filemtime($path) { |
|
| 555 | - return $this->basicOperation('filemtime', $path); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * @param string $path |
|
| 560 | - * @param int|string $mtime |
|
| 561 | - * @return bool |
|
| 562 | - */ |
|
| 563 | - public function touch($path, $mtime = null) { |
|
| 564 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 565 | - $mtime = strtotime($mtime); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - $hooks = ['touch']; |
|
| 569 | - |
|
| 570 | - if (!$this->file_exists($path)) { |
|
| 571 | - $hooks[] = 'create'; |
|
| 572 | - $hooks[] = 'write'; |
|
| 573 | - } |
|
| 574 | - try { |
|
| 575 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 576 | - } catch (\Exception $e) { |
|
| 577 | - $this->logger->logException($e, ['level' => ILogger::INFO, 'message' => 'Error while setting modified time']); |
|
| 578 | - $result = false; |
|
| 579 | - } |
|
| 580 | - if (!$result) { |
|
| 581 | - // If create file fails because of permissions on external storage like SMB folders, |
|
| 582 | - // check file exists and return false if not. |
|
| 583 | - if (!$this->file_exists($path)) { |
|
| 584 | - return false; |
|
| 585 | - } |
|
| 586 | - if (is_null($mtime)) { |
|
| 587 | - $mtime = time(); |
|
| 588 | - } |
|
| 589 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 590 | - $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
| 591 | - } |
|
| 592 | - return true; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * @param string $path |
|
| 597 | - * @return mixed |
|
| 598 | - * @throws LockedException |
|
| 599 | - */ |
|
| 600 | - public function file_get_contents($path) { |
|
| 601 | - return $this->basicOperation('file_get_contents', $path, ['read']); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * @param bool $exists |
|
| 606 | - * @param string $path |
|
| 607 | - * @param bool $run |
|
| 608 | - */ |
|
| 609 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 610 | - if (!$exists) { |
|
| 611 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
| 612 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 613 | - Filesystem::signal_param_run => &$run, |
|
| 614 | - ]); |
|
| 615 | - } else { |
|
| 616 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
| 617 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 618 | - Filesystem::signal_param_run => &$run, |
|
| 619 | - ]); |
|
| 620 | - } |
|
| 621 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
| 622 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 623 | - Filesystem::signal_param_run => &$run, |
|
| 624 | - ]); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * @param bool $exists |
|
| 629 | - * @param string $path |
|
| 630 | - */ |
|
| 631 | - protected function emit_file_hooks_post($exists, $path) { |
|
| 632 | - if (!$exists) { |
|
| 633 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
| 634 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 635 | - ]); |
|
| 636 | - } else { |
|
| 637 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
| 638 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 639 | - ]); |
|
| 640 | - } |
|
| 641 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
| 642 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 643 | - ]); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * @param string $path |
|
| 648 | - * @param string|resource $data |
|
| 649 | - * @return bool|mixed |
|
| 650 | - * @throws LockedException |
|
| 651 | - */ |
|
| 652 | - public function file_put_contents($path, $data) { |
|
| 653 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 654 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 655 | - if (Filesystem::isValidPath($path) |
|
| 656 | - and !Filesystem::isFileBlacklisted($path) |
|
| 657 | - ) { |
|
| 658 | - $path = $this->getRelativePath($absolutePath); |
|
| 659 | - |
|
| 660 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 661 | - |
|
| 662 | - $exists = $this->file_exists($path); |
|
| 663 | - $run = true; |
|
| 664 | - if ($this->shouldEmitHooks($path)) { |
|
| 665 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 666 | - } |
|
| 667 | - if (!$run) { |
|
| 668 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 669 | - return false; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - try { |
|
| 673 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 674 | - } catch (\Exception $e) { |
|
| 675 | - // Release the shared lock before throwing. |
|
| 676 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 677 | - throw $e; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 681 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 682 | - $target = $storage->fopen($internalPath, 'w'); |
|
| 683 | - if ($target) { |
|
| 684 | - [, $result] = \OC_Helper::streamCopy($data, $target); |
|
| 685 | - fclose($target); |
|
| 686 | - fclose($data); |
|
| 687 | - |
|
| 688 | - $this->writeUpdate($storage, $internalPath); |
|
| 689 | - |
|
| 690 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 691 | - |
|
| 692 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 693 | - $this->emit_file_hooks_post($exists, $path); |
|
| 694 | - } |
|
| 695 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 696 | - return $result; |
|
| 697 | - } else { |
|
| 698 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 699 | - return false; |
|
| 700 | - } |
|
| 701 | - } else { |
|
| 702 | - return false; |
|
| 703 | - } |
|
| 704 | - } else { |
|
| 705 | - $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
| 706 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * @param string $path |
|
| 712 | - * @return bool|mixed |
|
| 713 | - */ |
|
| 714 | - public function unlink($path) { |
|
| 715 | - if ($path === '' || $path === '/') { |
|
| 716 | - // do not allow deleting the root |
|
| 717 | - return false; |
|
| 718 | - } |
|
| 719 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 720 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 721 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 722 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 723 | - return $this->removeMount($mount, $absolutePath); |
|
| 724 | - } |
|
| 725 | - if ($this->is_dir($path)) { |
|
| 726 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 727 | - } else { |
|
| 728 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 729 | - } |
|
| 730 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 731 | - $storage = $mount->getStorage(); |
|
| 732 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 733 | - $storage->getUpdater()->remove($internalPath); |
|
| 734 | - return true; |
|
| 735 | - } else { |
|
| 736 | - return $result; |
|
| 737 | - } |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * @param string $directory |
|
| 742 | - * @return bool|mixed |
|
| 743 | - */ |
|
| 744 | - public function deleteAll($directory) { |
|
| 745 | - return $this->rmdir($directory); |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - /** |
|
| 749 | - * Rename/move a file or folder from the source path to target path. |
|
| 750 | - * |
|
| 751 | - * @param string $path1 source path |
|
| 752 | - * @param string $path2 target path |
|
| 753 | - * |
|
| 754 | - * @return bool|mixed |
|
| 755 | - * @throws LockedException |
|
| 756 | - */ |
|
| 757 | - public function rename($path1, $path2) { |
|
| 758 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 759 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 760 | - $result = false; |
|
| 761 | - if ( |
|
| 762 | - Filesystem::isValidPath($path2) |
|
| 763 | - and Filesystem::isValidPath($path1) |
|
| 764 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 765 | - ) { |
|
| 766 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 767 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 768 | - $exists = $this->file_exists($path2); |
|
| 769 | - |
|
| 770 | - if ($path1 == null or $path2 == null) { |
|
| 771 | - return false; |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 775 | - try { |
|
| 776 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 777 | - |
|
| 778 | - $run = true; |
|
| 779 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 780 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 781 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 782 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
| 783 | - \OC_Hook::emit( |
|
| 784 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 785 | - [ |
|
| 786 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 787 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 788 | - Filesystem::signal_param_run => &$run |
|
| 789 | - ] |
|
| 790 | - ); |
|
| 791 | - } |
|
| 792 | - if ($run) { |
|
| 793 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
| 794 | - |
|
| 795 | - $manager = Filesystem::getMountManager(); |
|
| 796 | - $mount1 = $this->getMount($path1); |
|
| 797 | - $mount2 = $this->getMount($path2); |
|
| 798 | - $storage1 = $mount1->getStorage(); |
|
| 799 | - $storage2 = $mount2->getStorage(); |
|
| 800 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 801 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 802 | - |
|
| 803 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 804 | - try { |
|
| 805 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 806 | - |
|
| 807 | - if ($internalPath1 === '') { |
|
| 808 | - if ($mount1 instanceof MoveableMount) { |
|
| 809 | - $sourceParentMount = $this->getMount(dirname($path1)); |
|
| 810 | - if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) { |
|
| 811 | - /** |
|
| 812 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 813 | - */ |
|
| 814 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
| 815 | - $result = $mount1->moveMount($absolutePath2); |
|
| 816 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 817 | - } else { |
|
| 818 | - $result = false; |
|
| 819 | - } |
|
| 820 | - } else { |
|
| 821 | - $result = false; |
|
| 822 | - } |
|
| 823 | - // moving a file/folder within the same mount point |
|
| 824 | - } elseif ($storage1 === $storage2) { |
|
| 825 | - if ($storage1) { |
|
| 826 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 827 | - } else { |
|
| 828 | - $result = false; |
|
| 829 | - } |
|
| 830 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 831 | - } else { |
|
| 832 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 836 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 837 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 838 | - } elseif ($result) { |
|
| 839 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 840 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 841 | - } |
|
| 842 | - } |
|
| 843 | - } catch (\Exception $e) { |
|
| 844 | - throw $e; |
|
| 845 | - } finally { |
|
| 846 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 847 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 851 | - if ($this->shouldEmitHooks()) { |
|
| 852 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 853 | - } |
|
| 854 | - } elseif ($result) { |
|
| 855 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 856 | - \OC_Hook::emit( |
|
| 857 | - Filesystem::CLASSNAME, |
|
| 858 | - Filesystem::signal_post_rename, |
|
| 859 | - [ |
|
| 860 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 861 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 862 | - ] |
|
| 863 | - ); |
|
| 864 | - } |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - } catch (\Exception $e) { |
|
| 868 | - throw $e; |
|
| 869 | - } finally { |
|
| 870 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 871 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 872 | - } |
|
| 873 | - } |
|
| 874 | - return $result; |
|
| 875 | - } |
|
| 876 | - |
|
| 877 | - /** |
|
| 878 | - * Copy a file/folder from the source path to target path |
|
| 879 | - * |
|
| 880 | - * @param string $path1 source path |
|
| 881 | - * @param string $path2 target path |
|
| 882 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 883 | - * |
|
| 884 | - * @return bool|mixed |
|
| 885 | - */ |
|
| 886 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
| 887 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 888 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 889 | - $result = false; |
|
| 890 | - if ( |
|
| 891 | - Filesystem::isValidPath($path2) |
|
| 892 | - and Filesystem::isValidPath($path1) |
|
| 893 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 894 | - ) { |
|
| 895 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 896 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 897 | - |
|
| 898 | - if ($path1 == null or $path2 == null) { |
|
| 899 | - return false; |
|
| 900 | - } |
|
| 901 | - $run = true; |
|
| 902 | - |
|
| 903 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 904 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 905 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 906 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 907 | - |
|
| 908 | - try { |
|
| 909 | - $exists = $this->file_exists($path2); |
|
| 910 | - if ($this->shouldEmitHooks()) { |
|
| 911 | - \OC_Hook::emit( |
|
| 912 | - Filesystem::CLASSNAME, |
|
| 913 | - Filesystem::signal_copy, |
|
| 914 | - [ |
|
| 915 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 916 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 917 | - Filesystem::signal_param_run => &$run |
|
| 918 | - ] |
|
| 919 | - ); |
|
| 920 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 921 | - } |
|
| 922 | - if ($run) { |
|
| 923 | - $mount1 = $this->getMount($path1); |
|
| 924 | - $mount2 = $this->getMount($path2); |
|
| 925 | - $storage1 = $mount1->getStorage(); |
|
| 926 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 927 | - $storage2 = $mount2->getStorage(); |
|
| 928 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 929 | - |
|
| 930 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 931 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 932 | - |
|
| 933 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 934 | - if ($storage1) { |
|
| 935 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 936 | - } else { |
|
| 937 | - $result = false; |
|
| 938 | - } |
|
| 939 | - } else { |
|
| 940 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 944 | - |
|
| 945 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 946 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 947 | - |
|
| 948 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
| 949 | - \OC_Hook::emit( |
|
| 950 | - Filesystem::CLASSNAME, |
|
| 951 | - Filesystem::signal_post_copy, |
|
| 952 | - [ |
|
| 953 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 954 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 955 | - ] |
|
| 956 | - ); |
|
| 957 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 958 | - } |
|
| 959 | - } |
|
| 960 | - } catch (\Exception $e) { |
|
| 961 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 962 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 963 | - throw $e; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 967 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 968 | - } |
|
| 969 | - return $result; |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * @param string $path |
|
| 974 | - * @param string $mode 'r' or 'w' |
|
| 975 | - * @return resource |
|
| 976 | - * @throws LockedException |
|
| 977 | - */ |
|
| 978 | - public function fopen($path, $mode) { |
|
| 979 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 980 | - $hooks = []; |
|
| 981 | - switch ($mode) { |
|
| 982 | - case 'r': |
|
| 983 | - $hooks[] = 'read'; |
|
| 984 | - break; |
|
| 985 | - case 'r+': |
|
| 986 | - case 'w+': |
|
| 987 | - case 'x+': |
|
| 988 | - case 'a+': |
|
| 989 | - $hooks[] = 'read'; |
|
| 990 | - $hooks[] = 'write'; |
|
| 991 | - break; |
|
| 992 | - case 'w': |
|
| 993 | - case 'x': |
|
| 994 | - case 'a': |
|
| 995 | - $hooks[] = 'write'; |
|
| 996 | - break; |
|
| 997 | - default: |
|
| 998 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - if ($mode !== 'r' && $mode !== 'w') { |
|
| 1002 | - \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'); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 1006 | - } |
|
| 1007 | - |
|
| 1008 | - /** |
|
| 1009 | - * @param string $path |
|
| 1010 | - * @return bool|string |
|
| 1011 | - * @throws \OCP\Files\InvalidPathException |
|
| 1012 | - */ |
|
| 1013 | - public function toTmpFile($path) { |
|
| 1014 | - $this->assertPathLength($path); |
|
| 1015 | - if (Filesystem::isValidPath($path)) { |
|
| 1016 | - $source = $this->fopen($path, 'r'); |
|
| 1017 | - if ($source) { |
|
| 1018 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1019 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1020 | - file_put_contents($tmpFile, $source); |
|
| 1021 | - return $tmpFile; |
|
| 1022 | - } else { |
|
| 1023 | - return false; |
|
| 1024 | - } |
|
| 1025 | - } else { |
|
| 1026 | - return false; |
|
| 1027 | - } |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * @param string $tmpFile |
|
| 1032 | - * @param string $path |
|
| 1033 | - * @return bool|mixed |
|
| 1034 | - * @throws \OCP\Files\InvalidPathException |
|
| 1035 | - */ |
|
| 1036 | - public function fromTmpFile($tmpFile, $path) { |
|
| 1037 | - $this->assertPathLength($path); |
|
| 1038 | - if (Filesystem::isValidPath($path)) { |
|
| 1039 | - |
|
| 1040 | - // Get directory that the file is going into |
|
| 1041 | - $filePath = dirname($path); |
|
| 1042 | - |
|
| 1043 | - // Create the directories if any |
|
| 1044 | - if (!$this->file_exists($filePath)) { |
|
| 1045 | - $result = $this->createParentDirectories($filePath); |
|
| 1046 | - if ($result === false) { |
|
| 1047 | - return false; |
|
| 1048 | - } |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - $source = fopen($tmpFile, 'r'); |
|
| 1052 | - if ($source) { |
|
| 1053 | - $result = $this->file_put_contents($path, $source); |
|
| 1054 | - // $this->file_put_contents() might have already closed |
|
| 1055 | - // the resource, so we check it, before trying to close it |
|
| 1056 | - // to avoid messages in the error log. |
|
| 1057 | - if (is_resource($source)) { |
|
| 1058 | - fclose($source); |
|
| 1059 | - } |
|
| 1060 | - unlink($tmpFile); |
|
| 1061 | - return $result; |
|
| 1062 | - } else { |
|
| 1063 | - return false; |
|
| 1064 | - } |
|
| 1065 | - } else { |
|
| 1066 | - return false; |
|
| 1067 | - } |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - |
|
| 1071 | - /** |
|
| 1072 | - * @param string $path |
|
| 1073 | - * @return mixed |
|
| 1074 | - * @throws \OCP\Files\InvalidPathException |
|
| 1075 | - */ |
|
| 1076 | - public function getMimeType($path) { |
|
| 1077 | - $this->assertPathLength($path); |
|
| 1078 | - return $this->basicOperation('getMimeType', $path); |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - /** |
|
| 1082 | - * @param string $type |
|
| 1083 | - * @param string $path |
|
| 1084 | - * @param bool $raw |
|
| 1085 | - * @return bool|string |
|
| 1086 | - */ |
|
| 1087 | - public function hash($type, $path, $raw = false) { |
|
| 1088 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1089 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1090 | - if (Filesystem::isValidPath($path)) { |
|
| 1091 | - $path = $this->getRelativePath($absolutePath); |
|
| 1092 | - if ($path == null) { |
|
| 1093 | - return false; |
|
| 1094 | - } |
|
| 1095 | - if ($this->shouldEmitHooks($path)) { |
|
| 1096 | - \OC_Hook::emit( |
|
| 1097 | - Filesystem::CLASSNAME, |
|
| 1098 | - Filesystem::signal_read, |
|
| 1099 | - [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
| 1100 | - ); |
|
| 1101 | - } |
|
| 1102 | - /** @var Storage|null $storage */ |
|
| 1103 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1104 | - if ($storage) { |
|
| 1105 | - return $storage->hash($type, $internalPath, $raw); |
|
| 1106 | - } |
|
| 1107 | - } |
|
| 1108 | - return false; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * @param string $path |
|
| 1113 | - * @return mixed |
|
| 1114 | - * @throws \OCP\Files\InvalidPathException |
|
| 1115 | - */ |
|
| 1116 | - public function free_space($path = '/') { |
|
| 1117 | - $this->assertPathLength($path); |
|
| 1118 | - $result = $this->basicOperation('free_space', $path); |
|
| 1119 | - if ($result === null) { |
|
| 1120 | - throw new InvalidPathException(); |
|
| 1121 | - } |
|
| 1122 | - return $result; |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - /** |
|
| 1126 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1127 | - * |
|
| 1128 | - * @param string $operation |
|
| 1129 | - * @param string $path |
|
| 1130 | - * @param array $hooks (optional) |
|
| 1131 | - * @param mixed $extraParam (optional) |
|
| 1132 | - * @return mixed |
|
| 1133 | - * @throws LockedException |
|
| 1134 | - * |
|
| 1135 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1136 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1137 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1138 | - */ |
|
| 1139 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1140 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1141 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1142 | - if (Filesystem::isValidPath($path) |
|
| 1143 | - and !Filesystem::isFileBlacklisted($path) |
|
| 1144 | - ) { |
|
| 1145 | - $path = $this->getRelativePath($absolutePath); |
|
| 1146 | - if ($path == null) { |
|
| 1147 | - return false; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1151 | - // always a shared lock during pre-hooks so the hook can read the file |
|
| 1152 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - $run = $this->runHooks($hooks, $path); |
|
| 1156 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1157 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1158 | - if ($run and $storage) { |
|
| 1159 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1160 | - try { |
|
| 1161 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1162 | - } catch (LockedException $e) { |
|
| 1163 | - // release the shared lock we acquired before quiting |
|
| 1164 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1165 | - throw $e; |
|
| 1166 | - } |
|
| 1167 | - } |
|
| 1168 | - try { |
|
| 1169 | - if (!is_null($extraParam)) { |
|
| 1170 | - $result = $storage->$operation($internalPath, $extraParam); |
|
| 1171 | - } else { |
|
| 1172 | - $result = $storage->$operation($internalPath); |
|
| 1173 | - } |
|
| 1174 | - } catch (\Exception $e) { |
|
| 1175 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1176 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1177 | - } elseif (in_array('read', $hooks)) { |
|
| 1178 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1179 | - } |
|
| 1180 | - throw $e; |
|
| 1181 | - } |
|
| 1182 | - |
|
| 1183 | - if ($result && in_array('delete', $hooks)) { |
|
| 1184 | - $this->removeUpdate($storage, $internalPath); |
|
| 1185 | - } |
|
| 1186 | - if ($result && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
| 1187 | - $this->writeUpdate($storage, $internalPath); |
|
| 1188 | - } |
|
| 1189 | - if ($result && in_array('touch', $hooks)) { |
|
| 1190 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1194 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - $unlockLater = false; |
|
| 1198 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1199 | - $unlockLater = true; |
|
| 1200 | - // make sure our unlocking callback will still be called if connection is aborted |
|
| 1201 | - ignore_user_abort(true); |
|
| 1202 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1203 | - if (in_array('write', $hooks)) { |
|
| 1204 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1205 | - } elseif (in_array('read', $hooks)) { |
|
| 1206 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1207 | - } |
|
| 1208 | - }); |
|
| 1209 | - } |
|
| 1210 | - |
|
| 1211 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1212 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1213 | - $this->runHooks($hooks, $path, true); |
|
| 1214 | - } |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - if (!$unlockLater |
|
| 1218 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1219 | - ) { |
|
| 1220 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1221 | - } |
|
| 1222 | - return $result; |
|
| 1223 | - } else { |
|
| 1224 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1225 | - } |
|
| 1226 | - } |
|
| 1227 | - return null; |
|
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - /** |
|
| 1231 | - * get the path relative to the default root for hook usage |
|
| 1232 | - * |
|
| 1233 | - * @param string $path |
|
| 1234 | - * @return string |
|
| 1235 | - */ |
|
| 1236 | - private function getHookPath($path) { |
|
| 1237 | - if (!Filesystem::getView()) { |
|
| 1238 | - return $path; |
|
| 1239 | - } |
|
| 1240 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - private function shouldEmitHooks($path = '') { |
|
| 1244 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1245 | - return false; |
|
| 1246 | - } |
|
| 1247 | - if (!Filesystem::$loaded) { |
|
| 1248 | - return false; |
|
| 1249 | - } |
|
| 1250 | - $defaultRoot = Filesystem::getRoot(); |
|
| 1251 | - if ($defaultRoot === null) { |
|
| 1252 | - return false; |
|
| 1253 | - } |
|
| 1254 | - if ($this->fakeRoot === $defaultRoot) { |
|
| 1255 | - return true; |
|
| 1256 | - } |
|
| 1257 | - $fullPath = $this->getAbsolutePath($path); |
|
| 1258 | - |
|
| 1259 | - if ($fullPath === $defaultRoot) { |
|
| 1260 | - return true; |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - /** |
|
| 1267 | - * @param string[] $hooks |
|
| 1268 | - * @param string $path |
|
| 1269 | - * @param bool $post |
|
| 1270 | - * @return bool |
|
| 1271 | - */ |
|
| 1272 | - private function runHooks($hooks, $path, $post = false) { |
|
| 1273 | - $relativePath = $path; |
|
| 1274 | - $path = $this->getHookPath($path); |
|
| 1275 | - $prefix = $post ? 'post_' : ''; |
|
| 1276 | - $run = true; |
|
| 1277 | - if ($this->shouldEmitHooks($relativePath)) { |
|
| 1278 | - foreach ($hooks as $hook) { |
|
| 1279 | - if ($hook != 'read') { |
|
| 1280 | - \OC_Hook::emit( |
|
| 1281 | - Filesystem::CLASSNAME, |
|
| 1282 | - $prefix . $hook, |
|
| 1283 | - [ |
|
| 1284 | - Filesystem::signal_param_run => &$run, |
|
| 1285 | - Filesystem::signal_param_path => $path |
|
| 1286 | - ] |
|
| 1287 | - ); |
|
| 1288 | - } elseif (!$post) { |
|
| 1289 | - \OC_Hook::emit( |
|
| 1290 | - Filesystem::CLASSNAME, |
|
| 1291 | - $prefix . $hook, |
|
| 1292 | - [ |
|
| 1293 | - Filesystem::signal_param_path => $path |
|
| 1294 | - ] |
|
| 1295 | - ); |
|
| 1296 | - } |
|
| 1297 | - } |
|
| 1298 | - } |
|
| 1299 | - return $run; |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - /** |
|
| 1303 | - * check if a file or folder has been updated since $time |
|
| 1304 | - * |
|
| 1305 | - * @param string $path |
|
| 1306 | - * @param int $time |
|
| 1307 | - * @return bool |
|
| 1308 | - */ |
|
| 1309 | - public function hasUpdated($path, $time) { |
|
| 1310 | - return $this->basicOperation('hasUpdated', $path, [], $time); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * @param string $ownerId |
|
| 1315 | - * @return \OC\User\User |
|
| 1316 | - */ |
|
| 1317 | - private function getUserObjectForOwner($ownerId) { |
|
| 1318 | - $owner = $this->userManager->get($ownerId); |
|
| 1319 | - if ($owner instanceof IUser) { |
|
| 1320 | - return $owner; |
|
| 1321 | - } else { |
|
| 1322 | - return new User($ownerId, null, \OC::$server->getEventDispatcher()); |
|
| 1323 | - } |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - /** |
|
| 1327 | - * Get file info from cache |
|
| 1328 | - * |
|
| 1329 | - * If the file is not in cached it will be scanned |
|
| 1330 | - * If the file has changed on storage the cache will be updated |
|
| 1331 | - * |
|
| 1332 | - * @param \OC\Files\Storage\Storage $storage |
|
| 1333 | - * @param string $internalPath |
|
| 1334 | - * @param string $relativePath |
|
| 1335 | - * @return ICacheEntry|bool |
|
| 1336 | - */ |
|
| 1337 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1338 | - $cache = $storage->getCache($internalPath); |
|
| 1339 | - $data = $cache->get($internalPath); |
|
| 1340 | - $watcher = $storage->getWatcher($internalPath); |
|
| 1341 | - |
|
| 1342 | - try { |
|
| 1343 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1344 | - if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
| 1345 | - if (!$storage->file_exists($internalPath)) { |
|
| 1346 | - return false; |
|
| 1347 | - } |
|
| 1348 | - // don't need to get a lock here since the scanner does it's own locking |
|
| 1349 | - $scanner = $storage->getScanner($internalPath); |
|
| 1350 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1351 | - $data = $cache->get($internalPath); |
|
| 1352 | - } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1353 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1354 | - $watcher->update($internalPath, $data); |
|
| 1355 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1356 | - $data = $cache->get($internalPath); |
|
| 1357 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1358 | - } |
|
| 1359 | - } catch (LockedException $e) { |
|
| 1360 | - // if the file is locked we just use the old cache info |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - return $data; |
|
| 1364 | - } |
|
| 1365 | - |
|
| 1366 | - /** |
|
| 1367 | - * get the filesystem info |
|
| 1368 | - * |
|
| 1369 | - * @param string $path |
|
| 1370 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1371 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1372 | - * defaults to true |
|
| 1373 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1374 | - */ |
|
| 1375 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1376 | - $this->assertPathLength($path); |
|
| 1377 | - if (!Filesystem::isValidPath($path)) { |
|
| 1378 | - return false; |
|
| 1379 | - } |
|
| 1380 | - if (Cache\Scanner::isPartialFile($path)) { |
|
| 1381 | - return $this->getPartFileInfo($path); |
|
| 1382 | - } |
|
| 1383 | - $relativePath = $path; |
|
| 1384 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1385 | - |
|
| 1386 | - $mount = Filesystem::getMountManager()->find($path); |
|
| 1387 | - $storage = $mount->getStorage(); |
|
| 1388 | - $internalPath = $mount->getInternalPath($path); |
|
| 1389 | - if ($storage) { |
|
| 1390 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1391 | - |
|
| 1392 | - if (!$data instanceof ICacheEntry) { |
|
| 1393 | - return false; |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1397 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1398 | - } |
|
| 1399 | - $ownerId = $storage->getOwner($internalPath); |
|
| 1400 | - $owner = null; |
|
| 1401 | - if ($ownerId !== null && $ownerId !== false) { |
|
| 1402 | - // ownerId might be null if files are accessed with an access token without file system access |
|
| 1403 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1404 | - } |
|
| 1405 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1406 | - |
|
| 1407 | - if (isset($data['fileid'])) { |
|
| 1408 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1409 | - //add the sizes of other mount points to the folder |
|
| 1410 | - $extOnly = ($includeMountPoints === 'ext'); |
|
| 1411 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1412 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1413 | - $subStorage = $mount->getStorage(); |
|
| 1414 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1415 | - })); |
|
| 1416 | - } |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - return $info; |
|
| 1420 | - } else { |
|
| 1421 | - \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - return false; |
|
| 1425 | - } |
|
| 1426 | - |
|
| 1427 | - /** |
|
| 1428 | - * get the content of a directory |
|
| 1429 | - * |
|
| 1430 | - * @param string $directory path under datadirectory |
|
| 1431 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1432 | - * @return FileInfo[] |
|
| 1433 | - */ |
|
| 1434 | - public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) { |
|
| 1435 | - $this->assertPathLength($directory); |
|
| 1436 | - if (!Filesystem::isValidPath($directory)) { |
|
| 1437 | - return []; |
|
| 1438 | - } |
|
| 1439 | - |
|
| 1440 | - $path = $this->getAbsolutePath($directory); |
|
| 1441 | - $path = Filesystem::normalizePath($path); |
|
| 1442 | - $mount = $this->getMount($directory); |
|
| 1443 | - $storage = $mount->getStorage(); |
|
| 1444 | - $internalPath = $mount->getInternalPath($path); |
|
| 1445 | - if (!$storage) { |
|
| 1446 | - return []; |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - $cache = $storage->getCache($internalPath); |
|
| 1450 | - $user = \OC_User::getUser(); |
|
| 1451 | - |
|
| 1452 | - if (!$directoryInfo) { |
|
| 1453 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1454 | - if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
| 1455 | - return []; |
|
| 1456 | - } |
|
| 1457 | - } else { |
|
| 1458 | - $data = $directoryInfo; |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1462 | - return []; |
|
| 1463 | - } |
|
| 1464 | - |
|
| 1465 | - $folderId = $data->getId(); |
|
| 1466 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1467 | - |
|
| 1468 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1469 | - |
|
| 1470 | - $fileNames = array_map(function (ICacheEntry $content) { |
|
| 1471 | - return $content->getName(); |
|
| 1472 | - }, $contents); |
|
| 1473 | - /** |
|
| 1474 | - * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1475 | - */ |
|
| 1476 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1477 | - if ($sharingDisabled) { |
|
| 1478 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1479 | - } |
|
| 1480 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1481 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1482 | - }, $contents); |
|
| 1483 | - $files = array_combine($fileNames, $fileInfos); |
|
| 1484 | - |
|
| 1485 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1486 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1487 | - $dirLength = strlen($path); |
|
| 1488 | - foreach ($mounts as $mount) { |
|
| 1489 | - $mountPoint = $mount->getMountPoint(); |
|
| 1490 | - $subStorage = $mount->getStorage(); |
|
| 1491 | - if ($subStorage) { |
|
| 1492 | - $subCache = $subStorage->getCache(''); |
|
| 1493 | - |
|
| 1494 | - $rootEntry = $subCache->get(''); |
|
| 1495 | - if (!$rootEntry) { |
|
| 1496 | - $subScanner = $subStorage->getScanner(); |
|
| 1497 | - try { |
|
| 1498 | - $subScanner->scanFile(''); |
|
| 1499 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1500 | - continue; |
|
| 1501 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1502 | - continue; |
|
| 1503 | - } catch (\Exception $e) { |
|
| 1504 | - // sometimes when the storage is not available it can be any exception |
|
| 1505 | - \OC::$server->getLogger()->logException($e, [ |
|
| 1506 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1507 | - 'level' => ILogger::ERROR, |
|
| 1508 | - 'app' => 'lib', |
|
| 1509 | - ]); |
|
| 1510 | - continue; |
|
| 1511 | - } |
|
| 1512 | - $rootEntry = $subCache->get(''); |
|
| 1513 | - } |
|
| 1514 | - |
|
| 1515 | - if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1516 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1517 | - if ($pos = strpos($relativePath, '/')) { |
|
| 1518 | - //mountpoint inside subfolder add size to the correct folder |
|
| 1519 | - $entryName = substr($relativePath, 0, $pos); |
|
| 1520 | - foreach ($files as &$entry) { |
|
| 1521 | - if ($entry->getName() === $entryName) { |
|
| 1522 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1523 | - } |
|
| 1524 | - } |
|
| 1525 | - } else { //mountpoint in this folder, add an entry for it |
|
| 1526 | - $rootEntry['name'] = $relativePath; |
|
| 1527 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1528 | - $permissions = $rootEntry['permissions']; |
|
| 1529 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1530 | - // for shared files/folders we use the permissions given by the owner |
|
| 1531 | - if ($mount instanceof MoveableMount) { |
|
| 1532 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1533 | - } else { |
|
| 1534 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1535 | - } |
|
| 1536 | - |
|
| 1537 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1538 | - |
|
| 1539 | - // if sharing was disabled for the user we remove the share permissions |
|
| 1540 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1541 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1545 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1546 | - } |
|
| 1547 | - } |
|
| 1548 | - } |
|
| 1549 | - } |
|
| 1550 | - |
|
| 1551 | - if ($mimetype_filter) { |
|
| 1552 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1553 | - if (strpos($mimetype_filter, '/')) { |
|
| 1554 | - return $file->getMimetype() === $mimetype_filter; |
|
| 1555 | - } else { |
|
| 1556 | - return $file->getMimePart() === $mimetype_filter; |
|
| 1557 | - } |
|
| 1558 | - }); |
|
| 1559 | - } |
|
| 1560 | - |
|
| 1561 | - return array_values($files); |
|
| 1562 | - } |
|
| 1563 | - |
|
| 1564 | - /** |
|
| 1565 | - * change file metadata |
|
| 1566 | - * |
|
| 1567 | - * @param string $path |
|
| 1568 | - * @param array|\OCP\Files\FileInfo $data |
|
| 1569 | - * @return int |
|
| 1570 | - * |
|
| 1571 | - * returns the fileid of the updated file |
|
| 1572 | - */ |
|
| 1573 | - public function putFileInfo($path, $data) { |
|
| 1574 | - $this->assertPathLength($path); |
|
| 1575 | - if ($data instanceof FileInfo) { |
|
| 1576 | - $data = $data->getData(); |
|
| 1577 | - } |
|
| 1578 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1579 | - /** |
|
| 1580 | - * @var \OC\Files\Storage\Storage $storage |
|
| 1581 | - * @var string $internalPath |
|
| 1582 | - */ |
|
| 1583 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 1584 | - if ($storage) { |
|
| 1585 | - $cache = $storage->getCache($path); |
|
| 1586 | - |
|
| 1587 | - if (!$cache->inCache($internalPath)) { |
|
| 1588 | - $scanner = $storage->getScanner($internalPath); |
|
| 1589 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1590 | - } |
|
| 1591 | - |
|
| 1592 | - return $cache->put($internalPath, $data); |
|
| 1593 | - } else { |
|
| 1594 | - return -1; |
|
| 1595 | - } |
|
| 1596 | - } |
|
| 1597 | - |
|
| 1598 | - /** |
|
| 1599 | - * search for files with the name matching $query |
|
| 1600 | - * |
|
| 1601 | - * @param string $query |
|
| 1602 | - * @return FileInfo[] |
|
| 1603 | - */ |
|
| 1604 | - public function search($query) { |
|
| 1605 | - return $this->searchCommon('search', ['%' . $query . '%']); |
|
| 1606 | - } |
|
| 1607 | - |
|
| 1608 | - /** |
|
| 1609 | - * search for files with the name matching $query |
|
| 1610 | - * |
|
| 1611 | - * @param string $query |
|
| 1612 | - * @return FileInfo[] |
|
| 1613 | - */ |
|
| 1614 | - public function searchRaw($query) { |
|
| 1615 | - return $this->searchCommon('search', [$query]); |
|
| 1616 | - } |
|
| 1617 | - |
|
| 1618 | - /** |
|
| 1619 | - * search for files by mimetype |
|
| 1620 | - * |
|
| 1621 | - * @param string $mimetype |
|
| 1622 | - * @return FileInfo[] |
|
| 1623 | - */ |
|
| 1624 | - public function searchByMime($mimetype) { |
|
| 1625 | - return $this->searchCommon('searchByMime', [$mimetype]); |
|
| 1626 | - } |
|
| 1627 | - |
|
| 1628 | - /** |
|
| 1629 | - * search for files by tag |
|
| 1630 | - * |
|
| 1631 | - * @param string|int $tag name or tag id |
|
| 1632 | - * @param string $userId owner of the tags |
|
| 1633 | - * @return FileInfo[] |
|
| 1634 | - */ |
|
| 1635 | - public function searchByTag($tag, $userId) { |
|
| 1636 | - return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
| 1637 | - } |
|
| 1638 | - |
|
| 1639 | - /** |
|
| 1640 | - * @param string $method cache method |
|
| 1641 | - * @param array $args |
|
| 1642 | - * @return FileInfo[] |
|
| 1643 | - */ |
|
| 1644 | - private function searchCommon($method, $args) { |
|
| 1645 | - $files = []; |
|
| 1646 | - $rootLength = strlen($this->fakeRoot); |
|
| 1647 | - |
|
| 1648 | - $mount = $this->getMount(''); |
|
| 1649 | - $mountPoint = $mount->getMountPoint(); |
|
| 1650 | - $storage = $mount->getStorage(); |
|
| 1651 | - $userManager = \OC::$server->getUserManager(); |
|
| 1652 | - if ($storage) { |
|
| 1653 | - $cache = $storage->getCache(''); |
|
| 1654 | - |
|
| 1655 | - $results = call_user_func_array([$cache, $method], $args); |
|
| 1656 | - foreach ($results as $result) { |
|
| 1657 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1658 | - $internalPath = $result['path']; |
|
| 1659 | - $path = $mountPoint . $result['path']; |
|
| 1660 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1661 | - $owner = $userManager->get($storage->getOwner($internalPath)); |
|
| 1662 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1663 | - } |
|
| 1664 | - } |
|
| 1665 | - |
|
| 1666 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1667 | - foreach ($mounts as $mount) { |
|
| 1668 | - $mountPoint = $mount->getMountPoint(); |
|
| 1669 | - $storage = $mount->getStorage(); |
|
| 1670 | - if ($storage) { |
|
| 1671 | - $cache = $storage->getCache(''); |
|
| 1672 | - |
|
| 1673 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1674 | - $results = call_user_func_array([$cache, $method], $args); |
|
| 1675 | - if ($results) { |
|
| 1676 | - foreach ($results as $result) { |
|
| 1677 | - $internalPath = $result['path']; |
|
| 1678 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1679 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1680 | - $owner = $userManager->get($storage->getOwner($internalPath)); |
|
| 1681 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1682 | - } |
|
| 1683 | - } |
|
| 1684 | - } |
|
| 1685 | - } |
|
| 1686 | - } |
|
| 1687 | - return $files; |
|
| 1688 | - } |
|
| 1689 | - |
|
| 1690 | - /** |
|
| 1691 | - * Get the owner for a file or folder |
|
| 1692 | - * |
|
| 1693 | - * @param string $path |
|
| 1694 | - * @return string the user id of the owner |
|
| 1695 | - * @throws NotFoundException |
|
| 1696 | - */ |
|
| 1697 | - public function getOwner($path) { |
|
| 1698 | - $info = $this->getFileInfo($path); |
|
| 1699 | - if (!$info) { |
|
| 1700 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1701 | - } |
|
| 1702 | - |
|
| 1703 | - if ($info->getOwner() === null) { |
|
| 1704 | - throw new NotFoundException($path . ' has no owner'); |
|
| 1705 | - } |
|
| 1706 | - |
|
| 1707 | - return $info->getOwner()->getUID(); |
|
| 1708 | - } |
|
| 1709 | - |
|
| 1710 | - /** |
|
| 1711 | - * get the ETag for a file or folder |
|
| 1712 | - * |
|
| 1713 | - * @param string $path |
|
| 1714 | - * @return string |
|
| 1715 | - */ |
|
| 1716 | - public function getETag($path) { |
|
| 1717 | - /** |
|
| 1718 | - * @var Storage\Storage $storage |
|
| 1719 | - * @var string $internalPath |
|
| 1720 | - */ |
|
| 1721 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1722 | - if ($storage) { |
|
| 1723 | - return $storage->getETag($internalPath); |
|
| 1724 | - } else { |
|
| 1725 | - return null; |
|
| 1726 | - } |
|
| 1727 | - } |
|
| 1728 | - |
|
| 1729 | - /** |
|
| 1730 | - * Get the path of a file by id, relative to the view |
|
| 1731 | - * |
|
| 1732 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1733 | - * |
|
| 1734 | - * @param int $id |
|
| 1735 | - * @param int|null $storageId |
|
| 1736 | - * @return string |
|
| 1737 | - * @throws NotFoundException |
|
| 1738 | - */ |
|
| 1739 | - public function getPath($id, int $storageId = null) { |
|
| 1740 | - $id = (int)$id; |
|
| 1741 | - $manager = Filesystem::getMountManager(); |
|
| 1742 | - $mounts = $manager->findIn($this->fakeRoot); |
|
| 1743 | - $mounts[] = $manager->find($this->fakeRoot); |
|
| 1744 | - $mounts = array_filter($mounts); |
|
| 1745 | - // reverse the array, so we start with the storage this view is in |
|
| 1746 | - // which is the most likely to contain the file we're looking for |
|
| 1747 | - $mounts = array_reverse($mounts); |
|
| 1748 | - |
|
| 1749 | - // put non-shared mounts in front of the shared mount |
|
| 1750 | - // this prevents unneeded recursion into shares |
|
| 1751 | - usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
| 1752 | - return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1; |
|
| 1753 | - }); |
|
| 1754 | - |
|
| 1755 | - if (!is_null($storageId)) { |
|
| 1756 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) { |
|
| 1757 | - return $mount->getNumericStorageId() === $storageId; |
|
| 1758 | - }); |
|
| 1759 | - } |
|
| 1760 | - |
|
| 1761 | - foreach ($mounts as $mount) { |
|
| 1762 | - /** |
|
| 1763 | - * @var \OC\Files\Mount\MountPoint $mount |
|
| 1764 | - */ |
|
| 1765 | - if ($mount->getStorage()) { |
|
| 1766 | - $cache = $mount->getStorage()->getCache(); |
|
| 1767 | - $internalPath = $cache->getPathById($id); |
|
| 1768 | - if (is_string($internalPath)) { |
|
| 1769 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1770 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1771 | - return $path; |
|
| 1772 | - } |
|
| 1773 | - } |
|
| 1774 | - } |
|
| 1775 | - } |
|
| 1776 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1777 | - } |
|
| 1778 | - |
|
| 1779 | - /** |
|
| 1780 | - * @param string $path |
|
| 1781 | - * @throws InvalidPathException |
|
| 1782 | - */ |
|
| 1783 | - private function assertPathLength($path) { |
|
| 1784 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1785 | - // Check for the string length - performed using isset() instead of strlen() |
|
| 1786 | - // because isset() is about 5x-40x faster. |
|
| 1787 | - if (isset($path[$maxLen])) { |
|
| 1788 | - $pathLen = strlen($path); |
|
| 1789 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1790 | - } |
|
| 1791 | - } |
|
| 1792 | - |
|
| 1793 | - /** |
|
| 1794 | - * check if it is allowed to move a mount point to a given target. |
|
| 1795 | - * It is not allowed to move a mount point into a different mount point or |
|
| 1796 | - * into an already shared folder |
|
| 1797 | - * |
|
| 1798 | - * @param IStorage $targetStorage |
|
| 1799 | - * @param string $targetInternalPath |
|
| 1800 | - * @return boolean |
|
| 1801 | - */ |
|
| 1802 | - private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) { |
|
| 1803 | - |
|
| 1804 | - // note: cannot use the view because the target is already locked |
|
| 1805 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1806 | - if ($fileId === -1) { |
|
| 1807 | - // target might not exist, need to check parent instead |
|
| 1808 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1809 | - } |
|
| 1810 | - |
|
| 1811 | - // check if any of the parents were shared by the current owner (include collections) |
|
| 1812 | - $shares = \OCP\Share::getItemShared( |
|
| 1813 | - 'folder', |
|
| 1814 | - $fileId, |
|
| 1815 | - \OCP\Share::FORMAT_NONE, |
|
| 1816 | - null, |
|
| 1817 | - true |
|
| 1818 | - ); |
|
| 1819 | - |
|
| 1820 | - if (count($shares) > 0) { |
|
| 1821 | - \OCP\Util::writeLog('files', |
|
| 1822 | - 'It is not allowed to move one mount point into a shared folder', |
|
| 1823 | - ILogger::DEBUG); |
|
| 1824 | - return false; |
|
| 1825 | - } |
|
| 1826 | - |
|
| 1827 | - return true; |
|
| 1828 | - } |
|
| 1829 | - |
|
| 1830 | - /** |
|
| 1831 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1832 | - * |
|
| 1833 | - * @param string $path |
|
| 1834 | - * @return \OCP\Files\FileInfo |
|
| 1835 | - */ |
|
| 1836 | - private function getPartFileInfo($path) { |
|
| 1837 | - $mount = $this->getMount($path); |
|
| 1838 | - $storage = $mount->getStorage(); |
|
| 1839 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1840 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1841 | - return new FileInfo( |
|
| 1842 | - $this->getAbsolutePath($path), |
|
| 1843 | - $storage, |
|
| 1844 | - $internalPath, |
|
| 1845 | - [ |
|
| 1846 | - 'fileid' => null, |
|
| 1847 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1848 | - 'name' => basename($path), |
|
| 1849 | - 'etag' => null, |
|
| 1850 | - 'size' => $storage->filesize($internalPath), |
|
| 1851 | - 'mtime' => $storage->filemtime($internalPath), |
|
| 1852 | - 'encrypted' => false, |
|
| 1853 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1854 | - ], |
|
| 1855 | - $mount, |
|
| 1856 | - $owner |
|
| 1857 | - ); |
|
| 1858 | - } |
|
| 1859 | - |
|
| 1860 | - /** |
|
| 1861 | - * @param string $path |
|
| 1862 | - * @param string $fileName |
|
| 1863 | - * @throws InvalidPathException |
|
| 1864 | - */ |
|
| 1865 | - public function verifyPath($path, $fileName) { |
|
| 1866 | - try { |
|
| 1867 | - /** @type \OCP\Files\Storage $storage */ |
|
| 1868 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1869 | - $storage->verifyPath($internalPath, $fileName); |
|
| 1870 | - } catch (ReservedWordException $ex) { |
|
| 1871 | - $l = \OC::$server->getL10N('lib'); |
|
| 1872 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1873 | - } catch (InvalidCharacterInPathException $ex) { |
|
| 1874 | - $l = \OC::$server->getL10N('lib'); |
|
| 1875 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1876 | - } catch (FileNameTooLongException $ex) { |
|
| 1877 | - $l = \OC::$server->getL10N('lib'); |
|
| 1878 | - throw new InvalidPathException($l->t('File name is too long')); |
|
| 1879 | - } catch (InvalidDirectoryException $ex) { |
|
| 1880 | - $l = \OC::$server->getL10N('lib'); |
|
| 1881 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1882 | - } catch (EmptyFileNameException $ex) { |
|
| 1883 | - $l = \OC::$server->getL10N('lib'); |
|
| 1884 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1885 | - } |
|
| 1886 | - } |
|
| 1887 | - |
|
| 1888 | - /** |
|
| 1889 | - * get all parent folders of $path |
|
| 1890 | - * |
|
| 1891 | - * @param string $path |
|
| 1892 | - * @return string[] |
|
| 1893 | - */ |
|
| 1894 | - private function getParents($path) { |
|
| 1895 | - $path = trim($path, '/'); |
|
| 1896 | - if (!$path) { |
|
| 1897 | - return []; |
|
| 1898 | - } |
|
| 1899 | - |
|
| 1900 | - $parts = explode('/', $path); |
|
| 1901 | - |
|
| 1902 | - // remove the single file |
|
| 1903 | - array_pop($parts); |
|
| 1904 | - $result = ['/']; |
|
| 1905 | - $resultPath = ''; |
|
| 1906 | - foreach ($parts as $part) { |
|
| 1907 | - if ($part) { |
|
| 1908 | - $resultPath .= '/' . $part; |
|
| 1909 | - $result[] = $resultPath; |
|
| 1910 | - } |
|
| 1911 | - } |
|
| 1912 | - return $result; |
|
| 1913 | - } |
|
| 1914 | - |
|
| 1915 | - /** |
|
| 1916 | - * Returns the mount point for which to lock |
|
| 1917 | - * |
|
| 1918 | - * @param string $absolutePath absolute path |
|
| 1919 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1920 | - * is mounted directly on the given path, false otherwise |
|
| 1921 | - * @return IMountPoint mount point for which to apply locks |
|
| 1922 | - */ |
|
| 1923 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1924 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1925 | - |
|
| 1926 | - if ($useParentMount) { |
|
| 1927 | - // find out if something is mounted directly on the path |
|
| 1928 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1929 | - if ($internalPath === '') { |
|
| 1930 | - // resolve the parent mount instead |
|
| 1931 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1932 | - } |
|
| 1933 | - } |
|
| 1934 | - |
|
| 1935 | - return $mount; |
|
| 1936 | - } |
|
| 1937 | - |
|
| 1938 | - /** |
|
| 1939 | - * Lock the given path |
|
| 1940 | - * |
|
| 1941 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1942 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1943 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1944 | - * |
|
| 1945 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1946 | - * @throws LockedException if the path is already locked |
|
| 1947 | - */ |
|
| 1948 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1949 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1950 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1951 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1952 | - return false; |
|
| 1953 | - } |
|
| 1954 | - |
|
| 1955 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1956 | - if ($mount) { |
|
| 1957 | - try { |
|
| 1958 | - $storage = $mount->getStorage(); |
|
| 1959 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1960 | - $storage->acquireLock( |
|
| 1961 | - $mount->getInternalPath($absolutePath), |
|
| 1962 | - $type, |
|
| 1963 | - $this->lockingProvider |
|
| 1964 | - ); |
|
| 1965 | - } |
|
| 1966 | - } catch (LockedException $e) { |
|
| 1967 | - // rethrow with the a human-readable path |
|
| 1968 | - throw new LockedException( |
|
| 1969 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1970 | - $e, |
|
| 1971 | - $e->getExistingLock() |
|
| 1972 | - ); |
|
| 1973 | - } |
|
| 1974 | - } |
|
| 1975 | - |
|
| 1976 | - return true; |
|
| 1977 | - } |
|
| 1978 | - |
|
| 1979 | - /** |
|
| 1980 | - * Change the lock type |
|
| 1981 | - * |
|
| 1982 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1983 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1984 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1985 | - * |
|
| 1986 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1987 | - * @throws LockedException if the path is already locked |
|
| 1988 | - */ |
|
| 1989 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1990 | - $path = Filesystem::normalizePath($path); |
|
| 1991 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1992 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1993 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1994 | - return false; |
|
| 1995 | - } |
|
| 1996 | - |
|
| 1997 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1998 | - if ($mount) { |
|
| 1999 | - try { |
|
| 2000 | - $storage = $mount->getStorage(); |
|
| 2001 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2002 | - $storage->changeLock( |
|
| 2003 | - $mount->getInternalPath($absolutePath), |
|
| 2004 | - $type, |
|
| 2005 | - $this->lockingProvider |
|
| 2006 | - ); |
|
| 2007 | - } |
|
| 2008 | - } catch (LockedException $e) { |
|
| 2009 | - try { |
|
| 2010 | - // rethrow with the a human-readable path |
|
| 2011 | - throw new LockedException( |
|
| 2012 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 2013 | - $e, |
|
| 2014 | - $e->getExistingLock() |
|
| 2015 | - ); |
|
| 2016 | - } catch (\InvalidArgumentException $ex) { |
|
| 2017 | - throw new LockedException( |
|
| 2018 | - $absolutePath, |
|
| 2019 | - $ex, |
|
| 2020 | - $e->getExistingLock() |
|
| 2021 | - ); |
|
| 2022 | - } |
|
| 2023 | - } |
|
| 2024 | - } |
|
| 2025 | - |
|
| 2026 | - return true; |
|
| 2027 | - } |
|
| 2028 | - |
|
| 2029 | - /** |
|
| 2030 | - * Unlock the given path |
|
| 2031 | - * |
|
| 2032 | - * @param string $path the path of the file to unlock, relative to the view |
|
| 2033 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2034 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2035 | - * |
|
| 2036 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2037 | - * @throws LockedException |
|
| 2038 | - */ |
|
| 2039 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2040 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2041 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2042 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2043 | - return false; |
|
| 2044 | - } |
|
| 2045 | - |
|
| 2046 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2047 | - if ($mount) { |
|
| 2048 | - $storage = $mount->getStorage(); |
|
| 2049 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2050 | - $storage->releaseLock( |
|
| 2051 | - $mount->getInternalPath($absolutePath), |
|
| 2052 | - $type, |
|
| 2053 | - $this->lockingProvider |
|
| 2054 | - ); |
|
| 2055 | - } |
|
| 2056 | - } |
|
| 2057 | - |
|
| 2058 | - return true; |
|
| 2059 | - } |
|
| 2060 | - |
|
| 2061 | - /** |
|
| 2062 | - * Lock a path and all its parents up to the root of the view |
|
| 2063 | - * |
|
| 2064 | - * @param string $path the path of the file to lock relative to the view |
|
| 2065 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2066 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2067 | - * |
|
| 2068 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2069 | - * @throws LockedException |
|
| 2070 | - */ |
|
| 2071 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2072 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2073 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2074 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2075 | - return false; |
|
| 2076 | - } |
|
| 2077 | - |
|
| 2078 | - $this->lockPath($path, $type, $lockMountPoint); |
|
| 2079 | - |
|
| 2080 | - $parents = $this->getParents($path); |
|
| 2081 | - foreach ($parents as $parent) { |
|
| 2082 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2083 | - } |
|
| 2084 | - |
|
| 2085 | - return true; |
|
| 2086 | - } |
|
| 2087 | - |
|
| 2088 | - /** |
|
| 2089 | - * Unlock a path and all its parents up to the root of the view |
|
| 2090 | - * |
|
| 2091 | - * @param string $path the path of the file to lock relative to the view |
|
| 2092 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2093 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2094 | - * |
|
| 2095 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2096 | - * @throws LockedException |
|
| 2097 | - */ |
|
| 2098 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2099 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2100 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2101 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2102 | - return false; |
|
| 2103 | - } |
|
| 2104 | - |
|
| 2105 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2106 | - |
|
| 2107 | - $parents = $this->getParents($path); |
|
| 2108 | - foreach ($parents as $parent) { |
|
| 2109 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2110 | - } |
|
| 2111 | - |
|
| 2112 | - return true; |
|
| 2113 | - } |
|
| 2114 | - |
|
| 2115 | - /** |
|
| 2116 | - * Only lock files in data/user/files/ |
|
| 2117 | - * |
|
| 2118 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2119 | - * @return bool |
|
| 2120 | - */ |
|
| 2121 | - protected function shouldLockFile($path) { |
|
| 2122 | - $path = Filesystem::normalizePath($path); |
|
| 2123 | - |
|
| 2124 | - $pathSegments = explode('/', $path); |
|
| 2125 | - if (isset($pathSegments[2])) { |
|
| 2126 | - // E.g.: /username/files/path-to-file |
|
| 2127 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2128 | - } |
|
| 2129 | - |
|
| 2130 | - return strpos($path, '/appdata_') !== 0; |
|
| 2131 | - } |
|
| 2132 | - |
|
| 2133 | - /** |
|
| 2134 | - * Shortens the given absolute path to be relative to |
|
| 2135 | - * "$user/files". |
|
| 2136 | - * |
|
| 2137 | - * @param string $absolutePath absolute path which is under "files" |
|
| 2138 | - * |
|
| 2139 | - * @return string path relative to "files" with trimmed slashes or null |
|
| 2140 | - * if the path was NOT relative to files |
|
| 2141 | - * |
|
| 2142 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2143 | - * @since 8.1.0 |
|
| 2144 | - */ |
|
| 2145 | - public function getPathRelativeToFiles($absolutePath) { |
|
| 2146 | - $path = Filesystem::normalizePath($absolutePath); |
|
| 2147 | - $parts = explode('/', trim($path, '/'), 3); |
|
| 2148 | - // "$user", "files", "path/to/dir" |
|
| 2149 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2150 | - $this->logger->error( |
|
| 2151 | - '$absolutePath must be relative to "files", value is "%s"', |
|
| 2152 | - [ |
|
| 2153 | - $absolutePath |
|
| 2154 | - ] |
|
| 2155 | - ); |
|
| 2156 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2157 | - } |
|
| 2158 | - if (isset($parts[2])) { |
|
| 2159 | - return $parts[2]; |
|
| 2160 | - } |
|
| 2161 | - return ''; |
|
| 2162 | - } |
|
| 2163 | - |
|
| 2164 | - /** |
|
| 2165 | - * @param string $filename |
|
| 2166 | - * @return array |
|
| 2167 | - * @throws \OC\User\NoUserException |
|
| 2168 | - * @throws NotFoundException |
|
| 2169 | - */ |
|
| 2170 | - public function getUidAndFilename($filename) { |
|
| 2171 | - $info = $this->getFileInfo($filename); |
|
| 2172 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2173 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2174 | - } |
|
| 2175 | - $uid = $info->getOwner()->getUID(); |
|
| 2176 | - if ($uid != \OC_User::getUser()) { |
|
| 2177 | - Filesystem::initMountPoints($uid); |
|
| 2178 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2179 | - try { |
|
| 2180 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 2181 | - } catch (NotFoundException $e) { |
|
| 2182 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2183 | - } |
|
| 2184 | - } |
|
| 2185 | - return [$uid, $filename]; |
|
| 2186 | - } |
|
| 2187 | - |
|
| 2188 | - /** |
|
| 2189 | - * Creates parent non-existing folders |
|
| 2190 | - * |
|
| 2191 | - * @param string $filePath |
|
| 2192 | - * @return bool |
|
| 2193 | - */ |
|
| 2194 | - private function createParentDirectories($filePath) { |
|
| 2195 | - $directoryParts = explode('/', $filePath); |
|
| 2196 | - $directoryParts = array_filter($directoryParts); |
|
| 2197 | - foreach ($directoryParts as $key => $part) { |
|
| 2198 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2199 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2200 | - if ($this->is_file($currentPath)) { |
|
| 2201 | - return false; |
|
| 2202 | - } |
|
| 2203 | - if (!$this->file_exists($currentPath)) { |
|
| 2204 | - $this->mkdir($currentPath); |
|
| 2205 | - } |
|
| 2206 | - } |
|
| 2207 | - |
|
| 2208 | - return true; |
|
| 2209 | - } |
|
| 87 | + /** @var string */ |
|
| 88 | + private $fakeRoot = ''; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var \OCP\Lock\ILockingProvider |
|
| 92 | + */ |
|
| 93 | + protected $lockingProvider; |
|
| 94 | + |
|
| 95 | + private $lockingEnabled; |
|
| 96 | + |
|
| 97 | + private $updaterEnabled = true; |
|
| 98 | + |
|
| 99 | + /** @var \OC\User\Manager */ |
|
| 100 | + private $userManager; |
|
| 101 | + |
|
| 102 | + /** @var \OCP\ILogger */ |
|
| 103 | + private $logger; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param string $root |
|
| 107 | + * @throws \Exception If $root contains an invalid path |
|
| 108 | + */ |
|
| 109 | + public function __construct($root = '') { |
|
| 110 | + if (is_null($root)) { |
|
| 111 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 112 | + } |
|
| 113 | + if (!Filesystem::isValidPath($root)) { |
|
| 114 | + throw new \Exception(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->fakeRoot = $root; |
|
| 118 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 119 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 120 | + $this->userManager = \OC::$server->getUserManager(); |
|
| 121 | + $this->logger = \OC::$server->getLogger(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + public function getAbsolutePath($path = '/') { |
|
| 125 | + if ($path === null) { |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + $this->assertPathLength($path); |
|
| 129 | + if ($path === '') { |
|
| 130 | + $path = '/'; |
|
| 131 | + } |
|
| 132 | + if ($path[0] !== '/') { |
|
| 133 | + $path = '/' . $path; |
|
| 134 | + } |
|
| 135 | + return $this->fakeRoot . $path; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * change the root to a fake root |
|
| 140 | + * |
|
| 141 | + * @param string $fakeRoot |
|
| 142 | + * @return boolean|null |
|
| 143 | + */ |
|
| 144 | + public function chroot($fakeRoot) { |
|
| 145 | + if (!$fakeRoot == '') { |
|
| 146 | + if ($fakeRoot[0] !== '/') { |
|
| 147 | + $fakeRoot = '/' . $fakeRoot; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + $this->fakeRoot = $fakeRoot; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * get the fake root |
|
| 155 | + * |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function getRoot() { |
|
| 159 | + return $this->fakeRoot; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * get path relative to the root of the view |
|
| 164 | + * |
|
| 165 | + * @param string $path |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + public function getRelativePath($path) { |
|
| 169 | + $this->assertPathLength($path); |
|
| 170 | + if ($this->fakeRoot == '') { |
|
| 171 | + return $path; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 175 | + return '/'; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // missing slashes can cause wrong matches! |
|
| 179 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 180 | + |
|
| 181 | + if (strpos($path, $root) !== 0) { |
|
| 182 | + return null; |
|
| 183 | + } else { |
|
| 184 | + $path = substr($path, strlen($this->fakeRoot)); |
|
| 185 | + if (strlen($path) === 0) { |
|
| 186 | + return '/'; |
|
| 187 | + } else { |
|
| 188 | + return $path; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * get the mountpoint of the storage object for a path |
|
| 195 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 196 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 197 | + * and does not take the chroot into account ) |
|
| 198 | + * |
|
| 199 | + * @param string $path |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function getMountPoint($path) { |
|
| 203 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * get the mountpoint of the storage object for a path |
|
| 208 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 209 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 210 | + * and does not take the chroot into account ) |
|
| 211 | + * |
|
| 212 | + * @param string $path |
|
| 213 | + * @return \OCP\Files\Mount\IMountPoint |
|
| 214 | + */ |
|
| 215 | + public function getMount($path) { |
|
| 216 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * resolve a path to a storage and internal path |
|
| 221 | + * |
|
| 222 | + * @param string $path |
|
| 223 | + * @return array an array consisting of the storage and the internal path |
|
| 224 | + */ |
|
| 225 | + public function resolvePath($path) { |
|
| 226 | + $a = $this->getAbsolutePath($path); |
|
| 227 | + $p = Filesystem::normalizePath($a); |
|
| 228 | + return Filesystem::resolvePath($p); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * return the path to a local version of the file |
|
| 233 | + * we need this because we can't know if a file is stored local or not from |
|
| 234 | + * outside the filestorage and for some purposes a local file is needed |
|
| 235 | + * |
|
| 236 | + * @param string $path |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + public function getLocalFile($path) { |
|
| 240 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 241 | + $path = $this->getAbsolutePath($path); |
|
| 242 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 243 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 244 | + return $storage->getLocalFile($internalPath); |
|
| 245 | + } else { |
|
| 246 | + return null; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param string $path |
|
| 252 | + * @return string |
|
| 253 | + */ |
|
| 254 | + public function getLocalFolder($path) { |
|
| 255 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 256 | + $path = $this->getAbsolutePath($path); |
|
| 257 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 258 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 259 | + return $storage->getLocalFolder($internalPath); |
|
| 260 | + } else { |
|
| 261 | + return null; |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * the following functions operate with arguments and return values identical |
|
| 267 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 268 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 269 | + */ |
|
| 270 | + public function mkdir($path) { |
|
| 271 | + return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * remove mount point |
|
| 276 | + * |
|
| 277 | + * @param IMountPoint $mount |
|
| 278 | + * @param string $path relative to data/ |
|
| 279 | + * @return boolean |
|
| 280 | + */ |
|
| 281 | + protected function removeMount($mount, $path) { |
|
| 282 | + if ($mount instanceof MoveableMount) { |
|
| 283 | + // cut of /user/files to get the relative path to data/user/files |
|
| 284 | + $pathParts = explode('/', $path, 4); |
|
| 285 | + $relPath = '/' . $pathParts[3]; |
|
| 286 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 287 | + \OC_Hook::emit( |
|
| 288 | + Filesystem::CLASSNAME, "umount", |
|
| 289 | + [Filesystem::signal_param_path => $relPath] |
|
| 290 | + ); |
|
| 291 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 292 | + $result = $mount->removeMount(); |
|
| 293 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 294 | + if ($result) { |
|
| 295 | + \OC_Hook::emit( |
|
| 296 | + Filesystem::CLASSNAME, "post_umount", |
|
| 297 | + [Filesystem::signal_param_path => $relPath] |
|
| 298 | + ); |
|
| 299 | + } |
|
| 300 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 301 | + return $result; |
|
| 302 | + } else { |
|
| 303 | + // do not allow deleting the storage's root / the mount point |
|
| 304 | + // because for some storages it might delete the whole contents |
|
| 305 | + // but isn't supposed to work that way |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + public function disableCacheUpdate() { |
|
| 311 | + $this->updaterEnabled = false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + public function enableCacheUpdate() { |
|
| 315 | + $this->updaterEnabled = true; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 319 | + if ($this->updaterEnabled) { |
|
| 320 | + if (is_null($time)) { |
|
| 321 | + $time = time(); |
|
| 322 | + } |
|
| 323 | + $storage->getUpdater()->update($internalPath, $time); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 328 | + if ($this->updaterEnabled) { |
|
| 329 | + $storage->getUpdater()->remove($internalPath); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 334 | + if ($this->updaterEnabled) { |
|
| 335 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * @param string $path |
|
| 341 | + * @return bool|mixed |
|
| 342 | + */ |
|
| 343 | + public function rmdir($path) { |
|
| 344 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 345 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 346 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 347 | + return $this->removeMount($mount, $absolutePath); |
|
| 348 | + } |
|
| 349 | + if ($this->is_dir($path)) { |
|
| 350 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 351 | + } else { |
|
| 352 | + $result = false; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 356 | + $storage = $mount->getStorage(); |
|
| 357 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 358 | + $storage->getUpdater()->remove($internalPath); |
|
| 359 | + } |
|
| 360 | + return $result; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * @param string $path |
|
| 365 | + * @return resource |
|
| 366 | + */ |
|
| 367 | + public function opendir($path) { |
|
| 368 | + return $this->basicOperation('opendir', $path, ['read']); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @param string $path |
|
| 373 | + * @return bool|mixed |
|
| 374 | + */ |
|
| 375 | + public function is_dir($path) { |
|
| 376 | + if ($path == '/') { |
|
| 377 | + return true; |
|
| 378 | + } |
|
| 379 | + return $this->basicOperation('is_dir', $path); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * @param string $path |
|
| 384 | + * @return bool|mixed |
|
| 385 | + */ |
|
| 386 | + public function is_file($path) { |
|
| 387 | + if ($path == '/') { |
|
| 388 | + return false; |
|
| 389 | + } |
|
| 390 | + return $this->basicOperation('is_file', $path); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @param string $path |
|
| 395 | + * @return mixed |
|
| 396 | + */ |
|
| 397 | + public function stat($path) { |
|
| 398 | + return $this->basicOperation('stat', $path); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @param string $path |
|
| 403 | + * @return mixed |
|
| 404 | + */ |
|
| 405 | + public function filetype($path) { |
|
| 406 | + return $this->basicOperation('filetype', $path); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * @param string $path |
|
| 411 | + * @return mixed |
|
| 412 | + */ |
|
| 413 | + public function filesize($path) { |
|
| 414 | + return $this->basicOperation('filesize', $path); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * @param string $path |
|
| 419 | + * @return bool|mixed |
|
| 420 | + * @throws \OCP\Files\InvalidPathException |
|
| 421 | + */ |
|
| 422 | + public function readfile($path) { |
|
| 423 | + $this->assertPathLength($path); |
|
| 424 | + if (ob_get_level()) { |
|
| 425 | + ob_end_clean(); |
|
| 426 | + } |
|
| 427 | + $handle = $this->fopen($path, 'rb'); |
|
| 428 | + if ($handle) { |
|
| 429 | + $chunkSize = 524288; // 512 kB chunks |
|
| 430 | + while (!feof($handle)) { |
|
| 431 | + echo fread($handle, $chunkSize); |
|
| 432 | + flush(); |
|
| 433 | + } |
|
| 434 | + fclose($handle); |
|
| 435 | + return $this->filesize($path); |
|
| 436 | + } |
|
| 437 | + return false; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * @param string $path |
|
| 442 | + * @param int $from |
|
| 443 | + * @param int $to |
|
| 444 | + * @return bool|mixed |
|
| 445 | + * @throws \OCP\Files\InvalidPathException |
|
| 446 | + * @throws \OCP\Files\UnseekableException |
|
| 447 | + */ |
|
| 448 | + public function readfilePart($path, $from, $to) { |
|
| 449 | + $this->assertPathLength($path); |
|
| 450 | + if (ob_get_level()) { |
|
| 451 | + ob_end_clean(); |
|
| 452 | + } |
|
| 453 | + $handle = $this->fopen($path, 'rb'); |
|
| 454 | + if ($handle) { |
|
| 455 | + $chunkSize = 524288; // 512 kB chunks |
|
| 456 | + $startReading = true; |
|
| 457 | + |
|
| 458 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 459 | + // forward file handle via chunked fread because fseek seem to have failed |
|
| 460 | + |
|
| 461 | + $end = $from + 1; |
|
| 462 | + while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
| 463 | + $len = $from - ftell($handle); |
|
| 464 | + if ($len > $chunkSize) { |
|
| 465 | + $len = $chunkSize; |
|
| 466 | + } |
|
| 467 | + $result = fread($handle, $len); |
|
| 468 | + |
|
| 469 | + if ($result === false) { |
|
| 470 | + $startReading = false; |
|
| 471 | + break; |
|
| 472 | + } |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + if ($startReading) { |
|
| 477 | + $end = $to + 1; |
|
| 478 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 479 | + $len = $end - ftell($handle); |
|
| 480 | + if ($len > $chunkSize) { |
|
| 481 | + $len = $chunkSize; |
|
| 482 | + } |
|
| 483 | + echo fread($handle, $len); |
|
| 484 | + flush(); |
|
| 485 | + } |
|
| 486 | + return ftell($handle) - $from; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 490 | + } |
|
| 491 | + return false; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * @param string $path |
|
| 496 | + * @return mixed |
|
| 497 | + */ |
|
| 498 | + public function isCreatable($path) { |
|
| 499 | + return $this->basicOperation('isCreatable', $path); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * @param string $path |
|
| 504 | + * @return mixed |
|
| 505 | + */ |
|
| 506 | + public function isReadable($path) { |
|
| 507 | + return $this->basicOperation('isReadable', $path); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * @param string $path |
|
| 512 | + * @return mixed |
|
| 513 | + */ |
|
| 514 | + public function isUpdatable($path) { |
|
| 515 | + return $this->basicOperation('isUpdatable', $path); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * @param string $path |
|
| 520 | + * @return bool|mixed |
|
| 521 | + */ |
|
| 522 | + public function isDeletable($path) { |
|
| 523 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 524 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 525 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 526 | + return $mount instanceof MoveableMount; |
|
| 527 | + } |
|
| 528 | + return $this->basicOperation('isDeletable', $path); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * @param string $path |
|
| 533 | + * @return mixed |
|
| 534 | + */ |
|
| 535 | + public function isSharable($path) { |
|
| 536 | + return $this->basicOperation('isSharable', $path); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * @param string $path |
|
| 541 | + * @return bool|mixed |
|
| 542 | + */ |
|
| 543 | + public function file_exists($path) { |
|
| 544 | + if ($path == '/') { |
|
| 545 | + return true; |
|
| 546 | + } |
|
| 547 | + return $this->basicOperation('file_exists', $path); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * @param string $path |
|
| 552 | + * @return mixed |
|
| 553 | + */ |
|
| 554 | + public function filemtime($path) { |
|
| 555 | + return $this->basicOperation('filemtime', $path); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * @param string $path |
|
| 560 | + * @param int|string $mtime |
|
| 561 | + * @return bool |
|
| 562 | + */ |
|
| 563 | + public function touch($path, $mtime = null) { |
|
| 564 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 565 | + $mtime = strtotime($mtime); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + $hooks = ['touch']; |
|
| 569 | + |
|
| 570 | + if (!$this->file_exists($path)) { |
|
| 571 | + $hooks[] = 'create'; |
|
| 572 | + $hooks[] = 'write'; |
|
| 573 | + } |
|
| 574 | + try { |
|
| 575 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 576 | + } catch (\Exception $e) { |
|
| 577 | + $this->logger->logException($e, ['level' => ILogger::INFO, 'message' => 'Error while setting modified time']); |
|
| 578 | + $result = false; |
|
| 579 | + } |
|
| 580 | + if (!$result) { |
|
| 581 | + // If create file fails because of permissions on external storage like SMB folders, |
|
| 582 | + // check file exists and return false if not. |
|
| 583 | + if (!$this->file_exists($path)) { |
|
| 584 | + return false; |
|
| 585 | + } |
|
| 586 | + if (is_null($mtime)) { |
|
| 587 | + $mtime = time(); |
|
| 588 | + } |
|
| 589 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 590 | + $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
| 591 | + } |
|
| 592 | + return true; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * @param string $path |
|
| 597 | + * @return mixed |
|
| 598 | + * @throws LockedException |
|
| 599 | + */ |
|
| 600 | + public function file_get_contents($path) { |
|
| 601 | + return $this->basicOperation('file_get_contents', $path, ['read']); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * @param bool $exists |
|
| 606 | + * @param string $path |
|
| 607 | + * @param bool $run |
|
| 608 | + */ |
|
| 609 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 610 | + if (!$exists) { |
|
| 611 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
| 612 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 613 | + Filesystem::signal_param_run => &$run, |
|
| 614 | + ]); |
|
| 615 | + } else { |
|
| 616 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
| 617 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 618 | + Filesystem::signal_param_run => &$run, |
|
| 619 | + ]); |
|
| 620 | + } |
|
| 621 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
| 622 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 623 | + Filesystem::signal_param_run => &$run, |
|
| 624 | + ]); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * @param bool $exists |
|
| 629 | + * @param string $path |
|
| 630 | + */ |
|
| 631 | + protected function emit_file_hooks_post($exists, $path) { |
|
| 632 | + if (!$exists) { |
|
| 633 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
| 634 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 635 | + ]); |
|
| 636 | + } else { |
|
| 637 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
| 638 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 639 | + ]); |
|
| 640 | + } |
|
| 641 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
| 642 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 643 | + ]); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * @param string $path |
|
| 648 | + * @param string|resource $data |
|
| 649 | + * @return bool|mixed |
|
| 650 | + * @throws LockedException |
|
| 651 | + */ |
|
| 652 | + public function file_put_contents($path, $data) { |
|
| 653 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 654 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 655 | + if (Filesystem::isValidPath($path) |
|
| 656 | + and !Filesystem::isFileBlacklisted($path) |
|
| 657 | + ) { |
|
| 658 | + $path = $this->getRelativePath($absolutePath); |
|
| 659 | + |
|
| 660 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 661 | + |
|
| 662 | + $exists = $this->file_exists($path); |
|
| 663 | + $run = true; |
|
| 664 | + if ($this->shouldEmitHooks($path)) { |
|
| 665 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 666 | + } |
|
| 667 | + if (!$run) { |
|
| 668 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 669 | + return false; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + try { |
|
| 673 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 674 | + } catch (\Exception $e) { |
|
| 675 | + // Release the shared lock before throwing. |
|
| 676 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 677 | + throw $e; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 681 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 682 | + $target = $storage->fopen($internalPath, 'w'); |
|
| 683 | + if ($target) { |
|
| 684 | + [, $result] = \OC_Helper::streamCopy($data, $target); |
|
| 685 | + fclose($target); |
|
| 686 | + fclose($data); |
|
| 687 | + |
|
| 688 | + $this->writeUpdate($storage, $internalPath); |
|
| 689 | + |
|
| 690 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 691 | + |
|
| 692 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 693 | + $this->emit_file_hooks_post($exists, $path); |
|
| 694 | + } |
|
| 695 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 696 | + return $result; |
|
| 697 | + } else { |
|
| 698 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 699 | + return false; |
|
| 700 | + } |
|
| 701 | + } else { |
|
| 702 | + return false; |
|
| 703 | + } |
|
| 704 | + } else { |
|
| 705 | + $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
| 706 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * @param string $path |
|
| 712 | + * @return bool|mixed |
|
| 713 | + */ |
|
| 714 | + public function unlink($path) { |
|
| 715 | + if ($path === '' || $path === '/') { |
|
| 716 | + // do not allow deleting the root |
|
| 717 | + return false; |
|
| 718 | + } |
|
| 719 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 720 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 721 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 722 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 723 | + return $this->removeMount($mount, $absolutePath); |
|
| 724 | + } |
|
| 725 | + if ($this->is_dir($path)) { |
|
| 726 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 727 | + } else { |
|
| 728 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 729 | + } |
|
| 730 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 731 | + $storage = $mount->getStorage(); |
|
| 732 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 733 | + $storage->getUpdater()->remove($internalPath); |
|
| 734 | + return true; |
|
| 735 | + } else { |
|
| 736 | + return $result; |
|
| 737 | + } |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * @param string $directory |
|
| 742 | + * @return bool|mixed |
|
| 743 | + */ |
|
| 744 | + public function deleteAll($directory) { |
|
| 745 | + return $this->rmdir($directory); |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + /** |
|
| 749 | + * Rename/move a file or folder from the source path to target path. |
|
| 750 | + * |
|
| 751 | + * @param string $path1 source path |
|
| 752 | + * @param string $path2 target path |
|
| 753 | + * |
|
| 754 | + * @return bool|mixed |
|
| 755 | + * @throws LockedException |
|
| 756 | + */ |
|
| 757 | + public function rename($path1, $path2) { |
|
| 758 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 759 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 760 | + $result = false; |
|
| 761 | + if ( |
|
| 762 | + Filesystem::isValidPath($path2) |
|
| 763 | + and Filesystem::isValidPath($path1) |
|
| 764 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 765 | + ) { |
|
| 766 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 767 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 768 | + $exists = $this->file_exists($path2); |
|
| 769 | + |
|
| 770 | + if ($path1 == null or $path2 == null) { |
|
| 771 | + return false; |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 775 | + try { |
|
| 776 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 777 | + |
|
| 778 | + $run = true; |
|
| 779 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 780 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 781 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 782 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
| 783 | + \OC_Hook::emit( |
|
| 784 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 785 | + [ |
|
| 786 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 787 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 788 | + Filesystem::signal_param_run => &$run |
|
| 789 | + ] |
|
| 790 | + ); |
|
| 791 | + } |
|
| 792 | + if ($run) { |
|
| 793 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
| 794 | + |
|
| 795 | + $manager = Filesystem::getMountManager(); |
|
| 796 | + $mount1 = $this->getMount($path1); |
|
| 797 | + $mount2 = $this->getMount($path2); |
|
| 798 | + $storage1 = $mount1->getStorage(); |
|
| 799 | + $storage2 = $mount2->getStorage(); |
|
| 800 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 801 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 802 | + |
|
| 803 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 804 | + try { |
|
| 805 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 806 | + |
|
| 807 | + if ($internalPath1 === '') { |
|
| 808 | + if ($mount1 instanceof MoveableMount) { |
|
| 809 | + $sourceParentMount = $this->getMount(dirname($path1)); |
|
| 810 | + if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) { |
|
| 811 | + /** |
|
| 812 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 813 | + */ |
|
| 814 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
| 815 | + $result = $mount1->moveMount($absolutePath2); |
|
| 816 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 817 | + } else { |
|
| 818 | + $result = false; |
|
| 819 | + } |
|
| 820 | + } else { |
|
| 821 | + $result = false; |
|
| 822 | + } |
|
| 823 | + // moving a file/folder within the same mount point |
|
| 824 | + } elseif ($storage1 === $storage2) { |
|
| 825 | + if ($storage1) { |
|
| 826 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 827 | + } else { |
|
| 828 | + $result = false; |
|
| 829 | + } |
|
| 830 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 831 | + } else { |
|
| 832 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 836 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 837 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 838 | + } elseif ($result) { |
|
| 839 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 840 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 841 | + } |
|
| 842 | + } |
|
| 843 | + } catch (\Exception $e) { |
|
| 844 | + throw $e; |
|
| 845 | + } finally { |
|
| 846 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 847 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 851 | + if ($this->shouldEmitHooks()) { |
|
| 852 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 853 | + } |
|
| 854 | + } elseif ($result) { |
|
| 855 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 856 | + \OC_Hook::emit( |
|
| 857 | + Filesystem::CLASSNAME, |
|
| 858 | + Filesystem::signal_post_rename, |
|
| 859 | + [ |
|
| 860 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 861 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 862 | + ] |
|
| 863 | + ); |
|
| 864 | + } |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + } catch (\Exception $e) { |
|
| 868 | + throw $e; |
|
| 869 | + } finally { |
|
| 870 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 871 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 872 | + } |
|
| 873 | + } |
|
| 874 | + return $result; |
|
| 875 | + } |
|
| 876 | + |
|
| 877 | + /** |
|
| 878 | + * Copy a file/folder from the source path to target path |
|
| 879 | + * |
|
| 880 | + * @param string $path1 source path |
|
| 881 | + * @param string $path2 target path |
|
| 882 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 883 | + * |
|
| 884 | + * @return bool|mixed |
|
| 885 | + */ |
|
| 886 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
| 887 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 888 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 889 | + $result = false; |
|
| 890 | + if ( |
|
| 891 | + Filesystem::isValidPath($path2) |
|
| 892 | + and Filesystem::isValidPath($path1) |
|
| 893 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 894 | + ) { |
|
| 895 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 896 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 897 | + |
|
| 898 | + if ($path1 == null or $path2 == null) { |
|
| 899 | + return false; |
|
| 900 | + } |
|
| 901 | + $run = true; |
|
| 902 | + |
|
| 903 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 904 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 905 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 906 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 907 | + |
|
| 908 | + try { |
|
| 909 | + $exists = $this->file_exists($path2); |
|
| 910 | + if ($this->shouldEmitHooks()) { |
|
| 911 | + \OC_Hook::emit( |
|
| 912 | + Filesystem::CLASSNAME, |
|
| 913 | + Filesystem::signal_copy, |
|
| 914 | + [ |
|
| 915 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 916 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 917 | + Filesystem::signal_param_run => &$run |
|
| 918 | + ] |
|
| 919 | + ); |
|
| 920 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 921 | + } |
|
| 922 | + if ($run) { |
|
| 923 | + $mount1 = $this->getMount($path1); |
|
| 924 | + $mount2 = $this->getMount($path2); |
|
| 925 | + $storage1 = $mount1->getStorage(); |
|
| 926 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 927 | + $storage2 = $mount2->getStorage(); |
|
| 928 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 929 | + |
|
| 930 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 931 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 932 | + |
|
| 933 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 934 | + if ($storage1) { |
|
| 935 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 936 | + } else { |
|
| 937 | + $result = false; |
|
| 938 | + } |
|
| 939 | + } else { |
|
| 940 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 944 | + |
|
| 945 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 946 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 947 | + |
|
| 948 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
| 949 | + \OC_Hook::emit( |
|
| 950 | + Filesystem::CLASSNAME, |
|
| 951 | + Filesystem::signal_post_copy, |
|
| 952 | + [ |
|
| 953 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 954 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 955 | + ] |
|
| 956 | + ); |
|
| 957 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 958 | + } |
|
| 959 | + } |
|
| 960 | + } catch (\Exception $e) { |
|
| 961 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 962 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 963 | + throw $e; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 967 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 968 | + } |
|
| 969 | + return $result; |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * @param string $path |
|
| 974 | + * @param string $mode 'r' or 'w' |
|
| 975 | + * @return resource |
|
| 976 | + * @throws LockedException |
|
| 977 | + */ |
|
| 978 | + public function fopen($path, $mode) { |
|
| 979 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 980 | + $hooks = []; |
|
| 981 | + switch ($mode) { |
|
| 982 | + case 'r': |
|
| 983 | + $hooks[] = 'read'; |
|
| 984 | + break; |
|
| 985 | + case 'r+': |
|
| 986 | + case 'w+': |
|
| 987 | + case 'x+': |
|
| 988 | + case 'a+': |
|
| 989 | + $hooks[] = 'read'; |
|
| 990 | + $hooks[] = 'write'; |
|
| 991 | + break; |
|
| 992 | + case 'w': |
|
| 993 | + case 'x': |
|
| 994 | + case 'a': |
|
| 995 | + $hooks[] = 'write'; |
|
| 996 | + break; |
|
| 997 | + default: |
|
| 998 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + if ($mode !== 'r' && $mode !== 'w') { |
|
| 1002 | + \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'); |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 1006 | + } |
|
| 1007 | + |
|
| 1008 | + /** |
|
| 1009 | + * @param string $path |
|
| 1010 | + * @return bool|string |
|
| 1011 | + * @throws \OCP\Files\InvalidPathException |
|
| 1012 | + */ |
|
| 1013 | + public function toTmpFile($path) { |
|
| 1014 | + $this->assertPathLength($path); |
|
| 1015 | + if (Filesystem::isValidPath($path)) { |
|
| 1016 | + $source = $this->fopen($path, 'r'); |
|
| 1017 | + if ($source) { |
|
| 1018 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1019 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1020 | + file_put_contents($tmpFile, $source); |
|
| 1021 | + return $tmpFile; |
|
| 1022 | + } else { |
|
| 1023 | + return false; |
|
| 1024 | + } |
|
| 1025 | + } else { |
|
| 1026 | + return false; |
|
| 1027 | + } |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * @param string $tmpFile |
|
| 1032 | + * @param string $path |
|
| 1033 | + * @return bool|mixed |
|
| 1034 | + * @throws \OCP\Files\InvalidPathException |
|
| 1035 | + */ |
|
| 1036 | + public function fromTmpFile($tmpFile, $path) { |
|
| 1037 | + $this->assertPathLength($path); |
|
| 1038 | + if (Filesystem::isValidPath($path)) { |
|
| 1039 | + |
|
| 1040 | + // Get directory that the file is going into |
|
| 1041 | + $filePath = dirname($path); |
|
| 1042 | + |
|
| 1043 | + // Create the directories if any |
|
| 1044 | + if (!$this->file_exists($filePath)) { |
|
| 1045 | + $result = $this->createParentDirectories($filePath); |
|
| 1046 | + if ($result === false) { |
|
| 1047 | + return false; |
|
| 1048 | + } |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + $source = fopen($tmpFile, 'r'); |
|
| 1052 | + if ($source) { |
|
| 1053 | + $result = $this->file_put_contents($path, $source); |
|
| 1054 | + // $this->file_put_contents() might have already closed |
|
| 1055 | + // the resource, so we check it, before trying to close it |
|
| 1056 | + // to avoid messages in the error log. |
|
| 1057 | + if (is_resource($source)) { |
|
| 1058 | + fclose($source); |
|
| 1059 | + } |
|
| 1060 | + unlink($tmpFile); |
|
| 1061 | + return $result; |
|
| 1062 | + } else { |
|
| 1063 | + return false; |
|
| 1064 | + } |
|
| 1065 | + } else { |
|
| 1066 | + return false; |
|
| 1067 | + } |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + |
|
| 1071 | + /** |
|
| 1072 | + * @param string $path |
|
| 1073 | + * @return mixed |
|
| 1074 | + * @throws \OCP\Files\InvalidPathException |
|
| 1075 | + */ |
|
| 1076 | + public function getMimeType($path) { |
|
| 1077 | + $this->assertPathLength($path); |
|
| 1078 | + return $this->basicOperation('getMimeType', $path); |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + /** |
|
| 1082 | + * @param string $type |
|
| 1083 | + * @param string $path |
|
| 1084 | + * @param bool $raw |
|
| 1085 | + * @return bool|string |
|
| 1086 | + */ |
|
| 1087 | + public function hash($type, $path, $raw = false) { |
|
| 1088 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1089 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1090 | + if (Filesystem::isValidPath($path)) { |
|
| 1091 | + $path = $this->getRelativePath($absolutePath); |
|
| 1092 | + if ($path == null) { |
|
| 1093 | + return false; |
|
| 1094 | + } |
|
| 1095 | + if ($this->shouldEmitHooks($path)) { |
|
| 1096 | + \OC_Hook::emit( |
|
| 1097 | + Filesystem::CLASSNAME, |
|
| 1098 | + Filesystem::signal_read, |
|
| 1099 | + [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
| 1100 | + ); |
|
| 1101 | + } |
|
| 1102 | + /** @var Storage|null $storage */ |
|
| 1103 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1104 | + if ($storage) { |
|
| 1105 | + return $storage->hash($type, $internalPath, $raw); |
|
| 1106 | + } |
|
| 1107 | + } |
|
| 1108 | + return false; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * @param string $path |
|
| 1113 | + * @return mixed |
|
| 1114 | + * @throws \OCP\Files\InvalidPathException |
|
| 1115 | + */ |
|
| 1116 | + public function free_space($path = '/') { |
|
| 1117 | + $this->assertPathLength($path); |
|
| 1118 | + $result = $this->basicOperation('free_space', $path); |
|
| 1119 | + if ($result === null) { |
|
| 1120 | + throw new InvalidPathException(); |
|
| 1121 | + } |
|
| 1122 | + return $result; |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + /** |
|
| 1126 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1127 | + * |
|
| 1128 | + * @param string $operation |
|
| 1129 | + * @param string $path |
|
| 1130 | + * @param array $hooks (optional) |
|
| 1131 | + * @param mixed $extraParam (optional) |
|
| 1132 | + * @return mixed |
|
| 1133 | + * @throws LockedException |
|
| 1134 | + * |
|
| 1135 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1136 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1137 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1138 | + */ |
|
| 1139 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1140 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1141 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1142 | + if (Filesystem::isValidPath($path) |
|
| 1143 | + and !Filesystem::isFileBlacklisted($path) |
|
| 1144 | + ) { |
|
| 1145 | + $path = $this->getRelativePath($absolutePath); |
|
| 1146 | + if ($path == null) { |
|
| 1147 | + return false; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1151 | + // always a shared lock during pre-hooks so the hook can read the file |
|
| 1152 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + $run = $this->runHooks($hooks, $path); |
|
| 1156 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1157 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1158 | + if ($run and $storage) { |
|
| 1159 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1160 | + try { |
|
| 1161 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1162 | + } catch (LockedException $e) { |
|
| 1163 | + // release the shared lock we acquired before quiting |
|
| 1164 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1165 | + throw $e; |
|
| 1166 | + } |
|
| 1167 | + } |
|
| 1168 | + try { |
|
| 1169 | + if (!is_null($extraParam)) { |
|
| 1170 | + $result = $storage->$operation($internalPath, $extraParam); |
|
| 1171 | + } else { |
|
| 1172 | + $result = $storage->$operation($internalPath); |
|
| 1173 | + } |
|
| 1174 | + } catch (\Exception $e) { |
|
| 1175 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1176 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1177 | + } elseif (in_array('read', $hooks)) { |
|
| 1178 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1179 | + } |
|
| 1180 | + throw $e; |
|
| 1181 | + } |
|
| 1182 | + |
|
| 1183 | + if ($result && in_array('delete', $hooks)) { |
|
| 1184 | + $this->removeUpdate($storage, $internalPath); |
|
| 1185 | + } |
|
| 1186 | + if ($result && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
| 1187 | + $this->writeUpdate($storage, $internalPath); |
|
| 1188 | + } |
|
| 1189 | + if ($result && in_array('touch', $hooks)) { |
|
| 1190 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1194 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + $unlockLater = false; |
|
| 1198 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1199 | + $unlockLater = true; |
|
| 1200 | + // make sure our unlocking callback will still be called if connection is aborted |
|
| 1201 | + ignore_user_abort(true); |
|
| 1202 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1203 | + if (in_array('write', $hooks)) { |
|
| 1204 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1205 | + } elseif (in_array('read', $hooks)) { |
|
| 1206 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1207 | + } |
|
| 1208 | + }); |
|
| 1209 | + } |
|
| 1210 | + |
|
| 1211 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1212 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1213 | + $this->runHooks($hooks, $path, true); |
|
| 1214 | + } |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + if (!$unlockLater |
|
| 1218 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1219 | + ) { |
|
| 1220 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1221 | + } |
|
| 1222 | + return $result; |
|
| 1223 | + } else { |
|
| 1224 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1225 | + } |
|
| 1226 | + } |
|
| 1227 | + return null; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + /** |
|
| 1231 | + * get the path relative to the default root for hook usage |
|
| 1232 | + * |
|
| 1233 | + * @param string $path |
|
| 1234 | + * @return string |
|
| 1235 | + */ |
|
| 1236 | + private function getHookPath($path) { |
|
| 1237 | + if (!Filesystem::getView()) { |
|
| 1238 | + return $path; |
|
| 1239 | + } |
|
| 1240 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + private function shouldEmitHooks($path = '') { |
|
| 1244 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1245 | + return false; |
|
| 1246 | + } |
|
| 1247 | + if (!Filesystem::$loaded) { |
|
| 1248 | + return false; |
|
| 1249 | + } |
|
| 1250 | + $defaultRoot = Filesystem::getRoot(); |
|
| 1251 | + if ($defaultRoot === null) { |
|
| 1252 | + return false; |
|
| 1253 | + } |
|
| 1254 | + if ($this->fakeRoot === $defaultRoot) { |
|
| 1255 | + return true; |
|
| 1256 | + } |
|
| 1257 | + $fullPath = $this->getAbsolutePath($path); |
|
| 1258 | + |
|
| 1259 | + if ($fullPath === $defaultRoot) { |
|
| 1260 | + return true; |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + /** |
|
| 1267 | + * @param string[] $hooks |
|
| 1268 | + * @param string $path |
|
| 1269 | + * @param bool $post |
|
| 1270 | + * @return bool |
|
| 1271 | + */ |
|
| 1272 | + private function runHooks($hooks, $path, $post = false) { |
|
| 1273 | + $relativePath = $path; |
|
| 1274 | + $path = $this->getHookPath($path); |
|
| 1275 | + $prefix = $post ? 'post_' : ''; |
|
| 1276 | + $run = true; |
|
| 1277 | + if ($this->shouldEmitHooks($relativePath)) { |
|
| 1278 | + foreach ($hooks as $hook) { |
|
| 1279 | + if ($hook != 'read') { |
|
| 1280 | + \OC_Hook::emit( |
|
| 1281 | + Filesystem::CLASSNAME, |
|
| 1282 | + $prefix . $hook, |
|
| 1283 | + [ |
|
| 1284 | + Filesystem::signal_param_run => &$run, |
|
| 1285 | + Filesystem::signal_param_path => $path |
|
| 1286 | + ] |
|
| 1287 | + ); |
|
| 1288 | + } elseif (!$post) { |
|
| 1289 | + \OC_Hook::emit( |
|
| 1290 | + Filesystem::CLASSNAME, |
|
| 1291 | + $prefix . $hook, |
|
| 1292 | + [ |
|
| 1293 | + Filesystem::signal_param_path => $path |
|
| 1294 | + ] |
|
| 1295 | + ); |
|
| 1296 | + } |
|
| 1297 | + } |
|
| 1298 | + } |
|
| 1299 | + return $run; |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + /** |
|
| 1303 | + * check if a file or folder has been updated since $time |
|
| 1304 | + * |
|
| 1305 | + * @param string $path |
|
| 1306 | + * @param int $time |
|
| 1307 | + * @return bool |
|
| 1308 | + */ |
|
| 1309 | + public function hasUpdated($path, $time) { |
|
| 1310 | + return $this->basicOperation('hasUpdated', $path, [], $time); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * @param string $ownerId |
|
| 1315 | + * @return \OC\User\User |
|
| 1316 | + */ |
|
| 1317 | + private function getUserObjectForOwner($ownerId) { |
|
| 1318 | + $owner = $this->userManager->get($ownerId); |
|
| 1319 | + if ($owner instanceof IUser) { |
|
| 1320 | + return $owner; |
|
| 1321 | + } else { |
|
| 1322 | + return new User($ownerId, null, \OC::$server->getEventDispatcher()); |
|
| 1323 | + } |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + /** |
|
| 1327 | + * Get file info from cache |
|
| 1328 | + * |
|
| 1329 | + * If the file is not in cached it will be scanned |
|
| 1330 | + * If the file has changed on storage the cache will be updated |
|
| 1331 | + * |
|
| 1332 | + * @param \OC\Files\Storage\Storage $storage |
|
| 1333 | + * @param string $internalPath |
|
| 1334 | + * @param string $relativePath |
|
| 1335 | + * @return ICacheEntry|bool |
|
| 1336 | + */ |
|
| 1337 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1338 | + $cache = $storage->getCache($internalPath); |
|
| 1339 | + $data = $cache->get($internalPath); |
|
| 1340 | + $watcher = $storage->getWatcher($internalPath); |
|
| 1341 | + |
|
| 1342 | + try { |
|
| 1343 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1344 | + if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
| 1345 | + if (!$storage->file_exists($internalPath)) { |
|
| 1346 | + return false; |
|
| 1347 | + } |
|
| 1348 | + // don't need to get a lock here since the scanner does it's own locking |
|
| 1349 | + $scanner = $storage->getScanner($internalPath); |
|
| 1350 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1351 | + $data = $cache->get($internalPath); |
|
| 1352 | + } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1353 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1354 | + $watcher->update($internalPath, $data); |
|
| 1355 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1356 | + $data = $cache->get($internalPath); |
|
| 1357 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1358 | + } |
|
| 1359 | + } catch (LockedException $e) { |
|
| 1360 | + // if the file is locked we just use the old cache info |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + return $data; |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + /** |
|
| 1367 | + * get the filesystem info |
|
| 1368 | + * |
|
| 1369 | + * @param string $path |
|
| 1370 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1371 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1372 | + * defaults to true |
|
| 1373 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1374 | + */ |
|
| 1375 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1376 | + $this->assertPathLength($path); |
|
| 1377 | + if (!Filesystem::isValidPath($path)) { |
|
| 1378 | + return false; |
|
| 1379 | + } |
|
| 1380 | + if (Cache\Scanner::isPartialFile($path)) { |
|
| 1381 | + return $this->getPartFileInfo($path); |
|
| 1382 | + } |
|
| 1383 | + $relativePath = $path; |
|
| 1384 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1385 | + |
|
| 1386 | + $mount = Filesystem::getMountManager()->find($path); |
|
| 1387 | + $storage = $mount->getStorage(); |
|
| 1388 | + $internalPath = $mount->getInternalPath($path); |
|
| 1389 | + if ($storage) { |
|
| 1390 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1391 | + |
|
| 1392 | + if (!$data instanceof ICacheEntry) { |
|
| 1393 | + return false; |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1397 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1398 | + } |
|
| 1399 | + $ownerId = $storage->getOwner($internalPath); |
|
| 1400 | + $owner = null; |
|
| 1401 | + if ($ownerId !== null && $ownerId !== false) { |
|
| 1402 | + // ownerId might be null if files are accessed with an access token without file system access |
|
| 1403 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1404 | + } |
|
| 1405 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1406 | + |
|
| 1407 | + if (isset($data['fileid'])) { |
|
| 1408 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1409 | + //add the sizes of other mount points to the folder |
|
| 1410 | + $extOnly = ($includeMountPoints === 'ext'); |
|
| 1411 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1412 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1413 | + $subStorage = $mount->getStorage(); |
|
| 1414 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1415 | + })); |
|
| 1416 | + } |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + return $info; |
|
| 1420 | + } else { |
|
| 1421 | + \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + return false; |
|
| 1425 | + } |
|
| 1426 | + |
|
| 1427 | + /** |
|
| 1428 | + * get the content of a directory |
|
| 1429 | + * |
|
| 1430 | + * @param string $directory path under datadirectory |
|
| 1431 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1432 | + * @return FileInfo[] |
|
| 1433 | + */ |
|
| 1434 | + public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) { |
|
| 1435 | + $this->assertPathLength($directory); |
|
| 1436 | + if (!Filesystem::isValidPath($directory)) { |
|
| 1437 | + return []; |
|
| 1438 | + } |
|
| 1439 | + |
|
| 1440 | + $path = $this->getAbsolutePath($directory); |
|
| 1441 | + $path = Filesystem::normalizePath($path); |
|
| 1442 | + $mount = $this->getMount($directory); |
|
| 1443 | + $storage = $mount->getStorage(); |
|
| 1444 | + $internalPath = $mount->getInternalPath($path); |
|
| 1445 | + if (!$storage) { |
|
| 1446 | + return []; |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + $cache = $storage->getCache($internalPath); |
|
| 1450 | + $user = \OC_User::getUser(); |
|
| 1451 | + |
|
| 1452 | + if (!$directoryInfo) { |
|
| 1453 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1454 | + if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
| 1455 | + return []; |
|
| 1456 | + } |
|
| 1457 | + } else { |
|
| 1458 | + $data = $directoryInfo; |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1462 | + return []; |
|
| 1463 | + } |
|
| 1464 | + |
|
| 1465 | + $folderId = $data->getId(); |
|
| 1466 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1467 | + |
|
| 1468 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1469 | + |
|
| 1470 | + $fileNames = array_map(function (ICacheEntry $content) { |
|
| 1471 | + return $content->getName(); |
|
| 1472 | + }, $contents); |
|
| 1473 | + /** |
|
| 1474 | + * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1475 | + */ |
|
| 1476 | + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1477 | + if ($sharingDisabled) { |
|
| 1478 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1479 | + } |
|
| 1480 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1481 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1482 | + }, $contents); |
|
| 1483 | + $files = array_combine($fileNames, $fileInfos); |
|
| 1484 | + |
|
| 1485 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1486 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1487 | + $dirLength = strlen($path); |
|
| 1488 | + foreach ($mounts as $mount) { |
|
| 1489 | + $mountPoint = $mount->getMountPoint(); |
|
| 1490 | + $subStorage = $mount->getStorage(); |
|
| 1491 | + if ($subStorage) { |
|
| 1492 | + $subCache = $subStorage->getCache(''); |
|
| 1493 | + |
|
| 1494 | + $rootEntry = $subCache->get(''); |
|
| 1495 | + if (!$rootEntry) { |
|
| 1496 | + $subScanner = $subStorage->getScanner(); |
|
| 1497 | + try { |
|
| 1498 | + $subScanner->scanFile(''); |
|
| 1499 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1500 | + continue; |
|
| 1501 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1502 | + continue; |
|
| 1503 | + } catch (\Exception $e) { |
|
| 1504 | + // sometimes when the storage is not available it can be any exception |
|
| 1505 | + \OC::$server->getLogger()->logException($e, [ |
|
| 1506 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1507 | + 'level' => ILogger::ERROR, |
|
| 1508 | + 'app' => 'lib', |
|
| 1509 | + ]); |
|
| 1510 | + continue; |
|
| 1511 | + } |
|
| 1512 | + $rootEntry = $subCache->get(''); |
|
| 1513 | + } |
|
| 1514 | + |
|
| 1515 | + if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1516 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1517 | + if ($pos = strpos($relativePath, '/')) { |
|
| 1518 | + //mountpoint inside subfolder add size to the correct folder |
|
| 1519 | + $entryName = substr($relativePath, 0, $pos); |
|
| 1520 | + foreach ($files as &$entry) { |
|
| 1521 | + if ($entry->getName() === $entryName) { |
|
| 1522 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1523 | + } |
|
| 1524 | + } |
|
| 1525 | + } else { //mountpoint in this folder, add an entry for it |
|
| 1526 | + $rootEntry['name'] = $relativePath; |
|
| 1527 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1528 | + $permissions = $rootEntry['permissions']; |
|
| 1529 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1530 | + // for shared files/folders we use the permissions given by the owner |
|
| 1531 | + if ($mount instanceof MoveableMount) { |
|
| 1532 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1533 | + } else { |
|
| 1534 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1535 | + } |
|
| 1536 | + |
|
| 1537 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1538 | + |
|
| 1539 | + // if sharing was disabled for the user we remove the share permissions |
|
| 1540 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1541 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1545 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1546 | + } |
|
| 1547 | + } |
|
| 1548 | + } |
|
| 1549 | + } |
|
| 1550 | + |
|
| 1551 | + if ($mimetype_filter) { |
|
| 1552 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1553 | + if (strpos($mimetype_filter, '/')) { |
|
| 1554 | + return $file->getMimetype() === $mimetype_filter; |
|
| 1555 | + } else { |
|
| 1556 | + return $file->getMimePart() === $mimetype_filter; |
|
| 1557 | + } |
|
| 1558 | + }); |
|
| 1559 | + } |
|
| 1560 | + |
|
| 1561 | + return array_values($files); |
|
| 1562 | + } |
|
| 1563 | + |
|
| 1564 | + /** |
|
| 1565 | + * change file metadata |
|
| 1566 | + * |
|
| 1567 | + * @param string $path |
|
| 1568 | + * @param array|\OCP\Files\FileInfo $data |
|
| 1569 | + * @return int |
|
| 1570 | + * |
|
| 1571 | + * returns the fileid of the updated file |
|
| 1572 | + */ |
|
| 1573 | + public function putFileInfo($path, $data) { |
|
| 1574 | + $this->assertPathLength($path); |
|
| 1575 | + if ($data instanceof FileInfo) { |
|
| 1576 | + $data = $data->getData(); |
|
| 1577 | + } |
|
| 1578 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1579 | + /** |
|
| 1580 | + * @var \OC\Files\Storage\Storage $storage |
|
| 1581 | + * @var string $internalPath |
|
| 1582 | + */ |
|
| 1583 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 1584 | + if ($storage) { |
|
| 1585 | + $cache = $storage->getCache($path); |
|
| 1586 | + |
|
| 1587 | + if (!$cache->inCache($internalPath)) { |
|
| 1588 | + $scanner = $storage->getScanner($internalPath); |
|
| 1589 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1590 | + } |
|
| 1591 | + |
|
| 1592 | + return $cache->put($internalPath, $data); |
|
| 1593 | + } else { |
|
| 1594 | + return -1; |
|
| 1595 | + } |
|
| 1596 | + } |
|
| 1597 | + |
|
| 1598 | + /** |
|
| 1599 | + * search for files with the name matching $query |
|
| 1600 | + * |
|
| 1601 | + * @param string $query |
|
| 1602 | + * @return FileInfo[] |
|
| 1603 | + */ |
|
| 1604 | + public function search($query) { |
|
| 1605 | + return $this->searchCommon('search', ['%' . $query . '%']); |
|
| 1606 | + } |
|
| 1607 | + |
|
| 1608 | + /** |
|
| 1609 | + * search for files with the name matching $query |
|
| 1610 | + * |
|
| 1611 | + * @param string $query |
|
| 1612 | + * @return FileInfo[] |
|
| 1613 | + */ |
|
| 1614 | + public function searchRaw($query) { |
|
| 1615 | + return $this->searchCommon('search', [$query]); |
|
| 1616 | + } |
|
| 1617 | + |
|
| 1618 | + /** |
|
| 1619 | + * search for files by mimetype |
|
| 1620 | + * |
|
| 1621 | + * @param string $mimetype |
|
| 1622 | + * @return FileInfo[] |
|
| 1623 | + */ |
|
| 1624 | + public function searchByMime($mimetype) { |
|
| 1625 | + return $this->searchCommon('searchByMime', [$mimetype]); |
|
| 1626 | + } |
|
| 1627 | + |
|
| 1628 | + /** |
|
| 1629 | + * search for files by tag |
|
| 1630 | + * |
|
| 1631 | + * @param string|int $tag name or tag id |
|
| 1632 | + * @param string $userId owner of the tags |
|
| 1633 | + * @return FileInfo[] |
|
| 1634 | + */ |
|
| 1635 | + public function searchByTag($tag, $userId) { |
|
| 1636 | + return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
| 1637 | + } |
|
| 1638 | + |
|
| 1639 | + /** |
|
| 1640 | + * @param string $method cache method |
|
| 1641 | + * @param array $args |
|
| 1642 | + * @return FileInfo[] |
|
| 1643 | + */ |
|
| 1644 | + private function searchCommon($method, $args) { |
|
| 1645 | + $files = []; |
|
| 1646 | + $rootLength = strlen($this->fakeRoot); |
|
| 1647 | + |
|
| 1648 | + $mount = $this->getMount(''); |
|
| 1649 | + $mountPoint = $mount->getMountPoint(); |
|
| 1650 | + $storage = $mount->getStorage(); |
|
| 1651 | + $userManager = \OC::$server->getUserManager(); |
|
| 1652 | + if ($storage) { |
|
| 1653 | + $cache = $storage->getCache(''); |
|
| 1654 | + |
|
| 1655 | + $results = call_user_func_array([$cache, $method], $args); |
|
| 1656 | + foreach ($results as $result) { |
|
| 1657 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1658 | + $internalPath = $result['path']; |
|
| 1659 | + $path = $mountPoint . $result['path']; |
|
| 1660 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1661 | + $owner = $userManager->get($storage->getOwner($internalPath)); |
|
| 1662 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1663 | + } |
|
| 1664 | + } |
|
| 1665 | + |
|
| 1666 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1667 | + foreach ($mounts as $mount) { |
|
| 1668 | + $mountPoint = $mount->getMountPoint(); |
|
| 1669 | + $storage = $mount->getStorage(); |
|
| 1670 | + if ($storage) { |
|
| 1671 | + $cache = $storage->getCache(''); |
|
| 1672 | + |
|
| 1673 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1674 | + $results = call_user_func_array([$cache, $method], $args); |
|
| 1675 | + if ($results) { |
|
| 1676 | + foreach ($results as $result) { |
|
| 1677 | + $internalPath = $result['path']; |
|
| 1678 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1679 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1680 | + $owner = $userManager->get($storage->getOwner($internalPath)); |
|
| 1681 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1682 | + } |
|
| 1683 | + } |
|
| 1684 | + } |
|
| 1685 | + } |
|
| 1686 | + } |
|
| 1687 | + return $files; |
|
| 1688 | + } |
|
| 1689 | + |
|
| 1690 | + /** |
|
| 1691 | + * Get the owner for a file or folder |
|
| 1692 | + * |
|
| 1693 | + * @param string $path |
|
| 1694 | + * @return string the user id of the owner |
|
| 1695 | + * @throws NotFoundException |
|
| 1696 | + */ |
|
| 1697 | + public function getOwner($path) { |
|
| 1698 | + $info = $this->getFileInfo($path); |
|
| 1699 | + if (!$info) { |
|
| 1700 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1701 | + } |
|
| 1702 | + |
|
| 1703 | + if ($info->getOwner() === null) { |
|
| 1704 | + throw new NotFoundException($path . ' has no owner'); |
|
| 1705 | + } |
|
| 1706 | + |
|
| 1707 | + return $info->getOwner()->getUID(); |
|
| 1708 | + } |
|
| 1709 | + |
|
| 1710 | + /** |
|
| 1711 | + * get the ETag for a file or folder |
|
| 1712 | + * |
|
| 1713 | + * @param string $path |
|
| 1714 | + * @return string |
|
| 1715 | + */ |
|
| 1716 | + public function getETag($path) { |
|
| 1717 | + /** |
|
| 1718 | + * @var Storage\Storage $storage |
|
| 1719 | + * @var string $internalPath |
|
| 1720 | + */ |
|
| 1721 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1722 | + if ($storage) { |
|
| 1723 | + return $storage->getETag($internalPath); |
|
| 1724 | + } else { |
|
| 1725 | + return null; |
|
| 1726 | + } |
|
| 1727 | + } |
|
| 1728 | + |
|
| 1729 | + /** |
|
| 1730 | + * Get the path of a file by id, relative to the view |
|
| 1731 | + * |
|
| 1732 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1733 | + * |
|
| 1734 | + * @param int $id |
|
| 1735 | + * @param int|null $storageId |
|
| 1736 | + * @return string |
|
| 1737 | + * @throws NotFoundException |
|
| 1738 | + */ |
|
| 1739 | + public function getPath($id, int $storageId = null) { |
|
| 1740 | + $id = (int)$id; |
|
| 1741 | + $manager = Filesystem::getMountManager(); |
|
| 1742 | + $mounts = $manager->findIn($this->fakeRoot); |
|
| 1743 | + $mounts[] = $manager->find($this->fakeRoot); |
|
| 1744 | + $mounts = array_filter($mounts); |
|
| 1745 | + // reverse the array, so we start with the storage this view is in |
|
| 1746 | + // which is the most likely to contain the file we're looking for |
|
| 1747 | + $mounts = array_reverse($mounts); |
|
| 1748 | + |
|
| 1749 | + // put non-shared mounts in front of the shared mount |
|
| 1750 | + // this prevents unneeded recursion into shares |
|
| 1751 | + usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
| 1752 | + return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1; |
|
| 1753 | + }); |
|
| 1754 | + |
|
| 1755 | + if (!is_null($storageId)) { |
|
| 1756 | + $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) { |
|
| 1757 | + return $mount->getNumericStorageId() === $storageId; |
|
| 1758 | + }); |
|
| 1759 | + } |
|
| 1760 | + |
|
| 1761 | + foreach ($mounts as $mount) { |
|
| 1762 | + /** |
|
| 1763 | + * @var \OC\Files\Mount\MountPoint $mount |
|
| 1764 | + */ |
|
| 1765 | + if ($mount->getStorage()) { |
|
| 1766 | + $cache = $mount->getStorage()->getCache(); |
|
| 1767 | + $internalPath = $cache->getPathById($id); |
|
| 1768 | + if (is_string($internalPath)) { |
|
| 1769 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1770 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1771 | + return $path; |
|
| 1772 | + } |
|
| 1773 | + } |
|
| 1774 | + } |
|
| 1775 | + } |
|
| 1776 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1777 | + } |
|
| 1778 | + |
|
| 1779 | + /** |
|
| 1780 | + * @param string $path |
|
| 1781 | + * @throws InvalidPathException |
|
| 1782 | + */ |
|
| 1783 | + private function assertPathLength($path) { |
|
| 1784 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1785 | + // Check for the string length - performed using isset() instead of strlen() |
|
| 1786 | + // because isset() is about 5x-40x faster. |
|
| 1787 | + if (isset($path[$maxLen])) { |
|
| 1788 | + $pathLen = strlen($path); |
|
| 1789 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1790 | + } |
|
| 1791 | + } |
|
| 1792 | + |
|
| 1793 | + /** |
|
| 1794 | + * check if it is allowed to move a mount point to a given target. |
|
| 1795 | + * It is not allowed to move a mount point into a different mount point or |
|
| 1796 | + * into an already shared folder |
|
| 1797 | + * |
|
| 1798 | + * @param IStorage $targetStorage |
|
| 1799 | + * @param string $targetInternalPath |
|
| 1800 | + * @return boolean |
|
| 1801 | + */ |
|
| 1802 | + private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) { |
|
| 1803 | + |
|
| 1804 | + // note: cannot use the view because the target is already locked |
|
| 1805 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1806 | + if ($fileId === -1) { |
|
| 1807 | + // target might not exist, need to check parent instead |
|
| 1808 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1809 | + } |
|
| 1810 | + |
|
| 1811 | + // check if any of the parents were shared by the current owner (include collections) |
|
| 1812 | + $shares = \OCP\Share::getItemShared( |
|
| 1813 | + 'folder', |
|
| 1814 | + $fileId, |
|
| 1815 | + \OCP\Share::FORMAT_NONE, |
|
| 1816 | + null, |
|
| 1817 | + true |
|
| 1818 | + ); |
|
| 1819 | + |
|
| 1820 | + if (count($shares) > 0) { |
|
| 1821 | + \OCP\Util::writeLog('files', |
|
| 1822 | + 'It is not allowed to move one mount point into a shared folder', |
|
| 1823 | + ILogger::DEBUG); |
|
| 1824 | + return false; |
|
| 1825 | + } |
|
| 1826 | + |
|
| 1827 | + return true; |
|
| 1828 | + } |
|
| 1829 | + |
|
| 1830 | + /** |
|
| 1831 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1832 | + * |
|
| 1833 | + * @param string $path |
|
| 1834 | + * @return \OCP\Files\FileInfo |
|
| 1835 | + */ |
|
| 1836 | + private function getPartFileInfo($path) { |
|
| 1837 | + $mount = $this->getMount($path); |
|
| 1838 | + $storage = $mount->getStorage(); |
|
| 1839 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1840 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1841 | + return new FileInfo( |
|
| 1842 | + $this->getAbsolutePath($path), |
|
| 1843 | + $storage, |
|
| 1844 | + $internalPath, |
|
| 1845 | + [ |
|
| 1846 | + 'fileid' => null, |
|
| 1847 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1848 | + 'name' => basename($path), |
|
| 1849 | + 'etag' => null, |
|
| 1850 | + 'size' => $storage->filesize($internalPath), |
|
| 1851 | + 'mtime' => $storage->filemtime($internalPath), |
|
| 1852 | + 'encrypted' => false, |
|
| 1853 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1854 | + ], |
|
| 1855 | + $mount, |
|
| 1856 | + $owner |
|
| 1857 | + ); |
|
| 1858 | + } |
|
| 1859 | + |
|
| 1860 | + /** |
|
| 1861 | + * @param string $path |
|
| 1862 | + * @param string $fileName |
|
| 1863 | + * @throws InvalidPathException |
|
| 1864 | + */ |
|
| 1865 | + public function verifyPath($path, $fileName) { |
|
| 1866 | + try { |
|
| 1867 | + /** @type \OCP\Files\Storage $storage */ |
|
| 1868 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1869 | + $storage->verifyPath($internalPath, $fileName); |
|
| 1870 | + } catch (ReservedWordException $ex) { |
|
| 1871 | + $l = \OC::$server->getL10N('lib'); |
|
| 1872 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1873 | + } catch (InvalidCharacterInPathException $ex) { |
|
| 1874 | + $l = \OC::$server->getL10N('lib'); |
|
| 1875 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1876 | + } catch (FileNameTooLongException $ex) { |
|
| 1877 | + $l = \OC::$server->getL10N('lib'); |
|
| 1878 | + throw new InvalidPathException($l->t('File name is too long')); |
|
| 1879 | + } catch (InvalidDirectoryException $ex) { |
|
| 1880 | + $l = \OC::$server->getL10N('lib'); |
|
| 1881 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1882 | + } catch (EmptyFileNameException $ex) { |
|
| 1883 | + $l = \OC::$server->getL10N('lib'); |
|
| 1884 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1885 | + } |
|
| 1886 | + } |
|
| 1887 | + |
|
| 1888 | + /** |
|
| 1889 | + * get all parent folders of $path |
|
| 1890 | + * |
|
| 1891 | + * @param string $path |
|
| 1892 | + * @return string[] |
|
| 1893 | + */ |
|
| 1894 | + private function getParents($path) { |
|
| 1895 | + $path = trim($path, '/'); |
|
| 1896 | + if (!$path) { |
|
| 1897 | + return []; |
|
| 1898 | + } |
|
| 1899 | + |
|
| 1900 | + $parts = explode('/', $path); |
|
| 1901 | + |
|
| 1902 | + // remove the single file |
|
| 1903 | + array_pop($parts); |
|
| 1904 | + $result = ['/']; |
|
| 1905 | + $resultPath = ''; |
|
| 1906 | + foreach ($parts as $part) { |
|
| 1907 | + if ($part) { |
|
| 1908 | + $resultPath .= '/' . $part; |
|
| 1909 | + $result[] = $resultPath; |
|
| 1910 | + } |
|
| 1911 | + } |
|
| 1912 | + return $result; |
|
| 1913 | + } |
|
| 1914 | + |
|
| 1915 | + /** |
|
| 1916 | + * Returns the mount point for which to lock |
|
| 1917 | + * |
|
| 1918 | + * @param string $absolutePath absolute path |
|
| 1919 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1920 | + * is mounted directly on the given path, false otherwise |
|
| 1921 | + * @return IMountPoint mount point for which to apply locks |
|
| 1922 | + */ |
|
| 1923 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1924 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1925 | + |
|
| 1926 | + if ($useParentMount) { |
|
| 1927 | + // find out if something is mounted directly on the path |
|
| 1928 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1929 | + if ($internalPath === '') { |
|
| 1930 | + // resolve the parent mount instead |
|
| 1931 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1932 | + } |
|
| 1933 | + } |
|
| 1934 | + |
|
| 1935 | + return $mount; |
|
| 1936 | + } |
|
| 1937 | + |
|
| 1938 | + /** |
|
| 1939 | + * Lock the given path |
|
| 1940 | + * |
|
| 1941 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1942 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1943 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1944 | + * |
|
| 1945 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1946 | + * @throws LockedException if the path is already locked |
|
| 1947 | + */ |
|
| 1948 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1949 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1950 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1951 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1952 | + return false; |
|
| 1953 | + } |
|
| 1954 | + |
|
| 1955 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1956 | + if ($mount) { |
|
| 1957 | + try { |
|
| 1958 | + $storage = $mount->getStorage(); |
|
| 1959 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1960 | + $storage->acquireLock( |
|
| 1961 | + $mount->getInternalPath($absolutePath), |
|
| 1962 | + $type, |
|
| 1963 | + $this->lockingProvider |
|
| 1964 | + ); |
|
| 1965 | + } |
|
| 1966 | + } catch (LockedException $e) { |
|
| 1967 | + // rethrow with the a human-readable path |
|
| 1968 | + throw new LockedException( |
|
| 1969 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1970 | + $e, |
|
| 1971 | + $e->getExistingLock() |
|
| 1972 | + ); |
|
| 1973 | + } |
|
| 1974 | + } |
|
| 1975 | + |
|
| 1976 | + return true; |
|
| 1977 | + } |
|
| 1978 | + |
|
| 1979 | + /** |
|
| 1980 | + * Change the lock type |
|
| 1981 | + * |
|
| 1982 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1983 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1984 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1985 | + * |
|
| 1986 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1987 | + * @throws LockedException if the path is already locked |
|
| 1988 | + */ |
|
| 1989 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1990 | + $path = Filesystem::normalizePath($path); |
|
| 1991 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1992 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1993 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1994 | + return false; |
|
| 1995 | + } |
|
| 1996 | + |
|
| 1997 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1998 | + if ($mount) { |
|
| 1999 | + try { |
|
| 2000 | + $storage = $mount->getStorage(); |
|
| 2001 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2002 | + $storage->changeLock( |
|
| 2003 | + $mount->getInternalPath($absolutePath), |
|
| 2004 | + $type, |
|
| 2005 | + $this->lockingProvider |
|
| 2006 | + ); |
|
| 2007 | + } |
|
| 2008 | + } catch (LockedException $e) { |
|
| 2009 | + try { |
|
| 2010 | + // rethrow with the a human-readable path |
|
| 2011 | + throw new LockedException( |
|
| 2012 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 2013 | + $e, |
|
| 2014 | + $e->getExistingLock() |
|
| 2015 | + ); |
|
| 2016 | + } catch (\InvalidArgumentException $ex) { |
|
| 2017 | + throw new LockedException( |
|
| 2018 | + $absolutePath, |
|
| 2019 | + $ex, |
|
| 2020 | + $e->getExistingLock() |
|
| 2021 | + ); |
|
| 2022 | + } |
|
| 2023 | + } |
|
| 2024 | + } |
|
| 2025 | + |
|
| 2026 | + return true; |
|
| 2027 | + } |
|
| 2028 | + |
|
| 2029 | + /** |
|
| 2030 | + * Unlock the given path |
|
| 2031 | + * |
|
| 2032 | + * @param string $path the path of the file to unlock, relative to the view |
|
| 2033 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2034 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2035 | + * |
|
| 2036 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2037 | + * @throws LockedException |
|
| 2038 | + */ |
|
| 2039 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2040 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2041 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2042 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2043 | + return false; |
|
| 2044 | + } |
|
| 2045 | + |
|
| 2046 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2047 | + if ($mount) { |
|
| 2048 | + $storage = $mount->getStorage(); |
|
| 2049 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2050 | + $storage->releaseLock( |
|
| 2051 | + $mount->getInternalPath($absolutePath), |
|
| 2052 | + $type, |
|
| 2053 | + $this->lockingProvider |
|
| 2054 | + ); |
|
| 2055 | + } |
|
| 2056 | + } |
|
| 2057 | + |
|
| 2058 | + return true; |
|
| 2059 | + } |
|
| 2060 | + |
|
| 2061 | + /** |
|
| 2062 | + * Lock a path and all its parents up to the root of the view |
|
| 2063 | + * |
|
| 2064 | + * @param string $path the path of the file to lock relative to the view |
|
| 2065 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2066 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2067 | + * |
|
| 2068 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2069 | + * @throws LockedException |
|
| 2070 | + */ |
|
| 2071 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2072 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2073 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2074 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2075 | + return false; |
|
| 2076 | + } |
|
| 2077 | + |
|
| 2078 | + $this->lockPath($path, $type, $lockMountPoint); |
|
| 2079 | + |
|
| 2080 | + $parents = $this->getParents($path); |
|
| 2081 | + foreach ($parents as $parent) { |
|
| 2082 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2083 | + } |
|
| 2084 | + |
|
| 2085 | + return true; |
|
| 2086 | + } |
|
| 2087 | + |
|
| 2088 | + /** |
|
| 2089 | + * Unlock a path and all its parents up to the root of the view |
|
| 2090 | + * |
|
| 2091 | + * @param string $path the path of the file to lock relative to the view |
|
| 2092 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2093 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2094 | + * |
|
| 2095 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2096 | + * @throws LockedException |
|
| 2097 | + */ |
|
| 2098 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2099 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2100 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2101 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2102 | + return false; |
|
| 2103 | + } |
|
| 2104 | + |
|
| 2105 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2106 | + |
|
| 2107 | + $parents = $this->getParents($path); |
|
| 2108 | + foreach ($parents as $parent) { |
|
| 2109 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2110 | + } |
|
| 2111 | + |
|
| 2112 | + return true; |
|
| 2113 | + } |
|
| 2114 | + |
|
| 2115 | + /** |
|
| 2116 | + * Only lock files in data/user/files/ |
|
| 2117 | + * |
|
| 2118 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2119 | + * @return bool |
|
| 2120 | + */ |
|
| 2121 | + protected function shouldLockFile($path) { |
|
| 2122 | + $path = Filesystem::normalizePath($path); |
|
| 2123 | + |
|
| 2124 | + $pathSegments = explode('/', $path); |
|
| 2125 | + if (isset($pathSegments[2])) { |
|
| 2126 | + // E.g.: /username/files/path-to-file |
|
| 2127 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2128 | + } |
|
| 2129 | + |
|
| 2130 | + return strpos($path, '/appdata_') !== 0; |
|
| 2131 | + } |
|
| 2132 | + |
|
| 2133 | + /** |
|
| 2134 | + * Shortens the given absolute path to be relative to |
|
| 2135 | + * "$user/files". |
|
| 2136 | + * |
|
| 2137 | + * @param string $absolutePath absolute path which is under "files" |
|
| 2138 | + * |
|
| 2139 | + * @return string path relative to "files" with trimmed slashes or null |
|
| 2140 | + * if the path was NOT relative to files |
|
| 2141 | + * |
|
| 2142 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2143 | + * @since 8.1.0 |
|
| 2144 | + */ |
|
| 2145 | + public function getPathRelativeToFiles($absolutePath) { |
|
| 2146 | + $path = Filesystem::normalizePath($absolutePath); |
|
| 2147 | + $parts = explode('/', trim($path, '/'), 3); |
|
| 2148 | + // "$user", "files", "path/to/dir" |
|
| 2149 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2150 | + $this->logger->error( |
|
| 2151 | + '$absolutePath must be relative to "files", value is "%s"', |
|
| 2152 | + [ |
|
| 2153 | + $absolutePath |
|
| 2154 | + ] |
|
| 2155 | + ); |
|
| 2156 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2157 | + } |
|
| 2158 | + if (isset($parts[2])) { |
|
| 2159 | + return $parts[2]; |
|
| 2160 | + } |
|
| 2161 | + return ''; |
|
| 2162 | + } |
|
| 2163 | + |
|
| 2164 | + /** |
|
| 2165 | + * @param string $filename |
|
| 2166 | + * @return array |
|
| 2167 | + * @throws \OC\User\NoUserException |
|
| 2168 | + * @throws NotFoundException |
|
| 2169 | + */ |
|
| 2170 | + public function getUidAndFilename($filename) { |
|
| 2171 | + $info = $this->getFileInfo($filename); |
|
| 2172 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2173 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2174 | + } |
|
| 2175 | + $uid = $info->getOwner()->getUID(); |
|
| 2176 | + if ($uid != \OC_User::getUser()) { |
|
| 2177 | + Filesystem::initMountPoints($uid); |
|
| 2178 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 2179 | + try { |
|
| 2180 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 2181 | + } catch (NotFoundException $e) { |
|
| 2182 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2183 | + } |
|
| 2184 | + } |
|
| 2185 | + return [$uid, $filename]; |
|
| 2186 | + } |
|
| 2187 | + |
|
| 2188 | + /** |
|
| 2189 | + * Creates parent non-existing folders |
|
| 2190 | + * |
|
| 2191 | + * @param string $filePath |
|
| 2192 | + * @return bool |
|
| 2193 | + */ |
|
| 2194 | + private function createParentDirectories($filePath) { |
|
| 2195 | + $directoryParts = explode('/', $filePath); |
|
| 2196 | + $directoryParts = array_filter($directoryParts); |
|
| 2197 | + foreach ($directoryParts as $key => $part) { |
|
| 2198 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2199 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2200 | + if ($this->is_file($currentPath)) { |
|
| 2201 | + return false; |
|
| 2202 | + } |
|
| 2203 | + if (!$this->file_exists($currentPath)) { |
|
| 2204 | + $this->mkdir($currentPath); |
|
| 2205 | + } |
|
| 2206 | + } |
|
| 2207 | + |
|
| 2208 | + return true; |
|
| 2209 | + } |
|
| 2210 | 2210 | } |