@@ -22,718 +22,718 @@ |
||
| 22 | 22 | use Psr\Log\LoggerInterface; |
| 23 | 23 | |
| 24 | 24 | class Filesystem { |
| 25 | - private static ?Mount\Manager $mounts = null; |
|
| 26 | - |
|
| 27 | - public static bool $loaded = false; |
|
| 28 | - |
|
| 29 | - private static ?View $defaultInstance = null; |
|
| 30 | - |
|
| 31 | - private static ?CappedMemoryCache $normalizedPathCache = null; |
|
| 32 | - |
|
| 33 | - private static ?FilenameValidator $validator = null; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * classname which used for hooks handling |
|
| 37 | - * used as signalclass in OC_Hooks::emit() |
|
| 38 | - */ |
|
| 39 | - public const CLASSNAME = 'OC_Filesystem'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * signalname emitted before file renaming |
|
| 43 | - * |
|
| 44 | - * @param string $oldpath |
|
| 45 | - * @param string $newpath |
|
| 46 | - */ |
|
| 47 | - public const signal_rename = 'rename'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * signal emitted after file renaming |
|
| 51 | - * |
|
| 52 | - * @param string $oldpath |
|
| 53 | - * @param string $newpath |
|
| 54 | - */ |
|
| 55 | - public const signal_post_rename = 'post_rename'; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * signal emitted before file/dir creation |
|
| 59 | - * |
|
| 60 | - * @param string $path |
|
| 61 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 62 | - */ |
|
| 63 | - public const signal_create = 'create'; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * signal emitted after file/dir creation |
|
| 67 | - * |
|
| 68 | - * @param string $path |
|
| 69 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 70 | - */ |
|
| 71 | - public const signal_post_create = 'post_create'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * signal emits before file/dir copy |
|
| 75 | - * |
|
| 76 | - * @param string $oldpath |
|
| 77 | - * @param string $newpath |
|
| 78 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 79 | - */ |
|
| 80 | - public const signal_copy = 'copy'; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * signal emits after file/dir copy |
|
| 84 | - * |
|
| 85 | - * @param string $oldpath |
|
| 86 | - * @param string $newpath |
|
| 87 | - */ |
|
| 88 | - public const signal_post_copy = 'post_copy'; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * signal emits before file/dir save |
|
| 92 | - * |
|
| 93 | - * @param string $path |
|
| 94 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 95 | - */ |
|
| 96 | - public const signal_write = 'write'; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * signal emits after file/dir save |
|
| 100 | - * |
|
| 101 | - * @param string $path |
|
| 102 | - */ |
|
| 103 | - public const signal_post_write = 'post_write'; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * signal emitted before file/dir update |
|
| 107 | - * |
|
| 108 | - * @param string $path |
|
| 109 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 110 | - */ |
|
| 111 | - public const signal_update = 'update'; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * signal emitted after file/dir update |
|
| 115 | - * |
|
| 116 | - * @param string $path |
|
| 117 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 118 | - */ |
|
| 119 | - public const signal_post_update = 'post_update'; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * signal emits when reading file/dir |
|
| 123 | - * |
|
| 124 | - * @param string $path |
|
| 125 | - */ |
|
| 126 | - public const signal_read = 'read'; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * signal emits when removing file/dir |
|
| 130 | - * |
|
| 131 | - * @param string $path |
|
| 132 | - */ |
|
| 133 | - public const signal_delete = 'delete'; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * parameters definitions for signals |
|
| 137 | - */ |
|
| 138 | - public const signal_param_path = 'path'; |
|
| 139 | - public const signal_param_oldpath = 'oldpath'; |
|
| 140 | - public const signal_param_newpath = 'newpath'; |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * run - changing this flag to false in hook handler will cancel event |
|
| 144 | - */ |
|
| 145 | - public const signal_param_run = 'run'; |
|
| 146 | - |
|
| 147 | - public const signal_create_mount = 'create_mount'; |
|
| 148 | - public const signal_delete_mount = 'delete_mount'; |
|
| 149 | - public const signal_param_mount_type = 'mounttype'; |
|
| 150 | - public const signal_param_users = 'users'; |
|
| 151 | - |
|
| 152 | - private static ?\OC\Files\Storage\StorageFactory $loader = null; |
|
| 153 | - |
|
| 154 | - private static bool $logWarningWhenAddingStorageWrapper = true; |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @param bool $shouldLog |
|
| 158 | - * @return bool previous value |
|
| 159 | - * @internal |
|
| 160 | - */ |
|
| 161 | - public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool { |
|
| 162 | - $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 163 | - self::$logWarningWhenAddingStorageWrapper = $shouldLog; |
|
| 164 | - return $previousValue; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param string $wrapperName |
|
| 169 | - * @param callable $wrapper |
|
| 170 | - * @param int $priority |
|
| 171 | - */ |
|
| 172 | - public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 173 | - if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 174 | - \OCP\Server::get(LoggerInterface::class)->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 175 | - 'wrapper' => $wrapperName, |
|
| 176 | - 'app' => 'filesystem', |
|
| 177 | - ]); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $mounts = self::getMountManager()->getAll(); |
|
| 181 | - /** @var StorageFactory $loader */ |
|
| 182 | - $loader = self::getLoader(); |
|
| 183 | - if (!$loader->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 184 | - // do not re-wrap if storage with this name already existed |
|
| 185 | - return; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Returns the storage factory |
|
| 191 | - * |
|
| 192 | - * @return IStorageFactory |
|
| 193 | - */ |
|
| 194 | - public static function getLoader() { |
|
| 195 | - if (!self::$loader) { |
|
| 196 | - self::$loader = \OC::$server->get(IStorageFactory::class); |
|
| 197 | - } |
|
| 198 | - return self::$loader; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Returns the mount manager |
|
| 203 | - */ |
|
| 204 | - public static function getMountManager(): Mount\Manager { |
|
| 205 | - self::initMountManager(); |
|
| 206 | - assert(self::$mounts !== null); |
|
| 207 | - return self::$mounts; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * get the mountpoint of the storage object for a path |
|
| 212 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 213 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 214 | - * and doesn't take the chroot into account ) |
|
| 215 | - * |
|
| 216 | - * @param string $path |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - public static function getMountPoint($path) { |
|
| 220 | - if (!self::$mounts) { |
|
| 221 | - \OC_Util::setupFS(); |
|
| 222 | - } |
|
| 223 | - $mount = self::$mounts->find($path); |
|
| 224 | - return $mount->getMountPoint(); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * get a list of all mount points in a directory |
|
| 229 | - * |
|
| 230 | - * @param string $path |
|
| 231 | - * @return string[] |
|
| 232 | - */ |
|
| 233 | - public static function getMountPoints($path) { |
|
| 234 | - if (!self::$mounts) { |
|
| 235 | - \OC_Util::setupFS(); |
|
| 236 | - } |
|
| 237 | - $result = []; |
|
| 238 | - $mounts = self::$mounts->findIn($path); |
|
| 239 | - foreach ($mounts as $mount) { |
|
| 240 | - $result[] = $mount->getMountPoint(); |
|
| 241 | - } |
|
| 242 | - return $result; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * get the storage mounted at $mountPoint |
|
| 247 | - * |
|
| 248 | - * @param string $mountPoint |
|
| 249 | - * @return \OC\Files\Storage\Storage|null |
|
| 250 | - */ |
|
| 251 | - public static function getStorage($mountPoint) { |
|
| 252 | - $mount = self::getMountManager()->find($mountPoint); |
|
| 253 | - return $mount->getStorage(); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * @param string $id |
|
| 258 | - * @return Mount\MountPoint[] |
|
| 259 | - */ |
|
| 260 | - public static function getMountByStorageId($id) { |
|
| 261 | - return self::getMountManager()->findByStorageId($id); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param int $id |
|
| 266 | - * @return Mount\MountPoint[] |
|
| 267 | - */ |
|
| 268 | - public static function getMountByNumericId($id) { |
|
| 269 | - return self::getMountManager()->findByNumericId($id); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * resolve a path to a storage and internal path |
|
| 274 | - * |
|
| 275 | - * @param string $path |
|
| 276 | - * @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path |
|
| 277 | - */ |
|
| 278 | - public static function resolvePath($path): array { |
|
| 279 | - $mount = self::getMountManager()->find($path); |
|
| 280 | - return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')]; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - public static function init(string|IUser|null $user, string $root): bool { |
|
| 284 | - if (self::$defaultInstance) { |
|
| 285 | - return false; |
|
| 286 | - } |
|
| 287 | - self::initInternal($root); |
|
| 288 | - |
|
| 289 | - //load custom mount config |
|
| 290 | - self::initMountPoints($user); |
|
| 291 | - |
|
| 292 | - return true; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - public static function initInternal(string $root): bool { |
|
| 296 | - if (self::$defaultInstance) { |
|
| 297 | - return false; |
|
| 298 | - } |
|
| 299 | - self::getLoader(); |
|
| 300 | - self::$defaultInstance = new View($root); |
|
| 301 | - /** @var IEventDispatcher $eventDispatcher */ |
|
| 302 | - $eventDispatcher = \OC::$server->get(IEventDispatcher::class); |
|
| 303 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
| 304 | - self::$defaultInstance = null; |
|
| 305 | - self::$loaded = false; |
|
| 306 | - }); |
|
| 307 | - |
|
| 308 | - self::initMountManager(); |
|
| 309 | - |
|
| 310 | - self::$loaded = true; |
|
| 311 | - |
|
| 312 | - return true; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - public static function initMountManager(): void { |
|
| 316 | - if (!self::$mounts) { |
|
| 317 | - self::$mounts = \OC::$server->get(IMountManager::class); |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Initialize system and personal mount points for a user |
|
| 323 | - * |
|
| 324 | - * @throws \OC\User\NoUserException if the user is not available |
|
| 325 | - */ |
|
| 326 | - public static function initMountPoints(string|IUser|null $user = ''): void { |
|
| 327 | - /** @var IUserManager $userManager */ |
|
| 328 | - $userManager = \OC::$server->get(IUserManager::class); |
|
| 329 | - |
|
| 330 | - $userObject = ($user instanceof IUser) ? $user : $userManager->get($user); |
|
| 331 | - if ($userObject) { |
|
| 332 | - /** @var SetupManager $setupManager */ |
|
| 333 | - $setupManager = \OC::$server->get(SetupManager::class); |
|
| 334 | - $setupManager->setupForUser($userObject); |
|
| 335 | - } else { |
|
| 336 | - throw new NoUserException(); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Get the default filesystem view |
|
| 342 | - */ |
|
| 343 | - public static function getView(): ?View { |
|
| 344 | - if (!self::$defaultInstance) { |
|
| 345 | - /** @var IUserSession $session */ |
|
| 346 | - $session = \OC::$server->get(IUserSession::class); |
|
| 347 | - $user = $session->getUser(); |
|
| 348 | - if ($user) { |
|
| 349 | - $userDir = '/' . $user->getUID() . '/files'; |
|
| 350 | - self::initInternal($userDir); |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - return self::$defaultInstance; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * tear down the filesystem, removing all storage providers |
|
| 358 | - */ |
|
| 359 | - public static function tearDown() { |
|
| 360 | - \OC_Util::tearDownFS(); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * get the relative path of the root data directory for the current user |
|
| 365 | - * |
|
| 366 | - * @return ?string |
|
| 367 | - * |
|
| 368 | - * Returns path like /admin/files |
|
| 369 | - */ |
|
| 370 | - public static function getRoot() { |
|
| 371 | - if (!self::$defaultInstance) { |
|
| 372 | - return null; |
|
| 373 | - } |
|
| 374 | - return self::$defaultInstance->getRoot(); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 379 | - * |
|
| 380 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 381 | - * @param array $arguments |
|
| 382 | - * @param string $mountpoint |
|
| 383 | - */ |
|
| 384 | - public static function mount($class, $arguments, $mountpoint) { |
|
| 385 | - if (!self::$mounts) { |
|
| 386 | - \OC_Util::setupFS(); |
|
| 387 | - } |
|
| 388 | - $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 389 | - self::$mounts->addMount($mount); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * return the path to a local version of the file |
|
| 394 | - * we need this because we can't know if a file is stored local or not from |
|
| 395 | - * outside the filestorage and for some purposes a local file is needed |
|
| 396 | - */ |
|
| 397 | - public static function getLocalFile(string $path): string|false { |
|
| 398 | - return self::$defaultInstance->getLocalFile($path); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * return path to file which reflects one visible in browser |
|
| 403 | - * |
|
| 404 | - * @param string $path |
|
| 405 | - * @return string |
|
| 406 | - */ |
|
| 407 | - public static function getLocalPath($path) { |
|
| 408 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 409 | - $newpath = $path; |
|
| 410 | - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 411 | - $newpath = substr($path, strlen($datadir)); |
|
| 412 | - } |
|
| 413 | - return $newpath; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * check if the requested path is valid |
|
| 418 | - * |
|
| 419 | - * @param string $path |
|
| 420 | - * @return bool |
|
| 421 | - */ |
|
| 422 | - public static function isValidPath($path) { |
|
| 423 | - $path = self::normalizePath($path); |
|
| 424 | - if (!$path || $path[0] !== '/') { |
|
| 425 | - $path = '/' . $path; |
|
| 426 | - } |
|
| 427 | - if (str_contains($path, '/../') || strrchr($path, '/') === '/..') { |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - return true; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * @param string $filename |
|
| 435 | - * @return bool |
|
| 436 | - * |
|
| 437 | - * @deprecated 30.0.0 - use \OC\Files\FilenameValidator::isForbidden |
|
| 438 | - */ |
|
| 439 | - public static function isFileBlacklisted($filename) { |
|
| 440 | - if (self::$validator === null) { |
|
| 441 | - self::$validator = \OCP\Server::get(FilenameValidator::class); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $filename = self::normalizePath($filename); |
|
| 445 | - return self::$validator->isForbidden($filename); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * check if the directory should be ignored when scanning |
|
| 450 | - * NOTE: the special directories . and .. would cause never ending recursion |
|
| 451 | - * |
|
| 452 | - * @param string $dir |
|
| 453 | - * @return boolean |
|
| 454 | - */ |
|
| 455 | - public static function isIgnoredDir($dir) { |
|
| 456 | - if ($dir === '.' || $dir === '..') { |
|
| 457 | - return true; |
|
| 458 | - } |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 464 | - */ |
|
| 465 | - public static function mkdir($path) { |
|
| 466 | - return self::$defaultInstance->mkdir($path); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - public static function rmdir($path) { |
|
| 470 | - return self::$defaultInstance->rmdir($path); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - public static function is_dir($path) { |
|
| 474 | - return self::$defaultInstance->is_dir($path); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - public static function is_file($path) { |
|
| 478 | - return self::$defaultInstance->is_file($path); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - public static function stat($path) { |
|
| 482 | - return self::$defaultInstance->stat($path); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - public static function filetype($path) { |
|
| 486 | - return self::$defaultInstance->filetype($path); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - public static function filesize($path) { |
|
| 490 | - return self::$defaultInstance->filesize($path); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - public static function readfile($path) { |
|
| 494 | - return self::$defaultInstance->readfile($path); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - public static function isCreatable($path) { |
|
| 498 | - return self::$defaultInstance->isCreatable($path); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - public static function isReadable($path) { |
|
| 502 | - return self::$defaultInstance->isReadable($path); |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - public static function isUpdatable($path) { |
|
| 506 | - return self::$defaultInstance->isUpdatable($path); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - public static function isDeletable($path) { |
|
| 510 | - return self::$defaultInstance->isDeletable($path); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - public static function isSharable($path) { |
|
| 514 | - return self::$defaultInstance->isSharable($path); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - public static function file_exists($path) { |
|
| 518 | - return self::$defaultInstance->file_exists($path); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - public static function filemtime($path) { |
|
| 522 | - return self::$defaultInstance->filemtime($path); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - public static function touch($path, $mtime = null) { |
|
| 526 | - return self::$defaultInstance->touch($path, $mtime); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * @return string|false |
|
| 531 | - */ |
|
| 532 | - public static function file_get_contents($path) { |
|
| 533 | - return self::$defaultInstance->file_get_contents($path); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - public static function file_put_contents($path, $data) { |
|
| 537 | - return self::$defaultInstance->file_put_contents($path, $data); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - public static function unlink($path) { |
|
| 541 | - return self::$defaultInstance->unlink($path); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - public static function rename($source, $target) { |
|
| 545 | - return self::$defaultInstance->rename($source, $target); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - public static function copy($source, $target) { |
|
| 549 | - return self::$defaultInstance->copy($source, $target); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - public static function fopen($path, $mode) { |
|
| 553 | - return self::$defaultInstance->fopen($path, $mode); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * @param string $path |
|
| 558 | - * @throws \OCP\Files\InvalidPathException |
|
| 559 | - */ |
|
| 560 | - public static function toTmpFile($path): string|false { |
|
| 561 | - return self::$defaultInstance->toTmpFile($path); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - public static function fromTmpFile($tmpFile, $path) { |
|
| 565 | - return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - public static function getMimeType($path) { |
|
| 569 | - return self::$defaultInstance->getMimeType($path); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - public static function hash($type, $path, $raw = false) { |
|
| 573 | - return self::$defaultInstance->hash($type, $path, $raw); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - public static function free_space($path = '/') { |
|
| 577 | - return self::$defaultInstance->free_space($path); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - public static function search($query) { |
|
| 581 | - return self::$defaultInstance->search($query); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * @param string $query |
|
| 586 | - */ |
|
| 587 | - public static function searchByMime($query) { |
|
| 588 | - return self::$defaultInstance->searchByMime($query); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * @param string|int $tag name or tag id |
|
| 593 | - * @param string $userId owner of the tags |
|
| 594 | - * @return FileInfo[] array or file info |
|
| 595 | - */ |
|
| 596 | - public static function searchByTag($tag, $userId) { |
|
| 597 | - return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * check if a file or folder has been updated since $time |
|
| 602 | - * |
|
| 603 | - * @param string $path |
|
| 604 | - * @param int $time |
|
| 605 | - * @return bool |
|
| 606 | - */ |
|
| 607 | - public static function hasUpdated($path, $time) { |
|
| 608 | - return self::$defaultInstance->hasUpdated($path, $time); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Fix common problems with a file path |
|
| 613 | - * |
|
| 614 | - * @param string $path |
|
| 615 | - * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 616 | - * @param bool $isAbsolutePath whether the given path is absolute |
|
| 617 | - * @param bool $keepUnicode true to disable unicode normalization |
|
| 618 | - * @psalm-taint-escape file |
|
| 619 | - * @return string |
|
| 620 | - */ |
|
| 621 | - public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 622 | - /** |
|
| 623 | - * FIXME: This is a workaround for existing classes and files which call |
|
| 624 | - * this function with another type than a valid string. This |
|
| 625 | - * conversion should get removed as soon as all existing |
|
| 626 | - * function calls have been fixed. |
|
| 627 | - */ |
|
| 628 | - $path = (string)$path; |
|
| 629 | - |
|
| 630 | - if ($path === '') { |
|
| 631 | - return '/'; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - if (is_null(self::$normalizedPathCache)) { |
|
| 635 | - self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 639 | - |
|
| 640 | - if ($cacheKey && isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 641 | - return self::$normalizedPathCache[$cacheKey]; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - //normalize unicode if possible |
|
| 645 | - if (!$keepUnicode) { |
|
| 646 | - $path = \OC_Util::normalizeUnicode($path); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - //add leading slash, if it is already there we strip it anyway |
|
| 650 | - $path = '/' . $path; |
|
| 651 | - |
|
| 652 | - $patterns = [ |
|
| 653 | - '#\\\\#s', // no windows style '\\' slashes |
|
| 654 | - '#/\.(/\.)*/#s', // remove '/./' |
|
| 655 | - '#\//+#s', // remove sequence of slashes |
|
| 656 | - '#/\.$#s', // remove trailing '/.' |
|
| 657 | - ]; |
|
| 658 | - |
|
| 659 | - do { |
|
| 660 | - $count = 0; |
|
| 661 | - $path = preg_replace($patterns, '/', $path, -1, $count); |
|
| 662 | - } while ($count > 0); |
|
| 663 | - |
|
| 664 | - //remove trailing slash |
|
| 665 | - if ($stripTrailingSlash && strlen($path) > 1) { |
|
| 666 | - $path = rtrim($path, '/'); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - self::$normalizedPathCache[$cacheKey] = $path; |
|
| 670 | - |
|
| 671 | - return $path; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * get the filesystem info |
|
| 676 | - * |
|
| 677 | - * @param string $path |
|
| 678 | - * @param bool|string $includeMountPoints whether to add mountpoint sizes, |
|
| 679 | - * defaults to true |
|
| 680 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 681 | - */ |
|
| 682 | - public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 683 | - return self::getView()->getFileInfo($path, $includeMountPoints); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * change file metadata |
|
| 688 | - * |
|
| 689 | - * @param string $path |
|
| 690 | - * @param array $data |
|
| 691 | - * @return int |
|
| 692 | - * |
|
| 693 | - * returns the fileid of the updated file |
|
| 694 | - */ |
|
| 695 | - public static function putFileInfo($path, $data) { |
|
| 696 | - return self::$defaultInstance->putFileInfo($path, $data); |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * get the content of a directory |
|
| 701 | - * |
|
| 702 | - * @param string $directory path under datadirectory |
|
| 703 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 704 | - * @return \OC\Files\FileInfo[] |
|
| 705 | - */ |
|
| 706 | - public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 707 | - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Get the path of a file by id |
|
| 712 | - * |
|
| 713 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 714 | - * |
|
| 715 | - * @param int $id |
|
| 716 | - * @throws NotFoundException |
|
| 717 | - * @return string |
|
| 718 | - */ |
|
| 719 | - public static function getPath($id) { |
|
| 720 | - return self::$defaultInstance->getPath($id); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Get the owner for a file or folder |
|
| 725 | - * |
|
| 726 | - * @param string $path |
|
| 727 | - * @return string |
|
| 728 | - */ |
|
| 729 | - public static function getOwner($path) { |
|
| 730 | - return self::$defaultInstance->getOwner($path); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * get the ETag for a file or folder |
|
| 735 | - */ |
|
| 736 | - public static function getETag(string $path): string|false { |
|
| 737 | - return self::$defaultInstance->getETag($path); |
|
| 738 | - } |
|
| 25 | + private static ?Mount\Manager $mounts = null; |
|
| 26 | + |
|
| 27 | + public static bool $loaded = false; |
|
| 28 | + |
|
| 29 | + private static ?View $defaultInstance = null; |
|
| 30 | + |
|
| 31 | + private static ?CappedMemoryCache $normalizedPathCache = null; |
|
| 32 | + |
|
| 33 | + private static ?FilenameValidator $validator = null; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * classname which used for hooks handling |
|
| 37 | + * used as signalclass in OC_Hooks::emit() |
|
| 38 | + */ |
|
| 39 | + public const CLASSNAME = 'OC_Filesystem'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * signalname emitted before file renaming |
|
| 43 | + * |
|
| 44 | + * @param string $oldpath |
|
| 45 | + * @param string $newpath |
|
| 46 | + */ |
|
| 47 | + public const signal_rename = 'rename'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * signal emitted after file renaming |
|
| 51 | + * |
|
| 52 | + * @param string $oldpath |
|
| 53 | + * @param string $newpath |
|
| 54 | + */ |
|
| 55 | + public const signal_post_rename = 'post_rename'; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * signal emitted before file/dir creation |
|
| 59 | + * |
|
| 60 | + * @param string $path |
|
| 61 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 62 | + */ |
|
| 63 | + public const signal_create = 'create'; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * signal emitted after file/dir creation |
|
| 67 | + * |
|
| 68 | + * @param string $path |
|
| 69 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 70 | + */ |
|
| 71 | + public const signal_post_create = 'post_create'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * signal emits before file/dir copy |
|
| 75 | + * |
|
| 76 | + * @param string $oldpath |
|
| 77 | + * @param string $newpath |
|
| 78 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 79 | + */ |
|
| 80 | + public const signal_copy = 'copy'; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * signal emits after file/dir copy |
|
| 84 | + * |
|
| 85 | + * @param string $oldpath |
|
| 86 | + * @param string $newpath |
|
| 87 | + */ |
|
| 88 | + public const signal_post_copy = 'post_copy'; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * signal emits before file/dir save |
|
| 92 | + * |
|
| 93 | + * @param string $path |
|
| 94 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 95 | + */ |
|
| 96 | + public const signal_write = 'write'; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * signal emits after file/dir save |
|
| 100 | + * |
|
| 101 | + * @param string $path |
|
| 102 | + */ |
|
| 103 | + public const signal_post_write = 'post_write'; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * signal emitted before file/dir update |
|
| 107 | + * |
|
| 108 | + * @param string $path |
|
| 109 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 110 | + */ |
|
| 111 | + public const signal_update = 'update'; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * signal emitted after file/dir update |
|
| 115 | + * |
|
| 116 | + * @param string $path |
|
| 117 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 118 | + */ |
|
| 119 | + public const signal_post_update = 'post_update'; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * signal emits when reading file/dir |
|
| 123 | + * |
|
| 124 | + * @param string $path |
|
| 125 | + */ |
|
| 126 | + public const signal_read = 'read'; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * signal emits when removing file/dir |
|
| 130 | + * |
|
| 131 | + * @param string $path |
|
| 132 | + */ |
|
| 133 | + public const signal_delete = 'delete'; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * parameters definitions for signals |
|
| 137 | + */ |
|
| 138 | + public const signal_param_path = 'path'; |
|
| 139 | + public const signal_param_oldpath = 'oldpath'; |
|
| 140 | + public const signal_param_newpath = 'newpath'; |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * run - changing this flag to false in hook handler will cancel event |
|
| 144 | + */ |
|
| 145 | + public const signal_param_run = 'run'; |
|
| 146 | + |
|
| 147 | + public const signal_create_mount = 'create_mount'; |
|
| 148 | + public const signal_delete_mount = 'delete_mount'; |
|
| 149 | + public const signal_param_mount_type = 'mounttype'; |
|
| 150 | + public const signal_param_users = 'users'; |
|
| 151 | + |
|
| 152 | + private static ?\OC\Files\Storage\StorageFactory $loader = null; |
|
| 153 | + |
|
| 154 | + private static bool $logWarningWhenAddingStorageWrapper = true; |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @param bool $shouldLog |
|
| 158 | + * @return bool previous value |
|
| 159 | + * @internal |
|
| 160 | + */ |
|
| 161 | + public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool { |
|
| 162 | + $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 163 | + self::$logWarningWhenAddingStorageWrapper = $shouldLog; |
|
| 164 | + return $previousValue; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param string $wrapperName |
|
| 169 | + * @param callable $wrapper |
|
| 170 | + * @param int $priority |
|
| 171 | + */ |
|
| 172 | + public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 173 | + if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 174 | + \OCP\Server::get(LoggerInterface::class)->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 175 | + 'wrapper' => $wrapperName, |
|
| 176 | + 'app' => 'filesystem', |
|
| 177 | + ]); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $mounts = self::getMountManager()->getAll(); |
|
| 181 | + /** @var StorageFactory $loader */ |
|
| 182 | + $loader = self::getLoader(); |
|
| 183 | + if (!$loader->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 184 | + // do not re-wrap if storage with this name already existed |
|
| 185 | + return; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Returns the storage factory |
|
| 191 | + * |
|
| 192 | + * @return IStorageFactory |
|
| 193 | + */ |
|
| 194 | + public static function getLoader() { |
|
| 195 | + if (!self::$loader) { |
|
| 196 | + self::$loader = \OC::$server->get(IStorageFactory::class); |
|
| 197 | + } |
|
| 198 | + return self::$loader; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Returns the mount manager |
|
| 203 | + */ |
|
| 204 | + public static function getMountManager(): Mount\Manager { |
|
| 205 | + self::initMountManager(); |
|
| 206 | + assert(self::$mounts !== null); |
|
| 207 | + return self::$mounts; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * get the mountpoint of the storage object for a path |
|
| 212 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 213 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 214 | + * and doesn't take the chroot into account ) |
|
| 215 | + * |
|
| 216 | + * @param string $path |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + public static function getMountPoint($path) { |
|
| 220 | + if (!self::$mounts) { |
|
| 221 | + \OC_Util::setupFS(); |
|
| 222 | + } |
|
| 223 | + $mount = self::$mounts->find($path); |
|
| 224 | + return $mount->getMountPoint(); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * get a list of all mount points in a directory |
|
| 229 | + * |
|
| 230 | + * @param string $path |
|
| 231 | + * @return string[] |
|
| 232 | + */ |
|
| 233 | + public static function getMountPoints($path) { |
|
| 234 | + if (!self::$mounts) { |
|
| 235 | + \OC_Util::setupFS(); |
|
| 236 | + } |
|
| 237 | + $result = []; |
|
| 238 | + $mounts = self::$mounts->findIn($path); |
|
| 239 | + foreach ($mounts as $mount) { |
|
| 240 | + $result[] = $mount->getMountPoint(); |
|
| 241 | + } |
|
| 242 | + return $result; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * get the storage mounted at $mountPoint |
|
| 247 | + * |
|
| 248 | + * @param string $mountPoint |
|
| 249 | + * @return \OC\Files\Storage\Storage|null |
|
| 250 | + */ |
|
| 251 | + public static function getStorage($mountPoint) { |
|
| 252 | + $mount = self::getMountManager()->find($mountPoint); |
|
| 253 | + return $mount->getStorage(); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * @param string $id |
|
| 258 | + * @return Mount\MountPoint[] |
|
| 259 | + */ |
|
| 260 | + public static function getMountByStorageId($id) { |
|
| 261 | + return self::getMountManager()->findByStorageId($id); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param int $id |
|
| 266 | + * @return Mount\MountPoint[] |
|
| 267 | + */ |
|
| 268 | + public static function getMountByNumericId($id) { |
|
| 269 | + return self::getMountManager()->findByNumericId($id); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * resolve a path to a storage and internal path |
|
| 274 | + * |
|
| 275 | + * @param string $path |
|
| 276 | + * @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path |
|
| 277 | + */ |
|
| 278 | + public static function resolvePath($path): array { |
|
| 279 | + $mount = self::getMountManager()->find($path); |
|
| 280 | + return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')]; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + public static function init(string|IUser|null $user, string $root): bool { |
|
| 284 | + if (self::$defaultInstance) { |
|
| 285 | + return false; |
|
| 286 | + } |
|
| 287 | + self::initInternal($root); |
|
| 288 | + |
|
| 289 | + //load custom mount config |
|
| 290 | + self::initMountPoints($user); |
|
| 291 | + |
|
| 292 | + return true; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + public static function initInternal(string $root): bool { |
|
| 296 | + if (self::$defaultInstance) { |
|
| 297 | + return false; |
|
| 298 | + } |
|
| 299 | + self::getLoader(); |
|
| 300 | + self::$defaultInstance = new View($root); |
|
| 301 | + /** @var IEventDispatcher $eventDispatcher */ |
|
| 302 | + $eventDispatcher = \OC::$server->get(IEventDispatcher::class); |
|
| 303 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
| 304 | + self::$defaultInstance = null; |
|
| 305 | + self::$loaded = false; |
|
| 306 | + }); |
|
| 307 | + |
|
| 308 | + self::initMountManager(); |
|
| 309 | + |
|
| 310 | + self::$loaded = true; |
|
| 311 | + |
|
| 312 | + return true; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + public static function initMountManager(): void { |
|
| 316 | + if (!self::$mounts) { |
|
| 317 | + self::$mounts = \OC::$server->get(IMountManager::class); |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Initialize system and personal mount points for a user |
|
| 323 | + * |
|
| 324 | + * @throws \OC\User\NoUserException if the user is not available |
|
| 325 | + */ |
|
| 326 | + public static function initMountPoints(string|IUser|null $user = ''): void { |
|
| 327 | + /** @var IUserManager $userManager */ |
|
| 328 | + $userManager = \OC::$server->get(IUserManager::class); |
|
| 329 | + |
|
| 330 | + $userObject = ($user instanceof IUser) ? $user : $userManager->get($user); |
|
| 331 | + if ($userObject) { |
|
| 332 | + /** @var SetupManager $setupManager */ |
|
| 333 | + $setupManager = \OC::$server->get(SetupManager::class); |
|
| 334 | + $setupManager->setupForUser($userObject); |
|
| 335 | + } else { |
|
| 336 | + throw new NoUserException(); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Get the default filesystem view |
|
| 342 | + */ |
|
| 343 | + public static function getView(): ?View { |
|
| 344 | + if (!self::$defaultInstance) { |
|
| 345 | + /** @var IUserSession $session */ |
|
| 346 | + $session = \OC::$server->get(IUserSession::class); |
|
| 347 | + $user = $session->getUser(); |
|
| 348 | + if ($user) { |
|
| 349 | + $userDir = '/' . $user->getUID() . '/files'; |
|
| 350 | + self::initInternal($userDir); |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + return self::$defaultInstance; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * tear down the filesystem, removing all storage providers |
|
| 358 | + */ |
|
| 359 | + public static function tearDown() { |
|
| 360 | + \OC_Util::tearDownFS(); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * get the relative path of the root data directory for the current user |
|
| 365 | + * |
|
| 366 | + * @return ?string |
|
| 367 | + * |
|
| 368 | + * Returns path like /admin/files |
|
| 369 | + */ |
|
| 370 | + public static function getRoot() { |
|
| 371 | + if (!self::$defaultInstance) { |
|
| 372 | + return null; |
|
| 373 | + } |
|
| 374 | + return self::$defaultInstance->getRoot(); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 379 | + * |
|
| 380 | + * @param \OC\Files\Storage\Storage|string $class |
|
| 381 | + * @param array $arguments |
|
| 382 | + * @param string $mountpoint |
|
| 383 | + */ |
|
| 384 | + public static function mount($class, $arguments, $mountpoint) { |
|
| 385 | + if (!self::$mounts) { |
|
| 386 | + \OC_Util::setupFS(); |
|
| 387 | + } |
|
| 388 | + $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 389 | + self::$mounts->addMount($mount); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * return the path to a local version of the file |
|
| 394 | + * we need this because we can't know if a file is stored local or not from |
|
| 395 | + * outside the filestorage and for some purposes a local file is needed |
|
| 396 | + */ |
|
| 397 | + public static function getLocalFile(string $path): string|false { |
|
| 398 | + return self::$defaultInstance->getLocalFile($path); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * return path to file which reflects one visible in browser |
|
| 403 | + * |
|
| 404 | + * @param string $path |
|
| 405 | + * @return string |
|
| 406 | + */ |
|
| 407 | + public static function getLocalPath($path) { |
|
| 408 | + $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 409 | + $newpath = $path; |
|
| 410 | + if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 411 | + $newpath = substr($path, strlen($datadir)); |
|
| 412 | + } |
|
| 413 | + return $newpath; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * check if the requested path is valid |
|
| 418 | + * |
|
| 419 | + * @param string $path |
|
| 420 | + * @return bool |
|
| 421 | + */ |
|
| 422 | + public static function isValidPath($path) { |
|
| 423 | + $path = self::normalizePath($path); |
|
| 424 | + if (!$path || $path[0] !== '/') { |
|
| 425 | + $path = '/' . $path; |
|
| 426 | + } |
|
| 427 | + if (str_contains($path, '/../') || strrchr($path, '/') === '/..') { |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + return true; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * @param string $filename |
|
| 435 | + * @return bool |
|
| 436 | + * |
|
| 437 | + * @deprecated 30.0.0 - use \OC\Files\FilenameValidator::isForbidden |
|
| 438 | + */ |
|
| 439 | + public static function isFileBlacklisted($filename) { |
|
| 440 | + if (self::$validator === null) { |
|
| 441 | + self::$validator = \OCP\Server::get(FilenameValidator::class); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $filename = self::normalizePath($filename); |
|
| 445 | + return self::$validator->isForbidden($filename); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * check if the directory should be ignored when scanning |
|
| 450 | + * NOTE: the special directories . and .. would cause never ending recursion |
|
| 451 | + * |
|
| 452 | + * @param string $dir |
|
| 453 | + * @return boolean |
|
| 454 | + */ |
|
| 455 | + public static function isIgnoredDir($dir) { |
|
| 456 | + if ($dir === '.' || $dir === '..') { |
|
| 457 | + return true; |
|
| 458 | + } |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 464 | + */ |
|
| 465 | + public static function mkdir($path) { |
|
| 466 | + return self::$defaultInstance->mkdir($path); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + public static function rmdir($path) { |
|
| 470 | + return self::$defaultInstance->rmdir($path); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + public static function is_dir($path) { |
|
| 474 | + return self::$defaultInstance->is_dir($path); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + public static function is_file($path) { |
|
| 478 | + return self::$defaultInstance->is_file($path); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + public static function stat($path) { |
|
| 482 | + return self::$defaultInstance->stat($path); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + public static function filetype($path) { |
|
| 486 | + return self::$defaultInstance->filetype($path); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + public static function filesize($path) { |
|
| 490 | + return self::$defaultInstance->filesize($path); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + public static function readfile($path) { |
|
| 494 | + return self::$defaultInstance->readfile($path); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + public static function isCreatable($path) { |
|
| 498 | + return self::$defaultInstance->isCreatable($path); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + public static function isReadable($path) { |
|
| 502 | + return self::$defaultInstance->isReadable($path); |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + public static function isUpdatable($path) { |
|
| 506 | + return self::$defaultInstance->isUpdatable($path); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + public static function isDeletable($path) { |
|
| 510 | + return self::$defaultInstance->isDeletable($path); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + public static function isSharable($path) { |
|
| 514 | + return self::$defaultInstance->isSharable($path); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + public static function file_exists($path) { |
|
| 518 | + return self::$defaultInstance->file_exists($path); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + public static function filemtime($path) { |
|
| 522 | + return self::$defaultInstance->filemtime($path); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + public static function touch($path, $mtime = null) { |
|
| 526 | + return self::$defaultInstance->touch($path, $mtime); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * @return string|false |
|
| 531 | + */ |
|
| 532 | + public static function file_get_contents($path) { |
|
| 533 | + return self::$defaultInstance->file_get_contents($path); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + public static function file_put_contents($path, $data) { |
|
| 537 | + return self::$defaultInstance->file_put_contents($path, $data); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + public static function unlink($path) { |
|
| 541 | + return self::$defaultInstance->unlink($path); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + public static function rename($source, $target) { |
|
| 545 | + return self::$defaultInstance->rename($source, $target); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + public static function copy($source, $target) { |
|
| 549 | + return self::$defaultInstance->copy($source, $target); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + public static function fopen($path, $mode) { |
|
| 553 | + return self::$defaultInstance->fopen($path, $mode); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * @param string $path |
|
| 558 | + * @throws \OCP\Files\InvalidPathException |
|
| 559 | + */ |
|
| 560 | + public static function toTmpFile($path): string|false { |
|
| 561 | + return self::$defaultInstance->toTmpFile($path); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + public static function fromTmpFile($tmpFile, $path) { |
|
| 565 | + return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + public static function getMimeType($path) { |
|
| 569 | + return self::$defaultInstance->getMimeType($path); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + public static function hash($type, $path, $raw = false) { |
|
| 573 | + return self::$defaultInstance->hash($type, $path, $raw); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + public static function free_space($path = '/') { |
|
| 577 | + return self::$defaultInstance->free_space($path); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + public static function search($query) { |
|
| 581 | + return self::$defaultInstance->search($query); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * @param string $query |
|
| 586 | + */ |
|
| 587 | + public static function searchByMime($query) { |
|
| 588 | + return self::$defaultInstance->searchByMime($query); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * @param string|int $tag name or tag id |
|
| 593 | + * @param string $userId owner of the tags |
|
| 594 | + * @return FileInfo[] array or file info |
|
| 595 | + */ |
|
| 596 | + public static function searchByTag($tag, $userId) { |
|
| 597 | + return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * check if a file or folder has been updated since $time |
|
| 602 | + * |
|
| 603 | + * @param string $path |
|
| 604 | + * @param int $time |
|
| 605 | + * @return bool |
|
| 606 | + */ |
|
| 607 | + public static function hasUpdated($path, $time) { |
|
| 608 | + return self::$defaultInstance->hasUpdated($path, $time); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Fix common problems with a file path |
|
| 613 | + * |
|
| 614 | + * @param string $path |
|
| 615 | + * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 616 | + * @param bool $isAbsolutePath whether the given path is absolute |
|
| 617 | + * @param bool $keepUnicode true to disable unicode normalization |
|
| 618 | + * @psalm-taint-escape file |
|
| 619 | + * @return string |
|
| 620 | + */ |
|
| 621 | + public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 622 | + /** |
|
| 623 | + * FIXME: This is a workaround for existing classes and files which call |
|
| 624 | + * this function with another type than a valid string. This |
|
| 625 | + * conversion should get removed as soon as all existing |
|
| 626 | + * function calls have been fixed. |
|
| 627 | + */ |
|
| 628 | + $path = (string)$path; |
|
| 629 | + |
|
| 630 | + if ($path === '') { |
|
| 631 | + return '/'; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + if (is_null(self::$normalizedPathCache)) { |
|
| 635 | + self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 639 | + |
|
| 640 | + if ($cacheKey && isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 641 | + return self::$normalizedPathCache[$cacheKey]; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + //normalize unicode if possible |
|
| 645 | + if (!$keepUnicode) { |
|
| 646 | + $path = \OC_Util::normalizeUnicode($path); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + //add leading slash, if it is already there we strip it anyway |
|
| 650 | + $path = '/' . $path; |
|
| 651 | + |
|
| 652 | + $patterns = [ |
|
| 653 | + '#\\\\#s', // no windows style '\\' slashes |
|
| 654 | + '#/\.(/\.)*/#s', // remove '/./' |
|
| 655 | + '#\//+#s', // remove sequence of slashes |
|
| 656 | + '#/\.$#s', // remove trailing '/.' |
|
| 657 | + ]; |
|
| 658 | + |
|
| 659 | + do { |
|
| 660 | + $count = 0; |
|
| 661 | + $path = preg_replace($patterns, '/', $path, -1, $count); |
|
| 662 | + } while ($count > 0); |
|
| 663 | + |
|
| 664 | + //remove trailing slash |
|
| 665 | + if ($stripTrailingSlash && strlen($path) > 1) { |
|
| 666 | + $path = rtrim($path, '/'); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + self::$normalizedPathCache[$cacheKey] = $path; |
|
| 670 | + |
|
| 671 | + return $path; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * get the filesystem info |
|
| 676 | + * |
|
| 677 | + * @param string $path |
|
| 678 | + * @param bool|string $includeMountPoints whether to add mountpoint sizes, |
|
| 679 | + * defaults to true |
|
| 680 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 681 | + */ |
|
| 682 | + public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 683 | + return self::getView()->getFileInfo($path, $includeMountPoints); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * change file metadata |
|
| 688 | + * |
|
| 689 | + * @param string $path |
|
| 690 | + * @param array $data |
|
| 691 | + * @return int |
|
| 692 | + * |
|
| 693 | + * returns the fileid of the updated file |
|
| 694 | + */ |
|
| 695 | + public static function putFileInfo($path, $data) { |
|
| 696 | + return self::$defaultInstance->putFileInfo($path, $data); |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * get the content of a directory |
|
| 701 | + * |
|
| 702 | + * @param string $directory path under datadirectory |
|
| 703 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 704 | + * @return \OC\Files\FileInfo[] |
|
| 705 | + */ |
|
| 706 | + public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 707 | + return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Get the path of a file by id |
|
| 712 | + * |
|
| 713 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 714 | + * |
|
| 715 | + * @param int $id |
|
| 716 | + * @throws NotFoundException |
|
| 717 | + * @return string |
|
| 718 | + */ |
|
| 719 | + public static function getPath($id) { |
|
| 720 | + return self::$defaultInstance->getPath($id); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Get the owner for a file or folder |
|
| 725 | + * |
|
| 726 | + * @param string $path |
|
| 727 | + * @return string |
|
| 728 | + */ |
|
| 729 | + public static function getOwner($path) { |
|
| 730 | + return self::$defaultInstance->getOwner($path); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * get the ETag for a file or folder |
|
| 735 | + */ |
|
| 736 | + public static function getETag(string $path): string|false { |
|
| 737 | + return self::$defaultInstance->getETag($path); |
|
| 738 | + } |
|
| 739 | 739 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | use Psr\Log\LoggerInterface; |
| 23 | 23 | |
| 24 | 24 | class Filesystem { |
| 25 | - private static ?Mount\Manager $mounts = null; |
|
| 25 | + private static ? Mount\Manager $mounts = null; |
|
| 26 | 26 | |
| 27 | 27 | public static bool $loaded = false; |
| 28 | 28 | |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | public const signal_param_mount_type = 'mounttype'; |
| 150 | 150 | public const signal_param_users = 'users'; |
| 151 | 151 | |
| 152 | - private static ?\OC\Files\Storage\StorageFactory $loader = null; |
|
| 152 | + private static ? \OC\Files\Storage\StorageFactory $loader = null; |
|
| 153 | 153 | |
| 154 | 154 | private static bool $logWarningWhenAddingStorageWrapper = true; |
| 155 | 155 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')]; |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - public static function init(string|IUser|null $user, string $root): bool { |
|
| 283 | + public static function init(string | IUser | null $user, string $root): bool { |
|
| 284 | 284 | if (self::$defaultInstance) { |
| 285 | 285 | return false; |
| 286 | 286 | } |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | self::$defaultInstance = new View($root); |
| 301 | 301 | /** @var IEventDispatcher $eventDispatcher */ |
| 302 | 302 | $eventDispatcher = \OC::$server->get(IEventDispatcher::class); |
| 303 | - $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () { |
|
| 303 | + $eventDispatcher->addListener(FilesystemTornDownEvent::class, function() { |
|
| 304 | 304 | self::$defaultInstance = null; |
| 305 | 305 | self::$loaded = false; |
| 306 | 306 | }); |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | * |
| 324 | 324 | * @throws \OC\User\NoUserException if the user is not available |
| 325 | 325 | */ |
| 326 | - public static function initMountPoints(string|IUser|null $user = ''): void { |
|
| 326 | + public static function initMountPoints(string | IUser | null $user = ''): void { |
|
| 327 | 327 | /** @var IUserManager $userManager */ |
| 328 | 328 | $userManager = \OC::$server->get(IUserManager::class); |
| 329 | 329 | |
@@ -346,7 +346,7 @@ discard block |
||
| 346 | 346 | $session = \OC::$server->get(IUserSession::class); |
| 347 | 347 | $user = $session->getUser(); |
| 348 | 348 | if ($user) { |
| 349 | - $userDir = '/' . $user->getUID() . '/files'; |
|
| 349 | + $userDir = '/'.$user->getUID().'/files'; |
|
| 350 | 350 | self::initInternal($userDir); |
| 351 | 351 | } |
| 352 | 352 | } |
@@ -394,7 +394,7 @@ discard block |
||
| 394 | 394 | * we need this because we can't know if a file is stored local or not from |
| 395 | 395 | * outside the filestorage and for some purposes a local file is needed |
| 396 | 396 | */ |
| 397 | - public static function getLocalFile(string $path): string|false { |
|
| 397 | + public static function getLocalFile(string $path): string | false { |
|
| 398 | 398 | return self::$defaultInstance->getLocalFile($path); |
| 399 | 399 | } |
| 400 | 400 | |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | * @return string |
| 406 | 406 | */ |
| 407 | 407 | public static function getLocalPath($path) { |
| 408 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 408 | + $datadir = \OC_User::getHome(\OC_User::getUser()).'/files'; |
|
| 409 | 409 | $newpath = $path; |
| 410 | 410 | if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
| 411 | 411 | $newpath = substr($path, strlen($datadir)); |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | public static function isValidPath($path) { |
| 423 | 423 | $path = self::normalizePath($path); |
| 424 | 424 | if (!$path || $path[0] !== '/') { |
| 425 | - $path = '/' . $path; |
|
| 425 | + $path = '/'.$path; |
|
| 426 | 426 | } |
| 427 | 427 | if (str_contains($path, '/../') || strrchr($path, '/') === '/..') { |
| 428 | 428 | return false; |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | * @param string $path |
| 558 | 558 | * @throws \OCP\Files\InvalidPathException |
| 559 | 559 | */ |
| 560 | - public static function toTmpFile($path): string|false { |
|
| 560 | + public static function toTmpFile($path): string | false { |
|
| 561 | 561 | return self::$defaultInstance->toTmpFile($path); |
| 562 | 562 | } |
| 563 | 563 | |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | * conversion should get removed as soon as all existing |
| 626 | 626 | * function calls have been fixed. |
| 627 | 627 | */ |
| 628 | - $path = (string)$path; |
|
| 628 | + $path = (string) $path; |
|
| 629 | 629 | |
| 630 | 630 | if ($path === '') { |
| 631 | 631 | return '/'; |
@@ -647,13 +647,13 @@ discard block |
||
| 647 | 647 | } |
| 648 | 648 | |
| 649 | 649 | //add leading slash, if it is already there we strip it anyway |
| 650 | - $path = '/' . $path; |
|
| 650 | + $path = '/'.$path; |
|
| 651 | 651 | |
| 652 | 652 | $patterns = [ |
| 653 | - '#\\\\#s', // no windows style '\\' slashes |
|
| 653 | + '#\\\\#s', // no windows style '\\' slashes |
|
| 654 | 654 | '#/\.(/\.)*/#s', // remove '/./' |
| 655 | - '#\//+#s', // remove sequence of slashes |
|
| 656 | - '#/\.$#s', // remove trailing '/.' |
|
| 655 | + '#\//+#s', // remove sequence of slashes |
|
| 656 | + '#/\.$#s', // remove trailing '/.' |
|
| 657 | 657 | ]; |
| 658 | 658 | |
| 659 | 659 | do { |
@@ -733,7 +733,7 @@ discard block |
||
| 733 | 733 | /** |
| 734 | 734 | * get the ETag for a file or folder |
| 735 | 735 | */ |
| 736 | - public static function getETag(string $path): string|false { |
|
| 736 | + public static function getETag(string $path): string | false { |
|
| 737 | 737 | return self::$defaultInstance->getETag($path); |
| 738 | 738 | } |
| 739 | 739 | } |