aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/View.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Files/View.php')
-rw-r--r--lib/private/Files/View.php458
1 files changed, 282 insertions, 176 deletions
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index 6eefb093795..a852f453963 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -1,74 +1,43 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Ashod Nakashian <ashod.nakashian@collabora.co.uk>
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Florin Peter <github@florin-peter.de>
- * @author Jesús Macias <jmacias@solidgear.es>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author karakayasemi <karakayasemi@itu.edu.tr>
- * @author Klaas Freitag <freitag@owncloud.com>
- * @author korelstar <korelstar@users.noreply.github.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Luke Policinski <lpolicinski@gmail.com>
- * @author Michael Gapczynski <GapczynskiM@gmail.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Piotr Filiciak <piotr@filiciak.pl>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Sam Tuke <mail@samtuke.com>
- * @author Scott Dutton <exussum12@users.noreply.github.com>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Thomas Tanghus <thomas@tanghus.net>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Files;
use Icewind\Streams\CallbackWrapper;
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
+use OC\Files\Storage\Wrapper\Quota;
use OC\Share\Share;
use OC\User\LazyUser;
use OC\User\Manager as UserManager;
use OC\User\User;
use OCA\Files_Sharing\SharedMount;
use OCP\Constants;
+use OCP\Files;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\ConnectionLostException;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
+use OCP\Files\ForbiddenException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidDirectoryException;
use OCP\Files\InvalidPathException;
+use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
-use OCP\Files\Storage\IStorage;
use OCP\IUser;
+use OCP\IUserManager;
+use OCP\L10N\IFactory;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
+use OCP\Server;
+use OCP\Share\IManager;
+use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
/**
@@ -104,7 +73,7 @@ class View {
}
$this->fakeRoot = $root;
- $this->lockingProvider = \OC::$server->getLockingProvider();
+ $this->lockingProvider = \OC::$server->get(ILockingProvider::class);
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
$this->userManager = \OC::$server->getUserManager();
$this->logger = \OC::$server->get(LoggerInterface::class);
@@ -257,7 +226,7 @@ class View {
$relPath = '/' . $pathParts[3];
$this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true);
\OC_Hook::emit(
- Filesystem::CLASSNAME, "umount",
+ Filesystem::CLASSNAME, 'umount',
[Filesystem::signal_param_path => $relPath]
);
$this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true);
@@ -265,7 +234,7 @@ class View {
$this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true);
if ($result) {
\OC_Hook::emit(
- Filesystem::CLASSNAME, "post_umount",
+ Filesystem::CLASSNAME, 'post_umount',
[Filesystem::signal_param_path => $relPath]
);
}
@@ -287,12 +256,12 @@ class View {
$this->updaterEnabled = true;
}
- protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null): void {
+ protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void {
if ($this->updaterEnabled) {
if (is_null($time)) {
$time = time();
}
- $storage->getUpdater()->update($internalPath, $time);
+ $storage->getUpdater()->update($internalPath, $time, $sizeDifference);
}
}
@@ -308,6 +277,12 @@ class View {
}
}
+ protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
+ if ($this->updaterEnabled) {
+ $targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
+ }
+ }
+
/**
* @param string $path
* @return bool|mixed
@@ -634,13 +609,13 @@ class View {
$this->lockFile($path, ILockingProvider::LOCK_SHARED);
$exists = $this->file_exists($path);
- $run = true;
if ($this->shouldEmitHooks($path)) {
+ $run = true;
$this->emit_file_hooks_pre($exists, $path, $run);
- }
- if (!$run) {
- $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
- return false;
+ if (!$run) {
+ $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
+ return false;
+ }
}
try {
@@ -655,7 +630,7 @@ class View {
[$storage, $internalPath] = $this->resolvePath($path);
$target = $storage->fopen($internalPath, 'w');
if ($target) {
- [, $result] = \OC_Helper::streamCopy($data, $target);
+ [, $result] = Files::streamCopy($data, $target, true);
fclose($target);
fclose($data);
@@ -724,13 +699,26 @@ class View {
*
* @param string $source source path
* @param string $target target path
+ * @param array $options
*
* @return bool|mixed
* @throws LockedException
*/
- public function rename($source, $target) {
+ public function rename($source, $target, array $options = []) {
+ $checkSubMounts = $options['checkSubMounts'] ?? true;
+
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
+
+ if (str_starts_with($absolutePath2, $absolutePath1 . '/')) {
+ throw new ForbiddenException('Moving a folder into a child folder is forbidden', false);
+ }
+
+ /** @var IMountManager $mountManager */
+ $mountManager = \OC::$server->get(IMountManager::class);
+
+ $targetParts = explode('/', $absolutePath2);
+ $targetUser = $targetParts[1] ?? null;
$result = false;
if (
Filesystem::isValidPath($target)
@@ -745,6 +733,12 @@ class View {
return false;
}
+ try {
+ $this->verifyPath(dirname($target), basename($target));
+ } catch (InvalidPathException) {
+ return false;
+ }
+
$this->lockFile($source, ILockingProvider::LOCK_SHARED, true);
try {
$this->lockFile($target, ILockingProvider::LOCK_SHARED, true);
@@ -768,8 +762,6 @@ class View {
}
}
if ($run) {
- $this->verifyPath(dirname($target), basename($target));
-
$manager = Filesystem::getMountManager();
$mount1 = $this->getMount($source);
$mount2 = $this->getMount($target);
@@ -782,31 +774,38 @@ class View {
try {
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true);
+ if ($checkSubMounts) {
+ $movedMounts = $mountManager->findIn($this->getAbsolutePath($source));
+ } else {
+ $movedMounts = [];
+ }
+
if ($internalPath1 === '') {
- if ($mount1 instanceof MoveableMount) {
- $sourceParentMount = $this->getMount(dirname($source));
- if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) {
- /**
- * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
- */
- $sourceMountPoint = $mount1->getMountPoint();
- $result = $mount1->moveMount($absolutePath2);
- $manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
- } else {
- $result = false;
- }
- } else {
- $result = false;
- }
- // moving a file/folder within the same mount point
+ $sourceParentMount = $this->getMount(dirname($source));
+ $movedMounts[] = $mount1;
+ $this->validateMountMove($movedMounts, $sourceParentMount, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
+ /**
+ * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
+ */
+ $sourceMountPoint = $mount1->getMountPoint();
+ $result = $mount1->moveMount($absolutePath2);
+ $manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
+
+ // moving a file/folder within the same mount point
} elseif ($storage1 === $storage2) {
+ if (count($movedMounts) > 0) {
+ $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
+ }
if ($storage1) {
$result = $storage1->rename($internalPath1, $internalPath2);
} else {
$result = false;
}
- // moving a file/folder between storages (from $storage1 to $storage2)
+ // moving a file/folder between storages (from $storage1 to $storage2)
} else {
+ if (count($movedMounts) > 0) {
+ $this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
+ }
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
}
@@ -857,6 +856,56 @@ class View {
}
/**
+ * @throws ForbiddenException
+ */
+ private function validateMountMove(array $mounts, IMountPoint $sourceMount, IMountPoint $targetMount, bool $targetIsShared): void {
+ $targetPath = $this->getRelativePath($targetMount->getMountPoint());
+ if ($targetPath) {
+ $targetPath = trim($targetPath, '/');
+ } else {
+ $targetPath = $targetMount->getMountPoint();
+ }
+
+ $l = \OC::$server->get(IFactory::class)->get('files');
+ foreach ($mounts as $mount) {
+ $sourcePath = $this->getRelativePath($mount->getMountPoint());
+ if ($sourcePath) {
+ $sourcePath = trim($sourcePath, '/');
+ } else {
+ $sourcePath = $mount->getMountPoint();
+ }
+
+ if (!$mount instanceof MoveableMount) {
+ throw new ForbiddenException($l->t('Storage %s cannot be moved', [$sourcePath]), false);
+ }
+
+ if ($targetIsShared) {
+ if ($sourceMount instanceof SharedMount) {
+ throw new ForbiddenException($l->t('Moving a share (%s) into a shared folder is not allowed', [$sourcePath]), false);
+ } else {
+ throw new ForbiddenException($l->t('Moving a storage (%s) into a shared folder is not allowed', [$sourcePath]), false);
+ }
+ }
+
+ if ($sourceMount !== $targetMount) {
+ if ($sourceMount instanceof SharedMount) {
+ if ($targetMount instanceof SharedMount) {
+ throw new ForbiddenException($l->t('Moving a share (%s) into another share (%s) is not allowed', [$sourcePath, $targetPath]), false);
+ } else {
+ throw new ForbiddenException($l->t('Moving a share (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
+ }
+ } else {
+ if ($targetMount instanceof SharedMount) {
+ throw new ForbiddenException($l->t('Moving a storage (%s) into a share (%s) is not allowed', [$sourcePath, $targetPath]), false);
+ } else {
+ throw new ForbiddenException($l->t('Moving a storage (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
+ }
+ }
+ }
+ }
+ }
+
+ /**
* Copy a file/folder from the source path to target path
*
* @param string $source source path
@@ -889,7 +938,7 @@ class View {
try {
$exists = $this->file_exists($target);
- if ($this->shouldEmitHooks()) {
+ if ($this->shouldEmitHooks($target)) {
\OC_Hook::emit(
Filesystem::CLASSNAME,
Filesystem::signal_copy,
@@ -922,12 +971,14 @@ class View {
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
}
- $this->writeUpdate($storage2, $internalPath2);
+ if ($result) {
+ $this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2);
+ }
$this->changeLock($target, ILockingProvider::LOCK_SHARED);
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
- if ($this->shouldEmitHooks() && $result !== false) {
+ if ($this->shouldEmitHooks($target) && $result !== false) {
\OC_Hook::emit(
Filesystem::CLASSNAME,
Filesystem::signal_post_copy,
@@ -1173,10 +1224,12 @@ class View {
$this->removeUpdate($storage, $internalPath);
}
if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') {
- $this->writeUpdate($storage, $internalPath);
+ $isCreateOperation = $operation === 'mkdir' || ($operation === 'file_put_contents' && in_array('create', $hooks, true));
+ $sizeDifference = $operation === 'mkdir' ? 0 : $result;
+ $this->writeUpdate($storage, $internalPath, null, $isCreateOperation ? $sizeDifference : null);
}
if ($result !== false && in_array('touch', $hooks)) {
- $this->writeUpdate($storage, $internalPath, $extraParam);
+ $this->writeUpdate($storage, $internalPath, $extraParam, 0);
}
if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) {
@@ -1353,7 +1406,7 @@ class View {
*
* @param string $path
* @param bool|string $includeMountPoints true to add mountpoint sizes,
- * 'ext' to add only ext storage mount point sizes. Defaults to true.
+ * 'ext' to add only ext storage mount point sizes. Defaults to true.
* @return \OC\Files\FileInfo|false False if file does not exist
*/
public function getFileInfo($path, $includeMountPoints = true) {
@@ -1361,9 +1414,6 @@ class View {
if (!Filesystem::isValidPath($path)) {
return false;
}
- if (Cache\Scanner::isPartialFile($path)) {
- return $this->getPartFileInfo($path);
- }
$relativePath = $path;
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
@@ -1374,15 +1424,23 @@ class View {
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
if (!$data instanceof ICacheEntry) {
+ if (Cache\Scanner::isPartialFile($relativePath)) {
+ return $this->getPartFileInfo($relativePath);
+ }
+
return false;
}
if ($mount instanceof MoveableMount && $internalPath === '') {
$data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
}
+ if ($internalPath === '' && $data['name']) {
+ $data['name'] = basename($path);
+ }
+
$ownerId = $storage->getOwner($internalPath);
$owner = null;
- if ($ownerId !== null && $ownerId !== false) {
+ if ($ownerId !== false) {
// ownerId might be null if files are accessed with an access token without file system access
$owner = $this->getUserObjectForOwner($ownerId);
}
@@ -1410,8 +1468,7 @@ class View {
public function addSubMounts(FileInfo $info, $extOnly = false): void {
$mounts = Filesystem::getMountManager()->findIn($info->getPath());
$info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) {
- $subStorage = $mount->getStorage();
- return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage);
+ return !($extOnly && $mount instanceof SharedMount);
}));
}
@@ -1422,7 +1479,7 @@ class View {
* @param string $mimetype_filter limit returned content to this mimetype or mimepart
* @return FileInfo[]
*/
- public function getDirectoryContent($directory, $mimetype_filter = '', \OCP\Files\FileInfo $directoryInfo = null) {
+ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Files\FileInfo $directoryInfo = null) {
$this->assertPathLength($directory);
if (!Filesystem::isValidPath($directory)) {
return [];
@@ -1468,13 +1525,25 @@ class View {
if ($sharingDisabled) {
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}
- $owner = $this->getUserObjectForOwner($storage->getOwner($content['path']));
+ $ownerId = $storage->getOwner($content['path']);
+ if ($ownerId !== false) {
+ $owner = $this->getUserObjectForOwner($ownerId);
+ } else {
+ $owner = null;
+ }
return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner);
}, $contents);
$files = array_combine($fileNames, $fileInfos);
//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
$mounts = Filesystem::getMountManager()->findIn($path);
+
+ // make sure nested mounts are sorted after their parent mounts
+ // otherwise doesn't propagate the etag across storage boundaries correctly
+ usort($mounts, function (IMountPoint $a, IMountPoint $b) {
+ return $a->getMountPoint() <=> $b->getMountPoint();
+ });
+
$dirLength = strlen($path);
foreach ($mounts as $mount) {
$mountPoint = $mount->getMountPoint();
@@ -1507,6 +1576,32 @@ class View {
if ($pos = strpos($relativePath, '/')) {
//mountpoint inside subfolder add size to the correct folder
$entryName = substr($relativePath, 0, $pos);
+
+ // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet
+ if (!isset($files[$entryName])) {
+ try {
+ [$storage, ] = $this->resolvePath($path . '/' . $entryName);
+ // make sure we can create the mountpoint folder, even if the user has a quota of 0
+ if ($storage->instanceOfStorage(Quota::class)) {
+ $storage->enableQuota(false);
+ }
+
+ if ($this->mkdir($path . '/' . $entryName) !== false) {
+ $info = $this->getFileInfo($path . '/' . $entryName);
+ if ($info !== false) {
+ $files[$entryName] = $info;
+ }
+ }
+
+ if ($storage->instanceOfStorage(Quota::class)) {
+ $storage->enableQuota(true);
+ }
+ } catch (\Exception $e) {
+ // Creating the parent folder might not be possible, for example due to a lack of permissions.
+ $this->logger->debug('Failed to create non-existent parent', ['exception' => $e, 'path' => $path . '/' . $entryName]);
+ }
+ }
+
if (isset($files[$entryName])) {
$files[$entryName]->addSubEntry($rootEntry, $mountPoint);
}
@@ -1529,7 +1624,12 @@ class View {
$rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}
- $owner = $this->getUserObjectForOwner($subStorage->getOwner(''));
+ $ownerId = $subStorage->getOwner('');
+ if ($ownerId !== false) {
+ $owner = $this->getUserObjectForOwner($ownerId);
+ } else {
+ $owner = null;
+ }
$files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner);
}
}
@@ -1646,7 +1746,12 @@ class View {
$internalPath = $result['path'];
$path = $mountPoint . $result['path'];
$result['path'] = substr($mountPoint . $result['path'], $rootLength);
- $owner = $userManager->get($storage->getOwner($internalPath));
+ $ownerId = $storage->getOwner($internalPath);
+ if ($ownerId !== false) {
+ $owner = $userManager->get($ownerId);
+ } else {
+ $owner = null;
+ }
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
}
}
@@ -1665,7 +1770,12 @@ class View {
$internalPath = $result['path'];
$result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
$path = rtrim($mountPoint . $internalPath, '/');
- $owner = $userManager->get($storage->getOwner($internalPath));
+ $ownerId = $storage->getOwner($internalPath);
+ if ($ownerId !== false) {
+ $owner = $userManager->get($ownerId);
+ } else {
+ $owner = null;
+ }
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
}
}
@@ -1678,11 +1788,9 @@ class View {
/**
* Get the owner for a file or folder
*
- * @param string $path
- * @return string the user id of the owner
* @throws NotFoundException
*/
- public function getOwner($path) {
+ public function getOwner(string $path): string {
$info = $this->getFileInfo($path);
if (!$info) {
throw new NotFoundException($path . ' not found while trying to get owner');
@@ -1720,43 +1828,25 @@ class View {
* @return string
* @throws NotFoundException
*/
- public function getPath($id, int $storageId = null) {
+ public function getPath($id, ?int $storageId = null): string {
$id = (int)$id;
- $manager = Filesystem::getMountManager();
- $mounts = $manager->findIn($this->fakeRoot);
- $mounts[] = $manager->find($this->fakeRoot);
- $mounts = array_filter($mounts);
- // reverse the array, so we start with the storage this view is in
- // which is the most likely to contain the file we're looking for
- $mounts = array_reverse($mounts);
-
- // put non-shared mounts in front of the shared mount
- // this prevents unneeded recursion into shares
- usort($mounts, function (IMountPoint $a, IMountPoint $b) {
- return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1;
- });
+ $rootFolder = Server::get(Files\IRootFolder::class);
- if (!is_null($storageId)) {
- $mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) {
- return $mount->getNumericStorageId() === $storageId;
- });
+ $node = $rootFolder->getFirstNodeByIdInPath($id, $this->getRoot());
+ if ($node) {
+ if ($storageId === null || $storageId === $node->getStorage()->getCache()->getNumericStorageId()) {
+ return $this->getRelativePath($node->getPath()) ?? '';
+ }
+ } else {
+ throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
}
- foreach ($mounts as $mount) {
- /**
- * @var \OC\Files\Mount\MountPoint $mount
- */
- if ($mount->getStorage()) {
- $cache = $mount->getStorage()->getCache();
- $internalPath = $cache->getPathById($id);
- if (is_string($internalPath)) {
- $fullPath = $mount->getMountPoint() . $internalPath;
- if (!is_null($path = $this->getRelativePath($fullPath))) {
- return $path;
- }
- }
+ foreach ($rootFolder->getByIdInPath($id, $this->getRoot()) as $node) {
+ if ($storageId === $node->getStorage()->getCache()->getNumericStorageId()) {
+ return $this->getRelativePath($node->getPath()) ?? '';
}
}
+
throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
}
@@ -1779,28 +1869,30 @@ class View {
* It is not allowed to move a mount point into a different mount point or
* into an already shared folder
*/
- private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath): bool {
- // note: cannot use the view because the target is already locked
- $fileId = $targetStorage->getCache()->getId($targetInternalPath);
- if ($fileId === -1) {
- // target might not exist, need to check parent instead
- $fileId = $targetStorage->getCache()->getId(dirname($targetInternalPath));
- }
-
- // check if any of the parents were shared by the current owner (include collections)
- $shares = Share::getItemShared(
- 'folder',
- (string)$fileId,
- \OC\Share\Constants::FORMAT_NONE,
- null,
- true
- );
-
- if (count($shares) > 0) {
- $this->logger->debug(
- 'It is not allowed to move one mount point into a shared folder',
- ['app' => 'files']);
- return false;
+ private function targetIsNotShared(string $user, string $targetPath): bool {
+ $providers = [
+ IShare::TYPE_USER,
+ IShare::TYPE_GROUP,
+ IShare::TYPE_EMAIL,
+ IShare::TYPE_CIRCLE,
+ IShare::TYPE_ROOM,
+ IShare::TYPE_DECK,
+ IShare::TYPE_SCIENCEMESH
+ ];
+ $shareManager = Server::get(IManager::class);
+ /** @var IShare[] $shares */
+ $shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) {
+ return $shareManager->getSharesBy($user, $type);
+ }, $providers));
+
+ foreach ($shares as $share) {
+ $sharedPath = $share->getNode()->getPath();
+ if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath . '/')) {
+ $this->logger->debug(
+ 'It is not allowed to move one mount point into a shared folder',
+ ['app' => 'files']);
+ return false;
+ }
}
return true;
@@ -1813,7 +1905,12 @@ class View {
$mount = $this->getMount($path);
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
- $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
+ $ownerId = $storage->getOwner($internalPath);
+ if ($ownerId !== false) {
+ $owner = Server::get(IUserManager::class)->get($ownerId);
+ } else {
+ $owner = null;
+ }
return new FileInfo(
$this->getAbsolutePath($path),
$storage,
@@ -1836,27 +1933,44 @@ class View {
/**
* @param string $path
* @param string $fileName
+ * @param bool $readonly Check only if the path is allowed for read-only access
* @throws InvalidPathException
*/
- public function verifyPath($path, $fileName): void {
+ public function verifyPath($path, $fileName, $readonly = false): void {
+ // All of the view's functions disallow '..' in the path so we can short cut if the path is invalid
+ if (!Filesystem::isValidPath($path ?: '/')) {
+ $l = \OCP\Util::getL10N('lib');
+ throw new InvalidPathException($l->t('Path contains invalid segments'));
+ }
+
+ // Short cut for read-only validation
+ if ($readonly) {
+ $validator = Server::get(FilenameValidator::class);
+ if ($validator->isForbidden($fileName)) {
+ $l = \OCP\Util::getL10N('lib');
+ throw new InvalidPathException($l->t('Filename is a reserved word'));
+ }
+ return;
+ }
+
try {
/** @type \OCP\Files\Storage $storage */
[$storage, $internalPath] = $this->resolvePath($path);
$storage->verifyPath($internalPath, $fileName);
} catch (ReservedWordException $ex) {
- $l = \OC::$server->getL10N('lib');
- throw new InvalidPathException($l->t('File name is a reserved word'));
+ $l = \OCP\Util::getL10N('lib');
+ throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename is a reserved word'));
} catch (InvalidCharacterInPathException $ex) {
- $l = \OC::$server->getL10N('lib');
- throw new InvalidPathException($l->t('File name contains at least one invalid character'));
+ $l = \OCP\Util::getL10N('lib');
+ throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename contains at least one invalid character'));
} catch (FileNameTooLongException $ex) {
- $l = \OC::$server->getL10N('lib');
- throw new InvalidPathException($l->t('File name is too long'));
+ $l = \OCP\Util::getL10N('lib');
+ throw new InvalidPathException($l->t('Filename is too long'));
} catch (InvalidDirectoryException $ex) {
- $l = \OC::$server->getL10N('lib');
+ $l = \OCP\Util::getL10N('lib');
throw new InvalidPathException($l->t('Dot files are not allowed'));
} catch (EmptyFileNameException $ex) {
- $l = \OC::$server->getL10N('lib');
+ $l = \OCP\Util::getL10N('lib');
throw new InvalidPathException($l->t('Empty filename is not allowed'));
}
}
@@ -1893,7 +2007,7 @@ class View {
*
* @param string $absolutePath absolute path
* @param bool $useParentMount true to return parent mount instead of whatever
- * is mounted directly on the given path, false otherwise
+ * is mounted directly on the given path, false otherwise
* @return IMountPoint mount point for which to apply locks
*/
private function getMountForLock(string $absolutePath, bool $useParentMount = false): IMountPoint {
@@ -1939,9 +2053,9 @@ class View {
);
}
} catch (LockedException $e) {
- // rethrow with the a human-readable path
+ // rethrow with the human-readable path
throw new LockedException(
- $this->getPathRelativeToFiles($absolutePath),
+ $path,
$e,
$e->getExistingLock()
);
@@ -1979,20 +2093,12 @@ class View {
);
}
} catch (LockedException $e) {
- try {
- // rethrow with the a human-readable path
- throw new LockedException(
- $this->getPathRelativeToFiles($absolutePath),
- $e,
- $e->getExistingLock()
- );
- } catch (\InvalidArgumentException $ex) {
- throw new LockedException(
- $absolutePath,
- $ex,
- $e->getExistingLock()
- );
- }
+ // rethrow with the a human-readable path
+ throw new LockedException(
+ $path,
+ $e,
+ $e->getExistingLock()
+ );
}
return true;
@@ -2107,7 +2213,7 @@ class View {
* @param string $absolutePath absolute path which is under "files"
*
* @return string path relative to "files" with trimmed slashes or null
- * if the path was NOT relative to files
+ * if the path was NOT relative to files
*
* @throws \InvalidArgumentException if the given path was not under "files"
* @since 8.1.0