@@ -51,484 +51,484 @@ |
||
| 51 | 51 | * @package OC\Files\Node |
| 52 | 52 | */ |
| 53 | 53 | class Root extends Folder implements IRootFolder { |
| 54 | - private Manager $mountManager; |
|
| 55 | - private PublicEmitter $emitter; |
|
| 56 | - private ?IUser $user; |
|
| 57 | - private CappedMemoryCache $userFolderCache; |
|
| 58 | - private IUserMountCache $userMountCache; |
|
| 59 | - private LoggerInterface $logger; |
|
| 60 | - private IUserManager $userManager; |
|
| 61 | - private IEventDispatcher $eventDispatcher; |
|
| 62 | - private ICache $pathByIdCache; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param Manager $manager |
|
| 66 | - * @param View $view |
|
| 67 | - * @param IUser|null $user |
|
| 68 | - */ |
|
| 69 | - public function __construct( |
|
| 70 | - $manager, |
|
| 71 | - $view, |
|
| 72 | - $user, |
|
| 73 | - IUserMountCache $userMountCache, |
|
| 74 | - LoggerInterface $logger, |
|
| 75 | - IUserManager $userManager, |
|
| 76 | - IEventDispatcher $eventDispatcher, |
|
| 77 | - ICacheFactory $cacheFactory, |
|
| 78 | - ) { |
|
| 79 | - parent::__construct($this, $view, ''); |
|
| 80 | - $this->mountManager = $manager; |
|
| 81 | - $this->user = $user; |
|
| 82 | - $this->emitter = new PublicEmitter(); |
|
| 83 | - $this->userFolderCache = new CappedMemoryCache(); |
|
| 84 | - $this->userMountCache = $userMountCache; |
|
| 85 | - $this->logger = $logger; |
|
| 86 | - $this->userManager = $userManager; |
|
| 87 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
| 88 | - $this->userFolderCache = new CappedMemoryCache(); |
|
| 89 | - }); |
|
| 90 | - $this->pathByIdCache = $cacheFactory->createLocal('path-by-id'); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the user for which the filesystem is setup |
|
| 95 | - * |
|
| 96 | - * @return \OC\User\User |
|
| 97 | - */ |
|
| 98 | - public function getUser() { |
|
| 99 | - return $this->user; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $scope |
|
| 104 | - * @param string $method |
|
| 105 | - * @param callable $callback |
|
| 106 | - */ |
|
| 107 | - public function listen($scope, $method, callable $callback) { |
|
| 108 | - $this->emitter->listen($scope, $method, $callback); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $scope optional |
|
| 113 | - * @param string $method optional |
|
| 114 | - * @param callable $callback optional |
|
| 115 | - */ |
|
| 116 | - public function removeListener($scope = null, $method = null, ?callable $callback = null) { |
|
| 117 | - $this->emitter->removeListener($scope, $method, $callback); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param string $scope |
|
| 122 | - * @param string $method |
|
| 123 | - * @param Node[] $arguments |
|
| 124 | - */ |
|
| 125 | - public function emit($scope, $method, $arguments = []) { |
|
| 126 | - $this->emitter->emit($scope, $method, $arguments); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param \OC\Files\Storage\Storage $storage |
|
| 131 | - * @param string $mountPoint |
|
| 132 | - * @param array $arguments |
|
| 133 | - */ |
|
| 134 | - public function mount($storage, $mountPoint, $arguments = []) { |
|
| 135 | - $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
| 136 | - $this->mountManager->addMount($mount); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - public function getMount(string $mountPoint): IMountPoint { |
|
| 140 | - return $this->mountManager->find($mountPoint); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param string $mountPoint |
|
| 145 | - * @return \OC\Files\Mount\MountPoint[] |
|
| 146 | - */ |
|
| 147 | - public function getMountsIn(string $mountPoint): array { |
|
| 148 | - return $this->mountManager->findIn($mountPoint); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $storageId |
|
| 153 | - * @return \OC\Files\Mount\MountPoint[] |
|
| 154 | - */ |
|
| 155 | - public function getMountByStorageId($storageId) { |
|
| 156 | - return $this->mountManager->findByStorageId($storageId); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param int $numericId |
|
| 161 | - * @return MountPoint[] |
|
| 162 | - */ |
|
| 163 | - public function getMountByNumericStorageId($numericId) { |
|
| 164 | - return $this->mountManager->findByNumericId($numericId); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param \OC\Files\Mount\MountPoint $mount |
|
| 169 | - */ |
|
| 170 | - public function unMount($mount) { |
|
| 171 | - $this->mountManager->remove($mount); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - public function get($path) { |
|
| 175 | - $path = $this->normalizePath($path); |
|
| 176 | - if ($this->isValidPath($path)) { |
|
| 177 | - $fullPath = $this->getFullPath($path); |
|
| 178 | - $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
| 179 | - if ($fileInfo) { |
|
| 180 | - return $this->createNode($fullPath, $fileInfo, false); |
|
| 181 | - } else { |
|
| 182 | - throw new NotFoundException($path); |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - throw new NotPermittedException(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - //most operations can't be done on the root |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @param string $targetPath |
|
| 193 | - * @return Node |
|
| 194 | - * @throws \OCP\Files\NotPermittedException |
|
| 195 | - */ |
|
| 196 | - public function rename($targetPath) { |
|
| 197 | - throw new NotPermittedException(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - public function delete() { |
|
| 201 | - throw new NotPermittedException(); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @param string $targetPath |
|
| 206 | - * @return Node |
|
| 207 | - * @throws \OCP\Files\NotPermittedException |
|
| 208 | - */ |
|
| 209 | - public function copy($targetPath) { |
|
| 210 | - throw new NotPermittedException(); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @param int $mtime |
|
| 215 | - * @throws \OCP\Files\NotPermittedException |
|
| 216 | - */ |
|
| 217 | - public function touch($mtime = null) { |
|
| 218 | - throw new NotPermittedException(); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @return \OC\Files\Storage\Storage |
|
| 223 | - * @throws \OCP\Files\NotFoundException |
|
| 224 | - */ |
|
| 225 | - public function getStorage() { |
|
| 226 | - throw new NotFoundException(); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @return string |
|
| 231 | - */ |
|
| 232 | - public function getPath() { |
|
| 233 | - return '/'; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - public function getInternalPath() { |
|
| 240 | - return ''; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @return int |
|
| 245 | - */ |
|
| 246 | - public function getId() { |
|
| 247 | - return 0; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - public function stat() { |
|
| 254 | - return []; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @return int |
|
| 259 | - */ |
|
| 260 | - public function getMTime() { |
|
| 261 | - return 0; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param bool $includeMounts |
|
| 266 | - * @return int|float |
|
| 267 | - */ |
|
| 268 | - public function getSize($includeMounts = true): int|float { |
|
| 269 | - return 0; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - public function getEtag() { |
|
| 276 | - return ''; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @return int |
|
| 281 | - */ |
|
| 282 | - public function getPermissions() { |
|
| 283 | - return \OCP\Constants::PERMISSION_CREATE; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @return bool |
|
| 288 | - */ |
|
| 289 | - public function isReadable() { |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @return bool |
|
| 295 | - */ |
|
| 296 | - public function isUpdateable() { |
|
| 297 | - return false; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @return bool |
|
| 302 | - */ |
|
| 303 | - public function isDeletable() { |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * @return bool |
|
| 309 | - */ |
|
| 310 | - public function isShareable() { |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @throws \OCP\Files\NotFoundException |
|
| 316 | - */ |
|
| 317 | - public function getParent(): INode|IRootFolder { |
|
| 318 | - throw new NotFoundException(); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - public function getName() { |
|
| 325 | - return ''; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Returns a view to user's files folder |
|
| 330 | - * |
|
| 331 | - * @param string $userId user ID |
|
| 332 | - * @return \OCP\Files\Folder |
|
| 333 | - * @throws NoUserException |
|
| 334 | - * @throws NotPermittedException |
|
| 335 | - */ |
|
| 336 | - public function getUserFolder($userId) { |
|
| 337 | - $userObject = $this->userManager->get($userId); |
|
| 338 | - |
|
| 339 | - if (is_null($userObject)) { |
|
| 340 | - $e = new NoUserException('Backends provided no user object'); |
|
| 341 | - $this->logger->error( |
|
| 342 | - sprintf( |
|
| 343 | - 'Backends provided no user object for %s', |
|
| 344 | - $userId |
|
| 345 | - ), |
|
| 346 | - [ |
|
| 347 | - 'app' => 'files', |
|
| 348 | - 'exception' => $e, |
|
| 349 | - ] |
|
| 350 | - ); |
|
| 351 | - throw $e; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $userId = $userObject->getUID(); |
|
| 355 | - |
|
| 356 | - if (!$this->userFolderCache->hasKey($userId)) { |
|
| 357 | - if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
| 358 | - try { |
|
| 359 | - $folder = $this->get('/' . $userId . '/files'); |
|
| 360 | - if (!$folder instanceof \OCP\Files\Folder) { |
|
| 361 | - throw new \Exception("Account folder for \"$userId\" exists as a file"); |
|
| 362 | - } |
|
| 363 | - } catch (NotFoundException $e) { |
|
| 364 | - if (!$this->nodeExists('/' . $userId)) { |
|
| 365 | - $this->newFolder('/' . $userId); |
|
| 366 | - } |
|
| 367 | - $folder = $this->newFolder('/' . $userId . '/files'); |
|
| 368 | - } |
|
| 369 | - } else { |
|
| 370 | - $folder = new LazyUserFolder($this, $userObject, $this->mountManager); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $this->userFolderCache->set($userId, $folder); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return $this->userFolderCache->get($userId); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - public function getUserMountCache() { |
|
| 380 | - return $this->userMountCache; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - public function getFirstNodeByIdInPath(int $id, string $path): ?INode { |
|
| 384 | - // scope the cache by user, so we don't return nodes for different users |
|
| 385 | - if ($this->user) { |
|
| 386 | - $cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id); |
|
| 387 | - if ($cachedPath && str_starts_with($cachedPath, $path)) { |
|
| 388 | - // getting the node by path is significantly cheaper than finding it by id |
|
| 389 | - try { |
|
| 390 | - $node = $this->get($cachedPath); |
|
| 391 | - // by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path |
|
| 392 | - // if the cached path is invalid or a different file now we fall back to the uncached logic |
|
| 393 | - if ($node && $node->getId() === $id) { |
|
| 394 | - return $node; |
|
| 395 | - } |
|
| 396 | - } catch (NotFoundException|NotPermittedException) { |
|
| 397 | - // The file may be moved but the old path still in cache |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - $node = current($this->getByIdInPath($id, $path)); |
|
| 402 | - if (!$node) { |
|
| 403 | - return null; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - if ($this->user) { |
|
| 407 | - $this->pathByIdCache->set($this->user->getUID() . '::' . $id, $node->getPath()); |
|
| 408 | - } |
|
| 409 | - return $node; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * @param int $id |
|
| 414 | - * @return Node[] |
|
| 415 | - */ |
|
| 416 | - public function getByIdInPath(int $id, string $path): array { |
|
| 417 | - $mountCache = $this->getUserMountCache(); |
|
| 418 | - if ($path !== '' && strpos($path, '/', 1) > 0) { |
|
| 419 | - [, $user] = explode('/', $path); |
|
| 420 | - } else { |
|
| 421 | - $user = null; |
|
| 422 | - } |
|
| 423 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
| 424 | - |
|
| 425 | - // if the mount isn't in the cache yet, perform a setup first, then try again |
|
| 426 | - if (count($mountsContainingFile) === 0) { |
|
| 427 | - $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
| 428 | - $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // when a user has access through the same storage through multiple paths |
|
| 432 | - // (such as an external storage that is both mounted for a user and shared to the user) |
|
| 433 | - // the mount cache will only hold a single entry for the storage |
|
| 434 | - // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
| 435 | - // |
|
| 436 | - // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
| 437 | - |
|
| 438 | - $mountRootIds = array_map(function ($mount) { |
|
| 439 | - return $mount->getRootId(); |
|
| 440 | - }, $mountsContainingFile); |
|
| 441 | - $mountRootPaths = array_map(function ($mount) { |
|
| 442 | - return $mount->getRootInternalPath(); |
|
| 443 | - }, $mountsContainingFile); |
|
| 444 | - $mountProviders = array_unique(array_map(function ($mount) { |
|
| 445 | - return $mount->getMountProvider(); |
|
| 446 | - }, $mountsContainingFile)); |
|
| 447 | - $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
| 448 | - |
|
| 449 | - $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
| 450 | - |
|
| 451 | - $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
| 452 | - return isset($mountRoots[$mount->getStorageRootId()]); |
|
| 453 | - }); |
|
| 454 | - |
|
| 455 | - if (count($mountsContainingFile) === 0) { |
|
| 456 | - if ($user === $this->getAppDataDirectoryName()) { |
|
| 457 | - $folder = $this->get($path); |
|
| 458 | - if ($folder instanceof Folder) { |
|
| 459 | - return $folder->getByIdInRootMount($id); |
|
| 460 | - } else { |
|
| 461 | - throw new \Exception('getByIdInPath with non folder'); |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - return []; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
| 468 | - $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
| 469 | - $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
| 470 | - if (!$cacheEntry) { |
|
| 471 | - return null; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - // cache jails will hide the "true" internal path |
|
| 475 | - $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
| 476 | - $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
| 477 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 478 | - $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
| 479 | - $storage = $mount->getStorage(); |
|
| 480 | - if ($storage === null) { |
|
| 481 | - return null; |
|
| 482 | - } |
|
| 483 | - $ownerId = $storage->getOwner($pathRelativeToMount); |
|
| 484 | - if ($ownerId !== false) { |
|
| 485 | - $owner = Server::get(IUserManager::class)->get($ownerId); |
|
| 486 | - } else { |
|
| 487 | - $owner = null; |
|
| 488 | - } |
|
| 489 | - return $this->createNode($absolutePath, new FileInfo( |
|
| 490 | - $absolutePath, |
|
| 491 | - $storage, |
|
| 492 | - $cacheEntry->getPath(), |
|
| 493 | - $cacheEntry, |
|
| 494 | - $mount, |
|
| 495 | - $owner, |
|
| 496 | - )); |
|
| 497 | - }, $mountsContainingFile); |
|
| 498 | - |
|
| 499 | - $nodes = array_filter($nodes); |
|
| 500 | - |
|
| 501 | - $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
| 502 | - return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
| 503 | - }); |
|
| 504 | - usort($folders, function ($a, $b) { |
|
| 505 | - return $b->getPath() <=> $a->getPath(); |
|
| 506 | - }); |
|
| 507 | - return $folders; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode { |
|
| 511 | - $path = $cacheEntry->getPath(); |
|
| 512 | - $fullPath = $mountPoint->getMountPoint() . $path; |
|
| 513 | - // todo: LazyNode? |
|
| 514 | - $info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint); |
|
| 515 | - $parentPath = dirname($fullPath); |
|
| 516 | - $parent = new LazyFolder($this, function () use ($parentPath) { |
|
| 517 | - $parent = $this->get($parentPath); |
|
| 518 | - if ($parent instanceof \OCP\Files\Folder) { |
|
| 519 | - return $parent; |
|
| 520 | - } else { |
|
| 521 | - throw new \Exception("parent $parentPath is not a folder"); |
|
| 522 | - } |
|
| 523 | - }, [ |
|
| 524 | - 'path' => $parentPath, |
|
| 525 | - ]); |
|
| 526 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 527 | - $view = new View(''); |
|
| 528 | - if ($isDir) { |
|
| 529 | - return new Folder($this, $view, $fullPath, $info, $parent); |
|
| 530 | - } else { |
|
| 531 | - return new File($this, $view, $fullPath, $info, $parent); |
|
| 532 | - } |
|
| 533 | - } |
|
| 54 | + private Manager $mountManager; |
|
| 55 | + private PublicEmitter $emitter; |
|
| 56 | + private ?IUser $user; |
|
| 57 | + private CappedMemoryCache $userFolderCache; |
|
| 58 | + private IUserMountCache $userMountCache; |
|
| 59 | + private LoggerInterface $logger; |
|
| 60 | + private IUserManager $userManager; |
|
| 61 | + private IEventDispatcher $eventDispatcher; |
|
| 62 | + private ICache $pathByIdCache; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param Manager $manager |
|
| 66 | + * @param View $view |
|
| 67 | + * @param IUser|null $user |
|
| 68 | + */ |
|
| 69 | + public function __construct( |
|
| 70 | + $manager, |
|
| 71 | + $view, |
|
| 72 | + $user, |
|
| 73 | + IUserMountCache $userMountCache, |
|
| 74 | + LoggerInterface $logger, |
|
| 75 | + IUserManager $userManager, |
|
| 76 | + IEventDispatcher $eventDispatcher, |
|
| 77 | + ICacheFactory $cacheFactory, |
|
| 78 | + ) { |
|
| 79 | + parent::__construct($this, $view, ''); |
|
| 80 | + $this->mountManager = $manager; |
|
| 81 | + $this->user = $user; |
|
| 82 | + $this->emitter = new PublicEmitter(); |
|
| 83 | + $this->userFolderCache = new CappedMemoryCache(); |
|
| 84 | + $this->userMountCache = $userMountCache; |
|
| 85 | + $this->logger = $logger; |
|
| 86 | + $this->userManager = $userManager; |
|
| 87 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
| 88 | + $this->userFolderCache = new CappedMemoryCache(); |
|
| 89 | + }); |
|
| 90 | + $this->pathByIdCache = $cacheFactory->createLocal('path-by-id'); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the user for which the filesystem is setup |
|
| 95 | + * |
|
| 96 | + * @return \OC\User\User |
|
| 97 | + */ |
|
| 98 | + public function getUser() { |
|
| 99 | + return $this->user; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $scope |
|
| 104 | + * @param string $method |
|
| 105 | + * @param callable $callback |
|
| 106 | + */ |
|
| 107 | + public function listen($scope, $method, callable $callback) { |
|
| 108 | + $this->emitter->listen($scope, $method, $callback); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $scope optional |
|
| 113 | + * @param string $method optional |
|
| 114 | + * @param callable $callback optional |
|
| 115 | + */ |
|
| 116 | + public function removeListener($scope = null, $method = null, ?callable $callback = null) { |
|
| 117 | + $this->emitter->removeListener($scope, $method, $callback); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param string $scope |
|
| 122 | + * @param string $method |
|
| 123 | + * @param Node[] $arguments |
|
| 124 | + */ |
|
| 125 | + public function emit($scope, $method, $arguments = []) { |
|
| 126 | + $this->emitter->emit($scope, $method, $arguments); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param \OC\Files\Storage\Storage $storage |
|
| 131 | + * @param string $mountPoint |
|
| 132 | + * @param array $arguments |
|
| 133 | + */ |
|
| 134 | + public function mount($storage, $mountPoint, $arguments = []) { |
|
| 135 | + $mount = new MountPoint($storage, $mountPoint, $arguments); |
|
| 136 | + $this->mountManager->addMount($mount); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + public function getMount(string $mountPoint): IMountPoint { |
|
| 140 | + return $this->mountManager->find($mountPoint); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param string $mountPoint |
|
| 145 | + * @return \OC\Files\Mount\MountPoint[] |
|
| 146 | + */ |
|
| 147 | + public function getMountsIn(string $mountPoint): array { |
|
| 148 | + return $this->mountManager->findIn($mountPoint); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $storageId |
|
| 153 | + * @return \OC\Files\Mount\MountPoint[] |
|
| 154 | + */ |
|
| 155 | + public function getMountByStorageId($storageId) { |
|
| 156 | + return $this->mountManager->findByStorageId($storageId); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param int $numericId |
|
| 161 | + * @return MountPoint[] |
|
| 162 | + */ |
|
| 163 | + public function getMountByNumericStorageId($numericId) { |
|
| 164 | + return $this->mountManager->findByNumericId($numericId); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param \OC\Files\Mount\MountPoint $mount |
|
| 169 | + */ |
|
| 170 | + public function unMount($mount) { |
|
| 171 | + $this->mountManager->remove($mount); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + public function get($path) { |
|
| 175 | + $path = $this->normalizePath($path); |
|
| 176 | + if ($this->isValidPath($path)) { |
|
| 177 | + $fullPath = $this->getFullPath($path); |
|
| 178 | + $fileInfo = $this->view->getFileInfo($fullPath, false); |
|
| 179 | + if ($fileInfo) { |
|
| 180 | + return $this->createNode($fullPath, $fileInfo, false); |
|
| 181 | + } else { |
|
| 182 | + throw new NotFoundException($path); |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + throw new NotPermittedException(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + //most operations can't be done on the root |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @param string $targetPath |
|
| 193 | + * @return Node |
|
| 194 | + * @throws \OCP\Files\NotPermittedException |
|
| 195 | + */ |
|
| 196 | + public function rename($targetPath) { |
|
| 197 | + throw new NotPermittedException(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + public function delete() { |
|
| 201 | + throw new NotPermittedException(); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @param string $targetPath |
|
| 206 | + * @return Node |
|
| 207 | + * @throws \OCP\Files\NotPermittedException |
|
| 208 | + */ |
|
| 209 | + public function copy($targetPath) { |
|
| 210 | + throw new NotPermittedException(); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @param int $mtime |
|
| 215 | + * @throws \OCP\Files\NotPermittedException |
|
| 216 | + */ |
|
| 217 | + public function touch($mtime = null) { |
|
| 218 | + throw new NotPermittedException(); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @return \OC\Files\Storage\Storage |
|
| 223 | + * @throws \OCP\Files\NotFoundException |
|
| 224 | + */ |
|
| 225 | + public function getStorage() { |
|
| 226 | + throw new NotFoundException(); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @return string |
|
| 231 | + */ |
|
| 232 | + public function getPath() { |
|
| 233 | + return '/'; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + public function getInternalPath() { |
|
| 240 | + return ''; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @return int |
|
| 245 | + */ |
|
| 246 | + public function getId() { |
|
| 247 | + return 0; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + public function stat() { |
|
| 254 | + return []; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @return int |
|
| 259 | + */ |
|
| 260 | + public function getMTime() { |
|
| 261 | + return 0; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param bool $includeMounts |
|
| 266 | + * @return int|float |
|
| 267 | + */ |
|
| 268 | + public function getSize($includeMounts = true): int|float { |
|
| 269 | + return 0; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + public function getEtag() { |
|
| 276 | + return ''; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @return int |
|
| 281 | + */ |
|
| 282 | + public function getPermissions() { |
|
| 283 | + return \OCP\Constants::PERMISSION_CREATE; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @return bool |
|
| 288 | + */ |
|
| 289 | + public function isReadable() { |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @return bool |
|
| 295 | + */ |
|
| 296 | + public function isUpdateable() { |
|
| 297 | + return false; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @return bool |
|
| 302 | + */ |
|
| 303 | + public function isDeletable() { |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * @return bool |
|
| 309 | + */ |
|
| 310 | + public function isShareable() { |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @throws \OCP\Files\NotFoundException |
|
| 316 | + */ |
|
| 317 | + public function getParent(): INode|IRootFolder { |
|
| 318 | + throw new NotFoundException(); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + public function getName() { |
|
| 325 | + return ''; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Returns a view to user's files folder |
|
| 330 | + * |
|
| 331 | + * @param string $userId user ID |
|
| 332 | + * @return \OCP\Files\Folder |
|
| 333 | + * @throws NoUserException |
|
| 334 | + * @throws NotPermittedException |
|
| 335 | + */ |
|
| 336 | + public function getUserFolder($userId) { |
|
| 337 | + $userObject = $this->userManager->get($userId); |
|
| 338 | + |
|
| 339 | + if (is_null($userObject)) { |
|
| 340 | + $e = new NoUserException('Backends provided no user object'); |
|
| 341 | + $this->logger->error( |
|
| 342 | + sprintf( |
|
| 343 | + 'Backends provided no user object for %s', |
|
| 344 | + $userId |
|
| 345 | + ), |
|
| 346 | + [ |
|
| 347 | + 'app' => 'files', |
|
| 348 | + 'exception' => $e, |
|
| 349 | + ] |
|
| 350 | + ); |
|
| 351 | + throw $e; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $userId = $userObject->getUID(); |
|
| 355 | + |
|
| 356 | + if (!$this->userFolderCache->hasKey($userId)) { |
|
| 357 | + if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) { |
|
| 358 | + try { |
|
| 359 | + $folder = $this->get('/' . $userId . '/files'); |
|
| 360 | + if (!$folder instanceof \OCP\Files\Folder) { |
|
| 361 | + throw new \Exception("Account folder for \"$userId\" exists as a file"); |
|
| 362 | + } |
|
| 363 | + } catch (NotFoundException $e) { |
|
| 364 | + if (!$this->nodeExists('/' . $userId)) { |
|
| 365 | + $this->newFolder('/' . $userId); |
|
| 366 | + } |
|
| 367 | + $folder = $this->newFolder('/' . $userId . '/files'); |
|
| 368 | + } |
|
| 369 | + } else { |
|
| 370 | + $folder = new LazyUserFolder($this, $userObject, $this->mountManager); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $this->userFolderCache->set($userId, $folder); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return $this->userFolderCache->get($userId); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + public function getUserMountCache() { |
|
| 380 | + return $this->userMountCache; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + public function getFirstNodeByIdInPath(int $id, string $path): ?INode { |
|
| 384 | + // scope the cache by user, so we don't return nodes for different users |
|
| 385 | + if ($this->user) { |
|
| 386 | + $cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id); |
|
| 387 | + if ($cachedPath && str_starts_with($cachedPath, $path)) { |
|
| 388 | + // getting the node by path is significantly cheaper than finding it by id |
|
| 389 | + try { |
|
| 390 | + $node = $this->get($cachedPath); |
|
| 391 | + // by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path |
|
| 392 | + // if the cached path is invalid or a different file now we fall back to the uncached logic |
|
| 393 | + if ($node && $node->getId() === $id) { |
|
| 394 | + return $node; |
|
| 395 | + } |
|
| 396 | + } catch (NotFoundException|NotPermittedException) { |
|
| 397 | + // The file may be moved but the old path still in cache |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + $node = current($this->getByIdInPath($id, $path)); |
|
| 402 | + if (!$node) { |
|
| 403 | + return null; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + if ($this->user) { |
|
| 407 | + $this->pathByIdCache->set($this->user->getUID() . '::' . $id, $node->getPath()); |
|
| 408 | + } |
|
| 409 | + return $node; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * @param int $id |
|
| 414 | + * @return Node[] |
|
| 415 | + */ |
|
| 416 | + public function getByIdInPath(int $id, string $path): array { |
|
| 417 | + $mountCache = $this->getUserMountCache(); |
|
| 418 | + if ($path !== '' && strpos($path, '/', 1) > 0) { |
|
| 419 | + [, $user] = explode('/', $path); |
|
| 420 | + } else { |
|
| 421 | + $user = null; |
|
| 422 | + } |
|
| 423 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
| 424 | + |
|
| 425 | + // if the mount isn't in the cache yet, perform a setup first, then try again |
|
| 426 | + if (count($mountsContainingFile) === 0) { |
|
| 427 | + $this->mountManager->getSetupManager()->setupForPath($path, true); |
|
| 428 | + $mountsContainingFile = $mountCache->getMountsForFileId($id, $user); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // when a user has access through the same storage through multiple paths |
|
| 432 | + // (such as an external storage that is both mounted for a user and shared to the user) |
|
| 433 | + // the mount cache will only hold a single entry for the storage |
|
| 434 | + // this can lead to issues as the different ways the user has access to a storage can have different permissions |
|
| 435 | + // |
|
| 436 | + // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry |
|
| 437 | + |
|
| 438 | + $mountRootIds = array_map(function ($mount) { |
|
| 439 | + return $mount->getRootId(); |
|
| 440 | + }, $mountsContainingFile); |
|
| 441 | + $mountRootPaths = array_map(function ($mount) { |
|
| 442 | + return $mount->getRootInternalPath(); |
|
| 443 | + }, $mountsContainingFile); |
|
| 444 | + $mountProviders = array_unique(array_map(function ($mount) { |
|
| 445 | + return $mount->getMountProvider(); |
|
| 446 | + }, $mountsContainingFile)); |
|
| 447 | + $mountRoots = array_combine($mountRootIds, $mountRootPaths); |
|
| 448 | + |
|
| 449 | + $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders); |
|
| 450 | + |
|
| 451 | + $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) { |
|
| 452 | + return isset($mountRoots[$mount->getStorageRootId()]); |
|
| 453 | + }); |
|
| 454 | + |
|
| 455 | + if (count($mountsContainingFile) === 0) { |
|
| 456 | + if ($user === $this->getAppDataDirectoryName()) { |
|
| 457 | + $folder = $this->get($path); |
|
| 458 | + if ($folder instanceof Folder) { |
|
| 459 | + return $folder->getByIdInRootMount($id); |
|
| 460 | + } else { |
|
| 461 | + throw new \Exception('getByIdInPath with non folder'); |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + return []; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) { |
|
| 468 | + $rootInternalPath = $mountRoots[$mount->getStorageRootId()]; |
|
| 469 | + $cacheEntry = $mount->getStorage()->getCache()->get($id); |
|
| 470 | + if (!$cacheEntry) { |
|
| 471 | + return null; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + // cache jails will hide the "true" internal path |
|
| 475 | + $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/'); |
|
| 476 | + $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath)); |
|
| 477 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 478 | + $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/'); |
|
| 479 | + $storage = $mount->getStorage(); |
|
| 480 | + if ($storage === null) { |
|
| 481 | + return null; |
|
| 482 | + } |
|
| 483 | + $ownerId = $storage->getOwner($pathRelativeToMount); |
|
| 484 | + if ($ownerId !== false) { |
|
| 485 | + $owner = Server::get(IUserManager::class)->get($ownerId); |
|
| 486 | + } else { |
|
| 487 | + $owner = null; |
|
| 488 | + } |
|
| 489 | + return $this->createNode($absolutePath, new FileInfo( |
|
| 490 | + $absolutePath, |
|
| 491 | + $storage, |
|
| 492 | + $cacheEntry->getPath(), |
|
| 493 | + $cacheEntry, |
|
| 494 | + $mount, |
|
| 495 | + $owner, |
|
| 496 | + )); |
|
| 497 | + }, $mountsContainingFile); |
|
| 498 | + |
|
| 499 | + $nodes = array_filter($nodes); |
|
| 500 | + |
|
| 501 | + $folders = array_filter($nodes, function (Node $node) use ($path) { |
|
| 502 | + return PathHelper::getRelativePath($path, $node->getPath()) !== null; |
|
| 503 | + }); |
|
| 504 | + usort($folders, function ($a, $b) { |
|
| 505 | + return $b->getPath() <=> $a->getPath(); |
|
| 506 | + }); |
|
| 507 | + return $folders; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode { |
|
| 511 | + $path = $cacheEntry->getPath(); |
|
| 512 | + $fullPath = $mountPoint->getMountPoint() . $path; |
|
| 513 | + // todo: LazyNode? |
|
| 514 | + $info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint); |
|
| 515 | + $parentPath = dirname($fullPath); |
|
| 516 | + $parent = new LazyFolder($this, function () use ($parentPath) { |
|
| 517 | + $parent = $this->get($parentPath); |
|
| 518 | + if ($parent instanceof \OCP\Files\Folder) { |
|
| 519 | + return $parent; |
|
| 520 | + } else { |
|
| 521 | + throw new \Exception("parent $parentPath is not a folder"); |
|
| 522 | + } |
|
| 523 | + }, [ |
|
| 524 | + 'path' => $parentPath, |
|
| 525 | + ]); |
|
| 526 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 527 | + $view = new View(''); |
|
| 528 | + if ($isDir) { |
|
| 529 | + return new Folder($this, $view, $fullPath, $info, $parent); |
|
| 530 | + } else { |
|
| 531 | + return new File($this, $view, $fullPath, $info, $parent); |
|
| 532 | + } |
|
| 533 | + } |
|
| 534 | 534 | } |
@@ -57,2230 +57,2230 @@ |
||
| 57 | 57 | * \OC\Files\Storage\Storage object |
| 58 | 58 | */ |
| 59 | 59 | class View { |
| 60 | - private string $fakeRoot = ''; |
|
| 61 | - private ILockingProvider $lockingProvider; |
|
| 62 | - private bool $lockingEnabled; |
|
| 63 | - private bool $updaterEnabled = true; |
|
| 64 | - private UserManager $userManager; |
|
| 65 | - private LoggerInterface $logger; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @throws \Exception If $root contains an invalid path |
|
| 69 | - */ |
|
| 70 | - public function __construct(string $root = '') { |
|
| 71 | - if (!Filesystem::isValidPath($root)) { |
|
| 72 | - throw new \Exception(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $this->fakeRoot = $root; |
|
| 76 | - $this->lockingProvider = \OC::$server->get(ILockingProvider::class); |
|
| 77 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 78 | - $this->userManager = \OC::$server->getUserManager(); |
|
| 79 | - $this->logger = \OC::$server->get(LoggerInterface::class); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param ?string $path |
|
| 84 | - * @psalm-template S as string|null |
|
| 85 | - * @psalm-param S $path |
|
| 86 | - * @psalm-return (S is string ? string : null) |
|
| 87 | - */ |
|
| 88 | - public function getAbsolutePath($path = '/'): ?string { |
|
| 89 | - if ($path === null) { |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - $this->assertPathLength($path); |
|
| 93 | - if ($path === '') { |
|
| 94 | - $path = '/'; |
|
| 95 | - } |
|
| 96 | - if ($path[0] !== '/') { |
|
| 97 | - $path = '/' . $path; |
|
| 98 | - } |
|
| 99 | - return $this->fakeRoot . $path; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Change the root to a fake root |
|
| 104 | - * |
|
| 105 | - * @param string $fakeRoot |
|
| 106 | - */ |
|
| 107 | - public function chroot($fakeRoot): void { |
|
| 108 | - if (!$fakeRoot == '') { |
|
| 109 | - if ($fakeRoot[0] !== '/') { |
|
| 110 | - $fakeRoot = '/' . $fakeRoot; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - $this->fakeRoot = $fakeRoot; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Get the fake root |
|
| 118 | - */ |
|
| 119 | - public function getRoot(): string { |
|
| 120 | - return $this->fakeRoot; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * get path relative to the root of the view |
|
| 125 | - * |
|
| 126 | - * @param string $path |
|
| 127 | - */ |
|
| 128 | - public function getRelativePath($path): ?string { |
|
| 129 | - $this->assertPathLength($path); |
|
| 130 | - if ($this->fakeRoot == '') { |
|
| 131 | - return $path; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 135 | - return '/'; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // missing slashes can cause wrong matches! |
|
| 139 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 140 | - |
|
| 141 | - if (!str_starts_with($path, $root)) { |
|
| 142 | - return null; |
|
| 143 | - } else { |
|
| 144 | - $path = substr($path, strlen($this->fakeRoot)); |
|
| 145 | - if (strlen($path) === 0) { |
|
| 146 | - return '/'; |
|
| 147 | - } else { |
|
| 148 | - return $path; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the mountpoint of the storage object for a path |
|
| 155 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 156 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 157 | - * and does not take the chroot into account ) |
|
| 158 | - * |
|
| 159 | - * @param string $path |
|
| 160 | - */ |
|
| 161 | - public function getMountPoint($path): string { |
|
| 162 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the mountpoint of the storage object for a path |
|
| 167 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 168 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 169 | - * and does not take the chroot into account ) |
|
| 170 | - * |
|
| 171 | - * @param string $path |
|
| 172 | - */ |
|
| 173 | - public function getMount($path): IMountPoint { |
|
| 174 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Resolve a path to a storage and internal path |
|
| 179 | - * |
|
| 180 | - * @param string $path |
|
| 181 | - * @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path |
|
| 182 | - */ |
|
| 183 | - public function resolvePath($path): array { |
|
| 184 | - $a = $this->getAbsolutePath($path); |
|
| 185 | - $p = Filesystem::normalizePath($a); |
|
| 186 | - return Filesystem::resolvePath($p); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Return the path to a local version of the file |
|
| 191 | - * we need this because we can't know if a file is stored local or not from |
|
| 192 | - * outside the filestorage and for some purposes a local file is needed |
|
| 193 | - * |
|
| 194 | - * @param string $path |
|
| 195 | - */ |
|
| 196 | - public function getLocalFile($path): string|false { |
|
| 197 | - $parent = substr($path, 0, strrpos($path, '/') ?: 0); |
|
| 198 | - $path = $this->getAbsolutePath($path); |
|
| 199 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 200 | - if (Filesystem::isValidPath($parent) && $storage) { |
|
| 201 | - return $storage->getLocalFile($internalPath); |
|
| 202 | - } else { |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * the following functions operate with arguments and return values identical |
|
| 209 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 210 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 211 | - */ |
|
| 212 | - public function mkdir($path) { |
|
| 213 | - return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * remove mount point |
|
| 218 | - * |
|
| 219 | - * @param IMountPoint $mount |
|
| 220 | - * @param string $path relative to data/ |
|
| 221 | - */ |
|
| 222 | - protected function removeMount($mount, $path): bool { |
|
| 223 | - if ($mount instanceof MoveableMount) { |
|
| 224 | - // cut of /user/files to get the relative path to data/user/files |
|
| 225 | - $pathParts = explode('/', $path, 4); |
|
| 226 | - $relPath = '/' . $pathParts[3]; |
|
| 227 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 228 | - \OC_Hook::emit( |
|
| 229 | - Filesystem::CLASSNAME, 'umount', |
|
| 230 | - [Filesystem::signal_param_path => $relPath] |
|
| 231 | - ); |
|
| 232 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 233 | - $result = $mount->removeMount(); |
|
| 234 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 235 | - if ($result) { |
|
| 236 | - \OC_Hook::emit( |
|
| 237 | - Filesystem::CLASSNAME, 'post_umount', |
|
| 238 | - [Filesystem::signal_param_path => $relPath] |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 242 | - return $result; |
|
| 243 | - } else { |
|
| 244 | - // do not allow deleting the storage's root / the mount point |
|
| 245 | - // because for some storages it might delete the whole contents |
|
| 246 | - // but isn't supposed to work that way |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - public function disableCacheUpdate(): void { |
|
| 252 | - $this->updaterEnabled = false; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - public function enableCacheUpdate(): void { |
|
| 256 | - $this->updaterEnabled = true; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void { |
|
| 260 | - if ($this->updaterEnabled) { |
|
| 261 | - if (is_null($time)) { |
|
| 262 | - $time = time(); |
|
| 263 | - } |
|
| 264 | - $storage->getUpdater()->update($internalPath, $time, $sizeDifference); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - protected function removeUpdate(Storage $storage, string $internalPath): void { |
|
| 269 | - if ($this->updaterEnabled) { |
|
| 270 | - $storage->getUpdater()->remove($internalPath); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void { |
|
| 275 | - if ($this->updaterEnabled) { |
|
| 276 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void { |
|
| 281 | - if ($this->updaterEnabled) { |
|
| 282 | - $targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param string $path |
|
| 288 | - * @return bool|mixed |
|
| 289 | - */ |
|
| 290 | - public function rmdir($path) { |
|
| 291 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 292 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 293 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 294 | - return $this->removeMount($mount, $absolutePath); |
|
| 295 | - } |
|
| 296 | - if ($this->is_dir($path)) { |
|
| 297 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 298 | - } else { |
|
| 299 | - $result = false; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 303 | - $storage = $mount->getStorage(); |
|
| 304 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 305 | - $storage->getUpdater()->remove($internalPath); |
|
| 306 | - } |
|
| 307 | - return $result; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param string $path |
|
| 312 | - * @return resource|false |
|
| 313 | - */ |
|
| 314 | - public function opendir($path) { |
|
| 315 | - return $this->basicOperation('opendir', $path, ['read']); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @param string $path |
|
| 320 | - * @return bool|mixed |
|
| 321 | - */ |
|
| 322 | - public function is_dir($path) { |
|
| 323 | - if ($path == '/') { |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - return $this->basicOperation('is_dir', $path); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @param string $path |
|
| 331 | - * @return bool|mixed |
|
| 332 | - */ |
|
| 333 | - public function is_file($path) { |
|
| 334 | - if ($path == '/') { |
|
| 335 | - return false; |
|
| 336 | - } |
|
| 337 | - return $this->basicOperation('is_file', $path); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @param string $path |
|
| 342 | - * @return mixed |
|
| 343 | - */ |
|
| 344 | - public function stat($path) { |
|
| 345 | - return $this->basicOperation('stat', $path); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * @param string $path |
|
| 350 | - * @return mixed |
|
| 351 | - */ |
|
| 352 | - public function filetype($path) { |
|
| 353 | - return $this->basicOperation('filetype', $path); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * @param string $path |
|
| 358 | - * @return mixed |
|
| 359 | - */ |
|
| 360 | - public function filesize(string $path) { |
|
| 361 | - return $this->basicOperation('filesize', $path); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * @param string $path |
|
| 366 | - * @return bool|mixed |
|
| 367 | - * @throws InvalidPathException |
|
| 368 | - */ |
|
| 369 | - public function readfile($path) { |
|
| 370 | - $this->assertPathLength($path); |
|
| 371 | - if (ob_get_level()) { |
|
| 372 | - ob_end_clean(); |
|
| 373 | - } |
|
| 374 | - $handle = $this->fopen($path, 'rb'); |
|
| 375 | - if ($handle) { |
|
| 376 | - $chunkSize = 524288; // 512 kiB chunks |
|
| 377 | - while (!feof($handle)) { |
|
| 378 | - echo fread($handle, $chunkSize); |
|
| 379 | - flush(); |
|
| 380 | - $this->checkConnectionStatus(); |
|
| 381 | - } |
|
| 382 | - fclose($handle); |
|
| 383 | - return $this->filesize($path); |
|
| 384 | - } |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * @param string $path |
|
| 390 | - * @param int $from |
|
| 391 | - * @param int $to |
|
| 392 | - * @return bool|mixed |
|
| 393 | - * @throws InvalidPathException |
|
| 394 | - * @throws \OCP\Files\UnseekableException |
|
| 395 | - */ |
|
| 396 | - public function readfilePart($path, $from, $to) { |
|
| 397 | - $this->assertPathLength($path); |
|
| 398 | - if (ob_get_level()) { |
|
| 399 | - ob_end_clean(); |
|
| 400 | - } |
|
| 401 | - $handle = $this->fopen($path, 'rb'); |
|
| 402 | - if ($handle) { |
|
| 403 | - $chunkSize = 524288; // 512 kiB chunks |
|
| 404 | - $startReading = true; |
|
| 405 | - |
|
| 406 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 407 | - // forward file handle via chunked fread because fseek seem to have failed |
|
| 408 | - |
|
| 409 | - $end = $from + 1; |
|
| 410 | - while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
| 411 | - $len = $from - ftell($handle); |
|
| 412 | - if ($len > $chunkSize) { |
|
| 413 | - $len = $chunkSize; |
|
| 414 | - } |
|
| 415 | - $result = fread($handle, $len); |
|
| 416 | - |
|
| 417 | - if ($result === false) { |
|
| 418 | - $startReading = false; |
|
| 419 | - break; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - if ($startReading) { |
|
| 425 | - $end = $to + 1; |
|
| 426 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 427 | - $len = $end - ftell($handle); |
|
| 428 | - if ($len > $chunkSize) { |
|
| 429 | - $len = $chunkSize; |
|
| 430 | - } |
|
| 431 | - echo fread($handle, $len); |
|
| 432 | - flush(); |
|
| 433 | - $this->checkConnectionStatus(); |
|
| 434 | - } |
|
| 435 | - return ftell($handle) - $from; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 439 | - } |
|
| 440 | - return false; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - private function checkConnectionStatus(): void { |
|
| 444 | - $connectionStatus = \connection_status(); |
|
| 445 | - if ($connectionStatus !== CONNECTION_NORMAL) { |
|
| 446 | - throw new ConnectionLostException("Connection lost. Status: $connectionStatus"); |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * @param string $path |
|
| 452 | - * @return mixed |
|
| 453 | - */ |
|
| 454 | - public function isCreatable($path) { |
|
| 455 | - return $this->basicOperation('isCreatable', $path); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * @param string $path |
|
| 460 | - * @return mixed |
|
| 461 | - */ |
|
| 462 | - public function isReadable($path) { |
|
| 463 | - return $this->basicOperation('isReadable', $path); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * @param string $path |
|
| 468 | - * @return mixed |
|
| 469 | - */ |
|
| 470 | - public function isUpdatable($path) { |
|
| 471 | - return $this->basicOperation('isUpdatable', $path); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * @param string $path |
|
| 476 | - * @return bool|mixed |
|
| 477 | - */ |
|
| 478 | - public function isDeletable($path) { |
|
| 479 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 480 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 481 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 482 | - return $mount instanceof MoveableMount; |
|
| 483 | - } |
|
| 484 | - return $this->basicOperation('isDeletable', $path); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * @param string $path |
|
| 489 | - * @return mixed |
|
| 490 | - */ |
|
| 491 | - public function isSharable($path) { |
|
| 492 | - return $this->basicOperation('isSharable', $path); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * @param string $path |
|
| 497 | - * @return bool|mixed |
|
| 498 | - */ |
|
| 499 | - public function file_exists($path) { |
|
| 500 | - if ($path == '/') { |
|
| 501 | - return true; |
|
| 502 | - } |
|
| 503 | - return $this->basicOperation('file_exists', $path); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * @param string $path |
|
| 508 | - * @return mixed |
|
| 509 | - */ |
|
| 510 | - public function filemtime($path) { |
|
| 511 | - return $this->basicOperation('filemtime', $path); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * @param string $path |
|
| 516 | - * @param int|string $mtime |
|
| 517 | - */ |
|
| 518 | - public function touch($path, $mtime = null): bool { |
|
| 519 | - if (!is_null($mtime) && !is_numeric($mtime)) { |
|
| 520 | - $mtime = strtotime($mtime); |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - $hooks = ['touch']; |
|
| 524 | - |
|
| 525 | - if (!$this->file_exists($path)) { |
|
| 526 | - $hooks[] = 'create'; |
|
| 527 | - $hooks[] = 'write'; |
|
| 528 | - } |
|
| 529 | - try { |
|
| 530 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 531 | - } catch (\Exception $e) { |
|
| 532 | - $this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]); |
|
| 533 | - $result = false; |
|
| 534 | - } |
|
| 535 | - if (!$result) { |
|
| 536 | - // If create file fails because of permissions on external storage like SMB folders, |
|
| 537 | - // check file exists and return false if not. |
|
| 538 | - if (!$this->file_exists($path)) { |
|
| 539 | - return false; |
|
| 540 | - } |
|
| 541 | - if (is_null($mtime)) { |
|
| 542 | - $mtime = time(); |
|
| 543 | - } |
|
| 544 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 545 | - $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
| 546 | - } |
|
| 547 | - return true; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * @param string $path |
|
| 552 | - * @return string|false |
|
| 553 | - * @throws LockedException |
|
| 554 | - */ |
|
| 555 | - public function file_get_contents($path) { |
|
| 556 | - return $this->basicOperation('file_get_contents', $path, ['read']); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - protected function emit_file_hooks_pre(bool $exists, string $path, bool &$run): void { |
|
| 560 | - if (!$exists) { |
|
| 561 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
| 562 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 563 | - Filesystem::signal_param_run => &$run, |
|
| 564 | - ]); |
|
| 565 | - } else { |
|
| 566 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
| 567 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 568 | - Filesystem::signal_param_run => &$run, |
|
| 569 | - ]); |
|
| 570 | - } |
|
| 571 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
| 572 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 573 | - Filesystem::signal_param_run => &$run, |
|
| 574 | - ]); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - protected function emit_file_hooks_post(bool $exists, string $path): void { |
|
| 578 | - if (!$exists) { |
|
| 579 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
| 580 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 581 | - ]); |
|
| 582 | - } else { |
|
| 583 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
| 584 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 585 | - ]); |
|
| 586 | - } |
|
| 587 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
| 588 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 589 | - ]); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * @param string $path |
|
| 594 | - * @param string|resource $data |
|
| 595 | - * @return bool|mixed |
|
| 596 | - * @throws LockedException |
|
| 597 | - */ |
|
| 598 | - public function file_put_contents($path, $data) { |
|
| 599 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 600 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 601 | - if (Filesystem::isValidPath($path) |
|
| 602 | - && !Filesystem::isFileBlacklisted($path) |
|
| 603 | - ) { |
|
| 604 | - $path = $this->getRelativePath($absolutePath); |
|
| 605 | - if ($path === null) { |
|
| 606 | - throw new InvalidPathException("Path $absolutePath is not in the expected root"); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 610 | - |
|
| 611 | - $exists = $this->file_exists($path); |
|
| 612 | - if ($this->shouldEmitHooks($path)) { |
|
| 613 | - $run = true; |
|
| 614 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 615 | - if (!$run) { |
|
| 616 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 617 | - return false; |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - try { |
|
| 622 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 623 | - } catch (\Exception $e) { |
|
| 624 | - // Release the shared lock before throwing. |
|
| 625 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 626 | - throw $e; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** @var Storage $storage */ |
|
| 630 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 631 | - $target = $storage->fopen($internalPath, 'w'); |
|
| 632 | - if ($target) { |
|
| 633 | - [, $result] = Files::streamCopy($data, $target, true); |
|
| 634 | - fclose($target); |
|
| 635 | - fclose($data); |
|
| 636 | - |
|
| 637 | - $this->writeUpdate($storage, $internalPath); |
|
| 638 | - |
|
| 639 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 640 | - |
|
| 641 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 642 | - $this->emit_file_hooks_post($exists, $path); |
|
| 643 | - } |
|
| 644 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 645 | - return $result; |
|
| 646 | - } else { |
|
| 647 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 648 | - return false; |
|
| 649 | - } |
|
| 650 | - } else { |
|
| 651 | - return false; |
|
| 652 | - } |
|
| 653 | - } else { |
|
| 654 | - $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
| 655 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * @param string $path |
|
| 661 | - * @return bool|mixed |
|
| 662 | - */ |
|
| 663 | - public function unlink($path) { |
|
| 664 | - if ($path === '' || $path === '/') { |
|
| 665 | - // do not allow deleting the root |
|
| 666 | - return false; |
|
| 667 | - } |
|
| 668 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 669 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 670 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 671 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 672 | - return $this->removeMount($mount, $absolutePath); |
|
| 673 | - } |
|
| 674 | - if ($this->is_dir($path)) { |
|
| 675 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 676 | - } else { |
|
| 677 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 678 | - } |
|
| 679 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 680 | - $storage = $mount->getStorage(); |
|
| 681 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 682 | - $storage->getUpdater()->remove($internalPath); |
|
| 683 | - return true; |
|
| 684 | - } else { |
|
| 685 | - return $result; |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * @param string $directory |
|
| 691 | - * @return bool|mixed |
|
| 692 | - */ |
|
| 693 | - public function deleteAll($directory) { |
|
| 694 | - return $this->rmdir($directory); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * Rename/move a file or folder from the source path to target path. |
|
| 699 | - * |
|
| 700 | - * @param string $source source path |
|
| 701 | - * @param string $target target path |
|
| 702 | - * @param array $options |
|
| 703 | - * |
|
| 704 | - * @return bool|mixed |
|
| 705 | - * @throws LockedException |
|
| 706 | - */ |
|
| 707 | - public function rename($source, $target, array $options = []) { |
|
| 708 | - $checkSubMounts = $options['checkSubMounts'] ?? true; |
|
| 709 | - |
|
| 710 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
| 711 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
| 712 | - |
|
| 713 | - if (str_starts_with($absolutePath2, $absolutePath1 . '/')) { |
|
| 714 | - throw new ForbiddenException('Moving a folder into a child folder is forbidden', false); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - /** @var IMountManager $mountManager */ |
|
| 718 | - $mountManager = \OC::$server->get(IMountManager::class); |
|
| 719 | - |
|
| 720 | - $targetParts = explode('/', $absolutePath2); |
|
| 721 | - $targetUser = $targetParts[1] ?? null; |
|
| 722 | - $result = false; |
|
| 723 | - if ( |
|
| 724 | - Filesystem::isValidPath($target) |
|
| 725 | - && Filesystem::isValidPath($source) |
|
| 726 | - && !Filesystem::isFileBlacklisted($target) |
|
| 727 | - ) { |
|
| 728 | - $source = $this->getRelativePath($absolutePath1); |
|
| 729 | - $target = $this->getRelativePath($absolutePath2); |
|
| 730 | - $exists = $this->file_exists($target); |
|
| 731 | - |
|
| 732 | - if ($source == null || $target == null) { |
|
| 733 | - return false; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - try { |
|
| 737 | - $this->verifyPath(dirname($target), basename($target)); |
|
| 738 | - } catch (InvalidPathException) { |
|
| 739 | - return false; |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - $this->lockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
| 743 | - try { |
|
| 744 | - $this->lockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
| 745 | - |
|
| 746 | - $run = true; |
|
| 747 | - if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) { |
|
| 748 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 749 | - $this->emit_file_hooks_pre($exists, $target, $run); |
|
| 750 | - } elseif ($this->shouldEmitHooks($source)) { |
|
| 751 | - $sourcePath = $this->getHookPath($source); |
|
| 752 | - $targetPath = $this->getHookPath($target); |
|
| 753 | - if ($sourcePath !== null && $targetPath !== null) { |
|
| 754 | - \OC_Hook::emit( |
|
| 755 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 756 | - [ |
|
| 757 | - Filesystem::signal_param_oldpath => $sourcePath, |
|
| 758 | - Filesystem::signal_param_newpath => $targetPath, |
|
| 759 | - Filesystem::signal_param_run => &$run |
|
| 760 | - ] |
|
| 761 | - ); |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - if ($run) { |
|
| 765 | - $manager = Filesystem::getMountManager(); |
|
| 766 | - $mount1 = $this->getMount($source); |
|
| 767 | - $mount2 = $this->getMount($target); |
|
| 768 | - $storage1 = $mount1->getStorage(); |
|
| 769 | - $storage2 = $mount2->getStorage(); |
|
| 770 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 771 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 772 | - |
|
| 773 | - $this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 774 | - try { |
|
| 775 | - $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 776 | - |
|
| 777 | - if ($checkSubMounts) { |
|
| 778 | - $movedMounts = $mountManager->findIn($this->getAbsolutePath($source)); |
|
| 779 | - } else { |
|
| 780 | - $movedMounts = []; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - if ($internalPath1 === '') { |
|
| 784 | - $sourceParentMount = $this->getMount(dirname($source)); |
|
| 785 | - $movedMounts[] = $mount1; |
|
| 786 | - $this->validateMountMove($movedMounts, $sourceParentMount, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 787 | - /** |
|
| 788 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 789 | - */ |
|
| 790 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
| 791 | - $result = $mount1->moveMount($absolutePath2); |
|
| 792 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 793 | - |
|
| 794 | - // moving a file/folder within the same mount point |
|
| 795 | - } elseif ($storage1 === $storage2) { |
|
| 796 | - if (count($movedMounts) > 0) { |
|
| 797 | - $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 798 | - } |
|
| 799 | - if ($storage1) { |
|
| 800 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 801 | - } else { |
|
| 802 | - $result = false; |
|
| 803 | - } |
|
| 804 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 805 | - } else { |
|
| 806 | - if (count($movedMounts) > 0) { |
|
| 807 | - $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 808 | - } |
|
| 809 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
| 813 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 814 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 815 | - } elseif ($result) { |
|
| 816 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 817 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 818 | - } |
|
| 819 | - } |
|
| 820 | - } catch (\Exception $e) { |
|
| 821 | - throw $e; |
|
| 822 | - } finally { |
|
| 823 | - $this->changeLock($source, ILockingProvider::LOCK_SHARED, true); |
|
| 824 | - $this->changeLock($target, ILockingProvider::LOCK_SHARED, true); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
| 828 | - if ($this->shouldEmitHooks()) { |
|
| 829 | - $this->emit_file_hooks_post($exists, $target); |
|
| 830 | - } |
|
| 831 | - } elseif ($result) { |
|
| 832 | - if ($this->shouldEmitHooks($source) && $this->shouldEmitHooks($target)) { |
|
| 833 | - $sourcePath = $this->getHookPath($source); |
|
| 834 | - $targetPath = $this->getHookPath($target); |
|
| 835 | - if ($sourcePath !== null && $targetPath !== null) { |
|
| 836 | - \OC_Hook::emit( |
|
| 837 | - Filesystem::CLASSNAME, |
|
| 838 | - Filesystem::signal_post_rename, |
|
| 839 | - [ |
|
| 840 | - Filesystem::signal_param_oldpath => $sourcePath, |
|
| 841 | - Filesystem::signal_param_newpath => $targetPath, |
|
| 842 | - ] |
|
| 843 | - ); |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - } catch (\Exception $e) { |
|
| 849 | - throw $e; |
|
| 850 | - } finally { |
|
| 851 | - $this->unlockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
| 852 | - $this->unlockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
| 853 | - } |
|
| 854 | - } |
|
| 855 | - return $result; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * @throws ForbiddenException |
|
| 860 | - */ |
|
| 861 | - private function validateMountMove(array $mounts, IMountPoint $sourceMount, IMountPoint $targetMount, bool $targetIsShared): void { |
|
| 862 | - $targetPath = $this->getRelativePath($targetMount->getMountPoint()); |
|
| 863 | - if ($targetPath) { |
|
| 864 | - $targetPath = trim($targetPath, '/'); |
|
| 865 | - } else { |
|
| 866 | - $targetPath = $targetMount->getMountPoint(); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - $l = \OC::$server->get(IFactory::class)->get('files'); |
|
| 870 | - foreach ($mounts as $mount) { |
|
| 871 | - $sourcePath = $this->getRelativePath($mount->getMountPoint()); |
|
| 872 | - if ($sourcePath) { |
|
| 873 | - $sourcePath = trim($sourcePath, '/'); |
|
| 874 | - } else { |
|
| 875 | - $sourcePath = $mount->getMountPoint(); |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - if (!$mount instanceof MoveableMount) { |
|
| 879 | - throw new ForbiddenException($l->t('Storage %s cannot be moved', [$sourcePath]), false); |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - if ($targetIsShared) { |
|
| 883 | - if ($sourceMount instanceof SharedMount) { |
|
| 884 | - throw new ForbiddenException($l->t('Moving a share (%s) into a shared folder is not allowed', [$sourcePath]), false); |
|
| 885 | - } else { |
|
| 886 | - throw new ForbiddenException($l->t('Moving a storage (%s) into a shared folder is not allowed', [$sourcePath]), false); |
|
| 887 | - } |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - if ($sourceMount !== $targetMount) { |
|
| 891 | - if ($sourceMount instanceof SharedMount) { |
|
| 892 | - if ($targetMount instanceof SharedMount) { |
|
| 893 | - throw new ForbiddenException($l->t('Moving a share (%s) into another share (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 894 | - } else { |
|
| 895 | - throw new ForbiddenException($l->t('Moving a share (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 896 | - } |
|
| 897 | - } else { |
|
| 898 | - if ($targetMount instanceof SharedMount) { |
|
| 899 | - throw new ForbiddenException($l->t('Moving a storage (%s) into a share (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 900 | - } else { |
|
| 901 | - throw new ForbiddenException($l->t('Moving a storage (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - } |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - |
|
| 908 | - /** |
|
| 909 | - * Copy a file/folder from the source path to target path |
|
| 910 | - * |
|
| 911 | - * @param string $source source path |
|
| 912 | - * @param string $target target path |
|
| 913 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 914 | - * |
|
| 915 | - * @return bool|mixed |
|
| 916 | - */ |
|
| 917 | - public function copy($source, $target, $preserveMtime = false) { |
|
| 918 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
| 919 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
| 920 | - $result = false; |
|
| 921 | - if ( |
|
| 922 | - Filesystem::isValidPath($target) |
|
| 923 | - && Filesystem::isValidPath($source) |
|
| 924 | - && !Filesystem::isFileBlacklisted($target) |
|
| 925 | - ) { |
|
| 926 | - $source = $this->getRelativePath($absolutePath1); |
|
| 927 | - $target = $this->getRelativePath($absolutePath2); |
|
| 928 | - |
|
| 929 | - if ($source == null || $target == null) { |
|
| 930 | - return false; |
|
| 931 | - } |
|
| 932 | - $run = true; |
|
| 933 | - |
|
| 934 | - $this->lockFile($target, ILockingProvider::LOCK_SHARED); |
|
| 935 | - $this->lockFile($source, ILockingProvider::LOCK_SHARED); |
|
| 936 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 937 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 938 | - |
|
| 939 | - try { |
|
| 940 | - $exists = $this->file_exists($target); |
|
| 941 | - if ($this->shouldEmitHooks($target)) { |
|
| 942 | - \OC_Hook::emit( |
|
| 943 | - Filesystem::CLASSNAME, |
|
| 944 | - Filesystem::signal_copy, |
|
| 945 | - [ |
|
| 946 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
| 947 | - Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
| 948 | - Filesystem::signal_param_run => &$run |
|
| 949 | - ] |
|
| 950 | - ); |
|
| 951 | - $this->emit_file_hooks_pre($exists, $target, $run); |
|
| 952 | - } |
|
| 953 | - if ($run) { |
|
| 954 | - $mount1 = $this->getMount($source); |
|
| 955 | - $mount2 = $this->getMount($target); |
|
| 956 | - $storage1 = $mount1->getStorage(); |
|
| 957 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 958 | - $storage2 = $mount2->getStorage(); |
|
| 959 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 960 | - |
|
| 961 | - $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 962 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 963 | - |
|
| 964 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 965 | - if ($storage1) { |
|
| 966 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 967 | - } else { |
|
| 968 | - $result = false; |
|
| 969 | - } |
|
| 970 | - } else { |
|
| 971 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - if ($result) { |
|
| 975 | - $this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - $this->changeLock($target, ILockingProvider::LOCK_SHARED); |
|
| 979 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 980 | - |
|
| 981 | - if ($this->shouldEmitHooks($target) && $result !== false) { |
|
| 982 | - \OC_Hook::emit( |
|
| 983 | - Filesystem::CLASSNAME, |
|
| 984 | - Filesystem::signal_post_copy, |
|
| 985 | - [ |
|
| 986 | - Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
| 987 | - Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
| 988 | - ] |
|
| 989 | - ); |
|
| 990 | - $this->emit_file_hooks_post($exists, $target); |
|
| 991 | - } |
|
| 992 | - } |
|
| 993 | - } catch (\Exception $e) { |
|
| 994 | - $this->unlockFile($target, $lockTypePath2); |
|
| 995 | - $this->unlockFile($source, $lockTypePath1); |
|
| 996 | - throw $e; |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - $this->unlockFile($target, $lockTypePath2); |
|
| 1000 | - $this->unlockFile($source, $lockTypePath1); |
|
| 1001 | - } |
|
| 1002 | - return $result; |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - /** |
|
| 1006 | - * @param string $path |
|
| 1007 | - * @param string $mode 'r' or 'w' |
|
| 1008 | - * @return resource|false |
|
| 1009 | - * @throws LockedException |
|
| 1010 | - */ |
|
| 1011 | - public function fopen($path, $mode) { |
|
| 1012 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 1013 | - $hooks = []; |
|
| 1014 | - switch ($mode) { |
|
| 1015 | - case 'r': |
|
| 1016 | - $hooks[] = 'read'; |
|
| 1017 | - break; |
|
| 1018 | - case 'r+': |
|
| 1019 | - case 'w+': |
|
| 1020 | - case 'x+': |
|
| 1021 | - case 'a+': |
|
| 1022 | - $hooks[] = 'read'; |
|
| 1023 | - $hooks[] = 'write'; |
|
| 1024 | - break; |
|
| 1025 | - case 'w': |
|
| 1026 | - case 'x': |
|
| 1027 | - case 'a': |
|
| 1028 | - $hooks[] = 'write'; |
|
| 1029 | - break; |
|
| 1030 | - default: |
|
| 1031 | - $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - if ($mode !== 'r' && $mode !== 'w') { |
|
| 1035 | - $this->logger->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends', ['app' => 'core']); |
|
| 1036 | - } |
|
| 1037 | - |
|
| 1038 | - $handle = $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 1039 | - if (!is_resource($handle) && $mode === 'r') { |
|
| 1040 | - // trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed |
|
| 1041 | - $mount = $this->getMount($path); |
|
| 1042 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1043 | - $storage = $mount->getStorage(); |
|
| 1044 | - if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) { |
|
| 1045 | - $this->writeUpdate($storage, $internalPath); |
|
| 1046 | - } |
|
| 1047 | - } |
|
| 1048 | - return $handle; |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * @param string $path |
|
| 1053 | - * @throws InvalidPathException |
|
| 1054 | - */ |
|
| 1055 | - public function toTmpFile($path): string|false { |
|
| 1056 | - $this->assertPathLength($path); |
|
| 1057 | - if (Filesystem::isValidPath($path)) { |
|
| 1058 | - $source = $this->fopen($path, 'r'); |
|
| 1059 | - if ($source) { |
|
| 1060 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1061 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1062 | - file_put_contents($tmpFile, $source); |
|
| 1063 | - return $tmpFile; |
|
| 1064 | - } else { |
|
| 1065 | - return false; |
|
| 1066 | - } |
|
| 1067 | - } else { |
|
| 1068 | - return false; |
|
| 1069 | - } |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - /** |
|
| 1073 | - * @param string $tmpFile |
|
| 1074 | - * @param string $path |
|
| 1075 | - * @return bool|mixed |
|
| 1076 | - * @throws InvalidPathException |
|
| 1077 | - */ |
|
| 1078 | - public function fromTmpFile($tmpFile, $path) { |
|
| 1079 | - $this->assertPathLength($path); |
|
| 1080 | - if (Filesystem::isValidPath($path)) { |
|
| 1081 | - // Get directory that the file is going into |
|
| 1082 | - $filePath = dirname($path); |
|
| 1083 | - |
|
| 1084 | - // Create the directories if any |
|
| 1085 | - if (!$this->file_exists($filePath)) { |
|
| 1086 | - $result = $this->createParentDirectories($filePath); |
|
| 1087 | - if ($result === false) { |
|
| 1088 | - return false; |
|
| 1089 | - } |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - $source = fopen($tmpFile, 'r'); |
|
| 1093 | - if ($source) { |
|
| 1094 | - $result = $this->file_put_contents($path, $source); |
|
| 1095 | - /** |
|
| 1096 | - * $this->file_put_contents() might have already closed |
|
| 1097 | - * the resource, so we check it, before trying to close it |
|
| 1098 | - * to avoid messages in the error log. |
|
| 1099 | - * @psalm-suppress RedundantCondition false-positive |
|
| 1100 | - */ |
|
| 1101 | - if (is_resource($source)) { |
|
| 1102 | - fclose($source); |
|
| 1103 | - } |
|
| 1104 | - unlink($tmpFile); |
|
| 1105 | - return $result; |
|
| 1106 | - } else { |
|
| 1107 | - return false; |
|
| 1108 | - } |
|
| 1109 | - } else { |
|
| 1110 | - return false; |
|
| 1111 | - } |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * @param string $path |
|
| 1117 | - * @return mixed |
|
| 1118 | - * @throws InvalidPathException |
|
| 1119 | - */ |
|
| 1120 | - public function getMimeType($path) { |
|
| 1121 | - $this->assertPathLength($path); |
|
| 1122 | - return $this->basicOperation('getMimeType', $path); |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - /** |
|
| 1126 | - * @param string $type |
|
| 1127 | - * @param string $path |
|
| 1128 | - * @param bool $raw |
|
| 1129 | - */ |
|
| 1130 | - public function hash($type, $path, $raw = false): string|bool { |
|
| 1131 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1132 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1133 | - if (Filesystem::isValidPath($path)) { |
|
| 1134 | - $path = $this->getRelativePath($absolutePath); |
|
| 1135 | - if ($path == null) { |
|
| 1136 | - return false; |
|
| 1137 | - } |
|
| 1138 | - if ($this->shouldEmitHooks($path)) { |
|
| 1139 | - \OC_Hook::emit( |
|
| 1140 | - Filesystem::CLASSNAME, |
|
| 1141 | - Filesystem::signal_read, |
|
| 1142 | - [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
| 1143 | - ); |
|
| 1144 | - } |
|
| 1145 | - /** @var Storage|null $storage */ |
|
| 1146 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1147 | - if ($storage) { |
|
| 1148 | - return $storage->hash($type, $internalPath, $raw); |
|
| 1149 | - } |
|
| 1150 | - } |
|
| 1151 | - return false; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - /** |
|
| 1155 | - * @param string $path |
|
| 1156 | - * @return mixed |
|
| 1157 | - * @throws InvalidPathException |
|
| 1158 | - */ |
|
| 1159 | - public function free_space($path = '/') { |
|
| 1160 | - $this->assertPathLength($path); |
|
| 1161 | - $result = $this->basicOperation('free_space', $path); |
|
| 1162 | - if ($result === null) { |
|
| 1163 | - throw new InvalidPathException(); |
|
| 1164 | - } |
|
| 1165 | - return $result; |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1170 | - * |
|
| 1171 | - * @param mixed $extraParam (optional) |
|
| 1172 | - * @return mixed |
|
| 1173 | - * @throws LockedException |
|
| 1174 | - * |
|
| 1175 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1176 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1177 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1178 | - */ |
|
| 1179 | - private function basicOperation(string $operation, string $path, array $hooks = [], $extraParam = null) { |
|
| 1180 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1181 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1182 | - if (Filesystem::isValidPath($path) |
|
| 1183 | - && !Filesystem::isFileBlacklisted($path) |
|
| 1184 | - ) { |
|
| 1185 | - $path = $this->getRelativePath($absolutePath); |
|
| 1186 | - if ($path == null) { |
|
| 1187 | - return false; |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1191 | - // always a shared lock during pre-hooks so the hook can read the file |
|
| 1192 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - $run = $this->runHooks($hooks, $path); |
|
| 1196 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1197 | - if ($run && $storage) { |
|
| 1198 | - /** @var Storage $storage */ |
|
| 1199 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1200 | - try { |
|
| 1201 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1202 | - } catch (LockedException $e) { |
|
| 1203 | - // release the shared lock we acquired before quitting |
|
| 1204 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1205 | - throw $e; |
|
| 1206 | - } |
|
| 1207 | - } |
|
| 1208 | - try { |
|
| 1209 | - if (!is_null($extraParam)) { |
|
| 1210 | - $result = $storage->$operation($internalPath, $extraParam); |
|
| 1211 | - } else { |
|
| 1212 | - $result = $storage->$operation($internalPath); |
|
| 1213 | - } |
|
| 1214 | - } catch (\Exception $e) { |
|
| 1215 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1216 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1217 | - } elseif (in_array('read', $hooks)) { |
|
| 1218 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1219 | - } |
|
| 1220 | - throw $e; |
|
| 1221 | - } |
|
| 1222 | - |
|
| 1223 | - if ($result !== false && in_array('delete', $hooks)) { |
|
| 1224 | - $this->removeUpdate($storage, $internalPath); |
|
| 1225 | - } |
|
| 1226 | - if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
| 1227 | - $isCreateOperation = $operation === 'mkdir' || ($operation === 'file_put_contents' && in_array('create', $hooks, true)); |
|
| 1228 | - $sizeDifference = $operation === 'mkdir' ? 0 : $result; |
|
| 1229 | - $this->writeUpdate($storage, $internalPath, null, $isCreateOperation ? $sizeDifference : null); |
|
| 1230 | - } |
|
| 1231 | - if ($result !== false && in_array('touch', $hooks)) { |
|
| 1232 | - $this->writeUpdate($storage, $internalPath, $extraParam, 0); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1236 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - $unlockLater = false; |
|
| 1240 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1241 | - $unlockLater = true; |
|
| 1242 | - // make sure our unlocking callback will still be called if connection is aborted |
|
| 1243 | - ignore_user_abort(true); |
|
| 1244 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1245 | - if (in_array('write', $hooks)) { |
|
| 1246 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1247 | - } elseif (in_array('read', $hooks)) { |
|
| 1248 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1249 | - } |
|
| 1250 | - }); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1254 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1255 | - $this->runHooks($hooks, $path, true); |
|
| 1256 | - } |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - if (!$unlockLater |
|
| 1260 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1261 | - ) { |
|
| 1262 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1263 | - } |
|
| 1264 | - return $result; |
|
| 1265 | - } else { |
|
| 1266 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - return null; |
|
| 1270 | - } |
|
| 1271 | - |
|
| 1272 | - /** |
|
| 1273 | - * get the path relative to the default root for hook usage |
|
| 1274 | - * |
|
| 1275 | - * @param string $path |
|
| 1276 | - * @return ?string |
|
| 1277 | - */ |
|
| 1278 | - private function getHookPath($path): ?string { |
|
| 1279 | - $view = Filesystem::getView(); |
|
| 1280 | - if (!$view) { |
|
| 1281 | - return $path; |
|
| 1282 | - } |
|
| 1283 | - return $view->getRelativePath($this->getAbsolutePath($path)); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - private function shouldEmitHooks(string $path = ''): bool { |
|
| 1287 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1288 | - return false; |
|
| 1289 | - } |
|
| 1290 | - if (!Filesystem::$loaded) { |
|
| 1291 | - return false; |
|
| 1292 | - } |
|
| 1293 | - $defaultRoot = Filesystem::getRoot(); |
|
| 1294 | - if ($defaultRoot === null) { |
|
| 1295 | - return false; |
|
| 1296 | - } |
|
| 1297 | - if ($this->fakeRoot === $defaultRoot) { |
|
| 1298 | - return true; |
|
| 1299 | - } |
|
| 1300 | - $fullPath = $this->getAbsolutePath($path); |
|
| 1301 | - |
|
| 1302 | - if ($fullPath === $defaultRoot) { |
|
| 1303 | - return true; |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * @param string[] $hooks |
|
| 1311 | - * @param string $path |
|
| 1312 | - * @param bool $post |
|
| 1313 | - * @return bool |
|
| 1314 | - */ |
|
| 1315 | - private function runHooks($hooks, $path, $post = false) { |
|
| 1316 | - $relativePath = $path; |
|
| 1317 | - $path = $this->getHookPath($path); |
|
| 1318 | - $prefix = $post ? 'post_' : ''; |
|
| 1319 | - $run = true; |
|
| 1320 | - if ($this->shouldEmitHooks($relativePath)) { |
|
| 1321 | - foreach ($hooks as $hook) { |
|
| 1322 | - if ($hook != 'read') { |
|
| 1323 | - \OC_Hook::emit( |
|
| 1324 | - Filesystem::CLASSNAME, |
|
| 1325 | - $prefix . $hook, |
|
| 1326 | - [ |
|
| 1327 | - Filesystem::signal_param_run => &$run, |
|
| 1328 | - Filesystem::signal_param_path => $path |
|
| 1329 | - ] |
|
| 1330 | - ); |
|
| 1331 | - } elseif (!$post) { |
|
| 1332 | - \OC_Hook::emit( |
|
| 1333 | - Filesystem::CLASSNAME, |
|
| 1334 | - $prefix . $hook, |
|
| 1335 | - [ |
|
| 1336 | - Filesystem::signal_param_path => $path |
|
| 1337 | - ] |
|
| 1338 | - ); |
|
| 1339 | - } |
|
| 1340 | - } |
|
| 1341 | - } |
|
| 1342 | - return $run; |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - /** |
|
| 1346 | - * check if a file or folder has been updated since $time |
|
| 1347 | - * |
|
| 1348 | - * @param string $path |
|
| 1349 | - * @param int $time |
|
| 1350 | - * @return bool |
|
| 1351 | - */ |
|
| 1352 | - public function hasUpdated($path, $time) { |
|
| 1353 | - return $this->basicOperation('hasUpdated', $path, [], $time); |
|
| 1354 | - } |
|
| 1355 | - |
|
| 1356 | - /** |
|
| 1357 | - * @param string $ownerId |
|
| 1358 | - * @return IUser |
|
| 1359 | - */ |
|
| 1360 | - private function getUserObjectForOwner(string $ownerId) { |
|
| 1361 | - return new LazyUser($ownerId, $this->userManager); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - /** |
|
| 1365 | - * Get file info from cache |
|
| 1366 | - * |
|
| 1367 | - * If the file is not in cached it will be scanned |
|
| 1368 | - * If the file has changed on storage the cache will be updated |
|
| 1369 | - * |
|
| 1370 | - * @param Storage $storage |
|
| 1371 | - * @param string $internalPath |
|
| 1372 | - * @param string $relativePath |
|
| 1373 | - * @return ICacheEntry|bool |
|
| 1374 | - */ |
|
| 1375 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1376 | - $cache = $storage->getCache($internalPath); |
|
| 1377 | - $data = $cache->get($internalPath); |
|
| 1378 | - $watcher = $storage->getWatcher($internalPath); |
|
| 1379 | - |
|
| 1380 | - try { |
|
| 1381 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1382 | - if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
| 1383 | - if (!$storage->file_exists($internalPath)) { |
|
| 1384 | - return false; |
|
| 1385 | - } |
|
| 1386 | - // don't need to get a lock here since the scanner does it's own locking |
|
| 1387 | - $scanner = $storage->getScanner($internalPath); |
|
| 1388 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1389 | - $data = $cache->get($internalPath); |
|
| 1390 | - } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1391 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1392 | - $watcher->update($internalPath, $data); |
|
| 1393 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1394 | - $data = $cache->get($internalPath); |
|
| 1395 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1396 | - } |
|
| 1397 | - } catch (LockedException $e) { |
|
| 1398 | - // if the file is locked we just use the old cache info |
|
| 1399 | - } |
|
| 1400 | - |
|
| 1401 | - return $data; |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - /** |
|
| 1405 | - * get the filesystem info |
|
| 1406 | - * |
|
| 1407 | - * @param string $path |
|
| 1408 | - * @param bool|string $includeMountPoints true to add mountpoint sizes, |
|
| 1409 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1410 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1411 | - */ |
|
| 1412 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1413 | - $this->assertPathLength($path); |
|
| 1414 | - if (!Filesystem::isValidPath($path)) { |
|
| 1415 | - return false; |
|
| 1416 | - } |
|
| 1417 | - $relativePath = $path; |
|
| 1418 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1419 | - |
|
| 1420 | - $mount = Filesystem::getMountManager()->find($path); |
|
| 1421 | - $storage = $mount->getStorage(); |
|
| 1422 | - $internalPath = $mount->getInternalPath($path); |
|
| 1423 | - if ($storage) { |
|
| 1424 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1425 | - |
|
| 1426 | - if (!$data instanceof ICacheEntry) { |
|
| 1427 | - if (Cache\Scanner::isPartialFile($relativePath)) { |
|
| 1428 | - return $this->getPartFileInfo($relativePath); |
|
| 1429 | - } |
|
| 1430 | - |
|
| 1431 | - return false; |
|
| 1432 | - } |
|
| 1433 | - |
|
| 1434 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1435 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1436 | - } |
|
| 1437 | - if ($internalPath === '' && $data['name']) { |
|
| 1438 | - $data['name'] = basename($path); |
|
| 1439 | - } |
|
| 1440 | - |
|
| 1441 | - $ownerId = $storage->getOwner($internalPath); |
|
| 1442 | - $owner = null; |
|
| 1443 | - if ($ownerId !== false) { |
|
| 1444 | - // ownerId might be null if files are accessed with an access token without file system access |
|
| 1445 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1446 | - } |
|
| 1447 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1448 | - |
|
| 1449 | - if (isset($data['fileid'])) { |
|
| 1450 | - if ($includeMountPoints && $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1451 | - //add the sizes of other mount points to the folder |
|
| 1452 | - $extOnly = ($includeMountPoints === 'ext'); |
|
| 1453 | - $this->addSubMounts($info, $extOnly); |
|
| 1454 | - } |
|
| 1455 | - } |
|
| 1456 | - |
|
| 1457 | - return $info; |
|
| 1458 | - } else { |
|
| 1459 | - $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - return false; |
|
| 1463 | - } |
|
| 1464 | - |
|
| 1465 | - /** |
|
| 1466 | - * Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts |
|
| 1467 | - */ |
|
| 1468 | - public function addSubMounts(FileInfo $info, $extOnly = false): void { |
|
| 1469 | - $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
|
| 1470 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1471 | - return !($extOnly && $mount instanceof SharedMount); |
|
| 1472 | - })); |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * get the content of a directory |
|
| 1477 | - * |
|
| 1478 | - * @param string $directory path under datadirectory |
|
| 1479 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1480 | - * @return FileInfo[] |
|
| 1481 | - */ |
|
| 1482 | - public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Files\FileInfo $directoryInfo = null) { |
|
| 1483 | - $this->assertPathLength($directory); |
|
| 1484 | - if (!Filesystem::isValidPath($directory)) { |
|
| 1485 | - return []; |
|
| 1486 | - } |
|
| 1487 | - |
|
| 1488 | - $path = $this->getAbsolutePath($directory); |
|
| 1489 | - $path = Filesystem::normalizePath($path); |
|
| 1490 | - $mount = $this->getMount($directory); |
|
| 1491 | - $storage = $mount->getStorage(); |
|
| 1492 | - $internalPath = $mount->getInternalPath($path); |
|
| 1493 | - if (!$storage) { |
|
| 1494 | - return []; |
|
| 1495 | - } |
|
| 1496 | - |
|
| 1497 | - $cache = $storage->getCache($internalPath); |
|
| 1498 | - $user = \OC_User::getUser(); |
|
| 1499 | - |
|
| 1500 | - if (!$directoryInfo) { |
|
| 1501 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1502 | - if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
| 1503 | - return []; |
|
| 1504 | - } |
|
| 1505 | - } else { |
|
| 1506 | - $data = $directoryInfo; |
|
| 1507 | - } |
|
| 1508 | - |
|
| 1509 | - if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1510 | - return []; |
|
| 1511 | - } |
|
| 1512 | - |
|
| 1513 | - $folderId = $data->getId(); |
|
| 1514 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1515 | - |
|
| 1516 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1517 | - |
|
| 1518 | - $fileNames = array_map(function (ICacheEntry $content) { |
|
| 1519 | - return $content->getName(); |
|
| 1520 | - }, $contents); |
|
| 1521 | - /** |
|
| 1522 | - * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1523 | - */ |
|
| 1524 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1525 | - if ($sharingDisabled) { |
|
| 1526 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1527 | - } |
|
| 1528 | - $ownerId = $storage->getOwner($content['path']); |
|
| 1529 | - if ($ownerId !== false) { |
|
| 1530 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1531 | - } else { |
|
| 1532 | - $owner = null; |
|
| 1533 | - } |
|
| 1534 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1535 | - }, $contents); |
|
| 1536 | - $files = array_combine($fileNames, $fileInfos); |
|
| 1537 | - |
|
| 1538 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1539 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1540 | - |
|
| 1541 | - // make sure nested mounts are sorted after their parent mounts |
|
| 1542 | - // otherwise doesn't propagate the etag across storage boundaries correctly |
|
| 1543 | - usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
| 1544 | - return $a->getMountPoint() <=> $b->getMountPoint(); |
|
| 1545 | - }); |
|
| 1546 | - |
|
| 1547 | - $dirLength = strlen($path); |
|
| 1548 | - foreach ($mounts as $mount) { |
|
| 1549 | - $mountPoint = $mount->getMountPoint(); |
|
| 1550 | - $subStorage = $mount->getStorage(); |
|
| 1551 | - if ($subStorage) { |
|
| 1552 | - $subCache = $subStorage->getCache(''); |
|
| 1553 | - |
|
| 1554 | - $rootEntry = $subCache->get(''); |
|
| 1555 | - if (!$rootEntry) { |
|
| 1556 | - $subScanner = $subStorage->getScanner(); |
|
| 1557 | - try { |
|
| 1558 | - $subScanner->scanFile(''); |
|
| 1559 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1560 | - continue; |
|
| 1561 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1562 | - continue; |
|
| 1563 | - } catch (\Exception $e) { |
|
| 1564 | - // sometimes when the storage is not available it can be any exception |
|
| 1565 | - $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
| 1566 | - 'exception' => $e, |
|
| 1567 | - 'app' => 'core', |
|
| 1568 | - ]); |
|
| 1569 | - continue; |
|
| 1570 | - } |
|
| 1571 | - $rootEntry = $subCache->get(''); |
|
| 1572 | - } |
|
| 1573 | - |
|
| 1574 | - if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1575 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1576 | - if ($pos = strpos($relativePath, '/')) { |
|
| 1577 | - //mountpoint inside subfolder add size to the correct folder |
|
| 1578 | - $entryName = substr($relativePath, 0, $pos); |
|
| 1579 | - |
|
| 1580 | - // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet |
|
| 1581 | - if (!isset($files[$entryName])) { |
|
| 1582 | - try { |
|
| 1583 | - [$storage, ] = $this->resolvePath($path . '/' . $entryName); |
|
| 1584 | - // make sure we can create the mountpoint folder, even if the user has a quota of 0 |
|
| 1585 | - if ($storage->instanceOfStorage(Quota::class)) { |
|
| 1586 | - $storage->enableQuota(false); |
|
| 1587 | - } |
|
| 1588 | - |
|
| 1589 | - if ($this->mkdir($path . '/' . $entryName) !== false) { |
|
| 1590 | - $info = $this->getFileInfo($path . '/' . $entryName); |
|
| 1591 | - if ($info !== false) { |
|
| 1592 | - $files[$entryName] = $info; |
|
| 1593 | - } |
|
| 1594 | - } |
|
| 1595 | - |
|
| 1596 | - if ($storage->instanceOfStorage(Quota::class)) { |
|
| 1597 | - $storage->enableQuota(true); |
|
| 1598 | - } |
|
| 1599 | - } catch (\Exception $e) { |
|
| 1600 | - // Creating the parent folder might not be possible, for example due to a lack of permissions. |
|
| 1601 | - $this->logger->debug('Failed to create non-existent parent', ['exception' => $e, 'path' => $path . '/' . $entryName]); |
|
| 1602 | - } |
|
| 1603 | - } |
|
| 1604 | - |
|
| 1605 | - if (isset($files[$entryName])) { |
|
| 1606 | - $files[$entryName]->addSubEntry($rootEntry, $mountPoint); |
|
| 1607 | - } |
|
| 1608 | - } else { //mountpoint in this folder, add an entry for it |
|
| 1609 | - $rootEntry['name'] = $relativePath; |
|
| 1610 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1611 | - $permissions = $rootEntry['permissions']; |
|
| 1612 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1613 | - // for shared files/folders we use the permissions given by the owner |
|
| 1614 | - if ($mount instanceof MoveableMount) { |
|
| 1615 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1616 | - } else { |
|
| 1617 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1618 | - } |
|
| 1619 | - |
|
| 1620 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1621 | - |
|
| 1622 | - // if sharing was disabled for the user we remove the share permissions |
|
| 1623 | - if ($sharingDisabled) { |
|
| 1624 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1625 | - } |
|
| 1626 | - |
|
| 1627 | - $ownerId = $subStorage->getOwner(''); |
|
| 1628 | - if ($ownerId !== false) { |
|
| 1629 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1630 | - } else { |
|
| 1631 | - $owner = null; |
|
| 1632 | - } |
|
| 1633 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1634 | - } |
|
| 1635 | - } |
|
| 1636 | - } |
|
| 1637 | - } |
|
| 1638 | - |
|
| 1639 | - if ($mimetype_filter) { |
|
| 1640 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1641 | - if (strpos($mimetype_filter, '/')) { |
|
| 1642 | - return $file->getMimetype() === $mimetype_filter; |
|
| 1643 | - } else { |
|
| 1644 | - return $file->getMimePart() === $mimetype_filter; |
|
| 1645 | - } |
|
| 1646 | - }); |
|
| 1647 | - } |
|
| 1648 | - |
|
| 1649 | - return array_values($files); |
|
| 1650 | - } |
|
| 1651 | - |
|
| 1652 | - /** |
|
| 1653 | - * change file metadata |
|
| 1654 | - * |
|
| 1655 | - * @param string $path |
|
| 1656 | - * @param array|\OCP\Files\FileInfo $data |
|
| 1657 | - * @return int |
|
| 1658 | - * |
|
| 1659 | - * returns the fileid of the updated file |
|
| 1660 | - */ |
|
| 1661 | - public function putFileInfo($path, $data) { |
|
| 1662 | - $this->assertPathLength($path); |
|
| 1663 | - if ($data instanceof FileInfo) { |
|
| 1664 | - $data = $data->getData(); |
|
| 1665 | - } |
|
| 1666 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1667 | - /** |
|
| 1668 | - * @var Storage $storage |
|
| 1669 | - * @var string $internalPath |
|
| 1670 | - */ |
|
| 1671 | - [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 1672 | - if ($storage) { |
|
| 1673 | - $cache = $storage->getCache($path); |
|
| 1674 | - |
|
| 1675 | - if (!$cache->inCache($internalPath)) { |
|
| 1676 | - $scanner = $storage->getScanner($internalPath); |
|
| 1677 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1678 | - } |
|
| 1679 | - |
|
| 1680 | - return $cache->put($internalPath, $data); |
|
| 1681 | - } else { |
|
| 1682 | - return -1; |
|
| 1683 | - } |
|
| 1684 | - } |
|
| 1685 | - |
|
| 1686 | - /** |
|
| 1687 | - * search for files with the name matching $query |
|
| 1688 | - * |
|
| 1689 | - * @param string $query |
|
| 1690 | - * @return FileInfo[] |
|
| 1691 | - */ |
|
| 1692 | - public function search($query) { |
|
| 1693 | - return $this->searchCommon('search', ['%' . $query . '%']); |
|
| 1694 | - } |
|
| 1695 | - |
|
| 1696 | - /** |
|
| 1697 | - * search for files with the name matching $query |
|
| 1698 | - * |
|
| 1699 | - * @param string $query |
|
| 1700 | - * @return FileInfo[] |
|
| 1701 | - */ |
|
| 1702 | - public function searchRaw($query) { |
|
| 1703 | - return $this->searchCommon('search', [$query]); |
|
| 1704 | - } |
|
| 1705 | - |
|
| 1706 | - /** |
|
| 1707 | - * search for files by mimetype |
|
| 1708 | - * |
|
| 1709 | - * @param string $mimetype |
|
| 1710 | - * @return FileInfo[] |
|
| 1711 | - */ |
|
| 1712 | - public function searchByMime($mimetype) { |
|
| 1713 | - return $this->searchCommon('searchByMime', [$mimetype]); |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - /** |
|
| 1717 | - * search for files by tag |
|
| 1718 | - * |
|
| 1719 | - * @param string|int $tag name or tag id |
|
| 1720 | - * @param string $userId owner of the tags |
|
| 1721 | - * @return FileInfo[] |
|
| 1722 | - */ |
|
| 1723 | - public function searchByTag($tag, $userId) { |
|
| 1724 | - return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
| 1725 | - } |
|
| 1726 | - |
|
| 1727 | - /** |
|
| 1728 | - * @param string $method cache method |
|
| 1729 | - * @param array $args |
|
| 1730 | - * @return FileInfo[] |
|
| 1731 | - */ |
|
| 1732 | - private function searchCommon($method, $args) { |
|
| 1733 | - $files = []; |
|
| 1734 | - $rootLength = strlen($this->fakeRoot); |
|
| 1735 | - |
|
| 1736 | - $mount = $this->getMount(''); |
|
| 1737 | - $mountPoint = $mount->getMountPoint(); |
|
| 1738 | - $storage = $mount->getStorage(); |
|
| 1739 | - $userManager = \OC::$server->getUserManager(); |
|
| 1740 | - if ($storage) { |
|
| 1741 | - $cache = $storage->getCache(''); |
|
| 1742 | - |
|
| 1743 | - $results = call_user_func_array([$cache, $method], $args); |
|
| 1744 | - foreach ($results as $result) { |
|
| 1745 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1746 | - $internalPath = $result['path']; |
|
| 1747 | - $path = $mountPoint . $result['path']; |
|
| 1748 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1749 | - $ownerId = $storage->getOwner($internalPath); |
|
| 1750 | - if ($ownerId !== false) { |
|
| 1751 | - $owner = $userManager->get($ownerId); |
|
| 1752 | - } else { |
|
| 1753 | - $owner = null; |
|
| 1754 | - } |
|
| 1755 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1756 | - } |
|
| 1757 | - } |
|
| 1758 | - |
|
| 1759 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1760 | - foreach ($mounts as $mount) { |
|
| 1761 | - $mountPoint = $mount->getMountPoint(); |
|
| 1762 | - $storage = $mount->getStorage(); |
|
| 1763 | - if ($storage) { |
|
| 1764 | - $cache = $storage->getCache(''); |
|
| 1765 | - |
|
| 1766 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1767 | - $results = call_user_func_array([$cache, $method], $args); |
|
| 1768 | - if ($results) { |
|
| 1769 | - foreach ($results as $result) { |
|
| 1770 | - $internalPath = $result['path']; |
|
| 1771 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1772 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1773 | - $ownerId = $storage->getOwner($internalPath); |
|
| 1774 | - if ($ownerId !== false) { |
|
| 1775 | - $owner = $userManager->get($ownerId); |
|
| 1776 | - } else { |
|
| 1777 | - $owner = null; |
|
| 1778 | - } |
|
| 1779 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1780 | - } |
|
| 1781 | - } |
|
| 1782 | - } |
|
| 1783 | - } |
|
| 1784 | - } |
|
| 1785 | - return $files; |
|
| 1786 | - } |
|
| 1787 | - |
|
| 1788 | - /** |
|
| 1789 | - * Get the owner for a file or folder |
|
| 1790 | - * |
|
| 1791 | - * @throws NotFoundException |
|
| 1792 | - */ |
|
| 1793 | - public function getOwner(string $path): string { |
|
| 1794 | - $info = $this->getFileInfo($path); |
|
| 1795 | - if (!$info) { |
|
| 1796 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1797 | - } |
|
| 1798 | - |
|
| 1799 | - if ($info->getOwner() === null) { |
|
| 1800 | - throw new NotFoundException($path . ' has no owner'); |
|
| 1801 | - } |
|
| 1802 | - |
|
| 1803 | - return $info->getOwner()->getUID(); |
|
| 1804 | - } |
|
| 1805 | - |
|
| 1806 | - /** |
|
| 1807 | - * get the ETag for a file or folder |
|
| 1808 | - * |
|
| 1809 | - * @param string $path |
|
| 1810 | - * @return string|false |
|
| 1811 | - */ |
|
| 1812 | - public function getETag($path) { |
|
| 1813 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1814 | - if ($storage) { |
|
| 1815 | - return $storage->getETag($internalPath); |
|
| 1816 | - } else { |
|
| 1817 | - return false; |
|
| 1818 | - } |
|
| 1819 | - } |
|
| 1820 | - |
|
| 1821 | - /** |
|
| 1822 | - * Get the path of a file by id, relative to the view |
|
| 1823 | - * |
|
| 1824 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 1825 | - * |
|
| 1826 | - * @param int $id |
|
| 1827 | - * @param int|null $storageId |
|
| 1828 | - * @return string |
|
| 1829 | - * @throws NotFoundException |
|
| 1830 | - */ |
|
| 1831 | - public function getPath($id, ?int $storageId = null): string { |
|
| 1832 | - $id = (int)$id; |
|
| 1833 | - $rootFolder = Server::get(Files\IRootFolder::class); |
|
| 1834 | - |
|
| 1835 | - $node = $rootFolder->getFirstNodeByIdInPath($id, $this->getRoot()); |
|
| 1836 | - if ($node) { |
|
| 1837 | - if ($storageId === null || $storageId === $node->getStorage()->getCache()->getNumericStorageId()) { |
|
| 1838 | - return $this->getRelativePath($node->getPath()) ?? ''; |
|
| 1839 | - } |
|
| 1840 | - } else { |
|
| 1841 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1842 | - } |
|
| 1843 | - |
|
| 1844 | - foreach ($rootFolder->getByIdInPath($id, $this->getRoot()) as $node) { |
|
| 1845 | - if ($storageId === $node->getStorage()->getCache()->getNumericStorageId()) { |
|
| 1846 | - return $this->getRelativePath($node->getPath()) ?? ''; |
|
| 1847 | - } |
|
| 1848 | - } |
|
| 1849 | - |
|
| 1850 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1851 | - } |
|
| 1852 | - |
|
| 1853 | - /** |
|
| 1854 | - * @param string $path |
|
| 1855 | - * @throws InvalidPathException |
|
| 1856 | - */ |
|
| 1857 | - private function assertPathLength($path): void { |
|
| 1858 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1859 | - // Check for the string length - performed using isset() instead of strlen() |
|
| 1860 | - // because isset() is about 5x-40x faster. |
|
| 1861 | - if (isset($path[$maxLen])) { |
|
| 1862 | - $pathLen = strlen($path); |
|
| 1863 | - throw new InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1864 | - } |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - /** |
|
| 1868 | - * check if it is allowed to move a mount point to a given target. |
|
| 1869 | - * It is not allowed to move a mount point into a different mount point or |
|
| 1870 | - * into an already shared folder |
|
| 1871 | - */ |
|
| 1872 | - private function targetIsNotShared(string $user, string $targetPath): bool { |
|
| 1873 | - $providers = [ |
|
| 1874 | - IShare::TYPE_USER, |
|
| 1875 | - IShare::TYPE_GROUP, |
|
| 1876 | - IShare::TYPE_EMAIL, |
|
| 1877 | - IShare::TYPE_CIRCLE, |
|
| 1878 | - IShare::TYPE_ROOM, |
|
| 1879 | - IShare::TYPE_DECK, |
|
| 1880 | - IShare::TYPE_SCIENCEMESH |
|
| 1881 | - ]; |
|
| 1882 | - $shareManager = Server::get(IManager::class); |
|
| 1883 | - /** @var IShare[] $shares */ |
|
| 1884 | - $shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) { |
|
| 1885 | - return $shareManager->getSharesBy($user, $type); |
|
| 1886 | - }, $providers)); |
|
| 1887 | - |
|
| 1888 | - foreach ($shares as $share) { |
|
| 1889 | - $sharedPath = $share->getNode()->getPath(); |
|
| 1890 | - if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath . '/')) { |
|
| 1891 | - $this->logger->debug( |
|
| 1892 | - 'It is not allowed to move one mount point into a shared folder', |
|
| 1893 | - ['app' => 'files']); |
|
| 1894 | - return false; |
|
| 1895 | - } |
|
| 1896 | - } |
|
| 1897 | - |
|
| 1898 | - return true; |
|
| 1899 | - } |
|
| 1900 | - |
|
| 1901 | - /** |
|
| 1902 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1903 | - */ |
|
| 1904 | - private function getPartFileInfo(string $path): \OC\Files\FileInfo { |
|
| 1905 | - $mount = $this->getMount($path); |
|
| 1906 | - $storage = $mount->getStorage(); |
|
| 1907 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1908 | - $ownerId = $storage->getOwner($internalPath); |
|
| 1909 | - if ($ownerId !== false) { |
|
| 1910 | - $owner = Server::get(IUserManager::class)->get($ownerId); |
|
| 1911 | - } else { |
|
| 1912 | - $owner = null; |
|
| 1913 | - } |
|
| 1914 | - return new FileInfo( |
|
| 1915 | - $this->getAbsolutePath($path), |
|
| 1916 | - $storage, |
|
| 1917 | - $internalPath, |
|
| 1918 | - [ |
|
| 1919 | - 'fileid' => null, |
|
| 1920 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1921 | - 'name' => basename($path), |
|
| 1922 | - 'etag' => null, |
|
| 1923 | - 'size' => $storage->filesize($internalPath), |
|
| 1924 | - 'mtime' => $storage->filemtime($internalPath), |
|
| 1925 | - 'encrypted' => false, |
|
| 1926 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1927 | - ], |
|
| 1928 | - $mount, |
|
| 1929 | - $owner |
|
| 1930 | - ); |
|
| 1931 | - } |
|
| 1932 | - |
|
| 1933 | - /** |
|
| 1934 | - * @param string $path |
|
| 1935 | - * @param string $fileName |
|
| 1936 | - * @param bool $readonly Check only if the path is allowed for read-only access |
|
| 1937 | - * @throws InvalidPathException |
|
| 1938 | - */ |
|
| 1939 | - public function verifyPath($path, $fileName, $readonly = false): void { |
|
| 1940 | - // All of the view's functions disallow '..' in the path so we can short cut if the path is invalid |
|
| 1941 | - if (!Filesystem::isValidPath($path ?: '/')) { |
|
| 1942 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1943 | - throw new InvalidPathException($l->t('Path contains invalid segments')); |
|
| 1944 | - } |
|
| 1945 | - |
|
| 1946 | - // Short cut for read-only validation |
|
| 1947 | - if ($readonly) { |
|
| 1948 | - $validator = Server::get(FilenameValidator::class); |
|
| 1949 | - if ($validator->isForbidden($fileName)) { |
|
| 1950 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1951 | - throw new InvalidPathException($l->t('Filename is a reserved word')); |
|
| 1952 | - } |
|
| 1953 | - return; |
|
| 1954 | - } |
|
| 1955 | - |
|
| 1956 | - try { |
|
| 1957 | - /** @type \OCP\Files\Storage $storage */ |
|
| 1958 | - [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1959 | - $storage->verifyPath($internalPath, $fileName); |
|
| 1960 | - } catch (ReservedWordException $ex) { |
|
| 1961 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1962 | - throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename is a reserved word')); |
|
| 1963 | - } catch (InvalidCharacterInPathException $ex) { |
|
| 1964 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1965 | - throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename contains at least one invalid character')); |
|
| 1966 | - } catch (FileNameTooLongException $ex) { |
|
| 1967 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1968 | - throw new InvalidPathException($l->t('Filename is too long')); |
|
| 1969 | - } catch (InvalidDirectoryException $ex) { |
|
| 1970 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1971 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1972 | - } catch (EmptyFileNameException $ex) { |
|
| 1973 | - $l = \OCP\Util::getL10N('lib'); |
|
| 1974 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1975 | - } |
|
| 1976 | - } |
|
| 1977 | - |
|
| 1978 | - /** |
|
| 1979 | - * get all parent folders of $path |
|
| 1980 | - * |
|
| 1981 | - * @param string $path |
|
| 1982 | - * @return string[] |
|
| 1983 | - */ |
|
| 1984 | - private function getParents($path) { |
|
| 1985 | - $path = trim($path, '/'); |
|
| 1986 | - if (!$path) { |
|
| 1987 | - return []; |
|
| 1988 | - } |
|
| 1989 | - |
|
| 1990 | - $parts = explode('/', $path); |
|
| 1991 | - |
|
| 1992 | - // remove the single file |
|
| 1993 | - array_pop($parts); |
|
| 1994 | - $result = ['/']; |
|
| 1995 | - $resultPath = ''; |
|
| 1996 | - foreach ($parts as $part) { |
|
| 1997 | - if ($part) { |
|
| 1998 | - $resultPath .= '/' . $part; |
|
| 1999 | - $result[] = $resultPath; |
|
| 2000 | - } |
|
| 2001 | - } |
|
| 2002 | - return $result; |
|
| 2003 | - } |
|
| 2004 | - |
|
| 2005 | - /** |
|
| 2006 | - * Returns the mount point for which to lock |
|
| 2007 | - * |
|
| 2008 | - * @param string $absolutePath absolute path |
|
| 2009 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 2010 | - * is mounted directly on the given path, false otherwise |
|
| 2011 | - * @return IMountPoint mount point for which to apply locks |
|
| 2012 | - */ |
|
| 2013 | - private function getMountForLock(string $absolutePath, bool $useParentMount = false): IMountPoint { |
|
| 2014 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 2015 | - |
|
| 2016 | - if ($useParentMount) { |
|
| 2017 | - // find out if something is mounted directly on the path |
|
| 2018 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 2019 | - if ($internalPath === '') { |
|
| 2020 | - // resolve the parent mount instead |
|
| 2021 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 2022 | - } |
|
| 2023 | - } |
|
| 2024 | - |
|
| 2025 | - return $mount; |
|
| 2026 | - } |
|
| 2027 | - |
|
| 2028 | - /** |
|
| 2029 | - * Lock the given path |
|
| 2030 | - * |
|
| 2031 | - * @param string $path the path of the file to lock, relative to the view |
|
| 2032 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2033 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2034 | - * |
|
| 2035 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2036 | - * @throws LockedException if the path is already locked |
|
| 2037 | - */ |
|
| 2038 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 2039 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2040 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2041 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2042 | - return false; |
|
| 2043 | - } |
|
| 2044 | - |
|
| 2045 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2046 | - try { |
|
| 2047 | - $storage = $mount->getStorage(); |
|
| 2048 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2049 | - $storage->acquireLock( |
|
| 2050 | - $mount->getInternalPath($absolutePath), |
|
| 2051 | - $type, |
|
| 2052 | - $this->lockingProvider |
|
| 2053 | - ); |
|
| 2054 | - } |
|
| 2055 | - } catch (LockedException $e) { |
|
| 2056 | - // rethrow with the human-readable path |
|
| 2057 | - throw new LockedException( |
|
| 2058 | - $path, |
|
| 2059 | - $e, |
|
| 2060 | - $e->getExistingLock() |
|
| 2061 | - ); |
|
| 2062 | - } |
|
| 2063 | - |
|
| 2064 | - return true; |
|
| 2065 | - } |
|
| 2066 | - |
|
| 2067 | - /** |
|
| 2068 | - * Change the lock type |
|
| 2069 | - * |
|
| 2070 | - * @param string $path the path of the file to lock, relative to the view |
|
| 2071 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2072 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2073 | - * |
|
| 2074 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2075 | - * @throws LockedException if the path is already locked |
|
| 2076 | - */ |
|
| 2077 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 2078 | - $path = Filesystem::normalizePath($path); |
|
| 2079 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2080 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2081 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2082 | - return false; |
|
| 2083 | - } |
|
| 2084 | - |
|
| 2085 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2086 | - try { |
|
| 2087 | - $storage = $mount->getStorage(); |
|
| 2088 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2089 | - $storage->changeLock( |
|
| 2090 | - $mount->getInternalPath($absolutePath), |
|
| 2091 | - $type, |
|
| 2092 | - $this->lockingProvider |
|
| 2093 | - ); |
|
| 2094 | - } |
|
| 2095 | - } catch (LockedException $e) { |
|
| 2096 | - // rethrow with the a human-readable path |
|
| 2097 | - throw new LockedException( |
|
| 2098 | - $path, |
|
| 2099 | - $e, |
|
| 2100 | - $e->getExistingLock() |
|
| 2101 | - ); |
|
| 2102 | - } |
|
| 2103 | - |
|
| 2104 | - return true; |
|
| 2105 | - } |
|
| 2106 | - |
|
| 2107 | - /** |
|
| 2108 | - * Unlock the given path |
|
| 2109 | - * |
|
| 2110 | - * @param string $path the path of the file to unlock, relative to the view |
|
| 2111 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2112 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2113 | - * |
|
| 2114 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2115 | - * @throws LockedException |
|
| 2116 | - */ |
|
| 2117 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2118 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2119 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2120 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2121 | - return false; |
|
| 2122 | - } |
|
| 2123 | - |
|
| 2124 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2125 | - $storage = $mount->getStorage(); |
|
| 2126 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2127 | - $storage->releaseLock( |
|
| 2128 | - $mount->getInternalPath($absolutePath), |
|
| 2129 | - $type, |
|
| 2130 | - $this->lockingProvider |
|
| 2131 | - ); |
|
| 2132 | - } |
|
| 2133 | - |
|
| 2134 | - return true; |
|
| 2135 | - } |
|
| 2136 | - |
|
| 2137 | - /** |
|
| 2138 | - * Lock a path and all its parents up to the root of the view |
|
| 2139 | - * |
|
| 2140 | - * @param string $path the path of the file to lock relative to the view |
|
| 2141 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2142 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2143 | - * |
|
| 2144 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2145 | - * @throws LockedException |
|
| 2146 | - */ |
|
| 2147 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2148 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2149 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2150 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2151 | - return false; |
|
| 2152 | - } |
|
| 2153 | - |
|
| 2154 | - $this->lockPath($path, $type, $lockMountPoint); |
|
| 2155 | - |
|
| 2156 | - $parents = $this->getParents($path); |
|
| 2157 | - foreach ($parents as $parent) { |
|
| 2158 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2159 | - } |
|
| 2160 | - |
|
| 2161 | - return true; |
|
| 2162 | - } |
|
| 2163 | - |
|
| 2164 | - /** |
|
| 2165 | - * Unlock a path and all its parents up to the root of the view |
|
| 2166 | - * |
|
| 2167 | - * @param string $path the path of the file to lock relative to the view |
|
| 2168 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2169 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2170 | - * |
|
| 2171 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2172 | - * @throws LockedException |
|
| 2173 | - */ |
|
| 2174 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2175 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2176 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2177 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2178 | - return false; |
|
| 2179 | - } |
|
| 2180 | - |
|
| 2181 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2182 | - |
|
| 2183 | - $parents = $this->getParents($path); |
|
| 2184 | - foreach ($parents as $parent) { |
|
| 2185 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2186 | - } |
|
| 2187 | - |
|
| 2188 | - return true; |
|
| 2189 | - } |
|
| 2190 | - |
|
| 2191 | - /** |
|
| 2192 | - * Only lock files in data/user/files/ |
|
| 2193 | - * |
|
| 2194 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2195 | - * @return bool |
|
| 2196 | - */ |
|
| 2197 | - protected function shouldLockFile($path) { |
|
| 2198 | - $path = Filesystem::normalizePath($path); |
|
| 2199 | - |
|
| 2200 | - $pathSegments = explode('/', $path); |
|
| 2201 | - if (isset($pathSegments[2])) { |
|
| 2202 | - // E.g.: /username/files/path-to-file |
|
| 2203 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2204 | - } |
|
| 2205 | - |
|
| 2206 | - return !str_starts_with($path, '/appdata_'); |
|
| 2207 | - } |
|
| 2208 | - |
|
| 2209 | - /** |
|
| 2210 | - * Shortens the given absolute path to be relative to |
|
| 2211 | - * "$user/files". |
|
| 2212 | - * |
|
| 2213 | - * @param string $absolutePath absolute path which is under "files" |
|
| 2214 | - * |
|
| 2215 | - * @return string path relative to "files" with trimmed slashes or null |
|
| 2216 | - * if the path was NOT relative to files |
|
| 2217 | - * |
|
| 2218 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2219 | - * @since 8.1.0 |
|
| 2220 | - */ |
|
| 2221 | - public function getPathRelativeToFiles($absolutePath) { |
|
| 2222 | - $path = Filesystem::normalizePath($absolutePath); |
|
| 2223 | - $parts = explode('/', trim($path, '/'), 3); |
|
| 2224 | - // "$user", "files", "path/to/dir" |
|
| 2225 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2226 | - $this->logger->error( |
|
| 2227 | - '$absolutePath must be relative to "files", value is "{absolutePath}"', |
|
| 2228 | - [ |
|
| 2229 | - 'absolutePath' => $absolutePath, |
|
| 2230 | - ] |
|
| 2231 | - ); |
|
| 2232 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2233 | - } |
|
| 2234 | - if (isset($parts[2])) { |
|
| 2235 | - return $parts[2]; |
|
| 2236 | - } |
|
| 2237 | - return ''; |
|
| 2238 | - } |
|
| 2239 | - |
|
| 2240 | - /** |
|
| 2241 | - * @param string $filename |
|
| 2242 | - * @return array |
|
| 2243 | - * @throws \OC\User\NoUserException |
|
| 2244 | - * @throws NotFoundException |
|
| 2245 | - */ |
|
| 2246 | - public function getUidAndFilename($filename) { |
|
| 2247 | - $info = $this->getFileInfo($filename); |
|
| 2248 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2249 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2250 | - } |
|
| 2251 | - $uid = $info->getOwner()->getUID(); |
|
| 2252 | - if ($uid != \OC_User::getUser()) { |
|
| 2253 | - Filesystem::initMountPoints($uid); |
|
| 2254 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2255 | - try { |
|
| 2256 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 2257 | - } catch (NotFoundException $e) { |
|
| 2258 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2259 | - } |
|
| 2260 | - } |
|
| 2261 | - return [$uid, $filename]; |
|
| 2262 | - } |
|
| 2263 | - |
|
| 2264 | - /** |
|
| 2265 | - * Creates parent non-existing folders |
|
| 2266 | - * |
|
| 2267 | - * @param string $filePath |
|
| 2268 | - * @return bool |
|
| 2269 | - */ |
|
| 2270 | - private function createParentDirectories($filePath) { |
|
| 2271 | - $directoryParts = explode('/', $filePath); |
|
| 2272 | - $directoryParts = array_filter($directoryParts); |
|
| 2273 | - foreach ($directoryParts as $key => $part) { |
|
| 2274 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2275 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2276 | - if ($this->is_file($currentPath)) { |
|
| 2277 | - return false; |
|
| 2278 | - } |
|
| 2279 | - if (!$this->file_exists($currentPath)) { |
|
| 2280 | - $this->mkdir($currentPath); |
|
| 2281 | - } |
|
| 2282 | - } |
|
| 2283 | - |
|
| 2284 | - return true; |
|
| 2285 | - } |
|
| 60 | + private string $fakeRoot = ''; |
|
| 61 | + private ILockingProvider $lockingProvider; |
|
| 62 | + private bool $lockingEnabled; |
|
| 63 | + private bool $updaterEnabled = true; |
|
| 64 | + private UserManager $userManager; |
|
| 65 | + private LoggerInterface $logger; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @throws \Exception If $root contains an invalid path |
|
| 69 | + */ |
|
| 70 | + public function __construct(string $root = '') { |
|
| 71 | + if (!Filesystem::isValidPath($root)) { |
|
| 72 | + throw new \Exception(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $this->fakeRoot = $root; |
|
| 76 | + $this->lockingProvider = \OC::$server->get(ILockingProvider::class); |
|
| 77 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 78 | + $this->userManager = \OC::$server->getUserManager(); |
|
| 79 | + $this->logger = \OC::$server->get(LoggerInterface::class); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param ?string $path |
|
| 84 | + * @psalm-template S as string|null |
|
| 85 | + * @psalm-param S $path |
|
| 86 | + * @psalm-return (S is string ? string : null) |
|
| 87 | + */ |
|
| 88 | + public function getAbsolutePath($path = '/'): ?string { |
|
| 89 | + if ($path === null) { |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + $this->assertPathLength($path); |
|
| 93 | + if ($path === '') { |
|
| 94 | + $path = '/'; |
|
| 95 | + } |
|
| 96 | + if ($path[0] !== '/') { |
|
| 97 | + $path = '/' . $path; |
|
| 98 | + } |
|
| 99 | + return $this->fakeRoot . $path; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Change the root to a fake root |
|
| 104 | + * |
|
| 105 | + * @param string $fakeRoot |
|
| 106 | + */ |
|
| 107 | + public function chroot($fakeRoot): void { |
|
| 108 | + if (!$fakeRoot == '') { |
|
| 109 | + if ($fakeRoot[0] !== '/') { |
|
| 110 | + $fakeRoot = '/' . $fakeRoot; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + $this->fakeRoot = $fakeRoot; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Get the fake root |
|
| 118 | + */ |
|
| 119 | + public function getRoot(): string { |
|
| 120 | + return $this->fakeRoot; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * get path relative to the root of the view |
|
| 125 | + * |
|
| 126 | + * @param string $path |
|
| 127 | + */ |
|
| 128 | + public function getRelativePath($path): ?string { |
|
| 129 | + $this->assertPathLength($path); |
|
| 130 | + if ($this->fakeRoot == '') { |
|
| 131 | + return $path; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 135 | + return '/'; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // missing slashes can cause wrong matches! |
|
| 139 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 140 | + |
|
| 141 | + if (!str_starts_with($path, $root)) { |
|
| 142 | + return null; |
|
| 143 | + } else { |
|
| 144 | + $path = substr($path, strlen($this->fakeRoot)); |
|
| 145 | + if (strlen($path) === 0) { |
|
| 146 | + return '/'; |
|
| 147 | + } else { |
|
| 148 | + return $path; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the mountpoint of the storage object for a path |
|
| 155 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 156 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 157 | + * and does not take the chroot into account ) |
|
| 158 | + * |
|
| 159 | + * @param string $path |
|
| 160 | + */ |
|
| 161 | + public function getMountPoint($path): string { |
|
| 162 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the mountpoint of the storage object for a path |
|
| 167 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 168 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 169 | + * and does not take the chroot into account ) |
|
| 170 | + * |
|
| 171 | + * @param string $path |
|
| 172 | + */ |
|
| 173 | + public function getMount($path): IMountPoint { |
|
| 174 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Resolve a path to a storage and internal path |
|
| 179 | + * |
|
| 180 | + * @param string $path |
|
| 181 | + * @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path |
|
| 182 | + */ |
|
| 183 | + public function resolvePath($path): array { |
|
| 184 | + $a = $this->getAbsolutePath($path); |
|
| 185 | + $p = Filesystem::normalizePath($a); |
|
| 186 | + return Filesystem::resolvePath($p); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Return the path to a local version of the file |
|
| 191 | + * we need this because we can't know if a file is stored local or not from |
|
| 192 | + * outside the filestorage and for some purposes a local file is needed |
|
| 193 | + * |
|
| 194 | + * @param string $path |
|
| 195 | + */ |
|
| 196 | + public function getLocalFile($path): string|false { |
|
| 197 | + $parent = substr($path, 0, strrpos($path, '/') ?: 0); |
|
| 198 | + $path = $this->getAbsolutePath($path); |
|
| 199 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 200 | + if (Filesystem::isValidPath($parent) && $storage) { |
|
| 201 | + return $storage->getLocalFile($internalPath); |
|
| 202 | + } else { |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * the following functions operate with arguments and return values identical |
|
| 209 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 210 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 211 | + */ |
|
| 212 | + public function mkdir($path) { |
|
| 213 | + return $this->basicOperation('mkdir', $path, ['create', 'write']); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * remove mount point |
|
| 218 | + * |
|
| 219 | + * @param IMountPoint $mount |
|
| 220 | + * @param string $path relative to data/ |
|
| 221 | + */ |
|
| 222 | + protected function removeMount($mount, $path): bool { |
|
| 223 | + if ($mount instanceof MoveableMount) { |
|
| 224 | + // cut of /user/files to get the relative path to data/user/files |
|
| 225 | + $pathParts = explode('/', $path, 4); |
|
| 226 | + $relPath = '/' . $pathParts[3]; |
|
| 227 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 228 | + \OC_Hook::emit( |
|
| 229 | + Filesystem::CLASSNAME, 'umount', |
|
| 230 | + [Filesystem::signal_param_path => $relPath] |
|
| 231 | + ); |
|
| 232 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 233 | + $result = $mount->removeMount(); |
|
| 234 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 235 | + if ($result) { |
|
| 236 | + \OC_Hook::emit( |
|
| 237 | + Filesystem::CLASSNAME, 'post_umount', |
|
| 238 | + [Filesystem::signal_param_path => $relPath] |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 242 | + return $result; |
|
| 243 | + } else { |
|
| 244 | + // do not allow deleting the storage's root / the mount point |
|
| 245 | + // because for some storages it might delete the whole contents |
|
| 246 | + // but isn't supposed to work that way |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + public function disableCacheUpdate(): void { |
|
| 252 | + $this->updaterEnabled = false; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + public function enableCacheUpdate(): void { |
|
| 256 | + $this->updaterEnabled = true; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void { |
|
| 260 | + if ($this->updaterEnabled) { |
|
| 261 | + if (is_null($time)) { |
|
| 262 | + $time = time(); |
|
| 263 | + } |
|
| 264 | + $storage->getUpdater()->update($internalPath, $time, $sizeDifference); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + protected function removeUpdate(Storage $storage, string $internalPath): void { |
|
| 269 | + if ($this->updaterEnabled) { |
|
| 270 | + $storage->getUpdater()->remove($internalPath); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void { |
|
| 275 | + if ($this->updaterEnabled) { |
|
| 276 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void { |
|
| 281 | + if ($this->updaterEnabled) { |
|
| 282 | + $targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param string $path |
|
| 288 | + * @return bool|mixed |
|
| 289 | + */ |
|
| 290 | + public function rmdir($path) { |
|
| 291 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 292 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 293 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 294 | + return $this->removeMount($mount, $absolutePath); |
|
| 295 | + } |
|
| 296 | + if ($this->is_dir($path)) { |
|
| 297 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 298 | + } else { |
|
| 299 | + $result = false; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 303 | + $storage = $mount->getStorage(); |
|
| 304 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 305 | + $storage->getUpdater()->remove($internalPath); |
|
| 306 | + } |
|
| 307 | + return $result; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param string $path |
|
| 312 | + * @return resource|false |
|
| 313 | + */ |
|
| 314 | + public function opendir($path) { |
|
| 315 | + return $this->basicOperation('opendir', $path, ['read']); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @param string $path |
|
| 320 | + * @return bool|mixed |
|
| 321 | + */ |
|
| 322 | + public function is_dir($path) { |
|
| 323 | + if ($path == '/') { |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + return $this->basicOperation('is_dir', $path); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @param string $path |
|
| 331 | + * @return bool|mixed |
|
| 332 | + */ |
|
| 333 | + public function is_file($path) { |
|
| 334 | + if ($path == '/') { |
|
| 335 | + return false; |
|
| 336 | + } |
|
| 337 | + return $this->basicOperation('is_file', $path); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @param string $path |
|
| 342 | + * @return mixed |
|
| 343 | + */ |
|
| 344 | + public function stat($path) { |
|
| 345 | + return $this->basicOperation('stat', $path); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * @param string $path |
|
| 350 | + * @return mixed |
|
| 351 | + */ |
|
| 352 | + public function filetype($path) { |
|
| 353 | + return $this->basicOperation('filetype', $path); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * @param string $path |
|
| 358 | + * @return mixed |
|
| 359 | + */ |
|
| 360 | + public function filesize(string $path) { |
|
| 361 | + return $this->basicOperation('filesize', $path); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * @param string $path |
|
| 366 | + * @return bool|mixed |
|
| 367 | + * @throws InvalidPathException |
|
| 368 | + */ |
|
| 369 | + public function readfile($path) { |
|
| 370 | + $this->assertPathLength($path); |
|
| 371 | + if (ob_get_level()) { |
|
| 372 | + ob_end_clean(); |
|
| 373 | + } |
|
| 374 | + $handle = $this->fopen($path, 'rb'); |
|
| 375 | + if ($handle) { |
|
| 376 | + $chunkSize = 524288; // 512 kiB chunks |
|
| 377 | + while (!feof($handle)) { |
|
| 378 | + echo fread($handle, $chunkSize); |
|
| 379 | + flush(); |
|
| 380 | + $this->checkConnectionStatus(); |
|
| 381 | + } |
|
| 382 | + fclose($handle); |
|
| 383 | + return $this->filesize($path); |
|
| 384 | + } |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * @param string $path |
|
| 390 | + * @param int $from |
|
| 391 | + * @param int $to |
|
| 392 | + * @return bool|mixed |
|
| 393 | + * @throws InvalidPathException |
|
| 394 | + * @throws \OCP\Files\UnseekableException |
|
| 395 | + */ |
|
| 396 | + public function readfilePart($path, $from, $to) { |
|
| 397 | + $this->assertPathLength($path); |
|
| 398 | + if (ob_get_level()) { |
|
| 399 | + ob_end_clean(); |
|
| 400 | + } |
|
| 401 | + $handle = $this->fopen($path, 'rb'); |
|
| 402 | + if ($handle) { |
|
| 403 | + $chunkSize = 524288; // 512 kiB chunks |
|
| 404 | + $startReading = true; |
|
| 405 | + |
|
| 406 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 407 | + // forward file handle via chunked fread because fseek seem to have failed |
|
| 408 | + |
|
| 409 | + $end = $from + 1; |
|
| 410 | + while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) { |
|
| 411 | + $len = $from - ftell($handle); |
|
| 412 | + if ($len > $chunkSize) { |
|
| 413 | + $len = $chunkSize; |
|
| 414 | + } |
|
| 415 | + $result = fread($handle, $len); |
|
| 416 | + |
|
| 417 | + if ($result === false) { |
|
| 418 | + $startReading = false; |
|
| 419 | + break; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + if ($startReading) { |
|
| 425 | + $end = $to + 1; |
|
| 426 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 427 | + $len = $end - ftell($handle); |
|
| 428 | + if ($len > $chunkSize) { |
|
| 429 | + $len = $chunkSize; |
|
| 430 | + } |
|
| 431 | + echo fread($handle, $len); |
|
| 432 | + flush(); |
|
| 433 | + $this->checkConnectionStatus(); |
|
| 434 | + } |
|
| 435 | + return ftell($handle) - $from; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 439 | + } |
|
| 440 | + return false; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + private function checkConnectionStatus(): void { |
|
| 444 | + $connectionStatus = \connection_status(); |
|
| 445 | + if ($connectionStatus !== CONNECTION_NORMAL) { |
|
| 446 | + throw new ConnectionLostException("Connection lost. Status: $connectionStatus"); |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * @param string $path |
|
| 452 | + * @return mixed |
|
| 453 | + */ |
|
| 454 | + public function isCreatable($path) { |
|
| 455 | + return $this->basicOperation('isCreatable', $path); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * @param string $path |
|
| 460 | + * @return mixed |
|
| 461 | + */ |
|
| 462 | + public function isReadable($path) { |
|
| 463 | + return $this->basicOperation('isReadable', $path); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * @param string $path |
|
| 468 | + * @return mixed |
|
| 469 | + */ |
|
| 470 | + public function isUpdatable($path) { |
|
| 471 | + return $this->basicOperation('isUpdatable', $path); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * @param string $path |
|
| 476 | + * @return bool|mixed |
|
| 477 | + */ |
|
| 478 | + public function isDeletable($path) { |
|
| 479 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 480 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 481 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 482 | + return $mount instanceof MoveableMount; |
|
| 483 | + } |
|
| 484 | + return $this->basicOperation('isDeletable', $path); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * @param string $path |
|
| 489 | + * @return mixed |
|
| 490 | + */ |
|
| 491 | + public function isSharable($path) { |
|
| 492 | + return $this->basicOperation('isSharable', $path); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * @param string $path |
|
| 497 | + * @return bool|mixed |
|
| 498 | + */ |
|
| 499 | + public function file_exists($path) { |
|
| 500 | + if ($path == '/') { |
|
| 501 | + return true; |
|
| 502 | + } |
|
| 503 | + return $this->basicOperation('file_exists', $path); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * @param string $path |
|
| 508 | + * @return mixed |
|
| 509 | + */ |
|
| 510 | + public function filemtime($path) { |
|
| 511 | + return $this->basicOperation('filemtime', $path); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * @param string $path |
|
| 516 | + * @param int|string $mtime |
|
| 517 | + */ |
|
| 518 | + public function touch($path, $mtime = null): bool { |
|
| 519 | + if (!is_null($mtime) && !is_numeric($mtime)) { |
|
| 520 | + $mtime = strtotime($mtime); |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + $hooks = ['touch']; |
|
| 524 | + |
|
| 525 | + if (!$this->file_exists($path)) { |
|
| 526 | + $hooks[] = 'create'; |
|
| 527 | + $hooks[] = 'write'; |
|
| 528 | + } |
|
| 529 | + try { |
|
| 530 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 531 | + } catch (\Exception $e) { |
|
| 532 | + $this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]); |
|
| 533 | + $result = false; |
|
| 534 | + } |
|
| 535 | + if (!$result) { |
|
| 536 | + // If create file fails because of permissions on external storage like SMB folders, |
|
| 537 | + // check file exists and return false if not. |
|
| 538 | + if (!$this->file_exists($path)) { |
|
| 539 | + return false; |
|
| 540 | + } |
|
| 541 | + if (is_null($mtime)) { |
|
| 542 | + $mtime = time(); |
|
| 543 | + } |
|
| 544 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 545 | + $this->putFileInfo($path, ['mtime' => floor($mtime)]); |
|
| 546 | + } |
|
| 547 | + return true; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * @param string $path |
|
| 552 | + * @return string|false |
|
| 553 | + * @throws LockedException |
|
| 554 | + */ |
|
| 555 | + public function file_get_contents($path) { |
|
| 556 | + return $this->basicOperation('file_get_contents', $path, ['read']); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + protected function emit_file_hooks_pre(bool $exists, string $path, bool &$run): void { |
|
| 560 | + if (!$exists) { |
|
| 561 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [ |
|
| 562 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 563 | + Filesystem::signal_param_run => &$run, |
|
| 564 | + ]); |
|
| 565 | + } else { |
|
| 566 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [ |
|
| 567 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 568 | + Filesystem::signal_param_run => &$run, |
|
| 569 | + ]); |
|
| 570 | + } |
|
| 571 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [ |
|
| 572 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 573 | + Filesystem::signal_param_run => &$run, |
|
| 574 | + ]); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + protected function emit_file_hooks_post(bool $exists, string $path): void { |
|
| 578 | + if (!$exists) { |
|
| 579 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [ |
|
| 580 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 581 | + ]); |
|
| 582 | + } else { |
|
| 583 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [ |
|
| 584 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 585 | + ]); |
|
| 586 | + } |
|
| 587 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [ |
|
| 588 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 589 | + ]); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * @param string $path |
|
| 594 | + * @param string|resource $data |
|
| 595 | + * @return bool|mixed |
|
| 596 | + * @throws LockedException |
|
| 597 | + */ |
|
| 598 | + public function file_put_contents($path, $data) { |
|
| 599 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 600 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 601 | + if (Filesystem::isValidPath($path) |
|
| 602 | + && !Filesystem::isFileBlacklisted($path) |
|
| 603 | + ) { |
|
| 604 | + $path = $this->getRelativePath($absolutePath); |
|
| 605 | + if ($path === null) { |
|
| 606 | + throw new InvalidPathException("Path $absolutePath is not in the expected root"); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 610 | + |
|
| 611 | + $exists = $this->file_exists($path); |
|
| 612 | + if ($this->shouldEmitHooks($path)) { |
|
| 613 | + $run = true; |
|
| 614 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 615 | + if (!$run) { |
|
| 616 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 617 | + return false; |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + try { |
|
| 622 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 623 | + } catch (\Exception $e) { |
|
| 624 | + // Release the shared lock before throwing. |
|
| 625 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 626 | + throw $e; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** @var Storage $storage */ |
|
| 630 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 631 | + $target = $storage->fopen($internalPath, 'w'); |
|
| 632 | + if ($target) { |
|
| 633 | + [, $result] = Files::streamCopy($data, $target, true); |
|
| 634 | + fclose($target); |
|
| 635 | + fclose($data); |
|
| 636 | + |
|
| 637 | + $this->writeUpdate($storage, $internalPath); |
|
| 638 | + |
|
| 639 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 640 | + |
|
| 641 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 642 | + $this->emit_file_hooks_post($exists, $path); |
|
| 643 | + } |
|
| 644 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 645 | + return $result; |
|
| 646 | + } else { |
|
| 647 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 648 | + return false; |
|
| 649 | + } |
|
| 650 | + } else { |
|
| 651 | + return false; |
|
| 652 | + } |
|
| 653 | + } else { |
|
| 654 | + $hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write']; |
|
| 655 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * @param string $path |
|
| 661 | + * @return bool|mixed |
|
| 662 | + */ |
|
| 663 | + public function unlink($path) { |
|
| 664 | + if ($path === '' || $path === '/') { |
|
| 665 | + // do not allow deleting the root |
|
| 666 | + return false; |
|
| 667 | + } |
|
| 668 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 669 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 670 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 671 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 672 | + return $this->removeMount($mount, $absolutePath); |
|
| 673 | + } |
|
| 674 | + if ($this->is_dir($path)) { |
|
| 675 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 676 | + } else { |
|
| 677 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 678 | + } |
|
| 679 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 680 | + $storage = $mount->getStorage(); |
|
| 681 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 682 | + $storage->getUpdater()->remove($internalPath); |
|
| 683 | + return true; |
|
| 684 | + } else { |
|
| 685 | + return $result; |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * @param string $directory |
|
| 691 | + * @return bool|mixed |
|
| 692 | + */ |
|
| 693 | + public function deleteAll($directory) { |
|
| 694 | + return $this->rmdir($directory); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * Rename/move a file or folder from the source path to target path. |
|
| 699 | + * |
|
| 700 | + * @param string $source source path |
|
| 701 | + * @param string $target target path |
|
| 702 | + * @param array $options |
|
| 703 | + * |
|
| 704 | + * @return bool|mixed |
|
| 705 | + * @throws LockedException |
|
| 706 | + */ |
|
| 707 | + public function rename($source, $target, array $options = []) { |
|
| 708 | + $checkSubMounts = $options['checkSubMounts'] ?? true; |
|
| 709 | + |
|
| 710 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
| 711 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
| 712 | + |
|
| 713 | + if (str_starts_with($absolutePath2, $absolutePath1 . '/')) { |
|
| 714 | + throw new ForbiddenException('Moving a folder into a child folder is forbidden', false); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + /** @var IMountManager $mountManager */ |
|
| 718 | + $mountManager = \OC::$server->get(IMountManager::class); |
|
| 719 | + |
|
| 720 | + $targetParts = explode('/', $absolutePath2); |
|
| 721 | + $targetUser = $targetParts[1] ?? null; |
|
| 722 | + $result = false; |
|
| 723 | + if ( |
|
| 724 | + Filesystem::isValidPath($target) |
|
| 725 | + && Filesystem::isValidPath($source) |
|
| 726 | + && !Filesystem::isFileBlacklisted($target) |
|
| 727 | + ) { |
|
| 728 | + $source = $this->getRelativePath($absolutePath1); |
|
| 729 | + $target = $this->getRelativePath($absolutePath2); |
|
| 730 | + $exists = $this->file_exists($target); |
|
| 731 | + |
|
| 732 | + if ($source == null || $target == null) { |
|
| 733 | + return false; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + try { |
|
| 737 | + $this->verifyPath(dirname($target), basename($target)); |
|
| 738 | + } catch (InvalidPathException) { |
|
| 739 | + return false; |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + $this->lockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
| 743 | + try { |
|
| 744 | + $this->lockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
| 745 | + |
|
| 746 | + $run = true; |
|
| 747 | + if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) { |
|
| 748 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 749 | + $this->emit_file_hooks_pre($exists, $target, $run); |
|
| 750 | + } elseif ($this->shouldEmitHooks($source)) { |
|
| 751 | + $sourcePath = $this->getHookPath($source); |
|
| 752 | + $targetPath = $this->getHookPath($target); |
|
| 753 | + if ($sourcePath !== null && $targetPath !== null) { |
|
| 754 | + \OC_Hook::emit( |
|
| 755 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 756 | + [ |
|
| 757 | + Filesystem::signal_param_oldpath => $sourcePath, |
|
| 758 | + Filesystem::signal_param_newpath => $targetPath, |
|
| 759 | + Filesystem::signal_param_run => &$run |
|
| 760 | + ] |
|
| 761 | + ); |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + if ($run) { |
|
| 765 | + $manager = Filesystem::getMountManager(); |
|
| 766 | + $mount1 = $this->getMount($source); |
|
| 767 | + $mount2 = $this->getMount($target); |
|
| 768 | + $storage1 = $mount1->getStorage(); |
|
| 769 | + $storage2 = $mount2->getStorage(); |
|
| 770 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 771 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 772 | + |
|
| 773 | + $this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 774 | + try { |
|
| 775 | + $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 776 | + |
|
| 777 | + if ($checkSubMounts) { |
|
| 778 | + $movedMounts = $mountManager->findIn($this->getAbsolutePath($source)); |
|
| 779 | + } else { |
|
| 780 | + $movedMounts = []; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + if ($internalPath1 === '') { |
|
| 784 | + $sourceParentMount = $this->getMount(dirname($source)); |
|
| 785 | + $movedMounts[] = $mount1; |
|
| 786 | + $this->validateMountMove($movedMounts, $sourceParentMount, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 787 | + /** |
|
| 788 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 789 | + */ |
|
| 790 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
| 791 | + $result = $mount1->moveMount($absolutePath2); |
|
| 792 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 793 | + |
|
| 794 | + // moving a file/folder within the same mount point |
|
| 795 | + } elseif ($storage1 === $storage2) { |
|
| 796 | + if (count($movedMounts) > 0) { |
|
| 797 | + $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 798 | + } |
|
| 799 | + if ($storage1) { |
|
| 800 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 801 | + } else { |
|
| 802 | + $result = false; |
|
| 803 | + } |
|
| 804 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 805 | + } else { |
|
| 806 | + if (count($movedMounts) > 0) { |
|
| 807 | + $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2)); |
|
| 808 | + } |
|
| 809 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
| 813 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 814 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 815 | + } elseif ($result) { |
|
| 816 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 817 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 818 | + } |
|
| 819 | + } |
|
| 820 | + } catch (\Exception $e) { |
|
| 821 | + throw $e; |
|
| 822 | + } finally { |
|
| 823 | + $this->changeLock($source, ILockingProvider::LOCK_SHARED, true); |
|
| 824 | + $this->changeLock($target, ILockingProvider::LOCK_SHARED, true); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) { |
|
| 828 | + if ($this->shouldEmitHooks()) { |
|
| 829 | + $this->emit_file_hooks_post($exists, $target); |
|
| 830 | + } |
|
| 831 | + } elseif ($result) { |
|
| 832 | + if ($this->shouldEmitHooks($source) && $this->shouldEmitHooks($target)) { |
|
| 833 | + $sourcePath = $this->getHookPath($source); |
|
| 834 | + $targetPath = $this->getHookPath($target); |
|
| 835 | + if ($sourcePath !== null && $targetPath !== null) { |
|
| 836 | + \OC_Hook::emit( |
|
| 837 | + Filesystem::CLASSNAME, |
|
| 838 | + Filesystem::signal_post_rename, |
|
| 839 | + [ |
|
| 840 | + Filesystem::signal_param_oldpath => $sourcePath, |
|
| 841 | + Filesystem::signal_param_newpath => $targetPath, |
|
| 842 | + ] |
|
| 843 | + ); |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + } catch (\Exception $e) { |
|
| 849 | + throw $e; |
|
| 850 | + } finally { |
|
| 851 | + $this->unlockFile($source, ILockingProvider::LOCK_SHARED, true); |
|
| 852 | + $this->unlockFile($target, ILockingProvider::LOCK_SHARED, true); |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | + return $result; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * @throws ForbiddenException |
|
| 860 | + */ |
|
| 861 | + private function validateMountMove(array $mounts, IMountPoint $sourceMount, IMountPoint $targetMount, bool $targetIsShared): void { |
|
| 862 | + $targetPath = $this->getRelativePath($targetMount->getMountPoint()); |
|
| 863 | + if ($targetPath) { |
|
| 864 | + $targetPath = trim($targetPath, '/'); |
|
| 865 | + } else { |
|
| 866 | + $targetPath = $targetMount->getMountPoint(); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + $l = \OC::$server->get(IFactory::class)->get('files'); |
|
| 870 | + foreach ($mounts as $mount) { |
|
| 871 | + $sourcePath = $this->getRelativePath($mount->getMountPoint()); |
|
| 872 | + if ($sourcePath) { |
|
| 873 | + $sourcePath = trim($sourcePath, '/'); |
|
| 874 | + } else { |
|
| 875 | + $sourcePath = $mount->getMountPoint(); |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + if (!$mount instanceof MoveableMount) { |
|
| 879 | + throw new ForbiddenException($l->t('Storage %s cannot be moved', [$sourcePath]), false); |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + if ($targetIsShared) { |
|
| 883 | + if ($sourceMount instanceof SharedMount) { |
|
| 884 | + throw new ForbiddenException($l->t('Moving a share (%s) into a shared folder is not allowed', [$sourcePath]), false); |
|
| 885 | + } else { |
|
| 886 | + throw new ForbiddenException($l->t('Moving a storage (%s) into a shared folder is not allowed', [$sourcePath]), false); |
|
| 887 | + } |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + if ($sourceMount !== $targetMount) { |
|
| 891 | + if ($sourceMount instanceof SharedMount) { |
|
| 892 | + if ($targetMount instanceof SharedMount) { |
|
| 893 | + throw new ForbiddenException($l->t('Moving a share (%s) into another share (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 894 | + } else { |
|
| 895 | + throw new ForbiddenException($l->t('Moving a share (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 896 | + } |
|
| 897 | + } else { |
|
| 898 | + if ($targetMount instanceof SharedMount) { |
|
| 899 | + throw new ForbiddenException($l->t('Moving a storage (%s) into a share (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 900 | + } else { |
|
| 901 | + throw new ForbiddenException($l->t('Moving a storage (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false); |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + } |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + |
|
| 908 | + /** |
|
| 909 | + * Copy a file/folder from the source path to target path |
|
| 910 | + * |
|
| 911 | + * @param string $source source path |
|
| 912 | + * @param string $target target path |
|
| 913 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 914 | + * |
|
| 915 | + * @return bool|mixed |
|
| 916 | + */ |
|
| 917 | + public function copy($source, $target, $preserveMtime = false) { |
|
| 918 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
|
| 919 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
|
| 920 | + $result = false; |
|
| 921 | + if ( |
|
| 922 | + Filesystem::isValidPath($target) |
|
| 923 | + && Filesystem::isValidPath($source) |
|
| 924 | + && !Filesystem::isFileBlacklisted($target) |
|
| 925 | + ) { |
|
| 926 | + $source = $this->getRelativePath($absolutePath1); |
|
| 927 | + $target = $this->getRelativePath($absolutePath2); |
|
| 928 | + |
|
| 929 | + if ($source == null || $target == null) { |
|
| 930 | + return false; |
|
| 931 | + } |
|
| 932 | + $run = true; |
|
| 933 | + |
|
| 934 | + $this->lockFile($target, ILockingProvider::LOCK_SHARED); |
|
| 935 | + $this->lockFile($source, ILockingProvider::LOCK_SHARED); |
|
| 936 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 937 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 938 | + |
|
| 939 | + try { |
|
| 940 | + $exists = $this->file_exists($target); |
|
| 941 | + if ($this->shouldEmitHooks($target)) { |
|
| 942 | + \OC_Hook::emit( |
|
| 943 | + Filesystem::CLASSNAME, |
|
| 944 | + Filesystem::signal_copy, |
|
| 945 | + [ |
|
| 946 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
| 947 | + Filesystem::signal_param_newpath => $this->getHookPath($target), |
|
| 948 | + Filesystem::signal_param_run => &$run |
|
| 949 | + ] |
|
| 950 | + ); |
|
| 951 | + $this->emit_file_hooks_pre($exists, $target, $run); |
|
| 952 | + } |
|
| 953 | + if ($run) { |
|
| 954 | + $mount1 = $this->getMount($source); |
|
| 955 | + $mount2 = $this->getMount($target); |
|
| 956 | + $storage1 = $mount1->getStorage(); |
|
| 957 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 958 | + $storage2 = $mount2->getStorage(); |
|
| 959 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 960 | + |
|
| 961 | + $this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 962 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 963 | + |
|
| 964 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 965 | + if ($storage1) { |
|
| 966 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 967 | + } else { |
|
| 968 | + $result = false; |
|
| 969 | + } |
|
| 970 | + } else { |
|
| 971 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + if ($result) { |
|
| 975 | + $this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + $this->changeLock($target, ILockingProvider::LOCK_SHARED); |
|
| 979 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 980 | + |
|
| 981 | + if ($this->shouldEmitHooks($target) && $result !== false) { |
|
| 982 | + \OC_Hook::emit( |
|
| 983 | + Filesystem::CLASSNAME, |
|
| 984 | + Filesystem::signal_post_copy, |
|
| 985 | + [ |
|
| 986 | + Filesystem::signal_param_oldpath => $this->getHookPath($source), |
|
| 987 | + Filesystem::signal_param_newpath => $this->getHookPath($target) |
|
| 988 | + ] |
|
| 989 | + ); |
|
| 990 | + $this->emit_file_hooks_post($exists, $target); |
|
| 991 | + } |
|
| 992 | + } |
|
| 993 | + } catch (\Exception $e) { |
|
| 994 | + $this->unlockFile($target, $lockTypePath2); |
|
| 995 | + $this->unlockFile($source, $lockTypePath1); |
|
| 996 | + throw $e; |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + $this->unlockFile($target, $lockTypePath2); |
|
| 1000 | + $this->unlockFile($source, $lockTypePath1); |
|
| 1001 | + } |
|
| 1002 | + return $result; |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + /** |
|
| 1006 | + * @param string $path |
|
| 1007 | + * @param string $mode 'r' or 'w' |
|
| 1008 | + * @return resource|false |
|
| 1009 | + * @throws LockedException |
|
| 1010 | + */ |
|
| 1011 | + public function fopen($path, $mode) { |
|
| 1012 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 1013 | + $hooks = []; |
|
| 1014 | + switch ($mode) { |
|
| 1015 | + case 'r': |
|
| 1016 | + $hooks[] = 'read'; |
|
| 1017 | + break; |
|
| 1018 | + case 'r+': |
|
| 1019 | + case 'w+': |
|
| 1020 | + case 'x+': |
|
| 1021 | + case 'a+': |
|
| 1022 | + $hooks[] = 'read'; |
|
| 1023 | + $hooks[] = 'write'; |
|
| 1024 | + break; |
|
| 1025 | + case 'w': |
|
| 1026 | + case 'x': |
|
| 1027 | + case 'a': |
|
| 1028 | + $hooks[] = 'write'; |
|
| 1029 | + break; |
|
| 1030 | + default: |
|
| 1031 | + $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + if ($mode !== 'r' && $mode !== 'w') { |
|
| 1035 | + $this->logger->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends', ['app' => 'core']); |
|
| 1036 | + } |
|
| 1037 | + |
|
| 1038 | + $handle = $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 1039 | + if (!is_resource($handle) && $mode === 'r') { |
|
| 1040 | + // trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed |
|
| 1041 | + $mount = $this->getMount($path); |
|
| 1042 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1043 | + $storage = $mount->getStorage(); |
|
| 1044 | + if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) { |
|
| 1045 | + $this->writeUpdate($storage, $internalPath); |
|
| 1046 | + } |
|
| 1047 | + } |
|
| 1048 | + return $handle; |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * @param string $path |
|
| 1053 | + * @throws InvalidPathException |
|
| 1054 | + */ |
|
| 1055 | + public function toTmpFile($path): string|false { |
|
| 1056 | + $this->assertPathLength($path); |
|
| 1057 | + if (Filesystem::isValidPath($path)) { |
|
| 1058 | + $source = $this->fopen($path, 'r'); |
|
| 1059 | + if ($source) { |
|
| 1060 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1061 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1062 | + file_put_contents($tmpFile, $source); |
|
| 1063 | + return $tmpFile; |
|
| 1064 | + } else { |
|
| 1065 | + return false; |
|
| 1066 | + } |
|
| 1067 | + } else { |
|
| 1068 | + return false; |
|
| 1069 | + } |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + /** |
|
| 1073 | + * @param string $tmpFile |
|
| 1074 | + * @param string $path |
|
| 1075 | + * @return bool|mixed |
|
| 1076 | + * @throws InvalidPathException |
|
| 1077 | + */ |
|
| 1078 | + public function fromTmpFile($tmpFile, $path) { |
|
| 1079 | + $this->assertPathLength($path); |
|
| 1080 | + if (Filesystem::isValidPath($path)) { |
|
| 1081 | + // Get directory that the file is going into |
|
| 1082 | + $filePath = dirname($path); |
|
| 1083 | + |
|
| 1084 | + // Create the directories if any |
|
| 1085 | + if (!$this->file_exists($filePath)) { |
|
| 1086 | + $result = $this->createParentDirectories($filePath); |
|
| 1087 | + if ($result === false) { |
|
| 1088 | + return false; |
|
| 1089 | + } |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + $source = fopen($tmpFile, 'r'); |
|
| 1093 | + if ($source) { |
|
| 1094 | + $result = $this->file_put_contents($path, $source); |
|
| 1095 | + /** |
|
| 1096 | + * $this->file_put_contents() might have already closed |
|
| 1097 | + * the resource, so we check it, before trying to close it |
|
| 1098 | + * to avoid messages in the error log. |
|
| 1099 | + * @psalm-suppress RedundantCondition false-positive |
|
| 1100 | + */ |
|
| 1101 | + if (is_resource($source)) { |
|
| 1102 | + fclose($source); |
|
| 1103 | + } |
|
| 1104 | + unlink($tmpFile); |
|
| 1105 | + return $result; |
|
| 1106 | + } else { |
|
| 1107 | + return false; |
|
| 1108 | + } |
|
| 1109 | + } else { |
|
| 1110 | + return false; |
|
| 1111 | + } |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * @param string $path |
|
| 1117 | + * @return mixed |
|
| 1118 | + * @throws InvalidPathException |
|
| 1119 | + */ |
|
| 1120 | + public function getMimeType($path) { |
|
| 1121 | + $this->assertPathLength($path); |
|
| 1122 | + return $this->basicOperation('getMimeType', $path); |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + /** |
|
| 1126 | + * @param string $type |
|
| 1127 | + * @param string $path |
|
| 1128 | + * @param bool $raw |
|
| 1129 | + */ |
|
| 1130 | + public function hash($type, $path, $raw = false): string|bool { |
|
| 1131 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1132 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1133 | + if (Filesystem::isValidPath($path)) { |
|
| 1134 | + $path = $this->getRelativePath($absolutePath); |
|
| 1135 | + if ($path == null) { |
|
| 1136 | + return false; |
|
| 1137 | + } |
|
| 1138 | + if ($this->shouldEmitHooks($path)) { |
|
| 1139 | + \OC_Hook::emit( |
|
| 1140 | + Filesystem::CLASSNAME, |
|
| 1141 | + Filesystem::signal_read, |
|
| 1142 | + [Filesystem::signal_param_path => $this->getHookPath($path)] |
|
| 1143 | + ); |
|
| 1144 | + } |
|
| 1145 | + /** @var Storage|null $storage */ |
|
| 1146 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1147 | + if ($storage) { |
|
| 1148 | + return $storage->hash($type, $internalPath, $raw); |
|
| 1149 | + } |
|
| 1150 | + } |
|
| 1151 | + return false; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + /** |
|
| 1155 | + * @param string $path |
|
| 1156 | + * @return mixed |
|
| 1157 | + * @throws InvalidPathException |
|
| 1158 | + */ |
|
| 1159 | + public function free_space($path = '/') { |
|
| 1160 | + $this->assertPathLength($path); |
|
| 1161 | + $result = $this->basicOperation('free_space', $path); |
|
| 1162 | + if ($result === null) { |
|
| 1163 | + throw new InvalidPathException(); |
|
| 1164 | + } |
|
| 1165 | + return $result; |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1170 | + * |
|
| 1171 | + * @param mixed $extraParam (optional) |
|
| 1172 | + * @return mixed |
|
| 1173 | + * @throws LockedException |
|
| 1174 | + * |
|
| 1175 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1176 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1177 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1178 | + */ |
|
| 1179 | + private function basicOperation(string $operation, string $path, array $hooks = [], $extraParam = null) { |
|
| 1180 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1181 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1182 | + if (Filesystem::isValidPath($path) |
|
| 1183 | + && !Filesystem::isFileBlacklisted($path) |
|
| 1184 | + ) { |
|
| 1185 | + $path = $this->getRelativePath($absolutePath); |
|
| 1186 | + if ($path == null) { |
|
| 1187 | + return false; |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1191 | + // always a shared lock during pre-hooks so the hook can read the file |
|
| 1192 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + $run = $this->runHooks($hooks, $path); |
|
| 1196 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1197 | + if ($run && $storage) { |
|
| 1198 | + /** @var Storage $storage */ |
|
| 1199 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1200 | + try { |
|
| 1201 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1202 | + } catch (LockedException $e) { |
|
| 1203 | + // release the shared lock we acquired before quitting |
|
| 1204 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1205 | + throw $e; |
|
| 1206 | + } |
|
| 1207 | + } |
|
| 1208 | + try { |
|
| 1209 | + if (!is_null($extraParam)) { |
|
| 1210 | + $result = $storage->$operation($internalPath, $extraParam); |
|
| 1211 | + } else { |
|
| 1212 | + $result = $storage->$operation($internalPath); |
|
| 1213 | + } |
|
| 1214 | + } catch (\Exception $e) { |
|
| 1215 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1216 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1217 | + } elseif (in_array('read', $hooks)) { |
|
| 1218 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1219 | + } |
|
| 1220 | + throw $e; |
|
| 1221 | + } |
|
| 1222 | + |
|
| 1223 | + if ($result !== false && in_array('delete', $hooks)) { |
|
| 1224 | + $this->removeUpdate($storage, $internalPath); |
|
| 1225 | + } |
|
| 1226 | + if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
| 1227 | + $isCreateOperation = $operation === 'mkdir' || ($operation === 'file_put_contents' && in_array('create', $hooks, true)); |
|
| 1228 | + $sizeDifference = $operation === 'mkdir' ? 0 : $result; |
|
| 1229 | + $this->writeUpdate($storage, $internalPath, null, $isCreateOperation ? $sizeDifference : null); |
|
| 1230 | + } |
|
| 1231 | + if ($result !== false && in_array('touch', $hooks)) { |
|
| 1232 | + $this->writeUpdate($storage, $internalPath, $extraParam, 0); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1236 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + $unlockLater = false; |
|
| 1240 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1241 | + $unlockLater = true; |
|
| 1242 | + // make sure our unlocking callback will still be called if connection is aborted |
|
| 1243 | + ignore_user_abort(true); |
|
| 1244 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1245 | + if (in_array('write', $hooks)) { |
|
| 1246 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1247 | + } elseif (in_array('read', $hooks)) { |
|
| 1248 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1249 | + } |
|
| 1250 | + }); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1254 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1255 | + $this->runHooks($hooks, $path, true); |
|
| 1256 | + } |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + if (!$unlockLater |
|
| 1260 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1261 | + ) { |
|
| 1262 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1263 | + } |
|
| 1264 | + return $result; |
|
| 1265 | + } else { |
|
| 1266 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + return null; |
|
| 1270 | + } |
|
| 1271 | + |
|
| 1272 | + /** |
|
| 1273 | + * get the path relative to the default root for hook usage |
|
| 1274 | + * |
|
| 1275 | + * @param string $path |
|
| 1276 | + * @return ?string |
|
| 1277 | + */ |
|
| 1278 | + private function getHookPath($path): ?string { |
|
| 1279 | + $view = Filesystem::getView(); |
|
| 1280 | + if (!$view) { |
|
| 1281 | + return $path; |
|
| 1282 | + } |
|
| 1283 | + return $view->getRelativePath($this->getAbsolutePath($path)); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + private function shouldEmitHooks(string $path = ''): bool { |
|
| 1287 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1288 | + return false; |
|
| 1289 | + } |
|
| 1290 | + if (!Filesystem::$loaded) { |
|
| 1291 | + return false; |
|
| 1292 | + } |
|
| 1293 | + $defaultRoot = Filesystem::getRoot(); |
|
| 1294 | + if ($defaultRoot === null) { |
|
| 1295 | + return false; |
|
| 1296 | + } |
|
| 1297 | + if ($this->fakeRoot === $defaultRoot) { |
|
| 1298 | + return true; |
|
| 1299 | + } |
|
| 1300 | + $fullPath = $this->getAbsolutePath($path); |
|
| 1301 | + |
|
| 1302 | + if ($fullPath === $defaultRoot) { |
|
| 1303 | + return true; |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * @param string[] $hooks |
|
| 1311 | + * @param string $path |
|
| 1312 | + * @param bool $post |
|
| 1313 | + * @return bool |
|
| 1314 | + */ |
|
| 1315 | + private function runHooks($hooks, $path, $post = false) { |
|
| 1316 | + $relativePath = $path; |
|
| 1317 | + $path = $this->getHookPath($path); |
|
| 1318 | + $prefix = $post ? 'post_' : ''; |
|
| 1319 | + $run = true; |
|
| 1320 | + if ($this->shouldEmitHooks($relativePath)) { |
|
| 1321 | + foreach ($hooks as $hook) { |
|
| 1322 | + if ($hook != 'read') { |
|
| 1323 | + \OC_Hook::emit( |
|
| 1324 | + Filesystem::CLASSNAME, |
|
| 1325 | + $prefix . $hook, |
|
| 1326 | + [ |
|
| 1327 | + Filesystem::signal_param_run => &$run, |
|
| 1328 | + Filesystem::signal_param_path => $path |
|
| 1329 | + ] |
|
| 1330 | + ); |
|
| 1331 | + } elseif (!$post) { |
|
| 1332 | + \OC_Hook::emit( |
|
| 1333 | + Filesystem::CLASSNAME, |
|
| 1334 | + $prefix . $hook, |
|
| 1335 | + [ |
|
| 1336 | + Filesystem::signal_param_path => $path |
|
| 1337 | + ] |
|
| 1338 | + ); |
|
| 1339 | + } |
|
| 1340 | + } |
|
| 1341 | + } |
|
| 1342 | + return $run; |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + /** |
|
| 1346 | + * check if a file or folder has been updated since $time |
|
| 1347 | + * |
|
| 1348 | + * @param string $path |
|
| 1349 | + * @param int $time |
|
| 1350 | + * @return bool |
|
| 1351 | + */ |
|
| 1352 | + public function hasUpdated($path, $time) { |
|
| 1353 | + return $this->basicOperation('hasUpdated', $path, [], $time); |
|
| 1354 | + } |
|
| 1355 | + |
|
| 1356 | + /** |
|
| 1357 | + * @param string $ownerId |
|
| 1358 | + * @return IUser |
|
| 1359 | + */ |
|
| 1360 | + private function getUserObjectForOwner(string $ownerId) { |
|
| 1361 | + return new LazyUser($ownerId, $this->userManager); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + /** |
|
| 1365 | + * Get file info from cache |
|
| 1366 | + * |
|
| 1367 | + * If the file is not in cached it will be scanned |
|
| 1368 | + * If the file has changed on storage the cache will be updated |
|
| 1369 | + * |
|
| 1370 | + * @param Storage $storage |
|
| 1371 | + * @param string $internalPath |
|
| 1372 | + * @param string $relativePath |
|
| 1373 | + * @return ICacheEntry|bool |
|
| 1374 | + */ |
|
| 1375 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1376 | + $cache = $storage->getCache($internalPath); |
|
| 1377 | + $data = $cache->get($internalPath); |
|
| 1378 | + $watcher = $storage->getWatcher($internalPath); |
|
| 1379 | + |
|
| 1380 | + try { |
|
| 1381 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1382 | + if (!$data || (isset($data['size']) && $data['size'] === -1)) { |
|
| 1383 | + if (!$storage->file_exists($internalPath)) { |
|
| 1384 | + return false; |
|
| 1385 | + } |
|
| 1386 | + // don't need to get a lock here since the scanner does it's own locking |
|
| 1387 | + $scanner = $storage->getScanner($internalPath); |
|
| 1388 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1389 | + $data = $cache->get($internalPath); |
|
| 1390 | + } elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1391 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1392 | + $watcher->update($internalPath, $data); |
|
| 1393 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1394 | + $data = $cache->get($internalPath); |
|
| 1395 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1396 | + } |
|
| 1397 | + } catch (LockedException $e) { |
|
| 1398 | + // if the file is locked we just use the old cache info |
|
| 1399 | + } |
|
| 1400 | + |
|
| 1401 | + return $data; |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + /** |
|
| 1405 | + * get the filesystem info |
|
| 1406 | + * |
|
| 1407 | + * @param string $path |
|
| 1408 | + * @param bool|string $includeMountPoints true to add mountpoint sizes, |
|
| 1409 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1410 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1411 | + */ |
|
| 1412 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1413 | + $this->assertPathLength($path); |
|
| 1414 | + if (!Filesystem::isValidPath($path)) { |
|
| 1415 | + return false; |
|
| 1416 | + } |
|
| 1417 | + $relativePath = $path; |
|
| 1418 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1419 | + |
|
| 1420 | + $mount = Filesystem::getMountManager()->find($path); |
|
| 1421 | + $storage = $mount->getStorage(); |
|
| 1422 | + $internalPath = $mount->getInternalPath($path); |
|
| 1423 | + if ($storage) { |
|
| 1424 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1425 | + |
|
| 1426 | + if (!$data instanceof ICacheEntry) { |
|
| 1427 | + if (Cache\Scanner::isPartialFile($relativePath)) { |
|
| 1428 | + return $this->getPartFileInfo($relativePath); |
|
| 1429 | + } |
|
| 1430 | + |
|
| 1431 | + return false; |
|
| 1432 | + } |
|
| 1433 | + |
|
| 1434 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1435 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1436 | + } |
|
| 1437 | + if ($internalPath === '' && $data['name']) { |
|
| 1438 | + $data['name'] = basename($path); |
|
| 1439 | + } |
|
| 1440 | + |
|
| 1441 | + $ownerId = $storage->getOwner($internalPath); |
|
| 1442 | + $owner = null; |
|
| 1443 | + if ($ownerId !== false) { |
|
| 1444 | + // ownerId might be null if files are accessed with an access token without file system access |
|
| 1445 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1446 | + } |
|
| 1447 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1448 | + |
|
| 1449 | + if (isset($data['fileid'])) { |
|
| 1450 | + if ($includeMountPoints && $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1451 | + //add the sizes of other mount points to the folder |
|
| 1452 | + $extOnly = ($includeMountPoints === 'ext'); |
|
| 1453 | + $this->addSubMounts($info, $extOnly); |
|
| 1454 | + } |
|
| 1455 | + } |
|
| 1456 | + |
|
| 1457 | + return $info; |
|
| 1458 | + } else { |
|
| 1459 | + $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + return false; |
|
| 1463 | + } |
|
| 1464 | + |
|
| 1465 | + /** |
|
| 1466 | + * Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts |
|
| 1467 | + */ |
|
| 1468 | + public function addSubMounts(FileInfo $info, $extOnly = false): void { |
|
| 1469 | + $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
|
| 1470 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1471 | + return !($extOnly && $mount instanceof SharedMount); |
|
| 1472 | + })); |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * get the content of a directory |
|
| 1477 | + * |
|
| 1478 | + * @param string $directory path under datadirectory |
|
| 1479 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1480 | + * @return FileInfo[] |
|
| 1481 | + */ |
|
| 1482 | + public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Files\FileInfo $directoryInfo = null) { |
|
| 1483 | + $this->assertPathLength($directory); |
|
| 1484 | + if (!Filesystem::isValidPath($directory)) { |
|
| 1485 | + return []; |
|
| 1486 | + } |
|
| 1487 | + |
|
| 1488 | + $path = $this->getAbsolutePath($directory); |
|
| 1489 | + $path = Filesystem::normalizePath($path); |
|
| 1490 | + $mount = $this->getMount($directory); |
|
| 1491 | + $storage = $mount->getStorage(); |
|
| 1492 | + $internalPath = $mount->getInternalPath($path); |
|
| 1493 | + if (!$storage) { |
|
| 1494 | + return []; |
|
| 1495 | + } |
|
| 1496 | + |
|
| 1497 | + $cache = $storage->getCache($internalPath); |
|
| 1498 | + $user = \OC_User::getUser(); |
|
| 1499 | + |
|
| 1500 | + if (!$directoryInfo) { |
|
| 1501 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1502 | + if (!$data instanceof ICacheEntry || !isset($data['fileid'])) { |
|
| 1503 | + return []; |
|
| 1504 | + } |
|
| 1505 | + } else { |
|
| 1506 | + $data = $directoryInfo; |
|
| 1507 | + } |
|
| 1508 | + |
|
| 1509 | + if (!($data->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1510 | + return []; |
|
| 1511 | + } |
|
| 1512 | + |
|
| 1513 | + $folderId = $data->getId(); |
|
| 1514 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1515 | + |
|
| 1516 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1517 | + |
|
| 1518 | + $fileNames = array_map(function (ICacheEntry $content) { |
|
| 1519 | + return $content->getName(); |
|
| 1520 | + }, $contents); |
|
| 1521 | + /** |
|
| 1522 | + * @var \OC\Files\FileInfo[] $fileInfos |
|
| 1523 | + */ |
|
| 1524 | + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1525 | + if ($sharingDisabled) { |
|
| 1526 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1527 | + } |
|
| 1528 | + $ownerId = $storage->getOwner($content['path']); |
|
| 1529 | + if ($ownerId !== false) { |
|
| 1530 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1531 | + } else { |
|
| 1532 | + $owner = null; |
|
| 1533 | + } |
|
| 1534 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1535 | + }, $contents); |
|
| 1536 | + $files = array_combine($fileNames, $fileInfos); |
|
| 1537 | + |
|
| 1538 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1539 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1540 | + |
|
| 1541 | + // make sure nested mounts are sorted after their parent mounts |
|
| 1542 | + // otherwise doesn't propagate the etag across storage boundaries correctly |
|
| 1543 | + usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
| 1544 | + return $a->getMountPoint() <=> $b->getMountPoint(); |
|
| 1545 | + }); |
|
| 1546 | + |
|
| 1547 | + $dirLength = strlen($path); |
|
| 1548 | + foreach ($mounts as $mount) { |
|
| 1549 | + $mountPoint = $mount->getMountPoint(); |
|
| 1550 | + $subStorage = $mount->getStorage(); |
|
| 1551 | + if ($subStorage) { |
|
| 1552 | + $subCache = $subStorage->getCache(''); |
|
| 1553 | + |
|
| 1554 | + $rootEntry = $subCache->get(''); |
|
| 1555 | + if (!$rootEntry) { |
|
| 1556 | + $subScanner = $subStorage->getScanner(); |
|
| 1557 | + try { |
|
| 1558 | + $subScanner->scanFile(''); |
|
| 1559 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1560 | + continue; |
|
| 1561 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1562 | + continue; |
|
| 1563 | + } catch (\Exception $e) { |
|
| 1564 | + // sometimes when the storage is not available it can be any exception |
|
| 1565 | + $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
| 1566 | + 'exception' => $e, |
|
| 1567 | + 'app' => 'core', |
|
| 1568 | + ]); |
|
| 1569 | + continue; |
|
| 1570 | + } |
|
| 1571 | + $rootEntry = $subCache->get(''); |
|
| 1572 | + } |
|
| 1573 | + |
|
| 1574 | + if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) { |
|
| 1575 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1576 | + if ($pos = strpos($relativePath, '/')) { |
|
| 1577 | + //mountpoint inside subfolder add size to the correct folder |
|
| 1578 | + $entryName = substr($relativePath, 0, $pos); |
|
| 1579 | + |
|
| 1580 | + // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet |
|
| 1581 | + if (!isset($files[$entryName])) { |
|
| 1582 | + try { |
|
| 1583 | + [$storage, ] = $this->resolvePath($path . '/' . $entryName); |
|
| 1584 | + // make sure we can create the mountpoint folder, even if the user has a quota of 0 |
|
| 1585 | + if ($storage->instanceOfStorage(Quota::class)) { |
|
| 1586 | + $storage->enableQuota(false); |
|
| 1587 | + } |
|
| 1588 | + |
|
| 1589 | + if ($this->mkdir($path . '/' . $entryName) !== false) { |
|
| 1590 | + $info = $this->getFileInfo($path . '/' . $entryName); |
|
| 1591 | + if ($info !== false) { |
|
| 1592 | + $files[$entryName] = $info; |
|
| 1593 | + } |
|
| 1594 | + } |
|
| 1595 | + |
|
| 1596 | + if ($storage->instanceOfStorage(Quota::class)) { |
|
| 1597 | + $storage->enableQuota(true); |
|
| 1598 | + } |
|
| 1599 | + } catch (\Exception $e) { |
|
| 1600 | + // Creating the parent folder might not be possible, for example due to a lack of permissions. |
|
| 1601 | + $this->logger->debug('Failed to create non-existent parent', ['exception' => $e, 'path' => $path . '/' . $entryName]); |
|
| 1602 | + } |
|
| 1603 | + } |
|
| 1604 | + |
|
| 1605 | + if (isset($files[$entryName])) { |
|
| 1606 | + $files[$entryName]->addSubEntry($rootEntry, $mountPoint); |
|
| 1607 | + } |
|
| 1608 | + } else { //mountpoint in this folder, add an entry for it |
|
| 1609 | + $rootEntry['name'] = $relativePath; |
|
| 1610 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1611 | + $permissions = $rootEntry['permissions']; |
|
| 1612 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1613 | + // for shared files/folders we use the permissions given by the owner |
|
| 1614 | + if ($mount instanceof MoveableMount) { |
|
| 1615 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1616 | + } else { |
|
| 1617 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1618 | + } |
|
| 1619 | + |
|
| 1620 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1621 | + |
|
| 1622 | + // if sharing was disabled for the user we remove the share permissions |
|
| 1623 | + if ($sharingDisabled) { |
|
| 1624 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1625 | + } |
|
| 1626 | + |
|
| 1627 | + $ownerId = $subStorage->getOwner(''); |
|
| 1628 | + if ($ownerId !== false) { |
|
| 1629 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
| 1630 | + } else { |
|
| 1631 | + $owner = null; |
|
| 1632 | + } |
|
| 1633 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1634 | + } |
|
| 1635 | + } |
|
| 1636 | + } |
|
| 1637 | + } |
|
| 1638 | + |
|
| 1639 | + if ($mimetype_filter) { |
|
| 1640 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1641 | + if (strpos($mimetype_filter, '/')) { |
|
| 1642 | + return $file->getMimetype() === $mimetype_filter; |
|
| 1643 | + } else { |
|
| 1644 | + return $file->getMimePart() === $mimetype_filter; |
|
| 1645 | + } |
|
| 1646 | + }); |
|
| 1647 | + } |
|
| 1648 | + |
|
| 1649 | + return array_values($files); |
|
| 1650 | + } |
|
| 1651 | + |
|
| 1652 | + /** |
|
| 1653 | + * change file metadata |
|
| 1654 | + * |
|
| 1655 | + * @param string $path |
|
| 1656 | + * @param array|\OCP\Files\FileInfo $data |
|
| 1657 | + * @return int |
|
| 1658 | + * |
|
| 1659 | + * returns the fileid of the updated file |
|
| 1660 | + */ |
|
| 1661 | + public function putFileInfo($path, $data) { |
|
| 1662 | + $this->assertPathLength($path); |
|
| 1663 | + if ($data instanceof FileInfo) { |
|
| 1664 | + $data = $data->getData(); |
|
| 1665 | + } |
|
| 1666 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1667 | + /** |
|
| 1668 | + * @var Storage $storage |
|
| 1669 | + * @var string $internalPath |
|
| 1670 | + */ |
|
| 1671 | + [$storage, $internalPath] = Filesystem::resolvePath($path); |
|
| 1672 | + if ($storage) { |
|
| 1673 | + $cache = $storage->getCache($path); |
|
| 1674 | + |
|
| 1675 | + if (!$cache->inCache($internalPath)) { |
|
| 1676 | + $scanner = $storage->getScanner($internalPath); |
|
| 1677 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1678 | + } |
|
| 1679 | + |
|
| 1680 | + return $cache->put($internalPath, $data); |
|
| 1681 | + } else { |
|
| 1682 | + return -1; |
|
| 1683 | + } |
|
| 1684 | + } |
|
| 1685 | + |
|
| 1686 | + /** |
|
| 1687 | + * search for files with the name matching $query |
|
| 1688 | + * |
|
| 1689 | + * @param string $query |
|
| 1690 | + * @return FileInfo[] |
|
| 1691 | + */ |
|
| 1692 | + public function search($query) { |
|
| 1693 | + return $this->searchCommon('search', ['%' . $query . '%']); |
|
| 1694 | + } |
|
| 1695 | + |
|
| 1696 | + /** |
|
| 1697 | + * search for files with the name matching $query |
|
| 1698 | + * |
|
| 1699 | + * @param string $query |
|
| 1700 | + * @return FileInfo[] |
|
| 1701 | + */ |
|
| 1702 | + public function searchRaw($query) { |
|
| 1703 | + return $this->searchCommon('search', [$query]); |
|
| 1704 | + } |
|
| 1705 | + |
|
| 1706 | + /** |
|
| 1707 | + * search for files by mimetype |
|
| 1708 | + * |
|
| 1709 | + * @param string $mimetype |
|
| 1710 | + * @return FileInfo[] |
|
| 1711 | + */ |
|
| 1712 | + public function searchByMime($mimetype) { |
|
| 1713 | + return $this->searchCommon('searchByMime', [$mimetype]); |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + /** |
|
| 1717 | + * search for files by tag |
|
| 1718 | + * |
|
| 1719 | + * @param string|int $tag name or tag id |
|
| 1720 | + * @param string $userId owner of the tags |
|
| 1721 | + * @return FileInfo[] |
|
| 1722 | + */ |
|
| 1723 | + public function searchByTag($tag, $userId) { |
|
| 1724 | + return $this->searchCommon('searchByTag', [$tag, $userId]); |
|
| 1725 | + } |
|
| 1726 | + |
|
| 1727 | + /** |
|
| 1728 | + * @param string $method cache method |
|
| 1729 | + * @param array $args |
|
| 1730 | + * @return FileInfo[] |
|
| 1731 | + */ |
|
| 1732 | + private function searchCommon($method, $args) { |
|
| 1733 | + $files = []; |
|
| 1734 | + $rootLength = strlen($this->fakeRoot); |
|
| 1735 | + |
|
| 1736 | + $mount = $this->getMount(''); |
|
| 1737 | + $mountPoint = $mount->getMountPoint(); |
|
| 1738 | + $storage = $mount->getStorage(); |
|
| 1739 | + $userManager = \OC::$server->getUserManager(); |
|
| 1740 | + if ($storage) { |
|
| 1741 | + $cache = $storage->getCache(''); |
|
| 1742 | + |
|
| 1743 | + $results = call_user_func_array([$cache, $method], $args); |
|
| 1744 | + foreach ($results as $result) { |
|
| 1745 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1746 | + $internalPath = $result['path']; |
|
| 1747 | + $path = $mountPoint . $result['path']; |
|
| 1748 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1749 | + $ownerId = $storage->getOwner($internalPath); |
|
| 1750 | + if ($ownerId !== false) { |
|
| 1751 | + $owner = $userManager->get($ownerId); |
|
| 1752 | + } else { |
|
| 1753 | + $owner = null; |
|
| 1754 | + } |
|
| 1755 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1756 | + } |
|
| 1757 | + } |
|
| 1758 | + |
|
| 1759 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1760 | + foreach ($mounts as $mount) { |
|
| 1761 | + $mountPoint = $mount->getMountPoint(); |
|
| 1762 | + $storage = $mount->getStorage(); |
|
| 1763 | + if ($storage) { |
|
| 1764 | + $cache = $storage->getCache(''); |
|
| 1765 | + |
|
| 1766 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1767 | + $results = call_user_func_array([$cache, $method], $args); |
|
| 1768 | + if ($results) { |
|
| 1769 | + foreach ($results as $result) { |
|
| 1770 | + $internalPath = $result['path']; |
|
| 1771 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1772 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1773 | + $ownerId = $storage->getOwner($internalPath); |
|
| 1774 | + if ($ownerId !== false) { |
|
| 1775 | + $owner = $userManager->get($ownerId); |
|
| 1776 | + } else { |
|
| 1777 | + $owner = null; |
|
| 1778 | + } |
|
| 1779 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1780 | + } |
|
| 1781 | + } |
|
| 1782 | + } |
|
| 1783 | + } |
|
| 1784 | + } |
|
| 1785 | + return $files; |
|
| 1786 | + } |
|
| 1787 | + |
|
| 1788 | + /** |
|
| 1789 | + * Get the owner for a file or folder |
|
| 1790 | + * |
|
| 1791 | + * @throws NotFoundException |
|
| 1792 | + */ |
|
| 1793 | + public function getOwner(string $path): string { |
|
| 1794 | + $info = $this->getFileInfo($path); |
|
| 1795 | + if (!$info) { |
|
| 1796 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1797 | + } |
|
| 1798 | + |
|
| 1799 | + if ($info->getOwner() === null) { |
|
| 1800 | + throw new NotFoundException($path . ' has no owner'); |
|
| 1801 | + } |
|
| 1802 | + |
|
| 1803 | + return $info->getOwner()->getUID(); |
|
| 1804 | + } |
|
| 1805 | + |
|
| 1806 | + /** |
|
| 1807 | + * get the ETag for a file or folder |
|
| 1808 | + * |
|
| 1809 | + * @param string $path |
|
| 1810 | + * @return string|false |
|
| 1811 | + */ |
|
| 1812 | + public function getETag($path) { |
|
| 1813 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1814 | + if ($storage) { |
|
| 1815 | + return $storage->getETag($internalPath); |
|
| 1816 | + } else { |
|
| 1817 | + return false; |
|
| 1818 | + } |
|
| 1819 | + } |
|
| 1820 | + |
|
| 1821 | + /** |
|
| 1822 | + * Get the path of a file by id, relative to the view |
|
| 1823 | + * |
|
| 1824 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 1825 | + * |
|
| 1826 | + * @param int $id |
|
| 1827 | + * @param int|null $storageId |
|
| 1828 | + * @return string |
|
| 1829 | + * @throws NotFoundException |
|
| 1830 | + */ |
|
| 1831 | + public function getPath($id, ?int $storageId = null): string { |
|
| 1832 | + $id = (int)$id; |
|
| 1833 | + $rootFolder = Server::get(Files\IRootFolder::class); |
|
| 1834 | + |
|
| 1835 | + $node = $rootFolder->getFirstNodeByIdInPath($id, $this->getRoot()); |
|
| 1836 | + if ($node) { |
|
| 1837 | + if ($storageId === null || $storageId === $node->getStorage()->getCache()->getNumericStorageId()) { |
|
| 1838 | + return $this->getRelativePath($node->getPath()) ?? ''; |
|
| 1839 | + } |
|
| 1840 | + } else { |
|
| 1841 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1842 | + } |
|
| 1843 | + |
|
| 1844 | + foreach ($rootFolder->getByIdInPath($id, $this->getRoot()) as $node) { |
|
| 1845 | + if ($storageId === $node->getStorage()->getCache()->getNumericStorageId()) { |
|
| 1846 | + return $this->getRelativePath($node->getPath()) ?? ''; |
|
| 1847 | + } |
|
| 1848 | + } |
|
| 1849 | + |
|
| 1850 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1851 | + } |
|
| 1852 | + |
|
| 1853 | + /** |
|
| 1854 | + * @param string $path |
|
| 1855 | + * @throws InvalidPathException |
|
| 1856 | + */ |
|
| 1857 | + private function assertPathLength($path): void { |
|
| 1858 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1859 | + // Check for the string length - performed using isset() instead of strlen() |
|
| 1860 | + // because isset() is about 5x-40x faster. |
|
| 1861 | + if (isset($path[$maxLen])) { |
|
| 1862 | + $pathLen = strlen($path); |
|
| 1863 | + throw new InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1864 | + } |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + /** |
|
| 1868 | + * check if it is allowed to move a mount point to a given target. |
|
| 1869 | + * It is not allowed to move a mount point into a different mount point or |
|
| 1870 | + * into an already shared folder |
|
| 1871 | + */ |
|
| 1872 | + private function targetIsNotShared(string $user, string $targetPath): bool { |
|
| 1873 | + $providers = [ |
|
| 1874 | + IShare::TYPE_USER, |
|
| 1875 | + IShare::TYPE_GROUP, |
|
| 1876 | + IShare::TYPE_EMAIL, |
|
| 1877 | + IShare::TYPE_CIRCLE, |
|
| 1878 | + IShare::TYPE_ROOM, |
|
| 1879 | + IShare::TYPE_DECK, |
|
| 1880 | + IShare::TYPE_SCIENCEMESH |
|
| 1881 | + ]; |
|
| 1882 | + $shareManager = Server::get(IManager::class); |
|
| 1883 | + /** @var IShare[] $shares */ |
|
| 1884 | + $shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) { |
|
| 1885 | + return $shareManager->getSharesBy($user, $type); |
|
| 1886 | + }, $providers)); |
|
| 1887 | + |
|
| 1888 | + foreach ($shares as $share) { |
|
| 1889 | + $sharedPath = $share->getNode()->getPath(); |
|
| 1890 | + if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath . '/')) { |
|
| 1891 | + $this->logger->debug( |
|
| 1892 | + 'It is not allowed to move one mount point into a shared folder', |
|
| 1893 | + ['app' => 'files']); |
|
| 1894 | + return false; |
|
| 1895 | + } |
|
| 1896 | + } |
|
| 1897 | + |
|
| 1898 | + return true; |
|
| 1899 | + } |
|
| 1900 | + |
|
| 1901 | + /** |
|
| 1902 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1903 | + */ |
|
| 1904 | + private function getPartFileInfo(string $path): \OC\Files\FileInfo { |
|
| 1905 | + $mount = $this->getMount($path); |
|
| 1906 | + $storage = $mount->getStorage(); |
|
| 1907 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1908 | + $ownerId = $storage->getOwner($internalPath); |
|
| 1909 | + if ($ownerId !== false) { |
|
| 1910 | + $owner = Server::get(IUserManager::class)->get($ownerId); |
|
| 1911 | + } else { |
|
| 1912 | + $owner = null; |
|
| 1913 | + } |
|
| 1914 | + return new FileInfo( |
|
| 1915 | + $this->getAbsolutePath($path), |
|
| 1916 | + $storage, |
|
| 1917 | + $internalPath, |
|
| 1918 | + [ |
|
| 1919 | + 'fileid' => null, |
|
| 1920 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1921 | + 'name' => basename($path), |
|
| 1922 | + 'etag' => null, |
|
| 1923 | + 'size' => $storage->filesize($internalPath), |
|
| 1924 | + 'mtime' => $storage->filemtime($internalPath), |
|
| 1925 | + 'encrypted' => false, |
|
| 1926 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1927 | + ], |
|
| 1928 | + $mount, |
|
| 1929 | + $owner |
|
| 1930 | + ); |
|
| 1931 | + } |
|
| 1932 | + |
|
| 1933 | + /** |
|
| 1934 | + * @param string $path |
|
| 1935 | + * @param string $fileName |
|
| 1936 | + * @param bool $readonly Check only if the path is allowed for read-only access |
|
| 1937 | + * @throws InvalidPathException |
|
| 1938 | + */ |
|
| 1939 | + public function verifyPath($path, $fileName, $readonly = false): void { |
|
| 1940 | + // All of the view's functions disallow '..' in the path so we can short cut if the path is invalid |
|
| 1941 | + if (!Filesystem::isValidPath($path ?: '/')) { |
|
| 1942 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1943 | + throw new InvalidPathException($l->t('Path contains invalid segments')); |
|
| 1944 | + } |
|
| 1945 | + |
|
| 1946 | + // Short cut for read-only validation |
|
| 1947 | + if ($readonly) { |
|
| 1948 | + $validator = Server::get(FilenameValidator::class); |
|
| 1949 | + if ($validator->isForbidden($fileName)) { |
|
| 1950 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1951 | + throw new InvalidPathException($l->t('Filename is a reserved word')); |
|
| 1952 | + } |
|
| 1953 | + return; |
|
| 1954 | + } |
|
| 1955 | + |
|
| 1956 | + try { |
|
| 1957 | + /** @type \OCP\Files\Storage $storage */ |
|
| 1958 | + [$storage, $internalPath] = $this->resolvePath($path); |
|
| 1959 | + $storage->verifyPath($internalPath, $fileName); |
|
| 1960 | + } catch (ReservedWordException $ex) { |
|
| 1961 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1962 | + throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename is a reserved word')); |
|
| 1963 | + } catch (InvalidCharacterInPathException $ex) { |
|
| 1964 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1965 | + throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename contains at least one invalid character')); |
|
| 1966 | + } catch (FileNameTooLongException $ex) { |
|
| 1967 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1968 | + throw new InvalidPathException($l->t('Filename is too long')); |
|
| 1969 | + } catch (InvalidDirectoryException $ex) { |
|
| 1970 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1971 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1972 | + } catch (EmptyFileNameException $ex) { |
|
| 1973 | + $l = \OCP\Util::getL10N('lib'); |
|
| 1974 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1975 | + } |
|
| 1976 | + } |
|
| 1977 | + |
|
| 1978 | + /** |
|
| 1979 | + * get all parent folders of $path |
|
| 1980 | + * |
|
| 1981 | + * @param string $path |
|
| 1982 | + * @return string[] |
|
| 1983 | + */ |
|
| 1984 | + private function getParents($path) { |
|
| 1985 | + $path = trim($path, '/'); |
|
| 1986 | + if (!$path) { |
|
| 1987 | + return []; |
|
| 1988 | + } |
|
| 1989 | + |
|
| 1990 | + $parts = explode('/', $path); |
|
| 1991 | + |
|
| 1992 | + // remove the single file |
|
| 1993 | + array_pop($parts); |
|
| 1994 | + $result = ['/']; |
|
| 1995 | + $resultPath = ''; |
|
| 1996 | + foreach ($parts as $part) { |
|
| 1997 | + if ($part) { |
|
| 1998 | + $resultPath .= '/' . $part; |
|
| 1999 | + $result[] = $resultPath; |
|
| 2000 | + } |
|
| 2001 | + } |
|
| 2002 | + return $result; |
|
| 2003 | + } |
|
| 2004 | + |
|
| 2005 | + /** |
|
| 2006 | + * Returns the mount point for which to lock |
|
| 2007 | + * |
|
| 2008 | + * @param string $absolutePath absolute path |
|
| 2009 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 2010 | + * is mounted directly on the given path, false otherwise |
|
| 2011 | + * @return IMountPoint mount point for which to apply locks |
|
| 2012 | + */ |
|
| 2013 | + private function getMountForLock(string $absolutePath, bool $useParentMount = false): IMountPoint { |
|
| 2014 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 2015 | + |
|
| 2016 | + if ($useParentMount) { |
|
| 2017 | + // find out if something is mounted directly on the path |
|
| 2018 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 2019 | + if ($internalPath === '') { |
|
| 2020 | + // resolve the parent mount instead |
|
| 2021 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 2022 | + } |
|
| 2023 | + } |
|
| 2024 | + |
|
| 2025 | + return $mount; |
|
| 2026 | + } |
|
| 2027 | + |
|
| 2028 | + /** |
|
| 2029 | + * Lock the given path |
|
| 2030 | + * |
|
| 2031 | + * @param string $path the path of the file to lock, relative to the view |
|
| 2032 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2033 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2034 | + * |
|
| 2035 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2036 | + * @throws LockedException if the path is already locked |
|
| 2037 | + */ |
|
| 2038 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 2039 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2040 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2041 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2042 | + return false; |
|
| 2043 | + } |
|
| 2044 | + |
|
| 2045 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2046 | + try { |
|
| 2047 | + $storage = $mount->getStorage(); |
|
| 2048 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2049 | + $storage->acquireLock( |
|
| 2050 | + $mount->getInternalPath($absolutePath), |
|
| 2051 | + $type, |
|
| 2052 | + $this->lockingProvider |
|
| 2053 | + ); |
|
| 2054 | + } |
|
| 2055 | + } catch (LockedException $e) { |
|
| 2056 | + // rethrow with the human-readable path |
|
| 2057 | + throw new LockedException( |
|
| 2058 | + $path, |
|
| 2059 | + $e, |
|
| 2060 | + $e->getExistingLock() |
|
| 2061 | + ); |
|
| 2062 | + } |
|
| 2063 | + |
|
| 2064 | + return true; |
|
| 2065 | + } |
|
| 2066 | + |
|
| 2067 | + /** |
|
| 2068 | + * Change the lock type |
|
| 2069 | + * |
|
| 2070 | + * @param string $path the path of the file to lock, relative to the view |
|
| 2071 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2072 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2073 | + * |
|
| 2074 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2075 | + * @throws LockedException if the path is already locked |
|
| 2076 | + */ |
|
| 2077 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 2078 | + $path = Filesystem::normalizePath($path); |
|
| 2079 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2080 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2081 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2082 | + return false; |
|
| 2083 | + } |
|
| 2084 | + |
|
| 2085 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2086 | + try { |
|
| 2087 | + $storage = $mount->getStorage(); |
|
| 2088 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2089 | + $storage->changeLock( |
|
| 2090 | + $mount->getInternalPath($absolutePath), |
|
| 2091 | + $type, |
|
| 2092 | + $this->lockingProvider |
|
| 2093 | + ); |
|
| 2094 | + } |
|
| 2095 | + } catch (LockedException $e) { |
|
| 2096 | + // rethrow with the a human-readable path |
|
| 2097 | + throw new LockedException( |
|
| 2098 | + $path, |
|
| 2099 | + $e, |
|
| 2100 | + $e->getExistingLock() |
|
| 2101 | + ); |
|
| 2102 | + } |
|
| 2103 | + |
|
| 2104 | + return true; |
|
| 2105 | + } |
|
| 2106 | + |
|
| 2107 | + /** |
|
| 2108 | + * Unlock the given path |
|
| 2109 | + * |
|
| 2110 | + * @param string $path the path of the file to unlock, relative to the view |
|
| 2111 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2112 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2113 | + * |
|
| 2114 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2115 | + * @throws LockedException |
|
| 2116 | + */ |
|
| 2117 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 2118 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2119 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2120 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2121 | + return false; |
|
| 2122 | + } |
|
| 2123 | + |
|
| 2124 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2125 | + $storage = $mount->getStorage(); |
|
| 2126 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2127 | + $storage->releaseLock( |
|
| 2128 | + $mount->getInternalPath($absolutePath), |
|
| 2129 | + $type, |
|
| 2130 | + $this->lockingProvider |
|
| 2131 | + ); |
|
| 2132 | + } |
|
| 2133 | + |
|
| 2134 | + return true; |
|
| 2135 | + } |
|
| 2136 | + |
|
| 2137 | + /** |
|
| 2138 | + * Lock a path and all its parents up to the root of the view |
|
| 2139 | + * |
|
| 2140 | + * @param string $path the path of the file to lock relative to the view |
|
| 2141 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2142 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2143 | + * |
|
| 2144 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2145 | + * @throws LockedException |
|
| 2146 | + */ |
|
| 2147 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2148 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2149 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2150 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2151 | + return false; |
|
| 2152 | + } |
|
| 2153 | + |
|
| 2154 | + $this->lockPath($path, $type, $lockMountPoint); |
|
| 2155 | + |
|
| 2156 | + $parents = $this->getParents($path); |
|
| 2157 | + foreach ($parents as $parent) { |
|
| 2158 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2159 | + } |
|
| 2160 | + |
|
| 2161 | + return true; |
|
| 2162 | + } |
|
| 2163 | + |
|
| 2164 | + /** |
|
| 2165 | + * Unlock a path and all its parents up to the root of the view |
|
| 2166 | + * |
|
| 2167 | + * @param string $path the path of the file to lock relative to the view |
|
| 2168 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2169 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2170 | + * |
|
| 2171 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2172 | + * @throws LockedException |
|
| 2173 | + */ |
|
| 2174 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2175 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2176 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2177 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2178 | + return false; |
|
| 2179 | + } |
|
| 2180 | + |
|
| 2181 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2182 | + |
|
| 2183 | + $parents = $this->getParents($path); |
|
| 2184 | + foreach ($parents as $parent) { |
|
| 2185 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2186 | + } |
|
| 2187 | + |
|
| 2188 | + return true; |
|
| 2189 | + } |
|
| 2190 | + |
|
| 2191 | + /** |
|
| 2192 | + * Only lock files in data/user/files/ |
|
| 2193 | + * |
|
| 2194 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2195 | + * @return bool |
|
| 2196 | + */ |
|
| 2197 | + protected function shouldLockFile($path) { |
|
| 2198 | + $path = Filesystem::normalizePath($path); |
|
| 2199 | + |
|
| 2200 | + $pathSegments = explode('/', $path); |
|
| 2201 | + if (isset($pathSegments[2])) { |
|
| 2202 | + // E.g.: /username/files/path-to-file |
|
| 2203 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2204 | + } |
|
| 2205 | + |
|
| 2206 | + return !str_starts_with($path, '/appdata_'); |
|
| 2207 | + } |
|
| 2208 | + |
|
| 2209 | + /** |
|
| 2210 | + * Shortens the given absolute path to be relative to |
|
| 2211 | + * "$user/files". |
|
| 2212 | + * |
|
| 2213 | + * @param string $absolutePath absolute path which is under "files" |
|
| 2214 | + * |
|
| 2215 | + * @return string path relative to "files" with trimmed slashes or null |
|
| 2216 | + * if the path was NOT relative to files |
|
| 2217 | + * |
|
| 2218 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2219 | + * @since 8.1.0 |
|
| 2220 | + */ |
|
| 2221 | + public function getPathRelativeToFiles($absolutePath) { |
|
| 2222 | + $path = Filesystem::normalizePath($absolutePath); |
|
| 2223 | + $parts = explode('/', trim($path, '/'), 3); |
|
| 2224 | + // "$user", "files", "path/to/dir" |
|
| 2225 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2226 | + $this->logger->error( |
|
| 2227 | + '$absolutePath must be relative to "files", value is "{absolutePath}"', |
|
| 2228 | + [ |
|
| 2229 | + 'absolutePath' => $absolutePath, |
|
| 2230 | + ] |
|
| 2231 | + ); |
|
| 2232 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2233 | + } |
|
| 2234 | + if (isset($parts[2])) { |
|
| 2235 | + return $parts[2]; |
|
| 2236 | + } |
|
| 2237 | + return ''; |
|
| 2238 | + } |
|
| 2239 | + |
|
| 2240 | + /** |
|
| 2241 | + * @param string $filename |
|
| 2242 | + * @return array |
|
| 2243 | + * @throws \OC\User\NoUserException |
|
| 2244 | + * @throws NotFoundException |
|
| 2245 | + */ |
|
| 2246 | + public function getUidAndFilename($filename) { |
|
| 2247 | + $info = $this->getFileInfo($filename); |
|
| 2248 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2249 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2250 | + } |
|
| 2251 | + $uid = $info->getOwner()->getUID(); |
|
| 2252 | + if ($uid != \OC_User::getUser()) { |
|
| 2253 | + Filesystem::initMountPoints($uid); |
|
| 2254 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 2255 | + try { |
|
| 2256 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 2257 | + } catch (NotFoundException $e) { |
|
| 2258 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2259 | + } |
|
| 2260 | + } |
|
| 2261 | + return [$uid, $filename]; |
|
| 2262 | + } |
|
| 2263 | + |
|
| 2264 | + /** |
|
| 2265 | + * Creates parent non-existing folders |
|
| 2266 | + * |
|
| 2267 | + * @param string $filePath |
|
| 2268 | + * @return bool |
|
| 2269 | + */ |
|
| 2270 | + private function createParentDirectories($filePath) { |
|
| 2271 | + $directoryParts = explode('/', $filePath); |
|
| 2272 | + $directoryParts = array_filter($directoryParts); |
|
| 2273 | + foreach ($directoryParts as $key => $part) { |
|
| 2274 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2275 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2276 | + if ($this->is_file($currentPath)) { |
|
| 2277 | + return false; |
|
| 2278 | + } |
|
| 2279 | + if (!$this->file_exists($currentPath)) { |
|
| 2280 | + $this->mkdir($currentPath); |
|
| 2281 | + } |
|
| 2282 | + } |
|
| 2283 | + |
|
| 2284 | + return true; |
|
| 2285 | + } |
|
| 2286 | 2286 | } |
@@ -94,9 +94,9 @@ discard block |
||
| 94 | 94 | $path = '/'; |
| 95 | 95 | } |
| 96 | 96 | if ($path[0] !== '/') { |
| 97 | - $path = '/' . $path; |
|
| 97 | + $path = '/'.$path; |
|
| 98 | 98 | } |
| 99 | - return $this->fakeRoot . $path; |
|
| 99 | + return $this->fakeRoot.$path; |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | public function chroot($fakeRoot): void { |
| 108 | 108 | if (!$fakeRoot == '') { |
| 109 | 109 | if ($fakeRoot[0] !== '/') { |
| 110 | - $fakeRoot = '/' . $fakeRoot; |
|
| 110 | + $fakeRoot = '/'.$fakeRoot; |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | $this->fakeRoot = $fakeRoot; |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | // missing slashes can cause wrong matches! |
| 139 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 139 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
| 140 | 140 | |
| 141 | 141 | if (!str_starts_with($path, $root)) { |
| 142 | 142 | return null; |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | * |
| 194 | 194 | * @param string $path |
| 195 | 195 | */ |
| 196 | - public function getLocalFile($path): string|false { |
|
| 196 | + public function getLocalFile($path): string | false { |
|
| 197 | 197 | $parent = substr($path, 0, strrpos($path, '/') ?: 0); |
| 198 | 198 | $path = $this->getAbsolutePath($path); |
| 199 | 199 | [$storage, $internalPath] = Filesystem::resolvePath($path); |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | if ($mount instanceof MoveableMount) { |
| 224 | 224 | // cut of /user/files to get the relative path to data/user/files |
| 225 | 225 | $pathParts = explode('/', $path, 4); |
| 226 | - $relPath = '/' . $pathParts[3]; |
|
| 226 | + $relPath = '/'.$pathParts[3]; |
|
| 227 | 227 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
| 228 | 228 | \OC_Hook::emit( |
| 229 | 229 | Filesystem::CLASSNAME, 'umount', |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | } |
| 668 | 668 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
| 669 | 669 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
| 670 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 670 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
| 671 | 671 | if ($mount->getInternalPath($absolutePath) === '') { |
| 672 | 672 | return $this->removeMount($mount, $absolutePath); |
| 673 | 673 | } |
@@ -710,7 +710,7 @@ discard block |
||
| 710 | 710 | $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source)); |
| 711 | 711 | $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target)); |
| 712 | 712 | |
| 713 | - if (str_starts_with($absolutePath2, $absolutePath1 . '/')) { |
|
| 713 | + if (str_starts_with($absolutePath2, $absolutePath1.'/')) { |
|
| 714 | 714 | throw new ForbiddenException('Moving a folder into a child folder is forbidden', false); |
| 715 | 715 | } |
| 716 | 716 | |
@@ -1028,7 +1028,7 @@ discard block |
||
| 1028 | 1028 | $hooks[] = 'write'; |
| 1029 | 1029 | break; |
| 1030 | 1030 | default: |
| 1031 | - $this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']); |
|
| 1031 | + $this->logger->error('invalid mode ('.$mode.') for '.$path, ['app' => 'core']); |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1052,7 +1052,7 @@ discard block |
||
| 1052 | 1052 | * @param string $path |
| 1053 | 1053 | * @throws InvalidPathException |
| 1054 | 1054 | */ |
| 1055 | - public function toTmpFile($path): string|false { |
|
| 1055 | + public function toTmpFile($path): string | false { |
|
| 1056 | 1056 | $this->assertPathLength($path); |
| 1057 | 1057 | if (Filesystem::isValidPath($path)) { |
| 1058 | 1058 | $source = $this->fopen($path, 'r'); |
@@ -1127,7 +1127,7 @@ discard block |
||
| 1127 | 1127 | * @param string $path |
| 1128 | 1128 | * @param bool $raw |
| 1129 | 1129 | */ |
| 1130 | - public function hash($type, $path, $raw = false): string|bool { |
|
| 1130 | + public function hash($type, $path, $raw = false): string | bool { |
|
| 1131 | 1131 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
| 1132 | 1132 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
| 1133 | 1133 | if (Filesystem::isValidPath($path)) { |
@@ -1143,7 +1143,7 @@ discard block |
||
| 1143 | 1143 | ); |
| 1144 | 1144 | } |
| 1145 | 1145 | /** @var Storage|null $storage */ |
| 1146 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1146 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1147 | 1147 | if ($storage) { |
| 1148 | 1148 | return $storage->hash($type, $internalPath, $raw); |
| 1149 | 1149 | } |
@@ -1193,7 +1193,7 @@ discard block |
||
| 1193 | 1193 | } |
| 1194 | 1194 | |
| 1195 | 1195 | $run = $this->runHooks($hooks, $path); |
| 1196 | - [$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1196 | + [$storage, $internalPath] = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1197 | 1197 | if ($run && $storage) { |
| 1198 | 1198 | /** @var Storage $storage */ |
| 1199 | 1199 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
@@ -1241,7 +1241,7 @@ discard block |
||
| 1241 | 1241 | $unlockLater = true; |
| 1242 | 1242 | // make sure our unlocking callback will still be called if connection is aborted |
| 1243 | 1243 | ignore_user_abort(true); |
| 1244 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1244 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
| 1245 | 1245 | if (in_array('write', $hooks)) { |
| 1246 | 1246 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
| 1247 | 1247 | } elseif (in_array('read', $hooks)) { |
@@ -1303,7 +1303,7 @@ discard block |
||
| 1303 | 1303 | return true; |
| 1304 | 1304 | } |
| 1305 | 1305 | |
| 1306 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1306 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | /** |
@@ -1322,7 +1322,7 @@ discard block |
||
| 1322 | 1322 | if ($hook != 'read') { |
| 1323 | 1323 | \OC_Hook::emit( |
| 1324 | 1324 | Filesystem::CLASSNAME, |
| 1325 | - $prefix . $hook, |
|
| 1325 | + $prefix.$hook, |
|
| 1326 | 1326 | [ |
| 1327 | 1327 | Filesystem::signal_param_run => &$run, |
| 1328 | 1328 | Filesystem::signal_param_path => $path |
@@ -1331,7 +1331,7 @@ discard block |
||
| 1331 | 1331 | } elseif (!$post) { |
| 1332 | 1332 | \OC_Hook::emit( |
| 1333 | 1333 | Filesystem::CLASSNAME, |
| 1334 | - $prefix . $hook, |
|
| 1334 | + $prefix.$hook, |
|
| 1335 | 1335 | [ |
| 1336 | 1336 | Filesystem::signal_param_path => $path |
| 1337 | 1337 | ] |
@@ -1415,7 +1415,7 @@ discard block |
||
| 1415 | 1415 | return false; |
| 1416 | 1416 | } |
| 1417 | 1417 | $relativePath = $path; |
| 1418 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1418 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1419 | 1419 | |
| 1420 | 1420 | $mount = Filesystem::getMountManager()->find($path); |
| 1421 | 1421 | $storage = $mount->getStorage(); |
@@ -1456,7 +1456,7 @@ discard block |
||
| 1456 | 1456 | |
| 1457 | 1457 | return $info; |
| 1458 | 1458 | } else { |
| 1459 | - $this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']); |
|
| 1459 | + $this->logger->warning('Storage not valid for mountpoint: '.$mount->getMountPoint(), ['app' => 'core']); |
|
| 1460 | 1460 | } |
| 1461 | 1461 | |
| 1462 | 1462 | return false; |
@@ -1467,7 +1467,7 @@ discard block |
||
| 1467 | 1467 | */ |
| 1468 | 1468 | public function addSubMounts(FileInfo $info, $extOnly = false): void { |
| 1469 | 1469 | $mounts = Filesystem::getMountManager()->findIn($info->getPath()); |
| 1470 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1470 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
| 1471 | 1471 | return !($extOnly && $mount instanceof SharedMount); |
| 1472 | 1472 | })); |
| 1473 | 1473 | } |
@@ -1515,13 +1515,13 @@ discard block |
||
| 1515 | 1515 | |
| 1516 | 1516 | $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
| 1517 | 1517 | |
| 1518 | - $fileNames = array_map(function (ICacheEntry $content) { |
|
| 1518 | + $fileNames = array_map(function(ICacheEntry $content) { |
|
| 1519 | 1519 | return $content->getName(); |
| 1520 | 1520 | }, $contents); |
| 1521 | 1521 | /** |
| 1522 | 1522 | * @var \OC\Files\FileInfo[] $fileInfos |
| 1523 | 1523 | */ |
| 1524 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1524 | + $fileInfos = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1525 | 1525 | if ($sharingDisabled) { |
| 1526 | 1526 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
| 1527 | 1527 | } |
@@ -1531,7 +1531,7 @@ discard block |
||
| 1531 | 1531 | } else { |
| 1532 | 1532 | $owner = null; |
| 1533 | 1533 | } |
| 1534 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1534 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1535 | 1535 | }, $contents); |
| 1536 | 1536 | $files = array_combine($fileNames, $fileInfos); |
| 1537 | 1537 | |
@@ -1540,7 +1540,7 @@ discard block |
||
| 1540 | 1540 | |
| 1541 | 1541 | // make sure nested mounts are sorted after their parent mounts |
| 1542 | 1542 | // otherwise doesn't propagate the etag across storage boundaries correctly |
| 1543 | - usort($mounts, function (IMountPoint $a, IMountPoint $b) { |
|
| 1543 | + usort($mounts, function(IMountPoint $a, IMountPoint $b) { |
|
| 1544 | 1544 | return $a->getMountPoint() <=> $b->getMountPoint(); |
| 1545 | 1545 | }); |
| 1546 | 1546 | |
@@ -1562,7 +1562,7 @@ discard block |
||
| 1562 | 1562 | continue; |
| 1563 | 1563 | } catch (\Exception $e) { |
| 1564 | 1564 | // sometimes when the storage is not available it can be any exception |
| 1565 | - $this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [ |
|
| 1565 | + $this->logger->error('Exception while scanning storage "'.$subStorage->getId().'"', [ |
|
| 1566 | 1566 | 'exception' => $e, |
| 1567 | 1567 | 'app' => 'core', |
| 1568 | 1568 | ]); |
@@ -1580,14 +1580,14 @@ discard block |
||
| 1580 | 1580 | // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet |
| 1581 | 1581 | if (!isset($files[$entryName])) { |
| 1582 | 1582 | try { |
| 1583 | - [$storage, ] = $this->resolvePath($path . '/' . $entryName); |
|
| 1583 | + [$storage, ] = $this->resolvePath($path.'/'.$entryName); |
|
| 1584 | 1584 | // make sure we can create the mountpoint folder, even if the user has a quota of 0 |
| 1585 | 1585 | if ($storage->instanceOfStorage(Quota::class)) { |
| 1586 | 1586 | $storage->enableQuota(false); |
| 1587 | 1587 | } |
| 1588 | 1588 | |
| 1589 | - if ($this->mkdir($path . '/' . $entryName) !== false) { |
|
| 1590 | - $info = $this->getFileInfo($path . '/' . $entryName); |
|
| 1589 | + if ($this->mkdir($path.'/'.$entryName) !== false) { |
|
| 1590 | + $info = $this->getFileInfo($path.'/'.$entryName); |
|
| 1591 | 1591 | if ($info !== false) { |
| 1592 | 1592 | $files[$entryName] = $info; |
| 1593 | 1593 | } |
@@ -1598,7 +1598,7 @@ discard block |
||
| 1598 | 1598 | } |
| 1599 | 1599 | } catch (\Exception $e) { |
| 1600 | 1600 | // Creating the parent folder might not be possible, for example due to a lack of permissions. |
| 1601 | - $this->logger->debug('Failed to create non-existent parent', ['exception' => $e, 'path' => $path . '/' . $entryName]); |
|
| 1601 | + $this->logger->debug('Failed to create non-existent parent', ['exception' => $e, 'path' => $path.'/'.$entryName]); |
|
| 1602 | 1602 | } |
| 1603 | 1603 | } |
| 1604 | 1604 | |
@@ -1617,7 +1617,7 @@ discard block |
||
| 1617 | 1617 | $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
| 1618 | 1618 | } |
| 1619 | 1619 | |
| 1620 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1620 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1621 | 1621 | |
| 1622 | 1622 | // if sharing was disabled for the user we remove the share permissions |
| 1623 | 1623 | if ($sharingDisabled) { |
@@ -1630,14 +1630,14 @@ discard block |
||
| 1630 | 1630 | } else { |
| 1631 | 1631 | $owner = null; |
| 1632 | 1632 | } |
| 1633 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1633 | + $files[$rootEntry->getName()] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1634 | 1634 | } |
| 1635 | 1635 | } |
| 1636 | 1636 | } |
| 1637 | 1637 | } |
| 1638 | 1638 | |
| 1639 | 1639 | if ($mimetype_filter) { |
| 1640 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1640 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
| 1641 | 1641 | if (strpos($mimetype_filter, '/')) { |
| 1642 | 1642 | return $file->getMimetype() === $mimetype_filter; |
| 1643 | 1643 | } else { |
@@ -1663,7 +1663,7 @@ discard block |
||
| 1663 | 1663 | if ($data instanceof FileInfo) { |
| 1664 | 1664 | $data = $data->getData(); |
| 1665 | 1665 | } |
| 1666 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1666 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1667 | 1667 | /** |
| 1668 | 1668 | * @var Storage $storage |
| 1669 | 1669 | * @var string $internalPath |
@@ -1690,7 +1690,7 @@ discard block |
||
| 1690 | 1690 | * @return FileInfo[] |
| 1691 | 1691 | */ |
| 1692 | 1692 | public function search($query) { |
| 1693 | - return $this->searchCommon('search', ['%' . $query . '%']); |
|
| 1693 | + return $this->searchCommon('search', ['%'.$query.'%']); |
|
| 1694 | 1694 | } |
| 1695 | 1695 | |
| 1696 | 1696 | /** |
@@ -1742,10 +1742,10 @@ discard block |
||
| 1742 | 1742 | |
| 1743 | 1743 | $results = call_user_func_array([$cache, $method], $args); |
| 1744 | 1744 | foreach ($results as $result) { |
| 1745 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1745 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
| 1746 | 1746 | $internalPath = $result['path']; |
| 1747 | - $path = $mountPoint . $result['path']; |
|
| 1748 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1747 | + $path = $mountPoint.$result['path']; |
|
| 1748 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
| 1749 | 1749 | $ownerId = $storage->getOwner($internalPath); |
| 1750 | 1750 | if ($ownerId !== false) { |
| 1751 | 1751 | $owner = $userManager->get($ownerId); |
@@ -1768,8 +1768,8 @@ discard block |
||
| 1768 | 1768 | if ($results) { |
| 1769 | 1769 | foreach ($results as $result) { |
| 1770 | 1770 | $internalPath = $result['path']; |
| 1771 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1772 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1771 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
| 1772 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
| 1773 | 1773 | $ownerId = $storage->getOwner($internalPath); |
| 1774 | 1774 | if ($ownerId !== false) { |
| 1775 | 1775 | $owner = $userManager->get($ownerId); |
@@ -1793,11 +1793,11 @@ discard block |
||
| 1793 | 1793 | public function getOwner(string $path): string { |
| 1794 | 1794 | $info = $this->getFileInfo($path); |
| 1795 | 1795 | if (!$info) { |
| 1796 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1796 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
| 1797 | 1797 | } |
| 1798 | 1798 | |
| 1799 | 1799 | if ($info->getOwner() === null) { |
| 1800 | - throw new NotFoundException($path . ' has no owner'); |
|
| 1800 | + throw new NotFoundException($path.' has no owner'); |
|
| 1801 | 1801 | } |
| 1802 | 1802 | |
| 1803 | 1803 | return $info->getOwner()->getUID(); |
@@ -1829,7 +1829,7 @@ discard block |
||
| 1829 | 1829 | * @throws NotFoundException |
| 1830 | 1830 | */ |
| 1831 | 1831 | public function getPath($id, ?int $storageId = null): string { |
| 1832 | - $id = (int)$id; |
|
| 1832 | + $id = (int) $id; |
|
| 1833 | 1833 | $rootFolder = Server::get(Files\IRootFolder::class); |
| 1834 | 1834 | |
| 1835 | 1835 | $node = $rootFolder->getFirstNodeByIdInPath($id, $this->getRoot()); |
@@ -1881,13 +1881,13 @@ discard block |
||
| 1881 | 1881 | ]; |
| 1882 | 1882 | $shareManager = Server::get(IManager::class); |
| 1883 | 1883 | /** @var IShare[] $shares */ |
| 1884 | - $shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) { |
|
| 1884 | + $shares = array_merge(...array_map(function(int $type) use ($shareManager, $user) { |
|
| 1885 | 1885 | return $shareManager->getSharesBy($user, $type); |
| 1886 | 1886 | }, $providers)); |
| 1887 | 1887 | |
| 1888 | 1888 | foreach ($shares as $share) { |
| 1889 | 1889 | $sharedPath = $share->getNode()->getPath(); |
| 1890 | - if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath . '/')) { |
|
| 1890 | + if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath.'/')) { |
|
| 1891 | 1891 | $this->logger->debug( |
| 1892 | 1892 | 'It is not allowed to move one mount point into a shared folder', |
| 1893 | 1893 | ['app' => 'files']); |
@@ -1995,7 +1995,7 @@ discard block |
||
| 1995 | 1995 | $resultPath = ''; |
| 1996 | 1996 | foreach ($parts as $part) { |
| 1997 | 1997 | if ($part) { |
| 1998 | - $resultPath .= '/' . $part; |
|
| 1998 | + $resultPath .= '/'.$part; |
|
| 1999 | 1999 | $result[] = $resultPath; |
| 2000 | 2000 | } |
| 2001 | 2001 | } |
@@ -2246,16 +2246,16 @@ discard block |
||
| 2246 | 2246 | public function getUidAndFilename($filename) { |
| 2247 | 2247 | $info = $this->getFileInfo($filename); |
| 2248 | 2248 | if (!$info instanceof \OCP\Files\FileInfo) { |
| 2249 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2249 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
| 2250 | 2250 | } |
| 2251 | 2251 | $uid = $info->getOwner()->getUID(); |
| 2252 | 2252 | if ($uid != \OC_User::getUser()) { |
| 2253 | 2253 | Filesystem::initMountPoints($uid); |
| 2254 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2254 | + $ownerView = new View('/'.$uid.'/files'); |
|
| 2255 | 2255 | try { |
| 2256 | 2256 | $filename = $ownerView->getPath($info['fileid']); |
| 2257 | 2257 | } catch (NotFoundException $e) { |
| 2258 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2258 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
| 2259 | 2259 | } |
| 2260 | 2260 | } |
| 2261 | 2261 | return [$uid, $filename]; |
@@ -2272,7 +2272,7 @@ discard block |
||
| 2272 | 2272 | $directoryParts = array_filter($directoryParts); |
| 2273 | 2273 | foreach ($directoryParts as $key => $part) { |
| 2274 | 2274 | $currentPathElements = array_slice($directoryParts, 0, $key); |
| 2275 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2275 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
| 2276 | 2276 | if ($this->is_file($currentPath)) { |
| 2277 | 2277 | return false; |
| 2278 | 2278 | } |
@@ -44,581 +44,581 @@ |
||
| 44 | 44 | |
| 45 | 45 | class OwnershipTransferService { |
| 46 | 46 | |
| 47 | - public function __construct( |
|
| 48 | - private IEncryptionManager $encryptionManager, |
|
| 49 | - private IShareManager $shareManager, |
|
| 50 | - private IMountManager $mountManager, |
|
| 51 | - private IUserMountCache $userMountCache, |
|
| 52 | - private IUserManager $userManager, |
|
| 53 | - private IFactory $l10nFactory, |
|
| 54 | - private IRootFolder $rootFolder, |
|
| 55 | - ) { |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param IUser $sourceUser |
|
| 60 | - * @param IUser $destinationUser |
|
| 61 | - * @param string $path |
|
| 62 | - * |
|
| 63 | - * @param OutputInterface|null $output |
|
| 64 | - * @param bool $move |
|
| 65 | - * @throws TransferOwnershipException |
|
| 66 | - * @throws NoUserException |
|
| 67 | - */ |
|
| 68 | - public function transfer( |
|
| 69 | - IUser $sourceUser, |
|
| 70 | - IUser $destinationUser, |
|
| 71 | - string $path, |
|
| 72 | - ?OutputInterface $output = null, |
|
| 73 | - bool $move = false, |
|
| 74 | - bool $firstLogin = false, |
|
| 75 | - bool $includeExternalStorage = false, |
|
| 76 | - ): void { |
|
| 77 | - $output = $output ?? new NullOutput(); |
|
| 78 | - $sourceUid = $sourceUser->getUID(); |
|
| 79 | - $destinationUid = $destinationUser->getUID(); |
|
| 80 | - $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 81 | - |
|
| 82 | - // If encryption is on we have to ensure the user has logged in before and that all encryption modules are ready |
|
| 83 | - if (($this->encryptionManager->isEnabled() && $destinationUser->getLastLogin() === 0) |
|
| 84 | - || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
|
| 85 | - throw new TransferOwnershipException('The target user is not ready to accept files. The user has at least to have logged in once.', 2); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // setup filesystem |
|
| 89 | - // Requesting the user folder will set it up if the user hasn't logged in before |
|
| 90 | - // We need a setupFS for the full filesystem setup before as otherwise we will just return |
|
| 91 | - // a lazy root folder which does not create the destination users folder |
|
| 92 | - \OC_Util::setupFS($sourceUser->getUID()); |
|
| 93 | - \OC_Util::setupFS($destinationUser->getUID()); |
|
| 94 | - $this->rootFolder->getUserFolder($sourceUser->getUID()); |
|
| 95 | - $this->rootFolder->getUserFolder($destinationUser->getUID()); |
|
| 96 | - Filesystem::initMountPoints($sourceUid); |
|
| 97 | - Filesystem::initMountPoints($destinationUid); |
|
| 98 | - |
|
| 99 | - $view = new View(); |
|
| 100 | - |
|
| 101 | - if ($move) { |
|
| 102 | - $finalTarget = "$destinationUid/files/"; |
|
| 103 | - } else { |
|
| 104 | - $l = $this->l10nFactory->get('files', $this->l10nFactory->getUserLanguage($destinationUser)); |
|
| 105 | - $date = date('Y-m-d H-i-s'); |
|
| 106 | - |
|
| 107 | - $cleanUserName = $this->sanitizeFolderName($sourceUser->getDisplayName()) ?: $sourceUid; |
|
| 108 | - $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$cleanUserName, $date])); |
|
| 109 | - try { |
|
| 110 | - $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
|
| 111 | - } catch (InvalidPathException $e) { |
|
| 112 | - $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$sourceUid, $date])); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { |
|
| 117 | - throw new TransferOwnershipException("Unknown path provided: $path", 1); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if ($move && !$view->is_dir($finalTarget)) { |
|
| 121 | - // Initialize storage |
|
| 122 | - \OC_Util::setupFS($destinationUser->getUID()); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if ($move && !$firstLogin && count($view->getDirectoryContent($finalTarget)) > 0) { |
|
| 126 | - throw new TransferOwnershipException('Destination path does not exists or is not empty', 1); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - // analyse source folder |
|
| 131 | - $this->analyse( |
|
| 132 | - $sourceUid, |
|
| 133 | - $destinationUid, |
|
| 134 | - $sourcePath, |
|
| 135 | - $view, |
|
| 136 | - $output |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - // collect all the shares |
|
| 140 | - $shares = $this->collectUsersShares( |
|
| 141 | - $sourceUid, |
|
| 142 | - $output, |
|
| 143 | - $view, |
|
| 144 | - $sourcePath |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - $sourceSize = $view->getFileInfo($sourcePath)->getSize(); |
|
| 148 | - |
|
| 149 | - // transfer the files |
|
| 150 | - $this->transferFiles( |
|
| 151 | - $sourceUid, |
|
| 152 | - $sourcePath, |
|
| 153 | - $finalTarget, |
|
| 154 | - $view, |
|
| 155 | - $output, |
|
| 156 | - $includeExternalStorage, |
|
| 157 | - ); |
|
| 158 | - $sizeDifference = $sourceSize - $view->getFileInfo($finalTarget)->getSize(); |
|
| 159 | - |
|
| 160 | - // transfer the incoming shares |
|
| 161 | - $sourceShares = $this->collectIncomingShares( |
|
| 162 | - $sourceUid, |
|
| 163 | - $output, |
|
| 164 | - $sourcePath, |
|
| 165 | - ); |
|
| 166 | - $destinationShares = $this->collectIncomingShares( |
|
| 167 | - $destinationUid, |
|
| 168 | - $output, |
|
| 169 | - null, |
|
| 170 | - ); |
|
| 171 | - $this->transferIncomingShares( |
|
| 172 | - $sourceUid, |
|
| 173 | - $destinationUid, |
|
| 174 | - $sourceShares, |
|
| 175 | - $destinationShares, |
|
| 176 | - $output, |
|
| 177 | - $path, |
|
| 178 | - $finalTarget, |
|
| 179 | - $move |
|
| 180 | - ); |
|
| 181 | - |
|
| 182 | - $destinationPath = $finalTarget . '/' . $path; |
|
| 183 | - // restore the shares |
|
| 184 | - $this->restoreShares( |
|
| 185 | - $sourceUid, |
|
| 186 | - $destinationUid, |
|
| 187 | - $destinationPath, |
|
| 188 | - $shares, |
|
| 189 | - $output |
|
| 190 | - ); |
|
| 191 | - if ($sizeDifference !== 0) { |
|
| 192 | - $output->writeln("Transferred folder have a size difference of: $sizeDifference Bytes which means the transfer may be incomplete. Please check the logs if there was any issue during the transfer operation."); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - private function sanitizeFolderName(string $name): string { |
|
| 197 | - // Remove some characters which are prone to cause errors |
|
| 198 | - $name = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $name); |
|
| 199 | - // Replace multiple dashes with one dash |
|
| 200 | - return preg_replace('/-{2,}/s', '-', $name); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - private function walkFiles(View $view, $path, Closure $callBack) { |
|
| 204 | - foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 205 | - if (!$callBack($fileInfo)) { |
|
| 206 | - return; |
|
| 207 | - } |
|
| 208 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 209 | - $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @param OutputInterface $output |
|
| 216 | - * |
|
| 217 | - * @throws TransferOwnershipException |
|
| 218 | - */ |
|
| 219 | - protected function analyse( |
|
| 220 | - string $sourceUid, |
|
| 221 | - string $destinationUid, |
|
| 222 | - string $sourcePath, |
|
| 223 | - View $view, |
|
| 224 | - OutputInterface $output, |
|
| 225 | - bool $includeExternalStorage = false, |
|
| 226 | - ): void { |
|
| 227 | - $output->writeln('Validating quota'); |
|
| 228 | - $sourceFileInfo = $view->getFileInfo($sourcePath, false); |
|
| 229 | - if ($sourceFileInfo === false) { |
|
| 230 | - throw new TransferOwnershipException("Unknown path provided: $sourcePath", 1); |
|
| 231 | - } |
|
| 232 | - $size = $sourceFileInfo->getSize(false); |
|
| 233 | - $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 234 | - if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
|
| 235 | - throw new TransferOwnershipException('Target user does not have enough free space available.', 1); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $output->writeln("Analysing files of $sourceUid ..."); |
|
| 239 | - $progress = new ProgressBar($output); |
|
| 240 | - $progress->start(); |
|
| 241 | - |
|
| 242 | - if ($this->encryptionManager->isEnabled()) { |
|
| 243 | - $masterKeyEnabled = Server::get(Util::class)->isMasterKeyEnabled(); |
|
| 244 | - } else { |
|
| 245 | - $masterKeyEnabled = false; |
|
| 246 | - } |
|
| 247 | - $encryptedFiles = []; |
|
| 248 | - if ($sourceFileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 249 | - if ($sourceFileInfo->isEncrypted()) { |
|
| 250 | - /* Encrypted folder means e2ee encrypted */ |
|
| 251 | - $encryptedFiles[] = $sourceFileInfo; |
|
| 252 | - } else { |
|
| 253 | - $this->walkFiles($view, $sourcePath, |
|
| 254 | - function (FileInfo $fileInfo) use ($progress, $masterKeyEnabled, &$encryptedFiles, $includeExternalStorage) { |
|
| 255 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 256 | - $mount = $fileInfo->getMountPoint(); |
|
| 257 | - // only analyze into folders from main storage, |
|
| 258 | - if ( |
|
| 259 | - $mount->getMountProvider() instanceof IHomeMountProvider |
|
| 260 | - || ($includeExternalStorage && $mount->getMountProvider() instanceof ConfigAdapter) |
|
| 261 | - ) { |
|
| 262 | - if ($fileInfo->isEncrypted()) { |
|
| 263 | - /* Encrypted folder means e2ee encrypted, we cannot transfer it */ |
|
| 264 | - $encryptedFiles[] = $fileInfo; |
|
| 265 | - } |
|
| 266 | - return true; |
|
| 267 | - } else { |
|
| 268 | - return false; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - $progress->advance(); |
|
| 272 | - if ($fileInfo->isEncrypted() && !$masterKeyEnabled) { |
|
| 273 | - /* Encrypted file means SSE, we can only transfer it if master key is enabled */ |
|
| 274 | - $encryptedFiles[] = $fileInfo; |
|
| 275 | - } |
|
| 276 | - return true; |
|
| 277 | - }); |
|
| 278 | - } |
|
| 279 | - } elseif ($sourceFileInfo->isEncrypted() && !$masterKeyEnabled) { |
|
| 280 | - /* Encrypted file means SSE, we can only transfer it if master key is enabled */ |
|
| 281 | - $encryptedFiles[] = $sourceFileInfo; |
|
| 282 | - } |
|
| 283 | - $progress->finish(); |
|
| 284 | - $output->writeln(''); |
|
| 285 | - |
|
| 286 | - // no file is allowed to be encrypted |
|
| 287 | - if (!empty($encryptedFiles)) { |
|
| 288 | - $output->writeln('<error>Some files are encrypted - please decrypt them first.</error>'); |
|
| 289 | - foreach ($encryptedFiles as $encryptedFile) { |
|
| 290 | - /** @var FileInfo $encryptedFile */ |
|
| 291 | - $output->writeln(' ' . $encryptedFile->getPath()); |
|
| 292 | - } |
|
| 293 | - throw new TransferOwnershipException('Some files are encrypted - please decrypt them first.', 1); |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @return array<array{share: IShare, suffix: string}> |
|
| 299 | - */ |
|
| 300 | - private function collectUsersShares( |
|
| 301 | - string $sourceUid, |
|
| 302 | - OutputInterface $output, |
|
| 303 | - View $view, |
|
| 304 | - string $path, |
|
| 305 | - ): array { |
|
| 306 | - $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); |
|
| 307 | - |
|
| 308 | - $shares = []; |
|
| 309 | - $progress = new ProgressBar($output); |
|
| 310 | - |
|
| 311 | - $normalizedPath = Filesystem::normalizePath($path); |
|
| 312 | - |
|
| 313 | - $supportedShareTypes = [ |
|
| 314 | - IShare::TYPE_GROUP, |
|
| 315 | - IShare::TYPE_USER, |
|
| 316 | - IShare::TYPE_LINK, |
|
| 317 | - IShare::TYPE_REMOTE, |
|
| 318 | - IShare::TYPE_ROOM, |
|
| 319 | - IShare::TYPE_EMAIL, |
|
| 320 | - IShare::TYPE_CIRCLE, |
|
| 321 | - IShare::TYPE_DECK, |
|
| 322 | - IShare::TYPE_SCIENCEMESH, |
|
| 323 | - ]; |
|
| 324 | - |
|
| 325 | - foreach ($supportedShareTypes as $shareType) { |
|
| 326 | - $offset = 0; |
|
| 327 | - while (true) { |
|
| 328 | - $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset, onlyValid: false); |
|
| 329 | - $progress->advance(count($sharePage)); |
|
| 330 | - if (empty($sharePage)) { |
|
| 331 | - break; |
|
| 332 | - } |
|
| 333 | - if ($path !== "$sourceUid/files") { |
|
| 334 | - $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) { |
|
| 335 | - try { |
|
| 336 | - $sourceNode = $share->getNode(); |
|
| 337 | - $relativePath = $view->getRelativePath($sourceNode->getPath()); |
|
| 338 | - |
|
| 339 | - return str_starts_with($relativePath . '/', $normalizedPath . '/'); |
|
| 340 | - } catch (Exception $e) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - }); |
|
| 344 | - } |
|
| 345 | - $shares = array_merge($shares, $sharePage); |
|
| 346 | - $offset += 50; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - $progress->finish(); |
|
| 351 | - $output->writeln(''); |
|
| 352 | - |
|
| 353 | - return array_values(array_filter(array_map(function (IShare $share) use ($view, $normalizedPath, $output, $sourceUid) { |
|
| 354 | - try { |
|
| 355 | - $nodePath = $view->getRelativePath($share->getNode()->getPath()); |
|
| 356 | - } catch (NotFoundException $e) { |
|
| 357 | - $output->writeln("<error>Failed to find path for shared file {$share->getNodeId()} for user $sourceUid, skipping</error>"); |
|
| 358 | - return null; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - return [ |
|
| 362 | - 'share' => $share, |
|
| 363 | - 'suffix' => substr(Filesystem::normalizePath($nodePath), strlen($normalizedPath)), |
|
| 364 | - ]; |
|
| 365 | - }, $shares))); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - private function collectIncomingShares( |
|
| 369 | - string $sourceUid, |
|
| 370 | - OutputInterface $output, |
|
| 371 | - ?string $path, |
|
| 372 | - ): array { |
|
| 373 | - $output->writeln("Collecting all incoming share information for files and folders of $sourceUid ..."); |
|
| 374 | - |
|
| 375 | - $shares = []; |
|
| 376 | - $progress = new ProgressBar($output); |
|
| 377 | - $normalizedPath = Filesystem::normalizePath($path); |
|
| 378 | - |
|
| 379 | - $offset = 0; |
|
| 380 | - while (true) { |
|
| 381 | - $sharePage = $this->shareManager->getSharedWith($sourceUid, IShare::TYPE_USER, null, 50, $offset); |
|
| 382 | - $progress->advance(count($sharePage)); |
|
| 383 | - if (empty($sharePage)) { |
|
| 384 | - break; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - if ($path !== null && $path !== "$sourceUid/files") { |
|
| 388 | - $sharePage = array_filter($sharePage, static function (IShare $share) use ($sourceUid, $normalizedPath) { |
|
| 389 | - try { |
|
| 390 | - return str_starts_with(Filesystem::normalizePath($sourceUid . '/files' . $share->getTarget() . '/', false), $normalizedPath . '/'); |
|
| 391 | - } catch (Exception) { |
|
| 392 | - return false; |
|
| 393 | - } |
|
| 394 | - }); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - foreach ($sharePage as $share) { |
|
| 398 | - $shares[$share->getNodeId()] = $share; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - $offset += 50; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - |
|
| 405 | - $progress->finish(); |
|
| 406 | - $output->writeln(''); |
|
| 407 | - return $shares; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * @throws TransferOwnershipException |
|
| 412 | - */ |
|
| 413 | - protected function transferFiles( |
|
| 414 | - string $sourceUid, |
|
| 415 | - string $sourcePath, |
|
| 416 | - string $finalTarget, |
|
| 417 | - View $view, |
|
| 418 | - OutputInterface $output, |
|
| 419 | - bool $includeExternalStorage, |
|
| 420 | - ): void { |
|
| 421 | - $output->writeln("Transferring files to $finalTarget ..."); |
|
| 422 | - |
|
| 423 | - // This change will help user to transfer the folder specified using --path option. |
|
| 424 | - // Else only the content inside folder is transferred which is not correct. |
|
| 425 | - if ($sourcePath !== "$sourceUid/files") { |
|
| 426 | - $view->mkdir($finalTarget); |
|
| 427 | - $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 428 | - } |
|
| 429 | - $sourceInfo = $view->getFileInfo($sourcePath); |
|
| 430 | - |
|
| 431 | - /// handle the external storages mounted at the root, or the admin specifying an external storage with --path |
|
| 432 | - if ($sourceInfo->getInternalPath() === '' && $includeExternalStorage) { |
|
| 433 | - $this->moveMountContents($view, $sourcePath, $finalTarget); |
|
| 434 | - } else { |
|
| 435 | - if ($view->rename($sourcePath, $finalTarget, ['checkSubMounts' => false]) === false) { |
|
| 436 | - throw new TransferOwnershipException('Could not transfer files.', 1); |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - if ($includeExternalStorage) { |
|
| 441 | - $nestedMounts = $this->mountManager->findIn($sourcePath); |
|
| 442 | - foreach ($nestedMounts as $mount) { |
|
| 443 | - if ($mount->getMountProvider() === ConfigAdapter::class) { |
|
| 444 | - $relativePath = substr(trim($mount->getMountPoint(), '/'), strlen($sourcePath)); |
|
| 445 | - $this->moveMountContents($view, $mount->getMountPoint(), $finalTarget . $relativePath); |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - if (!is_dir("$sourceUid/files")) { |
|
| 451 | - // because the files folder is moved away we need to recreate it |
|
| 452 | - $view->mkdir("$sourceUid/files"); |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - private function moveMountContents(View $rootView, string $source, string $target) { |
|
| 457 | - if ($rootView->copy($source, $target)) { |
|
| 458 | - // just doing `rmdir` on the mountpoint would cause it to try and unmount the storage |
|
| 459 | - // we need to empty the contents instead |
|
| 460 | - $content = $rootView->getDirectoryContent($source); |
|
| 461 | - foreach ($content as $item) { |
|
| 462 | - if ($item->getType() === FileInfo::TYPE_FOLDER) { |
|
| 463 | - $rootView->rmdir($item->getPath()); |
|
| 464 | - } else { |
|
| 465 | - $rootView->unlink($item->getPath()); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - } else { |
|
| 469 | - throw new TransferOwnershipException("Could not transfer $source to $target"); |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * @param string $targetLocation New location of the transfered node |
|
| 475 | - * @param array<array{share: IShare, suffix: string}> $shares previously collected share information |
|
| 476 | - */ |
|
| 477 | - private function restoreShares( |
|
| 478 | - string $sourceUid, |
|
| 479 | - string $destinationUid, |
|
| 480 | - string $targetLocation, |
|
| 481 | - array $shares, |
|
| 482 | - OutputInterface $output, |
|
| 483 | - ):void { |
|
| 484 | - $output->writeln('Restoring shares ...'); |
|
| 485 | - $progress = new ProgressBar($output, count($shares)); |
|
| 486 | - |
|
| 487 | - foreach ($shares as ['share' => $share, 'suffix' => $suffix]) { |
|
| 488 | - try { |
|
| 489 | - $output->writeln('Transfering share ' . $share->getId() . ' of type ' . $share->getShareType(), OutputInterface::VERBOSITY_VERBOSE); |
|
| 490 | - if ($share->getShareType() === IShare::TYPE_USER |
|
| 491 | - && $share->getSharedWith() === $destinationUid) { |
|
| 492 | - // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 493 | - $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 494 | - if ($shareMountPoint) { |
|
| 495 | - $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 496 | - } |
|
| 497 | - $this->shareManager->deleteShare($share); |
|
| 498 | - } else { |
|
| 499 | - if ($share->getShareOwner() === $sourceUid) { |
|
| 500 | - $share->setShareOwner($destinationUid); |
|
| 501 | - } |
|
| 502 | - if ($share->getSharedBy() === $sourceUid) { |
|
| 503 | - $share->setSharedBy($destinationUid); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - if ($share->getShareType() === IShare::TYPE_USER |
|
| 507 | - && !$this->userManager->userExists($share->getSharedWith())) { |
|
| 508 | - // stray share with deleted user |
|
| 509 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted user "' . $share->getSharedWith() . '", deleting</error>'); |
|
| 510 | - $this->shareManager->deleteShare($share); |
|
| 511 | - continue; |
|
| 512 | - } else { |
|
| 513 | - // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 514 | - // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 515 | - $this->userMountCache->clear(); |
|
| 516 | - |
|
| 517 | - try { |
|
| 518 | - // Try to get the "old" id. |
|
| 519 | - // Normally the ID is preserved, |
|
| 520 | - // but for transferes between different storages the ID might change |
|
| 521 | - $newNodeId = $share->getNode()->getId(); |
|
| 522 | - } catch (NotFoundException) { |
|
| 523 | - // ID has changed due to transfer between different storages |
|
| 524 | - // Try to get the new ID from the target path and suffix of the share |
|
| 525 | - $node = $this->rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix)); |
|
| 526 | - $newNodeId = $node->getId(); |
|
| 527 | - $output->writeln('Had to change node id to ' . $newNodeId, OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
| 528 | - } |
|
| 529 | - $share->setNodeId($newNodeId); |
|
| 530 | - |
|
| 531 | - $this->shareManager->updateShare($share, onlyValid: false); |
|
| 532 | - } |
|
| 533 | - } |
|
| 534 | - } catch (NotFoundException $e) { |
|
| 535 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 536 | - } catch (\Throwable $e) { |
|
| 537 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getMessage() . ' : ' . $e->getTraceAsString() . '</error>'); |
|
| 538 | - } |
|
| 539 | - $progress->advance(); |
|
| 540 | - } |
|
| 541 | - $progress->finish(); |
|
| 542 | - $output->writeln(''); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - private function transferIncomingShares(string $sourceUid, |
|
| 546 | - string $destinationUid, |
|
| 547 | - array $sourceShares, |
|
| 548 | - array $destinationShares, |
|
| 549 | - OutputInterface $output, |
|
| 550 | - string $path, |
|
| 551 | - string $finalTarget, |
|
| 552 | - bool $move): void { |
|
| 553 | - $output->writeln('Restoring incoming shares ...'); |
|
| 554 | - $progress = new ProgressBar($output, count($sourceShares)); |
|
| 555 | - $prefix = "$destinationUid/files"; |
|
| 556 | - $finalShareTarget = ''; |
|
| 557 | - if (str_starts_with($finalTarget, $prefix)) { |
|
| 558 | - $finalShareTarget = substr($finalTarget, strlen($prefix)); |
|
| 559 | - } |
|
| 560 | - foreach ($sourceShares as $share) { |
|
| 561 | - try { |
|
| 562 | - // Only restore if share is in given path. |
|
| 563 | - $pathToCheck = '/'; |
|
| 564 | - if (trim($path, '/') !== '') { |
|
| 565 | - $pathToCheck = '/' . trim($path) . '/'; |
|
| 566 | - } |
|
| 567 | - if (!str_starts_with($share->getTarget(), $pathToCheck)) { |
|
| 568 | - continue; |
|
| 569 | - } |
|
| 570 | - $shareTarget = $share->getTarget(); |
|
| 571 | - $shareTarget = $finalShareTarget . $shareTarget; |
|
| 572 | - if ($share->getShareType() === IShare::TYPE_USER |
|
| 573 | - && $share->getSharedBy() === $destinationUid) { |
|
| 574 | - $this->shareManager->deleteShare($share); |
|
| 575 | - } elseif (isset($destinationShares[$share->getNodeId()])) { |
|
| 576 | - $destinationShare = $destinationShares[$share->getNodeId()]; |
|
| 577 | - // Keep the share which has the most permissions and discard the other one. |
|
| 578 | - if ($destinationShare->getPermissions() < $share->getPermissions()) { |
|
| 579 | - $this->shareManager->deleteShare($destinationShare); |
|
| 580 | - $share->setSharedWith($destinationUid); |
|
| 581 | - // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 582 | - // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 583 | - $this->userMountCache->clear(); |
|
| 584 | - $share->setNodeId($share->getNode()->getId()); |
|
| 585 | - $this->shareManager->updateShare($share); |
|
| 586 | - // The share is already transferred. |
|
| 587 | - $progress->advance(); |
|
| 588 | - if ($move) { |
|
| 589 | - continue; |
|
| 590 | - } |
|
| 591 | - $share->setTarget($shareTarget); |
|
| 592 | - $this->shareManager->moveShare($share, $destinationUid); |
|
| 593 | - continue; |
|
| 594 | - } |
|
| 595 | - $this->shareManager->deleteShare($share); |
|
| 596 | - } elseif ($share->getShareOwner() === $destinationUid) { |
|
| 597 | - $this->shareManager->deleteShare($share); |
|
| 598 | - } else { |
|
| 599 | - $share->setSharedWith($destinationUid); |
|
| 600 | - $share->setNodeId($share->getNode()->getId()); |
|
| 601 | - $this->shareManager->updateShare($share); |
|
| 602 | - // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 603 | - // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 604 | - $this->userMountCache->clear(); |
|
| 605 | - // The share is already transferred. |
|
| 606 | - $progress->advance(); |
|
| 607 | - if ($move) { |
|
| 608 | - continue; |
|
| 609 | - } |
|
| 610 | - $share->setTarget($shareTarget); |
|
| 611 | - $this->shareManager->moveShare($share, $destinationUid); |
|
| 612 | - continue; |
|
| 613 | - } |
|
| 614 | - } catch (NotFoundException $e) { |
|
| 615 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 616 | - } catch (\Throwable $e) { |
|
| 617 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 618 | - } |
|
| 619 | - $progress->advance(); |
|
| 620 | - } |
|
| 621 | - $progress->finish(); |
|
| 622 | - $output->writeln(''); |
|
| 623 | - } |
|
| 47 | + public function __construct( |
|
| 48 | + private IEncryptionManager $encryptionManager, |
|
| 49 | + private IShareManager $shareManager, |
|
| 50 | + private IMountManager $mountManager, |
|
| 51 | + private IUserMountCache $userMountCache, |
|
| 52 | + private IUserManager $userManager, |
|
| 53 | + private IFactory $l10nFactory, |
|
| 54 | + private IRootFolder $rootFolder, |
|
| 55 | + ) { |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param IUser $sourceUser |
|
| 60 | + * @param IUser $destinationUser |
|
| 61 | + * @param string $path |
|
| 62 | + * |
|
| 63 | + * @param OutputInterface|null $output |
|
| 64 | + * @param bool $move |
|
| 65 | + * @throws TransferOwnershipException |
|
| 66 | + * @throws NoUserException |
|
| 67 | + */ |
|
| 68 | + public function transfer( |
|
| 69 | + IUser $sourceUser, |
|
| 70 | + IUser $destinationUser, |
|
| 71 | + string $path, |
|
| 72 | + ?OutputInterface $output = null, |
|
| 73 | + bool $move = false, |
|
| 74 | + bool $firstLogin = false, |
|
| 75 | + bool $includeExternalStorage = false, |
|
| 76 | + ): void { |
|
| 77 | + $output = $output ?? new NullOutput(); |
|
| 78 | + $sourceUid = $sourceUser->getUID(); |
|
| 79 | + $destinationUid = $destinationUser->getUID(); |
|
| 80 | + $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 81 | + |
|
| 82 | + // If encryption is on we have to ensure the user has logged in before and that all encryption modules are ready |
|
| 83 | + if (($this->encryptionManager->isEnabled() && $destinationUser->getLastLogin() === 0) |
|
| 84 | + || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
|
| 85 | + throw new TransferOwnershipException('The target user is not ready to accept files. The user has at least to have logged in once.', 2); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // setup filesystem |
|
| 89 | + // Requesting the user folder will set it up if the user hasn't logged in before |
|
| 90 | + // We need a setupFS for the full filesystem setup before as otherwise we will just return |
|
| 91 | + // a lazy root folder which does not create the destination users folder |
|
| 92 | + \OC_Util::setupFS($sourceUser->getUID()); |
|
| 93 | + \OC_Util::setupFS($destinationUser->getUID()); |
|
| 94 | + $this->rootFolder->getUserFolder($sourceUser->getUID()); |
|
| 95 | + $this->rootFolder->getUserFolder($destinationUser->getUID()); |
|
| 96 | + Filesystem::initMountPoints($sourceUid); |
|
| 97 | + Filesystem::initMountPoints($destinationUid); |
|
| 98 | + |
|
| 99 | + $view = new View(); |
|
| 100 | + |
|
| 101 | + if ($move) { |
|
| 102 | + $finalTarget = "$destinationUid/files/"; |
|
| 103 | + } else { |
|
| 104 | + $l = $this->l10nFactory->get('files', $this->l10nFactory->getUserLanguage($destinationUser)); |
|
| 105 | + $date = date('Y-m-d H-i-s'); |
|
| 106 | + |
|
| 107 | + $cleanUserName = $this->sanitizeFolderName($sourceUser->getDisplayName()) ?: $sourceUid; |
|
| 108 | + $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$cleanUserName, $date])); |
|
| 109 | + try { |
|
| 110 | + $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
|
| 111 | + } catch (InvalidPathException $e) { |
|
| 112 | + $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$sourceUid, $date])); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { |
|
| 117 | + throw new TransferOwnershipException("Unknown path provided: $path", 1); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if ($move && !$view->is_dir($finalTarget)) { |
|
| 121 | + // Initialize storage |
|
| 122 | + \OC_Util::setupFS($destinationUser->getUID()); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if ($move && !$firstLogin && count($view->getDirectoryContent($finalTarget)) > 0) { |
|
| 126 | + throw new TransferOwnershipException('Destination path does not exists or is not empty', 1); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + // analyse source folder |
|
| 131 | + $this->analyse( |
|
| 132 | + $sourceUid, |
|
| 133 | + $destinationUid, |
|
| 134 | + $sourcePath, |
|
| 135 | + $view, |
|
| 136 | + $output |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + // collect all the shares |
|
| 140 | + $shares = $this->collectUsersShares( |
|
| 141 | + $sourceUid, |
|
| 142 | + $output, |
|
| 143 | + $view, |
|
| 144 | + $sourcePath |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + $sourceSize = $view->getFileInfo($sourcePath)->getSize(); |
|
| 148 | + |
|
| 149 | + // transfer the files |
|
| 150 | + $this->transferFiles( |
|
| 151 | + $sourceUid, |
|
| 152 | + $sourcePath, |
|
| 153 | + $finalTarget, |
|
| 154 | + $view, |
|
| 155 | + $output, |
|
| 156 | + $includeExternalStorage, |
|
| 157 | + ); |
|
| 158 | + $sizeDifference = $sourceSize - $view->getFileInfo($finalTarget)->getSize(); |
|
| 159 | + |
|
| 160 | + // transfer the incoming shares |
|
| 161 | + $sourceShares = $this->collectIncomingShares( |
|
| 162 | + $sourceUid, |
|
| 163 | + $output, |
|
| 164 | + $sourcePath, |
|
| 165 | + ); |
|
| 166 | + $destinationShares = $this->collectIncomingShares( |
|
| 167 | + $destinationUid, |
|
| 168 | + $output, |
|
| 169 | + null, |
|
| 170 | + ); |
|
| 171 | + $this->transferIncomingShares( |
|
| 172 | + $sourceUid, |
|
| 173 | + $destinationUid, |
|
| 174 | + $sourceShares, |
|
| 175 | + $destinationShares, |
|
| 176 | + $output, |
|
| 177 | + $path, |
|
| 178 | + $finalTarget, |
|
| 179 | + $move |
|
| 180 | + ); |
|
| 181 | + |
|
| 182 | + $destinationPath = $finalTarget . '/' . $path; |
|
| 183 | + // restore the shares |
|
| 184 | + $this->restoreShares( |
|
| 185 | + $sourceUid, |
|
| 186 | + $destinationUid, |
|
| 187 | + $destinationPath, |
|
| 188 | + $shares, |
|
| 189 | + $output |
|
| 190 | + ); |
|
| 191 | + if ($sizeDifference !== 0) { |
|
| 192 | + $output->writeln("Transferred folder have a size difference of: $sizeDifference Bytes which means the transfer may be incomplete. Please check the logs if there was any issue during the transfer operation."); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + private function sanitizeFolderName(string $name): string { |
|
| 197 | + // Remove some characters which are prone to cause errors |
|
| 198 | + $name = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $name); |
|
| 199 | + // Replace multiple dashes with one dash |
|
| 200 | + return preg_replace('/-{2,}/s', '-', $name); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + private function walkFiles(View $view, $path, Closure $callBack) { |
|
| 204 | + foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 205 | + if (!$callBack($fileInfo)) { |
|
| 206 | + return; |
|
| 207 | + } |
|
| 208 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 209 | + $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @param OutputInterface $output |
|
| 216 | + * |
|
| 217 | + * @throws TransferOwnershipException |
|
| 218 | + */ |
|
| 219 | + protected function analyse( |
|
| 220 | + string $sourceUid, |
|
| 221 | + string $destinationUid, |
|
| 222 | + string $sourcePath, |
|
| 223 | + View $view, |
|
| 224 | + OutputInterface $output, |
|
| 225 | + bool $includeExternalStorage = false, |
|
| 226 | + ): void { |
|
| 227 | + $output->writeln('Validating quota'); |
|
| 228 | + $sourceFileInfo = $view->getFileInfo($sourcePath, false); |
|
| 229 | + if ($sourceFileInfo === false) { |
|
| 230 | + throw new TransferOwnershipException("Unknown path provided: $sourcePath", 1); |
|
| 231 | + } |
|
| 232 | + $size = $sourceFileInfo->getSize(false); |
|
| 233 | + $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 234 | + if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
|
| 235 | + throw new TransferOwnershipException('Target user does not have enough free space available.', 1); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $output->writeln("Analysing files of $sourceUid ..."); |
|
| 239 | + $progress = new ProgressBar($output); |
|
| 240 | + $progress->start(); |
|
| 241 | + |
|
| 242 | + if ($this->encryptionManager->isEnabled()) { |
|
| 243 | + $masterKeyEnabled = Server::get(Util::class)->isMasterKeyEnabled(); |
|
| 244 | + } else { |
|
| 245 | + $masterKeyEnabled = false; |
|
| 246 | + } |
|
| 247 | + $encryptedFiles = []; |
|
| 248 | + if ($sourceFileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 249 | + if ($sourceFileInfo->isEncrypted()) { |
|
| 250 | + /* Encrypted folder means e2ee encrypted */ |
|
| 251 | + $encryptedFiles[] = $sourceFileInfo; |
|
| 252 | + } else { |
|
| 253 | + $this->walkFiles($view, $sourcePath, |
|
| 254 | + function (FileInfo $fileInfo) use ($progress, $masterKeyEnabled, &$encryptedFiles, $includeExternalStorage) { |
|
| 255 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 256 | + $mount = $fileInfo->getMountPoint(); |
|
| 257 | + // only analyze into folders from main storage, |
|
| 258 | + if ( |
|
| 259 | + $mount->getMountProvider() instanceof IHomeMountProvider |
|
| 260 | + || ($includeExternalStorage && $mount->getMountProvider() instanceof ConfigAdapter) |
|
| 261 | + ) { |
|
| 262 | + if ($fileInfo->isEncrypted()) { |
|
| 263 | + /* Encrypted folder means e2ee encrypted, we cannot transfer it */ |
|
| 264 | + $encryptedFiles[] = $fileInfo; |
|
| 265 | + } |
|
| 266 | + return true; |
|
| 267 | + } else { |
|
| 268 | + return false; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + $progress->advance(); |
|
| 272 | + if ($fileInfo->isEncrypted() && !$masterKeyEnabled) { |
|
| 273 | + /* Encrypted file means SSE, we can only transfer it if master key is enabled */ |
|
| 274 | + $encryptedFiles[] = $fileInfo; |
|
| 275 | + } |
|
| 276 | + return true; |
|
| 277 | + }); |
|
| 278 | + } |
|
| 279 | + } elseif ($sourceFileInfo->isEncrypted() && !$masterKeyEnabled) { |
|
| 280 | + /* Encrypted file means SSE, we can only transfer it if master key is enabled */ |
|
| 281 | + $encryptedFiles[] = $sourceFileInfo; |
|
| 282 | + } |
|
| 283 | + $progress->finish(); |
|
| 284 | + $output->writeln(''); |
|
| 285 | + |
|
| 286 | + // no file is allowed to be encrypted |
|
| 287 | + if (!empty($encryptedFiles)) { |
|
| 288 | + $output->writeln('<error>Some files are encrypted - please decrypt them first.</error>'); |
|
| 289 | + foreach ($encryptedFiles as $encryptedFile) { |
|
| 290 | + /** @var FileInfo $encryptedFile */ |
|
| 291 | + $output->writeln(' ' . $encryptedFile->getPath()); |
|
| 292 | + } |
|
| 293 | + throw new TransferOwnershipException('Some files are encrypted - please decrypt them first.', 1); |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @return array<array{share: IShare, suffix: string}> |
|
| 299 | + */ |
|
| 300 | + private function collectUsersShares( |
|
| 301 | + string $sourceUid, |
|
| 302 | + OutputInterface $output, |
|
| 303 | + View $view, |
|
| 304 | + string $path, |
|
| 305 | + ): array { |
|
| 306 | + $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); |
|
| 307 | + |
|
| 308 | + $shares = []; |
|
| 309 | + $progress = new ProgressBar($output); |
|
| 310 | + |
|
| 311 | + $normalizedPath = Filesystem::normalizePath($path); |
|
| 312 | + |
|
| 313 | + $supportedShareTypes = [ |
|
| 314 | + IShare::TYPE_GROUP, |
|
| 315 | + IShare::TYPE_USER, |
|
| 316 | + IShare::TYPE_LINK, |
|
| 317 | + IShare::TYPE_REMOTE, |
|
| 318 | + IShare::TYPE_ROOM, |
|
| 319 | + IShare::TYPE_EMAIL, |
|
| 320 | + IShare::TYPE_CIRCLE, |
|
| 321 | + IShare::TYPE_DECK, |
|
| 322 | + IShare::TYPE_SCIENCEMESH, |
|
| 323 | + ]; |
|
| 324 | + |
|
| 325 | + foreach ($supportedShareTypes as $shareType) { |
|
| 326 | + $offset = 0; |
|
| 327 | + while (true) { |
|
| 328 | + $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset, onlyValid: false); |
|
| 329 | + $progress->advance(count($sharePage)); |
|
| 330 | + if (empty($sharePage)) { |
|
| 331 | + break; |
|
| 332 | + } |
|
| 333 | + if ($path !== "$sourceUid/files") { |
|
| 334 | + $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) { |
|
| 335 | + try { |
|
| 336 | + $sourceNode = $share->getNode(); |
|
| 337 | + $relativePath = $view->getRelativePath($sourceNode->getPath()); |
|
| 338 | + |
|
| 339 | + return str_starts_with($relativePath . '/', $normalizedPath . '/'); |
|
| 340 | + } catch (Exception $e) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + }); |
|
| 344 | + } |
|
| 345 | + $shares = array_merge($shares, $sharePage); |
|
| 346 | + $offset += 50; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + $progress->finish(); |
|
| 351 | + $output->writeln(''); |
|
| 352 | + |
|
| 353 | + return array_values(array_filter(array_map(function (IShare $share) use ($view, $normalizedPath, $output, $sourceUid) { |
|
| 354 | + try { |
|
| 355 | + $nodePath = $view->getRelativePath($share->getNode()->getPath()); |
|
| 356 | + } catch (NotFoundException $e) { |
|
| 357 | + $output->writeln("<error>Failed to find path for shared file {$share->getNodeId()} for user $sourceUid, skipping</error>"); |
|
| 358 | + return null; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + return [ |
|
| 362 | + 'share' => $share, |
|
| 363 | + 'suffix' => substr(Filesystem::normalizePath($nodePath), strlen($normalizedPath)), |
|
| 364 | + ]; |
|
| 365 | + }, $shares))); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + private function collectIncomingShares( |
|
| 369 | + string $sourceUid, |
|
| 370 | + OutputInterface $output, |
|
| 371 | + ?string $path, |
|
| 372 | + ): array { |
|
| 373 | + $output->writeln("Collecting all incoming share information for files and folders of $sourceUid ..."); |
|
| 374 | + |
|
| 375 | + $shares = []; |
|
| 376 | + $progress = new ProgressBar($output); |
|
| 377 | + $normalizedPath = Filesystem::normalizePath($path); |
|
| 378 | + |
|
| 379 | + $offset = 0; |
|
| 380 | + while (true) { |
|
| 381 | + $sharePage = $this->shareManager->getSharedWith($sourceUid, IShare::TYPE_USER, null, 50, $offset); |
|
| 382 | + $progress->advance(count($sharePage)); |
|
| 383 | + if (empty($sharePage)) { |
|
| 384 | + break; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + if ($path !== null && $path !== "$sourceUid/files") { |
|
| 388 | + $sharePage = array_filter($sharePage, static function (IShare $share) use ($sourceUid, $normalizedPath) { |
|
| 389 | + try { |
|
| 390 | + return str_starts_with(Filesystem::normalizePath($sourceUid . '/files' . $share->getTarget() . '/', false), $normalizedPath . '/'); |
|
| 391 | + } catch (Exception) { |
|
| 392 | + return false; |
|
| 393 | + } |
|
| 394 | + }); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + foreach ($sharePage as $share) { |
|
| 398 | + $shares[$share->getNodeId()] = $share; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + $offset += 50; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + |
|
| 405 | + $progress->finish(); |
|
| 406 | + $output->writeln(''); |
|
| 407 | + return $shares; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * @throws TransferOwnershipException |
|
| 412 | + */ |
|
| 413 | + protected function transferFiles( |
|
| 414 | + string $sourceUid, |
|
| 415 | + string $sourcePath, |
|
| 416 | + string $finalTarget, |
|
| 417 | + View $view, |
|
| 418 | + OutputInterface $output, |
|
| 419 | + bool $includeExternalStorage, |
|
| 420 | + ): void { |
|
| 421 | + $output->writeln("Transferring files to $finalTarget ..."); |
|
| 422 | + |
|
| 423 | + // This change will help user to transfer the folder specified using --path option. |
|
| 424 | + // Else only the content inside folder is transferred which is not correct. |
|
| 425 | + if ($sourcePath !== "$sourceUid/files") { |
|
| 426 | + $view->mkdir($finalTarget); |
|
| 427 | + $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 428 | + } |
|
| 429 | + $sourceInfo = $view->getFileInfo($sourcePath); |
|
| 430 | + |
|
| 431 | + /// handle the external storages mounted at the root, or the admin specifying an external storage with --path |
|
| 432 | + if ($sourceInfo->getInternalPath() === '' && $includeExternalStorage) { |
|
| 433 | + $this->moveMountContents($view, $sourcePath, $finalTarget); |
|
| 434 | + } else { |
|
| 435 | + if ($view->rename($sourcePath, $finalTarget, ['checkSubMounts' => false]) === false) { |
|
| 436 | + throw new TransferOwnershipException('Could not transfer files.', 1); |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + if ($includeExternalStorage) { |
|
| 441 | + $nestedMounts = $this->mountManager->findIn($sourcePath); |
|
| 442 | + foreach ($nestedMounts as $mount) { |
|
| 443 | + if ($mount->getMountProvider() === ConfigAdapter::class) { |
|
| 444 | + $relativePath = substr(trim($mount->getMountPoint(), '/'), strlen($sourcePath)); |
|
| 445 | + $this->moveMountContents($view, $mount->getMountPoint(), $finalTarget . $relativePath); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + if (!is_dir("$sourceUid/files")) { |
|
| 451 | + // because the files folder is moved away we need to recreate it |
|
| 452 | + $view->mkdir("$sourceUid/files"); |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + private function moveMountContents(View $rootView, string $source, string $target) { |
|
| 457 | + if ($rootView->copy($source, $target)) { |
|
| 458 | + // just doing `rmdir` on the mountpoint would cause it to try and unmount the storage |
|
| 459 | + // we need to empty the contents instead |
|
| 460 | + $content = $rootView->getDirectoryContent($source); |
|
| 461 | + foreach ($content as $item) { |
|
| 462 | + if ($item->getType() === FileInfo::TYPE_FOLDER) { |
|
| 463 | + $rootView->rmdir($item->getPath()); |
|
| 464 | + } else { |
|
| 465 | + $rootView->unlink($item->getPath()); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + } else { |
|
| 469 | + throw new TransferOwnershipException("Could not transfer $source to $target"); |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * @param string $targetLocation New location of the transfered node |
|
| 475 | + * @param array<array{share: IShare, suffix: string}> $shares previously collected share information |
|
| 476 | + */ |
|
| 477 | + private function restoreShares( |
|
| 478 | + string $sourceUid, |
|
| 479 | + string $destinationUid, |
|
| 480 | + string $targetLocation, |
|
| 481 | + array $shares, |
|
| 482 | + OutputInterface $output, |
|
| 483 | + ):void { |
|
| 484 | + $output->writeln('Restoring shares ...'); |
|
| 485 | + $progress = new ProgressBar($output, count($shares)); |
|
| 486 | + |
|
| 487 | + foreach ($shares as ['share' => $share, 'suffix' => $suffix]) { |
|
| 488 | + try { |
|
| 489 | + $output->writeln('Transfering share ' . $share->getId() . ' of type ' . $share->getShareType(), OutputInterface::VERBOSITY_VERBOSE); |
|
| 490 | + if ($share->getShareType() === IShare::TYPE_USER |
|
| 491 | + && $share->getSharedWith() === $destinationUid) { |
|
| 492 | + // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 493 | + $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 494 | + if ($shareMountPoint) { |
|
| 495 | + $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 496 | + } |
|
| 497 | + $this->shareManager->deleteShare($share); |
|
| 498 | + } else { |
|
| 499 | + if ($share->getShareOwner() === $sourceUid) { |
|
| 500 | + $share->setShareOwner($destinationUid); |
|
| 501 | + } |
|
| 502 | + if ($share->getSharedBy() === $sourceUid) { |
|
| 503 | + $share->setSharedBy($destinationUid); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + if ($share->getShareType() === IShare::TYPE_USER |
|
| 507 | + && !$this->userManager->userExists($share->getSharedWith())) { |
|
| 508 | + // stray share with deleted user |
|
| 509 | + $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted user "' . $share->getSharedWith() . '", deleting</error>'); |
|
| 510 | + $this->shareManager->deleteShare($share); |
|
| 511 | + continue; |
|
| 512 | + } else { |
|
| 513 | + // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 514 | + // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 515 | + $this->userMountCache->clear(); |
|
| 516 | + |
|
| 517 | + try { |
|
| 518 | + // Try to get the "old" id. |
|
| 519 | + // Normally the ID is preserved, |
|
| 520 | + // but for transferes between different storages the ID might change |
|
| 521 | + $newNodeId = $share->getNode()->getId(); |
|
| 522 | + } catch (NotFoundException) { |
|
| 523 | + // ID has changed due to transfer between different storages |
|
| 524 | + // Try to get the new ID from the target path and suffix of the share |
|
| 525 | + $node = $this->rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix)); |
|
| 526 | + $newNodeId = $node->getId(); |
|
| 527 | + $output->writeln('Had to change node id to ' . $newNodeId, OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
| 528 | + } |
|
| 529 | + $share->setNodeId($newNodeId); |
|
| 530 | + |
|
| 531 | + $this->shareManager->updateShare($share, onlyValid: false); |
|
| 532 | + } |
|
| 533 | + } |
|
| 534 | + } catch (NotFoundException $e) { |
|
| 535 | + $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 536 | + } catch (\Throwable $e) { |
|
| 537 | + $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getMessage() . ' : ' . $e->getTraceAsString() . '</error>'); |
|
| 538 | + } |
|
| 539 | + $progress->advance(); |
|
| 540 | + } |
|
| 541 | + $progress->finish(); |
|
| 542 | + $output->writeln(''); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + private function transferIncomingShares(string $sourceUid, |
|
| 546 | + string $destinationUid, |
|
| 547 | + array $sourceShares, |
|
| 548 | + array $destinationShares, |
|
| 549 | + OutputInterface $output, |
|
| 550 | + string $path, |
|
| 551 | + string $finalTarget, |
|
| 552 | + bool $move): void { |
|
| 553 | + $output->writeln('Restoring incoming shares ...'); |
|
| 554 | + $progress = new ProgressBar($output, count($sourceShares)); |
|
| 555 | + $prefix = "$destinationUid/files"; |
|
| 556 | + $finalShareTarget = ''; |
|
| 557 | + if (str_starts_with($finalTarget, $prefix)) { |
|
| 558 | + $finalShareTarget = substr($finalTarget, strlen($prefix)); |
|
| 559 | + } |
|
| 560 | + foreach ($sourceShares as $share) { |
|
| 561 | + try { |
|
| 562 | + // Only restore if share is in given path. |
|
| 563 | + $pathToCheck = '/'; |
|
| 564 | + if (trim($path, '/') !== '') { |
|
| 565 | + $pathToCheck = '/' . trim($path) . '/'; |
|
| 566 | + } |
|
| 567 | + if (!str_starts_with($share->getTarget(), $pathToCheck)) { |
|
| 568 | + continue; |
|
| 569 | + } |
|
| 570 | + $shareTarget = $share->getTarget(); |
|
| 571 | + $shareTarget = $finalShareTarget . $shareTarget; |
|
| 572 | + if ($share->getShareType() === IShare::TYPE_USER |
|
| 573 | + && $share->getSharedBy() === $destinationUid) { |
|
| 574 | + $this->shareManager->deleteShare($share); |
|
| 575 | + } elseif (isset($destinationShares[$share->getNodeId()])) { |
|
| 576 | + $destinationShare = $destinationShares[$share->getNodeId()]; |
|
| 577 | + // Keep the share which has the most permissions and discard the other one. |
|
| 578 | + if ($destinationShare->getPermissions() < $share->getPermissions()) { |
|
| 579 | + $this->shareManager->deleteShare($destinationShare); |
|
| 580 | + $share->setSharedWith($destinationUid); |
|
| 581 | + // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 582 | + // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 583 | + $this->userMountCache->clear(); |
|
| 584 | + $share->setNodeId($share->getNode()->getId()); |
|
| 585 | + $this->shareManager->updateShare($share); |
|
| 586 | + // The share is already transferred. |
|
| 587 | + $progress->advance(); |
|
| 588 | + if ($move) { |
|
| 589 | + continue; |
|
| 590 | + } |
|
| 591 | + $share->setTarget($shareTarget); |
|
| 592 | + $this->shareManager->moveShare($share, $destinationUid); |
|
| 593 | + continue; |
|
| 594 | + } |
|
| 595 | + $this->shareManager->deleteShare($share); |
|
| 596 | + } elseif ($share->getShareOwner() === $destinationUid) { |
|
| 597 | + $this->shareManager->deleteShare($share); |
|
| 598 | + } else { |
|
| 599 | + $share->setSharedWith($destinationUid); |
|
| 600 | + $share->setNodeId($share->getNode()->getId()); |
|
| 601 | + $this->shareManager->updateShare($share); |
|
| 602 | + // trigger refetching of the node so that the new owner and mountpoint are taken into account |
|
| 603 | + // otherwise the checks on the share update will fail due to the original node not being available in the new user scope |
|
| 604 | + $this->userMountCache->clear(); |
|
| 605 | + // The share is already transferred. |
|
| 606 | + $progress->advance(); |
|
| 607 | + if ($move) { |
|
| 608 | + continue; |
|
| 609 | + } |
|
| 610 | + $share->setTarget($shareTarget); |
|
| 611 | + $this->shareManager->moveShare($share, $destinationUid); |
|
| 612 | + continue; |
|
| 613 | + } |
|
| 614 | + } catch (NotFoundException $e) { |
|
| 615 | + $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 616 | + } catch (\Throwable $e) { |
|
| 617 | + $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 618 | + } |
|
| 619 | + $progress->advance(); |
|
| 620 | + } |
|
| 621 | + $progress->finish(); |
|
| 622 | + $output->writeln(''); |
|
| 623 | + } |
|
| 624 | 624 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | $output = $output ?? new NullOutput(); |
| 78 | 78 | $sourceUid = $sourceUser->getUID(); |
| 79 | 79 | $destinationUid = $destinationUser->getUID(); |
| 80 | - $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 80 | + $sourcePath = rtrim($sourceUid.'/files/'.$path, '/'); |
|
| 81 | 81 | |
| 82 | 82 | // If encryption is on we have to ensure the user has logged in before and that all encryption modules are ready |
| 83 | 83 | if (($this->encryptionManager->isEnabled() && $destinationUser->getLastLogin() === 0) |
@@ -105,11 +105,11 @@ discard block |
||
| 105 | 105 | $date = date('Y-m-d H-i-s'); |
| 106 | 106 | |
| 107 | 107 | $cleanUserName = $this->sanitizeFolderName($sourceUser->getDisplayName()) ?: $sourceUid; |
| 108 | - $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$cleanUserName, $date])); |
|
| 108 | + $finalTarget = "$destinationUid/files/".$this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$cleanUserName, $date])); |
|
| 109 | 109 | try { |
| 110 | 110 | $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
| 111 | 111 | } catch (InvalidPathException $e) { |
| 112 | - $finalTarget = "$destinationUid/files/" . $this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$sourceUid, $date])); |
|
| 112 | + $finalTarget = "$destinationUid/files/".$this->sanitizeFolderName($l->t('Transferred from %1$s on %2$s', [$sourceUid, $date])); |
|
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | $move |
| 180 | 180 | ); |
| 181 | 181 | |
| 182 | - $destinationPath = $finalTarget . '/' . $path; |
|
| 182 | + $destinationPath = $finalTarget.'/'.$path; |
|
| 183 | 183 | // restore the shares |
| 184 | 184 | $this->restoreShares( |
| 185 | 185 | $sourceUid, |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | throw new TransferOwnershipException("Unknown path provided: $sourcePath", 1); |
| 231 | 231 | } |
| 232 | 232 | $size = $sourceFileInfo->getSize(false); |
| 233 | - $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 233 | + $freeSpace = $view->free_space($destinationUid.'/files/'); |
|
| 234 | 234 | if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
| 235 | 235 | throw new TransferOwnershipException('Target user does not have enough free space available.', 1); |
| 236 | 236 | } |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | $encryptedFiles[] = $sourceFileInfo; |
| 252 | 252 | } else { |
| 253 | 253 | $this->walkFiles($view, $sourcePath, |
| 254 | - function (FileInfo $fileInfo) use ($progress, $masterKeyEnabled, &$encryptedFiles, $includeExternalStorage) { |
|
| 254 | + function(FileInfo $fileInfo) use ($progress, $masterKeyEnabled, &$encryptedFiles, $includeExternalStorage) { |
|
| 255 | 255 | if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
| 256 | 256 | $mount = $fileInfo->getMountPoint(); |
| 257 | 257 | // only analyze into folders from main storage, |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | $output->writeln('<error>Some files are encrypted - please decrypt them first.</error>'); |
| 289 | 289 | foreach ($encryptedFiles as $encryptedFile) { |
| 290 | 290 | /** @var FileInfo $encryptedFile */ |
| 291 | - $output->writeln(' ' . $encryptedFile->getPath()); |
|
| 291 | + $output->writeln(' '.$encryptedFile->getPath()); |
|
| 292 | 292 | } |
| 293 | 293 | throw new TransferOwnershipException('Some files are encrypted - please decrypt them first.', 1); |
| 294 | 294 | } |
@@ -331,12 +331,12 @@ discard block |
||
| 331 | 331 | break; |
| 332 | 332 | } |
| 333 | 333 | if ($path !== "$sourceUid/files") { |
| 334 | - $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) { |
|
| 334 | + $sharePage = array_filter($sharePage, function(IShare $share) use ($view, $normalizedPath) { |
|
| 335 | 335 | try { |
| 336 | 336 | $sourceNode = $share->getNode(); |
| 337 | 337 | $relativePath = $view->getRelativePath($sourceNode->getPath()); |
| 338 | 338 | |
| 339 | - return str_starts_with($relativePath . '/', $normalizedPath . '/'); |
|
| 339 | + return str_starts_with($relativePath.'/', $normalizedPath.'/'); |
|
| 340 | 340 | } catch (Exception $e) { |
| 341 | 341 | return false; |
| 342 | 342 | } |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | $progress->finish(); |
| 351 | 351 | $output->writeln(''); |
| 352 | 352 | |
| 353 | - return array_values(array_filter(array_map(function (IShare $share) use ($view, $normalizedPath, $output, $sourceUid) { |
|
| 353 | + return array_values(array_filter(array_map(function(IShare $share) use ($view, $normalizedPath, $output, $sourceUid) { |
|
| 354 | 354 | try { |
| 355 | 355 | $nodePath = $view->getRelativePath($share->getNode()->getPath()); |
| 356 | 356 | } catch (NotFoundException $e) { |
@@ -385,9 +385,9 @@ discard block |
||
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | if ($path !== null && $path !== "$sourceUid/files") { |
| 388 | - $sharePage = array_filter($sharePage, static function (IShare $share) use ($sourceUid, $normalizedPath) { |
|
| 388 | + $sharePage = array_filter($sharePage, static function(IShare $share) use ($sourceUid, $normalizedPath) { |
|
| 389 | 389 | try { |
| 390 | - return str_starts_with(Filesystem::normalizePath($sourceUid . '/files' . $share->getTarget() . '/', false), $normalizedPath . '/'); |
|
| 390 | + return str_starts_with(Filesystem::normalizePath($sourceUid.'/files'.$share->getTarget().'/', false), $normalizedPath.'/'); |
|
| 391 | 391 | } catch (Exception) { |
| 392 | 392 | return false; |
| 393 | 393 | } |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | // Else only the content inside folder is transferred which is not correct. |
| 425 | 425 | if ($sourcePath !== "$sourceUid/files") { |
| 426 | 426 | $view->mkdir($finalTarget); |
| 427 | - $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 427 | + $finalTarget = $finalTarget.'/'.basename($sourcePath); |
|
| 428 | 428 | } |
| 429 | 429 | $sourceInfo = $view->getFileInfo($sourcePath); |
| 430 | 430 | |
@@ -442,7 +442,7 @@ discard block |
||
| 442 | 442 | foreach ($nestedMounts as $mount) { |
| 443 | 443 | if ($mount->getMountProvider() === ConfigAdapter::class) { |
| 444 | 444 | $relativePath = substr(trim($mount->getMountPoint(), '/'), strlen($sourcePath)); |
| 445 | - $this->moveMountContents($view, $mount->getMountPoint(), $finalTarget . $relativePath); |
|
| 445 | + $this->moveMountContents($view, $mount->getMountPoint(), $finalTarget.$relativePath); |
|
| 446 | 446 | } |
| 447 | 447 | } |
| 448 | 448 | } |
@@ -486,11 +486,11 @@ discard block |
||
| 486 | 486 | |
| 487 | 487 | foreach ($shares as ['share' => $share, 'suffix' => $suffix]) { |
| 488 | 488 | try { |
| 489 | - $output->writeln('Transfering share ' . $share->getId() . ' of type ' . $share->getShareType(), OutputInterface::VERBOSITY_VERBOSE); |
|
| 489 | + $output->writeln('Transfering share '.$share->getId().' of type '.$share->getShareType(), OutputInterface::VERBOSITY_VERBOSE); |
|
| 490 | 490 | if ($share->getShareType() === IShare::TYPE_USER |
| 491 | 491 | && $share->getSharedWith() === $destinationUid) { |
| 492 | 492 | // Unmount the shares before deleting, so we don't try to get the storage later on. |
| 493 | - $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 493 | + $shareMountPoint = $this->mountManager->find('/'.$destinationUid.'/files'.$share->getTarget()); |
|
| 494 | 494 | if ($shareMountPoint) { |
| 495 | 495 | $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
| 496 | 496 | } |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | if ($share->getShareType() === IShare::TYPE_USER |
| 507 | 507 | && !$this->userManager->userExists($share->getSharedWith())) { |
| 508 | 508 | // stray share with deleted user |
| 509 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted user "' . $share->getSharedWith() . '", deleting</error>'); |
|
| 509 | + $output->writeln('<error>Share with id '.$share->getId().' points at deleted user "'.$share->getSharedWith().'", deleting</error>'); |
|
| 510 | 510 | $this->shareManager->deleteShare($share); |
| 511 | 511 | continue; |
| 512 | 512 | } else { |
@@ -522,9 +522,9 @@ discard block |
||
| 522 | 522 | } catch (NotFoundException) { |
| 523 | 523 | // ID has changed due to transfer between different storages |
| 524 | 524 | // Try to get the new ID from the target path and suffix of the share |
| 525 | - $node = $this->rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix)); |
|
| 525 | + $node = $this->rootFolder->get(Filesystem::normalizePath($targetLocation.'/'.$suffix)); |
|
| 526 | 526 | $newNodeId = $node->getId(); |
| 527 | - $output->writeln('Had to change node id to ' . $newNodeId, OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
| 527 | + $output->writeln('Had to change node id to '.$newNodeId, OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
| 528 | 528 | } |
| 529 | 529 | $share->setNodeId($newNodeId); |
| 530 | 530 | |
@@ -532,9 +532,9 @@ discard block |
||
| 532 | 532 | } |
| 533 | 533 | } |
| 534 | 534 | } catch (NotFoundException $e) { |
| 535 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 535 | + $output->writeln('<error>Share with id '.$share->getId().' points at deleted file, skipping</error>'); |
|
| 536 | 536 | } catch (\Throwable $e) { |
| 537 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getMessage() . ' : ' . $e->getTraceAsString() . '</error>'); |
|
| 537 | + $output->writeln('<error>Could not restore share with id '.$share->getId().':'.$e->getMessage().' : '.$e->getTraceAsString().'</error>'); |
|
| 538 | 538 | } |
| 539 | 539 | $progress->advance(); |
| 540 | 540 | } |
@@ -562,13 +562,13 @@ discard block |
||
| 562 | 562 | // Only restore if share is in given path. |
| 563 | 563 | $pathToCheck = '/'; |
| 564 | 564 | if (trim($path, '/') !== '') { |
| 565 | - $pathToCheck = '/' . trim($path) . '/'; |
|
| 565 | + $pathToCheck = '/'.trim($path).'/'; |
|
| 566 | 566 | } |
| 567 | 567 | if (!str_starts_with($share->getTarget(), $pathToCheck)) { |
| 568 | 568 | continue; |
| 569 | 569 | } |
| 570 | 570 | $shareTarget = $share->getTarget(); |
| 571 | - $shareTarget = $finalShareTarget . $shareTarget; |
|
| 571 | + $shareTarget = $finalShareTarget.$shareTarget; |
|
| 572 | 572 | if ($share->getShareType() === IShare::TYPE_USER |
| 573 | 573 | && $share->getSharedBy() === $destinationUid) { |
| 574 | 574 | $this->shareManager->deleteShare($share); |
@@ -612,9 +612,9 @@ discard block |
||
| 612 | 612 | continue; |
| 613 | 613 | } |
| 614 | 614 | } catch (NotFoundException $e) { |
| 615 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 615 | + $output->writeln('<error>Share with id '.$share->getId().' points at deleted file, skipping</error>'); |
|
| 616 | 616 | } catch (\Throwable $e) { |
| 617 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 617 | + $output->writeln('<error>Could not restore share with id '.$share->getId().':'.$e->getTraceAsString().'</error>'); |
|
| 618 | 618 | } |
| 619 | 619 | $progress->advance(); |
| 620 | 620 | } |
@@ -51,40 +51,40 @@ discard block |
||
| 51 | 51 | use Test\Traits\UserTrait; |
| 52 | 52 | |
| 53 | 53 | class TemporaryNoTouch extends Temporary { |
| 54 | - public function touch(string $path, ?int $mtime = null): bool { |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 54 | + public function touch(string $path, ?int $mtime = null): bool { |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | class TemporaryNoCross extends Temporary { |
| 60 | - public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool { |
|
| 61 | - return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime); |
|
| 62 | - } |
|
| 60 | + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool { |
|
| 61 | + return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
|
| 65 | - return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 66 | - } |
|
| 64 | + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
|
| 65 | + return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 66 | + } |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | class TemporaryNoLocal extends Temporary { |
| 70 | - public function instanceOfStorage(string $class): bool { |
|
| 71 | - if ($class === '\OC\Files\Storage\Local') { |
|
| 72 | - return false; |
|
| 73 | - } else { |
|
| 74 | - return parent::instanceOfStorage($class); |
|
| 75 | - } |
|
| 76 | - } |
|
| 70 | + public function instanceOfStorage(string $class): bool { |
|
| 71 | + if ($class === '\OC\Files\Storage\Local') { |
|
| 72 | + return false; |
|
| 73 | + } else { |
|
| 74 | + return parent::instanceOfStorage($class); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | class TestEventHandler { |
| 80 | - public function umount() { |
|
| 81 | - } |
|
| 82 | - public function post_umount() { |
|
| 83 | - } |
|
| 84 | - public function preCallback() { |
|
| 85 | - } |
|
| 86 | - public function postCallback() { |
|
| 87 | - } |
|
| 80 | + public function umount() { |
|
| 81 | + } |
|
| 82 | + public function post_umount() { |
|
| 83 | + } |
|
| 84 | + public function preCallback() { |
|
| 85 | + } |
|
| 86 | + public function postCallback() { |
|
| 87 | + } |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
@@ -95,2809 +95,2809 @@ discard block |
||
| 95 | 95 | * @package Test\Files |
| 96 | 96 | */ |
| 97 | 97 | class ViewTest extends \Test\TestCase { |
| 98 | - use UserTrait; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @var Storage[] $storages |
|
| 102 | - */ |
|
| 103 | - private $storages = []; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @var string |
|
| 107 | - */ |
|
| 108 | - private $user; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @var IUser |
|
| 112 | - */ |
|
| 113 | - private $userObject; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @var IGroup |
|
| 117 | - */ |
|
| 118 | - private $groupObject; |
|
| 119 | - |
|
| 120 | - /** @var Storage */ |
|
| 121 | - private $tempStorage; |
|
| 122 | - |
|
| 123 | - protected function setUp(): void { |
|
| 124 | - parent::setUp(); |
|
| 125 | - \OC_Hook::clear(); |
|
| 126 | - |
|
| 127 | - Server::get(IUserManager::class)->clearBackends(); |
|
| 128 | - Server::get(IUserManager::class)->registerBackend(new \Test\Util\User\Dummy()); |
|
| 129 | - |
|
| 130 | - //login |
|
| 131 | - $userManager = Server::get(IUserManager::class); |
|
| 132 | - $groupManager = Server::get(IGroupManager::class); |
|
| 133 | - $this->user = 'test'; |
|
| 134 | - $this->userObject = $userManager->createUser('test', 'test'); |
|
| 135 | - |
|
| 136 | - $this->groupObject = $groupManager->createGroup('group1'); |
|
| 137 | - $this->groupObject->addUser($this->userObject); |
|
| 138 | - |
|
| 139 | - self::loginAsUser($this->user); |
|
| 140 | - |
|
| 141 | - /** @var IMountManager $manager */ |
|
| 142 | - $manager = Server::get(IMountManager::class); |
|
| 143 | - $manager->removeMount('/test'); |
|
| 144 | - |
|
| 145 | - $this->tempStorage = null; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - protected function tearDown(): void { |
|
| 149 | - \OC_User::setUserId($this->user); |
|
| 150 | - foreach ($this->storages as $storage) { |
|
| 151 | - $cache = $storage->getCache(); |
|
| 152 | - $ids = $cache->getAll(); |
|
| 153 | - $cache->clear(); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($this->tempStorage) { |
|
| 157 | - system('rm -rf ' . escapeshellarg($this->tempStorage->getDataDir())); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - self::logout(); |
|
| 161 | - |
|
| 162 | - /** @var SetupManager $setupManager */ |
|
| 163 | - $setupManager = Server::get(SetupManager::class); |
|
| 164 | - $setupManager->setupRoot(); |
|
| 165 | - |
|
| 166 | - $this->userObject->delete(); |
|
| 167 | - $this->groupObject->delete(); |
|
| 168 | - |
|
| 169 | - $mountProviderCollection = Server::get(IMountProviderCollection::class); |
|
| 170 | - self::invokePrivate($mountProviderCollection, 'providers', [[]]); |
|
| 171 | - |
|
| 172 | - parent::tearDown(); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @medium |
|
| 177 | - */ |
|
| 178 | - public function testCacheAPI(): void { |
|
| 179 | - $storage1 = $this->getTestStorage(); |
|
| 180 | - $storage2 = $this->getTestStorage(); |
|
| 181 | - $storage3 = $this->getTestStorage(); |
|
| 182 | - $root = self::getUniqueID('/'); |
|
| 183 | - Filesystem::mount($storage1, [], $root . '/'); |
|
| 184 | - Filesystem::mount($storage2, [], $root . '/substorage'); |
|
| 185 | - Filesystem::mount($storage3, [], $root . '/folder/anotherstorage'); |
|
| 186 | - $textSize = strlen("dummy file data\n"); |
|
| 187 | - $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png'); |
|
| 188 | - $storageSize = $textSize * 2 + $imageSize; |
|
| 189 | - |
|
| 190 | - $storageInfo = $storage3->getCache()->get(''); |
|
| 191 | - $this->assertEquals($storageSize, $storageInfo['size']); |
|
| 192 | - |
|
| 193 | - $rootView = new View($root); |
|
| 194 | - |
|
| 195 | - $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 196 | - $this->assertEquals($textSize, $cachedData['size']); |
|
| 197 | - $this->assertEquals('text/plain', $cachedData['mimetype']); |
|
| 198 | - $this->assertNotEquals(-1, $cachedData['permissions']); |
|
| 199 | - |
|
| 200 | - $cachedData = $rootView->getFileInfo('/'); |
|
| 201 | - $this->assertEquals($storageSize * 3, $cachedData['size']); |
|
| 202 | - $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 203 | - |
|
| 204 | - // get cached data excluding mount points |
|
| 205 | - $cachedData = $rootView->getFileInfo('/', false); |
|
| 206 | - $this->assertEquals($storageSize, $cachedData['size']); |
|
| 207 | - $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 208 | - |
|
| 209 | - $cachedData = $rootView->getFileInfo('/folder'); |
|
| 210 | - $this->assertEquals($storageSize + $textSize, $cachedData['size']); |
|
| 211 | - $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 212 | - |
|
| 213 | - $folderData = $rootView->getDirectoryContent('/'); |
|
| 214 | - /** |
|
| 215 | - * expected entries: |
|
| 216 | - * folder |
|
| 217 | - * foo.png |
|
| 218 | - * foo.txt |
|
| 219 | - * substorage |
|
| 220 | - */ |
|
| 221 | - $this->assertCount(4, $folderData); |
|
| 222 | - $this->assertEquals('folder', $folderData[0]['name']); |
|
| 223 | - $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 224 | - $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 225 | - $this->assertEquals('substorage', $folderData[3]['name']); |
|
| 226 | - |
|
| 227 | - $this->assertEquals($storageSize + $textSize, $folderData[0]['size']); |
|
| 228 | - $this->assertEquals($imageSize, $folderData[1]['size']); |
|
| 229 | - $this->assertEquals($textSize, $folderData[2]['size']); |
|
| 230 | - $this->assertEquals($storageSize, $folderData[3]['size']); |
|
| 231 | - |
|
| 232 | - $folderData = $rootView->getDirectoryContent('/substorage'); |
|
| 233 | - /** |
|
| 234 | - * expected entries: |
|
| 235 | - * folder |
|
| 236 | - * foo.png |
|
| 237 | - * foo.txt |
|
| 238 | - */ |
|
| 239 | - $this->assertCount(3, $folderData); |
|
| 240 | - $this->assertEquals('folder', $folderData[0]['name']); |
|
| 241 | - $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 242 | - $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 243 | - |
|
| 244 | - $folderView = new View($root . '/folder'); |
|
| 245 | - $this->assertEquals($rootView->getFileInfo('/folder'), $folderView->getFileInfo('/')); |
|
| 246 | - |
|
| 247 | - $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 248 | - $this->assertFalse($cachedData['encrypted']); |
|
| 249 | - $id = $rootView->putFileInfo('/foo.txt', ['encrypted' => true]); |
|
| 250 | - $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 251 | - $this->assertTrue($cachedData['encrypted']); |
|
| 252 | - $this->assertEquals($cachedData['fileid'], $id); |
|
| 253 | - |
|
| 254 | - $this->assertFalse($rootView->getFileInfo('/non/existing')); |
|
| 255 | - $this->assertEquals([], $rootView->getDirectoryContent('/non/existing')); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @medium |
|
| 260 | - */ |
|
| 261 | - public function testGetPath(): void { |
|
| 262 | - $user = $this->createMock(IUser::class); |
|
| 263 | - $user->method('getUID') |
|
| 264 | - ->willReturn('test'); |
|
| 265 | - $storage1 = $this->getTestStorage(); |
|
| 266 | - $storage2 = $this->getTestStorage(); |
|
| 267 | - $storage3 = $this->getTestStorage(); |
|
| 268 | - |
|
| 269 | - Filesystem::mount($storage1, [], '/test/files'); |
|
| 270 | - Filesystem::mount($storage2, [], '/test/files/substorage'); |
|
| 271 | - Filesystem::mount($storage3, [], '/test/files/folder/anotherstorage'); |
|
| 272 | - |
|
| 273 | - $userMountCache = Server::get(IUserMountCache::class); |
|
| 274 | - $userMountCache->registerMounts($user, [ |
|
| 275 | - new MountPoint($storage1, '/test/files'), |
|
| 276 | - new MountPoint($storage2, '/test/files/substorage'), |
|
| 277 | - new MountPoint($storage3, '/test/files/folder/anotherstorage'), |
|
| 278 | - ]); |
|
| 279 | - |
|
| 280 | - $rootView = new View('/test/files'); |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 284 | - $id1 = $cachedData->getId(); |
|
| 285 | - $this->assertEquals('/foo.txt', $rootView->getPath($id1)); |
|
| 286 | - |
|
| 287 | - $cachedData = $rootView->getFileInfo('/substorage/foo.txt'); |
|
| 288 | - $id2 = $cachedData->getId(); |
|
| 289 | - $this->assertEquals('/substorage/foo.txt', $rootView->getPath($id2)); |
|
| 290 | - |
|
| 291 | - $folderView = new View('/test/files/substorage'); |
|
| 292 | - $this->assertEquals('/foo.txt', $folderView->getPath($id2)); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - public function testGetPathNotExisting(): void { |
|
| 297 | - $this->expectException(NotFoundException::class); |
|
| 298 | - |
|
| 299 | - $storage1 = $this->getTestStorage(); |
|
| 300 | - Filesystem::mount($storage1, [], '/'); |
|
| 301 | - |
|
| 302 | - $rootView = new View(''); |
|
| 303 | - $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 304 | - /** @var int $id1 */ |
|
| 305 | - $id1 = $cachedData['fileid']; |
|
| 306 | - $folderView = new View('/substorage'); |
|
| 307 | - $this->assertNull($folderView->getPath($id1)); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @medium |
|
| 312 | - */ |
|
| 313 | - public function testMountPointOverwrite(): void { |
|
| 314 | - $storage1 = $this->getTestStorage(false); |
|
| 315 | - $storage2 = $this->getTestStorage(); |
|
| 316 | - $storage1->mkdir('substorage'); |
|
| 317 | - Filesystem::mount($storage1, [], '/'); |
|
| 318 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 319 | - |
|
| 320 | - $rootView = new View(''); |
|
| 321 | - $folderContent = $rootView->getDirectoryContent('/'); |
|
| 322 | - $this->assertCount(4, $folderContent); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - public static function sharingDisabledPermissionProvider(): array { |
|
| 326 | - return [ |
|
| 327 | - ['no', '', true], |
|
| 328 | - ['yes', 'group1', false], |
|
| 329 | - ]; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - #[\PHPUnit\Framework\Attributes\DataProvider('sharingDisabledPermissionProvider')] |
|
| 333 | - public function testRemoveSharePermissionWhenSharingDisabledForUser($excludeGroups, $excludeGroupsList, $expectedShareable): void { |
|
| 334 | - // Reset sharing disabled for users cache |
|
| 335 | - self::invokePrivate(Server::get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); |
|
| 336 | - |
|
| 337 | - $config = Server::get(IConfig::class); |
|
| 338 | - $oldExcludeGroupsFlag = $config->getAppValue('core', 'shareapi_exclude_groups', 'no'); |
|
| 339 | - $oldExcludeGroupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 340 | - $config->setAppValue('core', 'shareapi_exclude_groups', $excludeGroups); |
|
| 341 | - $config->setAppValue('core', 'shareapi_exclude_groups_list', $excludeGroupsList); |
|
| 342 | - |
|
| 343 | - $storage1 = $this->getTestStorage(); |
|
| 344 | - $storage2 = $this->getTestStorage(); |
|
| 345 | - Filesystem::mount($storage1, [], '/'); |
|
| 346 | - Filesystem::mount($storage2, [], '/mount'); |
|
| 347 | - |
|
| 348 | - $view = new View('/'); |
|
| 349 | - |
|
| 350 | - $folderContent = $view->getDirectoryContent(''); |
|
| 351 | - $this->assertEquals($expectedShareable, $folderContent[0]->isShareable()); |
|
| 352 | - |
|
| 353 | - $folderContent = $view->getDirectoryContent('mount'); |
|
| 354 | - $this->assertEquals($expectedShareable, $folderContent[0]->isShareable()); |
|
| 355 | - |
|
| 356 | - $config->setAppValue('core', 'shareapi_exclude_groups', $oldExcludeGroupsFlag); |
|
| 357 | - $config->setAppValue('core', 'shareapi_exclude_groups_list', $oldExcludeGroupsList); |
|
| 358 | - |
|
| 359 | - // Reset sharing disabled for users cache |
|
| 360 | - self::invokePrivate(Server::get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - public function testCacheIncompleteFolder(): void { |
|
| 364 | - $storage1 = $this->getTestStorage(false); |
|
| 365 | - Filesystem::mount($storage1, [], '/incomplete'); |
|
| 366 | - $rootView = new View('/incomplete'); |
|
| 367 | - |
|
| 368 | - $entries = $rootView->getDirectoryContent('/'); |
|
| 369 | - $this->assertCount(3, $entries); |
|
| 370 | - |
|
| 371 | - // /folder will already be in the cache but not scanned |
|
| 372 | - $entries = $rootView->getDirectoryContent('/folder'); |
|
| 373 | - $this->assertCount(1, $entries); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - public function testAutoScan(): void { |
|
| 377 | - $storage1 = $this->getTestStorage(false); |
|
| 378 | - $storage2 = $this->getTestStorage(false); |
|
| 379 | - Filesystem::mount($storage1, [], '/'); |
|
| 380 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 381 | - $textSize = strlen("dummy file data\n"); |
|
| 382 | - |
|
| 383 | - $rootView = new View(''); |
|
| 384 | - |
|
| 385 | - $cachedData = $rootView->getFileInfo('/'); |
|
| 386 | - $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 387 | - $this->assertEquals(-1, $cachedData['size']); |
|
| 388 | - |
|
| 389 | - $folderData = $rootView->getDirectoryContent('/substorage/folder'); |
|
| 390 | - $this->assertEquals('text/plain', $folderData[0]['mimetype']); |
|
| 391 | - $this->assertEquals($textSize, $folderData[0]['size']); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * @medium |
|
| 396 | - */ |
|
| 397 | - public function testSearch(): void { |
|
| 398 | - $storage1 = $this->getTestStorage(); |
|
| 399 | - $storage2 = $this->getTestStorage(); |
|
| 400 | - $storage3 = $this->getTestStorage(); |
|
| 401 | - Filesystem::mount($storage1, [], '/'); |
|
| 402 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 403 | - Filesystem::mount($storage3, [], '/folder/anotherstorage'); |
|
| 404 | - |
|
| 405 | - $rootView = new View(''); |
|
| 406 | - |
|
| 407 | - $results = $rootView->search('foo'); |
|
| 408 | - $this->assertCount(6, $results); |
|
| 409 | - $paths = []; |
|
| 410 | - foreach ($results as $result) { |
|
| 411 | - $this->assertEquals($result['path'], Filesystem::normalizePath($result['path'])); |
|
| 412 | - $paths[] = $result['path']; |
|
| 413 | - } |
|
| 414 | - $this->assertContains('/foo.txt', $paths); |
|
| 415 | - $this->assertContains('/foo.png', $paths); |
|
| 416 | - $this->assertContains('/substorage/foo.txt', $paths); |
|
| 417 | - $this->assertContains('/substorage/foo.png', $paths); |
|
| 418 | - $this->assertContains('/folder/anotherstorage/foo.txt', $paths); |
|
| 419 | - $this->assertContains('/folder/anotherstorage/foo.png', $paths); |
|
| 420 | - |
|
| 421 | - $folderView = new View('/folder'); |
|
| 422 | - $results = $folderView->search('bar'); |
|
| 423 | - $this->assertCount(2, $results); |
|
| 424 | - $paths = []; |
|
| 425 | - foreach ($results as $result) { |
|
| 426 | - $paths[] = $result['path']; |
|
| 427 | - } |
|
| 428 | - $this->assertContains('/anotherstorage/folder/bar.txt', $paths); |
|
| 429 | - $this->assertContains('/bar.txt', $paths); |
|
| 430 | - |
|
| 431 | - $results = $folderView->search('foo'); |
|
| 432 | - $this->assertCount(2, $results); |
|
| 433 | - $paths = []; |
|
| 434 | - foreach ($results as $result) { |
|
| 435 | - $paths[] = $result['path']; |
|
| 436 | - } |
|
| 437 | - $this->assertContains('/anotherstorage/foo.txt', $paths); |
|
| 438 | - $this->assertContains('/anotherstorage/foo.png', $paths); |
|
| 439 | - |
|
| 440 | - $this->assertCount(6, $rootView->searchByMime('text')); |
|
| 441 | - $this->assertCount(3, $folderView->searchByMime('text')); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * @medium |
|
| 446 | - */ |
|
| 447 | - public function testWatcher(): void { |
|
| 448 | - $storage1 = $this->getTestStorage(); |
|
| 449 | - Filesystem::mount($storage1, [], '/'); |
|
| 450 | - $storage1->getWatcher()->setPolicy(Watcher::CHECK_ALWAYS); |
|
| 451 | - |
|
| 452 | - $rootView = new View(''); |
|
| 453 | - |
|
| 454 | - $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 455 | - $this->assertEquals(16, $cachedData['size']); |
|
| 456 | - |
|
| 457 | - $rootView->putFileInfo('foo.txt', ['storage_mtime' => 10]); |
|
| 458 | - $storage1->file_put_contents('foo.txt', 'foo'); |
|
| 459 | - clearstatcache(); |
|
| 460 | - |
|
| 461 | - $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 462 | - $this->assertEquals(3, $cachedData['size']); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * @medium |
|
| 467 | - */ |
|
| 468 | - public function testCopyBetweenStorageNoCross(): void { |
|
| 469 | - $storage1 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 470 | - $storage2 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 471 | - $this->copyBetweenStorages($storage1, $storage2); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * @medium |
|
| 476 | - */ |
|
| 477 | - public function testCopyBetweenStorageCross(): void { |
|
| 478 | - $storage1 = $this->getTestStorage(); |
|
| 479 | - $storage2 = $this->getTestStorage(); |
|
| 480 | - $this->copyBetweenStorages($storage1, $storage2); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * @medium |
|
| 485 | - */ |
|
| 486 | - public function testCopyBetweenStorageCrossNonLocal(): void { |
|
| 487 | - $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 488 | - $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 489 | - $this->copyBetweenStorages($storage1, $storage2); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - public function copyBetweenStorages($storage1, $storage2) { |
|
| 493 | - Filesystem::mount($storage1, [], '/'); |
|
| 494 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 495 | - |
|
| 496 | - $rootView = new View(''); |
|
| 497 | - $rootView->mkdir('substorage/emptyfolder'); |
|
| 498 | - $rootView->copy('substorage', 'anotherfolder'); |
|
| 499 | - $this->assertTrue($rootView->is_dir('/anotherfolder')); |
|
| 500 | - $this->assertTrue($rootView->is_dir('/substorage')); |
|
| 501 | - $this->assertTrue($rootView->is_dir('/anotherfolder/emptyfolder')); |
|
| 502 | - $this->assertTrue($rootView->is_dir('/substorage/emptyfolder')); |
|
| 503 | - $this->assertTrue($rootView->file_exists('/anotherfolder/foo.txt')); |
|
| 504 | - $this->assertTrue($rootView->file_exists('/anotherfolder/foo.png')); |
|
| 505 | - $this->assertTrue($rootView->file_exists('/anotherfolder/folder/bar.txt')); |
|
| 506 | - $this->assertTrue($rootView->file_exists('/substorage/foo.txt')); |
|
| 507 | - $this->assertTrue($rootView->file_exists('/substorage/foo.png')); |
|
| 508 | - $this->assertTrue($rootView->file_exists('/substorage/folder/bar.txt')); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * @medium |
|
| 513 | - */ |
|
| 514 | - public function testMoveBetweenStorageNoCross(): void { |
|
| 515 | - $storage1 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 516 | - $storage2 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 517 | - $this->moveBetweenStorages($storage1, $storage2); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * @medium |
|
| 522 | - */ |
|
| 523 | - public function testMoveBetweenStorageCross(): void { |
|
| 524 | - $storage1 = $this->getTestStorage(); |
|
| 525 | - $storage2 = $this->getTestStorage(); |
|
| 526 | - $this->moveBetweenStorages($storage1, $storage2); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * @medium |
|
| 531 | - */ |
|
| 532 | - public function testMoveBetweenStorageCrossNonLocal(): void { |
|
| 533 | - $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 534 | - $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 535 | - $this->moveBetweenStorages($storage1, $storage2); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - public function moveBetweenStorages($storage1, $storage2) { |
|
| 539 | - Filesystem::mount($storage1, [], '/' . $this->user . '/'); |
|
| 540 | - Filesystem::mount($storage2, [], '/' . $this->user . '/substorage'); |
|
| 541 | - |
|
| 542 | - $rootView = new View('/' . $this->user); |
|
| 543 | - $rootView->rename('foo.txt', 'substorage/folder/foo.txt'); |
|
| 544 | - $this->assertFalse($rootView->file_exists('foo.txt')); |
|
| 545 | - $this->assertTrue($rootView->file_exists('substorage/folder/foo.txt')); |
|
| 546 | - $rootView->rename('substorage/folder', 'anotherfolder'); |
|
| 547 | - $this->assertFalse($rootView->is_dir('substorage/folder')); |
|
| 548 | - $this->assertTrue($rootView->file_exists('anotherfolder/foo.txt')); |
|
| 549 | - $this->assertTrue($rootView->file_exists('anotherfolder/bar.txt')); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * @medium |
|
| 554 | - */ |
|
| 555 | - public function testUnlink(): void { |
|
| 556 | - $storage1 = $this->getTestStorage(); |
|
| 557 | - $storage2 = $this->getTestStorage(); |
|
| 558 | - Filesystem::mount($storage1, [], '/'); |
|
| 559 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 560 | - |
|
| 561 | - $rootView = new View(''); |
|
| 562 | - $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 563 | - $rootView->file_put_contents('/substorage/bar.txt', 'asd'); |
|
| 564 | - |
|
| 565 | - $this->assertTrue($rootView->file_exists('foo.txt')); |
|
| 566 | - $this->assertTrue($rootView->file_exists('substorage/bar.txt')); |
|
| 567 | - |
|
| 568 | - $this->assertTrue($rootView->unlink('foo.txt')); |
|
| 569 | - $this->assertTrue($rootView->unlink('substorage/bar.txt')); |
|
| 570 | - |
|
| 571 | - $this->assertFalse($rootView->file_exists('foo.txt')); |
|
| 572 | - $this->assertFalse($rootView->file_exists('substorage/bar.txt')); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - public static function rmdirOrUnlinkDataProvider(): array { |
|
| 576 | - return [['rmdir'], ['unlink']]; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - #[\PHPUnit\Framework\Attributes\DataProvider('rmdirOrUnlinkDataProvider')] |
|
| 580 | - public function testRmdir($method): void { |
|
| 581 | - $storage1 = $this->getTestStorage(); |
|
| 582 | - Filesystem::mount($storage1, [], '/'); |
|
| 583 | - |
|
| 584 | - $rootView = new View(''); |
|
| 585 | - $rootView->mkdir('sub'); |
|
| 586 | - $rootView->mkdir('sub/deep'); |
|
| 587 | - $rootView->file_put_contents('/sub/deep/foo.txt', 'asd'); |
|
| 588 | - |
|
| 589 | - $this->assertTrue($rootView->file_exists('sub/deep/foo.txt')); |
|
| 590 | - |
|
| 591 | - $this->assertTrue($rootView->$method('sub')); |
|
| 592 | - |
|
| 593 | - $this->assertFalse($rootView->file_exists('sub')); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * @medium |
|
| 598 | - */ |
|
| 599 | - public function testUnlinkRootMustFail(): void { |
|
| 600 | - $storage1 = $this->getTestStorage(); |
|
| 601 | - $storage2 = $this->getTestStorage(); |
|
| 602 | - Filesystem::mount($storage1, [], '/'); |
|
| 603 | - Filesystem::mount($storage2, [], '/substorage'); |
|
| 604 | - |
|
| 605 | - $rootView = new View(''); |
|
| 606 | - $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 607 | - $rootView->file_put_contents('/substorage/bar.txt', 'asd'); |
|
| 608 | - |
|
| 609 | - $this->assertFalse($rootView->unlink('')); |
|
| 610 | - $this->assertFalse($rootView->unlink('/')); |
|
| 611 | - $this->assertFalse($rootView->unlink('substorage')); |
|
| 612 | - $this->assertFalse($rootView->unlink('/substorage')); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * @medium |
|
| 617 | - */ |
|
| 618 | - public function testTouch(): void { |
|
| 619 | - $storage = $this->getTestStorage(true, TemporaryNoTouch::class); |
|
| 620 | - |
|
| 621 | - Filesystem::mount($storage, [], '/'); |
|
| 622 | - |
|
| 623 | - $rootView = new View(''); |
|
| 624 | - $oldCachedData = $rootView->getFileInfo('foo.txt'); |
|
| 625 | - |
|
| 626 | - $rootView->touch('foo.txt', 500); |
|
| 627 | - |
|
| 628 | - $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 629 | - $this->assertEquals(500, $cachedData['mtime']); |
|
| 630 | - $this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']); |
|
| 631 | - |
|
| 632 | - $rootView->putFileInfo('foo.txt', ['storage_mtime' => 1000]); //make sure the watcher detects the change |
|
| 633 | - $rootView->file_put_contents('foo.txt', 'asd'); |
|
| 634 | - $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 635 | - $this->assertGreaterThanOrEqual($oldCachedData['mtime'], $cachedData['mtime']); |
|
| 636 | - $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * @medium |
|
| 641 | - */ |
|
| 642 | - public function testTouchFloat(): void { |
|
| 643 | - $storage = $this->getTestStorage(true, TemporaryNoTouch::class); |
|
| 644 | - |
|
| 645 | - Filesystem::mount($storage, [], '/'); |
|
| 646 | - |
|
| 647 | - $rootView = new View(''); |
|
| 648 | - $oldCachedData = $rootView->getFileInfo('foo.txt'); |
|
| 649 | - |
|
| 650 | - $rootView->touch('foo.txt', 500.5); |
|
| 651 | - |
|
| 652 | - $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 653 | - $this->assertEquals(500, $cachedData['mtime']); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * @medium |
|
| 658 | - */ |
|
| 659 | - public function testViewHooks(): void { |
|
| 660 | - $storage1 = $this->getTestStorage(); |
|
| 661 | - $storage2 = $this->getTestStorage(); |
|
| 662 | - $defaultRoot = Filesystem::getRoot(); |
|
| 663 | - Filesystem::mount($storage1, [], '/'); |
|
| 664 | - Filesystem::mount($storage2, [], $defaultRoot . '/substorage'); |
|
| 665 | - \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
| 666 | - |
|
| 667 | - $rootView = new View(''); |
|
| 668 | - $subView = new View($defaultRoot . '/substorage'); |
|
| 669 | - $this->hookPath = null; |
|
| 670 | - |
|
| 671 | - $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 672 | - $this->assertNull($this->hookPath); |
|
| 673 | - |
|
| 674 | - $subView->file_put_contents('/foo.txt', 'asd'); |
|
| 675 | - $this->assertEquals('/substorage/foo.txt', $this->hookPath); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - private $hookPath; |
|
| 679 | - |
|
| 680 | - public function dummyHook($params) { |
|
| 681 | - $this->hookPath = $params['path']; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - public function testSearchNotOutsideView(): void { |
|
| 685 | - $storage1 = $this->getTestStorage(); |
|
| 686 | - Filesystem::mount($storage1, [], '/'); |
|
| 687 | - $storage1->rename('folder', 'foo'); |
|
| 688 | - $scanner = $storage1->getScanner(); |
|
| 689 | - $scanner->scan(''); |
|
| 690 | - |
|
| 691 | - $view = new View('/foo'); |
|
| 692 | - |
|
| 693 | - $result = $view->search('.txt'); |
|
| 694 | - $this->assertCount(1, $result); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * @param bool $scan |
|
| 699 | - * @param string $class |
|
| 700 | - * @return Storage |
|
| 701 | - */ |
|
| 702 | - private function getTestStorage($scan = true, $class = Temporary::class) { |
|
| 703 | - /** |
|
| 704 | - * @var Storage $storage |
|
| 705 | - */ |
|
| 706 | - $storage = new $class([]); |
|
| 707 | - $textData = "dummy file data\n"; |
|
| 708 | - $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png'); |
|
| 709 | - $storage->mkdir('folder'); |
|
| 710 | - $storage->file_put_contents('foo.txt', $textData); |
|
| 711 | - $storage->file_put_contents('foo.png', $imgData); |
|
| 712 | - $storage->file_put_contents('folder/bar.txt', $textData); |
|
| 713 | - |
|
| 714 | - if ($scan) { |
|
| 715 | - $scanner = $storage->getScanner(); |
|
| 716 | - $scanner->scan(''); |
|
| 717 | - } |
|
| 718 | - $this->storages[] = $storage; |
|
| 719 | - return $storage; |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * @medium |
|
| 724 | - */ |
|
| 725 | - public function testViewHooksIfRootStartsTheSame(): void { |
|
| 726 | - $storage1 = $this->getTestStorage(); |
|
| 727 | - $storage2 = $this->getTestStorage(); |
|
| 728 | - $defaultRoot = Filesystem::getRoot(); |
|
| 729 | - Filesystem::mount($storage1, [], '/'); |
|
| 730 | - Filesystem::mount($storage2, [], $defaultRoot . '_substorage'); |
|
| 731 | - \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
| 732 | - |
|
| 733 | - $subView = new View($defaultRoot . '_substorage'); |
|
| 734 | - $this->hookPath = null; |
|
| 735 | - |
|
| 736 | - $subView->file_put_contents('/foo.txt', 'asd'); |
|
| 737 | - $this->assertNull($this->hookPath); |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - private $hookWritePath; |
|
| 741 | - private $hookCreatePath; |
|
| 742 | - private $hookUpdatePath; |
|
| 743 | - |
|
| 744 | - public function dummyHookWrite($params) { |
|
| 745 | - $this->hookWritePath = $params['path']; |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - public function dummyHookUpdate($params) { |
|
| 749 | - $this->hookUpdatePath = $params['path']; |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - public function dummyHookCreate($params) { |
|
| 753 | - $this->hookCreatePath = $params['path']; |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - public function testEditNoCreateHook(): void { |
|
| 757 | - $storage1 = $this->getTestStorage(); |
|
| 758 | - $storage2 = $this->getTestStorage(); |
|
| 759 | - $defaultRoot = Filesystem::getRoot(); |
|
| 760 | - Filesystem::mount($storage1, [], '/'); |
|
| 761 | - Filesystem::mount($storage2, [], $defaultRoot); |
|
| 762 | - \OC_Hook::connect('OC_Filesystem', 'post_create', $this, 'dummyHookCreate'); |
|
| 763 | - \OC_Hook::connect('OC_Filesystem', 'post_update', $this, 'dummyHookUpdate'); |
|
| 764 | - \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHookWrite'); |
|
| 765 | - |
|
| 766 | - $view = new View($defaultRoot); |
|
| 767 | - $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null; |
|
| 768 | - |
|
| 769 | - $view->file_put_contents('/asd.txt', 'foo'); |
|
| 770 | - $this->assertEquals('/asd.txt', $this->hookCreatePath); |
|
| 771 | - $this->assertNull($this->hookUpdatePath); |
|
| 772 | - $this->assertEquals('/asd.txt', $this->hookWritePath); |
|
| 773 | - |
|
| 774 | - $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null; |
|
| 775 | - |
|
| 776 | - $view->file_put_contents('/asd.txt', 'foo'); |
|
| 777 | - $this->assertNull($this->hookCreatePath); |
|
| 778 | - $this->assertEquals('/asd.txt', $this->hookUpdatePath); |
|
| 779 | - $this->assertEquals('/asd.txt', $this->hookWritePath); |
|
| 780 | - |
|
| 781 | - \OC_Hook::clear('OC_Filesystem', 'post_create'); |
|
| 782 | - \OC_Hook::clear('OC_Filesystem', 'post_update'); |
|
| 783 | - \OC_Hook::clear('OC_Filesystem', 'post_write'); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - #[\PHPUnit\Framework\Attributes\DataProvider('resolvePathTestProvider')] |
|
| 787 | - public function testResolvePath($expected, $pathToTest): void { |
|
| 788 | - $storage1 = $this->getTestStorage(); |
|
| 789 | - Filesystem::mount($storage1, [], '/'); |
|
| 790 | - |
|
| 791 | - $view = new View(''); |
|
| 792 | - |
|
| 793 | - $result = $view->resolvePath($pathToTest); |
|
| 794 | - $this->assertEquals($expected, $result[1]); |
|
| 795 | - |
|
| 796 | - $exists = $view->file_exists($pathToTest); |
|
| 797 | - $this->assertTrue($exists); |
|
| 798 | - |
|
| 799 | - $exists = $view->file_exists($result[1]); |
|
| 800 | - $this->assertTrue($exists); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - public static function resolvePathTestProvider(): array { |
|
| 804 | - return [ |
|
| 805 | - ['foo.txt', 'foo.txt'], |
|
| 806 | - ['foo.txt', '/foo.txt'], |
|
| 807 | - ['folder', 'folder'], |
|
| 808 | - ['folder', '/folder'], |
|
| 809 | - ['folder', 'folder/'], |
|
| 810 | - ['folder', '/folder/'], |
|
| 811 | - ['folder/bar.txt', 'folder/bar.txt'], |
|
| 812 | - ['folder/bar.txt', '/folder/bar.txt'], |
|
| 813 | - ['', ''], |
|
| 814 | - ['', '/'], |
|
| 815 | - ]; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - public function testUTF8Names(): void { |
|
| 819 | - $names = ['虚', '和知しゃ和で', 'regular ascii', 'sɨˈrɪlɪk', 'ѨѬ', 'أنا أحب القراءة كثيرا']; |
|
| 820 | - |
|
| 821 | - $storage = new Temporary([]); |
|
| 822 | - Filesystem::mount($storage, [], '/'); |
|
| 823 | - |
|
| 824 | - $rootView = new View(''); |
|
| 825 | - foreach ($names as $name) { |
|
| 826 | - $rootView->file_put_contents('/' . $name, 'dummy content'); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - $list = $rootView->getDirectoryContent('/'); |
|
| 830 | - |
|
| 831 | - $this->assertCount(count($names), $list); |
|
| 832 | - foreach ($list as $item) { |
|
| 833 | - $this->assertContains($item['name'], $names); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - $cache = $storage->getCache(); |
|
| 837 | - $scanner = $storage->getScanner(); |
|
| 838 | - $scanner->scan(''); |
|
| 839 | - |
|
| 840 | - $list = $cache->getFolderContents(''); |
|
| 841 | - |
|
| 842 | - $this->assertCount(count($names), $list); |
|
| 843 | - foreach ($list as $item) { |
|
| 844 | - $this->assertContains($item['name'], $names); |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - public function xtestLongPath() { |
|
| 849 | - $storage = new Temporary([]); |
|
| 850 | - Filesystem::mount($storage, [], '/'); |
|
| 851 | - |
|
| 852 | - $rootView = new View(''); |
|
| 853 | - |
|
| 854 | - $longPath = ''; |
|
| 855 | - $ds = DIRECTORY_SEPARATOR; |
|
| 856 | - /* |
|
| 98 | + use UserTrait; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @var Storage[] $storages |
|
| 102 | + */ |
|
| 103 | + private $storages = []; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @var string |
|
| 107 | + */ |
|
| 108 | + private $user; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @var IUser |
|
| 112 | + */ |
|
| 113 | + private $userObject; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @var IGroup |
|
| 117 | + */ |
|
| 118 | + private $groupObject; |
|
| 119 | + |
|
| 120 | + /** @var Storage */ |
|
| 121 | + private $tempStorage; |
|
| 122 | + |
|
| 123 | + protected function setUp(): void { |
|
| 124 | + parent::setUp(); |
|
| 125 | + \OC_Hook::clear(); |
|
| 126 | + |
|
| 127 | + Server::get(IUserManager::class)->clearBackends(); |
|
| 128 | + Server::get(IUserManager::class)->registerBackend(new \Test\Util\User\Dummy()); |
|
| 129 | + |
|
| 130 | + //login |
|
| 131 | + $userManager = Server::get(IUserManager::class); |
|
| 132 | + $groupManager = Server::get(IGroupManager::class); |
|
| 133 | + $this->user = 'test'; |
|
| 134 | + $this->userObject = $userManager->createUser('test', 'test'); |
|
| 135 | + |
|
| 136 | + $this->groupObject = $groupManager->createGroup('group1'); |
|
| 137 | + $this->groupObject->addUser($this->userObject); |
|
| 138 | + |
|
| 139 | + self::loginAsUser($this->user); |
|
| 140 | + |
|
| 141 | + /** @var IMountManager $manager */ |
|
| 142 | + $manager = Server::get(IMountManager::class); |
|
| 143 | + $manager->removeMount('/test'); |
|
| 144 | + |
|
| 145 | + $this->tempStorage = null; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + protected function tearDown(): void { |
|
| 149 | + \OC_User::setUserId($this->user); |
|
| 150 | + foreach ($this->storages as $storage) { |
|
| 151 | + $cache = $storage->getCache(); |
|
| 152 | + $ids = $cache->getAll(); |
|
| 153 | + $cache->clear(); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ($this->tempStorage) { |
|
| 157 | + system('rm -rf ' . escapeshellarg($this->tempStorage->getDataDir())); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + self::logout(); |
|
| 161 | + |
|
| 162 | + /** @var SetupManager $setupManager */ |
|
| 163 | + $setupManager = Server::get(SetupManager::class); |
|
| 164 | + $setupManager->setupRoot(); |
|
| 165 | + |
|
| 166 | + $this->userObject->delete(); |
|
| 167 | + $this->groupObject->delete(); |
|
| 168 | + |
|
| 169 | + $mountProviderCollection = Server::get(IMountProviderCollection::class); |
|
| 170 | + self::invokePrivate($mountProviderCollection, 'providers', [[]]); |
|
| 171 | + |
|
| 172 | + parent::tearDown(); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @medium |
|
| 177 | + */ |
|
| 178 | + public function testCacheAPI(): void { |
|
| 179 | + $storage1 = $this->getTestStorage(); |
|
| 180 | + $storage2 = $this->getTestStorage(); |
|
| 181 | + $storage3 = $this->getTestStorage(); |
|
| 182 | + $root = self::getUniqueID('/'); |
|
| 183 | + Filesystem::mount($storage1, [], $root . '/'); |
|
| 184 | + Filesystem::mount($storage2, [], $root . '/substorage'); |
|
| 185 | + Filesystem::mount($storage3, [], $root . '/folder/anotherstorage'); |
|
| 186 | + $textSize = strlen("dummy file data\n"); |
|
| 187 | + $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png'); |
|
| 188 | + $storageSize = $textSize * 2 + $imageSize; |
|
| 189 | + |
|
| 190 | + $storageInfo = $storage3->getCache()->get(''); |
|
| 191 | + $this->assertEquals($storageSize, $storageInfo['size']); |
|
| 192 | + |
|
| 193 | + $rootView = new View($root); |
|
| 194 | + |
|
| 195 | + $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 196 | + $this->assertEquals($textSize, $cachedData['size']); |
|
| 197 | + $this->assertEquals('text/plain', $cachedData['mimetype']); |
|
| 198 | + $this->assertNotEquals(-1, $cachedData['permissions']); |
|
| 199 | + |
|
| 200 | + $cachedData = $rootView->getFileInfo('/'); |
|
| 201 | + $this->assertEquals($storageSize * 3, $cachedData['size']); |
|
| 202 | + $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 203 | + |
|
| 204 | + // get cached data excluding mount points |
|
| 205 | + $cachedData = $rootView->getFileInfo('/', false); |
|
| 206 | + $this->assertEquals($storageSize, $cachedData['size']); |
|
| 207 | + $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 208 | + |
|
| 209 | + $cachedData = $rootView->getFileInfo('/folder'); |
|
| 210 | + $this->assertEquals($storageSize + $textSize, $cachedData['size']); |
|
| 211 | + $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 212 | + |
|
| 213 | + $folderData = $rootView->getDirectoryContent('/'); |
|
| 214 | + /** |
|
| 215 | + * expected entries: |
|
| 216 | + * folder |
|
| 217 | + * foo.png |
|
| 218 | + * foo.txt |
|
| 219 | + * substorage |
|
| 220 | + */ |
|
| 221 | + $this->assertCount(4, $folderData); |
|
| 222 | + $this->assertEquals('folder', $folderData[0]['name']); |
|
| 223 | + $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 224 | + $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 225 | + $this->assertEquals('substorage', $folderData[3]['name']); |
|
| 226 | + |
|
| 227 | + $this->assertEquals($storageSize + $textSize, $folderData[0]['size']); |
|
| 228 | + $this->assertEquals($imageSize, $folderData[1]['size']); |
|
| 229 | + $this->assertEquals($textSize, $folderData[2]['size']); |
|
| 230 | + $this->assertEquals($storageSize, $folderData[3]['size']); |
|
| 231 | + |
|
| 232 | + $folderData = $rootView->getDirectoryContent('/substorage'); |
|
| 233 | + /** |
|
| 234 | + * expected entries: |
|
| 235 | + * folder |
|
| 236 | + * foo.png |
|
| 237 | + * foo.txt |
|
| 238 | + */ |
|
| 239 | + $this->assertCount(3, $folderData); |
|
| 240 | + $this->assertEquals('folder', $folderData[0]['name']); |
|
| 241 | + $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 242 | + $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 243 | + |
|
| 244 | + $folderView = new View($root . '/folder'); |
|
| 245 | + $this->assertEquals($rootView->getFileInfo('/folder'), $folderView->getFileInfo('/')); |
|
| 246 | + |
|
| 247 | + $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 248 | + $this->assertFalse($cachedData['encrypted']); |
|
| 249 | + $id = $rootView->putFileInfo('/foo.txt', ['encrypted' => true]); |
|
| 250 | + $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 251 | + $this->assertTrue($cachedData['encrypted']); |
|
| 252 | + $this->assertEquals($cachedData['fileid'], $id); |
|
| 253 | + |
|
| 254 | + $this->assertFalse($rootView->getFileInfo('/non/existing')); |
|
| 255 | + $this->assertEquals([], $rootView->getDirectoryContent('/non/existing')); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @medium |
|
| 260 | + */ |
|
| 261 | + public function testGetPath(): void { |
|
| 262 | + $user = $this->createMock(IUser::class); |
|
| 263 | + $user->method('getUID') |
|
| 264 | + ->willReturn('test'); |
|
| 265 | + $storage1 = $this->getTestStorage(); |
|
| 266 | + $storage2 = $this->getTestStorage(); |
|
| 267 | + $storage3 = $this->getTestStorage(); |
|
| 268 | + |
|
| 269 | + Filesystem::mount($storage1, [], '/test/files'); |
|
| 270 | + Filesystem::mount($storage2, [], '/test/files/substorage'); |
|
| 271 | + Filesystem::mount($storage3, [], '/test/files/folder/anotherstorage'); |
|
| 272 | + |
|
| 273 | + $userMountCache = Server::get(IUserMountCache::class); |
|
| 274 | + $userMountCache->registerMounts($user, [ |
|
| 275 | + new MountPoint($storage1, '/test/files'), |
|
| 276 | + new MountPoint($storage2, '/test/files/substorage'), |
|
| 277 | + new MountPoint($storage3, '/test/files/folder/anotherstorage'), |
|
| 278 | + ]); |
|
| 279 | + |
|
| 280 | + $rootView = new View('/test/files'); |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 284 | + $id1 = $cachedData->getId(); |
|
| 285 | + $this->assertEquals('/foo.txt', $rootView->getPath($id1)); |
|
| 286 | + |
|
| 287 | + $cachedData = $rootView->getFileInfo('/substorage/foo.txt'); |
|
| 288 | + $id2 = $cachedData->getId(); |
|
| 289 | + $this->assertEquals('/substorage/foo.txt', $rootView->getPath($id2)); |
|
| 290 | + |
|
| 291 | + $folderView = new View('/test/files/substorage'); |
|
| 292 | + $this->assertEquals('/foo.txt', $folderView->getPath($id2)); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + public function testGetPathNotExisting(): void { |
|
| 297 | + $this->expectException(NotFoundException::class); |
|
| 298 | + |
|
| 299 | + $storage1 = $this->getTestStorage(); |
|
| 300 | + Filesystem::mount($storage1, [], '/'); |
|
| 301 | + |
|
| 302 | + $rootView = new View(''); |
|
| 303 | + $cachedData = $rootView->getFileInfo('/foo.txt'); |
|
| 304 | + /** @var int $id1 */ |
|
| 305 | + $id1 = $cachedData['fileid']; |
|
| 306 | + $folderView = new View('/substorage'); |
|
| 307 | + $this->assertNull($folderView->getPath($id1)); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @medium |
|
| 312 | + */ |
|
| 313 | + public function testMountPointOverwrite(): void { |
|
| 314 | + $storage1 = $this->getTestStorage(false); |
|
| 315 | + $storage2 = $this->getTestStorage(); |
|
| 316 | + $storage1->mkdir('substorage'); |
|
| 317 | + Filesystem::mount($storage1, [], '/'); |
|
| 318 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 319 | + |
|
| 320 | + $rootView = new View(''); |
|
| 321 | + $folderContent = $rootView->getDirectoryContent('/'); |
|
| 322 | + $this->assertCount(4, $folderContent); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + public static function sharingDisabledPermissionProvider(): array { |
|
| 326 | + return [ |
|
| 327 | + ['no', '', true], |
|
| 328 | + ['yes', 'group1', false], |
|
| 329 | + ]; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + #[\PHPUnit\Framework\Attributes\DataProvider('sharingDisabledPermissionProvider')] |
|
| 333 | + public function testRemoveSharePermissionWhenSharingDisabledForUser($excludeGroups, $excludeGroupsList, $expectedShareable): void { |
|
| 334 | + // Reset sharing disabled for users cache |
|
| 335 | + self::invokePrivate(Server::get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); |
|
| 336 | + |
|
| 337 | + $config = Server::get(IConfig::class); |
|
| 338 | + $oldExcludeGroupsFlag = $config->getAppValue('core', 'shareapi_exclude_groups', 'no'); |
|
| 339 | + $oldExcludeGroupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 340 | + $config->setAppValue('core', 'shareapi_exclude_groups', $excludeGroups); |
|
| 341 | + $config->setAppValue('core', 'shareapi_exclude_groups_list', $excludeGroupsList); |
|
| 342 | + |
|
| 343 | + $storage1 = $this->getTestStorage(); |
|
| 344 | + $storage2 = $this->getTestStorage(); |
|
| 345 | + Filesystem::mount($storage1, [], '/'); |
|
| 346 | + Filesystem::mount($storage2, [], '/mount'); |
|
| 347 | + |
|
| 348 | + $view = new View('/'); |
|
| 349 | + |
|
| 350 | + $folderContent = $view->getDirectoryContent(''); |
|
| 351 | + $this->assertEquals($expectedShareable, $folderContent[0]->isShareable()); |
|
| 352 | + |
|
| 353 | + $folderContent = $view->getDirectoryContent('mount'); |
|
| 354 | + $this->assertEquals($expectedShareable, $folderContent[0]->isShareable()); |
|
| 355 | + |
|
| 356 | + $config->setAppValue('core', 'shareapi_exclude_groups', $oldExcludeGroupsFlag); |
|
| 357 | + $config->setAppValue('core', 'shareapi_exclude_groups_list', $oldExcludeGroupsList); |
|
| 358 | + |
|
| 359 | + // Reset sharing disabled for users cache |
|
| 360 | + self::invokePrivate(Server::get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + public function testCacheIncompleteFolder(): void { |
|
| 364 | + $storage1 = $this->getTestStorage(false); |
|
| 365 | + Filesystem::mount($storage1, [], '/incomplete'); |
|
| 366 | + $rootView = new View('/incomplete'); |
|
| 367 | + |
|
| 368 | + $entries = $rootView->getDirectoryContent('/'); |
|
| 369 | + $this->assertCount(3, $entries); |
|
| 370 | + |
|
| 371 | + // /folder will already be in the cache but not scanned |
|
| 372 | + $entries = $rootView->getDirectoryContent('/folder'); |
|
| 373 | + $this->assertCount(1, $entries); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + public function testAutoScan(): void { |
|
| 377 | + $storage1 = $this->getTestStorage(false); |
|
| 378 | + $storage2 = $this->getTestStorage(false); |
|
| 379 | + Filesystem::mount($storage1, [], '/'); |
|
| 380 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 381 | + $textSize = strlen("dummy file data\n"); |
|
| 382 | + |
|
| 383 | + $rootView = new View(''); |
|
| 384 | + |
|
| 385 | + $cachedData = $rootView->getFileInfo('/'); |
|
| 386 | + $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']); |
|
| 387 | + $this->assertEquals(-1, $cachedData['size']); |
|
| 388 | + |
|
| 389 | + $folderData = $rootView->getDirectoryContent('/substorage/folder'); |
|
| 390 | + $this->assertEquals('text/plain', $folderData[0]['mimetype']); |
|
| 391 | + $this->assertEquals($textSize, $folderData[0]['size']); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * @medium |
|
| 396 | + */ |
|
| 397 | + public function testSearch(): void { |
|
| 398 | + $storage1 = $this->getTestStorage(); |
|
| 399 | + $storage2 = $this->getTestStorage(); |
|
| 400 | + $storage3 = $this->getTestStorage(); |
|
| 401 | + Filesystem::mount($storage1, [], '/'); |
|
| 402 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 403 | + Filesystem::mount($storage3, [], '/folder/anotherstorage'); |
|
| 404 | + |
|
| 405 | + $rootView = new View(''); |
|
| 406 | + |
|
| 407 | + $results = $rootView->search('foo'); |
|
| 408 | + $this->assertCount(6, $results); |
|
| 409 | + $paths = []; |
|
| 410 | + foreach ($results as $result) { |
|
| 411 | + $this->assertEquals($result['path'], Filesystem::normalizePath($result['path'])); |
|
| 412 | + $paths[] = $result['path']; |
|
| 413 | + } |
|
| 414 | + $this->assertContains('/foo.txt', $paths); |
|
| 415 | + $this->assertContains('/foo.png', $paths); |
|
| 416 | + $this->assertContains('/substorage/foo.txt', $paths); |
|
| 417 | + $this->assertContains('/substorage/foo.png', $paths); |
|
| 418 | + $this->assertContains('/folder/anotherstorage/foo.txt', $paths); |
|
| 419 | + $this->assertContains('/folder/anotherstorage/foo.png', $paths); |
|
| 420 | + |
|
| 421 | + $folderView = new View('/folder'); |
|
| 422 | + $results = $folderView->search('bar'); |
|
| 423 | + $this->assertCount(2, $results); |
|
| 424 | + $paths = []; |
|
| 425 | + foreach ($results as $result) { |
|
| 426 | + $paths[] = $result['path']; |
|
| 427 | + } |
|
| 428 | + $this->assertContains('/anotherstorage/folder/bar.txt', $paths); |
|
| 429 | + $this->assertContains('/bar.txt', $paths); |
|
| 430 | + |
|
| 431 | + $results = $folderView->search('foo'); |
|
| 432 | + $this->assertCount(2, $results); |
|
| 433 | + $paths = []; |
|
| 434 | + foreach ($results as $result) { |
|
| 435 | + $paths[] = $result['path']; |
|
| 436 | + } |
|
| 437 | + $this->assertContains('/anotherstorage/foo.txt', $paths); |
|
| 438 | + $this->assertContains('/anotherstorage/foo.png', $paths); |
|
| 439 | + |
|
| 440 | + $this->assertCount(6, $rootView->searchByMime('text')); |
|
| 441 | + $this->assertCount(3, $folderView->searchByMime('text')); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * @medium |
|
| 446 | + */ |
|
| 447 | + public function testWatcher(): void { |
|
| 448 | + $storage1 = $this->getTestStorage(); |
|
| 449 | + Filesystem::mount($storage1, [], '/'); |
|
| 450 | + $storage1->getWatcher()->setPolicy(Watcher::CHECK_ALWAYS); |
|
| 451 | + |
|
| 452 | + $rootView = new View(''); |
|
| 453 | + |
|
| 454 | + $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 455 | + $this->assertEquals(16, $cachedData['size']); |
|
| 456 | + |
|
| 457 | + $rootView->putFileInfo('foo.txt', ['storage_mtime' => 10]); |
|
| 458 | + $storage1->file_put_contents('foo.txt', 'foo'); |
|
| 459 | + clearstatcache(); |
|
| 460 | + |
|
| 461 | + $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 462 | + $this->assertEquals(3, $cachedData['size']); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * @medium |
|
| 467 | + */ |
|
| 468 | + public function testCopyBetweenStorageNoCross(): void { |
|
| 469 | + $storage1 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 470 | + $storage2 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 471 | + $this->copyBetweenStorages($storage1, $storage2); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * @medium |
|
| 476 | + */ |
|
| 477 | + public function testCopyBetweenStorageCross(): void { |
|
| 478 | + $storage1 = $this->getTestStorage(); |
|
| 479 | + $storage2 = $this->getTestStorage(); |
|
| 480 | + $this->copyBetweenStorages($storage1, $storage2); |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * @medium |
|
| 485 | + */ |
|
| 486 | + public function testCopyBetweenStorageCrossNonLocal(): void { |
|
| 487 | + $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 488 | + $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 489 | + $this->copyBetweenStorages($storage1, $storage2); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + public function copyBetweenStorages($storage1, $storage2) { |
|
| 493 | + Filesystem::mount($storage1, [], '/'); |
|
| 494 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 495 | + |
|
| 496 | + $rootView = new View(''); |
|
| 497 | + $rootView->mkdir('substorage/emptyfolder'); |
|
| 498 | + $rootView->copy('substorage', 'anotherfolder'); |
|
| 499 | + $this->assertTrue($rootView->is_dir('/anotherfolder')); |
|
| 500 | + $this->assertTrue($rootView->is_dir('/substorage')); |
|
| 501 | + $this->assertTrue($rootView->is_dir('/anotherfolder/emptyfolder')); |
|
| 502 | + $this->assertTrue($rootView->is_dir('/substorage/emptyfolder')); |
|
| 503 | + $this->assertTrue($rootView->file_exists('/anotherfolder/foo.txt')); |
|
| 504 | + $this->assertTrue($rootView->file_exists('/anotherfolder/foo.png')); |
|
| 505 | + $this->assertTrue($rootView->file_exists('/anotherfolder/folder/bar.txt')); |
|
| 506 | + $this->assertTrue($rootView->file_exists('/substorage/foo.txt')); |
|
| 507 | + $this->assertTrue($rootView->file_exists('/substorage/foo.png')); |
|
| 508 | + $this->assertTrue($rootView->file_exists('/substorage/folder/bar.txt')); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * @medium |
|
| 513 | + */ |
|
| 514 | + public function testMoveBetweenStorageNoCross(): void { |
|
| 515 | + $storage1 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 516 | + $storage2 = $this->getTestStorage(true, TemporaryNoCross::class); |
|
| 517 | + $this->moveBetweenStorages($storage1, $storage2); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * @medium |
|
| 522 | + */ |
|
| 523 | + public function testMoveBetweenStorageCross(): void { |
|
| 524 | + $storage1 = $this->getTestStorage(); |
|
| 525 | + $storage2 = $this->getTestStorage(); |
|
| 526 | + $this->moveBetweenStorages($storage1, $storage2); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * @medium |
|
| 531 | + */ |
|
| 532 | + public function testMoveBetweenStorageCrossNonLocal(): void { |
|
| 533 | + $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 534 | + $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class); |
|
| 535 | + $this->moveBetweenStorages($storage1, $storage2); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + public function moveBetweenStorages($storage1, $storage2) { |
|
| 539 | + Filesystem::mount($storage1, [], '/' . $this->user . '/'); |
|
| 540 | + Filesystem::mount($storage2, [], '/' . $this->user . '/substorage'); |
|
| 541 | + |
|
| 542 | + $rootView = new View('/' . $this->user); |
|
| 543 | + $rootView->rename('foo.txt', 'substorage/folder/foo.txt'); |
|
| 544 | + $this->assertFalse($rootView->file_exists('foo.txt')); |
|
| 545 | + $this->assertTrue($rootView->file_exists('substorage/folder/foo.txt')); |
|
| 546 | + $rootView->rename('substorage/folder', 'anotherfolder'); |
|
| 547 | + $this->assertFalse($rootView->is_dir('substorage/folder')); |
|
| 548 | + $this->assertTrue($rootView->file_exists('anotherfolder/foo.txt')); |
|
| 549 | + $this->assertTrue($rootView->file_exists('anotherfolder/bar.txt')); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * @medium |
|
| 554 | + */ |
|
| 555 | + public function testUnlink(): void { |
|
| 556 | + $storage1 = $this->getTestStorage(); |
|
| 557 | + $storage2 = $this->getTestStorage(); |
|
| 558 | + Filesystem::mount($storage1, [], '/'); |
|
| 559 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 560 | + |
|
| 561 | + $rootView = new View(''); |
|
| 562 | + $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 563 | + $rootView->file_put_contents('/substorage/bar.txt', 'asd'); |
|
| 564 | + |
|
| 565 | + $this->assertTrue($rootView->file_exists('foo.txt')); |
|
| 566 | + $this->assertTrue($rootView->file_exists('substorage/bar.txt')); |
|
| 567 | + |
|
| 568 | + $this->assertTrue($rootView->unlink('foo.txt')); |
|
| 569 | + $this->assertTrue($rootView->unlink('substorage/bar.txt')); |
|
| 570 | + |
|
| 571 | + $this->assertFalse($rootView->file_exists('foo.txt')); |
|
| 572 | + $this->assertFalse($rootView->file_exists('substorage/bar.txt')); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + public static function rmdirOrUnlinkDataProvider(): array { |
|
| 576 | + return [['rmdir'], ['unlink']]; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + #[\PHPUnit\Framework\Attributes\DataProvider('rmdirOrUnlinkDataProvider')] |
|
| 580 | + public function testRmdir($method): void { |
|
| 581 | + $storage1 = $this->getTestStorage(); |
|
| 582 | + Filesystem::mount($storage1, [], '/'); |
|
| 583 | + |
|
| 584 | + $rootView = new View(''); |
|
| 585 | + $rootView->mkdir('sub'); |
|
| 586 | + $rootView->mkdir('sub/deep'); |
|
| 587 | + $rootView->file_put_contents('/sub/deep/foo.txt', 'asd'); |
|
| 588 | + |
|
| 589 | + $this->assertTrue($rootView->file_exists('sub/deep/foo.txt')); |
|
| 590 | + |
|
| 591 | + $this->assertTrue($rootView->$method('sub')); |
|
| 592 | + |
|
| 593 | + $this->assertFalse($rootView->file_exists('sub')); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * @medium |
|
| 598 | + */ |
|
| 599 | + public function testUnlinkRootMustFail(): void { |
|
| 600 | + $storage1 = $this->getTestStorage(); |
|
| 601 | + $storage2 = $this->getTestStorage(); |
|
| 602 | + Filesystem::mount($storage1, [], '/'); |
|
| 603 | + Filesystem::mount($storage2, [], '/substorage'); |
|
| 604 | + |
|
| 605 | + $rootView = new View(''); |
|
| 606 | + $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 607 | + $rootView->file_put_contents('/substorage/bar.txt', 'asd'); |
|
| 608 | + |
|
| 609 | + $this->assertFalse($rootView->unlink('')); |
|
| 610 | + $this->assertFalse($rootView->unlink('/')); |
|
| 611 | + $this->assertFalse($rootView->unlink('substorage')); |
|
| 612 | + $this->assertFalse($rootView->unlink('/substorage')); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * @medium |
|
| 617 | + */ |
|
| 618 | + public function testTouch(): void { |
|
| 619 | + $storage = $this->getTestStorage(true, TemporaryNoTouch::class); |
|
| 620 | + |
|
| 621 | + Filesystem::mount($storage, [], '/'); |
|
| 622 | + |
|
| 623 | + $rootView = new View(''); |
|
| 624 | + $oldCachedData = $rootView->getFileInfo('foo.txt'); |
|
| 625 | + |
|
| 626 | + $rootView->touch('foo.txt', 500); |
|
| 627 | + |
|
| 628 | + $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 629 | + $this->assertEquals(500, $cachedData['mtime']); |
|
| 630 | + $this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']); |
|
| 631 | + |
|
| 632 | + $rootView->putFileInfo('foo.txt', ['storage_mtime' => 1000]); //make sure the watcher detects the change |
|
| 633 | + $rootView->file_put_contents('foo.txt', 'asd'); |
|
| 634 | + $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 635 | + $this->assertGreaterThanOrEqual($oldCachedData['mtime'], $cachedData['mtime']); |
|
| 636 | + $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * @medium |
|
| 641 | + */ |
|
| 642 | + public function testTouchFloat(): void { |
|
| 643 | + $storage = $this->getTestStorage(true, TemporaryNoTouch::class); |
|
| 644 | + |
|
| 645 | + Filesystem::mount($storage, [], '/'); |
|
| 646 | + |
|
| 647 | + $rootView = new View(''); |
|
| 648 | + $oldCachedData = $rootView->getFileInfo('foo.txt'); |
|
| 649 | + |
|
| 650 | + $rootView->touch('foo.txt', 500.5); |
|
| 651 | + |
|
| 652 | + $cachedData = $rootView->getFileInfo('foo.txt'); |
|
| 653 | + $this->assertEquals(500, $cachedData['mtime']); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * @medium |
|
| 658 | + */ |
|
| 659 | + public function testViewHooks(): void { |
|
| 660 | + $storage1 = $this->getTestStorage(); |
|
| 661 | + $storage2 = $this->getTestStorage(); |
|
| 662 | + $defaultRoot = Filesystem::getRoot(); |
|
| 663 | + Filesystem::mount($storage1, [], '/'); |
|
| 664 | + Filesystem::mount($storage2, [], $defaultRoot . '/substorage'); |
|
| 665 | + \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
| 666 | + |
|
| 667 | + $rootView = new View(''); |
|
| 668 | + $subView = new View($defaultRoot . '/substorage'); |
|
| 669 | + $this->hookPath = null; |
|
| 670 | + |
|
| 671 | + $rootView->file_put_contents('/foo.txt', 'asd'); |
|
| 672 | + $this->assertNull($this->hookPath); |
|
| 673 | + |
|
| 674 | + $subView->file_put_contents('/foo.txt', 'asd'); |
|
| 675 | + $this->assertEquals('/substorage/foo.txt', $this->hookPath); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + private $hookPath; |
|
| 679 | + |
|
| 680 | + public function dummyHook($params) { |
|
| 681 | + $this->hookPath = $params['path']; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + public function testSearchNotOutsideView(): void { |
|
| 685 | + $storage1 = $this->getTestStorage(); |
|
| 686 | + Filesystem::mount($storage1, [], '/'); |
|
| 687 | + $storage1->rename('folder', 'foo'); |
|
| 688 | + $scanner = $storage1->getScanner(); |
|
| 689 | + $scanner->scan(''); |
|
| 690 | + |
|
| 691 | + $view = new View('/foo'); |
|
| 692 | + |
|
| 693 | + $result = $view->search('.txt'); |
|
| 694 | + $this->assertCount(1, $result); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * @param bool $scan |
|
| 699 | + * @param string $class |
|
| 700 | + * @return Storage |
|
| 701 | + */ |
|
| 702 | + private function getTestStorage($scan = true, $class = Temporary::class) { |
|
| 703 | + /** |
|
| 704 | + * @var Storage $storage |
|
| 705 | + */ |
|
| 706 | + $storage = new $class([]); |
|
| 707 | + $textData = "dummy file data\n"; |
|
| 708 | + $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png'); |
|
| 709 | + $storage->mkdir('folder'); |
|
| 710 | + $storage->file_put_contents('foo.txt', $textData); |
|
| 711 | + $storage->file_put_contents('foo.png', $imgData); |
|
| 712 | + $storage->file_put_contents('folder/bar.txt', $textData); |
|
| 713 | + |
|
| 714 | + if ($scan) { |
|
| 715 | + $scanner = $storage->getScanner(); |
|
| 716 | + $scanner->scan(''); |
|
| 717 | + } |
|
| 718 | + $this->storages[] = $storage; |
|
| 719 | + return $storage; |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * @medium |
|
| 724 | + */ |
|
| 725 | + public function testViewHooksIfRootStartsTheSame(): void { |
|
| 726 | + $storage1 = $this->getTestStorage(); |
|
| 727 | + $storage2 = $this->getTestStorage(); |
|
| 728 | + $defaultRoot = Filesystem::getRoot(); |
|
| 729 | + Filesystem::mount($storage1, [], '/'); |
|
| 730 | + Filesystem::mount($storage2, [], $defaultRoot . '_substorage'); |
|
| 731 | + \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); |
|
| 732 | + |
|
| 733 | + $subView = new View($defaultRoot . '_substorage'); |
|
| 734 | + $this->hookPath = null; |
|
| 735 | + |
|
| 736 | + $subView->file_put_contents('/foo.txt', 'asd'); |
|
| 737 | + $this->assertNull($this->hookPath); |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + private $hookWritePath; |
|
| 741 | + private $hookCreatePath; |
|
| 742 | + private $hookUpdatePath; |
|
| 743 | + |
|
| 744 | + public function dummyHookWrite($params) { |
|
| 745 | + $this->hookWritePath = $params['path']; |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + public function dummyHookUpdate($params) { |
|
| 749 | + $this->hookUpdatePath = $params['path']; |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + public function dummyHookCreate($params) { |
|
| 753 | + $this->hookCreatePath = $params['path']; |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + public function testEditNoCreateHook(): void { |
|
| 757 | + $storage1 = $this->getTestStorage(); |
|
| 758 | + $storage2 = $this->getTestStorage(); |
|
| 759 | + $defaultRoot = Filesystem::getRoot(); |
|
| 760 | + Filesystem::mount($storage1, [], '/'); |
|
| 761 | + Filesystem::mount($storage2, [], $defaultRoot); |
|
| 762 | + \OC_Hook::connect('OC_Filesystem', 'post_create', $this, 'dummyHookCreate'); |
|
| 763 | + \OC_Hook::connect('OC_Filesystem', 'post_update', $this, 'dummyHookUpdate'); |
|
| 764 | + \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHookWrite'); |
|
| 765 | + |
|
| 766 | + $view = new View($defaultRoot); |
|
| 767 | + $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null; |
|
| 768 | + |
|
| 769 | + $view->file_put_contents('/asd.txt', 'foo'); |
|
| 770 | + $this->assertEquals('/asd.txt', $this->hookCreatePath); |
|
| 771 | + $this->assertNull($this->hookUpdatePath); |
|
| 772 | + $this->assertEquals('/asd.txt', $this->hookWritePath); |
|
| 773 | + |
|
| 774 | + $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null; |
|
| 775 | + |
|
| 776 | + $view->file_put_contents('/asd.txt', 'foo'); |
|
| 777 | + $this->assertNull($this->hookCreatePath); |
|
| 778 | + $this->assertEquals('/asd.txt', $this->hookUpdatePath); |
|
| 779 | + $this->assertEquals('/asd.txt', $this->hookWritePath); |
|
| 780 | + |
|
| 781 | + \OC_Hook::clear('OC_Filesystem', 'post_create'); |
|
| 782 | + \OC_Hook::clear('OC_Filesystem', 'post_update'); |
|
| 783 | + \OC_Hook::clear('OC_Filesystem', 'post_write'); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + #[\PHPUnit\Framework\Attributes\DataProvider('resolvePathTestProvider')] |
|
| 787 | + public function testResolvePath($expected, $pathToTest): void { |
|
| 788 | + $storage1 = $this->getTestStorage(); |
|
| 789 | + Filesystem::mount($storage1, [], '/'); |
|
| 790 | + |
|
| 791 | + $view = new View(''); |
|
| 792 | + |
|
| 793 | + $result = $view->resolvePath($pathToTest); |
|
| 794 | + $this->assertEquals($expected, $result[1]); |
|
| 795 | + |
|
| 796 | + $exists = $view->file_exists($pathToTest); |
|
| 797 | + $this->assertTrue($exists); |
|
| 798 | + |
|
| 799 | + $exists = $view->file_exists($result[1]); |
|
| 800 | + $this->assertTrue($exists); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + public static function resolvePathTestProvider(): array { |
|
| 804 | + return [ |
|
| 805 | + ['foo.txt', 'foo.txt'], |
|
| 806 | + ['foo.txt', '/foo.txt'], |
|
| 807 | + ['folder', 'folder'], |
|
| 808 | + ['folder', '/folder'], |
|
| 809 | + ['folder', 'folder/'], |
|
| 810 | + ['folder', '/folder/'], |
|
| 811 | + ['folder/bar.txt', 'folder/bar.txt'], |
|
| 812 | + ['folder/bar.txt', '/folder/bar.txt'], |
|
| 813 | + ['', ''], |
|
| 814 | + ['', '/'], |
|
| 815 | + ]; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + public function testUTF8Names(): void { |
|
| 819 | + $names = ['虚', '和知しゃ和で', 'regular ascii', 'sɨˈrɪlɪk', 'ѨѬ', 'أنا أحب القراءة كثيرا']; |
|
| 820 | + |
|
| 821 | + $storage = new Temporary([]); |
|
| 822 | + Filesystem::mount($storage, [], '/'); |
|
| 823 | + |
|
| 824 | + $rootView = new View(''); |
|
| 825 | + foreach ($names as $name) { |
|
| 826 | + $rootView->file_put_contents('/' . $name, 'dummy content'); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + $list = $rootView->getDirectoryContent('/'); |
|
| 830 | + |
|
| 831 | + $this->assertCount(count($names), $list); |
|
| 832 | + foreach ($list as $item) { |
|
| 833 | + $this->assertContains($item['name'], $names); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + $cache = $storage->getCache(); |
|
| 837 | + $scanner = $storage->getScanner(); |
|
| 838 | + $scanner->scan(''); |
|
| 839 | + |
|
| 840 | + $list = $cache->getFolderContents(''); |
|
| 841 | + |
|
| 842 | + $this->assertCount(count($names), $list); |
|
| 843 | + foreach ($list as $item) { |
|
| 844 | + $this->assertContains($item['name'], $names); |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + public function xtestLongPath() { |
|
| 849 | + $storage = new Temporary([]); |
|
| 850 | + Filesystem::mount($storage, [], '/'); |
|
| 851 | + |
|
| 852 | + $rootView = new View(''); |
|
| 853 | + |
|
| 854 | + $longPath = ''; |
|
| 855 | + $ds = DIRECTORY_SEPARATOR; |
|
| 856 | + /* |
|
| 857 | 857 | * 4096 is the maximum path length in file_cache.path in *nix |
| 858 | 858 | * 1024 is the max path length in mac |
| 859 | 859 | */ |
| 860 | - $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789'; |
|
| 861 | - $tmpdirLength = strlen(Server::get(ITempManager::class)->getTemporaryFolder()); |
|
| 862 | - if (\OC_Util::runningOnMac()) { |
|
| 863 | - $depth = ((1024 - $tmpdirLength) / 57); |
|
| 864 | - } else { |
|
| 865 | - $depth = ((4000 - $tmpdirLength) / 57); |
|
| 866 | - } |
|
| 867 | - foreach (range(0, $depth - 1) as $i) { |
|
| 868 | - $longPath .= $ds . $folderName; |
|
| 869 | - $result = $rootView->mkdir($longPath); |
|
| 870 | - $this->assertTrue($result, "mkdir failed on $i - path length: " . strlen($longPath)); |
|
| 871 | - |
|
| 872 | - $result = $rootView->file_put_contents($longPath . "{$ds}test.txt", 'lorem'); |
|
| 873 | - $this->assertEquals(5, $result, "file_put_contents failed on $i"); |
|
| 874 | - |
|
| 875 | - $this->assertTrue($rootView->file_exists($longPath)); |
|
| 876 | - $this->assertTrue($rootView->file_exists($longPath . "{$ds}test.txt")); |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - $cache = $storage->getCache(); |
|
| 880 | - $scanner = $storage->getScanner(); |
|
| 881 | - $scanner->scan(''); |
|
| 882 | - |
|
| 883 | - $longPath = $folderName; |
|
| 884 | - foreach (range(0, $depth - 1) as $i) { |
|
| 885 | - $cachedFolder = $cache->get($longPath); |
|
| 886 | - $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at $i"); |
|
| 887 | - $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at $i"); |
|
| 888 | - |
|
| 889 | - $cachedFile = $cache->get($longPath . '/test.txt'); |
|
| 890 | - $this->assertTrue(is_array($cachedFile), "No cache entry for file at $i"); |
|
| 891 | - $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at $i"); |
|
| 892 | - |
|
| 893 | - $longPath .= $ds . $folderName; |
|
| 894 | - } |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - public function testTouchNotSupported(): void { |
|
| 898 | - $storage = new TemporaryNoTouch([]); |
|
| 899 | - $scanner = $storage->getScanner(); |
|
| 900 | - Filesystem::mount($storage, [], '/test/'); |
|
| 901 | - $past = time() - 100; |
|
| 902 | - $storage->file_put_contents('test', 'foobar'); |
|
| 903 | - $scanner->scan(''); |
|
| 904 | - $view = new View(''); |
|
| 905 | - $info = $view->getFileInfo('/test/test'); |
|
| 906 | - |
|
| 907 | - $view->touch('/test/test', $past); |
|
| 908 | - $scanner->scanFile('test', Scanner::REUSE_ETAG); |
|
| 909 | - |
|
| 910 | - $info2 = $view->getFileInfo('/test/test'); |
|
| 911 | - $this->assertSame($info['etag'], $info2['etag']); |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - public function testWatcherEtagCrossStorage(): void { |
|
| 915 | - $storage1 = new Temporary([]); |
|
| 916 | - $storage2 = new Temporary([]); |
|
| 917 | - $scanner1 = $storage1->getScanner(); |
|
| 918 | - $scanner2 = $storage2->getScanner(); |
|
| 919 | - $storage1->mkdir('sub'); |
|
| 920 | - Filesystem::mount($storage1, [], '/test/'); |
|
| 921 | - Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 922 | - |
|
| 923 | - $past = time() - 100; |
|
| 924 | - $storage2->file_put_contents('test.txt', 'foobar'); |
|
| 925 | - $scanner1->scan(''); |
|
| 926 | - $scanner2->scan(''); |
|
| 927 | - $view = new View(''); |
|
| 928 | - |
|
| 929 | - $storage2->getWatcher('')->setPolicy(Watcher::CHECK_ALWAYS); |
|
| 930 | - |
|
| 931 | - $oldFileInfo = $view->getFileInfo('/test/sub/storage/test.txt'); |
|
| 932 | - $oldFolderInfo = $view->getFileInfo('/test'); |
|
| 933 | - |
|
| 934 | - $storage2->getCache()->update($oldFileInfo->getId(), [ |
|
| 935 | - 'storage_mtime' => $past, |
|
| 936 | - ]); |
|
| 937 | - |
|
| 938 | - $oldEtag = $oldFolderInfo->getEtag(); |
|
| 939 | - |
|
| 940 | - $view->getFileInfo('/test/sub/storage/test.txt'); |
|
| 941 | - $newFolderInfo = $view->getFileInfo('/test'); |
|
| 942 | - |
|
| 943 | - $this->assertNotEquals($newFolderInfo->getEtag(), $oldEtag); |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - #[\PHPUnit\Framework\Attributes\DataProvider('absolutePathProvider')] |
|
| 947 | - public function testGetAbsolutePath($expectedPath, $relativePath): void { |
|
| 948 | - $view = new View('/files'); |
|
| 949 | - $this->assertEquals($expectedPath, $view->getAbsolutePath($relativePath)); |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - public function testPartFileInfo(): void { |
|
| 953 | - $storage = new Temporary([]); |
|
| 954 | - $scanner = $storage->getScanner(); |
|
| 955 | - Filesystem::mount($storage, [], '/test/'); |
|
| 956 | - $sizeWritten = $storage->file_put_contents('test.part', 'foobar'); |
|
| 957 | - $scanner->scan(''); |
|
| 958 | - $view = new View('/test'); |
|
| 959 | - $info = $view->getFileInfo('test.part'); |
|
| 960 | - |
|
| 961 | - $this->assertInstanceOf('\OCP\Files\FileInfo', $info); |
|
| 962 | - $this->assertNull($info->getId()); |
|
| 963 | - $this->assertEquals(6, $sizeWritten); |
|
| 964 | - $this->assertEquals(6, $info->getSize()); |
|
| 965 | - $this->assertEquals('foobar', $view->file_get_contents('test.part')); |
|
| 966 | - } |
|
| 967 | - |
|
| 968 | - public static function absolutePathProvider(): array { |
|
| 969 | - return [ |
|
| 970 | - ['/files/', ''], |
|
| 971 | - ['/files/0', '0'], |
|
| 972 | - ['/files/false', 'false'], |
|
| 973 | - ['/files/true', 'true'], |
|
| 974 | - ['/files/', '/'], |
|
| 975 | - ['/files/test', 'test'], |
|
| 976 | - ['/files/test', '/test'], |
|
| 977 | - ]; |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - #[\PHPUnit\Framework\Attributes\DataProvider('chrootRelativePathProvider')] |
|
| 981 | - public function testChrootGetRelativePath($root, $absolutePath, $expectedPath): void { |
|
| 982 | - $view = new View('/files'); |
|
| 983 | - $view->chroot($root); |
|
| 984 | - $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath)); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - public static function chrootRelativePathProvider(): array { |
|
| 988 | - return self::relativePathProvider('/'); |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - #[\PHPUnit\Framework\Attributes\DataProvider('initRelativePathProvider')] |
|
| 992 | - public function testInitGetRelativePath($root, $absolutePath, $expectedPath): void { |
|
| 993 | - $view = new View($root); |
|
| 994 | - $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath)); |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - public static function initRelativePathProvider(): array { |
|
| 998 | - return self::relativePathProvider(null); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - public static function relativePathProvider($missingRootExpectedPath): array { |
|
| 1002 | - return [ |
|
| 1003 | - // No root - returns the path |
|
| 1004 | - ['', '/files', '/files'], |
|
| 1005 | - ['', '/files/', '/files/'], |
|
| 1006 | - |
|
| 1007 | - // Root equals path - / |
|
| 1008 | - ['/files/', '/files/', '/'], |
|
| 1009 | - ['/files/', '/files', '/'], |
|
| 1010 | - ['/files', '/files/', '/'], |
|
| 1011 | - ['/files', '/files', '/'], |
|
| 1012 | - |
|
| 1013 | - // False negatives: chroot fixes those by adding the leading slash. |
|
| 1014 | - // But setting them up with this root (instead of chroot($root)) |
|
| 1015 | - // will fail them, although they should be the same. |
|
| 1016 | - // TODO init should be fixed, so it also adds the leading slash |
|
| 1017 | - ['files/', '/files/', $missingRootExpectedPath], |
|
| 1018 | - ['files', '/files/', $missingRootExpectedPath], |
|
| 1019 | - ['files/', '/files', $missingRootExpectedPath], |
|
| 1020 | - ['files', '/files', $missingRootExpectedPath], |
|
| 1021 | - |
|
| 1022 | - // False negatives: Paths provided to the method should have a leading slash |
|
| 1023 | - // TODO input should be checked to have a leading slash |
|
| 1024 | - ['/files/', 'files/', null], |
|
| 1025 | - ['/files', 'files/', null], |
|
| 1026 | - ['/files/', 'files', null], |
|
| 1027 | - ['/files', 'files', null], |
|
| 1028 | - |
|
| 1029 | - // with trailing slashes |
|
| 1030 | - ['/files/', '/files/0', '0'], |
|
| 1031 | - ['/files/', '/files/false', 'false'], |
|
| 1032 | - ['/files/', '/files/true', 'true'], |
|
| 1033 | - ['/files/', '/files/test', 'test'], |
|
| 1034 | - ['/files/', '/files/test/foo', 'test/foo'], |
|
| 1035 | - |
|
| 1036 | - // without trailing slashes |
|
| 1037 | - // TODO false expectation: Should match "with trailing slashes" |
|
| 1038 | - ['/files', '/files/0', '/0'], |
|
| 1039 | - ['/files', '/files/false', '/false'], |
|
| 1040 | - ['/files', '/files/true', '/true'], |
|
| 1041 | - ['/files', '/files/test', '/test'], |
|
| 1042 | - ['/files', '/files/test/foo', '/test/foo'], |
|
| 1043 | - |
|
| 1044 | - // leading slashes |
|
| 1045 | - ['/files/', '/files_trashbin/', null], |
|
| 1046 | - ['/files', '/files_trashbin/', null], |
|
| 1047 | - ['/files/', '/files_trashbin', null], |
|
| 1048 | - ['/files', '/files_trashbin', null], |
|
| 1049 | - |
|
| 1050 | - // no leading slashes |
|
| 1051 | - ['files/', 'files_trashbin/', null], |
|
| 1052 | - ['files', 'files_trashbin/', null], |
|
| 1053 | - ['files/', 'files_trashbin', null], |
|
| 1054 | - ['files', 'files_trashbin', null], |
|
| 1055 | - |
|
| 1056 | - // mixed leading slashes |
|
| 1057 | - ['files/', '/files_trashbin/', null], |
|
| 1058 | - ['/files/', 'files_trashbin/', null], |
|
| 1059 | - ['files', '/files_trashbin/', null], |
|
| 1060 | - ['/files', 'files_trashbin/', null], |
|
| 1061 | - ['files/', '/files_trashbin', null], |
|
| 1062 | - ['/files/', 'files_trashbin', null], |
|
| 1063 | - ['files', '/files_trashbin', null], |
|
| 1064 | - ['/files', 'files_trashbin', null], |
|
| 1065 | - |
|
| 1066 | - ['files', 'files_trashbin/test', null], |
|
| 1067 | - ['/files', '/files_trashbin/test', null], |
|
| 1068 | - ['/files', 'files_trashbin/test', null], |
|
| 1069 | - ]; |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - public function testFileView(): void { |
|
| 1073 | - $storage = new Temporary([]); |
|
| 1074 | - $scanner = $storage->getScanner(); |
|
| 1075 | - $storage->file_put_contents('foo.txt', 'bar'); |
|
| 1076 | - Filesystem::mount($storage, [], '/test/'); |
|
| 1077 | - $scanner->scan(''); |
|
| 1078 | - $view = new View('/test/foo.txt'); |
|
| 1079 | - |
|
| 1080 | - $this->assertEquals('bar', $view->file_get_contents('')); |
|
| 1081 | - $fh = tmpfile(); |
|
| 1082 | - fwrite($fh, 'foo'); |
|
| 1083 | - rewind($fh); |
|
| 1084 | - $view->file_put_contents('', $fh); |
|
| 1085 | - $this->assertEquals('foo', $view->file_get_contents('')); |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - #[\PHPUnit\Framework\Attributes\DataProvider('tooLongPathDataProvider')] |
|
| 1089 | - public function testTooLongPath($operation, $param0 = null): void { |
|
| 1090 | - $this->expectException(InvalidPathException::class); |
|
| 1091 | - |
|
| 1092 | - |
|
| 1093 | - $longPath = ''; |
|
| 1094 | - // 4000 is the maximum path length in file_cache.path |
|
| 1095 | - $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789'; |
|
| 1096 | - $depth = (4000 / 57); |
|
| 1097 | - foreach (range(0, $depth + 1) as $i) { |
|
| 1098 | - $longPath .= '/' . $folderName; |
|
| 1099 | - } |
|
| 1100 | - |
|
| 1101 | - $storage = new Temporary([]); |
|
| 1102 | - $this->tempStorage = $storage; // for later hard cleanup |
|
| 1103 | - Filesystem::mount($storage, [], '/'); |
|
| 1104 | - |
|
| 1105 | - $rootView = new View(''); |
|
| 1106 | - |
|
| 1107 | - if ($param0 === '@0') { |
|
| 1108 | - $param0 = $longPath; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - if ($operation === 'hash') { |
|
| 1112 | - $param0 = $longPath; |
|
| 1113 | - $longPath = 'md5'; |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - call_user_func([$rootView, $operation], $longPath, $param0); |
|
| 1117 | - } |
|
| 1118 | - |
|
| 1119 | - public static function tooLongPathDataProvider(): array { |
|
| 1120 | - return [ |
|
| 1121 | - ['getAbsolutePath'], |
|
| 1122 | - ['getRelativePath'], |
|
| 1123 | - ['getMountPoint'], |
|
| 1124 | - ['resolvePath'], |
|
| 1125 | - ['getLocalFile'], |
|
| 1126 | - ['mkdir'], |
|
| 1127 | - ['rmdir'], |
|
| 1128 | - ['opendir'], |
|
| 1129 | - ['is_dir'], |
|
| 1130 | - ['is_file'], |
|
| 1131 | - ['stat'], |
|
| 1132 | - ['filetype'], |
|
| 1133 | - ['filesize'], |
|
| 1134 | - ['readfile'], |
|
| 1135 | - ['isCreatable'], |
|
| 1136 | - ['isReadable'], |
|
| 1137 | - ['isUpdatable'], |
|
| 1138 | - ['isDeletable'], |
|
| 1139 | - ['isSharable'], |
|
| 1140 | - ['file_exists'], |
|
| 1141 | - ['filemtime'], |
|
| 1142 | - ['touch'], |
|
| 1143 | - ['file_get_contents'], |
|
| 1144 | - ['unlink'], |
|
| 1145 | - ['deleteAll'], |
|
| 1146 | - ['toTmpFile'], |
|
| 1147 | - ['getMimeType'], |
|
| 1148 | - ['free_space'], |
|
| 1149 | - ['getFileInfo'], |
|
| 1150 | - ['getDirectoryContent'], |
|
| 1151 | - ['getOwner'], |
|
| 1152 | - ['getETag'], |
|
| 1153 | - ['file_put_contents', 'ipsum'], |
|
| 1154 | - ['rename', '@0'], |
|
| 1155 | - ['copy', '@0'], |
|
| 1156 | - ['fopen', 'r'], |
|
| 1157 | - ['fromTmpFile', '@0'], |
|
| 1158 | - ['hash'], |
|
| 1159 | - ['hasUpdated', 0], |
|
| 1160 | - ['putFileInfo', []], |
|
| 1161 | - ]; |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - public function testRenameCrossStoragePreserveMtime(): void { |
|
| 1165 | - $storage1 = new Temporary([]); |
|
| 1166 | - $storage2 = new Temporary([]); |
|
| 1167 | - $storage1->mkdir('sub'); |
|
| 1168 | - $storage1->mkdir('foo'); |
|
| 1169 | - $storage1->file_put_contents('foo.txt', 'asd'); |
|
| 1170 | - $storage1->file_put_contents('foo/bar.txt', 'asd'); |
|
| 1171 | - Filesystem::mount($storage1, [], '/test/'); |
|
| 1172 | - Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 1173 | - |
|
| 1174 | - $view = new View(''); |
|
| 1175 | - $time = time() - 200; |
|
| 1176 | - $view->touch('/test/foo.txt', $time); |
|
| 1177 | - $view->touch('/test/foo', $time); |
|
| 1178 | - $view->touch('/test/foo/bar.txt', $time); |
|
| 1179 | - |
|
| 1180 | - $view->rename('/test/foo.txt', '/test/sub/storage/foo.txt'); |
|
| 1181 | - |
|
| 1182 | - $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo.txt')); |
|
| 1183 | - |
|
| 1184 | - $view->rename('/test/foo', '/test/sub/storage/foo'); |
|
| 1185 | - |
|
| 1186 | - $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo/bar.txt')); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - public function testRenameFailDeleteTargetKeepSource(): void { |
|
| 1190 | - $this->doTestCopyRenameFail('rename'); |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - public function testCopyFailDeleteTargetKeepSource(): void { |
|
| 1194 | - $this->doTestCopyRenameFail('copy'); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - private function doTestCopyRenameFail($operation) { |
|
| 1198 | - $storage1 = new Temporary([]); |
|
| 1199 | - /** @var \PHPUnit\Framework\MockObject\MockObject|Temporary $storage2 */ |
|
| 1200 | - $storage2 = $this->getMockBuilder(TemporaryNoCross::class) |
|
| 1201 | - ->setConstructorArgs([[]]) |
|
| 1202 | - ->onlyMethods(['fopen', 'writeStream']) |
|
| 1203 | - ->getMock(); |
|
| 1204 | - |
|
| 1205 | - $storage2->method('writeStream') |
|
| 1206 | - ->willThrowException(new GenericFileException('Failed to copy stream')); |
|
| 1207 | - |
|
| 1208 | - $storage1->mkdir('sub'); |
|
| 1209 | - $storage1->file_put_contents('foo.txt', '0123456789ABCDEFGH'); |
|
| 1210 | - $storage1->mkdir('dirtomove'); |
|
| 1211 | - $storage1->file_put_contents('dirtomove/indir1.txt', '0123456'); // fits |
|
| 1212 | - $storage1->file_put_contents('dirtomove/indir2.txt', '0123456789ABCDEFGH'); // doesn't fit |
|
| 1213 | - $storage2->file_put_contents('existing.txt', '0123'); |
|
| 1214 | - $storage1->getScanner()->scan(''); |
|
| 1215 | - $storage2->getScanner()->scan(''); |
|
| 1216 | - Filesystem::mount($storage1, [], '/test/'); |
|
| 1217 | - Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 1218 | - |
|
| 1219 | - // move file |
|
| 1220 | - $view = new View(''); |
|
| 1221 | - $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1222 | - $this->assertFalse($storage2->file_exists('foo.txt')); |
|
| 1223 | - $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/foo.txt')); |
|
| 1224 | - $this->assertFalse($storage2->file_exists('foo.txt')); |
|
| 1225 | - $this->assertFalse($storage2->getCache()->get('foo.txt')); |
|
| 1226 | - $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1227 | - |
|
| 1228 | - // if target exists, it will be deleted too |
|
| 1229 | - $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/existing.txt')); |
|
| 1230 | - $this->assertFalse($storage2->file_exists('existing.txt')); |
|
| 1231 | - $this->assertFalse($storage2->getCache()->get('existing.txt')); |
|
| 1232 | - $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1233 | - |
|
| 1234 | - // move folder |
|
| 1235 | - $this->assertFalse($view->$operation('/test/dirtomove/', '/test/sub/storage/dirtomove/')); |
|
| 1236 | - // since the move failed, the full source tree is kept |
|
| 1237 | - $this->assertTrue($storage1->file_exists('dirtomove/indir1.txt')); |
|
| 1238 | - $this->assertTrue($storage1->file_exists('dirtomove/indir2.txt')); |
|
| 1239 | - // second file not moved/copied |
|
| 1240 | - $this->assertFalse($storage2->file_exists('dirtomove/indir2.txt')); |
|
| 1241 | - $this->assertFalse($storage2->getCache()->get('dirtomove/indir2.txt')); |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - public function testDeleteFailKeepCache(): void { |
|
| 1245 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 1246 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 1247 | - ->setConstructorArgs([[]]) |
|
| 1248 | - ->onlyMethods(['unlink']) |
|
| 1249 | - ->getMock(); |
|
| 1250 | - $storage->expects($this->once()) |
|
| 1251 | - ->method('unlink') |
|
| 1252 | - ->willReturn(false); |
|
| 1253 | - $scanner = $storage->getScanner(); |
|
| 1254 | - $cache = $storage->getCache(); |
|
| 1255 | - $storage->file_put_contents('foo.txt', 'asd'); |
|
| 1256 | - $scanner->scan(''); |
|
| 1257 | - Filesystem::mount($storage, [], '/test/'); |
|
| 1258 | - |
|
| 1259 | - $view = new View('/test'); |
|
| 1260 | - |
|
| 1261 | - $this->assertFalse($view->unlink('foo.txt')); |
|
| 1262 | - $this->assertTrue($cache->inCache('foo.txt')); |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - public static function directoryTraversalProvider(): array { |
|
| 1266 | - return [ |
|
| 1267 | - ['../test/'], |
|
| 1268 | - ['..\\test\\my/../folder'], |
|
| 1269 | - ['/test/my/../foo\\'], |
|
| 1270 | - ]; |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - /** |
|
| 1274 | - * @param string $root |
|
| 1275 | - */ |
|
| 1276 | - #[\PHPUnit\Framework\Attributes\DataProvider('directoryTraversalProvider')] |
|
| 1277 | - public function testConstructDirectoryTraversalException($root): void { |
|
| 1278 | - $this->expectException(\Exception::class); |
|
| 1279 | - |
|
| 1280 | - new View($root); |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - public function testRenameOverWrite(): void { |
|
| 1284 | - $storage = new Temporary([]); |
|
| 1285 | - $scanner = $storage->getScanner(); |
|
| 1286 | - $storage->mkdir('sub'); |
|
| 1287 | - $storage->mkdir('foo'); |
|
| 1288 | - $storage->file_put_contents('foo.txt', 'asd'); |
|
| 1289 | - $storage->file_put_contents('foo/bar.txt', 'asd'); |
|
| 1290 | - $scanner->scan(''); |
|
| 1291 | - Filesystem::mount($storage, [], '/test/'); |
|
| 1292 | - $view = new View(''); |
|
| 1293 | - $this->assertTrue($view->rename('/test/foo.txt', '/test/foo/bar.txt')); |
|
| 1294 | - } |
|
| 1295 | - |
|
| 1296 | - public function testSetMountOptionsInStorage(): void { |
|
| 1297 | - $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']); |
|
| 1298 | - Filesystem::getMountManager()->addMount($mount); |
|
| 1299 | - /** @var Common $storage */ |
|
| 1300 | - $storage = $mount->getStorage(); |
|
| 1301 | - $this->assertEquals($storage->getMountOption('foo'), 'bar'); |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - public function testSetMountOptionsWatcherPolicy(): void { |
|
| 1305 | - $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); |
|
| 1306 | - Filesystem::getMountManager()->addMount($mount); |
|
| 1307 | - /** @var Common $storage */ |
|
| 1308 | - $storage = $mount->getStorage(); |
|
| 1309 | - $watcher = $storage->getWatcher(); |
|
| 1310 | - $this->assertEquals(Watcher::CHECK_NEVER, $watcher->getPolicy()); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - public function testGetAbsolutePathOnNull(): void { |
|
| 1314 | - $view = new View(); |
|
| 1315 | - $this->assertNull($view->getAbsolutePath(null)); |
|
| 1316 | - } |
|
| 1317 | - |
|
| 1318 | - public function testGetRelativePathOnNull(): void { |
|
| 1319 | - $view = new View(); |
|
| 1320 | - $this->assertNull($view->getRelativePath(null)); |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - |
|
| 1324 | - public function testNullAsRoot(): void { |
|
| 1325 | - $this->expectException(\TypeError::class); |
|
| 1326 | - |
|
| 1327 | - new View(null); |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - /** |
|
| 1331 | - * e.g. reading from a folder that's being renamed |
|
| 1332 | - * |
|
| 1333 | - * |
|
| 1334 | - * |
|
| 1335 | - * @param string $rootPath |
|
| 1336 | - * @param string $pathPrefix |
|
| 1337 | - */ |
|
| 1338 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1339 | - public function testReadFromWriteLockedPath($rootPath, $pathPrefix): void { |
|
| 1340 | - $this->expectException(LockedException::class); |
|
| 1341 | - |
|
| 1342 | - $rootPath = str_replace('{folder}', 'files', $rootPath); |
|
| 1343 | - $pathPrefix = str_replace('{folder}', 'files', $pathPrefix); |
|
| 1344 | - |
|
| 1345 | - $view = new View($rootPath); |
|
| 1346 | - $storage = new Temporary([]); |
|
| 1347 | - Filesystem::mount($storage, [], '/'); |
|
| 1348 | - $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1349 | - $view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED); |
|
| 1350 | - } |
|
| 1351 | - |
|
| 1352 | - /** |
|
| 1353 | - * Reading from a files_encryption folder that's being renamed |
|
| 1354 | - * |
|
| 1355 | - * |
|
| 1356 | - * @param string $rootPath |
|
| 1357 | - * @param string $pathPrefix |
|
| 1358 | - */ |
|
| 1359 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1360 | - public function testReadFromWriteUnlockablePath($rootPath, $pathPrefix): void { |
|
| 1361 | - $rootPath = str_replace('{folder}', 'files_encryption', $rootPath); |
|
| 1362 | - $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix); |
|
| 1363 | - |
|
| 1364 | - $view = new View($rootPath); |
|
| 1365 | - $storage = new Temporary([]); |
|
| 1366 | - Filesystem::mount($storage, [], '/'); |
|
| 1367 | - $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1368 | - $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED)); |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - /** |
|
| 1372 | - * e.g. writing a file that's being downloaded |
|
| 1373 | - * |
|
| 1374 | - * |
|
| 1375 | - * |
|
| 1376 | - * @param string $rootPath |
|
| 1377 | - * @param string $pathPrefix |
|
| 1378 | - */ |
|
| 1379 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1380 | - public function testWriteToReadLockedFile($rootPath, $pathPrefix): void { |
|
| 1381 | - $this->expectException(LockedException::class); |
|
| 1382 | - |
|
| 1383 | - $rootPath = str_replace('{folder}', 'files', $rootPath); |
|
| 1384 | - $pathPrefix = str_replace('{folder}', 'files', $pathPrefix); |
|
| 1385 | - |
|
| 1386 | - $view = new View($rootPath); |
|
| 1387 | - $storage = new Temporary([]); |
|
| 1388 | - Filesystem::mount($storage, [], '/'); |
|
| 1389 | - $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED)); |
|
| 1390 | - $view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * Writing a file that's being downloaded |
|
| 1395 | - * |
|
| 1396 | - * |
|
| 1397 | - * @param string $rootPath |
|
| 1398 | - * @param string $pathPrefix |
|
| 1399 | - */ |
|
| 1400 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1401 | - public function testWriteToReadUnlockableFile($rootPath, $pathPrefix): void { |
|
| 1402 | - $rootPath = str_replace('{folder}', 'files_encryption', $rootPath); |
|
| 1403 | - $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix); |
|
| 1404 | - |
|
| 1405 | - $view = new View($rootPath); |
|
| 1406 | - $storage = new Temporary([]); |
|
| 1407 | - Filesystem::mount($storage, [], '/'); |
|
| 1408 | - $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED)); |
|
| 1409 | - $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - /** |
|
| 1413 | - * Test that locks are on mount point paths instead of mount root |
|
| 1414 | - */ |
|
| 1415 | - public function testLockLocalMountPointPathInsteadOfStorageRoot(): void { |
|
| 1416 | - $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1417 | - $view = new View('/testuser/files/'); |
|
| 1418 | - $storage = new Temporary([]); |
|
| 1419 | - Filesystem::mount($storage, [], '/'); |
|
| 1420 | - $mountedStorage = new Temporary([]); |
|
| 1421 | - Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint'); |
|
| 1422 | - |
|
| 1423 | - $this->assertTrue( |
|
| 1424 | - $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, true), |
|
| 1425 | - 'Can lock mount point' |
|
| 1426 | - ); |
|
| 1427 | - |
|
| 1428 | - // no exception here because storage root was not locked |
|
| 1429 | - $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1430 | - |
|
| 1431 | - $thrown = false; |
|
| 1432 | - try { |
|
| 1433 | - $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1434 | - } catch (LockedException $e) { |
|
| 1435 | - $thrown = true; |
|
| 1436 | - } |
|
| 1437 | - $this->assertTrue($thrown, 'Mount point path was locked on root storage'); |
|
| 1438 | - |
|
| 1439 | - $lockingProvider->releaseAll(); |
|
| 1440 | - } |
|
| 1441 | - |
|
| 1442 | - /** |
|
| 1443 | - * Test that locks are on mount point paths and also mount root when requested |
|
| 1444 | - */ |
|
| 1445 | - public function testLockStorageRootButNotLocalMountPoint(): void { |
|
| 1446 | - $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1447 | - $view = new View('/testuser/files/'); |
|
| 1448 | - $storage = new Temporary([]); |
|
| 1449 | - Filesystem::mount($storage, [], '/'); |
|
| 1450 | - $mountedStorage = new Temporary([]); |
|
| 1451 | - Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint'); |
|
| 1452 | - |
|
| 1453 | - $this->assertTrue( |
|
| 1454 | - $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, false), |
|
| 1455 | - 'Can lock mount point' |
|
| 1456 | - ); |
|
| 1457 | - |
|
| 1458 | - $thrown = false; |
|
| 1459 | - try { |
|
| 1460 | - $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1461 | - } catch (LockedException $e) { |
|
| 1462 | - $thrown = true; |
|
| 1463 | - } |
|
| 1464 | - $this->assertTrue($thrown, 'Mount point storage root was locked on original storage'); |
|
| 1465 | - |
|
| 1466 | - // local mount point was not locked |
|
| 1467 | - $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1468 | - |
|
| 1469 | - $lockingProvider->releaseAll(); |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - /** |
|
| 1473 | - * Test that locks are on mount point paths and also mount root when requested |
|
| 1474 | - */ |
|
| 1475 | - public function testLockMountPointPathFailReleasesBoth(): void { |
|
| 1476 | - $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1477 | - $view = new View('/testuser/files/'); |
|
| 1478 | - $storage = new Temporary([]); |
|
| 1479 | - Filesystem::mount($storage, [], '/'); |
|
| 1480 | - $mountedStorage = new Temporary([]); |
|
| 1481 | - Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint.txt'); |
|
| 1482 | - |
|
| 1483 | - // this would happen if someone is writing on the mount point |
|
| 1484 | - $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1485 | - |
|
| 1486 | - $thrown = false; |
|
| 1487 | - try { |
|
| 1488 | - // this actually acquires two locks, one on the mount point and one on the storage root, |
|
| 1489 | - // but the one on the storage root will fail |
|
| 1490 | - $view->lockFile('/mountpoint.txt', ILockingProvider::LOCK_SHARED); |
|
| 1491 | - } catch (LockedException $e) { |
|
| 1492 | - $thrown = true; |
|
| 1493 | - } |
|
| 1494 | - $this->assertTrue($thrown, 'Cannot acquire shared lock because storage root is already locked'); |
|
| 1495 | - |
|
| 1496 | - // from here we expect that the lock on the local mount point was released properly |
|
| 1497 | - // so acquiring an exclusive lock will succeed |
|
| 1498 | - $storage->acquireLock('/testuser/files/mountpoint.txt', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1499 | - |
|
| 1500 | - $lockingProvider->releaseAll(); |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - public static function dataLockPaths(): array { |
|
| 1504 | - return [ |
|
| 1505 | - ['/testuser/{folder}', ''], |
|
| 1506 | - ['/testuser', '/{folder}'], |
|
| 1507 | - ['', '/testuser/{folder}'], |
|
| 1508 | - ]; |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - public static function pathRelativeToFilesProvider(): array { |
|
| 1512 | - return [ |
|
| 1513 | - ['admin/files', ''], |
|
| 1514 | - ['admin/files/x', 'x'], |
|
| 1515 | - ['/admin/files', ''], |
|
| 1516 | - ['/admin/files/sub', 'sub'], |
|
| 1517 | - ['/admin/files/sub/', 'sub'], |
|
| 1518 | - ['/admin/files/sub/sub2', 'sub/sub2'], |
|
| 1519 | - ['//admin//files/sub//sub2', 'sub/sub2'], |
|
| 1520 | - ]; |
|
| 1521 | - } |
|
| 1522 | - |
|
| 1523 | - #[\PHPUnit\Framework\Attributes\DataProvider('pathRelativeToFilesProvider')] |
|
| 1524 | - public function testGetPathRelativeToFiles($path, $expectedPath): void { |
|
| 1525 | - $view = new View(); |
|
| 1526 | - $this->assertEquals($expectedPath, $view->getPathRelativeToFiles($path)); |
|
| 1527 | - } |
|
| 1528 | - |
|
| 1529 | - public static function pathRelativeToFilesProviderExceptionCases(): array { |
|
| 1530 | - return [ |
|
| 1531 | - [''], |
|
| 1532 | - ['x'], |
|
| 1533 | - ['files'], |
|
| 1534 | - ['/files'], |
|
| 1535 | - ['/admin/files_versions/abc'], |
|
| 1536 | - ]; |
|
| 1537 | - } |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * @param string $path |
|
| 1541 | - */ |
|
| 1542 | - #[\PHPUnit\Framework\Attributes\DataProvider('pathRelativeToFilesProviderExceptionCases')] |
|
| 1543 | - public function testGetPathRelativeToFilesWithInvalidArgument($path): void { |
|
| 1544 | - $this->expectException(\InvalidArgumentException::class); |
|
| 1545 | - $this->expectExceptionMessage('$absolutePath must be relative to "files"'); |
|
| 1546 | - |
|
| 1547 | - $view = new View(); |
|
| 1548 | - $view->getPathRelativeToFiles($path); |
|
| 1549 | - } |
|
| 1550 | - |
|
| 1551 | - public function testChangeLock(): void { |
|
| 1552 | - $view = new View('/testuser/files/'); |
|
| 1553 | - $storage = new Temporary([]); |
|
| 1554 | - Filesystem::mount($storage, [], '/'); |
|
| 1555 | - |
|
| 1556 | - $view->lockFile('/test/sub', ILockingProvider::LOCK_SHARED); |
|
| 1557 | - $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1558 | - $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1559 | - |
|
| 1560 | - $view->changeLock('//test/sub', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1561 | - $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1562 | - |
|
| 1563 | - $view->changeLock('test/sub', ILockingProvider::LOCK_SHARED); |
|
| 1564 | - $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1565 | - |
|
| 1566 | - $view->unlockFile('/test/sub/', ILockingProvider::LOCK_SHARED); |
|
| 1567 | - |
|
| 1568 | - $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1569 | - $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1570 | - } |
|
| 1571 | - |
|
| 1572 | - public static function hookPathProvider(): array { |
|
| 1573 | - return [ |
|
| 1574 | - ['/foo/files', '/foo', true], |
|
| 1575 | - ['/foo/files/bar', '/foo', true], |
|
| 1576 | - ['/foo', '/foo', false], |
|
| 1577 | - ['/foo', '/files/foo', true], |
|
| 1578 | - ['/foo', 'filesfoo', false], |
|
| 1579 | - ['', '/foo/files', true], |
|
| 1580 | - ['', '/foo/files/bar.txt', true], |
|
| 1581 | - ]; |
|
| 1582 | - } |
|
| 1583 | - |
|
| 1584 | - /** |
|
| 1585 | - * @param $root |
|
| 1586 | - * @param $path |
|
| 1587 | - * @param $shouldEmit |
|
| 1588 | - */ |
|
| 1589 | - #[\PHPUnit\Framework\Attributes\DataProvider('hookPathProvider')] |
|
| 1590 | - public function testHookPaths($root, $path, $shouldEmit): void { |
|
| 1591 | - $filesystemReflection = new \ReflectionClass(Filesystem::class); |
|
| 1592 | - $defaultRootValue = $filesystemReflection->getProperty('defaultInstance'); |
|
| 1593 | - $defaultRootValue->setAccessible(true); |
|
| 1594 | - $oldRoot = $defaultRootValue->getValue(); |
|
| 1595 | - $defaultView = new View('/foo/files'); |
|
| 1596 | - $defaultRootValue->setValue(null, $defaultView); |
|
| 1597 | - $view = new View($root); |
|
| 1598 | - $result = self::invokePrivate($view, 'shouldEmitHooks', [$path]); |
|
| 1599 | - $defaultRootValue->setValue(null, $oldRoot); |
|
| 1600 | - $this->assertEquals($shouldEmit, $result); |
|
| 1601 | - } |
|
| 1602 | - |
|
| 1603 | - /** |
|
| 1604 | - * Create test movable mount points |
|
| 1605 | - * |
|
| 1606 | - * @param array $mountPoints array of mount point locations |
|
| 1607 | - * @return array array of MountPoint objects |
|
| 1608 | - */ |
|
| 1609 | - private function createTestMovableMountPoints($mountPoints) { |
|
| 1610 | - $mounts = []; |
|
| 1611 | - foreach ($mountPoints as $mountPoint) { |
|
| 1612 | - $storage = $this->getMockBuilder(Storage::class) |
|
| 1613 | - ->onlyMethods([]) |
|
| 1614 | - ->getMock(); |
|
| 1615 | - $storage->method('getId')->willReturn('non-null-id'); |
|
| 1616 | - $storage->method('getStorageCache')->willReturnCallback(function () use ($storage) { |
|
| 1617 | - return new \OC\Files\Cache\Storage($storage, true, Server::get(IDBConnection::class)); |
|
| 1618 | - }); |
|
| 1619 | - |
|
| 1620 | - $mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class) |
|
| 1621 | - ->onlyMethods(['moveMount']) |
|
| 1622 | - ->setConstructorArgs([$storage, $mountPoint]) |
|
| 1623 | - ->getMock(); |
|
| 1624 | - } |
|
| 1625 | - |
|
| 1626 | - /** @var IMountProvider|\PHPUnit\Framework\MockObject\MockObject $mountProvider */ |
|
| 1627 | - $mountProvider = $this->createMock(IMountProvider::class); |
|
| 1628 | - $mountProvider->expects($this->any()) |
|
| 1629 | - ->method('getMountsForUser') |
|
| 1630 | - ->willReturn($mounts); |
|
| 1631 | - |
|
| 1632 | - $mountProviderCollection = Server::get(IMountProviderCollection::class); |
|
| 1633 | - $mountProviderCollection->registerProvider($mountProvider); |
|
| 1634 | - |
|
| 1635 | - return $mounts; |
|
| 1636 | - } |
|
| 1637 | - |
|
| 1638 | - /** |
|
| 1639 | - * Test mount point move |
|
| 1640 | - */ |
|
| 1641 | - public function testMountPointMove(): void { |
|
| 1642 | - self::loginAsUser($this->user); |
|
| 1643 | - |
|
| 1644 | - [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1645 | - $this->user . '/files/mount1', |
|
| 1646 | - $this->user . '/files/mount2', |
|
| 1647 | - ]); |
|
| 1648 | - $mount1->expects($this->once()) |
|
| 1649 | - ->method('moveMount') |
|
| 1650 | - ->willReturn(true); |
|
| 1651 | - |
|
| 1652 | - $mount2->expects($this->once()) |
|
| 1653 | - ->method('moveMount') |
|
| 1654 | - ->willReturn(true); |
|
| 1655 | - |
|
| 1656 | - $view = new View('/' . $this->user . '/files/'); |
|
| 1657 | - $view->mkdir('sub'); |
|
| 1658 | - |
|
| 1659 | - $this->assertTrue($view->rename('mount1', 'renamed_mount'), 'Can rename mount point'); |
|
| 1660 | - $this->assertTrue($view->rename('mount2', 'sub/moved_mount'), 'Can move a mount point into a subdirectory'); |
|
| 1661 | - } |
|
| 1662 | - |
|
| 1663 | - public function testMoveMountPointOverwrite(): void { |
|
| 1664 | - self::loginAsUser($this->user); |
|
| 1665 | - |
|
| 1666 | - [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1667 | - $this->user . '/files/mount1', |
|
| 1668 | - $this->user . '/files/mount2', |
|
| 1669 | - ]); |
|
| 1670 | - |
|
| 1671 | - $mount1->expects($this->never()) |
|
| 1672 | - ->method('moveMount'); |
|
| 1673 | - |
|
| 1674 | - $mount2->expects($this->never()) |
|
| 1675 | - ->method('moveMount'); |
|
| 1676 | - |
|
| 1677 | - $view = new View('/' . $this->user . '/files/'); |
|
| 1678 | - |
|
| 1679 | - $this->expectException(ForbiddenException::class); |
|
| 1680 | - $view->rename('mount1', 'mount2'); |
|
| 1681 | - } |
|
| 1682 | - |
|
| 1683 | - public function testMoveMountPointIntoMount(): void { |
|
| 1684 | - self::loginAsUser($this->user); |
|
| 1685 | - |
|
| 1686 | - [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1687 | - $this->user . '/files/mount1', |
|
| 1688 | - $this->user . '/files/mount2', |
|
| 1689 | - ]); |
|
| 1690 | - |
|
| 1691 | - $mount1->expects($this->never()) |
|
| 1692 | - ->method('moveMount'); |
|
| 1693 | - |
|
| 1694 | - $mount2->expects($this->never()) |
|
| 1695 | - ->method('moveMount'); |
|
| 1696 | - |
|
| 1697 | - $view = new View('/' . $this->user . '/files/'); |
|
| 1698 | - |
|
| 1699 | - $this->expectException(ForbiddenException::class); |
|
| 1700 | - $view->rename('mount1', 'mount2/sub'); |
|
| 1701 | - } |
|
| 1702 | - |
|
| 1703 | - /** |
|
| 1704 | - * Test that moving a mount point into a shared folder is forbidden |
|
| 1705 | - */ |
|
| 1706 | - public function testMoveMountPointIntoSharedFolder(): void { |
|
| 1707 | - self::loginAsUser($this->user); |
|
| 1708 | - |
|
| 1709 | - [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1710 | - $this->user . '/files/mount1', |
|
| 1711 | - $this->user . '/files/mount2', |
|
| 1712 | - ]); |
|
| 1713 | - |
|
| 1714 | - $mount1->expects($this->never()) |
|
| 1715 | - ->method('moveMount'); |
|
| 1716 | - |
|
| 1717 | - $mount2->expects($this->once()) |
|
| 1718 | - ->method('moveMount') |
|
| 1719 | - ->willReturn(true); |
|
| 1720 | - |
|
| 1721 | - $view = new View('/' . $this->user . '/files/'); |
|
| 1722 | - $view->mkdir('shareddir'); |
|
| 1723 | - $view->mkdir('shareddir/sub'); |
|
| 1724 | - $view->mkdir('shareddir/sub2'); |
|
| 1725 | - // Create a similar named but non-shared folder |
|
| 1726 | - $view->mkdir('shareddir notshared'); |
|
| 1727 | - |
|
| 1728 | - $fileId = $view->getFileInfo('shareddir')->getId(); |
|
| 1729 | - $userObject = Server::get(IUserManager::class)->createUser('test2', 'IHateNonMockableStaticClasses'); |
|
| 1730 | - |
|
| 1731 | - $userFolder = \OC::$server->getUserFolder($this->user); |
|
| 1732 | - $shareDir = $userFolder->get('shareddir'); |
|
| 1733 | - $shareManager = Server::get(IShareManager::class); |
|
| 1734 | - $share = $shareManager->newShare(); |
|
| 1735 | - $share->setSharedWith('test2') |
|
| 1736 | - ->setSharedBy($this->user) |
|
| 1737 | - ->setShareType(IShare::TYPE_USER) |
|
| 1738 | - ->setPermissions(Constants::PERMISSION_READ) |
|
| 1739 | - ->setNode($shareDir); |
|
| 1740 | - $shareManager->createShare($share); |
|
| 1741 | - |
|
| 1742 | - try { |
|
| 1743 | - $view->rename('mount1', 'shareddir'); |
|
| 1744 | - $this->fail('Cannot overwrite shared folder'); |
|
| 1745 | - } catch (ForbiddenException $e) { |
|
| 1746 | - |
|
| 1747 | - } |
|
| 1748 | - try { |
|
| 1749 | - $view->rename('mount1', 'shareddir/sub'); |
|
| 1750 | - $this->fail('Cannot move mount point into shared folder'); |
|
| 1751 | - } catch (ForbiddenException $e) { |
|
| 1752 | - |
|
| 1753 | - } |
|
| 1754 | - try { |
|
| 1755 | - $view->rename('mount1', 'shareddir/sub/sub2'); |
|
| 1756 | - $this->fail('Cannot move mount point into shared subfolder'); |
|
| 1757 | - } catch (ForbiddenException $e) { |
|
| 1758 | - |
|
| 1759 | - } |
|
| 1760 | - $this->assertTrue($view->rename('mount2', 'shareddir notshared/sub'), 'Can move mount point into a similarly named but non-shared folder'); |
|
| 1761 | - |
|
| 1762 | - $shareManager->deleteShare($share); |
|
| 1763 | - $userObject->delete(); |
|
| 1764 | - } |
|
| 1765 | - |
|
| 1766 | - public static function basicOperationProviderForLocks(): array { |
|
| 1767 | - return [ |
|
| 1768 | - // --- write hook ---- |
|
| 1769 | - [ |
|
| 1770 | - 'touch', |
|
| 1771 | - ['touch-create.txt'], |
|
| 1772 | - 'touch-create.txt', |
|
| 1773 | - 'create', |
|
| 1774 | - ILockingProvider::LOCK_SHARED, |
|
| 1775 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1776 | - ILockingProvider::LOCK_SHARED, |
|
| 1777 | - ], |
|
| 1778 | - [ |
|
| 1779 | - 'fopen', |
|
| 1780 | - ['test-write.txt', 'w'], |
|
| 1781 | - 'test-write.txt', |
|
| 1782 | - 'write', |
|
| 1783 | - ILockingProvider::LOCK_SHARED, |
|
| 1784 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1785 | - null, |
|
| 1786 | - // exclusive lock stays until fclose |
|
| 1787 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1788 | - ], |
|
| 1789 | - [ |
|
| 1790 | - 'mkdir', |
|
| 1791 | - ['newdir'], |
|
| 1792 | - 'newdir', |
|
| 1793 | - 'write', |
|
| 1794 | - ILockingProvider::LOCK_SHARED, |
|
| 1795 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1796 | - ILockingProvider::LOCK_SHARED, |
|
| 1797 | - ], |
|
| 1798 | - [ |
|
| 1799 | - 'file_put_contents', |
|
| 1800 | - ['file_put_contents.txt', 'blah'], |
|
| 1801 | - 'file_put_contents.txt', |
|
| 1802 | - 'write', |
|
| 1803 | - ILockingProvider::LOCK_SHARED, |
|
| 1804 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1805 | - ILockingProvider::LOCK_SHARED, |
|
| 1806 | - null, |
|
| 1807 | - 0, |
|
| 1808 | - ], |
|
| 1809 | - |
|
| 1810 | - // ---- delete hook ---- |
|
| 1811 | - [ |
|
| 1812 | - 'rmdir', |
|
| 1813 | - ['dir'], |
|
| 1814 | - 'dir', |
|
| 1815 | - 'delete', |
|
| 1816 | - ILockingProvider::LOCK_SHARED, |
|
| 1817 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1818 | - ILockingProvider::LOCK_SHARED, |
|
| 1819 | - ], |
|
| 1820 | - [ |
|
| 1821 | - 'unlink', |
|
| 1822 | - ['test.txt'], |
|
| 1823 | - 'test.txt', |
|
| 1824 | - 'delete', |
|
| 1825 | - ILockingProvider::LOCK_SHARED, |
|
| 1826 | - ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1827 | - ILockingProvider::LOCK_SHARED, |
|
| 1828 | - ], |
|
| 1829 | - |
|
| 1830 | - // ---- read hook (no post hooks) ---- |
|
| 1831 | - [ |
|
| 1832 | - 'file_get_contents', |
|
| 1833 | - ['test.txt'], |
|
| 1834 | - 'test.txt', |
|
| 1835 | - 'read', |
|
| 1836 | - ILockingProvider::LOCK_SHARED, |
|
| 1837 | - ILockingProvider::LOCK_SHARED, |
|
| 1838 | - null, |
|
| 1839 | - null, |
|
| 1840 | - false, |
|
| 1841 | - ], |
|
| 1842 | - [ |
|
| 1843 | - 'fopen', |
|
| 1844 | - ['test.txt', 'r'], |
|
| 1845 | - 'test.txt', |
|
| 1846 | - 'read', |
|
| 1847 | - ILockingProvider::LOCK_SHARED, |
|
| 1848 | - ILockingProvider::LOCK_SHARED, |
|
| 1849 | - null, |
|
| 1850 | - ], |
|
| 1851 | - [ |
|
| 1852 | - 'opendir', |
|
| 1853 | - ['dir'], |
|
| 1854 | - 'dir', |
|
| 1855 | - 'read', |
|
| 1856 | - ILockingProvider::LOCK_SHARED, |
|
| 1857 | - ILockingProvider::LOCK_SHARED, |
|
| 1858 | - null, |
|
| 1859 | - ], |
|
| 1860 | - |
|
| 1861 | - // ---- no lock, touch hook --- |
|
| 1862 | - ['touch', ['test.txt'], 'test.txt', 'touch', null, null, null], |
|
| 1863 | - |
|
| 1864 | - // ---- no hooks, no locks --- |
|
| 1865 | - ['is_dir', ['dir'], 'dir', null], |
|
| 1866 | - ['is_file', ['dir'], 'dir', null], |
|
| 1867 | - [ |
|
| 1868 | - 'stat', |
|
| 1869 | - ['dir'], |
|
| 1870 | - 'dir', |
|
| 1871 | - null, |
|
| 1872 | - ILockingProvider::LOCK_SHARED, |
|
| 1873 | - ILockingProvider::LOCK_SHARED, |
|
| 1874 | - ILockingProvider::LOCK_SHARED, |
|
| 1875 | - null, |
|
| 1876 | - false, |
|
| 1877 | - ], |
|
| 1878 | - [ |
|
| 1879 | - 'filetype', |
|
| 1880 | - ['dir'], |
|
| 1881 | - 'dir', |
|
| 1882 | - null, |
|
| 1883 | - ILockingProvider::LOCK_SHARED, |
|
| 1884 | - ILockingProvider::LOCK_SHARED, |
|
| 1885 | - ILockingProvider::LOCK_SHARED, |
|
| 1886 | - null, |
|
| 1887 | - false, |
|
| 1888 | - ], |
|
| 1889 | - [ |
|
| 1890 | - 'filesize', |
|
| 1891 | - ['dir'], |
|
| 1892 | - 'dir', |
|
| 1893 | - null, |
|
| 1894 | - ILockingProvider::LOCK_SHARED, |
|
| 1895 | - ILockingProvider::LOCK_SHARED, |
|
| 1896 | - ILockingProvider::LOCK_SHARED, |
|
| 1897 | - null, |
|
| 1898 | - /* Return an int */ |
|
| 1899 | - 100 |
|
| 1900 | - ], |
|
| 1901 | - ['isCreatable', ['dir'], 'dir', null], |
|
| 1902 | - ['isReadable', ['dir'], 'dir', null], |
|
| 1903 | - ['isUpdatable', ['dir'], 'dir', null], |
|
| 1904 | - ['isDeletable', ['dir'], 'dir', null], |
|
| 1905 | - ['isSharable', ['dir'], 'dir', null], |
|
| 1906 | - ['file_exists', ['dir'], 'dir', null], |
|
| 1907 | - [ |
|
| 1908 | - 'filemtime', |
|
| 1909 | - ['dir'], |
|
| 1910 | - 'dir', |
|
| 1911 | - null, |
|
| 1912 | - ILockingProvider::LOCK_SHARED, |
|
| 1913 | - ILockingProvider::LOCK_SHARED, |
|
| 1914 | - ILockingProvider::LOCK_SHARED, |
|
| 1915 | - null, |
|
| 1916 | - false, |
|
| 1917 | - ], |
|
| 1918 | - ]; |
|
| 1919 | - } |
|
| 1920 | - |
|
| 1921 | - /** |
|
| 1922 | - * Test whether locks are set before and after the operation |
|
| 1923 | - * |
|
| 1924 | - * |
|
| 1925 | - * @param string $operation operation name on the view |
|
| 1926 | - * @param array $operationArgs arguments for the operation |
|
| 1927 | - * @param string $lockedPath path of the locked item to check |
|
| 1928 | - * @param string $hookType hook type |
|
| 1929 | - * @param int $expectedLockBefore expected lock during pre hooks |
|
| 1930 | - * @param int $expectedLockDuring expected lock during operation |
|
| 1931 | - * @param int $expectedLockAfter expected lock during post hooks |
|
| 1932 | - * @param int $expectedStrayLock expected lock after returning, should |
|
| 1933 | - * be null (unlock) for most operations |
|
| 1934 | - */ |
|
| 1935 | - #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 1936 | - public function testLockBasicOperation( |
|
| 1937 | - $operation, |
|
| 1938 | - $operationArgs, |
|
| 1939 | - $lockedPath, |
|
| 1940 | - $hookType, |
|
| 1941 | - $expectedLockBefore = ILockingProvider::LOCK_SHARED, |
|
| 1942 | - $expectedLockDuring = ILockingProvider::LOCK_SHARED, |
|
| 1943 | - $expectedLockAfter = ILockingProvider::LOCK_SHARED, |
|
| 1944 | - $expectedStrayLock = null, |
|
| 1945 | - $returnValue = true, |
|
| 1946 | - ): void { |
|
| 1947 | - $view = new View('/' . $this->user . '/files/'); |
|
| 1948 | - |
|
| 1949 | - /** @var Temporary&MockObject $storage */ |
|
| 1950 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 1951 | - ->onlyMethods([$operation]) |
|
| 1952 | - ->getMock(); |
|
| 1953 | - |
|
| 1954 | - /* Pause trash to avoid the trashbin intercepting rmdir and unlink calls */ |
|
| 1955 | - Server::get(ITrashManager::class)->pauseTrash(); |
|
| 1956 | - /* Same thing with encryption wrapper */ |
|
| 1957 | - Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 1958 | - |
|
| 1959 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 1960 | - |
|
| 1961 | - // work directly on disk because mkdir might be mocked |
|
| 1962 | - $realPath = $storage->getSourcePath(''); |
|
| 1963 | - mkdir($realPath . '/files'); |
|
| 1964 | - mkdir($realPath . '/files/dir'); |
|
| 1965 | - file_put_contents($realPath . '/files/test.txt', 'blah'); |
|
| 1966 | - $storage->getScanner()->scan('files'); |
|
| 1967 | - |
|
| 1968 | - $storage->expects($this->once()) |
|
| 1969 | - ->method($operation) |
|
| 1970 | - ->willReturnCallback( |
|
| 1971 | - function () use ($view, $lockedPath, &$lockTypeDuring, $returnValue) { |
|
| 1972 | - $lockTypeDuring = $this->getFileLockType($view, $lockedPath); |
|
| 1973 | - |
|
| 1974 | - return $returnValue; |
|
| 1975 | - } |
|
| 1976 | - ); |
|
| 1977 | - |
|
| 1978 | - $this->assertNull($this->getFileLockType($view, $lockedPath), 'File not locked before operation'); |
|
| 1979 | - |
|
| 1980 | - $this->connectMockHooks($hookType, $view, $lockedPath, $lockTypePre, $lockTypePost); |
|
| 1981 | - |
|
| 1982 | - // do operation |
|
| 1983 | - call_user_func_array([$view, $operation], $operationArgs); |
|
| 1984 | - |
|
| 1985 | - if ($hookType !== null) { |
|
| 1986 | - $this->assertEquals($expectedLockBefore, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 1987 | - $this->assertEquals($expectedLockAfter, $lockTypePost, 'File locked properly during post-hook'); |
|
| 1988 | - $this->assertEquals($expectedLockDuring, $lockTypeDuring, 'File locked properly during operation'); |
|
| 1989 | - } else { |
|
| 1990 | - $this->assertNull($lockTypeDuring, 'File not locked during operation'); |
|
| 1991 | - } |
|
| 1992 | - |
|
| 1993 | - $this->assertEquals($expectedStrayLock, $this->getFileLockType($view, $lockedPath)); |
|
| 1994 | - |
|
| 1995 | - /* Resume trash to avoid side effects */ |
|
| 1996 | - Server::get(ITrashManager::class)->resumeTrash(); |
|
| 1997 | - } |
|
| 1998 | - |
|
| 1999 | - /** |
|
| 2000 | - * Test locks for file_put_content with stream. |
|
| 2001 | - * This code path uses $storage->fopen instead |
|
| 2002 | - */ |
|
| 2003 | - public function testLockFilePutContentWithStream(): void { |
|
| 2004 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2005 | - |
|
| 2006 | - $path = 'test_file_put_contents.txt'; |
|
| 2007 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2008 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2009 | - ->onlyMethods(['fopen']) |
|
| 2010 | - ->getMock(); |
|
| 2011 | - |
|
| 2012 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2013 | - $storage->mkdir('files'); |
|
| 2014 | - |
|
| 2015 | - $storage->expects($this->once()) |
|
| 2016 | - ->method('fopen') |
|
| 2017 | - ->willReturnCallback( |
|
| 2018 | - function () use ($view, $path, &$lockTypeDuring) { |
|
| 2019 | - $lockTypeDuring = $this->getFileLockType($view, $path); |
|
| 2020 | - |
|
| 2021 | - return fopen('php://temp', 'r+'); |
|
| 2022 | - } |
|
| 2023 | - ); |
|
| 2024 | - |
|
| 2025 | - $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost); |
|
| 2026 | - |
|
| 2027 | - $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation'); |
|
| 2028 | - |
|
| 2029 | - // do operation |
|
| 2030 | - $view->file_put_contents($path, fopen('php://temp', 'r+')); |
|
| 2031 | - |
|
| 2032 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 2033 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePost, 'File locked properly during post-hook'); |
|
| 2034 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation'); |
|
| 2035 | - |
|
| 2036 | - $this->assertNull($this->getFileLockType($view, $path)); |
|
| 2037 | - } |
|
| 2038 | - |
|
| 2039 | - /** |
|
| 2040 | - * Test locks for fopen with fclose at the end |
|
| 2041 | - */ |
|
| 2042 | - public function testLockFopen(): void { |
|
| 2043 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2044 | - |
|
| 2045 | - $path = 'test_file_put_contents.txt'; |
|
| 2046 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2047 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2048 | - ->onlyMethods(['fopen']) |
|
| 2049 | - ->getMock(); |
|
| 2050 | - |
|
| 2051 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2052 | - $storage->mkdir('files'); |
|
| 2053 | - |
|
| 2054 | - $storage->expects($this->once()) |
|
| 2055 | - ->method('fopen') |
|
| 2056 | - ->willReturnCallback( |
|
| 2057 | - function () use ($view, $path, &$lockTypeDuring) { |
|
| 2058 | - $lockTypeDuring = $this->getFileLockType($view, $path); |
|
| 2059 | - |
|
| 2060 | - return fopen('php://temp', 'r+'); |
|
| 2061 | - } |
|
| 2062 | - ); |
|
| 2063 | - |
|
| 2064 | - $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost); |
|
| 2065 | - |
|
| 2066 | - $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation'); |
|
| 2067 | - |
|
| 2068 | - // do operation |
|
| 2069 | - $res = $view->fopen($path, 'w'); |
|
| 2070 | - |
|
| 2071 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 2072 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation'); |
|
| 2073 | - $this->assertNull($lockTypePost, 'No post hook, no lock check possible'); |
|
| 2074 | - |
|
| 2075 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File still locked after fopen'); |
|
| 2076 | - |
|
| 2077 | - fclose($res); |
|
| 2078 | - |
|
| 2079 | - $this->assertNull($this->getFileLockType($view, $path), 'File unlocked after fclose'); |
|
| 2080 | - } |
|
| 2081 | - |
|
| 2082 | - /** |
|
| 2083 | - * Test locks for fopen with fclose at the end |
|
| 2084 | - * |
|
| 2085 | - * |
|
| 2086 | - * @param string $operation operation name on the view |
|
| 2087 | - * @param array $operationArgs arguments for the operation |
|
| 2088 | - * @param string $path path of the locked item to check |
|
| 2089 | - */ |
|
| 2090 | - #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 2091 | - public function testLockBasicOperationUnlocksAfterException( |
|
| 2092 | - $operation, |
|
| 2093 | - $operationArgs, |
|
| 2094 | - $path, |
|
| 2095 | - ): void { |
|
| 2096 | - if ($operation === 'touch') { |
|
| 2097 | - $this->markTestSkipped('touch handles storage exceptions internally'); |
|
| 2098 | - } |
|
| 2099 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2100 | - |
|
| 2101 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2102 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2103 | - ->onlyMethods([$operation]) |
|
| 2104 | - ->getMock(); |
|
| 2105 | - |
|
| 2106 | - /* Pause trash to avoid the trashbin intercepting rmdir and unlink calls */ |
|
| 2107 | - Server::get(ITrashManager::class)->pauseTrash(); |
|
| 2108 | - /* Same thing with encryption wrapper */ |
|
| 2109 | - Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2110 | - |
|
| 2111 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2112 | - |
|
| 2113 | - // work directly on disk because mkdir might be mocked |
|
| 2114 | - $realPath = $storage->getSourcePath(''); |
|
| 2115 | - mkdir($realPath . '/files'); |
|
| 2116 | - mkdir($realPath . '/files/dir'); |
|
| 2117 | - file_put_contents($realPath . '/files/test.txt', 'blah'); |
|
| 2118 | - $storage->getScanner()->scan('files'); |
|
| 2119 | - |
|
| 2120 | - $storage->expects($this->once()) |
|
| 2121 | - ->method($operation) |
|
| 2122 | - ->willReturnCallback( |
|
| 2123 | - function (): void { |
|
| 2124 | - throw new \Exception('Simulated exception'); |
|
| 2125 | - } |
|
| 2126 | - ); |
|
| 2127 | - |
|
| 2128 | - $thrown = false; |
|
| 2129 | - try { |
|
| 2130 | - call_user_func_array([$view, $operation], $operationArgs); |
|
| 2131 | - } catch (\Exception $e) { |
|
| 2132 | - $thrown = true; |
|
| 2133 | - $this->assertEquals('Simulated exception', $e->getMessage()); |
|
| 2134 | - } |
|
| 2135 | - $this->assertTrue($thrown, 'Exception was rethrown'); |
|
| 2136 | - $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception'); |
|
| 2137 | - |
|
| 2138 | - /* Resume trash to avoid side effects */ |
|
| 2139 | - Server::get(ITrashManager::class)->resumeTrash(); |
|
| 2140 | - } |
|
| 2141 | - |
|
| 2142 | - public function testLockBasicOperationUnlocksAfterLockException(): void { |
|
| 2143 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2144 | - |
|
| 2145 | - $storage = new Temporary([]); |
|
| 2146 | - |
|
| 2147 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2148 | - |
|
| 2149 | - $storage->mkdir('files'); |
|
| 2150 | - $storage->mkdir('files/dir'); |
|
| 2151 | - $storage->file_put_contents('files/test.txt', 'blah'); |
|
| 2152 | - $storage->getScanner()->scan('files'); |
|
| 2153 | - |
|
| 2154 | - // get a shared lock |
|
| 2155 | - $handle = $view->fopen('test.txt', 'r'); |
|
| 2156 | - |
|
| 2157 | - $thrown = false; |
|
| 2158 | - try { |
|
| 2159 | - // try (and fail) to get a write lock |
|
| 2160 | - $view->unlink('test.txt'); |
|
| 2161 | - } catch (\Exception $e) { |
|
| 2162 | - $thrown = true; |
|
| 2163 | - $this->assertInstanceOf(LockedException::class, $e); |
|
| 2164 | - } |
|
| 2165 | - $this->assertTrue($thrown, 'Exception was rethrown'); |
|
| 2166 | - |
|
| 2167 | - // clean shared lock |
|
| 2168 | - fclose($handle); |
|
| 2169 | - |
|
| 2170 | - $this->assertNull($this->getFileLockType($view, 'test.txt'), 'File got unlocked'); |
|
| 2171 | - } |
|
| 2172 | - |
|
| 2173 | - /** |
|
| 2174 | - * Test locks for fopen with fclose at the end |
|
| 2175 | - * |
|
| 2176 | - * |
|
| 2177 | - * @param string $operation operation name on the view |
|
| 2178 | - * @param array $operationArgs arguments for the operation |
|
| 2179 | - * @param string $path path of the locked item to check |
|
| 2180 | - * @param string $hookType hook type |
|
| 2181 | - */ |
|
| 2182 | - #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 2183 | - public function testLockBasicOperationUnlocksAfterCancelledHook( |
|
| 2184 | - $operation, |
|
| 2185 | - $operationArgs, |
|
| 2186 | - $path, |
|
| 2187 | - $hookType, |
|
| 2188 | - ): void { |
|
| 2189 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2190 | - |
|
| 2191 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2192 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2193 | - ->onlyMethods([$operation]) |
|
| 2194 | - ->getMock(); |
|
| 2195 | - |
|
| 2196 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2197 | - $storage->mkdir('files'); |
|
| 2198 | - |
|
| 2199 | - Util::connectHook( |
|
| 2200 | - Filesystem::CLASSNAME, |
|
| 2201 | - $hookType, |
|
| 2202 | - HookHelper::class, |
|
| 2203 | - 'cancellingCallback' |
|
| 2204 | - ); |
|
| 2205 | - |
|
| 2206 | - call_user_func_array([$view, $operation], $operationArgs); |
|
| 2207 | - |
|
| 2208 | - $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception'); |
|
| 2209 | - } |
|
| 2210 | - |
|
| 2211 | - public static function lockFileRenameOrCopyDataProvider(): array { |
|
| 2212 | - return [ |
|
| 2213 | - ['rename', ILockingProvider::LOCK_EXCLUSIVE], |
|
| 2214 | - ['copy', ILockingProvider::LOCK_SHARED], |
|
| 2215 | - ]; |
|
| 2216 | - } |
|
| 2217 | - |
|
| 2218 | - /** |
|
| 2219 | - * Test locks for rename or copy operation |
|
| 2220 | - * |
|
| 2221 | - * |
|
| 2222 | - * @param string $operation operation to be done on the view |
|
| 2223 | - * @param int $expectedLockTypeSourceDuring expected lock type on source file during |
|
| 2224 | - * the operation |
|
| 2225 | - */ |
|
| 2226 | - #[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyDataProvider')] |
|
| 2227 | - public function testLockFileRename($operation, $expectedLockTypeSourceDuring): void { |
|
| 2228 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2229 | - |
|
| 2230 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2231 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2232 | - ->onlyMethods([$operation, 'getMetaData', 'filemtime']) |
|
| 2233 | - ->getMock(); |
|
| 2234 | - |
|
| 2235 | - $storage->expects($this->any()) |
|
| 2236 | - ->method('getMetaData') |
|
| 2237 | - ->willReturn([ |
|
| 2238 | - 'mtime' => 1885434487, |
|
| 2239 | - 'etag' => '', |
|
| 2240 | - 'mimetype' => 'text/plain', |
|
| 2241 | - 'permissions' => Constants::PERMISSION_ALL, |
|
| 2242 | - 'size' => 3 |
|
| 2243 | - ]); |
|
| 2244 | - $storage->expects($this->any()) |
|
| 2245 | - ->method('filemtime') |
|
| 2246 | - ->willReturn(123456789); |
|
| 2247 | - |
|
| 2248 | - $sourcePath = 'original.txt'; |
|
| 2249 | - $targetPath = 'target.txt'; |
|
| 2250 | - |
|
| 2251 | - /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2252 | - Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2253 | - |
|
| 2254 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2255 | - $storage->mkdir('files'); |
|
| 2256 | - $view->file_put_contents($sourcePath, 'meh'); |
|
| 2257 | - |
|
| 2258 | - $storage->expects($this->once()) |
|
| 2259 | - ->method($operation) |
|
| 2260 | - ->willReturnCallback( |
|
| 2261 | - function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) { |
|
| 2262 | - $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath); |
|
| 2263 | - $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath); |
|
| 2264 | - |
|
| 2265 | - return true; |
|
| 2266 | - } |
|
| 2267 | - ); |
|
| 2268 | - |
|
| 2269 | - $this->connectMockHooks($operation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2270 | - $this->connectMockHooks($operation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2271 | - |
|
| 2272 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2273 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2274 | - |
|
| 2275 | - $view->$operation($sourcePath, $targetPath); |
|
| 2276 | - |
|
| 2277 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook'); |
|
| 2278 | - $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation'); |
|
| 2279 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook'); |
|
| 2280 | - |
|
| 2281 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook'); |
|
| 2282 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation'); |
|
| 2283 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook'); |
|
| 2284 | - |
|
| 2285 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2286 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2287 | - } |
|
| 2288 | - |
|
| 2289 | - /** |
|
| 2290 | - * simulate a failed copy operation. |
|
| 2291 | - * We expect that we catch the exception, free the lock and re-throw it. |
|
| 2292 | - * |
|
| 2293 | - */ |
|
| 2294 | - public function testLockFileCopyException(): void { |
|
| 2295 | - $this->expectException(\Exception::class); |
|
| 2296 | - |
|
| 2297 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2298 | - |
|
| 2299 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2300 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2301 | - ->onlyMethods(['copy']) |
|
| 2302 | - ->getMock(); |
|
| 2303 | - |
|
| 2304 | - $sourcePath = 'original.txt'; |
|
| 2305 | - $targetPath = 'target.txt'; |
|
| 2306 | - |
|
| 2307 | - /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2308 | - Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2309 | - |
|
| 2310 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2311 | - $storage->mkdir('files'); |
|
| 2312 | - $view->file_put_contents($sourcePath, 'meh'); |
|
| 2313 | - |
|
| 2314 | - $storage->expects($this->once()) |
|
| 2315 | - ->method('copy') |
|
| 2316 | - ->willReturnCallback( |
|
| 2317 | - function (): void { |
|
| 2318 | - throw new \Exception(); |
|
| 2319 | - } |
|
| 2320 | - ); |
|
| 2321 | - |
|
| 2322 | - $this->connectMockHooks('copy', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2323 | - $this->connectMockHooks('copy', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2324 | - |
|
| 2325 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2326 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2327 | - |
|
| 2328 | - try { |
|
| 2329 | - $view->copy($sourcePath, $targetPath); |
|
| 2330 | - } catch (\Exception $e) { |
|
| 2331 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2332 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2333 | - throw $e; |
|
| 2334 | - } |
|
| 2335 | - } |
|
| 2336 | - |
|
| 2337 | - /** |
|
| 2338 | - * Test rename operation: unlock first path when second path was locked |
|
| 2339 | - */ |
|
| 2340 | - public function testLockFileRenameUnlockOnException(): void { |
|
| 2341 | - self::loginAsUser('test'); |
|
| 2342 | - |
|
| 2343 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2344 | - |
|
| 2345 | - $sourcePath = 'original.txt'; |
|
| 2346 | - $targetPath = 'target.txt'; |
|
| 2347 | - $view->file_put_contents($sourcePath, 'meh'); |
|
| 2348 | - |
|
| 2349 | - // simulate that the target path is already locked |
|
| 2350 | - $view->lockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 2351 | - |
|
| 2352 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2353 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file is locked before operation'); |
|
| 2354 | - |
|
| 2355 | - $thrown = false; |
|
| 2356 | - try { |
|
| 2357 | - $view->rename($sourcePath, $targetPath); |
|
| 2358 | - } catch (LockedException $e) { |
|
| 2359 | - $thrown = true; |
|
| 2360 | - } |
|
| 2361 | - |
|
| 2362 | - $this->assertTrue($thrown, 'LockedException thrown'); |
|
| 2363 | - |
|
| 2364 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2365 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file still locked after operation'); |
|
| 2366 | - |
|
| 2367 | - $view->unlockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 2368 | - } |
|
| 2369 | - |
|
| 2370 | - /** |
|
| 2371 | - * Test rename operation: unlock first path when second path was locked |
|
| 2372 | - */ |
|
| 2373 | - public function testGetOwner(): void { |
|
| 2374 | - self::loginAsUser('test'); |
|
| 2375 | - |
|
| 2376 | - $view = new View('/test/files/'); |
|
| 2377 | - |
|
| 2378 | - $path = 'foo.txt'; |
|
| 2379 | - $view->file_put_contents($path, 'meh'); |
|
| 2380 | - |
|
| 2381 | - $this->assertEquals('test', $view->getFileInfo($path)->getOwner()->getUID()); |
|
| 2382 | - |
|
| 2383 | - $folderInfo = $view->getDirectoryContent(''); |
|
| 2384 | - $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) { |
|
| 2385 | - return $info->getName() === 'foo.txt'; |
|
| 2386 | - })); |
|
| 2387 | - |
|
| 2388 | - $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID()); |
|
| 2389 | - |
|
| 2390 | - $subStorage = new Temporary(); |
|
| 2391 | - Filesystem::mount($subStorage, [], '/test/files/asd'); |
|
| 2392 | - |
|
| 2393 | - $folderInfo = $view->getDirectoryContent(''); |
|
| 2394 | - $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) { |
|
| 2395 | - return $info->getName() === 'asd'; |
|
| 2396 | - })); |
|
| 2397 | - |
|
| 2398 | - $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID()); |
|
| 2399 | - } |
|
| 2400 | - |
|
| 2401 | - public static function lockFileRenameOrCopyCrossStorageDataProvider(): array { |
|
| 2402 | - return [ |
|
| 2403 | - ['rename', 'moveFromStorage', ILockingProvider::LOCK_EXCLUSIVE], |
|
| 2404 | - ['copy', 'copyFromStorage', ILockingProvider::LOCK_SHARED], |
|
| 2405 | - ]; |
|
| 2406 | - } |
|
| 2407 | - |
|
| 2408 | - /** |
|
| 2409 | - * Test locks for rename or copy operation cross-storage |
|
| 2410 | - * |
|
| 2411 | - * |
|
| 2412 | - * @param string $viewOperation operation to be done on the view |
|
| 2413 | - * @param string $storageOperation operation to be mocked on the storage |
|
| 2414 | - * @param int $expectedLockTypeSourceDuring expected lock type on source file during |
|
| 2415 | - * the operation |
|
| 2416 | - */ |
|
| 2417 | - #[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyCrossStorageDataProvider')] |
|
| 2418 | - public function testLockFileRenameCrossStorage($viewOperation, $storageOperation, $expectedLockTypeSourceDuring): void { |
|
| 2419 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2420 | - |
|
| 2421 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2422 | - $storage = $this->getMockBuilder(Temporary::class) |
|
| 2423 | - ->onlyMethods([$storageOperation]) |
|
| 2424 | - ->getMock(); |
|
| 2425 | - /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage2 */ |
|
| 2426 | - $storage2 = $this->getMockBuilder(Temporary::class) |
|
| 2427 | - ->onlyMethods([$storageOperation, 'getMetaData', 'filemtime']) |
|
| 2428 | - ->getMock(); |
|
| 2429 | - |
|
| 2430 | - $storage2->expects($this->any()) |
|
| 2431 | - ->method('getMetaData') |
|
| 2432 | - ->willReturn([ |
|
| 2433 | - 'mtime' => 1885434487, |
|
| 2434 | - 'etag' => '', |
|
| 2435 | - 'mimetype' => 'text/plain', |
|
| 2436 | - 'permissions' => Constants::PERMISSION_ALL, |
|
| 2437 | - 'size' => 3 |
|
| 2438 | - ]); |
|
| 2439 | - $storage2->expects($this->any()) |
|
| 2440 | - ->method('filemtime') |
|
| 2441 | - ->willReturn(123456789); |
|
| 2442 | - |
|
| 2443 | - $sourcePath = 'original.txt'; |
|
| 2444 | - $targetPath = 'substorage/target.txt'; |
|
| 2445 | - |
|
| 2446 | - /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2447 | - Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2448 | - |
|
| 2449 | - Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2450 | - Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
| 2451 | - $storage->mkdir('files'); |
|
| 2452 | - $view->file_put_contents($sourcePath, 'meh'); |
|
| 2453 | - $storage2->getUpdater()->update(''); |
|
| 2454 | - |
|
| 2455 | - $storage->expects($this->never()) |
|
| 2456 | - ->method($storageOperation); |
|
| 2457 | - $storage2->expects($this->once()) |
|
| 2458 | - ->method($storageOperation) |
|
| 2459 | - ->willReturnCallback( |
|
| 2460 | - function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) { |
|
| 2461 | - $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath); |
|
| 2462 | - $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath); |
|
| 2463 | - |
|
| 2464 | - return true; |
|
| 2465 | - } |
|
| 2466 | - ); |
|
| 2467 | - |
|
| 2468 | - $this->connectMockHooks($viewOperation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2469 | - $this->connectMockHooks($viewOperation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2470 | - |
|
| 2471 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2472 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2473 | - |
|
| 2474 | - $view->$viewOperation($sourcePath, $targetPath); |
|
| 2475 | - |
|
| 2476 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook'); |
|
| 2477 | - $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation'); |
|
| 2478 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook'); |
|
| 2479 | - |
|
| 2480 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook'); |
|
| 2481 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation'); |
|
| 2482 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook'); |
|
| 2483 | - |
|
| 2484 | - $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2485 | - $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2486 | - } |
|
| 2487 | - |
|
| 2488 | - /** |
|
| 2489 | - * Test locks when moving a mount point |
|
| 2490 | - */ |
|
| 2491 | - public function testLockMoveMountPoint(): void { |
|
| 2492 | - self::loginAsUser('test'); |
|
| 2493 | - |
|
| 2494 | - [$mount] = $this->createTestMovableMountPoints([ |
|
| 2495 | - $this->user . '/files/substorage', |
|
| 2496 | - ]); |
|
| 2497 | - |
|
| 2498 | - $view = new View('/' . $this->user . '/files/'); |
|
| 2499 | - $view->mkdir('subdir'); |
|
| 2500 | - |
|
| 2501 | - $sourcePath = 'substorage'; |
|
| 2502 | - $targetPath = 'subdir/substorage_moved'; |
|
| 2503 | - |
|
| 2504 | - $mount->expects($this->once()) |
|
| 2505 | - ->method('moveMount') |
|
| 2506 | - ->willReturnCallback( |
|
| 2507 | - function ($target) use ($mount, $view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring, &$lockTypeSharedRootDuring) { |
|
| 2508 | - $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath, true); |
|
| 2509 | - $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath, true); |
|
| 2510 | - |
|
| 2511 | - $lockTypeSharedRootDuring = $this->getFileLockType($view, $sourcePath, false); |
|
| 2512 | - |
|
| 2513 | - $mount->setMountPoint($target); |
|
| 2514 | - |
|
| 2515 | - return true; |
|
| 2516 | - } |
|
| 2517 | - ); |
|
| 2518 | - |
|
| 2519 | - $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost, true); |
|
| 2520 | - $this->connectMockHooks('rename', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost, true); |
|
| 2521 | - // in pre-hook, mount point is still on $sourcePath |
|
| 2522 | - $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSharedRootPre, $dummy, false); |
|
| 2523 | - // in post-hook, mount point is now on $targetPath |
|
| 2524 | - $this->connectMockHooks('rename', $view, $targetPath, $dummy, $lockTypeSharedRootPost, false); |
|
| 2525 | - |
|
| 2526 | - $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked before operation'); |
|
| 2527 | - $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked before operation'); |
|
| 2528 | - $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked before operation'); |
|
| 2529 | - |
|
| 2530 | - $view->rename($sourcePath, $targetPath); |
|
| 2531 | - |
|
| 2532 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source path locked properly during pre-hook'); |
|
| 2533 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeSourceDuring, 'Source path locked properly during operation'); |
|
| 2534 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source path locked properly during post-hook'); |
|
| 2535 | - |
|
| 2536 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target path locked properly during pre-hook'); |
|
| 2537 | - $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target path locked properly during operation'); |
|
| 2538 | - $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target path locked properly during post-hook'); |
|
| 2539 | - |
|
| 2540 | - $this->assertNull($lockTypeSharedRootPre, 'Shared storage root not locked during pre-hook'); |
|
| 2541 | - $this->assertNull($lockTypeSharedRootDuring, 'Shared storage root not locked during move'); |
|
| 2542 | - $this->assertNull($lockTypeSharedRootPost, 'Shared storage root not locked during post-hook'); |
|
| 2543 | - |
|
| 2544 | - $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked after operation'); |
|
| 2545 | - $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked after operation'); |
|
| 2546 | - $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked after operation'); |
|
| 2547 | - } |
|
| 2548 | - |
|
| 2549 | - /** |
|
| 2550 | - * Connect hook callbacks for hook type |
|
| 2551 | - * |
|
| 2552 | - * @param string $hookType hook type or null for none |
|
| 2553 | - * @param View $view view to check the lock on |
|
| 2554 | - * @param string $path path for which to check the lock |
|
| 2555 | - * @param int $lockTypePre variable to receive lock type that was active in the pre-hook |
|
| 2556 | - * @param int $lockTypePost variable to receive lock type that was active in the post-hook |
|
| 2557 | - * @param bool $onMountPoint true to check the mount point instead of the |
|
| 2558 | - * mounted storage |
|
| 2559 | - */ |
|
| 2560 | - private function connectMockHooks($hookType, $view, $path, &$lockTypePre, &$lockTypePost, $onMountPoint = false) { |
|
| 2561 | - if ($hookType === null) { |
|
| 2562 | - return; |
|
| 2563 | - } |
|
| 2564 | - |
|
| 2565 | - $eventHandler = $this->getMockBuilder(TestEventHandler::class) |
|
| 2566 | - ->onlyMethods(['preCallback', 'postCallback']) |
|
| 2567 | - ->getMock(); |
|
| 2568 | - |
|
| 2569 | - $eventHandler->expects($this->any()) |
|
| 2570 | - ->method('preCallback') |
|
| 2571 | - ->willReturnCallback( |
|
| 2572 | - function () use ($view, $path, $onMountPoint, &$lockTypePre): void { |
|
| 2573 | - $lockTypePre = $this->getFileLockType($view, $path, $onMountPoint); |
|
| 2574 | - } |
|
| 2575 | - ); |
|
| 2576 | - $eventHandler->expects($this->any()) |
|
| 2577 | - ->method('postCallback') |
|
| 2578 | - ->willReturnCallback( |
|
| 2579 | - function () use ($view, $path, $onMountPoint, &$lockTypePost): void { |
|
| 2580 | - $lockTypePost = $this->getFileLockType($view, $path, $onMountPoint); |
|
| 2581 | - } |
|
| 2582 | - ); |
|
| 2583 | - |
|
| 2584 | - if ($hookType !== null) { |
|
| 2585 | - Util::connectHook( |
|
| 2586 | - Filesystem::CLASSNAME, |
|
| 2587 | - $hookType, |
|
| 2588 | - $eventHandler, |
|
| 2589 | - 'preCallback' |
|
| 2590 | - ); |
|
| 2591 | - Util::connectHook( |
|
| 2592 | - Filesystem::CLASSNAME, |
|
| 2593 | - 'post_' . $hookType, |
|
| 2594 | - $eventHandler, |
|
| 2595 | - 'postCallback' |
|
| 2596 | - ); |
|
| 2597 | - } |
|
| 2598 | - } |
|
| 2599 | - |
|
| 2600 | - /** |
|
| 2601 | - * Returns the file lock type |
|
| 2602 | - * |
|
| 2603 | - * @param View $view view |
|
| 2604 | - * @param string $path path |
|
| 2605 | - * @param bool $onMountPoint true to check the mount point instead of the |
|
| 2606 | - * mounted storage |
|
| 2607 | - * |
|
| 2608 | - * @return int lock type or null if file was not locked |
|
| 2609 | - */ |
|
| 2610 | - private function getFileLockType(View $view, $path, $onMountPoint = false) { |
|
| 2611 | - if ($this->isFileLocked($view, $path, ILockingProvider::LOCK_EXCLUSIVE, $onMountPoint)) { |
|
| 2612 | - return ILockingProvider::LOCK_EXCLUSIVE; |
|
| 2613 | - } elseif ($this->isFileLocked($view, $path, ILockingProvider::LOCK_SHARED, $onMountPoint)) { |
|
| 2614 | - return ILockingProvider::LOCK_SHARED; |
|
| 2615 | - } |
|
| 2616 | - return null; |
|
| 2617 | - } |
|
| 2618 | - |
|
| 2619 | - |
|
| 2620 | - public function testRemoveMoveableMountPoint(): void { |
|
| 2621 | - $mountPoint = '/' . $this->user . '/files/mount/'; |
|
| 2622 | - |
|
| 2623 | - // Mock the mount point |
|
| 2624 | - /** @var TestMoveableMountPoint|\PHPUnit\Framework\MockObject\MockObject $mount */ |
|
| 2625 | - $mount = $this->createMock(TestMoveableMountPoint::class); |
|
| 2626 | - $mount->expects($this->once()) |
|
| 2627 | - ->method('getMountPoint') |
|
| 2628 | - ->willReturn($mountPoint); |
|
| 2629 | - $mount->expects($this->once()) |
|
| 2630 | - ->method('removeMount') |
|
| 2631 | - ->willReturn('foo'); |
|
| 2632 | - $mount->expects($this->any()) |
|
| 2633 | - ->method('getInternalPath') |
|
| 2634 | - ->willReturn(''); |
|
| 2635 | - |
|
| 2636 | - // Register mount |
|
| 2637 | - Filesystem::getMountManager()->addMount($mount); |
|
| 2638 | - |
|
| 2639 | - // Listen for events |
|
| 2640 | - $eventHandler = $this->getMockBuilder(TestEventHandler::class) |
|
| 2641 | - ->onlyMethods(['umount', 'post_umount']) |
|
| 2642 | - ->getMock(); |
|
| 2643 | - $eventHandler->expects($this->once()) |
|
| 2644 | - ->method('umount') |
|
| 2645 | - ->with([Filesystem::signal_param_path => '/mount']); |
|
| 2646 | - $eventHandler->expects($this->once()) |
|
| 2647 | - ->method('post_umount') |
|
| 2648 | - ->with([Filesystem::signal_param_path => '/mount']); |
|
| 2649 | - Util::connectHook( |
|
| 2650 | - Filesystem::CLASSNAME, |
|
| 2651 | - 'umount', |
|
| 2652 | - $eventHandler, |
|
| 2653 | - 'umount' |
|
| 2654 | - ); |
|
| 2655 | - Util::connectHook( |
|
| 2656 | - Filesystem::CLASSNAME, |
|
| 2657 | - 'post_umount', |
|
| 2658 | - $eventHandler, |
|
| 2659 | - 'post_umount' |
|
| 2660 | - ); |
|
| 2661 | - |
|
| 2662 | - //Delete the mountpoint |
|
| 2663 | - $view = new View('/' . $this->user . '/files'); |
|
| 2664 | - $this->assertEquals('foo', $view->rmdir('mount')); |
|
| 2665 | - } |
|
| 2666 | - |
|
| 2667 | - public static function mimeFilterProvider(): array { |
|
| 2668 | - return [ |
|
| 2669 | - [null, ['test1.txt', 'test2.txt', 'test3.md', 'test4.png']], |
|
| 2670 | - ['text/plain', ['test1.txt', 'test2.txt']], |
|
| 2671 | - ['text/markdown', ['test3.md']], |
|
| 2672 | - ['text', ['test1.txt', 'test2.txt', 'test3.md']], |
|
| 2673 | - ]; |
|
| 2674 | - } |
|
| 2675 | - |
|
| 2676 | - /** |
|
| 2677 | - * @param string $filter |
|
| 2678 | - * @param string[] $expected |
|
| 2679 | - */ |
|
| 2680 | - #[\PHPUnit\Framework\Attributes\DataProvider('mimeFilterProvider')] |
|
| 2681 | - public function testGetDirectoryContentMimeFilter($filter, $expected): void { |
|
| 2682 | - $storage1 = new Temporary(); |
|
| 2683 | - $root = self::getUniqueID('/'); |
|
| 2684 | - Filesystem::mount($storage1, [], $root . '/'); |
|
| 2685 | - $view = new View($root); |
|
| 2686 | - |
|
| 2687 | - $view->file_put_contents('test1.txt', 'asd'); |
|
| 2688 | - $view->file_put_contents('test2.txt', 'asd'); |
|
| 2689 | - $view->file_put_contents('test3.md', 'asd'); |
|
| 2690 | - $view->file_put_contents('test4.png', ''); |
|
| 2691 | - |
|
| 2692 | - $content = $view->getDirectoryContent('', $filter); |
|
| 2693 | - |
|
| 2694 | - $files = array_map(function (FileInfo $info) { |
|
| 2695 | - return $info->getName(); |
|
| 2696 | - }, $content); |
|
| 2697 | - sort($files); |
|
| 2698 | - |
|
| 2699 | - $this->assertEquals($expected, $files); |
|
| 2700 | - } |
|
| 2701 | - |
|
| 2702 | - public function testFilePutContentsClearsChecksum(): void { |
|
| 2703 | - $storage = new Temporary([]); |
|
| 2704 | - $scanner = $storage->getScanner(); |
|
| 2705 | - $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2706 | - Filesystem::mount($storage, [], '/test/'); |
|
| 2707 | - $scanner->scan(''); |
|
| 2708 | - |
|
| 2709 | - $view = new View('/test/foo.txt'); |
|
| 2710 | - $view->putFileInfo('.', ['checksum' => '42']); |
|
| 2711 | - |
|
| 2712 | - $this->assertEquals('bar', $view->file_get_contents('')); |
|
| 2713 | - $fh = tmpfile(); |
|
| 2714 | - fwrite($fh, 'fooo'); |
|
| 2715 | - rewind($fh); |
|
| 2716 | - clearstatcache(); |
|
| 2717 | - $view->file_put_contents('', $fh); |
|
| 2718 | - $this->assertEquals('fooo', $view->file_get_contents('')); |
|
| 2719 | - $data = $view->getFileInfo('.'); |
|
| 2720 | - $this->assertEquals('', $data->getChecksum()); |
|
| 2721 | - } |
|
| 2722 | - |
|
| 2723 | - public function testDeleteGhostFile(): void { |
|
| 2724 | - $storage = new Temporary([]); |
|
| 2725 | - $scanner = $storage->getScanner(); |
|
| 2726 | - $cache = $storage->getCache(); |
|
| 2727 | - $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2728 | - Filesystem::mount($storage, [], '/test/'); |
|
| 2729 | - $scanner->scan(''); |
|
| 2730 | - |
|
| 2731 | - $storage->unlink('foo.txt'); |
|
| 2732 | - |
|
| 2733 | - $this->assertTrue($cache->inCache('foo.txt')); |
|
| 2734 | - |
|
| 2735 | - $view = new View('/test'); |
|
| 2736 | - $rootInfo = $view->getFileInfo(''); |
|
| 2737 | - $this->assertEquals(3, $rootInfo->getSize()); |
|
| 2738 | - $view->unlink('foo.txt'); |
|
| 2739 | - $newInfo = $view->getFileInfo(''); |
|
| 2740 | - |
|
| 2741 | - $this->assertFalse($cache->inCache('foo.txt')); |
|
| 2742 | - $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag()); |
|
| 2743 | - $this->assertEquals(0, $newInfo->getSize()); |
|
| 2744 | - } |
|
| 2745 | - |
|
| 2746 | - public function testDeleteGhostFolder(): void { |
|
| 2747 | - $storage = new Temporary([]); |
|
| 2748 | - $scanner = $storage->getScanner(); |
|
| 2749 | - $cache = $storage->getCache(); |
|
| 2750 | - $storage->mkdir('foo'); |
|
| 2751 | - $storage->file_put_contents('foo/foo.txt', 'bar'); |
|
| 2752 | - Filesystem::mount($storage, [], '/test/'); |
|
| 2753 | - $scanner->scan(''); |
|
| 2754 | - |
|
| 2755 | - $storage->rmdir('foo'); |
|
| 2756 | - |
|
| 2757 | - $this->assertTrue($cache->inCache('foo')); |
|
| 2758 | - $this->assertTrue($cache->inCache('foo/foo.txt')); |
|
| 2759 | - |
|
| 2760 | - $view = new View('/test'); |
|
| 2761 | - $rootInfo = $view->getFileInfo(''); |
|
| 2762 | - $this->assertEquals(3, $rootInfo->getSize()); |
|
| 2763 | - $view->rmdir('foo'); |
|
| 2764 | - $newInfo = $view->getFileInfo(''); |
|
| 2765 | - |
|
| 2766 | - $this->assertFalse($cache->inCache('foo')); |
|
| 2767 | - $this->assertFalse($cache->inCache('foo/foo.txt')); |
|
| 2768 | - $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag()); |
|
| 2769 | - $this->assertEquals(0, $newInfo->getSize()); |
|
| 2770 | - } |
|
| 2771 | - |
|
| 2772 | - public function testCreateParentDirectories(): void { |
|
| 2773 | - $view = $this->getMockBuilder(View::class) |
|
| 2774 | - ->disableOriginalConstructor() |
|
| 2775 | - ->onlyMethods([ |
|
| 2776 | - 'is_file', |
|
| 2777 | - 'file_exists', |
|
| 2778 | - 'mkdir', |
|
| 2779 | - ]) |
|
| 2780 | - ->getMock(); |
|
| 2781 | - |
|
| 2782 | - $view->expects($this->exactly(3)) |
|
| 2783 | - ->method('is_file') |
|
| 2784 | - ->willReturnMap([ |
|
| 2785 | - ['/new', false], |
|
| 2786 | - ['/new/folder', false], |
|
| 2787 | - ['/new/folder/structure', false], |
|
| 2788 | - ]); |
|
| 2789 | - $view->expects($this->exactly(3)) |
|
| 2790 | - ->method('file_exists') |
|
| 2791 | - ->willReturnMap([ |
|
| 2792 | - ['/new', true], |
|
| 2793 | - ['/new/folder', false], |
|
| 2794 | - ['/new/folder/structure', false], |
|
| 2795 | - ]); |
|
| 2796 | - |
|
| 2797 | - $calls = ['/new/folder', '/new/folder/structure']; |
|
| 2798 | - $view->expects($this->exactly(2)) |
|
| 2799 | - ->method('mkdir') |
|
| 2800 | - ->willReturnCallback(function ($dir) use (&$calls): void { |
|
| 2801 | - $expected = array_shift($calls); |
|
| 2802 | - $this->assertEquals($expected, $dir); |
|
| 2803 | - }); |
|
| 2804 | - |
|
| 2805 | - $this->assertTrue(self::invokePrivate($view, 'createParentDirectories', ['/new/folder/structure'])); |
|
| 2806 | - } |
|
| 2807 | - |
|
| 2808 | - public function testCreateParentDirectoriesWithExistingFile(): void { |
|
| 2809 | - $view = $this->getMockBuilder(View::class) |
|
| 2810 | - ->disableOriginalConstructor() |
|
| 2811 | - ->onlyMethods([ |
|
| 2812 | - 'is_file', |
|
| 2813 | - 'file_exists', |
|
| 2814 | - 'mkdir', |
|
| 2815 | - ]) |
|
| 2816 | - ->getMock(); |
|
| 2817 | - |
|
| 2818 | - $view |
|
| 2819 | - ->expects($this->once()) |
|
| 2820 | - ->method('is_file') |
|
| 2821 | - ->with('/file.txt') |
|
| 2822 | - ->willReturn(true); |
|
| 2823 | - $this->assertFalse(self::invokePrivate($view, 'createParentDirectories', ['/file.txt/folder/structure'])); |
|
| 2824 | - } |
|
| 2825 | - |
|
| 2826 | - public function testCacheExtension(): void { |
|
| 2827 | - $storage = new Temporary([]); |
|
| 2828 | - $scanner = $storage->getScanner(); |
|
| 2829 | - $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2830 | - $scanner->scan(''); |
|
| 2831 | - |
|
| 2832 | - Filesystem::mount($storage, [], '/test/'); |
|
| 2833 | - $view = new View('/test'); |
|
| 2834 | - |
|
| 2835 | - $info = $view->getFileInfo('/foo.txt'); |
|
| 2836 | - $this->assertEquals(0, $info->getUploadTime()); |
|
| 2837 | - $this->assertEquals(0, $info->getCreationTime()); |
|
| 2838 | - |
|
| 2839 | - $view->putFileInfo('/foo.txt', ['upload_time' => 25]); |
|
| 2840 | - |
|
| 2841 | - $info = $view->getFileInfo('/foo.txt'); |
|
| 2842 | - $this->assertEquals(25, $info->getUploadTime()); |
|
| 2843 | - $this->assertEquals(0, $info->getCreationTime()); |
|
| 2844 | - } |
|
| 2845 | - |
|
| 2846 | - public function testFopenGone(): void { |
|
| 2847 | - $storage = new Temporary([]); |
|
| 2848 | - $scanner = $storage->getScanner(); |
|
| 2849 | - $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2850 | - $scanner->scan(''); |
|
| 2851 | - $cache = $storage->getCache(); |
|
| 2852 | - |
|
| 2853 | - Filesystem::mount($storage, [], '/test/'); |
|
| 2854 | - $view = new View('/test'); |
|
| 2855 | - |
|
| 2856 | - $storage->unlink('foo.txt'); |
|
| 2857 | - |
|
| 2858 | - $this->assertTrue($cache->inCache('foo.txt')); |
|
| 2859 | - |
|
| 2860 | - $this->assertFalse($view->fopen('foo.txt', 'r')); |
|
| 2861 | - |
|
| 2862 | - $this->assertFalse($cache->inCache('foo.txt')); |
|
| 2863 | - } |
|
| 2864 | - |
|
| 2865 | - public function testMountpointParentsCreated(): void { |
|
| 2866 | - $storage1 = $this->getTestStorage(); |
|
| 2867 | - Filesystem::mount($storage1, [], '/'); |
|
| 2868 | - |
|
| 2869 | - $storage2 = $this->getTestStorage(); |
|
| 2870 | - Filesystem::mount($storage2, [], '/A/B/C'); |
|
| 2871 | - |
|
| 2872 | - $rootView = new View(''); |
|
| 2873 | - |
|
| 2874 | - $folderData = $rootView->getDirectoryContent('/'); |
|
| 2875 | - $this->assertCount(4, $folderData); |
|
| 2876 | - $this->assertEquals('folder', $folderData[0]['name']); |
|
| 2877 | - $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 2878 | - $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 2879 | - $this->assertEquals('A', $folderData[3]['name']); |
|
| 2880 | - |
|
| 2881 | - $folderData = $rootView->getDirectoryContent('/A'); |
|
| 2882 | - $this->assertCount(1, $folderData); |
|
| 2883 | - $this->assertEquals('B', $folderData[0]['name']); |
|
| 2884 | - |
|
| 2885 | - $folderData = $rootView->getDirectoryContent('/A/B'); |
|
| 2886 | - $this->assertCount(1, $folderData); |
|
| 2887 | - $this->assertEquals('C', $folderData[0]['name']); |
|
| 2888 | - |
|
| 2889 | - $folderData = $rootView->getDirectoryContent('/A/B/C'); |
|
| 2890 | - $this->assertCount(3, $folderData); |
|
| 2891 | - $this->assertEquals('folder', $folderData[0]['name']); |
|
| 2892 | - $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 2893 | - $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 2894 | - } |
|
| 2895 | - |
|
| 2896 | - public function testCopyPreservesContent() { |
|
| 2897 | - $viewUser1 = new View('/' . 'userId' . '/files'); |
|
| 2898 | - $viewUser1->mkdir(''); |
|
| 2899 | - $viewUser1->file_put_contents('foo.txt', 'foo'); |
|
| 2900 | - $viewUser1->copy('foo.txt', 'bar.txt'); |
|
| 2901 | - $this->assertEquals('foo', $viewUser1->file_get_contents('bar.txt')); |
|
| 2902 | - } |
|
| 860 | + $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789'; |
|
| 861 | + $tmpdirLength = strlen(Server::get(ITempManager::class)->getTemporaryFolder()); |
|
| 862 | + if (\OC_Util::runningOnMac()) { |
|
| 863 | + $depth = ((1024 - $tmpdirLength) / 57); |
|
| 864 | + } else { |
|
| 865 | + $depth = ((4000 - $tmpdirLength) / 57); |
|
| 866 | + } |
|
| 867 | + foreach (range(0, $depth - 1) as $i) { |
|
| 868 | + $longPath .= $ds . $folderName; |
|
| 869 | + $result = $rootView->mkdir($longPath); |
|
| 870 | + $this->assertTrue($result, "mkdir failed on $i - path length: " . strlen($longPath)); |
|
| 871 | + |
|
| 872 | + $result = $rootView->file_put_contents($longPath . "{$ds}test.txt", 'lorem'); |
|
| 873 | + $this->assertEquals(5, $result, "file_put_contents failed on $i"); |
|
| 874 | + |
|
| 875 | + $this->assertTrue($rootView->file_exists($longPath)); |
|
| 876 | + $this->assertTrue($rootView->file_exists($longPath . "{$ds}test.txt")); |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + $cache = $storage->getCache(); |
|
| 880 | + $scanner = $storage->getScanner(); |
|
| 881 | + $scanner->scan(''); |
|
| 882 | + |
|
| 883 | + $longPath = $folderName; |
|
| 884 | + foreach (range(0, $depth - 1) as $i) { |
|
| 885 | + $cachedFolder = $cache->get($longPath); |
|
| 886 | + $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at $i"); |
|
| 887 | + $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at $i"); |
|
| 888 | + |
|
| 889 | + $cachedFile = $cache->get($longPath . '/test.txt'); |
|
| 890 | + $this->assertTrue(is_array($cachedFile), "No cache entry for file at $i"); |
|
| 891 | + $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at $i"); |
|
| 892 | + |
|
| 893 | + $longPath .= $ds . $folderName; |
|
| 894 | + } |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + public function testTouchNotSupported(): void { |
|
| 898 | + $storage = new TemporaryNoTouch([]); |
|
| 899 | + $scanner = $storage->getScanner(); |
|
| 900 | + Filesystem::mount($storage, [], '/test/'); |
|
| 901 | + $past = time() - 100; |
|
| 902 | + $storage->file_put_contents('test', 'foobar'); |
|
| 903 | + $scanner->scan(''); |
|
| 904 | + $view = new View(''); |
|
| 905 | + $info = $view->getFileInfo('/test/test'); |
|
| 906 | + |
|
| 907 | + $view->touch('/test/test', $past); |
|
| 908 | + $scanner->scanFile('test', Scanner::REUSE_ETAG); |
|
| 909 | + |
|
| 910 | + $info2 = $view->getFileInfo('/test/test'); |
|
| 911 | + $this->assertSame($info['etag'], $info2['etag']); |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + public function testWatcherEtagCrossStorage(): void { |
|
| 915 | + $storage1 = new Temporary([]); |
|
| 916 | + $storage2 = new Temporary([]); |
|
| 917 | + $scanner1 = $storage1->getScanner(); |
|
| 918 | + $scanner2 = $storage2->getScanner(); |
|
| 919 | + $storage1->mkdir('sub'); |
|
| 920 | + Filesystem::mount($storage1, [], '/test/'); |
|
| 921 | + Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 922 | + |
|
| 923 | + $past = time() - 100; |
|
| 924 | + $storage2->file_put_contents('test.txt', 'foobar'); |
|
| 925 | + $scanner1->scan(''); |
|
| 926 | + $scanner2->scan(''); |
|
| 927 | + $view = new View(''); |
|
| 928 | + |
|
| 929 | + $storage2->getWatcher('')->setPolicy(Watcher::CHECK_ALWAYS); |
|
| 930 | + |
|
| 931 | + $oldFileInfo = $view->getFileInfo('/test/sub/storage/test.txt'); |
|
| 932 | + $oldFolderInfo = $view->getFileInfo('/test'); |
|
| 933 | + |
|
| 934 | + $storage2->getCache()->update($oldFileInfo->getId(), [ |
|
| 935 | + 'storage_mtime' => $past, |
|
| 936 | + ]); |
|
| 937 | + |
|
| 938 | + $oldEtag = $oldFolderInfo->getEtag(); |
|
| 939 | + |
|
| 940 | + $view->getFileInfo('/test/sub/storage/test.txt'); |
|
| 941 | + $newFolderInfo = $view->getFileInfo('/test'); |
|
| 942 | + |
|
| 943 | + $this->assertNotEquals($newFolderInfo->getEtag(), $oldEtag); |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + #[\PHPUnit\Framework\Attributes\DataProvider('absolutePathProvider')] |
|
| 947 | + public function testGetAbsolutePath($expectedPath, $relativePath): void { |
|
| 948 | + $view = new View('/files'); |
|
| 949 | + $this->assertEquals($expectedPath, $view->getAbsolutePath($relativePath)); |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + public function testPartFileInfo(): void { |
|
| 953 | + $storage = new Temporary([]); |
|
| 954 | + $scanner = $storage->getScanner(); |
|
| 955 | + Filesystem::mount($storage, [], '/test/'); |
|
| 956 | + $sizeWritten = $storage->file_put_contents('test.part', 'foobar'); |
|
| 957 | + $scanner->scan(''); |
|
| 958 | + $view = new View('/test'); |
|
| 959 | + $info = $view->getFileInfo('test.part'); |
|
| 960 | + |
|
| 961 | + $this->assertInstanceOf('\OCP\Files\FileInfo', $info); |
|
| 962 | + $this->assertNull($info->getId()); |
|
| 963 | + $this->assertEquals(6, $sizeWritten); |
|
| 964 | + $this->assertEquals(6, $info->getSize()); |
|
| 965 | + $this->assertEquals('foobar', $view->file_get_contents('test.part')); |
|
| 966 | + } |
|
| 967 | + |
|
| 968 | + public static function absolutePathProvider(): array { |
|
| 969 | + return [ |
|
| 970 | + ['/files/', ''], |
|
| 971 | + ['/files/0', '0'], |
|
| 972 | + ['/files/false', 'false'], |
|
| 973 | + ['/files/true', 'true'], |
|
| 974 | + ['/files/', '/'], |
|
| 975 | + ['/files/test', 'test'], |
|
| 976 | + ['/files/test', '/test'], |
|
| 977 | + ]; |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + #[\PHPUnit\Framework\Attributes\DataProvider('chrootRelativePathProvider')] |
|
| 981 | + public function testChrootGetRelativePath($root, $absolutePath, $expectedPath): void { |
|
| 982 | + $view = new View('/files'); |
|
| 983 | + $view->chroot($root); |
|
| 984 | + $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath)); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + public static function chrootRelativePathProvider(): array { |
|
| 988 | + return self::relativePathProvider('/'); |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + #[\PHPUnit\Framework\Attributes\DataProvider('initRelativePathProvider')] |
|
| 992 | + public function testInitGetRelativePath($root, $absolutePath, $expectedPath): void { |
|
| 993 | + $view = new View($root); |
|
| 994 | + $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath)); |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + public static function initRelativePathProvider(): array { |
|
| 998 | + return self::relativePathProvider(null); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + public static function relativePathProvider($missingRootExpectedPath): array { |
|
| 1002 | + return [ |
|
| 1003 | + // No root - returns the path |
|
| 1004 | + ['', '/files', '/files'], |
|
| 1005 | + ['', '/files/', '/files/'], |
|
| 1006 | + |
|
| 1007 | + // Root equals path - / |
|
| 1008 | + ['/files/', '/files/', '/'], |
|
| 1009 | + ['/files/', '/files', '/'], |
|
| 1010 | + ['/files', '/files/', '/'], |
|
| 1011 | + ['/files', '/files', '/'], |
|
| 1012 | + |
|
| 1013 | + // False negatives: chroot fixes those by adding the leading slash. |
|
| 1014 | + // But setting them up with this root (instead of chroot($root)) |
|
| 1015 | + // will fail them, although they should be the same. |
|
| 1016 | + // TODO init should be fixed, so it also adds the leading slash |
|
| 1017 | + ['files/', '/files/', $missingRootExpectedPath], |
|
| 1018 | + ['files', '/files/', $missingRootExpectedPath], |
|
| 1019 | + ['files/', '/files', $missingRootExpectedPath], |
|
| 1020 | + ['files', '/files', $missingRootExpectedPath], |
|
| 1021 | + |
|
| 1022 | + // False negatives: Paths provided to the method should have a leading slash |
|
| 1023 | + // TODO input should be checked to have a leading slash |
|
| 1024 | + ['/files/', 'files/', null], |
|
| 1025 | + ['/files', 'files/', null], |
|
| 1026 | + ['/files/', 'files', null], |
|
| 1027 | + ['/files', 'files', null], |
|
| 1028 | + |
|
| 1029 | + // with trailing slashes |
|
| 1030 | + ['/files/', '/files/0', '0'], |
|
| 1031 | + ['/files/', '/files/false', 'false'], |
|
| 1032 | + ['/files/', '/files/true', 'true'], |
|
| 1033 | + ['/files/', '/files/test', 'test'], |
|
| 1034 | + ['/files/', '/files/test/foo', 'test/foo'], |
|
| 1035 | + |
|
| 1036 | + // without trailing slashes |
|
| 1037 | + // TODO false expectation: Should match "with trailing slashes" |
|
| 1038 | + ['/files', '/files/0', '/0'], |
|
| 1039 | + ['/files', '/files/false', '/false'], |
|
| 1040 | + ['/files', '/files/true', '/true'], |
|
| 1041 | + ['/files', '/files/test', '/test'], |
|
| 1042 | + ['/files', '/files/test/foo', '/test/foo'], |
|
| 1043 | + |
|
| 1044 | + // leading slashes |
|
| 1045 | + ['/files/', '/files_trashbin/', null], |
|
| 1046 | + ['/files', '/files_trashbin/', null], |
|
| 1047 | + ['/files/', '/files_trashbin', null], |
|
| 1048 | + ['/files', '/files_trashbin', null], |
|
| 1049 | + |
|
| 1050 | + // no leading slashes |
|
| 1051 | + ['files/', 'files_trashbin/', null], |
|
| 1052 | + ['files', 'files_trashbin/', null], |
|
| 1053 | + ['files/', 'files_trashbin', null], |
|
| 1054 | + ['files', 'files_trashbin', null], |
|
| 1055 | + |
|
| 1056 | + // mixed leading slashes |
|
| 1057 | + ['files/', '/files_trashbin/', null], |
|
| 1058 | + ['/files/', 'files_trashbin/', null], |
|
| 1059 | + ['files', '/files_trashbin/', null], |
|
| 1060 | + ['/files', 'files_trashbin/', null], |
|
| 1061 | + ['files/', '/files_trashbin', null], |
|
| 1062 | + ['/files/', 'files_trashbin', null], |
|
| 1063 | + ['files', '/files_trashbin', null], |
|
| 1064 | + ['/files', 'files_trashbin', null], |
|
| 1065 | + |
|
| 1066 | + ['files', 'files_trashbin/test', null], |
|
| 1067 | + ['/files', '/files_trashbin/test', null], |
|
| 1068 | + ['/files', 'files_trashbin/test', null], |
|
| 1069 | + ]; |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + public function testFileView(): void { |
|
| 1073 | + $storage = new Temporary([]); |
|
| 1074 | + $scanner = $storage->getScanner(); |
|
| 1075 | + $storage->file_put_contents('foo.txt', 'bar'); |
|
| 1076 | + Filesystem::mount($storage, [], '/test/'); |
|
| 1077 | + $scanner->scan(''); |
|
| 1078 | + $view = new View('/test/foo.txt'); |
|
| 1079 | + |
|
| 1080 | + $this->assertEquals('bar', $view->file_get_contents('')); |
|
| 1081 | + $fh = tmpfile(); |
|
| 1082 | + fwrite($fh, 'foo'); |
|
| 1083 | + rewind($fh); |
|
| 1084 | + $view->file_put_contents('', $fh); |
|
| 1085 | + $this->assertEquals('foo', $view->file_get_contents('')); |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + #[\PHPUnit\Framework\Attributes\DataProvider('tooLongPathDataProvider')] |
|
| 1089 | + public function testTooLongPath($operation, $param0 = null): void { |
|
| 1090 | + $this->expectException(InvalidPathException::class); |
|
| 1091 | + |
|
| 1092 | + |
|
| 1093 | + $longPath = ''; |
|
| 1094 | + // 4000 is the maximum path length in file_cache.path |
|
| 1095 | + $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789'; |
|
| 1096 | + $depth = (4000 / 57); |
|
| 1097 | + foreach (range(0, $depth + 1) as $i) { |
|
| 1098 | + $longPath .= '/' . $folderName; |
|
| 1099 | + } |
|
| 1100 | + |
|
| 1101 | + $storage = new Temporary([]); |
|
| 1102 | + $this->tempStorage = $storage; // for later hard cleanup |
|
| 1103 | + Filesystem::mount($storage, [], '/'); |
|
| 1104 | + |
|
| 1105 | + $rootView = new View(''); |
|
| 1106 | + |
|
| 1107 | + if ($param0 === '@0') { |
|
| 1108 | + $param0 = $longPath; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + if ($operation === 'hash') { |
|
| 1112 | + $param0 = $longPath; |
|
| 1113 | + $longPath = 'md5'; |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + call_user_func([$rootView, $operation], $longPath, $param0); |
|
| 1117 | + } |
|
| 1118 | + |
|
| 1119 | + public static function tooLongPathDataProvider(): array { |
|
| 1120 | + return [ |
|
| 1121 | + ['getAbsolutePath'], |
|
| 1122 | + ['getRelativePath'], |
|
| 1123 | + ['getMountPoint'], |
|
| 1124 | + ['resolvePath'], |
|
| 1125 | + ['getLocalFile'], |
|
| 1126 | + ['mkdir'], |
|
| 1127 | + ['rmdir'], |
|
| 1128 | + ['opendir'], |
|
| 1129 | + ['is_dir'], |
|
| 1130 | + ['is_file'], |
|
| 1131 | + ['stat'], |
|
| 1132 | + ['filetype'], |
|
| 1133 | + ['filesize'], |
|
| 1134 | + ['readfile'], |
|
| 1135 | + ['isCreatable'], |
|
| 1136 | + ['isReadable'], |
|
| 1137 | + ['isUpdatable'], |
|
| 1138 | + ['isDeletable'], |
|
| 1139 | + ['isSharable'], |
|
| 1140 | + ['file_exists'], |
|
| 1141 | + ['filemtime'], |
|
| 1142 | + ['touch'], |
|
| 1143 | + ['file_get_contents'], |
|
| 1144 | + ['unlink'], |
|
| 1145 | + ['deleteAll'], |
|
| 1146 | + ['toTmpFile'], |
|
| 1147 | + ['getMimeType'], |
|
| 1148 | + ['free_space'], |
|
| 1149 | + ['getFileInfo'], |
|
| 1150 | + ['getDirectoryContent'], |
|
| 1151 | + ['getOwner'], |
|
| 1152 | + ['getETag'], |
|
| 1153 | + ['file_put_contents', 'ipsum'], |
|
| 1154 | + ['rename', '@0'], |
|
| 1155 | + ['copy', '@0'], |
|
| 1156 | + ['fopen', 'r'], |
|
| 1157 | + ['fromTmpFile', '@0'], |
|
| 1158 | + ['hash'], |
|
| 1159 | + ['hasUpdated', 0], |
|
| 1160 | + ['putFileInfo', []], |
|
| 1161 | + ]; |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + public function testRenameCrossStoragePreserveMtime(): void { |
|
| 1165 | + $storage1 = new Temporary([]); |
|
| 1166 | + $storage2 = new Temporary([]); |
|
| 1167 | + $storage1->mkdir('sub'); |
|
| 1168 | + $storage1->mkdir('foo'); |
|
| 1169 | + $storage1->file_put_contents('foo.txt', 'asd'); |
|
| 1170 | + $storage1->file_put_contents('foo/bar.txt', 'asd'); |
|
| 1171 | + Filesystem::mount($storage1, [], '/test/'); |
|
| 1172 | + Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 1173 | + |
|
| 1174 | + $view = new View(''); |
|
| 1175 | + $time = time() - 200; |
|
| 1176 | + $view->touch('/test/foo.txt', $time); |
|
| 1177 | + $view->touch('/test/foo', $time); |
|
| 1178 | + $view->touch('/test/foo/bar.txt', $time); |
|
| 1179 | + |
|
| 1180 | + $view->rename('/test/foo.txt', '/test/sub/storage/foo.txt'); |
|
| 1181 | + |
|
| 1182 | + $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo.txt')); |
|
| 1183 | + |
|
| 1184 | + $view->rename('/test/foo', '/test/sub/storage/foo'); |
|
| 1185 | + |
|
| 1186 | + $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo/bar.txt')); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + public function testRenameFailDeleteTargetKeepSource(): void { |
|
| 1190 | + $this->doTestCopyRenameFail('rename'); |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + public function testCopyFailDeleteTargetKeepSource(): void { |
|
| 1194 | + $this->doTestCopyRenameFail('copy'); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + private function doTestCopyRenameFail($operation) { |
|
| 1198 | + $storage1 = new Temporary([]); |
|
| 1199 | + /** @var \PHPUnit\Framework\MockObject\MockObject|Temporary $storage2 */ |
|
| 1200 | + $storage2 = $this->getMockBuilder(TemporaryNoCross::class) |
|
| 1201 | + ->setConstructorArgs([[]]) |
|
| 1202 | + ->onlyMethods(['fopen', 'writeStream']) |
|
| 1203 | + ->getMock(); |
|
| 1204 | + |
|
| 1205 | + $storage2->method('writeStream') |
|
| 1206 | + ->willThrowException(new GenericFileException('Failed to copy stream')); |
|
| 1207 | + |
|
| 1208 | + $storage1->mkdir('sub'); |
|
| 1209 | + $storage1->file_put_contents('foo.txt', '0123456789ABCDEFGH'); |
|
| 1210 | + $storage1->mkdir('dirtomove'); |
|
| 1211 | + $storage1->file_put_contents('dirtomove/indir1.txt', '0123456'); // fits |
|
| 1212 | + $storage1->file_put_contents('dirtomove/indir2.txt', '0123456789ABCDEFGH'); // doesn't fit |
|
| 1213 | + $storage2->file_put_contents('existing.txt', '0123'); |
|
| 1214 | + $storage1->getScanner()->scan(''); |
|
| 1215 | + $storage2->getScanner()->scan(''); |
|
| 1216 | + Filesystem::mount($storage1, [], '/test/'); |
|
| 1217 | + Filesystem::mount($storage2, [], '/test/sub/storage'); |
|
| 1218 | + |
|
| 1219 | + // move file |
|
| 1220 | + $view = new View(''); |
|
| 1221 | + $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1222 | + $this->assertFalse($storage2->file_exists('foo.txt')); |
|
| 1223 | + $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/foo.txt')); |
|
| 1224 | + $this->assertFalse($storage2->file_exists('foo.txt')); |
|
| 1225 | + $this->assertFalse($storage2->getCache()->get('foo.txt')); |
|
| 1226 | + $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1227 | + |
|
| 1228 | + // if target exists, it will be deleted too |
|
| 1229 | + $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/existing.txt')); |
|
| 1230 | + $this->assertFalse($storage2->file_exists('existing.txt')); |
|
| 1231 | + $this->assertFalse($storage2->getCache()->get('existing.txt')); |
|
| 1232 | + $this->assertTrue($storage1->file_exists('foo.txt')); |
|
| 1233 | + |
|
| 1234 | + // move folder |
|
| 1235 | + $this->assertFalse($view->$operation('/test/dirtomove/', '/test/sub/storage/dirtomove/')); |
|
| 1236 | + // since the move failed, the full source tree is kept |
|
| 1237 | + $this->assertTrue($storage1->file_exists('dirtomove/indir1.txt')); |
|
| 1238 | + $this->assertTrue($storage1->file_exists('dirtomove/indir2.txt')); |
|
| 1239 | + // second file not moved/copied |
|
| 1240 | + $this->assertFalse($storage2->file_exists('dirtomove/indir2.txt')); |
|
| 1241 | + $this->assertFalse($storage2->getCache()->get('dirtomove/indir2.txt')); |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + public function testDeleteFailKeepCache(): void { |
|
| 1245 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 1246 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 1247 | + ->setConstructorArgs([[]]) |
|
| 1248 | + ->onlyMethods(['unlink']) |
|
| 1249 | + ->getMock(); |
|
| 1250 | + $storage->expects($this->once()) |
|
| 1251 | + ->method('unlink') |
|
| 1252 | + ->willReturn(false); |
|
| 1253 | + $scanner = $storage->getScanner(); |
|
| 1254 | + $cache = $storage->getCache(); |
|
| 1255 | + $storage->file_put_contents('foo.txt', 'asd'); |
|
| 1256 | + $scanner->scan(''); |
|
| 1257 | + Filesystem::mount($storage, [], '/test/'); |
|
| 1258 | + |
|
| 1259 | + $view = new View('/test'); |
|
| 1260 | + |
|
| 1261 | + $this->assertFalse($view->unlink('foo.txt')); |
|
| 1262 | + $this->assertTrue($cache->inCache('foo.txt')); |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + public static function directoryTraversalProvider(): array { |
|
| 1266 | + return [ |
|
| 1267 | + ['../test/'], |
|
| 1268 | + ['..\\test\\my/../folder'], |
|
| 1269 | + ['/test/my/../foo\\'], |
|
| 1270 | + ]; |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + /** |
|
| 1274 | + * @param string $root |
|
| 1275 | + */ |
|
| 1276 | + #[\PHPUnit\Framework\Attributes\DataProvider('directoryTraversalProvider')] |
|
| 1277 | + public function testConstructDirectoryTraversalException($root): void { |
|
| 1278 | + $this->expectException(\Exception::class); |
|
| 1279 | + |
|
| 1280 | + new View($root); |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + public function testRenameOverWrite(): void { |
|
| 1284 | + $storage = new Temporary([]); |
|
| 1285 | + $scanner = $storage->getScanner(); |
|
| 1286 | + $storage->mkdir('sub'); |
|
| 1287 | + $storage->mkdir('foo'); |
|
| 1288 | + $storage->file_put_contents('foo.txt', 'asd'); |
|
| 1289 | + $storage->file_put_contents('foo/bar.txt', 'asd'); |
|
| 1290 | + $scanner->scan(''); |
|
| 1291 | + Filesystem::mount($storage, [], '/test/'); |
|
| 1292 | + $view = new View(''); |
|
| 1293 | + $this->assertTrue($view->rename('/test/foo.txt', '/test/foo/bar.txt')); |
|
| 1294 | + } |
|
| 1295 | + |
|
| 1296 | + public function testSetMountOptionsInStorage(): void { |
|
| 1297 | + $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']); |
|
| 1298 | + Filesystem::getMountManager()->addMount($mount); |
|
| 1299 | + /** @var Common $storage */ |
|
| 1300 | + $storage = $mount->getStorage(); |
|
| 1301 | + $this->assertEquals($storage->getMountOption('foo'), 'bar'); |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + public function testSetMountOptionsWatcherPolicy(): void { |
|
| 1305 | + $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); |
|
| 1306 | + Filesystem::getMountManager()->addMount($mount); |
|
| 1307 | + /** @var Common $storage */ |
|
| 1308 | + $storage = $mount->getStorage(); |
|
| 1309 | + $watcher = $storage->getWatcher(); |
|
| 1310 | + $this->assertEquals(Watcher::CHECK_NEVER, $watcher->getPolicy()); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + public function testGetAbsolutePathOnNull(): void { |
|
| 1314 | + $view = new View(); |
|
| 1315 | + $this->assertNull($view->getAbsolutePath(null)); |
|
| 1316 | + } |
|
| 1317 | + |
|
| 1318 | + public function testGetRelativePathOnNull(): void { |
|
| 1319 | + $view = new View(); |
|
| 1320 | + $this->assertNull($view->getRelativePath(null)); |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + |
|
| 1324 | + public function testNullAsRoot(): void { |
|
| 1325 | + $this->expectException(\TypeError::class); |
|
| 1326 | + |
|
| 1327 | + new View(null); |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + /** |
|
| 1331 | + * e.g. reading from a folder that's being renamed |
|
| 1332 | + * |
|
| 1333 | + * |
|
| 1334 | + * |
|
| 1335 | + * @param string $rootPath |
|
| 1336 | + * @param string $pathPrefix |
|
| 1337 | + */ |
|
| 1338 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1339 | + public function testReadFromWriteLockedPath($rootPath, $pathPrefix): void { |
|
| 1340 | + $this->expectException(LockedException::class); |
|
| 1341 | + |
|
| 1342 | + $rootPath = str_replace('{folder}', 'files', $rootPath); |
|
| 1343 | + $pathPrefix = str_replace('{folder}', 'files', $pathPrefix); |
|
| 1344 | + |
|
| 1345 | + $view = new View($rootPath); |
|
| 1346 | + $storage = new Temporary([]); |
|
| 1347 | + Filesystem::mount($storage, [], '/'); |
|
| 1348 | + $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1349 | + $view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED); |
|
| 1350 | + } |
|
| 1351 | + |
|
| 1352 | + /** |
|
| 1353 | + * Reading from a files_encryption folder that's being renamed |
|
| 1354 | + * |
|
| 1355 | + * |
|
| 1356 | + * @param string $rootPath |
|
| 1357 | + * @param string $pathPrefix |
|
| 1358 | + */ |
|
| 1359 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1360 | + public function testReadFromWriteUnlockablePath($rootPath, $pathPrefix): void { |
|
| 1361 | + $rootPath = str_replace('{folder}', 'files_encryption', $rootPath); |
|
| 1362 | + $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix); |
|
| 1363 | + |
|
| 1364 | + $view = new View($rootPath); |
|
| 1365 | + $storage = new Temporary([]); |
|
| 1366 | + Filesystem::mount($storage, [], '/'); |
|
| 1367 | + $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1368 | + $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED)); |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + /** |
|
| 1372 | + * e.g. writing a file that's being downloaded |
|
| 1373 | + * |
|
| 1374 | + * |
|
| 1375 | + * |
|
| 1376 | + * @param string $rootPath |
|
| 1377 | + * @param string $pathPrefix |
|
| 1378 | + */ |
|
| 1379 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1380 | + public function testWriteToReadLockedFile($rootPath, $pathPrefix): void { |
|
| 1381 | + $this->expectException(LockedException::class); |
|
| 1382 | + |
|
| 1383 | + $rootPath = str_replace('{folder}', 'files', $rootPath); |
|
| 1384 | + $pathPrefix = str_replace('{folder}', 'files', $pathPrefix); |
|
| 1385 | + |
|
| 1386 | + $view = new View($rootPath); |
|
| 1387 | + $storage = new Temporary([]); |
|
| 1388 | + Filesystem::mount($storage, [], '/'); |
|
| 1389 | + $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED)); |
|
| 1390 | + $view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * Writing a file that's being downloaded |
|
| 1395 | + * |
|
| 1396 | + * |
|
| 1397 | + * @param string $rootPath |
|
| 1398 | + * @param string $pathPrefix |
|
| 1399 | + */ |
|
| 1400 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataLockPaths')] |
|
| 1401 | + public function testWriteToReadUnlockableFile($rootPath, $pathPrefix): void { |
|
| 1402 | + $rootPath = str_replace('{folder}', 'files_encryption', $rootPath); |
|
| 1403 | + $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix); |
|
| 1404 | + |
|
| 1405 | + $view = new View($rootPath); |
|
| 1406 | + $storage = new Temporary([]); |
|
| 1407 | + Filesystem::mount($storage, [], '/'); |
|
| 1408 | + $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED)); |
|
| 1409 | + $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * Test that locks are on mount point paths instead of mount root |
|
| 1414 | + */ |
|
| 1415 | + public function testLockLocalMountPointPathInsteadOfStorageRoot(): void { |
|
| 1416 | + $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1417 | + $view = new View('/testuser/files/'); |
|
| 1418 | + $storage = new Temporary([]); |
|
| 1419 | + Filesystem::mount($storage, [], '/'); |
|
| 1420 | + $mountedStorage = new Temporary([]); |
|
| 1421 | + Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint'); |
|
| 1422 | + |
|
| 1423 | + $this->assertTrue( |
|
| 1424 | + $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, true), |
|
| 1425 | + 'Can lock mount point' |
|
| 1426 | + ); |
|
| 1427 | + |
|
| 1428 | + // no exception here because storage root was not locked |
|
| 1429 | + $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1430 | + |
|
| 1431 | + $thrown = false; |
|
| 1432 | + try { |
|
| 1433 | + $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1434 | + } catch (LockedException $e) { |
|
| 1435 | + $thrown = true; |
|
| 1436 | + } |
|
| 1437 | + $this->assertTrue($thrown, 'Mount point path was locked on root storage'); |
|
| 1438 | + |
|
| 1439 | + $lockingProvider->releaseAll(); |
|
| 1440 | + } |
|
| 1441 | + |
|
| 1442 | + /** |
|
| 1443 | + * Test that locks are on mount point paths and also mount root when requested |
|
| 1444 | + */ |
|
| 1445 | + public function testLockStorageRootButNotLocalMountPoint(): void { |
|
| 1446 | + $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1447 | + $view = new View('/testuser/files/'); |
|
| 1448 | + $storage = new Temporary([]); |
|
| 1449 | + Filesystem::mount($storage, [], '/'); |
|
| 1450 | + $mountedStorage = new Temporary([]); |
|
| 1451 | + Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint'); |
|
| 1452 | + |
|
| 1453 | + $this->assertTrue( |
|
| 1454 | + $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, false), |
|
| 1455 | + 'Can lock mount point' |
|
| 1456 | + ); |
|
| 1457 | + |
|
| 1458 | + $thrown = false; |
|
| 1459 | + try { |
|
| 1460 | + $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1461 | + } catch (LockedException $e) { |
|
| 1462 | + $thrown = true; |
|
| 1463 | + } |
|
| 1464 | + $this->assertTrue($thrown, 'Mount point storage root was locked on original storage'); |
|
| 1465 | + |
|
| 1466 | + // local mount point was not locked |
|
| 1467 | + $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1468 | + |
|
| 1469 | + $lockingProvider->releaseAll(); |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + /** |
|
| 1473 | + * Test that locks are on mount point paths and also mount root when requested |
|
| 1474 | + */ |
|
| 1475 | + public function testLockMountPointPathFailReleasesBoth(): void { |
|
| 1476 | + $lockingProvider = Server::get(ILockingProvider::class); |
|
| 1477 | + $view = new View('/testuser/files/'); |
|
| 1478 | + $storage = new Temporary([]); |
|
| 1479 | + Filesystem::mount($storage, [], '/'); |
|
| 1480 | + $mountedStorage = new Temporary([]); |
|
| 1481 | + Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint.txt'); |
|
| 1482 | + |
|
| 1483 | + // this would happen if someone is writing on the mount point |
|
| 1484 | + $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1485 | + |
|
| 1486 | + $thrown = false; |
|
| 1487 | + try { |
|
| 1488 | + // this actually acquires two locks, one on the mount point and one on the storage root, |
|
| 1489 | + // but the one on the storage root will fail |
|
| 1490 | + $view->lockFile('/mountpoint.txt', ILockingProvider::LOCK_SHARED); |
|
| 1491 | + } catch (LockedException $e) { |
|
| 1492 | + $thrown = true; |
|
| 1493 | + } |
|
| 1494 | + $this->assertTrue($thrown, 'Cannot acquire shared lock because storage root is already locked'); |
|
| 1495 | + |
|
| 1496 | + // from here we expect that the lock on the local mount point was released properly |
|
| 1497 | + // so acquiring an exclusive lock will succeed |
|
| 1498 | + $storage->acquireLock('/testuser/files/mountpoint.txt', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); |
|
| 1499 | + |
|
| 1500 | + $lockingProvider->releaseAll(); |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + public static function dataLockPaths(): array { |
|
| 1504 | + return [ |
|
| 1505 | + ['/testuser/{folder}', ''], |
|
| 1506 | + ['/testuser', '/{folder}'], |
|
| 1507 | + ['', '/testuser/{folder}'], |
|
| 1508 | + ]; |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + public static function pathRelativeToFilesProvider(): array { |
|
| 1512 | + return [ |
|
| 1513 | + ['admin/files', ''], |
|
| 1514 | + ['admin/files/x', 'x'], |
|
| 1515 | + ['/admin/files', ''], |
|
| 1516 | + ['/admin/files/sub', 'sub'], |
|
| 1517 | + ['/admin/files/sub/', 'sub'], |
|
| 1518 | + ['/admin/files/sub/sub2', 'sub/sub2'], |
|
| 1519 | + ['//admin//files/sub//sub2', 'sub/sub2'], |
|
| 1520 | + ]; |
|
| 1521 | + } |
|
| 1522 | + |
|
| 1523 | + #[\PHPUnit\Framework\Attributes\DataProvider('pathRelativeToFilesProvider')] |
|
| 1524 | + public function testGetPathRelativeToFiles($path, $expectedPath): void { |
|
| 1525 | + $view = new View(); |
|
| 1526 | + $this->assertEquals($expectedPath, $view->getPathRelativeToFiles($path)); |
|
| 1527 | + } |
|
| 1528 | + |
|
| 1529 | + public static function pathRelativeToFilesProviderExceptionCases(): array { |
|
| 1530 | + return [ |
|
| 1531 | + [''], |
|
| 1532 | + ['x'], |
|
| 1533 | + ['files'], |
|
| 1534 | + ['/files'], |
|
| 1535 | + ['/admin/files_versions/abc'], |
|
| 1536 | + ]; |
|
| 1537 | + } |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * @param string $path |
|
| 1541 | + */ |
|
| 1542 | + #[\PHPUnit\Framework\Attributes\DataProvider('pathRelativeToFilesProviderExceptionCases')] |
|
| 1543 | + public function testGetPathRelativeToFilesWithInvalidArgument($path): void { |
|
| 1544 | + $this->expectException(\InvalidArgumentException::class); |
|
| 1545 | + $this->expectExceptionMessage('$absolutePath must be relative to "files"'); |
|
| 1546 | + |
|
| 1547 | + $view = new View(); |
|
| 1548 | + $view->getPathRelativeToFiles($path); |
|
| 1549 | + } |
|
| 1550 | + |
|
| 1551 | + public function testChangeLock(): void { |
|
| 1552 | + $view = new View('/testuser/files/'); |
|
| 1553 | + $storage = new Temporary([]); |
|
| 1554 | + Filesystem::mount($storage, [], '/'); |
|
| 1555 | + |
|
| 1556 | + $view->lockFile('/test/sub', ILockingProvider::LOCK_SHARED); |
|
| 1557 | + $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1558 | + $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1559 | + |
|
| 1560 | + $view->changeLock('//test/sub', ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1561 | + $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1562 | + |
|
| 1563 | + $view->changeLock('test/sub', ILockingProvider::LOCK_SHARED); |
|
| 1564 | + $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1565 | + |
|
| 1566 | + $view->unlockFile('/test/sub/', ILockingProvider::LOCK_SHARED); |
|
| 1567 | + |
|
| 1568 | + $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED)); |
|
| 1569 | + $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE)); |
|
| 1570 | + } |
|
| 1571 | + |
|
| 1572 | + public static function hookPathProvider(): array { |
|
| 1573 | + return [ |
|
| 1574 | + ['/foo/files', '/foo', true], |
|
| 1575 | + ['/foo/files/bar', '/foo', true], |
|
| 1576 | + ['/foo', '/foo', false], |
|
| 1577 | + ['/foo', '/files/foo', true], |
|
| 1578 | + ['/foo', 'filesfoo', false], |
|
| 1579 | + ['', '/foo/files', true], |
|
| 1580 | + ['', '/foo/files/bar.txt', true], |
|
| 1581 | + ]; |
|
| 1582 | + } |
|
| 1583 | + |
|
| 1584 | + /** |
|
| 1585 | + * @param $root |
|
| 1586 | + * @param $path |
|
| 1587 | + * @param $shouldEmit |
|
| 1588 | + */ |
|
| 1589 | + #[\PHPUnit\Framework\Attributes\DataProvider('hookPathProvider')] |
|
| 1590 | + public function testHookPaths($root, $path, $shouldEmit): void { |
|
| 1591 | + $filesystemReflection = new \ReflectionClass(Filesystem::class); |
|
| 1592 | + $defaultRootValue = $filesystemReflection->getProperty('defaultInstance'); |
|
| 1593 | + $defaultRootValue->setAccessible(true); |
|
| 1594 | + $oldRoot = $defaultRootValue->getValue(); |
|
| 1595 | + $defaultView = new View('/foo/files'); |
|
| 1596 | + $defaultRootValue->setValue(null, $defaultView); |
|
| 1597 | + $view = new View($root); |
|
| 1598 | + $result = self::invokePrivate($view, 'shouldEmitHooks', [$path]); |
|
| 1599 | + $defaultRootValue->setValue(null, $oldRoot); |
|
| 1600 | + $this->assertEquals($shouldEmit, $result); |
|
| 1601 | + } |
|
| 1602 | + |
|
| 1603 | + /** |
|
| 1604 | + * Create test movable mount points |
|
| 1605 | + * |
|
| 1606 | + * @param array $mountPoints array of mount point locations |
|
| 1607 | + * @return array array of MountPoint objects |
|
| 1608 | + */ |
|
| 1609 | + private function createTestMovableMountPoints($mountPoints) { |
|
| 1610 | + $mounts = []; |
|
| 1611 | + foreach ($mountPoints as $mountPoint) { |
|
| 1612 | + $storage = $this->getMockBuilder(Storage::class) |
|
| 1613 | + ->onlyMethods([]) |
|
| 1614 | + ->getMock(); |
|
| 1615 | + $storage->method('getId')->willReturn('non-null-id'); |
|
| 1616 | + $storage->method('getStorageCache')->willReturnCallback(function () use ($storage) { |
|
| 1617 | + return new \OC\Files\Cache\Storage($storage, true, Server::get(IDBConnection::class)); |
|
| 1618 | + }); |
|
| 1619 | + |
|
| 1620 | + $mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class) |
|
| 1621 | + ->onlyMethods(['moveMount']) |
|
| 1622 | + ->setConstructorArgs([$storage, $mountPoint]) |
|
| 1623 | + ->getMock(); |
|
| 1624 | + } |
|
| 1625 | + |
|
| 1626 | + /** @var IMountProvider|\PHPUnit\Framework\MockObject\MockObject $mountProvider */ |
|
| 1627 | + $mountProvider = $this->createMock(IMountProvider::class); |
|
| 1628 | + $mountProvider->expects($this->any()) |
|
| 1629 | + ->method('getMountsForUser') |
|
| 1630 | + ->willReturn($mounts); |
|
| 1631 | + |
|
| 1632 | + $mountProviderCollection = Server::get(IMountProviderCollection::class); |
|
| 1633 | + $mountProviderCollection->registerProvider($mountProvider); |
|
| 1634 | + |
|
| 1635 | + return $mounts; |
|
| 1636 | + } |
|
| 1637 | + |
|
| 1638 | + /** |
|
| 1639 | + * Test mount point move |
|
| 1640 | + */ |
|
| 1641 | + public function testMountPointMove(): void { |
|
| 1642 | + self::loginAsUser($this->user); |
|
| 1643 | + |
|
| 1644 | + [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1645 | + $this->user . '/files/mount1', |
|
| 1646 | + $this->user . '/files/mount2', |
|
| 1647 | + ]); |
|
| 1648 | + $mount1->expects($this->once()) |
|
| 1649 | + ->method('moveMount') |
|
| 1650 | + ->willReturn(true); |
|
| 1651 | + |
|
| 1652 | + $mount2->expects($this->once()) |
|
| 1653 | + ->method('moveMount') |
|
| 1654 | + ->willReturn(true); |
|
| 1655 | + |
|
| 1656 | + $view = new View('/' . $this->user . '/files/'); |
|
| 1657 | + $view->mkdir('sub'); |
|
| 1658 | + |
|
| 1659 | + $this->assertTrue($view->rename('mount1', 'renamed_mount'), 'Can rename mount point'); |
|
| 1660 | + $this->assertTrue($view->rename('mount2', 'sub/moved_mount'), 'Can move a mount point into a subdirectory'); |
|
| 1661 | + } |
|
| 1662 | + |
|
| 1663 | + public function testMoveMountPointOverwrite(): void { |
|
| 1664 | + self::loginAsUser($this->user); |
|
| 1665 | + |
|
| 1666 | + [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1667 | + $this->user . '/files/mount1', |
|
| 1668 | + $this->user . '/files/mount2', |
|
| 1669 | + ]); |
|
| 1670 | + |
|
| 1671 | + $mount1->expects($this->never()) |
|
| 1672 | + ->method('moveMount'); |
|
| 1673 | + |
|
| 1674 | + $mount2->expects($this->never()) |
|
| 1675 | + ->method('moveMount'); |
|
| 1676 | + |
|
| 1677 | + $view = new View('/' . $this->user . '/files/'); |
|
| 1678 | + |
|
| 1679 | + $this->expectException(ForbiddenException::class); |
|
| 1680 | + $view->rename('mount1', 'mount2'); |
|
| 1681 | + } |
|
| 1682 | + |
|
| 1683 | + public function testMoveMountPointIntoMount(): void { |
|
| 1684 | + self::loginAsUser($this->user); |
|
| 1685 | + |
|
| 1686 | + [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1687 | + $this->user . '/files/mount1', |
|
| 1688 | + $this->user . '/files/mount2', |
|
| 1689 | + ]); |
|
| 1690 | + |
|
| 1691 | + $mount1->expects($this->never()) |
|
| 1692 | + ->method('moveMount'); |
|
| 1693 | + |
|
| 1694 | + $mount2->expects($this->never()) |
|
| 1695 | + ->method('moveMount'); |
|
| 1696 | + |
|
| 1697 | + $view = new View('/' . $this->user . '/files/'); |
|
| 1698 | + |
|
| 1699 | + $this->expectException(ForbiddenException::class); |
|
| 1700 | + $view->rename('mount1', 'mount2/sub'); |
|
| 1701 | + } |
|
| 1702 | + |
|
| 1703 | + /** |
|
| 1704 | + * Test that moving a mount point into a shared folder is forbidden |
|
| 1705 | + */ |
|
| 1706 | + public function testMoveMountPointIntoSharedFolder(): void { |
|
| 1707 | + self::loginAsUser($this->user); |
|
| 1708 | + |
|
| 1709 | + [$mount1, $mount2] = $this->createTestMovableMountPoints([ |
|
| 1710 | + $this->user . '/files/mount1', |
|
| 1711 | + $this->user . '/files/mount2', |
|
| 1712 | + ]); |
|
| 1713 | + |
|
| 1714 | + $mount1->expects($this->never()) |
|
| 1715 | + ->method('moveMount'); |
|
| 1716 | + |
|
| 1717 | + $mount2->expects($this->once()) |
|
| 1718 | + ->method('moveMount') |
|
| 1719 | + ->willReturn(true); |
|
| 1720 | + |
|
| 1721 | + $view = new View('/' . $this->user . '/files/'); |
|
| 1722 | + $view->mkdir('shareddir'); |
|
| 1723 | + $view->mkdir('shareddir/sub'); |
|
| 1724 | + $view->mkdir('shareddir/sub2'); |
|
| 1725 | + // Create a similar named but non-shared folder |
|
| 1726 | + $view->mkdir('shareddir notshared'); |
|
| 1727 | + |
|
| 1728 | + $fileId = $view->getFileInfo('shareddir')->getId(); |
|
| 1729 | + $userObject = Server::get(IUserManager::class)->createUser('test2', 'IHateNonMockableStaticClasses'); |
|
| 1730 | + |
|
| 1731 | + $userFolder = \OC::$server->getUserFolder($this->user); |
|
| 1732 | + $shareDir = $userFolder->get('shareddir'); |
|
| 1733 | + $shareManager = Server::get(IShareManager::class); |
|
| 1734 | + $share = $shareManager->newShare(); |
|
| 1735 | + $share->setSharedWith('test2') |
|
| 1736 | + ->setSharedBy($this->user) |
|
| 1737 | + ->setShareType(IShare::TYPE_USER) |
|
| 1738 | + ->setPermissions(Constants::PERMISSION_READ) |
|
| 1739 | + ->setNode($shareDir); |
|
| 1740 | + $shareManager->createShare($share); |
|
| 1741 | + |
|
| 1742 | + try { |
|
| 1743 | + $view->rename('mount1', 'shareddir'); |
|
| 1744 | + $this->fail('Cannot overwrite shared folder'); |
|
| 1745 | + } catch (ForbiddenException $e) { |
|
| 1746 | + |
|
| 1747 | + } |
|
| 1748 | + try { |
|
| 1749 | + $view->rename('mount1', 'shareddir/sub'); |
|
| 1750 | + $this->fail('Cannot move mount point into shared folder'); |
|
| 1751 | + } catch (ForbiddenException $e) { |
|
| 1752 | + |
|
| 1753 | + } |
|
| 1754 | + try { |
|
| 1755 | + $view->rename('mount1', 'shareddir/sub/sub2'); |
|
| 1756 | + $this->fail('Cannot move mount point into shared subfolder'); |
|
| 1757 | + } catch (ForbiddenException $e) { |
|
| 1758 | + |
|
| 1759 | + } |
|
| 1760 | + $this->assertTrue($view->rename('mount2', 'shareddir notshared/sub'), 'Can move mount point into a similarly named but non-shared folder'); |
|
| 1761 | + |
|
| 1762 | + $shareManager->deleteShare($share); |
|
| 1763 | + $userObject->delete(); |
|
| 1764 | + } |
|
| 1765 | + |
|
| 1766 | + public static function basicOperationProviderForLocks(): array { |
|
| 1767 | + return [ |
|
| 1768 | + // --- write hook ---- |
|
| 1769 | + [ |
|
| 1770 | + 'touch', |
|
| 1771 | + ['touch-create.txt'], |
|
| 1772 | + 'touch-create.txt', |
|
| 1773 | + 'create', |
|
| 1774 | + ILockingProvider::LOCK_SHARED, |
|
| 1775 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1776 | + ILockingProvider::LOCK_SHARED, |
|
| 1777 | + ], |
|
| 1778 | + [ |
|
| 1779 | + 'fopen', |
|
| 1780 | + ['test-write.txt', 'w'], |
|
| 1781 | + 'test-write.txt', |
|
| 1782 | + 'write', |
|
| 1783 | + ILockingProvider::LOCK_SHARED, |
|
| 1784 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1785 | + null, |
|
| 1786 | + // exclusive lock stays until fclose |
|
| 1787 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1788 | + ], |
|
| 1789 | + [ |
|
| 1790 | + 'mkdir', |
|
| 1791 | + ['newdir'], |
|
| 1792 | + 'newdir', |
|
| 1793 | + 'write', |
|
| 1794 | + ILockingProvider::LOCK_SHARED, |
|
| 1795 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1796 | + ILockingProvider::LOCK_SHARED, |
|
| 1797 | + ], |
|
| 1798 | + [ |
|
| 1799 | + 'file_put_contents', |
|
| 1800 | + ['file_put_contents.txt', 'blah'], |
|
| 1801 | + 'file_put_contents.txt', |
|
| 1802 | + 'write', |
|
| 1803 | + ILockingProvider::LOCK_SHARED, |
|
| 1804 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1805 | + ILockingProvider::LOCK_SHARED, |
|
| 1806 | + null, |
|
| 1807 | + 0, |
|
| 1808 | + ], |
|
| 1809 | + |
|
| 1810 | + // ---- delete hook ---- |
|
| 1811 | + [ |
|
| 1812 | + 'rmdir', |
|
| 1813 | + ['dir'], |
|
| 1814 | + 'dir', |
|
| 1815 | + 'delete', |
|
| 1816 | + ILockingProvider::LOCK_SHARED, |
|
| 1817 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1818 | + ILockingProvider::LOCK_SHARED, |
|
| 1819 | + ], |
|
| 1820 | + [ |
|
| 1821 | + 'unlink', |
|
| 1822 | + ['test.txt'], |
|
| 1823 | + 'test.txt', |
|
| 1824 | + 'delete', |
|
| 1825 | + ILockingProvider::LOCK_SHARED, |
|
| 1826 | + ILockingProvider::LOCK_EXCLUSIVE, |
|
| 1827 | + ILockingProvider::LOCK_SHARED, |
|
| 1828 | + ], |
|
| 1829 | + |
|
| 1830 | + // ---- read hook (no post hooks) ---- |
|
| 1831 | + [ |
|
| 1832 | + 'file_get_contents', |
|
| 1833 | + ['test.txt'], |
|
| 1834 | + 'test.txt', |
|
| 1835 | + 'read', |
|
| 1836 | + ILockingProvider::LOCK_SHARED, |
|
| 1837 | + ILockingProvider::LOCK_SHARED, |
|
| 1838 | + null, |
|
| 1839 | + null, |
|
| 1840 | + false, |
|
| 1841 | + ], |
|
| 1842 | + [ |
|
| 1843 | + 'fopen', |
|
| 1844 | + ['test.txt', 'r'], |
|
| 1845 | + 'test.txt', |
|
| 1846 | + 'read', |
|
| 1847 | + ILockingProvider::LOCK_SHARED, |
|
| 1848 | + ILockingProvider::LOCK_SHARED, |
|
| 1849 | + null, |
|
| 1850 | + ], |
|
| 1851 | + [ |
|
| 1852 | + 'opendir', |
|
| 1853 | + ['dir'], |
|
| 1854 | + 'dir', |
|
| 1855 | + 'read', |
|
| 1856 | + ILockingProvider::LOCK_SHARED, |
|
| 1857 | + ILockingProvider::LOCK_SHARED, |
|
| 1858 | + null, |
|
| 1859 | + ], |
|
| 1860 | + |
|
| 1861 | + // ---- no lock, touch hook --- |
|
| 1862 | + ['touch', ['test.txt'], 'test.txt', 'touch', null, null, null], |
|
| 1863 | + |
|
| 1864 | + // ---- no hooks, no locks --- |
|
| 1865 | + ['is_dir', ['dir'], 'dir', null], |
|
| 1866 | + ['is_file', ['dir'], 'dir', null], |
|
| 1867 | + [ |
|
| 1868 | + 'stat', |
|
| 1869 | + ['dir'], |
|
| 1870 | + 'dir', |
|
| 1871 | + null, |
|
| 1872 | + ILockingProvider::LOCK_SHARED, |
|
| 1873 | + ILockingProvider::LOCK_SHARED, |
|
| 1874 | + ILockingProvider::LOCK_SHARED, |
|
| 1875 | + null, |
|
| 1876 | + false, |
|
| 1877 | + ], |
|
| 1878 | + [ |
|
| 1879 | + 'filetype', |
|
| 1880 | + ['dir'], |
|
| 1881 | + 'dir', |
|
| 1882 | + null, |
|
| 1883 | + ILockingProvider::LOCK_SHARED, |
|
| 1884 | + ILockingProvider::LOCK_SHARED, |
|
| 1885 | + ILockingProvider::LOCK_SHARED, |
|
| 1886 | + null, |
|
| 1887 | + false, |
|
| 1888 | + ], |
|
| 1889 | + [ |
|
| 1890 | + 'filesize', |
|
| 1891 | + ['dir'], |
|
| 1892 | + 'dir', |
|
| 1893 | + null, |
|
| 1894 | + ILockingProvider::LOCK_SHARED, |
|
| 1895 | + ILockingProvider::LOCK_SHARED, |
|
| 1896 | + ILockingProvider::LOCK_SHARED, |
|
| 1897 | + null, |
|
| 1898 | + /* Return an int */ |
|
| 1899 | + 100 |
|
| 1900 | + ], |
|
| 1901 | + ['isCreatable', ['dir'], 'dir', null], |
|
| 1902 | + ['isReadable', ['dir'], 'dir', null], |
|
| 1903 | + ['isUpdatable', ['dir'], 'dir', null], |
|
| 1904 | + ['isDeletable', ['dir'], 'dir', null], |
|
| 1905 | + ['isSharable', ['dir'], 'dir', null], |
|
| 1906 | + ['file_exists', ['dir'], 'dir', null], |
|
| 1907 | + [ |
|
| 1908 | + 'filemtime', |
|
| 1909 | + ['dir'], |
|
| 1910 | + 'dir', |
|
| 1911 | + null, |
|
| 1912 | + ILockingProvider::LOCK_SHARED, |
|
| 1913 | + ILockingProvider::LOCK_SHARED, |
|
| 1914 | + ILockingProvider::LOCK_SHARED, |
|
| 1915 | + null, |
|
| 1916 | + false, |
|
| 1917 | + ], |
|
| 1918 | + ]; |
|
| 1919 | + } |
|
| 1920 | + |
|
| 1921 | + /** |
|
| 1922 | + * Test whether locks are set before and after the operation |
|
| 1923 | + * |
|
| 1924 | + * |
|
| 1925 | + * @param string $operation operation name on the view |
|
| 1926 | + * @param array $operationArgs arguments for the operation |
|
| 1927 | + * @param string $lockedPath path of the locked item to check |
|
| 1928 | + * @param string $hookType hook type |
|
| 1929 | + * @param int $expectedLockBefore expected lock during pre hooks |
|
| 1930 | + * @param int $expectedLockDuring expected lock during operation |
|
| 1931 | + * @param int $expectedLockAfter expected lock during post hooks |
|
| 1932 | + * @param int $expectedStrayLock expected lock after returning, should |
|
| 1933 | + * be null (unlock) for most operations |
|
| 1934 | + */ |
|
| 1935 | + #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 1936 | + public function testLockBasicOperation( |
|
| 1937 | + $operation, |
|
| 1938 | + $operationArgs, |
|
| 1939 | + $lockedPath, |
|
| 1940 | + $hookType, |
|
| 1941 | + $expectedLockBefore = ILockingProvider::LOCK_SHARED, |
|
| 1942 | + $expectedLockDuring = ILockingProvider::LOCK_SHARED, |
|
| 1943 | + $expectedLockAfter = ILockingProvider::LOCK_SHARED, |
|
| 1944 | + $expectedStrayLock = null, |
|
| 1945 | + $returnValue = true, |
|
| 1946 | + ): void { |
|
| 1947 | + $view = new View('/' . $this->user . '/files/'); |
|
| 1948 | + |
|
| 1949 | + /** @var Temporary&MockObject $storage */ |
|
| 1950 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 1951 | + ->onlyMethods([$operation]) |
|
| 1952 | + ->getMock(); |
|
| 1953 | + |
|
| 1954 | + /* Pause trash to avoid the trashbin intercepting rmdir and unlink calls */ |
|
| 1955 | + Server::get(ITrashManager::class)->pauseTrash(); |
|
| 1956 | + /* Same thing with encryption wrapper */ |
|
| 1957 | + Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 1958 | + |
|
| 1959 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 1960 | + |
|
| 1961 | + // work directly on disk because mkdir might be mocked |
|
| 1962 | + $realPath = $storage->getSourcePath(''); |
|
| 1963 | + mkdir($realPath . '/files'); |
|
| 1964 | + mkdir($realPath . '/files/dir'); |
|
| 1965 | + file_put_contents($realPath . '/files/test.txt', 'blah'); |
|
| 1966 | + $storage->getScanner()->scan('files'); |
|
| 1967 | + |
|
| 1968 | + $storage->expects($this->once()) |
|
| 1969 | + ->method($operation) |
|
| 1970 | + ->willReturnCallback( |
|
| 1971 | + function () use ($view, $lockedPath, &$lockTypeDuring, $returnValue) { |
|
| 1972 | + $lockTypeDuring = $this->getFileLockType($view, $lockedPath); |
|
| 1973 | + |
|
| 1974 | + return $returnValue; |
|
| 1975 | + } |
|
| 1976 | + ); |
|
| 1977 | + |
|
| 1978 | + $this->assertNull($this->getFileLockType($view, $lockedPath), 'File not locked before operation'); |
|
| 1979 | + |
|
| 1980 | + $this->connectMockHooks($hookType, $view, $lockedPath, $lockTypePre, $lockTypePost); |
|
| 1981 | + |
|
| 1982 | + // do operation |
|
| 1983 | + call_user_func_array([$view, $operation], $operationArgs); |
|
| 1984 | + |
|
| 1985 | + if ($hookType !== null) { |
|
| 1986 | + $this->assertEquals($expectedLockBefore, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 1987 | + $this->assertEquals($expectedLockAfter, $lockTypePost, 'File locked properly during post-hook'); |
|
| 1988 | + $this->assertEquals($expectedLockDuring, $lockTypeDuring, 'File locked properly during operation'); |
|
| 1989 | + } else { |
|
| 1990 | + $this->assertNull($lockTypeDuring, 'File not locked during operation'); |
|
| 1991 | + } |
|
| 1992 | + |
|
| 1993 | + $this->assertEquals($expectedStrayLock, $this->getFileLockType($view, $lockedPath)); |
|
| 1994 | + |
|
| 1995 | + /* Resume trash to avoid side effects */ |
|
| 1996 | + Server::get(ITrashManager::class)->resumeTrash(); |
|
| 1997 | + } |
|
| 1998 | + |
|
| 1999 | + /** |
|
| 2000 | + * Test locks for file_put_content with stream. |
|
| 2001 | + * This code path uses $storage->fopen instead |
|
| 2002 | + */ |
|
| 2003 | + public function testLockFilePutContentWithStream(): void { |
|
| 2004 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2005 | + |
|
| 2006 | + $path = 'test_file_put_contents.txt'; |
|
| 2007 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2008 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2009 | + ->onlyMethods(['fopen']) |
|
| 2010 | + ->getMock(); |
|
| 2011 | + |
|
| 2012 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2013 | + $storage->mkdir('files'); |
|
| 2014 | + |
|
| 2015 | + $storage->expects($this->once()) |
|
| 2016 | + ->method('fopen') |
|
| 2017 | + ->willReturnCallback( |
|
| 2018 | + function () use ($view, $path, &$lockTypeDuring) { |
|
| 2019 | + $lockTypeDuring = $this->getFileLockType($view, $path); |
|
| 2020 | + |
|
| 2021 | + return fopen('php://temp', 'r+'); |
|
| 2022 | + } |
|
| 2023 | + ); |
|
| 2024 | + |
|
| 2025 | + $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost); |
|
| 2026 | + |
|
| 2027 | + $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation'); |
|
| 2028 | + |
|
| 2029 | + // do operation |
|
| 2030 | + $view->file_put_contents($path, fopen('php://temp', 'r+')); |
|
| 2031 | + |
|
| 2032 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 2033 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePost, 'File locked properly during post-hook'); |
|
| 2034 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation'); |
|
| 2035 | + |
|
| 2036 | + $this->assertNull($this->getFileLockType($view, $path)); |
|
| 2037 | + } |
|
| 2038 | + |
|
| 2039 | + /** |
|
| 2040 | + * Test locks for fopen with fclose at the end |
|
| 2041 | + */ |
|
| 2042 | + public function testLockFopen(): void { |
|
| 2043 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2044 | + |
|
| 2045 | + $path = 'test_file_put_contents.txt'; |
|
| 2046 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2047 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2048 | + ->onlyMethods(['fopen']) |
|
| 2049 | + ->getMock(); |
|
| 2050 | + |
|
| 2051 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2052 | + $storage->mkdir('files'); |
|
| 2053 | + |
|
| 2054 | + $storage->expects($this->once()) |
|
| 2055 | + ->method('fopen') |
|
| 2056 | + ->willReturnCallback( |
|
| 2057 | + function () use ($view, $path, &$lockTypeDuring) { |
|
| 2058 | + $lockTypeDuring = $this->getFileLockType($view, $path); |
|
| 2059 | + |
|
| 2060 | + return fopen('php://temp', 'r+'); |
|
| 2061 | + } |
|
| 2062 | + ); |
|
| 2063 | + |
|
| 2064 | + $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost); |
|
| 2065 | + |
|
| 2066 | + $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation'); |
|
| 2067 | + |
|
| 2068 | + // do operation |
|
| 2069 | + $res = $view->fopen($path, 'w'); |
|
| 2070 | + |
|
| 2071 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook'); |
|
| 2072 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation'); |
|
| 2073 | + $this->assertNull($lockTypePost, 'No post hook, no lock check possible'); |
|
| 2074 | + |
|
| 2075 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File still locked after fopen'); |
|
| 2076 | + |
|
| 2077 | + fclose($res); |
|
| 2078 | + |
|
| 2079 | + $this->assertNull($this->getFileLockType($view, $path), 'File unlocked after fclose'); |
|
| 2080 | + } |
|
| 2081 | + |
|
| 2082 | + /** |
|
| 2083 | + * Test locks for fopen with fclose at the end |
|
| 2084 | + * |
|
| 2085 | + * |
|
| 2086 | + * @param string $operation operation name on the view |
|
| 2087 | + * @param array $operationArgs arguments for the operation |
|
| 2088 | + * @param string $path path of the locked item to check |
|
| 2089 | + */ |
|
| 2090 | + #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 2091 | + public function testLockBasicOperationUnlocksAfterException( |
|
| 2092 | + $operation, |
|
| 2093 | + $operationArgs, |
|
| 2094 | + $path, |
|
| 2095 | + ): void { |
|
| 2096 | + if ($operation === 'touch') { |
|
| 2097 | + $this->markTestSkipped('touch handles storage exceptions internally'); |
|
| 2098 | + } |
|
| 2099 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2100 | + |
|
| 2101 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2102 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2103 | + ->onlyMethods([$operation]) |
|
| 2104 | + ->getMock(); |
|
| 2105 | + |
|
| 2106 | + /* Pause trash to avoid the trashbin intercepting rmdir and unlink calls */ |
|
| 2107 | + Server::get(ITrashManager::class)->pauseTrash(); |
|
| 2108 | + /* Same thing with encryption wrapper */ |
|
| 2109 | + Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2110 | + |
|
| 2111 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2112 | + |
|
| 2113 | + // work directly on disk because mkdir might be mocked |
|
| 2114 | + $realPath = $storage->getSourcePath(''); |
|
| 2115 | + mkdir($realPath . '/files'); |
|
| 2116 | + mkdir($realPath . '/files/dir'); |
|
| 2117 | + file_put_contents($realPath . '/files/test.txt', 'blah'); |
|
| 2118 | + $storage->getScanner()->scan('files'); |
|
| 2119 | + |
|
| 2120 | + $storage->expects($this->once()) |
|
| 2121 | + ->method($operation) |
|
| 2122 | + ->willReturnCallback( |
|
| 2123 | + function (): void { |
|
| 2124 | + throw new \Exception('Simulated exception'); |
|
| 2125 | + } |
|
| 2126 | + ); |
|
| 2127 | + |
|
| 2128 | + $thrown = false; |
|
| 2129 | + try { |
|
| 2130 | + call_user_func_array([$view, $operation], $operationArgs); |
|
| 2131 | + } catch (\Exception $e) { |
|
| 2132 | + $thrown = true; |
|
| 2133 | + $this->assertEquals('Simulated exception', $e->getMessage()); |
|
| 2134 | + } |
|
| 2135 | + $this->assertTrue($thrown, 'Exception was rethrown'); |
|
| 2136 | + $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception'); |
|
| 2137 | + |
|
| 2138 | + /* Resume trash to avoid side effects */ |
|
| 2139 | + Server::get(ITrashManager::class)->resumeTrash(); |
|
| 2140 | + } |
|
| 2141 | + |
|
| 2142 | + public function testLockBasicOperationUnlocksAfterLockException(): void { |
|
| 2143 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2144 | + |
|
| 2145 | + $storage = new Temporary([]); |
|
| 2146 | + |
|
| 2147 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2148 | + |
|
| 2149 | + $storage->mkdir('files'); |
|
| 2150 | + $storage->mkdir('files/dir'); |
|
| 2151 | + $storage->file_put_contents('files/test.txt', 'blah'); |
|
| 2152 | + $storage->getScanner()->scan('files'); |
|
| 2153 | + |
|
| 2154 | + // get a shared lock |
|
| 2155 | + $handle = $view->fopen('test.txt', 'r'); |
|
| 2156 | + |
|
| 2157 | + $thrown = false; |
|
| 2158 | + try { |
|
| 2159 | + // try (and fail) to get a write lock |
|
| 2160 | + $view->unlink('test.txt'); |
|
| 2161 | + } catch (\Exception $e) { |
|
| 2162 | + $thrown = true; |
|
| 2163 | + $this->assertInstanceOf(LockedException::class, $e); |
|
| 2164 | + } |
|
| 2165 | + $this->assertTrue($thrown, 'Exception was rethrown'); |
|
| 2166 | + |
|
| 2167 | + // clean shared lock |
|
| 2168 | + fclose($handle); |
|
| 2169 | + |
|
| 2170 | + $this->assertNull($this->getFileLockType($view, 'test.txt'), 'File got unlocked'); |
|
| 2171 | + } |
|
| 2172 | + |
|
| 2173 | + /** |
|
| 2174 | + * Test locks for fopen with fclose at the end |
|
| 2175 | + * |
|
| 2176 | + * |
|
| 2177 | + * @param string $operation operation name on the view |
|
| 2178 | + * @param array $operationArgs arguments for the operation |
|
| 2179 | + * @param string $path path of the locked item to check |
|
| 2180 | + * @param string $hookType hook type |
|
| 2181 | + */ |
|
| 2182 | + #[\PHPUnit\Framework\Attributes\DataProvider('basicOperationProviderForLocks')] |
|
| 2183 | + public function testLockBasicOperationUnlocksAfterCancelledHook( |
|
| 2184 | + $operation, |
|
| 2185 | + $operationArgs, |
|
| 2186 | + $path, |
|
| 2187 | + $hookType, |
|
| 2188 | + ): void { |
|
| 2189 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2190 | + |
|
| 2191 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2192 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2193 | + ->onlyMethods([$operation]) |
|
| 2194 | + ->getMock(); |
|
| 2195 | + |
|
| 2196 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2197 | + $storage->mkdir('files'); |
|
| 2198 | + |
|
| 2199 | + Util::connectHook( |
|
| 2200 | + Filesystem::CLASSNAME, |
|
| 2201 | + $hookType, |
|
| 2202 | + HookHelper::class, |
|
| 2203 | + 'cancellingCallback' |
|
| 2204 | + ); |
|
| 2205 | + |
|
| 2206 | + call_user_func_array([$view, $operation], $operationArgs); |
|
| 2207 | + |
|
| 2208 | + $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception'); |
|
| 2209 | + } |
|
| 2210 | + |
|
| 2211 | + public static function lockFileRenameOrCopyDataProvider(): array { |
|
| 2212 | + return [ |
|
| 2213 | + ['rename', ILockingProvider::LOCK_EXCLUSIVE], |
|
| 2214 | + ['copy', ILockingProvider::LOCK_SHARED], |
|
| 2215 | + ]; |
|
| 2216 | + } |
|
| 2217 | + |
|
| 2218 | + /** |
|
| 2219 | + * Test locks for rename or copy operation |
|
| 2220 | + * |
|
| 2221 | + * |
|
| 2222 | + * @param string $operation operation to be done on the view |
|
| 2223 | + * @param int $expectedLockTypeSourceDuring expected lock type on source file during |
|
| 2224 | + * the operation |
|
| 2225 | + */ |
|
| 2226 | + #[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyDataProvider')] |
|
| 2227 | + public function testLockFileRename($operation, $expectedLockTypeSourceDuring): void { |
|
| 2228 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2229 | + |
|
| 2230 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2231 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2232 | + ->onlyMethods([$operation, 'getMetaData', 'filemtime']) |
|
| 2233 | + ->getMock(); |
|
| 2234 | + |
|
| 2235 | + $storage->expects($this->any()) |
|
| 2236 | + ->method('getMetaData') |
|
| 2237 | + ->willReturn([ |
|
| 2238 | + 'mtime' => 1885434487, |
|
| 2239 | + 'etag' => '', |
|
| 2240 | + 'mimetype' => 'text/plain', |
|
| 2241 | + 'permissions' => Constants::PERMISSION_ALL, |
|
| 2242 | + 'size' => 3 |
|
| 2243 | + ]); |
|
| 2244 | + $storage->expects($this->any()) |
|
| 2245 | + ->method('filemtime') |
|
| 2246 | + ->willReturn(123456789); |
|
| 2247 | + |
|
| 2248 | + $sourcePath = 'original.txt'; |
|
| 2249 | + $targetPath = 'target.txt'; |
|
| 2250 | + |
|
| 2251 | + /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2252 | + Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2253 | + |
|
| 2254 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2255 | + $storage->mkdir('files'); |
|
| 2256 | + $view->file_put_contents($sourcePath, 'meh'); |
|
| 2257 | + |
|
| 2258 | + $storage->expects($this->once()) |
|
| 2259 | + ->method($operation) |
|
| 2260 | + ->willReturnCallback( |
|
| 2261 | + function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) { |
|
| 2262 | + $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath); |
|
| 2263 | + $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath); |
|
| 2264 | + |
|
| 2265 | + return true; |
|
| 2266 | + } |
|
| 2267 | + ); |
|
| 2268 | + |
|
| 2269 | + $this->connectMockHooks($operation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2270 | + $this->connectMockHooks($operation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2271 | + |
|
| 2272 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2273 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2274 | + |
|
| 2275 | + $view->$operation($sourcePath, $targetPath); |
|
| 2276 | + |
|
| 2277 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook'); |
|
| 2278 | + $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation'); |
|
| 2279 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook'); |
|
| 2280 | + |
|
| 2281 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook'); |
|
| 2282 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation'); |
|
| 2283 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook'); |
|
| 2284 | + |
|
| 2285 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2286 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2287 | + } |
|
| 2288 | + |
|
| 2289 | + /** |
|
| 2290 | + * simulate a failed copy operation. |
|
| 2291 | + * We expect that we catch the exception, free the lock and re-throw it. |
|
| 2292 | + * |
|
| 2293 | + */ |
|
| 2294 | + public function testLockFileCopyException(): void { |
|
| 2295 | + $this->expectException(\Exception::class); |
|
| 2296 | + |
|
| 2297 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2298 | + |
|
| 2299 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2300 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2301 | + ->onlyMethods(['copy']) |
|
| 2302 | + ->getMock(); |
|
| 2303 | + |
|
| 2304 | + $sourcePath = 'original.txt'; |
|
| 2305 | + $targetPath = 'target.txt'; |
|
| 2306 | + |
|
| 2307 | + /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2308 | + Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2309 | + |
|
| 2310 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2311 | + $storage->mkdir('files'); |
|
| 2312 | + $view->file_put_contents($sourcePath, 'meh'); |
|
| 2313 | + |
|
| 2314 | + $storage->expects($this->once()) |
|
| 2315 | + ->method('copy') |
|
| 2316 | + ->willReturnCallback( |
|
| 2317 | + function (): void { |
|
| 2318 | + throw new \Exception(); |
|
| 2319 | + } |
|
| 2320 | + ); |
|
| 2321 | + |
|
| 2322 | + $this->connectMockHooks('copy', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2323 | + $this->connectMockHooks('copy', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2324 | + |
|
| 2325 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2326 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2327 | + |
|
| 2328 | + try { |
|
| 2329 | + $view->copy($sourcePath, $targetPath); |
|
| 2330 | + } catch (\Exception $e) { |
|
| 2331 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2332 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2333 | + throw $e; |
|
| 2334 | + } |
|
| 2335 | + } |
|
| 2336 | + |
|
| 2337 | + /** |
|
| 2338 | + * Test rename operation: unlock first path when second path was locked |
|
| 2339 | + */ |
|
| 2340 | + public function testLockFileRenameUnlockOnException(): void { |
|
| 2341 | + self::loginAsUser('test'); |
|
| 2342 | + |
|
| 2343 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2344 | + |
|
| 2345 | + $sourcePath = 'original.txt'; |
|
| 2346 | + $targetPath = 'target.txt'; |
|
| 2347 | + $view->file_put_contents($sourcePath, 'meh'); |
|
| 2348 | + |
|
| 2349 | + // simulate that the target path is already locked |
|
| 2350 | + $view->lockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 2351 | + |
|
| 2352 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2353 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file is locked before operation'); |
|
| 2354 | + |
|
| 2355 | + $thrown = false; |
|
| 2356 | + try { |
|
| 2357 | + $view->rename($sourcePath, $targetPath); |
|
| 2358 | + } catch (LockedException $e) { |
|
| 2359 | + $thrown = true; |
|
| 2360 | + } |
|
| 2361 | + |
|
| 2362 | + $this->assertTrue($thrown, 'LockedException thrown'); |
|
| 2363 | + |
|
| 2364 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2365 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file still locked after operation'); |
|
| 2366 | + |
|
| 2367 | + $view->unlockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 2368 | + } |
|
| 2369 | + |
|
| 2370 | + /** |
|
| 2371 | + * Test rename operation: unlock first path when second path was locked |
|
| 2372 | + */ |
|
| 2373 | + public function testGetOwner(): void { |
|
| 2374 | + self::loginAsUser('test'); |
|
| 2375 | + |
|
| 2376 | + $view = new View('/test/files/'); |
|
| 2377 | + |
|
| 2378 | + $path = 'foo.txt'; |
|
| 2379 | + $view->file_put_contents($path, 'meh'); |
|
| 2380 | + |
|
| 2381 | + $this->assertEquals('test', $view->getFileInfo($path)->getOwner()->getUID()); |
|
| 2382 | + |
|
| 2383 | + $folderInfo = $view->getDirectoryContent(''); |
|
| 2384 | + $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) { |
|
| 2385 | + return $info->getName() === 'foo.txt'; |
|
| 2386 | + })); |
|
| 2387 | + |
|
| 2388 | + $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID()); |
|
| 2389 | + |
|
| 2390 | + $subStorage = new Temporary(); |
|
| 2391 | + Filesystem::mount($subStorage, [], '/test/files/asd'); |
|
| 2392 | + |
|
| 2393 | + $folderInfo = $view->getDirectoryContent(''); |
|
| 2394 | + $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) { |
|
| 2395 | + return $info->getName() === 'asd'; |
|
| 2396 | + })); |
|
| 2397 | + |
|
| 2398 | + $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID()); |
|
| 2399 | + } |
|
| 2400 | + |
|
| 2401 | + public static function lockFileRenameOrCopyCrossStorageDataProvider(): array { |
|
| 2402 | + return [ |
|
| 2403 | + ['rename', 'moveFromStorage', ILockingProvider::LOCK_EXCLUSIVE], |
|
| 2404 | + ['copy', 'copyFromStorage', ILockingProvider::LOCK_SHARED], |
|
| 2405 | + ]; |
|
| 2406 | + } |
|
| 2407 | + |
|
| 2408 | + /** |
|
| 2409 | + * Test locks for rename or copy operation cross-storage |
|
| 2410 | + * |
|
| 2411 | + * |
|
| 2412 | + * @param string $viewOperation operation to be done on the view |
|
| 2413 | + * @param string $storageOperation operation to be mocked on the storage |
|
| 2414 | + * @param int $expectedLockTypeSourceDuring expected lock type on source file during |
|
| 2415 | + * the operation |
|
| 2416 | + */ |
|
| 2417 | + #[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyCrossStorageDataProvider')] |
|
| 2418 | + public function testLockFileRenameCrossStorage($viewOperation, $storageOperation, $expectedLockTypeSourceDuring): void { |
|
| 2419 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2420 | + |
|
| 2421 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */ |
|
| 2422 | + $storage = $this->getMockBuilder(Temporary::class) |
|
| 2423 | + ->onlyMethods([$storageOperation]) |
|
| 2424 | + ->getMock(); |
|
| 2425 | + /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage2 */ |
|
| 2426 | + $storage2 = $this->getMockBuilder(Temporary::class) |
|
| 2427 | + ->onlyMethods([$storageOperation, 'getMetaData', 'filemtime']) |
|
| 2428 | + ->getMock(); |
|
| 2429 | + |
|
| 2430 | + $storage2->expects($this->any()) |
|
| 2431 | + ->method('getMetaData') |
|
| 2432 | + ->willReturn([ |
|
| 2433 | + 'mtime' => 1885434487, |
|
| 2434 | + 'etag' => '', |
|
| 2435 | + 'mimetype' => 'text/plain', |
|
| 2436 | + 'permissions' => Constants::PERMISSION_ALL, |
|
| 2437 | + 'size' => 3 |
|
| 2438 | + ]); |
|
| 2439 | + $storage2->expects($this->any()) |
|
| 2440 | + ->method('filemtime') |
|
| 2441 | + ->willReturn(123456789); |
|
| 2442 | + |
|
| 2443 | + $sourcePath = 'original.txt'; |
|
| 2444 | + $targetPath = 'substorage/target.txt'; |
|
| 2445 | + |
|
| 2446 | + /* Disable encryption wrapper to avoid it intercepting mocked call */ |
|
| 2447 | + Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption'); |
|
| 2448 | + |
|
| 2449 | + Filesystem::mount($storage, [], $this->user . '/'); |
|
| 2450 | + Filesystem::mount($storage2, [], $this->user . '/files/substorage'); |
|
| 2451 | + $storage->mkdir('files'); |
|
| 2452 | + $view->file_put_contents($sourcePath, 'meh'); |
|
| 2453 | + $storage2->getUpdater()->update(''); |
|
| 2454 | + |
|
| 2455 | + $storage->expects($this->never()) |
|
| 2456 | + ->method($storageOperation); |
|
| 2457 | + $storage2->expects($this->once()) |
|
| 2458 | + ->method($storageOperation) |
|
| 2459 | + ->willReturnCallback( |
|
| 2460 | + function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) { |
|
| 2461 | + $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath); |
|
| 2462 | + $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath); |
|
| 2463 | + |
|
| 2464 | + return true; |
|
| 2465 | + } |
|
| 2466 | + ); |
|
| 2467 | + |
|
| 2468 | + $this->connectMockHooks($viewOperation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost); |
|
| 2469 | + $this->connectMockHooks($viewOperation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost); |
|
| 2470 | + |
|
| 2471 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation'); |
|
| 2472 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation'); |
|
| 2473 | + |
|
| 2474 | + $view->$viewOperation($sourcePath, $targetPath); |
|
| 2475 | + |
|
| 2476 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook'); |
|
| 2477 | + $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation'); |
|
| 2478 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook'); |
|
| 2479 | + |
|
| 2480 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook'); |
|
| 2481 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation'); |
|
| 2482 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook'); |
|
| 2483 | + |
|
| 2484 | + $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation'); |
|
| 2485 | + $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation'); |
|
| 2486 | + } |
|
| 2487 | + |
|
| 2488 | + /** |
|
| 2489 | + * Test locks when moving a mount point |
|
| 2490 | + */ |
|
| 2491 | + public function testLockMoveMountPoint(): void { |
|
| 2492 | + self::loginAsUser('test'); |
|
| 2493 | + |
|
| 2494 | + [$mount] = $this->createTestMovableMountPoints([ |
|
| 2495 | + $this->user . '/files/substorage', |
|
| 2496 | + ]); |
|
| 2497 | + |
|
| 2498 | + $view = new View('/' . $this->user . '/files/'); |
|
| 2499 | + $view->mkdir('subdir'); |
|
| 2500 | + |
|
| 2501 | + $sourcePath = 'substorage'; |
|
| 2502 | + $targetPath = 'subdir/substorage_moved'; |
|
| 2503 | + |
|
| 2504 | + $mount->expects($this->once()) |
|
| 2505 | + ->method('moveMount') |
|
| 2506 | + ->willReturnCallback( |
|
| 2507 | + function ($target) use ($mount, $view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring, &$lockTypeSharedRootDuring) { |
|
| 2508 | + $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath, true); |
|
| 2509 | + $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath, true); |
|
| 2510 | + |
|
| 2511 | + $lockTypeSharedRootDuring = $this->getFileLockType($view, $sourcePath, false); |
|
| 2512 | + |
|
| 2513 | + $mount->setMountPoint($target); |
|
| 2514 | + |
|
| 2515 | + return true; |
|
| 2516 | + } |
|
| 2517 | + ); |
|
| 2518 | + |
|
| 2519 | + $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost, true); |
|
| 2520 | + $this->connectMockHooks('rename', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost, true); |
|
| 2521 | + // in pre-hook, mount point is still on $sourcePath |
|
| 2522 | + $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSharedRootPre, $dummy, false); |
|
| 2523 | + // in post-hook, mount point is now on $targetPath |
|
| 2524 | + $this->connectMockHooks('rename', $view, $targetPath, $dummy, $lockTypeSharedRootPost, false); |
|
| 2525 | + |
|
| 2526 | + $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked before operation'); |
|
| 2527 | + $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked before operation'); |
|
| 2528 | + $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked before operation'); |
|
| 2529 | + |
|
| 2530 | + $view->rename($sourcePath, $targetPath); |
|
| 2531 | + |
|
| 2532 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source path locked properly during pre-hook'); |
|
| 2533 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeSourceDuring, 'Source path locked properly during operation'); |
|
| 2534 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source path locked properly during post-hook'); |
|
| 2535 | + |
|
| 2536 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target path locked properly during pre-hook'); |
|
| 2537 | + $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target path locked properly during operation'); |
|
| 2538 | + $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target path locked properly during post-hook'); |
|
| 2539 | + |
|
| 2540 | + $this->assertNull($lockTypeSharedRootPre, 'Shared storage root not locked during pre-hook'); |
|
| 2541 | + $this->assertNull($lockTypeSharedRootDuring, 'Shared storage root not locked during move'); |
|
| 2542 | + $this->assertNull($lockTypeSharedRootPost, 'Shared storage root not locked during post-hook'); |
|
| 2543 | + |
|
| 2544 | + $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked after operation'); |
|
| 2545 | + $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked after operation'); |
|
| 2546 | + $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked after operation'); |
|
| 2547 | + } |
|
| 2548 | + |
|
| 2549 | + /** |
|
| 2550 | + * Connect hook callbacks for hook type |
|
| 2551 | + * |
|
| 2552 | + * @param string $hookType hook type or null for none |
|
| 2553 | + * @param View $view view to check the lock on |
|
| 2554 | + * @param string $path path for which to check the lock |
|
| 2555 | + * @param int $lockTypePre variable to receive lock type that was active in the pre-hook |
|
| 2556 | + * @param int $lockTypePost variable to receive lock type that was active in the post-hook |
|
| 2557 | + * @param bool $onMountPoint true to check the mount point instead of the |
|
| 2558 | + * mounted storage |
|
| 2559 | + */ |
|
| 2560 | + private function connectMockHooks($hookType, $view, $path, &$lockTypePre, &$lockTypePost, $onMountPoint = false) { |
|
| 2561 | + if ($hookType === null) { |
|
| 2562 | + return; |
|
| 2563 | + } |
|
| 2564 | + |
|
| 2565 | + $eventHandler = $this->getMockBuilder(TestEventHandler::class) |
|
| 2566 | + ->onlyMethods(['preCallback', 'postCallback']) |
|
| 2567 | + ->getMock(); |
|
| 2568 | + |
|
| 2569 | + $eventHandler->expects($this->any()) |
|
| 2570 | + ->method('preCallback') |
|
| 2571 | + ->willReturnCallback( |
|
| 2572 | + function () use ($view, $path, $onMountPoint, &$lockTypePre): void { |
|
| 2573 | + $lockTypePre = $this->getFileLockType($view, $path, $onMountPoint); |
|
| 2574 | + } |
|
| 2575 | + ); |
|
| 2576 | + $eventHandler->expects($this->any()) |
|
| 2577 | + ->method('postCallback') |
|
| 2578 | + ->willReturnCallback( |
|
| 2579 | + function () use ($view, $path, $onMountPoint, &$lockTypePost): void { |
|
| 2580 | + $lockTypePost = $this->getFileLockType($view, $path, $onMountPoint); |
|
| 2581 | + } |
|
| 2582 | + ); |
|
| 2583 | + |
|
| 2584 | + if ($hookType !== null) { |
|
| 2585 | + Util::connectHook( |
|
| 2586 | + Filesystem::CLASSNAME, |
|
| 2587 | + $hookType, |
|
| 2588 | + $eventHandler, |
|
| 2589 | + 'preCallback' |
|
| 2590 | + ); |
|
| 2591 | + Util::connectHook( |
|
| 2592 | + Filesystem::CLASSNAME, |
|
| 2593 | + 'post_' . $hookType, |
|
| 2594 | + $eventHandler, |
|
| 2595 | + 'postCallback' |
|
| 2596 | + ); |
|
| 2597 | + } |
|
| 2598 | + } |
|
| 2599 | + |
|
| 2600 | + /** |
|
| 2601 | + * Returns the file lock type |
|
| 2602 | + * |
|
| 2603 | + * @param View $view view |
|
| 2604 | + * @param string $path path |
|
| 2605 | + * @param bool $onMountPoint true to check the mount point instead of the |
|
| 2606 | + * mounted storage |
|
| 2607 | + * |
|
| 2608 | + * @return int lock type or null if file was not locked |
|
| 2609 | + */ |
|
| 2610 | + private function getFileLockType(View $view, $path, $onMountPoint = false) { |
|
| 2611 | + if ($this->isFileLocked($view, $path, ILockingProvider::LOCK_EXCLUSIVE, $onMountPoint)) { |
|
| 2612 | + return ILockingProvider::LOCK_EXCLUSIVE; |
|
| 2613 | + } elseif ($this->isFileLocked($view, $path, ILockingProvider::LOCK_SHARED, $onMountPoint)) { |
|
| 2614 | + return ILockingProvider::LOCK_SHARED; |
|
| 2615 | + } |
|
| 2616 | + return null; |
|
| 2617 | + } |
|
| 2618 | + |
|
| 2619 | + |
|
| 2620 | + public function testRemoveMoveableMountPoint(): void { |
|
| 2621 | + $mountPoint = '/' . $this->user . '/files/mount/'; |
|
| 2622 | + |
|
| 2623 | + // Mock the mount point |
|
| 2624 | + /** @var TestMoveableMountPoint|\PHPUnit\Framework\MockObject\MockObject $mount */ |
|
| 2625 | + $mount = $this->createMock(TestMoveableMountPoint::class); |
|
| 2626 | + $mount->expects($this->once()) |
|
| 2627 | + ->method('getMountPoint') |
|
| 2628 | + ->willReturn($mountPoint); |
|
| 2629 | + $mount->expects($this->once()) |
|
| 2630 | + ->method('removeMount') |
|
| 2631 | + ->willReturn('foo'); |
|
| 2632 | + $mount->expects($this->any()) |
|
| 2633 | + ->method('getInternalPath') |
|
| 2634 | + ->willReturn(''); |
|
| 2635 | + |
|
| 2636 | + // Register mount |
|
| 2637 | + Filesystem::getMountManager()->addMount($mount); |
|
| 2638 | + |
|
| 2639 | + // Listen for events |
|
| 2640 | + $eventHandler = $this->getMockBuilder(TestEventHandler::class) |
|
| 2641 | + ->onlyMethods(['umount', 'post_umount']) |
|
| 2642 | + ->getMock(); |
|
| 2643 | + $eventHandler->expects($this->once()) |
|
| 2644 | + ->method('umount') |
|
| 2645 | + ->with([Filesystem::signal_param_path => '/mount']); |
|
| 2646 | + $eventHandler->expects($this->once()) |
|
| 2647 | + ->method('post_umount') |
|
| 2648 | + ->with([Filesystem::signal_param_path => '/mount']); |
|
| 2649 | + Util::connectHook( |
|
| 2650 | + Filesystem::CLASSNAME, |
|
| 2651 | + 'umount', |
|
| 2652 | + $eventHandler, |
|
| 2653 | + 'umount' |
|
| 2654 | + ); |
|
| 2655 | + Util::connectHook( |
|
| 2656 | + Filesystem::CLASSNAME, |
|
| 2657 | + 'post_umount', |
|
| 2658 | + $eventHandler, |
|
| 2659 | + 'post_umount' |
|
| 2660 | + ); |
|
| 2661 | + |
|
| 2662 | + //Delete the mountpoint |
|
| 2663 | + $view = new View('/' . $this->user . '/files'); |
|
| 2664 | + $this->assertEquals('foo', $view->rmdir('mount')); |
|
| 2665 | + } |
|
| 2666 | + |
|
| 2667 | + public static function mimeFilterProvider(): array { |
|
| 2668 | + return [ |
|
| 2669 | + [null, ['test1.txt', 'test2.txt', 'test3.md', 'test4.png']], |
|
| 2670 | + ['text/plain', ['test1.txt', 'test2.txt']], |
|
| 2671 | + ['text/markdown', ['test3.md']], |
|
| 2672 | + ['text', ['test1.txt', 'test2.txt', 'test3.md']], |
|
| 2673 | + ]; |
|
| 2674 | + } |
|
| 2675 | + |
|
| 2676 | + /** |
|
| 2677 | + * @param string $filter |
|
| 2678 | + * @param string[] $expected |
|
| 2679 | + */ |
|
| 2680 | + #[\PHPUnit\Framework\Attributes\DataProvider('mimeFilterProvider')] |
|
| 2681 | + public function testGetDirectoryContentMimeFilter($filter, $expected): void { |
|
| 2682 | + $storage1 = new Temporary(); |
|
| 2683 | + $root = self::getUniqueID('/'); |
|
| 2684 | + Filesystem::mount($storage1, [], $root . '/'); |
|
| 2685 | + $view = new View($root); |
|
| 2686 | + |
|
| 2687 | + $view->file_put_contents('test1.txt', 'asd'); |
|
| 2688 | + $view->file_put_contents('test2.txt', 'asd'); |
|
| 2689 | + $view->file_put_contents('test3.md', 'asd'); |
|
| 2690 | + $view->file_put_contents('test4.png', ''); |
|
| 2691 | + |
|
| 2692 | + $content = $view->getDirectoryContent('', $filter); |
|
| 2693 | + |
|
| 2694 | + $files = array_map(function (FileInfo $info) { |
|
| 2695 | + return $info->getName(); |
|
| 2696 | + }, $content); |
|
| 2697 | + sort($files); |
|
| 2698 | + |
|
| 2699 | + $this->assertEquals($expected, $files); |
|
| 2700 | + } |
|
| 2701 | + |
|
| 2702 | + public function testFilePutContentsClearsChecksum(): void { |
|
| 2703 | + $storage = new Temporary([]); |
|
| 2704 | + $scanner = $storage->getScanner(); |
|
| 2705 | + $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2706 | + Filesystem::mount($storage, [], '/test/'); |
|
| 2707 | + $scanner->scan(''); |
|
| 2708 | + |
|
| 2709 | + $view = new View('/test/foo.txt'); |
|
| 2710 | + $view->putFileInfo('.', ['checksum' => '42']); |
|
| 2711 | + |
|
| 2712 | + $this->assertEquals('bar', $view->file_get_contents('')); |
|
| 2713 | + $fh = tmpfile(); |
|
| 2714 | + fwrite($fh, 'fooo'); |
|
| 2715 | + rewind($fh); |
|
| 2716 | + clearstatcache(); |
|
| 2717 | + $view->file_put_contents('', $fh); |
|
| 2718 | + $this->assertEquals('fooo', $view->file_get_contents('')); |
|
| 2719 | + $data = $view->getFileInfo('.'); |
|
| 2720 | + $this->assertEquals('', $data->getChecksum()); |
|
| 2721 | + } |
|
| 2722 | + |
|
| 2723 | + public function testDeleteGhostFile(): void { |
|
| 2724 | + $storage = new Temporary([]); |
|
| 2725 | + $scanner = $storage->getScanner(); |
|
| 2726 | + $cache = $storage->getCache(); |
|
| 2727 | + $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2728 | + Filesystem::mount($storage, [], '/test/'); |
|
| 2729 | + $scanner->scan(''); |
|
| 2730 | + |
|
| 2731 | + $storage->unlink('foo.txt'); |
|
| 2732 | + |
|
| 2733 | + $this->assertTrue($cache->inCache('foo.txt')); |
|
| 2734 | + |
|
| 2735 | + $view = new View('/test'); |
|
| 2736 | + $rootInfo = $view->getFileInfo(''); |
|
| 2737 | + $this->assertEquals(3, $rootInfo->getSize()); |
|
| 2738 | + $view->unlink('foo.txt'); |
|
| 2739 | + $newInfo = $view->getFileInfo(''); |
|
| 2740 | + |
|
| 2741 | + $this->assertFalse($cache->inCache('foo.txt')); |
|
| 2742 | + $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag()); |
|
| 2743 | + $this->assertEquals(0, $newInfo->getSize()); |
|
| 2744 | + } |
|
| 2745 | + |
|
| 2746 | + public function testDeleteGhostFolder(): void { |
|
| 2747 | + $storage = new Temporary([]); |
|
| 2748 | + $scanner = $storage->getScanner(); |
|
| 2749 | + $cache = $storage->getCache(); |
|
| 2750 | + $storage->mkdir('foo'); |
|
| 2751 | + $storage->file_put_contents('foo/foo.txt', 'bar'); |
|
| 2752 | + Filesystem::mount($storage, [], '/test/'); |
|
| 2753 | + $scanner->scan(''); |
|
| 2754 | + |
|
| 2755 | + $storage->rmdir('foo'); |
|
| 2756 | + |
|
| 2757 | + $this->assertTrue($cache->inCache('foo')); |
|
| 2758 | + $this->assertTrue($cache->inCache('foo/foo.txt')); |
|
| 2759 | + |
|
| 2760 | + $view = new View('/test'); |
|
| 2761 | + $rootInfo = $view->getFileInfo(''); |
|
| 2762 | + $this->assertEquals(3, $rootInfo->getSize()); |
|
| 2763 | + $view->rmdir('foo'); |
|
| 2764 | + $newInfo = $view->getFileInfo(''); |
|
| 2765 | + |
|
| 2766 | + $this->assertFalse($cache->inCache('foo')); |
|
| 2767 | + $this->assertFalse($cache->inCache('foo/foo.txt')); |
|
| 2768 | + $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag()); |
|
| 2769 | + $this->assertEquals(0, $newInfo->getSize()); |
|
| 2770 | + } |
|
| 2771 | + |
|
| 2772 | + public function testCreateParentDirectories(): void { |
|
| 2773 | + $view = $this->getMockBuilder(View::class) |
|
| 2774 | + ->disableOriginalConstructor() |
|
| 2775 | + ->onlyMethods([ |
|
| 2776 | + 'is_file', |
|
| 2777 | + 'file_exists', |
|
| 2778 | + 'mkdir', |
|
| 2779 | + ]) |
|
| 2780 | + ->getMock(); |
|
| 2781 | + |
|
| 2782 | + $view->expects($this->exactly(3)) |
|
| 2783 | + ->method('is_file') |
|
| 2784 | + ->willReturnMap([ |
|
| 2785 | + ['/new', false], |
|
| 2786 | + ['/new/folder', false], |
|
| 2787 | + ['/new/folder/structure', false], |
|
| 2788 | + ]); |
|
| 2789 | + $view->expects($this->exactly(3)) |
|
| 2790 | + ->method('file_exists') |
|
| 2791 | + ->willReturnMap([ |
|
| 2792 | + ['/new', true], |
|
| 2793 | + ['/new/folder', false], |
|
| 2794 | + ['/new/folder/structure', false], |
|
| 2795 | + ]); |
|
| 2796 | + |
|
| 2797 | + $calls = ['/new/folder', '/new/folder/structure']; |
|
| 2798 | + $view->expects($this->exactly(2)) |
|
| 2799 | + ->method('mkdir') |
|
| 2800 | + ->willReturnCallback(function ($dir) use (&$calls): void { |
|
| 2801 | + $expected = array_shift($calls); |
|
| 2802 | + $this->assertEquals($expected, $dir); |
|
| 2803 | + }); |
|
| 2804 | + |
|
| 2805 | + $this->assertTrue(self::invokePrivate($view, 'createParentDirectories', ['/new/folder/structure'])); |
|
| 2806 | + } |
|
| 2807 | + |
|
| 2808 | + public function testCreateParentDirectoriesWithExistingFile(): void { |
|
| 2809 | + $view = $this->getMockBuilder(View::class) |
|
| 2810 | + ->disableOriginalConstructor() |
|
| 2811 | + ->onlyMethods([ |
|
| 2812 | + 'is_file', |
|
| 2813 | + 'file_exists', |
|
| 2814 | + 'mkdir', |
|
| 2815 | + ]) |
|
| 2816 | + ->getMock(); |
|
| 2817 | + |
|
| 2818 | + $view |
|
| 2819 | + ->expects($this->once()) |
|
| 2820 | + ->method('is_file') |
|
| 2821 | + ->with('/file.txt') |
|
| 2822 | + ->willReturn(true); |
|
| 2823 | + $this->assertFalse(self::invokePrivate($view, 'createParentDirectories', ['/file.txt/folder/structure'])); |
|
| 2824 | + } |
|
| 2825 | + |
|
| 2826 | + public function testCacheExtension(): void { |
|
| 2827 | + $storage = new Temporary([]); |
|
| 2828 | + $scanner = $storage->getScanner(); |
|
| 2829 | + $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2830 | + $scanner->scan(''); |
|
| 2831 | + |
|
| 2832 | + Filesystem::mount($storage, [], '/test/'); |
|
| 2833 | + $view = new View('/test'); |
|
| 2834 | + |
|
| 2835 | + $info = $view->getFileInfo('/foo.txt'); |
|
| 2836 | + $this->assertEquals(0, $info->getUploadTime()); |
|
| 2837 | + $this->assertEquals(0, $info->getCreationTime()); |
|
| 2838 | + |
|
| 2839 | + $view->putFileInfo('/foo.txt', ['upload_time' => 25]); |
|
| 2840 | + |
|
| 2841 | + $info = $view->getFileInfo('/foo.txt'); |
|
| 2842 | + $this->assertEquals(25, $info->getUploadTime()); |
|
| 2843 | + $this->assertEquals(0, $info->getCreationTime()); |
|
| 2844 | + } |
|
| 2845 | + |
|
| 2846 | + public function testFopenGone(): void { |
|
| 2847 | + $storage = new Temporary([]); |
|
| 2848 | + $scanner = $storage->getScanner(); |
|
| 2849 | + $storage->file_put_contents('foo.txt', 'bar'); |
|
| 2850 | + $scanner->scan(''); |
|
| 2851 | + $cache = $storage->getCache(); |
|
| 2852 | + |
|
| 2853 | + Filesystem::mount($storage, [], '/test/'); |
|
| 2854 | + $view = new View('/test'); |
|
| 2855 | + |
|
| 2856 | + $storage->unlink('foo.txt'); |
|
| 2857 | + |
|
| 2858 | + $this->assertTrue($cache->inCache('foo.txt')); |
|
| 2859 | + |
|
| 2860 | + $this->assertFalse($view->fopen('foo.txt', 'r')); |
|
| 2861 | + |
|
| 2862 | + $this->assertFalse($cache->inCache('foo.txt')); |
|
| 2863 | + } |
|
| 2864 | + |
|
| 2865 | + public function testMountpointParentsCreated(): void { |
|
| 2866 | + $storage1 = $this->getTestStorage(); |
|
| 2867 | + Filesystem::mount($storage1, [], '/'); |
|
| 2868 | + |
|
| 2869 | + $storage2 = $this->getTestStorage(); |
|
| 2870 | + Filesystem::mount($storage2, [], '/A/B/C'); |
|
| 2871 | + |
|
| 2872 | + $rootView = new View(''); |
|
| 2873 | + |
|
| 2874 | + $folderData = $rootView->getDirectoryContent('/'); |
|
| 2875 | + $this->assertCount(4, $folderData); |
|
| 2876 | + $this->assertEquals('folder', $folderData[0]['name']); |
|
| 2877 | + $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 2878 | + $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 2879 | + $this->assertEquals('A', $folderData[3]['name']); |
|
| 2880 | + |
|
| 2881 | + $folderData = $rootView->getDirectoryContent('/A'); |
|
| 2882 | + $this->assertCount(1, $folderData); |
|
| 2883 | + $this->assertEquals('B', $folderData[0]['name']); |
|
| 2884 | + |
|
| 2885 | + $folderData = $rootView->getDirectoryContent('/A/B'); |
|
| 2886 | + $this->assertCount(1, $folderData); |
|
| 2887 | + $this->assertEquals('C', $folderData[0]['name']); |
|
| 2888 | + |
|
| 2889 | + $folderData = $rootView->getDirectoryContent('/A/B/C'); |
|
| 2890 | + $this->assertCount(3, $folderData); |
|
| 2891 | + $this->assertEquals('folder', $folderData[0]['name']); |
|
| 2892 | + $this->assertEquals('foo.png', $folderData[1]['name']); |
|
| 2893 | + $this->assertEquals('foo.txt', $folderData[2]['name']); |
|
| 2894 | + } |
|
| 2895 | + |
|
| 2896 | + public function testCopyPreservesContent() { |
|
| 2897 | + $viewUser1 = new View('/' . 'userId' . '/files'); |
|
| 2898 | + $viewUser1->mkdir(''); |
|
| 2899 | + $viewUser1->file_put_contents('foo.txt', 'foo'); |
|
| 2900 | + $viewUser1->copy('foo.txt', 'bar.txt'); |
|
| 2901 | + $this->assertEquals('foo', $viewUser1->file_get_contents('bar.txt')); |
|
| 2902 | + } |
|
| 2903 | 2903 | } |