2012-01-20 00:40:52 +01:00
|
|
|
<?php
|
2024-05-23 09:26:56 +02:00
|
|
|
|
2012-01-20 00:40:52 +01:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2012-05-31 17:02:35 +01:00
|
|
|
*/
|
2015-02-18 16:08:30 +01:00
|
|
|
namespace OC\Files;
|
|
|
|
|
2015-05-05 13:48:49 +02:00
|
|
|
use Icewind\Streams\CallbackWrapper;
|
2015-02-18 16:08:30 +01:00
|
|
|
use OC\Files\Mount\MoveableMount;
|
2015-11-25 13:53:31 +01:00
|
|
|
use OC\Files\Storage\Storage;
|
2022-04-19 13:05:38 +02:00
|
|
|
use OC\Share\Share;
|
|
|
|
use OC\User\LazyUser;
|
2023-03-13 18:33:12 +01:00
|
|
|
use OC\User\Manager as UserManager;
|
|
|
|
use OC\User\User;
|
2019-05-28 18:15:52 +02:00
|
|
|
use OCA\Files_Sharing\SharedMount;
|
2016-01-29 13:05:53 +01:00
|
|
|
use OCP\Constants;
|
2015-12-02 14:59:13 +01:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2023-09-06 09:16:55 +02:00
|
|
|
use OCP\Files\ConnectionLostException;
|
2016-11-09 10:58:11 +01:00
|
|
|
use OCP\Files\EmptyFileNameException;
|
2015-03-10 13:08:22 +01:00
|
|
|
use OCP\Files\FileNameTooLongException;
|
2024-04-24 14:50:13 +02:00
|
|
|
use OCP\Files\ForbiddenException;
|
2015-03-04 14:03:47 +01:00
|
|
|
use OCP\Files\InvalidCharacterInPathException;
|
2016-11-09 10:58:11 +01:00
|
|
|
use OCP\Files\InvalidDirectoryException;
|
2015-02-18 17:44:13 +01:00
|
|
|
use OCP\Files\InvalidPathException;
|
2024-11-28 19:08:27 +01:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2016-11-09 16:14:54 +01:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2015-06-15 14:10:10 +02:00
|
|
|
use OCP\Files\NotFoundException;
|
2015-03-04 14:03:47 +01:00
|
|
|
use OCP\Files\ReservedWordException;
|
2015-11-18 17:08:18 +01:00
|
|
|
use OCP\IUser;
|
2024-09-16 16:00:46 +02:00
|
|
|
use OCP\IUserManager;
|
2024-12-04 19:11:53 +01:00
|
|
|
use OCP\L10N\IFactory;
|
2015-05-04 15:05:09 +02:00
|
|
|
use OCP\Lock\ILockingProvider;
|
2015-05-22 13:43:44 +02:00
|
|
|
use OCP\Lock\LockedException;
|
2024-02-23 18:26:24 +01:00
|
|
|
use OCP\Server;
|
|
|
|
use OCP\Share\IManager;
|
|
|
|
use OCP\Share\IShare;
|
2022-03-30 10:55:41 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-02-18 16:08:30 +01:00
|
|
|
|
2012-05-31 17:02:35 +01:00
|
|
|
/**
|
2012-07-21 21:44:10 +02:00
|
|
|
* Class to provide access to ownCloud filesystem via a "view", and methods for
|
|
|
|
* working with files within that view (e.g. read, write, delete, etc.). Each
|
|
|
|
* view is restricted to a set of directories via a virtual root. The default view
|
|
|
|
* uses the currently logged in user's data directory as root (parts of
|
2014-05-12 16:30:39 +02:00
|
|
|
* OC_Filesystem are merely a wrapper for OC\Files\View).
|
2012-07-21 21:44:10 +02:00
|
|
|
*
|
2012-05-31 17:32:34 +01:00
|
|
|
* Apps that need to access files outside of the user data folders (to modify files
|
|
|
|
* belonging to a user other than the one currently logged in, for example) should
|
|
|
|
* use this class directly rather than using OC_Filesystem, or making use of PHP's
|
2012-07-21 21:44:10 +02:00
|
|
|
* built-in file manipulation functions. This will ensure all hooks and proxies
|
2012-05-31 17:32:34 +01:00
|
|
|
* are triggered correctly.
|
2012-05-31 17:57:34 +01:00
|
|
|
*
|
2012-07-21 21:44:10 +02:00
|
|
|
* Filesystem functions are not called directly; they are passed to the correct
|
2012-09-07 18:30:48 +02:00
|
|
|
* \OC\Files\Storage\Storage object
|
2012-05-31 17:02:35 +01:00
|
|
|
*/
|
2012-10-10 11:54:44 +02:00
|
|
|
class View {
|
2023-03-13 18:33:12 +01:00
|
|
|
private string $fakeRoot = '';
|
|
|
|
private ILockingProvider $lockingProvider;
|
|
|
|
private bool $lockingEnabled;
|
|
|
|
private bool $updaterEnabled = true;
|
|
|
|
private UserManager $userManager;
|
2022-03-30 10:55:41 +02:00
|
|
|
private LoggerInterface $logger;
|
2017-03-20 11:06:08 +01:00
|
|
|
|
2015-02-18 16:01:24 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception If $root contains an invalid path
|
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function __construct(string $root = '') {
|
2015-05-04 15:05:09 +02:00
|
|
|
if (!Filesystem::isValidPath($root)) {
|
2015-02-18 16:01:24 +01:00
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
|
2012-10-10 11:54:44 +02:00
|
|
|
$this->fakeRoot = $root;
|
2023-08-29 18:16:45 -05:00
|
|
|
$this->lockingProvider = \OC::$server->get(ILockingProvider::class);
|
2015-06-30 13:45:41 +02:00
|
|
|
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
|
2015-12-30 14:28:53 +01:00
|
|
|
$this->userManager = \OC::$server->getUserManager();
|
2022-03-30 10:55:41 +02:00
|
|
|
$this->logger = \OC::$server->get(LoggerInterface::class);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-03-27 02:24:52 +02:00
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
/**
|
|
|
|
* @param ?string $path
|
|
|
|
* @psalm-template S as string|null
|
|
|
|
* @psalm-param S $path
|
|
|
|
* @psalm-return (S is string ? string : null)
|
|
|
|
*/
|
|
|
|
public function getAbsolutePath($path = '/'): ?string {
|
2015-04-22 14:51:02 +02:00
|
|
|
if ($path === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2014-05-13 14:17:51 +02:00
|
|
|
if ($path === '') {
|
2012-10-10 11:54:44 +02:00
|
|
|
$path = '/';
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($path[0] !== '/') {
|
|
|
|
$path = '/' . $path;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
return $this->fakeRoot . $path;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2012-01-20 00:40:52 +01:00
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Change the root to a fake root
|
2012-10-10 11:54:44 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param string $fakeRoot
|
2012-10-10 11:54:44 +02:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function chroot($fakeRoot): void {
|
2012-10-10 11:54:44 +02:00
|
|
|
if (!$fakeRoot == '') {
|
|
|
|
if ($fakeRoot[0] !== '/') {
|
|
|
|
$fakeRoot = '/' . $fakeRoot;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
$this->fakeRoot = $fakeRoot;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Get the fake root
|
2012-01-20 00:40:52 +01:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function getRoot(): string {
|
2012-01-20 00:40:52 +01:00
|
|
|
return $this->fakeRoot;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:33:57 +02:00
|
|
|
/**
|
|
|
|
* get path relative to the root of the view
|
2012-10-10 11:54:44 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param string $path
|
2012-06-09 17:33:57 +02:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function getRelativePath($path): ?string {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($this->fakeRoot == '') {
|
2012-06-09 17:33:57 +02:00
|
|
|
return $path;
|
|
|
|
}
|
2015-02-04 12:07:16 +01:00
|
|
|
|
2015-05-04 15:05:09 +02:00
|
|
|
if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
|
2015-02-04 12:07:16 +01:00
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:31:32 +02:00
|
|
|
// missing slashes can cause wrong matches!
|
|
|
|
$root = rtrim($this->fakeRoot, '/') . '/';
|
|
|
|
|
2023-05-15 15:17:19 +03:30
|
|
|
if (!str_starts_with($path, $root)) {
|
2012-06-09 17:33:57 +02:00
|
|
|
return null;
|
2012-10-10 11:54:44 +02:00
|
|
|
} else {
|
|
|
|
$path = substr($path, strlen($this->fakeRoot));
|
|
|
|
if (strlen($path) === 0) {
|
2012-08-14 03:07:14 +02:00
|
|
|
return '/';
|
2012-10-10 11:54:44 +02:00
|
|
|
} else {
|
2012-08-14 03:07:14 +02:00
|
|
|
return $path;
|
|
|
|
}
|
2012-06-09 17:33:57 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-21 21:44:10 +02:00
|
|
|
|
2012-01-20 00:40:52 +01:00
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Get the mountpoint of the storage object for a path
|
2013-02-11 17:44:02 +01:00
|
|
|
* ( note: because a storage is not always mounted inside the fakeroot, the
|
|
|
|
* returned mountpoint is relative to the absolute root of the filesystem
|
2015-02-18 17:44:13 +01:00
|
|
|
* and does not take the chroot into account )
|
2012-10-10 11:54:44 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param string $path
|
2012-10-10 11:54:44 +02:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function getMountPoint($path): string {
|
2012-10-10 12:25:46 +02:00
|
|
|
return Filesystem::getMountPoint($this->getAbsolutePath($path));
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:24:48 +01:00
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Get the mountpoint of the storage object for a path
|
2014-12-16 14:24:48 +01:00
|
|
|
* ( note: because a storage is not always mounted inside the fakeroot, the
|
|
|
|
* returned mountpoint is relative to the absolute root of the filesystem
|
2015-02-18 17:44:13 +01:00
|
|
|
* and does not take the chroot into account )
|
2014-12-16 14:24:48 +01:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function getMount($path): IMountPoint {
|
2014-12-16 14:24:48 +01:00
|
|
|
return Filesystem::getMountManager()->find($this->getAbsolutePath($path));
|
|
|
|
}
|
|
|
|
|
2012-11-08 17:42:26 +01:00
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Resolve a path to a storage and internal path
|
2012-11-08 17:42:26 +01:00
|
|
|
*
|
|
|
|
* @param string $path
|
2023-03-13 18:33:12 +01:00
|
|
|
* @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path
|
2012-11-08 17:42:26 +01:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function resolvePath($path): array {
|
2013-10-10 16:06:26 +02:00
|
|
|
$a = $this->getAbsolutePath($path);
|
|
|
|
$p = Filesystem::normalizePath($a);
|
|
|
|
return Filesystem::resolvePath($p);
|
2012-11-08 17:42:26 +01:00
|
|
|
}
|
|
|
|
|
2012-01-20 00:40:52 +01:00
|
|
|
/**
|
2023-03-13 18:33:12 +01:00
|
|
|
* Return the path to a local version of the file
|
2013-02-11 17:44:02 +01:00
|
|
|
* we need this because we can't know if a file is stored local or not from
|
|
|
|
* outside the filestorage and for some purposes a local file is needed
|
2012-10-10 11:54:44 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param string $path
|
2012-10-10 11:54:44 +02:00
|
|
|
*/
|
2023-03-13 18:59:16 +01:00
|
|
|
public function getLocalFile($path): string|false {
|
2023-03-13 18:33:12 +01:00
|
|
|
$parent = substr($path, 0, strrpos($path, '/') ?: 0);
|
2012-10-27 12:17:35 +02:00
|
|
|
$path = $this->getAbsolutePath($path);
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = Filesystem::resolvePath($path);
|
2023-05-02 10:10:02 +02:00
|
|
|
if (Filesystem::isValidPath($parent) && $storage) {
|
2012-10-10 17:46:29 +02:00
|
|
|
return $storage->getLocalFile($internalPath);
|
2012-10-10 12:25:46 +02:00
|
|
|
} else {
|
2023-03-13 18:33:12 +01:00
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2012-01-20 00:40:52 +01:00
|
|
|
/**
|
2012-07-21 21:44:10 +02:00
|
|
|
* the following functions operate with arguments and return values identical
|
|
|
|
* to those of their PHP built-in equivalents. Mostly they are merely wrappers
|
2012-09-07 18:30:48 +02:00
|
|
|
* for \OC\Files\Storage\Storage via basicOperation().
|
2012-01-20 00:40:52 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function mkdir($path) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->basicOperation('mkdir', $path, ['create', 'write']);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2014-07-02 17:53:54 +02:00
|
|
|
/**
|
|
|
|
* remove mount point
|
|
|
|
*
|
2022-03-03 17:15:02 +01:00
|
|
|
* @param IMountPoint $mount
|
2014-07-02 17:53:54 +02:00
|
|
|
* @param string $path relative to data/
|
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
protected function removeMount($mount, $path): bool {
|
2014-06-16 16:24:42 +02:00
|
|
|
if ($mount instanceof MoveableMount) {
|
2014-07-02 17:53:54 +02:00
|
|
|
// cut of /user/files to get the relative path to data/user/files
|
2014-08-04 14:29:46 +02:00
|
|
|
$pathParts = explode('/', $path, 4);
|
2014-07-02 17:53:54 +02:00
|
|
|
$relPath = '/' . $pathParts[3];
|
2016-03-24 11:40:14 +01:00
|
|
|
$this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true);
|
2014-06-16 16:24:42 +02:00
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME, 'umount',
|
2020-03-26 09:30:18 +01:00
|
|
|
[Filesystem::signal_param_path => $relPath]
|
2014-06-16 16:24:42 +02:00
|
|
|
);
|
2016-03-24 11:40:14 +01:00
|
|
|
$this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true);
|
2014-06-16 16:24:42 +02:00
|
|
|
$result = $mount->removeMount();
|
2016-03-24 11:40:14 +01:00
|
|
|
$this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true);
|
2014-06-16 16:24:42 +02:00
|
|
|
if ($result) {
|
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME, 'post_umount',
|
2020-03-26 09:30:18 +01:00
|
|
|
[Filesystem::signal_param_path => $relPath]
|
2014-06-16 16:24:42 +02:00
|
|
|
);
|
|
|
|
}
|
2016-03-24 11:40:14 +01:00
|
|
|
$this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true);
|
2014-06-16 16:24:42 +02:00
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
// do not allow deleting the storage's root / the mount point
|
|
|
|
// because for some storages it might delete the whole contents
|
|
|
|
// but isn't supposed to work that way
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
public function disableCacheUpdate(): void {
|
2015-11-25 13:53:31 +01:00
|
|
|
$this->updaterEnabled = false;
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
public function enableCacheUpdate(): void {
|
2015-11-25 13:53:31 +01:00
|
|
|
$this->updaterEnabled = true;
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:56:33 +01:00
|
|
|
protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void {
|
2015-11-25 13:53:31 +01:00
|
|
|
if ($this->updaterEnabled) {
|
|
|
|
if (is_null($time)) {
|
|
|
|
$time = time();
|
|
|
|
}
|
2024-01-05 14:56:33 +01:00
|
|
|
$storage->getUpdater()->update($internalPath, $time, $sizeDifference);
|
2015-11-25 13:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
protected function removeUpdate(Storage $storage, string $internalPath): void {
|
2015-11-25 13:53:31 +01:00
|
|
|
if ($this->updaterEnabled) {
|
|
|
|
$storage->getUpdater()->remove($internalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
|
2015-11-25 13:53:31 +01:00
|
|
|
if ($this->updaterEnabled) {
|
|
|
|
$targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-03 16:24:28 +01:00
|
|
|
protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
|
|
|
|
if ($this->updaterEnabled) {
|
|
|
|
$targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:51:37 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function rmdir($path) {
|
2014-08-04 14:29:46 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2014-06-16 16:24:42 +02:00
|
|
|
$mount = Filesystem::getMountManager()->find($absolutePath);
|
|
|
|
if ($mount->getInternalPath($absolutePath) === '') {
|
2015-10-16 14:14:00 +02:00
|
|
|
return $this->removeMount($mount, $absolutePath);
|
2014-06-16 16:24:42 +02:00
|
|
|
}
|
2013-12-02 22:43:58 +01:00
|
|
|
if ($this->is_dir($path)) {
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = $this->basicOperation('rmdir', $path, ['delete']);
|
2013-12-02 22:43:58 +01:00
|
|
|
} else {
|
2016-05-24 15:07:23 +02:00
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
$internalPath = $mount->getInternalPath($absolutePath);
|
|
|
|
$storage->getUpdater()->remove($internalPath);
|
2013-12-02 22:43:58 +01:00
|
|
|
}
|
2016-05-24 15:07:23 +02:00
|
|
|
return $result;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2014-05-12 23:56:31 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2023-03-13 18:59:16 +01:00
|
|
|
* @return resource|false
|
2014-05-12 23:56:31 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function opendir($path) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->basicOperation('opendir', $path, ['read']);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function is_dir($path) {
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($path == '/') {
|
2012-01-20 00:40:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-21 21:44:10 +02:00
|
|
|
return $this->basicOperation('is_dir', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function is_file($path) {
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($path == '/') {
|
2012-01-20 00:40:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-21 21:44:10 +02:00
|
|
|
return $this->basicOperation('is_file', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function stat($path) {
|
|
|
|
return $this->basicOperation('stat', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function filetype($path) {
|
|
|
|
return $this->basicOperation('filetype', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2023-01-20 18:02:45 +01:00
|
|
|
public function filesize(string $path) {
|
2012-07-21 21:44:10 +02:00
|
|
|
return $this->basicOperation('filesize', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function readfile($path) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2021-04-10 15:05:18 +02:00
|
|
|
if (ob_get_level()) {
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
$handle = $this->fopen($path, 'rb');
|
2012-04-01 02:38:26 -04:00
|
|
|
if ($handle) {
|
2023-09-06 09:16:55 +02:00
|
|
|
$chunkSize = 524288; // 512 kiB chunks
|
2012-04-01 02:38:26 -04:00
|
|
|
while (!feof($handle)) {
|
|
|
|
echo fread($handle, $chunkSize);
|
|
|
|
flush();
|
2023-09-06 09:16:55 +02:00
|
|
|
$this->checkConnectionStatus();
|
2012-04-01 02:38:26 -04:00
|
|
|
}
|
2016-11-18 12:19:49 +01:00
|
|
|
fclose($handle);
|
2018-01-26 00:02:03 +01:00
|
|
|
return $this->filesize($path);
|
2012-02-26 03:54:21 +01:00
|
|
|
}
|
2012-04-01 02:38:26 -04:00
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2016-05-20 18:16:44 +02:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2016-05-24 15:07:23 +02:00
|
|
|
* @param int $from
|
2016-05-20 18:16:44 +02:00
|
|
|
* @param int $to
|
|
|
|
* @return bool|mixed
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2016-05-23 15:17:00 +02:00
|
|
|
* @throws \OCP\Files\UnseekableException
|
2016-05-20 18:16:44 +02:00
|
|
|
*/
|
|
|
|
public function readfilePart($path, $from, $to) {
|
|
|
|
$this->assertPathLength($path);
|
2021-04-10 15:05:18 +02:00
|
|
|
if (ob_get_level()) {
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
2016-05-20 18:16:44 +02:00
|
|
|
$handle = $this->fopen($path, 'rb');
|
|
|
|
if ($handle) {
|
2023-09-06 09:16:55 +02:00
|
|
|
$chunkSize = 524288; // 512 kiB chunks
|
2017-10-26 16:47:54 +02:00
|
|
|
$startReading = true;
|
|
|
|
|
|
|
|
if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
|
|
|
|
// forward file handle via chunked fread because fseek seem to have failed
|
|
|
|
|
|
|
|
$end = $from + 1;
|
2021-02-26 11:12:20 +01:00
|
|
|
while (!feof($handle) && ftell($handle) < $end && ftell($handle) !== $from) {
|
2017-10-26 16:47:54 +02:00
|
|
|
$len = $from - ftell($handle);
|
|
|
|
if ($len > $chunkSize) {
|
|
|
|
$len = $chunkSize;
|
|
|
|
}
|
|
|
|
$result = fread($handle, $len);
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
$startReading = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($startReading) {
|
2016-05-24 15:07:23 +02:00
|
|
|
$end = $to + 1;
|
|
|
|
while (!feof($handle) && ftell($handle) < $end) {
|
|
|
|
$len = $end - ftell($handle);
|
|
|
|
if ($len > $chunkSize) {
|
|
|
|
$len = $chunkSize;
|
|
|
|
}
|
|
|
|
echo fread($handle, $len);
|
|
|
|
flush();
|
2023-09-06 09:16:55 +02:00
|
|
|
$this->checkConnectionStatus();
|
2016-05-20 18:16:44 +02:00
|
|
|
}
|
2018-01-26 00:02:03 +01:00
|
|
|
return ftell($handle) - $from;
|
2016-05-20 18:16:44 +02:00
|
|
|
}
|
2016-05-23 15:17:00 +02:00
|
|
|
|
|
|
|
throw new \OCP\Files\UnseekableException('fseek error');
|
2016-05-20 18:16:44 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-06 09:16:55 +02:00
|
|
|
private function checkConnectionStatus(): void {
|
|
|
|
$connectionStatus = \connection_status();
|
|
|
|
if ($connectionStatus !== CONNECTION_NORMAL) {
|
|
|
|
throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-24 17:42:07 -04:00
|
|
|
public function isCreatable($path) {
|
|
|
|
return $this->basicOperation('isCreatable', $path);
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-24 17:42:07 -04:00
|
|
|
public function isReadable($path) {
|
|
|
|
return $this->basicOperation('isReadable', $path);
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-24 17:42:07 -04:00
|
|
|
public function isUpdatable($path) {
|
|
|
|
return $this->basicOperation('isUpdatable', $path);
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-24 17:42:07 -04:00
|
|
|
public function isDeletable($path) {
|
2014-12-17 16:09:28 +01:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
|
|
|
$mount = Filesystem::getMountManager()->find($absolutePath);
|
|
|
|
if ($mount->getInternalPath($absolutePath) === '') {
|
|
|
|
return $mount instanceof MoveableMount;
|
|
|
|
}
|
2012-07-24 17:42:07 -04:00
|
|
|
return $this->basicOperation('isDeletable', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-24 17:42:07 -04:00
|
|
|
public function isSharable($path) {
|
|
|
|
return $this->basicOperation('isSharable', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function file_exists($path) {
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($path == '/') {
|
2012-01-20 00:40:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-21 21:44:10 +02:00
|
|
|
return $this->basicOperation('file_exists', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function filemtime($path) {
|
|
|
|
return $this->basicOperation('filemtime', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param int|string $mtime
|
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function touch($path, $mtime = null): bool {
|
2023-05-02 10:10:02 +02:00
|
|
|
if (!is_null($mtime) && !is_numeric($mtime)) {
|
2012-10-23 16:34:58 +02:00
|
|
|
$mtime = strtotime($mtime);
|
|
|
|
}
|
2013-04-22 21:23:12 +02:00
|
|
|
|
2020-03-26 09:30:18 +01:00
|
|
|
$hooks = ['touch'];
|
2013-04-22 21:23:12 +02:00
|
|
|
|
2013-03-07 15:51:44 +01:00
|
|
|
if (!$this->file_exists($path)) {
|
2013-08-29 15:31:03 +02:00
|
|
|
$hooks[] = 'create';
|
2013-03-07 15:51:44 +01:00
|
|
|
$hooks[] = 'write';
|
|
|
|
}
|
2019-06-06 16:09:27 +02:00
|
|
|
try {
|
|
|
|
$result = $this->basicOperation('touch', $path, $hooks, $mtime);
|
|
|
|
} catch (\Exception $e) {
|
2022-03-30 10:55:41 +02:00
|
|
|
$this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]);
|
2019-06-06 16:09:27 +02:00
|
|
|
$result = false;
|
|
|
|
}
|
2014-09-03 18:20:09 +02:00
|
|
|
if (!$result) {
|
2014-09-03 18:12:36 +02:00
|
|
|
// If create file fails because of permissions on external storage like SMB folders,
|
2014-09-03 18:20:09 +02:00
|
|
|
// check file exists and return false if not.
|
2014-10-02 17:37:33 +02:00
|
|
|
if (!$this->file_exists($path)) {
|
2014-09-03 18:20:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 14:29:46 +02:00
|
|
|
if (is_null($mtime)) {
|
|
|
|
$mtime = time();
|
|
|
|
}
|
2014-09-03 18:20:09 +02:00
|
|
|
//if native touch fails, we emulate it by changing the mtime in the cache
|
2020-03-26 09:30:18 +01:00
|
|
|
$this->putFileInfo($path, ['mtime' => floor($mtime)]);
|
2013-02-10 12:44:27 +01:00
|
|
|
}
|
|
|
|
return true;
|
2012-02-10 11:30:38 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2023-04-27 09:56:05 +02:00
|
|
|
* @return string|false
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function file_get_contents($path) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->basicOperation('file_get_contents', $path, ['read']);
|
2012-07-21 21:44:10 +02:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
protected function emit_file_hooks_pre(bool $exists, string $path, bool &$run): void {
|
2014-04-28 17:45:03 +02:00
|
|
|
if (!$exists) {
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
|
|
|
Filesystem::signal_param_run => &$run,
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
} else {
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
|
|
|
Filesystem::signal_param_run => &$run,
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
}
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
|
|
|
Filesystem::signal_param_run => &$run,
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 18:33:12 +01:00
|
|
|
protected function emit_file_hooks_post(bool $exists, string $path): void {
|
2014-04-28 17:45:03 +02:00
|
|
|
if (!$exists) {
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
} else {
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
}
|
2020-03-26 09:30:18 +01:00
|
|
|
\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, [
|
2014-04-28 17:45:03 +02:00
|
|
|
Filesystem::signal_param_path => $this->getHookPath($path),
|
2020-03-26 09:30:18 +01:00
|
|
|
]);
|
2014-04-28 17:45:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2018-06-26 11:07:41 +02:00
|
|
|
* @param string|resource $data
|
2015-02-18 16:08:30 +01:00
|
|
|
* @return bool|mixed
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function file_put_contents($path, $data) {
|
2012-10-10 11:54:44 +02:00
|
|
|
if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
|
2012-10-10 12:25:46 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
|
2015-03-30 22:45:58 +02:00
|
|
|
if (Filesystem::isValidPath($path)
|
2023-05-02 10:10:02 +02:00
|
|
|
&& !Filesystem::isFileBlacklisted($path)
|
2013-04-22 21:23:12 +02:00
|
|
|
) {
|
2012-07-26 22:53:55 -04:00
|
|
|
$path = $this->getRelativePath($absolutePath);
|
2023-03-28 10:34:01 +02:00
|
|
|
if ($path === null) {
|
|
|
|
throw new InvalidPathException("Path $absolutePath is not in the expected root");
|
|
|
|
}
|
2015-05-29 16:35:49 +02:00
|
|
|
|
2015-06-03 16:23:43 +02:00
|
|
|
$this->lockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
|
2012-07-26 22:53:55 -04:00
|
|
|
$exists = $this->file_exists($path);
|
2013-08-17 10:57:31 +02:00
|
|
|
if ($this->shouldEmitHooks($path)) {
|
2025-01-26 14:56:17 +01:00
|
|
|
$run = true;
|
2014-04-28 17:45:03 +02:00
|
|
|
$this->emit_file_hooks_pre($exists, $path, $run);
|
2025-01-26 14:56:17 +01:00
|
|
|
if (!$run) {
|
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-26 22:53:55 -04:00
|
|
|
}
|
2015-06-03 16:23:43 +02:00
|
|
|
|
2020-10-27 22:57:24 -04:00
|
|
|
try {
|
|
|
|
$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Release the shared lock before throwing.
|
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-06-03 16:23:43 +02:00
|
|
|
|
2023-04-05 09:13:39 +02:00
|
|
|
/** @var Storage $storage */
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = $this->resolvePath($path);
|
2015-06-03 16:23:43 +02:00
|
|
|
$target = $storage->fopen($internalPath, 'w');
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($target) {
|
2020-09-15 17:01:33 +02:00
|
|
|
[, $result] = \OC_Helper::streamCopy($data, $target);
|
2012-07-26 22:53:55 -04:00
|
|
|
fclose($target);
|
|
|
|
fclose($data);
|
2015-06-03 16:51:21 +02:00
|
|
|
|
2015-11-25 13:53:31 +01:00
|
|
|
$this->writeUpdate($storage, $internalPath);
|
2015-06-03 16:23:43 +02:00
|
|
|
|
2015-06-19 13:54:00 +02:00
|
|
|
$this->changeLock($path, ILockingProvider::LOCK_SHARED);
|
2015-06-03 16:23:43 +02:00
|
|
|
|
2013-08-17 10:57:31 +02:00
|
|
|
if ($this->shouldEmitHooks($path) && $result !== false) {
|
2014-04-28 17:45:03 +02:00
|
|
|
$this->emit_file_hooks_post($exists, $path);
|
2012-07-26 22:53:55 -04:00
|
|
|
}
|
2015-06-19 13:54:00 +02:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
2013-02-22 16:43:11 +01:00
|
|
|
return $result;
|
2012-10-10 11:54:44 +02:00
|
|
|
} else {
|
2015-06-03 16:23:43 +02:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
|
2012-07-26 22:53:55 -04:00
|
|
|
return false;
|
2012-04-25 00:09:14 +02:00
|
|
|
}
|
2012-10-10 12:25:46 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-02-15 16:23:00 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
} else {
|
2020-03-26 09:30:18 +01:00
|
|
|
$hooks = $this->file_exists($path) ? ['update', 'write'] : ['create', 'write'];
|
2014-08-05 15:12:20 +02:00
|
|
|
return $this->basicOperation('file_put_contents', $path, $hooks, $data);
|
2012-02-15 16:23:00 +01:00
|
|
|
}
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function unlink($path) {
|
2014-01-08 13:17:36 +01:00
|
|
|
if ($path === '' || $path === '/') {
|
|
|
|
// do not allow deleting the root
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-25 22:26:47 +01:00
|
|
|
$postFix = (substr($path, -1) === '/') ? '/' : '';
|
2014-01-08 13:17:36 +01:00
|
|
|
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
|
2014-05-22 01:39:24 +02:00
|
|
|
$mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
|
2022-03-03 17:15:02 +01:00
|
|
|
if ($mount->getInternalPath($absolutePath) === '') {
|
2014-07-02 17:53:54 +02:00
|
|
|
return $this->removeMount($mount, $absolutePath);
|
2014-01-08 13:17:36 +01:00
|
|
|
}
|
2017-02-08 15:56:34 +01:00
|
|
|
if ($this->is_dir($path)) {
|
|
|
|
$result = $this->basicOperation('rmdir', $path, ['delete']);
|
|
|
|
} else {
|
|
|
|
$result = $this->basicOperation('unlink', $path, ['delete']);
|
|
|
|
}
|
2016-05-24 15:07:23 +02:00
|
|
|
if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
$internalPath = $mount->getInternalPath($absolutePath);
|
|
|
|
$storage->getUpdater()->remove($internalPath);
|
2016-06-03 13:34:54 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $result;
|
2016-05-24 15:07:23 +02:00
|
|
|
}
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2014-02-06 16:30:58 +01:00
|
|
|
/**
|
|
|
|
* @param string $directory
|
2015-01-13 14:51:37 +01:00
|
|
|
* @return bool|mixed
|
2014-02-06 16:30:58 +01:00
|
|
|
*/
|
2015-01-13 16:56:15 +01:00
|
|
|
public function deleteAll($directory) {
|
2013-09-07 14:10:51 +02:00
|
|
|
return $this->rmdir($directory);
|
2012-06-21 18:07:21 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-01-13 14:51:37 +01:00
|
|
|
/**
|
2015-02-25 12:44:44 +01:00
|
|
|
* Rename/move a file or folder from the source path to target path.
|
|
|
|
*
|
2022-10-18 12:49:34 +02:00
|
|
|
* @param string $source source path
|
|
|
|
* @param string $target target path
|
2025-01-03 17:41:39 +01:00
|
|
|
* @param array $options
|
2015-02-25 12:44:44 +01:00
|
|
|
*
|
2015-01-13 14:51:37 +01:00
|
|
|
* @return bool|mixed
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-01-13 14:51:37 +01:00
|
|
|
*/
|
2025-01-03 17:41:39 +01:00
|
|
|
public function rename($source, $target, array $options = []) {
|
|
|
|
$checkSubMounts = $options['checkSubMounts'] ?? true;
|
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
|
|
|
|
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
|
2024-04-24 14:50:13 +02:00
|
|
|
|
|
|
|
if (str_starts_with($absolutePath2, $absolutePath1 . '/')) {
|
|
|
|
throw new ForbiddenException('Moving a folder into a child folder is forbidden', false);
|
|
|
|
}
|
|
|
|
|
2024-11-28 19:08:27 +01:00
|
|
|
/** @var IMountManager $mountManager */
|
|
|
|
$mountManager = \OC::$server->get(IMountManager::class);
|
|
|
|
|
2024-02-23 18:26:24 +01:00
|
|
|
$targetParts = explode('/', $absolutePath2);
|
|
|
|
$targetUser = $targetParts[1] ?? null;
|
2015-06-19 13:54:00 +02:00
|
|
|
$result = false;
|
2013-04-22 21:23:12 +02:00
|
|
|
if (
|
2022-10-18 12:49:34 +02:00
|
|
|
Filesystem::isValidPath($target)
|
2023-05-02 10:10:02 +02:00
|
|
|
&& Filesystem::isValidPath($source)
|
|
|
|
&& !Filesystem::isFileBlacklisted($target)
|
2013-04-22 21:23:12 +02:00
|
|
|
) {
|
2022-10-18 12:49:34 +02:00
|
|
|
$source = $this->getRelativePath($absolutePath1);
|
|
|
|
$target = $this->getRelativePath($absolutePath2);
|
|
|
|
$exists = $this->file_exists($target);
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2023-05-02 10:10:02 +02:00
|
|
|
if ($source == null || $target == null) {
|
2012-06-09 17:33:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-05-29 16:35:49 +02:00
|
|
|
|
2024-07-15 16:28:43 +02:00
|
|
|
try {
|
|
|
|
$this->verifyPath(dirname($target), basename($target));
|
|
|
|
} catch (InvalidPathException) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->lockFile($source, ILockingProvider::LOCK_SHARED, true);
|
2015-06-29 16:45:08 +02:00
|
|
|
try {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->lockFile($target, ILockingProvider::LOCK_SHARED, true);
|
2015-05-29 16:35:49 +02:00
|
|
|
|
2017-10-30 18:12:37 +01:00
|
|
|
$run = true;
|
2022-10-18 12:49:34 +02:00
|
|
|
if ($this->shouldEmitHooks($source) && (Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target))) {
|
2017-10-30 18:12:37 +01:00
|
|
|
// if it was a rename from a part file to a regular file it was a write and not a rename operation
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->emit_file_hooks_pre($exists, $target, $run);
|
|
|
|
} elseif ($this->shouldEmitHooks($source)) {
|
2023-07-19 16:39:04 +02:00
|
|
|
$sourcePath = $this->getHookPath($source);
|
|
|
|
$targetPath = $this->getHookPath($target);
|
|
|
|
if ($sourcePath !== null && $targetPath !== null) {
|
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME, Filesystem::signal_rename,
|
|
|
|
[
|
|
|
|
Filesystem::signal_param_oldpath => $sourcePath,
|
|
|
|
Filesystem::signal_param_newpath => $targetPath,
|
|
|
|
Filesystem::signal_param_run => &$run
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2017-10-30 18:12:37 +01:00
|
|
|
}
|
|
|
|
if ($run) {
|
|
|
|
$manager = Filesystem::getMountManager();
|
2022-10-18 12:49:34 +02:00
|
|
|
$mount1 = $this->getMount($source);
|
|
|
|
$mount2 = $this->getMount($target);
|
2017-10-30 18:12:37 +01:00
|
|
|
$storage1 = $mount1->getStorage();
|
|
|
|
$storage2 = $mount2->getStorage();
|
|
|
|
$internalPath1 = $mount1->getInternalPath($absolutePath1);
|
|
|
|
$internalPath2 = $mount2->getInternalPath($absolutePath2);
|
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->changeLock($source, ILockingProvider::LOCK_EXCLUSIVE, true);
|
2017-10-30 18:12:37 +01:00
|
|
|
try {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true);
|
2017-10-30 18:12:37 +01:00
|
|
|
|
2025-01-03 17:41:39 +01:00
|
|
|
if ($checkSubMounts) {
|
|
|
|
$movedMounts = $mountManager->findIn($this->getAbsolutePath($source));
|
|
|
|
} else {
|
|
|
|
$movedMounts = [];
|
|
|
|
}
|
2024-11-28 19:08:27 +01:00
|
|
|
|
2017-10-30 18:12:37 +01:00
|
|
|
if ($internalPath1 === '') {
|
2024-11-28 19:08:27 +01:00
|
|
|
$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());
|
|
|
|
|
2024-03-28 16:13:19 +01:00
|
|
|
// moving a file/folder within the same mount point
|
2017-10-30 18:12:37 +01:00
|
|
|
} elseif ($storage1 === $storage2) {
|
2024-11-28 19:08:27 +01:00
|
|
|
if (count($movedMounts) > 0) {
|
|
|
|
$this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
|
|
|
|
}
|
2017-10-30 18:12:37 +01:00
|
|
|
if ($storage1) {
|
|
|
|
$result = $storage1->rename($internalPath1, $internalPath2);
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
2024-03-28 16:13:19 +01:00
|
|
|
// moving a file/folder between storages (from $storage1 to $storage2)
|
2017-07-03 14:58:34 +02:00
|
|
|
} else {
|
2024-11-28 19:08:27 +01:00
|
|
|
if (count($movedMounts) > 0) {
|
|
|
|
$this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
|
|
|
|
}
|
2017-10-30 18:12:37 +01:00
|
|
|
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
|
2017-07-03 14:58:34 +02:00
|
|
|
}
|
2015-11-25 13:53:31 +01:00
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) {
|
2017-10-30 18:12:37 +01:00
|
|
|
// if it was a rename from a part file to a regular file it was a write and not a rename operation
|
|
|
|
$this->writeUpdate($storage2, $internalPath2);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif ($result) {
|
2017-10-30 18:12:37 +01:00
|
|
|
if ($internalPath1 !== '') { // don't do a cache update for moved mounts
|
|
|
|
$this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw $e;
|
|
|
|
} finally {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->changeLock($source, ILockingProvider::LOCK_SHARED, true);
|
|
|
|
$this->changeLock($target, ILockingProvider::LOCK_SHARED, true);
|
2015-06-18 13:47:03 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
if ((Cache\Scanner::isPartialFile($source) && !Cache\Scanner::isPartialFile($target)) && $result !== false) {
|
2017-10-30 18:12:37 +01:00
|
|
|
if ($this->shouldEmitHooks()) {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->emit_file_hooks_post($exists, $target);
|
2017-10-30 18:12:37 +01:00
|
|
|
}
|
|
|
|
} elseif ($result) {
|
2023-05-02 10:10:02 +02:00
|
|
|
if ($this->shouldEmitHooks($source) && $this->shouldEmitHooks($target)) {
|
2023-07-19 16:39:04 +02:00
|
|
|
$sourcePath = $this->getHookPath($source);
|
|
|
|
$targetPath = $this->getHookPath($target);
|
|
|
|
if ($sourcePath !== null && $targetPath !== null) {
|
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_post_rename,
|
|
|
|
[
|
|
|
|
Filesystem::signal_param_oldpath => $sourcePath,
|
|
|
|
Filesystem::signal_param_newpath => $targetPath,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2017-10-30 18:12:37 +01:00
|
|
|
}
|
2015-01-26 14:34:40 +01:00
|
|
|
}
|
2012-09-17 22:12:17 +02:00
|
|
|
}
|
2017-10-30 18:12:37 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw $e;
|
|
|
|
} finally {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->unlockFile($source, ILockingProvider::LOCK_SHARED, true);
|
|
|
|
$this->unlockFile($target, ILockingProvider::LOCK_SHARED, true);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-19 13:54:00 +02:00
|
|
|
return $result;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2024-12-04 19:11:53 +01:00
|
|
|
/**
|
|
|
|
* @throws ForbiddenException
|
|
|
|
*/
|
2024-11-28 19:08:27 +01:00
|
|
|
private function validateMountMove(array $mounts, IMountPoint $sourceMount, IMountPoint $targetMount, bool $targetIsShared): void {
|
2024-12-04 19:11:53 +01:00
|
|
|
$targetPath = $this->getRelativePath($targetMount->getMountPoint());
|
|
|
|
if ($targetPath) {
|
|
|
|
$targetPath = trim($targetPath, '/');
|
|
|
|
} else {
|
|
|
|
$targetPath = $targetMount->getMountPoint();
|
2024-11-28 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
2025-03-05 11:04:55 +01:00
|
|
|
$l = \OC::$server->get(IFactory::class)->get('files');
|
2024-11-28 19:08:27 +01:00
|
|
|
foreach ($mounts as $mount) {
|
2024-12-04 19:11:53 +01:00
|
|
|
$sourcePath = $this->getRelativePath($mount->getMountPoint());
|
|
|
|
if ($sourcePath) {
|
|
|
|
$sourcePath = trim($sourcePath, '/');
|
|
|
|
} else {
|
|
|
|
$sourcePath = $mount->getMountPoint();
|
2024-11-28 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$mount instanceof MoveableMount) {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Storage %s cannot be moved', [$sourcePath]), false);
|
2024-11-28 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($targetIsShared) {
|
2024-12-04 19:11:53 +01:00
|
|
|
if ($sourceMount instanceof SharedMount) {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a share (%s) into a shared folder is not allowed', [$sourcePath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
} else {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a storage (%s) into a shared folder is not allowed', [$sourcePath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
}
|
2024-11-28 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($sourceMount !== $targetMount) {
|
2024-12-04 19:11:53 +01:00
|
|
|
if ($sourceMount instanceof SharedMount) {
|
|
|
|
if ($targetMount instanceof SharedMount) {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a share (%s) into another share (%s) is not allowed', [$sourcePath, $targetPath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
} else {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a share (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($targetMount instanceof SharedMount) {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a storage (%s) into a share (%s) is not allowed', [$sourcePath, $targetPath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
} else {
|
2025-03-05 08:59:32 +01:00
|
|
|
throw new ForbiddenException($l->t('Moving a storage (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
|
2024-12-04 19:11:53 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-28 19:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
2015-02-25 12:44:44 +01:00
|
|
|
* Copy a file/folder from the source path to target path
|
|
|
|
*
|
2022-10-18 12:49:34 +02:00
|
|
|
* @param string $source source path
|
|
|
|
* @param string $target target path
|
2015-02-25 12:44:44 +01:00
|
|
|
* @param bool $preserveMtime whether to preserve mtime on the copy
|
|
|
|
*
|
2015-02-18 16:08:30 +01:00
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
2022-10-18 12:49:34 +02:00
|
|
|
public function copy($source, $target, $preserveMtime = false) {
|
|
|
|
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
|
|
|
|
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
|
2015-06-19 13:54:00 +02:00
|
|
|
$result = false;
|
2013-04-22 21:23:12 +02:00
|
|
|
if (
|
2022-10-18 12:49:34 +02:00
|
|
|
Filesystem::isValidPath($target)
|
2023-05-02 10:10:02 +02:00
|
|
|
&& Filesystem::isValidPath($source)
|
|
|
|
&& !Filesystem::isFileBlacklisted($target)
|
2013-04-22 21:23:12 +02:00
|
|
|
) {
|
2022-10-18 12:49:34 +02:00
|
|
|
$source = $this->getRelativePath($absolutePath1);
|
|
|
|
$target = $this->getRelativePath($absolutePath2);
|
2012-08-29 08:38:33 +02:00
|
|
|
|
2023-05-02 10:10:02 +02:00
|
|
|
if ($source == null || $target == null) {
|
2012-06-09 17:33:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
$run = true;
|
2015-05-29 16:35:49 +02:00
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->lockFile($target, ILockingProvider::LOCK_SHARED);
|
|
|
|
$this->lockFile($source, ILockingProvider::LOCK_SHARED);
|
2015-09-22 11:18:42 +02:00
|
|
|
$lockTypePath1 = ILockingProvider::LOCK_SHARED;
|
|
|
|
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
|
2015-05-29 16:35:49 +02:00
|
|
|
|
2015-09-22 11:18:42 +02:00
|
|
|
try {
|
2022-10-18 12:49:34 +02:00
|
|
|
$exists = $this->file_exists($target);
|
2015-09-22 11:18:42 +02:00
|
|
|
if ($this->shouldEmitHooks()) {
|
2012-10-10 12:25:46 +02:00
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
2015-09-22 11:18:42 +02:00
|
|
|
Filesystem::signal_copy,
|
2020-03-26 09:30:18 +01:00
|
|
|
[
|
2022-10-18 12:49:34 +02:00
|
|
|
Filesystem::signal_param_oldpath => $this->getHookPath($source),
|
|
|
|
Filesystem::signal_param_newpath => $this->getHookPath($target),
|
2015-09-22 11:18:42 +02:00
|
|
|
Filesystem::signal_param_run => &$run
|
2020-03-26 09:30:18 +01:00
|
|
|
]
|
2012-09-17 22:12:17 +02:00
|
|
|
);
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->emit_file_hooks_pre($exists, $target, $run);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2015-09-22 11:18:42 +02:00
|
|
|
if ($run) {
|
2022-10-18 12:49:34 +02:00
|
|
|
$mount1 = $this->getMount($source);
|
|
|
|
$mount2 = $this->getMount($target);
|
2015-09-22 11:18:42 +02:00
|
|
|
$storage1 = $mount1->getStorage();
|
|
|
|
$internalPath1 = $mount1->getInternalPath($absolutePath1);
|
|
|
|
$storage2 = $mount2->getStorage();
|
|
|
|
$internalPath2 = $mount2->getInternalPath($absolutePath2);
|
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE);
|
2015-09-22 11:18:42 +02:00
|
|
|
$lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE;
|
|
|
|
|
|
|
|
if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
|
|
|
|
if ($storage1) {
|
|
|
|
$result = $storage1->copy($internalPath1, $internalPath2);
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
|
|
|
|
}
|
2015-06-19 13:54:00 +02:00
|
|
|
|
2024-12-03 16:24:28 +01:00
|
|
|
if ($result) {
|
|
|
|
$this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2);
|
|
|
|
}
|
2015-09-22 11:18:42 +02:00
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->changeLock($target, ILockingProvider::LOCK_SHARED);
|
2015-09-22 11:18:42 +02:00
|
|
|
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
|
|
|
|
|
|
|
|
if ($this->shouldEmitHooks() && $result !== false) {
|
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_post_copy,
|
2020-03-26 09:30:18 +01:00
|
|
|
[
|
2022-10-18 12:49:34 +02:00
|
|
|
Filesystem::signal_param_oldpath => $this->getHookPath($source),
|
|
|
|
Filesystem::signal_param_newpath => $this->getHookPath($target)
|
2020-03-26 09:30:18 +01:00
|
|
|
]
|
2015-09-22 11:18:42 +02:00
|
|
|
);
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->emit_file_hooks_post($exists, $target);
|
2015-09-22 11:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->unlockFile($target, $lockTypePath2);
|
|
|
|
$this->unlockFile($source, $lockTypePath1);
|
2015-09-22 11:18:42 +02:00
|
|
|
throw $e;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2015-09-22 11:18:42 +02:00
|
|
|
|
2022-10-18 12:49:34 +02:00
|
|
|
$this->unlockFile($target, $lockTypePath2);
|
|
|
|
$this->unlockFile($source, $lockTypePath1);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2015-06-19 13:54:00 +02:00
|
|
|
return $result;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2014-05-12 23:27:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2017-01-03 15:01:47 +01:00
|
|
|
* @param string $mode 'r' or 'w'
|
2023-03-13 18:33:12 +01:00
|
|
|
* @return resource|false
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2014-05-12 23:27:36 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function fopen($path, $mode) {
|
2017-01-03 15:01:47 +01:00
|
|
|
$mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support
|
2020-03-26 09:30:18 +01:00
|
|
|
$hooks = [];
|
2012-10-10 11:54:44 +02:00
|
|
|
switch ($mode) {
|
2012-01-20 00:40:52 +01:00
|
|
|
case 'r':
|
2012-10-10 11:54:44 +02:00
|
|
|
$hooks[] = 'read';
|
2012-01-20 00:40:52 +01:00
|
|
|
break;
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'x+':
|
|
|
|
case 'a+':
|
2012-10-10 11:54:44 +02:00
|
|
|
$hooks[] = 'read';
|
|
|
|
$hooks[] = 'write';
|
2012-01-20 00:40:52 +01:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'x':
|
|
|
|
case 'a':
|
2012-10-10 11:54:44 +02:00
|
|
|
$hooks[] = 'write';
|
2012-01-20 00:40:52 +01:00
|
|
|
break;
|
|
|
|
default:
|
2022-03-30 10:55:41 +02:00
|
|
|
$this->logger->error('invalid mode (' . $mode . ') for ' . $path, ['app' => 'core']);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 15:01:47 +01:00
|
|
|
if ($mode !== 'r' && $mode !== 'w') {
|
2022-03-30 10:55:41 +02:00
|
|
|
$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']);
|
2017-01-03 15:01:47 +01:00
|
|
|
}
|
|
|
|
|
2022-08-16 17:17:51 +02:00
|
|
|
$handle = $this->basicOperation('fopen', $path, $hooks, $mode);
|
|
|
|
if (!is_resource($handle) && $mode === 'r') {
|
|
|
|
// trying to read a file that isn't on disk, check if the cache is out of sync and rescan if needed
|
|
|
|
$mount = $this->getMount($path);
|
|
|
|
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if ($storage->getCache()->inCache($internalPath) && !$storage->file_exists($path)) {
|
|
|
|
$this->writeUpdate($storage, $internalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $handle;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function toTmpFile($path): string|false {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2012-10-10 12:25:46 +02:00
|
|
|
if (Filesystem::isValidPath($path)) {
|
2012-07-21 21:44:10 +02:00
|
|
|
$source = $this->fopen($path, 'r');
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($source) {
|
2013-02-09 13:51:44 +01:00
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
2015-12-18 11:25:33 +01:00
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
|
2012-07-21 21:44:10 +02:00
|
|
|
file_put_contents($tmpFile, $source);
|
2012-03-27 02:24:52 +02:00
|
|
|
return $tmpFile;
|
2012-10-10 12:25:46 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-02-11 15:48:31 +01:00
|
|
|
}
|
2012-10-10 12:25:46 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $tmpFile
|
|
|
|
* @param string $path
|
|
|
|
* @return bool|mixed
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function fromTmpFile($tmpFile, $path) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2012-10-10 12:25:46 +02:00
|
|
|
if (Filesystem::isValidPath($path)) {
|
2014-02-19 21:23:39 +00:00
|
|
|
// Get directory that the file is going into
|
2014-04-07 23:17:35 +02:00
|
|
|
$filePath = dirname($path);
|
2014-02-19 21:28:32 +00:00
|
|
|
|
2014-02-19 21:23:39 +00:00
|
|
|
// Create the directories if any
|
2014-04-07 23:17:35 +02:00
|
|
|
if (!$this->file_exists($filePath)) {
|
2016-06-21 17:10:52 +02:00
|
|
|
$result = $this->createParentDirectories($filePath);
|
2017-01-03 15:01:47 +01:00
|
|
|
if ($result === false) {
|
2016-06-21 17:10:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-19 21:23:39 +00:00
|
|
|
}
|
2014-02-19 21:28:32 +00:00
|
|
|
|
2012-10-10 11:54:44 +02:00
|
|
|
$source = fopen($tmpFile, 'r');
|
|
|
|
if ($source) {
|
2014-12-08 15:25:21 +01:00
|
|
|
$result = $this->file_put_contents($path, $source);
|
2023-03-13 18:33:12 +01:00
|
|
|
/**
|
|
|
|
* $this->file_put_contents() might have already closed
|
|
|
|
* the resource, so we check it, before trying to close it
|
|
|
|
* to avoid messages in the error log.
|
|
|
|
* @psalm-suppress RedundantCondition false-positive
|
|
|
|
*/
|
2014-08-25 15:31:43 +02:00
|
|
|
if (is_resource($source)) {
|
|
|
|
fclose($source);
|
|
|
|
}
|
2012-02-11 15:48:31 +01:00
|
|
|
unlink($tmpFile);
|
2014-12-08 15:25:21 +01:00
|
|
|
return $result;
|
2012-07-21 21:44:10 +02:00
|
|
|
} else {
|
2012-10-10 12:25:46 +02:00
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-07-21 21:44:10 +02:00
|
|
|
} else {
|
2012-02-21 20:48:14 +01:00
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function getMimeType($path) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2012-07-21 21:44:10 +02:00
|
|
|
return $this->basicOperation('getMimeType', $path);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2012-10-10 11:54:44 +02:00
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $raw
|
|
|
|
*/
|
2023-03-13 18:33:12 +01:00
|
|
|
public function hash($type, $path, $raw = false): string|bool {
|
2018-01-25 22:26:47 +01:00
|
|
|
$postFix = (substr($path, -1) === '/') ? '/' : '';
|
2012-10-10 12:25:46 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
|
2015-03-30 22:45:58 +02:00
|
|
|
if (Filesystem::isValidPath($path)) {
|
2012-07-25 16:54:46 -04:00
|
|
|
$path = $this->getRelativePath($absolutePath);
|
|
|
|
if ($path == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-17 10:57:31 +02:00
|
|
|
if ($this->shouldEmitHooks($path)) {
|
2012-10-10 12:25:46 +02:00
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
Filesystem::signal_read,
|
2020-03-26 09:30:18 +01:00
|
|
|
[Filesystem::signal_param_path => $this->getHookPath($path)]
|
2012-07-25 16:54:46 -04:00
|
|
|
);
|
|
|
|
}
|
2021-06-04 16:22:39 +02:00
|
|
|
/** @var Storage|null $storage */
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix);
|
2012-10-10 17:46:29 +02:00
|
|
|
if ($storage) {
|
2018-01-26 00:02:03 +01:00
|
|
|
return $storage->hash($type, $internalPath, $raw);
|
2012-07-25 16:54:46 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 10:24:13 +01:00
|
|
|
return false;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:08:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
2023-03-28 10:34:01 +02:00
|
|
|
* @throws InvalidPathException
|
2015-02-18 16:08:30 +01:00
|
|
|
*/
|
2012-10-10 11:54:44 +02:00
|
|
|
public function free_space($path = '/') {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2017-05-04 20:43:03 -03:00
|
|
|
$result = $this->basicOperation('free_space', $path);
|
|
|
|
if ($result === null) {
|
|
|
|
throw new InvalidPathException();
|
|
|
|
}
|
|
|
|
return $result;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 17:50:53 +02:00
|
|
|
* abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
|
2014-06-04 17:08:25 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param mixed $extraParam (optional)
|
2012-01-20 00:40:52 +01:00
|
|
|
* @return mixed
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2012-07-21 21:44:10 +02:00
|
|
|
*
|
|
|
|
* This method takes requests for basic filesystem functions (e.g. reading & writing
|
|
|
|
* files), processes hooks and proxies, sanitises paths, and finally passes them on to
|
2012-09-07 18:30:48 +02:00
|
|
|
* \OC\Files\Storage\Storage for delegation to a storage backend for execution
|
2012-01-20 00:40:52 +01:00
|
|
|
*/
|
2023-03-28 10:34:01 +02:00
|
|
|
private function basicOperation(string $operation, string $path, array $hooks = [], $extraParam = null) {
|
2018-01-25 22:26:47 +01:00
|
|
|
$postFix = (substr($path, -1) === '/') ? '/' : '';
|
2012-10-10 12:25:46 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
|
2015-03-30 22:45:58 +02:00
|
|
|
if (Filesystem::isValidPath($path)
|
2023-05-02 10:10:02 +02:00
|
|
|
&& !Filesystem::isFileBlacklisted($path)
|
2013-05-10 12:00:13 +02:00
|
|
|
) {
|
2012-07-21 21:44:10 +02:00
|
|
|
$path = $this->getRelativePath($absolutePath);
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($path == null) {
|
2012-06-09 17:33:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-08 11:27:25 +01:00
|
|
|
|
2015-06-16 14:37:15 +02:00
|
|
|
if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) {
|
2015-05-29 16:35:49 +02:00
|
|
|
// always a shared lock during pre-hooks so the hook can read the file
|
|
|
|
$this->lockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
|
|
|
|
2012-10-10 11:54:44 +02:00
|
|
|
$run = $this->runHooks($hooks, $path);
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = Filesystem::resolvePath($absolutePath . $postFix);
|
2023-05-02 10:10:02 +02:00
|
|
|
if ($run && $storage) {
|
2023-04-05 09:13:39 +02:00
|
|
|
/** @var Storage $storage */
|
2015-05-05 13:48:49 +02:00
|
|
|
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
|
2019-01-22 15:32:48 +01:00
|
|
|
try {
|
|
|
|
$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
|
|
|
|
} catch (LockedException $e) {
|
2022-07-27 08:51:42 -04:00
|
|
|
// release the shared lock we acquired before quitting
|
2019-01-22 15:32:48 +01:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-05-05 13:48:49 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (!is_null($extraParam)) {
|
|
|
|
$result = $storage->$operation($internalPath, $extraParam);
|
|
|
|
} else {
|
|
|
|
$result = $storage->$operation($internalPath);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
|
2015-05-13 13:37:18 +02:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (in_array('read', $hooks)) {
|
2015-05-13 13:37:18 +02:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
2015-05-05 13:48:49 +02:00
|
|
|
}
|
|
|
|
throw $e;
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
2014-08-05 15:12:20 +02:00
|
|
|
|
2022-11-23 12:43:57 +01:00
|
|
|
if ($result !== false && in_array('delete', $hooks)) {
|
2015-11-25 13:53:31 +01:00
|
|
|
$this->removeUpdate($storage, $internalPath);
|
2014-08-05 15:12:20 +02:00
|
|
|
}
|
2022-11-23 12:43:57 +01:00
|
|
|
if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') {
|
2024-01-05 14:56:33 +01:00
|
|
|
$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);
|
2014-08-05 15:12:20 +02:00
|
|
|
}
|
2022-11-23 12:43:57 +01:00
|
|
|
if ($result !== false && in_array('touch', $hooks)) {
|
2024-05-08 11:50:20 +02:00
|
|
|
$this->writeUpdate($storage, $internalPath, $extraParam, 0);
|
2014-08-05 15:12:20 +02:00
|
|
|
}
|
|
|
|
|
2015-06-18 13:47:03 +02:00
|
|
|
if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) {
|
|
|
|
$this->changeLock($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
|
|
|
|
2015-06-19 13:54:00 +02:00
|
|
|
$unlockLater = false;
|
2015-06-30 13:45:41 +02:00
|
|
|
if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) {
|
2015-06-19 13:54:00 +02:00
|
|
|
$unlockLater = true;
|
2017-01-13 12:55:08 +01:00
|
|
|
// make sure our unlocking callback will still be called if connection is aborted
|
|
|
|
ignore_user_abort(true);
|
2015-05-05 13:48:49 +02:00
|
|
|
$result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) {
|
|
|
|
if (in_array('write', $hooks)) {
|
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (in_array('read', $hooks)) {
|
2015-05-05 13:48:49 +02:00
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-17 10:57:31 +02:00
|
|
|
if ($this->shouldEmitHooks($path) && $result !== false) {
|
2012-10-10 11:54:44 +02:00
|
|
|
if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
|
|
|
|
$this->runHooks($hooks, $path, true);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-19 13:54:00 +02:00
|
|
|
|
|
|
|
if (!$unlockLater
|
|
|
|
&& (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks))
|
|
|
|
) {
|
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
2012-01-20 00:40:52 +01:00
|
|
|
return $result;
|
2015-05-29 16:35:49 +02:00
|
|
|
} else {
|
|
|
|
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-15 17:42:39 +02:00
|
|
|
|
2013-08-17 10:57:31 +02:00
|
|
|
/**
|
|
|
|
* get the path relative to the default root for hook usage
|
|
|
|
*
|
|
|
|
* @param string $path
|
2023-02-18 15:41:01 +01:00
|
|
|
* @return ?string
|
2013-08-17 10:57:31 +02:00
|
|
|
*/
|
2023-03-28 10:34:01 +02:00
|
|
|
private function getHookPath($path): ?string {
|
|
|
|
$view = Filesystem::getView();
|
|
|
|
if (!$view) {
|
2013-08-17 10:57:31 +02:00
|
|
|
return $path;
|
|
|
|
}
|
2023-03-28 10:34:01 +02:00
|
|
|
return $view->getRelativePath($this->getAbsolutePath($path));
|
2013-08-17 10:57:31 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 10:34:01 +02:00
|
|
|
private function shouldEmitHooks(string $path = ''): bool {
|
2013-08-17 10:57:31 +02:00
|
|
|
if ($path && Cache\Scanner::isPartialFile($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Filesystem::$loaded) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$defaultRoot = Filesystem::getRoot();
|
2014-06-06 10:50:58 +02:00
|
|
|
if ($defaultRoot === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-02 22:43:58 +01:00
|
|
|
if ($this->fakeRoot === $defaultRoot) {
|
2013-10-09 20:46:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-01 14:08:14 +02:00
|
|
|
$fullPath = $this->getAbsolutePath($path);
|
2015-07-28 15:24:46 +02:00
|
|
|
|
|
|
|
if ($fullPath === $defaultRoot) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-01 14:08:14 +02:00
|
|
|
return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/');
|
2013-08-17 10:57:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-06 16:30:58 +01:00
|
|
|
/**
|
2014-02-26 14:29:13 +01:00
|
|
|
* @param string[] $hooks
|
2014-02-06 16:30:58 +01:00
|
|
|
* @param string $path
|
2014-02-26 14:29:13 +01:00
|
|
|
* @param bool $post
|
|
|
|
* @return bool
|
2014-02-06 16:30:58 +01:00
|
|
|
*/
|
2012-10-10 11:54:44 +02:00
|
|
|
private function runHooks($hooks, $path, $post = false) {
|
2015-06-15 15:07:48 +02:00
|
|
|
$relativePath = $path;
|
2013-08-17 10:57:31 +02:00
|
|
|
$path = $this->getHookPath($path);
|
2018-01-26 23:46:40 +01:00
|
|
|
$prefix = $post ? 'post_' : '';
|
2012-10-10 11:54:44 +02:00
|
|
|
$run = true;
|
2015-06-15 15:07:48 +02:00
|
|
|
if ($this->shouldEmitHooks($relativePath)) {
|
2012-10-10 11:54:44 +02:00
|
|
|
foreach ($hooks as $hook) {
|
|
|
|
if ($hook != 'read') {
|
2012-10-10 12:25:46 +02:00
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
2012-10-10 11:54:44 +02:00
|
|
|
$prefix . $hook,
|
2020-03-26 09:30:18 +01:00
|
|
|
[
|
2012-10-10 12:25:46 +02:00
|
|
|
Filesystem::signal_param_run => &$run,
|
|
|
|
Filesystem::signal_param_path => $path
|
2020-03-26 09:30:18 +01:00
|
|
|
]
|
2012-08-17 01:22:26 +02:00
|
|
|
);
|
2012-10-10 11:54:44 +02:00
|
|
|
} elseif (!$post) {
|
2012-10-10 12:25:46 +02:00
|
|
|
\OC_Hook::emit(
|
|
|
|
Filesystem::CLASSNAME,
|
2012-10-10 11:54:44 +02:00
|
|
|
$prefix . $hook,
|
2020-03-26 09:30:18 +01:00
|
|
|
[
|
2012-10-10 12:25:46 +02:00
|
|
|
Filesystem::signal_param_path => $path
|
2020-03-26 09:30:18 +01:00
|
|
|
]
|
2012-08-17 01:22:26 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $run;
|
|
|
|
}
|
|
|
|
|
2012-06-15 17:42:39 +02:00
|
|
|
/**
|
|
|
|
* check if a file or folder has been updated since $time
|
2012-10-10 11:54:44 +02:00
|
|
|
*
|
2012-10-10 12:25:46 +02:00
|
|
|
* @param string $path
|
2012-06-15 17:42:39 +02:00
|
|
|
* @param int $time
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-07-21 21:44:10 +02:00
|
|
|
public function hasUpdated($path, $time) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->basicOperation('hasUpdated', $path, [], $time);
|
2012-06-15 17:42:39 +02:00
|
|
|
}
|
2012-10-26 12:30:25 +02:00
|
|
|
|
2015-11-18 17:08:18 +01:00
|
|
|
/**
|
|
|
|
* @param string $ownerId
|
2022-04-22 16:29:52 +02:00
|
|
|
* @return IUser
|
2015-11-18 17:08:18 +01:00
|
|
|
*/
|
2022-04-22 16:29:52 +02:00
|
|
|
private function getUserObjectForOwner(string $ownerId) {
|
2022-05-17 15:20:28 +02:00
|
|
|
return new LazyUser($ownerId, $this->userManager);
|
2015-11-18 17:08:18 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:32:05 +01:00
|
|
|
/**
|
2015-11-10 16:14:08 +01:00
|
|
|
* Get file info from cache
|
|
|
|
*
|
|
|
|
* If the file is not in cached it will be scanned
|
|
|
|
* If the file has changed on storage the cache will be updated
|
|
|
|
*
|
2023-04-05 09:13:39 +02:00
|
|
|
* @param Storage $storage
|
2015-11-10 13:32:05 +01:00
|
|
|
* @param string $internalPath
|
|
|
|
* @param string $relativePath
|
2017-07-18 20:38:40 +02:00
|
|
|
* @return ICacheEntry|bool
|
2015-11-10 13:32:05 +01:00
|
|
|
*/
|
|
|
|
private function getCacheEntry($storage, $internalPath, $relativePath) {
|
|
|
|
$cache = $storage->getCache($internalPath);
|
|
|
|
$data = $cache->get($internalPath);
|
|
|
|
$watcher = $storage->getWatcher($internalPath);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// if the file is not in the cache or needs to be updated, trigger the scanner and reload the data
|
2021-11-24 11:55:06 +01:00
|
|
|
if (!$data || (isset($data['size']) && $data['size'] === -1)) {
|
2015-11-10 13:32:05 +01:00
|
|
|
if (!$storage->file_exists($internalPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 12:51:41 +02:00
|
|
|
// don't need to get a lock here since the scanner does it's own locking
|
2015-11-10 13:32:05 +01:00
|
|
|
$scanner = $storage->getScanner($internalPath);
|
|
|
|
$scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
|
|
|
|
$data = $cache->get($internalPath);
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) {
|
2015-11-10 13:32:05 +01:00
|
|
|
$this->lockFile($relativePath, ILockingProvider::LOCK_SHARED);
|
|
|
|
$watcher->update($internalPath, $data);
|
2015-11-25 13:53:31 +01:00
|
|
|
$storage->getPropagator()->propagateChange($internalPath, time());
|
2015-11-10 13:32:05 +01:00
|
|
|
$data = $cache->get($internalPath);
|
|
|
|
$this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
|
|
|
} catch (LockedException $e) {
|
|
|
|
// if the file is locked we just use the old cache info
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2012-10-26 12:30:25 +02:00
|
|
|
/**
|
|
|
|
* get the filesystem info
|
|
|
|
*
|
|
|
|
* @param string $path
|
2023-01-24 11:40:56 +01:00
|
|
|
* @param bool|string $includeMountPoints true to add mountpoint sizes,
|
2014-03-25 16:37:46 +01:00
|
|
|
* 'ext' to add only ext storage mount point sizes. Defaults to true.
|
2023-03-28 11:13:49 +02:00
|
|
|
* @return \OC\Files\FileInfo|false False if file does not exist
|
2012-10-26 12:30:25 +02:00
|
|
|
*/
|
2013-11-18 17:29:30 +01:00
|
|
|
public function getFileInfo($path, $includeMountPoints = true) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2013-02-03 23:34:27 +01:00
|
|
|
if (!Filesystem::isValidPath($path)) {
|
2015-11-10 13:52:02 +01:00
|
|
|
return false;
|
2013-02-03 23:34:27 +01:00
|
|
|
}
|
2015-06-16 14:37:15 +02:00
|
|
|
$relativePath = $path;
|
2012-10-26 13:23:15 +02:00
|
|
|
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
|
2014-06-16 16:20:40 +02:00
|
|
|
|
|
|
|
$mount = Filesystem::getMountManager()->find($path);
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
$internalPath = $mount->getInternalPath($path);
|
2012-11-24 16:42:54 -05:00
|
|
|
if ($storage) {
|
2015-11-10 13:32:05 +01:00
|
|
|
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
|
2012-10-26 12:30:25 +02:00
|
|
|
|
2015-12-02 14:59:13 +01:00
|
|
|
if (!$data instanceof ICacheEntry) {
|
2024-07-22 11:33:50 +02:00
|
|
|
if (Cache\Scanner::isPartialFile($relativePath)) {
|
|
|
|
return $this->getPartFileInfo($relativePath);
|
|
|
|
}
|
|
|
|
|
2015-11-10 14:34:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-15 12:43:25 -05:00
|
|
|
if ($mount instanceof MoveableMount && $internalPath === '') {
|
|
|
|
$data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
|
2015-11-10 13:52:02 +01:00
|
|
|
}
|
2024-07-22 22:01:12 +02:00
|
|
|
if ($internalPath === '' && $data['name']) {
|
|
|
|
$data['name'] = basename($path);
|
|
|
|
}
|
|
|
|
|
2019-02-24 11:08:04 +01:00
|
|
|
$ownerId = $storage->getOwner($internalPath);
|
|
|
|
$owner = null;
|
2024-09-16 16:00:46 +02:00
|
|
|
if ($ownerId !== false) {
|
2019-02-24 11:08:04 +01:00
|
|
|
// ownerId might be null if files are accessed with an access token without file system access
|
|
|
|
$owner = $this->getUserObjectForOwner($ownerId);
|
|
|
|
}
|
2015-11-10 13:52:02 +01:00
|
|
|
$info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner);
|
|
|
|
|
2022-01-06 16:57:32 +01:00
|
|
|
if (isset($data['fileid'])) {
|
2023-05-02 10:10:02 +02:00
|
|
|
if ($includeMountPoints && $data['mimetype'] === 'httpd/unix-directory') {
|
2014-06-05 15:45:59 +02:00
|
|
|
//add the sizes of other mount points to the folder
|
2014-03-25 16:37:46 +01:00
|
|
|
$extOnly = ($includeMountPoints === 'ext');
|
2023-02-08 14:34:40 +01:00
|
|
|
$this->addSubMounts($info, $extOnly);
|
2012-11-24 16:42:54 -05:00
|
|
|
}
|
2012-12-11 01:06:21 +01:00
|
|
|
}
|
2013-04-23 22:20:31 +02:00
|
|
|
|
2015-11-10 13:52:02 +01:00
|
|
|
return $info;
|
2018-05-02 15:11:59 +02:00
|
|
|
} else {
|
2022-03-30 10:55:41 +02:00
|
|
|
$this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']);
|
2014-06-16 16:20:40 +02:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:52:02 +01:00
|
|
|
return false;
|
2012-10-26 12:30:25 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 14:34:40 +01:00
|
|
|
/**
|
|
|
|
* Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2012-10-26 12:30:25 +02:00
|
|
|
/**
|
|
|
|
* get the content of a directory
|
|
|
|
*
|
|
|
|
* @param string $directory path under datadirectory
|
2013-04-08 22:40:03 +02:00
|
|
|
* @param string $mimetype_filter limit returned content to this mimetype or mimepart
|
2014-01-13 14:28:49 +01:00
|
|
|
* @return FileInfo[]
|
2012-10-26 12:30:25 +02:00
|
|
|
*/
|
2024-03-28 16:13:19 +01:00
|
|
|
public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Files\FileInfo $directoryInfo = null) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($directory);
|
2013-02-04 13:28:31 +01:00
|
|
|
if (!Filesystem::isValidPath($directory)) {
|
2015-11-10 13:52:02 +01:00
|
|
|
return [];
|
2013-02-03 23:34:27 +01:00
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
|
2014-10-02 17:37:33 +02:00
|
|
|
$path = $this->getAbsolutePath($directory);
|
2014-12-16 14:24:48 +01:00
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
$mount = $this->getMount($directory);
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
$internalPath = $mount->getInternalPath($path);
|
2022-04-05 10:35:53 +02:00
|
|
|
if (!$storage) {
|
|
|
|
return [];
|
|
|
|
}
|
2012-10-26 12:30:25 +02:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
$cache = $storage->getCache($internalPath);
|
|
|
|
$user = \OC_User::getUser();
|
2015-11-10 13:32:05 +01:00
|
|
|
|
2022-04-06 15:46:38 +02:00
|
|
|
if (!$directoryInfo) {
|
|
|
|
$data = $this->getCacheEntry($storage, $internalPath, $directory);
|
|
|
|
if (!$data instanceof ICacheEntry || !isset($data['fileid'])) {
|
2022-04-05 15:29:49 +02:00
|
|
|
return [];
|
|
|
|
}
|
2022-04-06 15:46:38 +02:00
|
|
|
} else {
|
|
|
|
$data = $directoryInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($data->getPermissions() & Constants::PERMISSION_READ)) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-06-16 14:37:15 +02:00
|
|
|
|
2022-04-06 15:46:38 +02:00
|
|
|
$folderId = $data->getId();
|
|
|
|
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
|
2018-08-16 20:39:51 +02:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
|
2012-11-24 16:42:54 -05:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
$fileNames = array_map(function (ICacheEntry $content) {
|
|
|
|
return $content->getName();
|
|
|
|
}, $contents);
|
|
|
|
/**
|
2022-04-05 16:47:13 +02:00
|
|
|
* @var \OC\Files\FileInfo[] $fileInfos
|
|
|
|
*/
|
2022-04-05 10:35:53 +02:00
|
|
|
$fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
|
|
|
|
if ($sharingDisabled) {
|
|
|
|
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
|
|
|
|
}
|
2024-09-16 16:00:46 +02:00
|
|
|
$ownerId = $storage->getOwner($content['path']);
|
|
|
|
if ($ownerId !== false) {
|
|
|
|
$owner = $this->getUserObjectForOwner($ownerId);
|
|
|
|
} else {
|
|
|
|
$owner = null;
|
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
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);
|
2024-03-18 16:18:35 +01:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
$dirLength = strlen($path);
|
|
|
|
foreach ($mounts as $mount) {
|
|
|
|
$mountPoint = $mount->getMountPoint();
|
|
|
|
$subStorage = $mount->getStorage();
|
|
|
|
if ($subStorage) {
|
|
|
|
$subCache = $subStorage->getCache('');
|
|
|
|
|
|
|
|
$rootEntry = $subCache->get('');
|
|
|
|
if (!$rootEntry) {
|
|
|
|
$subScanner = $subStorage->getScanner();
|
|
|
|
try {
|
|
|
|
$subScanner->scanFile('');
|
|
|
|
} catch (\OCP\Files\StorageNotAvailableException $e) {
|
|
|
|
continue;
|
|
|
|
} catch (\OCP\Files\StorageInvalidException $e) {
|
|
|
|
continue;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// sometimes when the storage is not available it can be any exception
|
2022-03-30 10:55:41 +02:00
|
|
|
$this->logger->error('Exception while scanning storage "' . $subStorage->getId() . '"', [
|
|
|
|
'exception' => $e,
|
|
|
|
'app' => 'core',
|
2022-04-05 10:35:53 +02:00
|
|
|
]);
|
|
|
|
continue;
|
2013-01-10 12:09:55 -05:00
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
$rootEntry = $subCache->get('');
|
|
|
|
}
|
2013-01-10 12:09:55 -05:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
if ($rootEntry && ($rootEntry->getPermissions() & Constants::PERMISSION_READ)) {
|
|
|
|
$relativePath = trim(substr($mountPoint, $dirLength), '/');
|
|
|
|
if ($pos = strpos($relativePath, '/')) {
|
|
|
|
//mountpoint inside subfolder add size to the correct folder
|
|
|
|
$entryName = substr($relativePath, 0, $pos);
|
2024-08-22 12:04:31 +02:00
|
|
|
|
|
|
|
// Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet
|
2024-12-16 12:51:56 +01:00
|
|
|
if (!isset($files[$entryName])) {
|
|
|
|
try {
|
|
|
|
if ($this->mkdir($path . '/' . $entryName) !== false) {
|
|
|
|
$info = $this->getFileInfo($path . '/' . $entryName);
|
|
|
|
if ($info !== false) {
|
|
|
|
$files[$entryName] = $info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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]);
|
2024-08-22 12:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:50:14 +02:00
|
|
|
if (isset($files[$entryName])) {
|
|
|
|
$files[$entryName]->addSubEntry($rootEntry, $mountPoint);
|
2022-04-05 10:35:53 +02:00
|
|
|
}
|
|
|
|
} else { //mountpoint in this folder, add an entry for it
|
|
|
|
$rootEntry['name'] = $relativePath;
|
|
|
|
$rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
|
|
|
|
$permissions = $rootEntry['permissions'];
|
|
|
|
// do not allow renaming/deleting the mount point if they are not shared files/folders
|
|
|
|
// for shared files/folders we use the permissions given by the owner
|
|
|
|
if ($mount instanceof MoveableMount) {
|
|
|
|
$rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
|
|
|
|
} else {
|
|
|
|
$rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE));
|
|
|
|
}
|
2013-01-22 20:57:15 +01:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
$rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/
|
2014-08-08 12:16:54 +02:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
// if sharing was disabled for the user we remove the share permissions
|
2023-10-19 19:40:52 +02:00
|
|
|
if ($sharingDisabled) {
|
2022-04-05 10:35:53 +02:00
|
|
|
$rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
|
2012-11-24 16:42:54 -05:00
|
|
|
}
|
2012-10-26 12:30:25 +02:00
|
|
|
|
2024-09-16 16:00:46 +02:00
|
|
|
$ownerId = $subStorage->getOwner('');
|
|
|
|
if ($ownerId !== false) {
|
|
|
|
$owner = $this->getUserObjectForOwner($ownerId);
|
|
|
|
} else {
|
|
|
|
$owner = null;
|
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
$files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner);
|
2012-10-27 12:17:35 +02:00
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
}
|
2012-10-27 12:17:35 +02:00
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
}
|
2014-01-13 14:28:49 +01:00
|
|
|
|
2022-04-05 10:35:53 +02:00
|
|
|
if ($mimetype_filter) {
|
|
|
|
$files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) {
|
|
|
|
if (strpos($mimetype_filter, '/')) {
|
|
|
|
return $file->getMimetype() === $mimetype_filter;
|
|
|
|
} else {
|
|
|
|
return $file->getMimePart() === $mimetype_filter;
|
|
|
|
}
|
|
|
|
});
|
2015-11-10 13:52:02 +01:00
|
|
|
}
|
2022-04-05 10:35:53 +02:00
|
|
|
|
|
|
|
return array_values($files);
|
2012-10-26 12:30:25 +02:00
|
|
|
}
|
2012-10-26 12:43:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* change file metadata
|
|
|
|
*
|
|
|
|
* @param string $path
|
2014-05-11 18:28:45 +01:00
|
|
|
* @param array|\OCP\Files\FileInfo $data
|
2012-10-26 12:43:23 +02:00
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* returns the fileid of the updated file
|
|
|
|
*/
|
|
|
|
public function putFileInfo($path, $data) {
|
2014-05-08 15:19:54 +02:00
|
|
|
$this->assertPathLength($path);
|
2014-01-13 15:57:49 +01:00
|
|
|
if ($data instanceof FileInfo) {
|
|
|
|
$data = $data->getData();
|
|
|
|
}
|
2012-10-26 13:23:15 +02:00
|
|
|
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
|
2012-10-26 12:43:23 +02:00
|
|
|
/**
|
2023-04-05 09:13:39 +02:00
|
|
|
* @var Storage $storage
|
2012-10-26 12:43:23 +02:00
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = Filesystem::resolvePath($path);
|
2012-11-24 16:42:54 -05:00
|
|
|
if ($storage) {
|
2013-01-01 20:19:53 +01:00
|
|
|
$cache = $storage->getCache($path);
|
2012-10-26 12:43:23 +02:00
|
|
|
|
2012-11-24 16:42:54 -05:00
|
|
|
if (!$cache->inCache($internalPath)) {
|
2013-01-01 20:19:53 +01:00
|
|
|
$scanner = $storage->getScanner($internalPath);
|
2012-11-24 16:42:54 -05:00
|
|
|
$scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
|
|
|
|
}
|
2012-10-26 12:43:23 +02:00
|
|
|
|
2012-11-24 16:42:54 -05:00
|
|
|
return $cache->put($internalPath, $data);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-26 12:43:23 +02:00
|
|
|
}
|
2012-10-26 13:23:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files with the name matching $query
|
|
|
|
*
|
|
|
|
* @param string $query
|
2014-01-13 14:28:49 +01:00
|
|
|
* @return FileInfo[]
|
2012-10-26 13:23:15 +02:00
|
|
|
*/
|
|
|
|
public function search($query) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->searchCommon('search', ['%' . $query . '%']);
|
2012-10-27 10:34:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 18:26:43 +02:00
|
|
|
/**
|
|
|
|
* search for files with the name matching $query
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @return FileInfo[]
|
|
|
|
*/
|
|
|
|
public function searchRaw($query) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->searchCommon('search', [$query]);
|
2014-10-10 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 10:34:25 +02:00
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
2013-09-22 01:23:18 +02:00
|
|
|
* @param string $mimetype
|
2014-01-13 14:28:49 +01:00
|
|
|
* @return FileInfo[]
|
2012-10-27 10:34:25 +02:00
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->searchCommon('searchByMime', [$mimetype]);
|
2012-10-27 10:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-04 14:01:15 +01:00
|
|
|
* search for files by tag
|
|
|
|
*
|
|
|
|
* @param string|int $tag name or tag id
|
2014-12-12 11:18:35 +01:00
|
|
|
* @param string $userId owner of the tags
|
2014-12-04 14:01:15 +01:00
|
|
|
* @return FileInfo[]
|
|
|
|
*/
|
2014-12-12 11:18:35 +01:00
|
|
|
public function searchByTag($tag, $userId) {
|
2020-03-26 09:30:18 +01:00
|
|
|
return $this->searchCommon('searchByTag', [$tag, $userId]);
|
2014-12-04 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-27 10:34:25 +02:00
|
|
|
/**
|
2014-12-12 11:18:35 +01:00
|
|
|
* @param string $method cache method
|
|
|
|
* @param array $args
|
2014-01-13 14:28:49 +01:00
|
|
|
* @return FileInfo[]
|
2012-10-27 10:34:25 +02:00
|
|
|
*/
|
2014-12-12 11:18:35 +01:00
|
|
|
private function searchCommon($method, $args) {
|
2020-03-26 09:30:18 +01:00
|
|
|
$files = [];
|
2012-10-26 13:23:15 +02:00
|
|
|
$rootLength = strlen($this->fakeRoot);
|
|
|
|
|
2014-12-16 14:24:48 +01:00
|
|
|
$mount = $this->getMount('');
|
|
|
|
$mountPoint = $mount->getMountPoint();
|
|
|
|
$storage = $mount->getStorage();
|
2021-03-10 19:35:13 +01:00
|
|
|
$userManager = \OC::$server->getUserManager();
|
2012-11-24 16:42:54 -05:00
|
|
|
if ($storage) {
|
2013-01-01 20:19:53 +01:00
|
|
|
$cache = $storage->getCache('');
|
2012-10-26 13:23:15 +02:00
|
|
|
|
2020-03-26 09:30:18 +01:00
|
|
|
$results = call_user_func_array([$cache, $method], $args);
|
2012-10-26 13:23:15 +02:00
|
|
|
foreach ($results as $result) {
|
2013-09-22 01:23:18 +02:00
|
|
|
if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
|
2014-01-13 14:28:49 +01:00
|
|
|
$internalPath = $result['path'];
|
2014-07-14 18:32:45 +02:00
|
|
|
$path = $mountPoint . $result['path'];
|
2012-11-24 16:42:54 -05:00
|
|
|
$result['path'] = substr($mountPoint . $result['path'], $rootLength);
|
2024-09-16 16:00:46 +02:00
|
|
|
$ownerId = $storage->getOwner($internalPath);
|
|
|
|
if ($ownerId !== false) {
|
|
|
|
$owner = $userManager->get($ownerId);
|
|
|
|
} else {
|
|
|
|
$owner = null;
|
|
|
|
}
|
2015-11-02 14:56:38 +01:00
|
|
|
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
|
2012-11-24 16:42:54 -05:00
|
|
|
}
|
2012-10-26 13:23:15 +02:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:24:48 +01:00
|
|
|
$mounts = Filesystem::getMountManager()->findIn($this->fakeRoot);
|
|
|
|
foreach ($mounts as $mount) {
|
|
|
|
$mountPoint = $mount->getMountPoint();
|
|
|
|
$storage = $mount->getStorage();
|
2012-11-24 16:42:54 -05:00
|
|
|
if ($storage) {
|
2013-01-01 20:19:53 +01:00
|
|
|
$cache = $storage->getCache('');
|
2012-11-24 16:42:54 -05:00
|
|
|
|
|
|
|
$relativeMountPoint = substr($mountPoint, $rootLength);
|
2020-03-26 09:30:18 +01:00
|
|
|
$results = call_user_func_array([$cache, $method], $args);
|
2013-09-22 01:23:18 +02:00
|
|
|
if ($results) {
|
|
|
|
foreach ($results as $result) {
|
2014-01-13 14:28:49 +01:00
|
|
|
$internalPath = $result['path'];
|
2014-04-25 12:31:44 +02:00
|
|
|
$result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
|
|
|
|
$path = rtrim($mountPoint . $internalPath, '/');
|
2024-09-16 16:00:46 +02:00
|
|
|
$ownerId = $storage->getOwner($internalPath);
|
|
|
|
if ($ownerId !== false) {
|
|
|
|
$owner = $userManager->get($ownerId);
|
|
|
|
} else {
|
|
|
|
$owner = null;
|
|
|
|
}
|
2015-11-02 14:56:38 +01:00
|
|
|
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
|
2013-09-22 01:23:18 +02:00
|
|
|
}
|
2012-11-24 16:42:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-26 13:23:15 +02:00
|
|
|
return $files;
|
|
|
|
}
|
2012-11-08 17:47:00 +01:00
|
|
|
|
2013-02-02 18:50:40 -05:00
|
|
|
/**
|
2013-04-22 21:23:12 +02:00
|
|
|
* Get the owner for a file or folder
|
|
|
|
*
|
2015-12-10 14:05:27 +01:00
|
|
|
* @throws NotFoundException
|
2013-04-22 21:23:12 +02:00
|
|
|
*/
|
2024-09-16 16:00:30 +02:00
|
|
|
public function getOwner(string $path): string {
|
2015-12-10 14:05:27 +01:00
|
|
|
$info = $this->getFileInfo($path);
|
|
|
|
if (!$info) {
|
2015-12-14 10:24:38 +01:00
|
|
|
throw new NotFoundException($path . ' not found while trying to get owner');
|
2015-12-10 14:05:27 +01:00
|
|
|
}
|
2020-03-16 13:12:10 +01:00
|
|
|
|
|
|
|
if ($info->getOwner() === null) {
|
|
|
|
throw new NotFoundException($path . ' has no owner');
|
|
|
|
}
|
|
|
|
|
2015-12-10 14:05:27 +01:00
|
|
|
return $info->getOwner()->getUID();
|
2013-02-02 18:50:40 -05:00
|
|
|
}
|
|
|
|
|
2012-11-08 17:47:00 +01:00
|
|
|
/**
|
|
|
|
* get the ETag for a file or folder
|
|
|
|
*
|
|
|
|
* @param string $path
|
2023-03-13 18:59:16 +01:00
|
|
|
* @return string|false
|
2012-11-08 17:47:00 +01:00
|
|
|
*/
|
2012-12-11 01:06:21 +01:00
|
|
|
public function getETag($path) {
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = $this->resolvePath($path);
|
2012-11-24 16:42:54 -05:00
|
|
|
if ($storage) {
|
|
|
|
return $storage->getETag($internalPath);
|
|
|
|
} else {
|
2023-03-13 18:59:16 +01:00
|
|
|
return false;
|
2012-11-24 16:42:54 -05:00
|
|
|
}
|
2012-11-08 17:47:00 +01:00
|
|
|
}
|
2013-01-27 00:13:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path of a file by id, relative to the view
|
|
|
|
*
|
2022-07-27 08:51:42 -04:00
|
|
|
* Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file
|
2013-01-27 00:13:16 +01:00
|
|
|
*
|
|
|
|
* @param int $id
|
2020-09-15 17:01:33 +02:00
|
|
|
* @param int|null $storageId
|
2015-06-15 14:10:10 +02:00
|
|
|
* @return string
|
2020-09-15 17:01:33 +02:00
|
|
|
* @throws NotFoundException
|
2013-01-27 00:13:16 +01:00
|
|
|
*/
|
2024-03-28 16:13:19 +01:00
|
|
|
public function getPath($id, ?int $storageId = null) {
|
2014-10-02 17:37:33 +02:00
|
|
|
$id = (int)$id;
|
2014-03-27 16:43:34 +01:00
|
|
|
$manager = Filesystem::getMountManager();
|
|
|
|
$mounts = $manager->findIn($this->fakeRoot);
|
|
|
|
$mounts[] = $manager->find($this->fakeRoot);
|
2022-02-23 18:11:46 +01:00
|
|
|
$mounts = array_filter($mounts);
|
|
|
|
// reverse the array, so we start with the storage this view is in
|
2014-03-27 16:43:34 +01:00
|
|
|
// which is the most likely to contain the file we're looking for
|
|
|
|
$mounts = array_reverse($mounts);
|
2019-05-28 18:15:52 +02:00
|
|
|
|
2022-02-23 18:11:46 +01:00
|
|
|
// put non-shared mounts in front of the shared mount
|
|
|
|
// this prevents unneeded recursion into shares
|
2020-04-09 13:53:40 +02:00
|
|
|
usort($mounts, function (IMountPoint $a, IMountPoint $b) {
|
2019-05-28 18:15:52 +02:00
|
|
|
return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1;
|
|
|
|
});
|
|
|
|
|
2020-09-15 17:01:33 +02:00
|
|
|
if (!is_null($storageId)) {
|
|
|
|
$mounts = array_filter($mounts, function (IMountPoint $mount) use ($storageId) {
|
|
|
|
return $mount->getNumericStorageId() === $storageId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-28 01:02:04 +01:00
|
|
|
foreach ($mounts as $mount) {
|
|
|
|
/**
|
2014-11-24 15:54:42 +01:00
|
|
|
* @var \OC\Files\Mount\MountPoint $mount
|
2013-01-28 01:02:04 +01:00
|
|
|
*/
|
2014-06-04 17:08:25 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-03-27 16:43:34 +01:00
|
|
|
}
|
2013-01-28 01:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-15 14:10:10 +02:00
|
|
|
throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
|
2013-01-27 00:13:16 +01:00
|
|
|
}
|
2014-05-08 15:19:54 +02:00
|
|
|
|
2015-06-15 14:10:10 +02:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @throws InvalidPathException
|
|
|
|
*/
|
2023-03-28 10:34:01 +02:00
|
|
|
private function assertPathLength($path): void {
|
2014-05-08 15:19:54 +02:00
|
|
|
$maxLen = min(PHP_MAXPATHLEN, 4000);
|
2015-01-10 11:50:07 +01:00
|
|
|
// Check for the string length - performed using isset() instead of strlen()
|
|
|
|
// because isset() is about 5x-40x faster.
|
2015-01-26 14:34:40 +01:00
|
|
|
if (isset($path[$maxLen])) {
|
2015-01-10 11:50:07 +01:00
|
|
|
$pathLen = strlen($path);
|
2023-03-28 10:34:01 +02:00
|
|
|
throw new InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path");
|
2014-05-08 15:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-01 13:33:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check if it is allowed to move a mount point to a given target.
|
2015-10-02 12:14:24 +02:00
|
|
|
* It is not allowed to move a mount point into a different mount point or
|
|
|
|
* into an already shared folder
|
2014-07-01 13:33:21 +02:00
|
|
|
*/
|
2024-02-23 18:26:24 +01:00
|
|
|
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) {
|
2024-09-04 22:24:19 +02:00
|
|
|
$sharedPath = $share->getNode()->getPath();
|
|
|
|
if ($targetPath === $sharedPath || str_starts_with($targetPath, $sharedPath . '/')) {
|
2024-02-23 18:26:24 +01:00
|
|
|
$this->logger->debug(
|
|
|
|
'It is not allowed to move one mount point into a shared folder',
|
|
|
|
['app' => 'files']);
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-02 12:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-01 13:33:21 +02:00
|
|
|
}
|
2015-01-13 13:59:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a fileinfo object for files that are ignored in the cache (part files)
|
|
|
|
*/
|
2023-03-28 11:13:49 +02:00
|
|
|
private function getPartFileInfo(string $path): \OC\Files\FileInfo {
|
2015-01-26 14:34:40 +01:00
|
|
|
$mount = $this->getMount($path);
|
2015-01-13 13:59:28 +01:00
|
|
|
$storage = $mount->getStorage();
|
|
|
|
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
|
2024-09-16 16:00:46 +02:00
|
|
|
$ownerId = $storage->getOwner($internalPath);
|
|
|
|
if ($ownerId !== false) {
|
|
|
|
$owner = Server::get(IUserManager::class)->get($ownerId);
|
|
|
|
} else {
|
|
|
|
$owner = null;
|
|
|
|
}
|
2015-01-13 13:59:28 +01:00
|
|
|
return new FileInfo(
|
|
|
|
$this->getAbsolutePath($path),
|
|
|
|
$storage,
|
|
|
|
$internalPath,
|
|
|
|
[
|
|
|
|
'fileid' => null,
|
|
|
|
'mimetype' => $storage->getMimeType($internalPath),
|
|
|
|
'name' => basename($path),
|
|
|
|
'etag' => null,
|
|
|
|
'size' => $storage->filesize($internalPath),
|
|
|
|
'mtime' => $storage->filemtime($internalPath),
|
|
|
|
'encrypted' => false,
|
|
|
|
'permissions' => \OCP\Constants::PERMISSION_ALL
|
|
|
|
],
|
2015-11-02 14:56:38 +01:00
|
|
|
$mount,
|
|
|
|
$owner
|
2015-01-13 13:59:28 +01:00
|
|
|
);
|
|
|
|
}
|
2015-02-27 14:14:42 +01:00
|
|
|
|
2015-02-18 18:28:24 +01:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $fileName
|
2024-08-27 14:06:23 +02:00
|
|
|
* @param bool $readonly Check only if the path is allowed for read-only access
|
2015-02-18 18:28:24 +01:00
|
|
|
* @throws InvalidPathException
|
|
|
|
*/
|
2024-08-27 14:06:23 +02:00
|
|
|
public function verifyPath($path, $fileName, $readonly = false): void {
|
2024-07-17 02:03:18 +02:00
|
|
|
// 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'));
|
|
|
|
}
|
|
|
|
|
2024-08-27 14:06:23 +02:00
|
|
|
// Short cut for read-only validation
|
|
|
|
if ($readonly) {
|
2024-09-16 16:00:46 +02:00
|
|
|
$validator = Server::get(FilenameValidator::class);
|
2024-08-27 14:06:23 +02:00
|
|
|
if ($validator->isForbidden($fileName)) {
|
|
|
|
$l = \OCP\Util::getL10N('lib');
|
|
|
|
throw new InvalidPathException($l->t('Filename is a reserved word'));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-04 14:03:47 +01:00
|
|
|
try {
|
|
|
|
/** @type \OCP\Files\Storage $storage */
|
2020-09-15 17:01:33 +02:00
|
|
|
[$storage, $internalPath] = $this->resolvePath($path);
|
2015-03-04 14:03:47 +01:00
|
|
|
$storage->verifyPath($internalPath, $fileName);
|
|
|
|
} catch (ReservedWordException $ex) {
|
2024-02-01 16:13:27 +01:00
|
|
|
$l = \OCP\Util::getL10N('lib');
|
2024-08-27 19:42:00 +02:00
|
|
|
throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename is a reserved word'));
|
2015-03-04 14:03:47 +01:00
|
|
|
} catch (InvalidCharacterInPathException $ex) {
|
2024-02-01 16:13:27 +01:00
|
|
|
$l = \OCP\Util::getL10N('lib');
|
2024-08-27 19:42:00 +02:00
|
|
|
throw new InvalidPathException($ex->getMessage() ?: $l->t('Filename contains at least one invalid character'));
|
2015-03-10 13:08:22 +01:00
|
|
|
} catch (FileNameTooLongException $ex) {
|
2024-02-01 16:13:27 +01:00
|
|
|
$l = \OCP\Util::getL10N('lib');
|
2024-08-27 19:42:00 +02:00
|
|
|
throw new InvalidPathException($l->t('Filename is too long'));
|
2016-11-09 10:58:11 +01:00
|
|
|
} catch (InvalidDirectoryException $ex) {
|
2024-02-01 16:13:27 +01:00
|
|
|
$l = \OCP\Util::getL10N('lib');
|
2016-11-09 10:58:11 +01:00
|
|
|
throw new InvalidPathException($l->t('Dot files are not allowed'));
|
|
|
|
} catch (EmptyFileNameException $ex) {
|
2024-02-01 16:13:27 +01:00
|
|
|
$l = \OCP\Util::getL10N('lib');
|
2016-11-09 10:58:11 +01:00
|
|
|
throw new InvalidPathException($l->t('Empty filename is not allowed'));
|
2015-03-04 14:03:47 +01:00
|
|
|
}
|
2015-02-18 17:44:13 +01:00
|
|
|
}
|
2015-05-04 15:05:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get all parent folders of $path
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getParents($path) {
|
2015-05-20 15:22:53 +02:00
|
|
|
$path = trim($path, '/');
|
2015-05-05 13:48:49 +02:00
|
|
|
if (!$path) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-05-13 12:12:39 +02:00
|
|
|
|
2015-05-04 15:05:09 +02:00
|
|
|
$parts = explode('/', $path);
|
|
|
|
|
2015-05-13 12:12:39 +02:00
|
|
|
// remove the single file
|
2015-05-04 15:05:09 +02:00
|
|
|
array_pop($parts);
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = ['/'];
|
2015-05-04 15:05:09 +02:00
|
|
|
$resultPath = '';
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
if ($part) {
|
|
|
|
$resultPath .= '/' . $part;
|
|
|
|
$result[] = $resultPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
/**
|
|
|
|
* Returns the mount point for which to lock
|
|
|
|
*
|
|
|
|
* @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
|
2022-03-03 17:15:02 +01:00
|
|
|
* @return IMountPoint mount point for which to apply locks
|
2015-06-22 12:38:48 +02:00
|
|
|
*/
|
2023-03-28 10:34:01 +02:00
|
|
|
private function getMountForLock(string $absolutePath, bool $useParentMount = false): IMountPoint {
|
2015-06-22 12:38:48 +02:00
|
|
|
$mount = Filesystem::getMountManager()->find($absolutePath);
|
|
|
|
|
|
|
|
if ($useParentMount) {
|
|
|
|
// find out if something is mounted directly on the path
|
|
|
|
$internalPath = $mount->getInternalPath($absolutePath);
|
|
|
|
if ($internalPath === '') {
|
|
|
|
// resolve the parent mount instead
|
|
|
|
$mount = Filesystem::getMountManager()->find(dirname($absolutePath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $mount;
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:37:18 +02:00
|
|
|
/**
|
2015-06-11 18:32:15 +02:00
|
|
|
* Lock the given path
|
|
|
|
*
|
2015-05-13 13:37:18 +02:00
|
|
|
* @param string $path the path of the file to lock, relative to the view
|
|
|
|
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
|
2015-06-22 12:38:48 +02:00
|
|
|
* @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
|
|
|
|
*
|
2015-06-12 11:41:05 +02:00
|
|
|
* @return bool False if the path is excluded from locking, true otherwise
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException if the path is already locked
|
2015-05-13 13:37:18 +02:00
|
|
|
*/
|
2015-06-22 12:38:48 +02:00
|
|
|
private function lockPath($path, $type, $lockMountPoint = false) {
|
2015-06-11 16:11:16 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2015-06-16 11:40:27 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($absolutePath);
|
2015-06-11 16:11:16 +02:00
|
|
|
if (!$this->shouldLockFile($absolutePath)) {
|
2015-06-12 11:41:05 +02:00
|
|
|
return false;
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
|
2023-03-28 10:34:01 +02:00
|
|
|
try {
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
|
|
|
|
$storage->acquireLock(
|
|
|
|
$mount->getInternalPath($absolutePath),
|
|
|
|
$type,
|
|
|
|
$this->lockingProvider
|
2015-06-11 18:32:15 +02:00
|
|
|
);
|
|
|
|
}
|
2023-03-28 10:34:01 +02:00
|
|
|
} catch (LockedException $e) {
|
2025-01-28 20:25:28 +01:00
|
|
|
// rethrow with the human-readable path
|
2023-03-28 10:34:01 +02:00
|
|
|
throw new LockedException(
|
2025-01-28 20:25:28 +01:00
|
|
|
$path,
|
2023-03-28 10:34:01 +02:00
|
|
|
$e,
|
|
|
|
$e->getExistingLock()
|
|
|
|
);
|
2015-05-05 13:48:49 +02:00
|
|
|
}
|
2015-06-12 11:41:05 +02:00
|
|
|
|
|
|
|
return true;
|
2015-05-04 15:05:09 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 16:35:49 +02:00
|
|
|
/**
|
2015-06-12 18:50:49 +02:00
|
|
|
* Change the lock type
|
|
|
|
*
|
2015-05-29 16:35:49 +02:00
|
|
|
* @param string $path the path of the file to lock, relative to the view
|
|
|
|
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
|
2015-06-22 12:38:48 +02:00
|
|
|
* @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
|
|
|
|
*
|
2015-06-12 11:41:05 +02:00
|
|
|
* @return bool False if the path is excluded from locking, true otherwise
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException if the path is already locked
|
2015-05-29 16:35:49 +02:00
|
|
|
*/
|
2015-06-22 12:38:48 +02:00
|
|
|
public function changeLock($path, $type, $lockMountPoint = false) {
|
2015-06-10 15:18:45 +02:00
|
|
|
$path = Filesystem::normalizePath($path);
|
2015-06-11 16:11:16 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2015-06-16 11:40:27 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($absolutePath);
|
2015-06-11 16:11:16 +02:00
|
|
|
if (!$this->shouldLockFile($absolutePath)) {
|
2015-06-12 11:41:05 +02:00
|
|
|
return false;
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
|
2023-03-28 10:34:01 +02:00
|
|
|
try {
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
|
|
|
|
$storage->changeLock(
|
|
|
|
$mount->getInternalPath($absolutePath),
|
|
|
|
$type,
|
|
|
|
$this->lockingProvider
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (LockedException $e) {
|
2025-01-28 20:25:28 +01:00
|
|
|
// rethrow with the a human-readable path
|
|
|
|
throw new LockedException(
|
|
|
|
$path,
|
|
|
|
$e,
|
|
|
|
$e->getExistingLock()
|
|
|
|
);
|
2015-05-29 16:35:49 +02:00
|
|
|
}
|
2015-06-12 11:41:05 +02:00
|
|
|
|
|
|
|
return true;
|
2015-05-29 16:35:49 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 13:37:18 +02:00
|
|
|
/**
|
2015-06-11 18:32:15 +02:00
|
|
|
* Unlock the given path
|
|
|
|
*
|
2015-05-13 13:37:18 +02:00
|
|
|
* @param string $path the path of the file to unlock, relative to the view
|
|
|
|
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
|
2015-06-22 12:38:48 +02:00
|
|
|
* @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
|
|
|
|
*
|
2015-06-12 11:41:05 +02:00
|
|
|
* @return bool False if the path is excluded from locking, true otherwise
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-05-13 13:37:18 +02:00
|
|
|
*/
|
2015-06-22 12:38:48 +02:00
|
|
|
private function unlockPath($path, $type, $lockMountPoint = false) {
|
2015-06-11 16:11:16 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2015-06-16 11:40:27 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($absolutePath);
|
2015-06-11 16:11:16 +02:00
|
|
|
if (!$this->shouldLockFile($absolutePath)) {
|
2015-06-12 11:41:05 +02:00
|
|
|
return false;
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
|
2023-03-28 10:34:01 +02:00
|
|
|
$storage = $mount->getStorage();
|
|
|
|
if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
|
|
|
|
$storage->releaseLock(
|
|
|
|
$mount->getInternalPath($absolutePath),
|
|
|
|
$type,
|
|
|
|
$this->lockingProvider
|
|
|
|
);
|
2015-05-05 13:48:49 +02:00
|
|
|
}
|
2015-06-12 11:41:05 +02:00
|
|
|
|
|
|
|
return true;
|
2015-05-04 15:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-13 13:37:18 +02:00
|
|
|
* Lock a path and all its parents up to the root of the view
|
2015-05-04 15:05:09 +02:00
|
|
|
*
|
2015-05-13 13:37:18 +02:00
|
|
|
* @param string $path the path of the file to lock relative to the view
|
|
|
|
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
|
2015-06-22 12:38:48 +02:00
|
|
|
* @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
|
|
|
|
*
|
2015-06-12 11:41:05 +02:00
|
|
|
* @return bool False if the path is excluded from locking, true otherwise
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-05-04 15:05:09 +02:00
|
|
|
*/
|
2015-06-22 12:38:48 +02:00
|
|
|
public function lockFile($path, $type, $lockMountPoint = false) {
|
2015-06-11 16:11:16 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2015-06-16 11:40:27 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($absolutePath);
|
2015-06-11 16:11:16 +02:00
|
|
|
if (!$this->shouldLockFile($absolutePath)) {
|
2015-06-12 11:41:05 +02:00
|
|
|
return false;
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
$this->lockPath($path, $type, $lockMountPoint);
|
2015-05-04 15:05:09 +02:00
|
|
|
|
|
|
|
$parents = $this->getParents($path);
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$this->lockPath($parent, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
2015-06-12 11:41:05 +02:00
|
|
|
|
|
|
|
return true;
|
2015-05-04 15:05:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-13 13:37:18 +02:00
|
|
|
* Unlock a path and all its parents up to the root of the view
|
2015-05-04 15:05:09 +02:00
|
|
|
*
|
2015-05-13 13:37:18 +02:00
|
|
|
* @param string $path the path of the file to lock relative to the view
|
|
|
|
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
|
2015-06-22 12:38:48 +02:00
|
|
|
* @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
|
|
|
|
*
|
2015-06-12 11:41:05 +02:00
|
|
|
* @return bool False if the path is excluded from locking, true otherwise
|
2019-11-28 14:17:15 +01:00
|
|
|
* @throws LockedException
|
2015-05-04 15:05:09 +02:00
|
|
|
*/
|
2015-06-22 12:38:48 +02:00
|
|
|
public function unlockFile($path, $type, $lockMountPoint = false) {
|
2015-06-11 16:11:16 +02:00
|
|
|
$absolutePath = $this->getAbsolutePath($path);
|
2015-06-16 11:40:27 +02:00
|
|
|
$absolutePath = Filesystem::normalizePath($absolutePath);
|
2015-06-11 16:11:16 +02:00
|
|
|
if (!$this->shouldLockFile($absolutePath)) {
|
2015-06-12 11:41:05 +02:00
|
|
|
return false;
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:38:48 +02:00
|
|
|
$this->unlockPath($path, $type, $lockMountPoint);
|
2015-05-04 15:05:09 +02:00
|
|
|
|
|
|
|
$parents = $this->getParents($path);
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$this->unlockPath($parent, ILockingProvider::LOCK_SHARED);
|
|
|
|
}
|
2015-06-12 11:41:05 +02:00
|
|
|
|
|
|
|
return true;
|
2015-05-04 15:05:09 +02:00
|
|
|
}
|
2015-06-11 16:11:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Only lock files in data/user/files/
|
|
|
|
*
|
|
|
|
* @param string $path Absolute path to the file/folder we try to (un)lock
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldLockFile($path) {
|
|
|
|
$path = Filesystem::normalizePath($path);
|
|
|
|
|
2015-06-12 11:58:26 +02:00
|
|
|
$pathSegments = explode('/', $path);
|
|
|
|
if (isset($pathSegments[2])) {
|
2015-06-11 16:11:16 +02:00
|
|
|
// E.g.: /username/files/path-to-file
|
2015-10-23 14:17:12 +02:00
|
|
|
return ($pathSegments[2] === 'files') && (count($pathSegments) > 3);
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:17:19 +03:30
|
|
|
return !str_starts_with($path, '/appdata_');
|
2015-06-11 16:11:16 +02:00
|
|
|
}
|
2015-06-11 18:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortens the given absolute path to be relative to
|
|
|
|
* "$user/files".
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the given path was not under "files"
|
|
|
|
* @since 8.1.0
|
|
|
|
*/
|
|
|
|
public function getPathRelativeToFiles($absolutePath) {
|
|
|
|
$path = Filesystem::normalizePath($absolutePath);
|
|
|
|
$parts = explode('/', trim($path, '/'), 3);
|
|
|
|
// "$user", "files", "path/to/dir"
|
|
|
|
if (!isset($parts[1]) || $parts[1] !== 'files') {
|
2017-03-20 11:06:08 +01:00
|
|
|
$this->logger->error(
|
2022-03-30 10:55:41 +02:00
|
|
|
'$absolutePath must be relative to "files", value is "{absolutePath}"',
|
2017-03-20 11:06:08 +01:00
|
|
|
[
|
2022-03-30 10:55:41 +02:00
|
|
|
'absolutePath' => $absolutePath,
|
2017-03-20 11:06:08 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
throw new \InvalidArgumentException('$absolutePath must be relative to "files"');
|
2015-06-11 18:32:15 +02:00
|
|
|
}
|
|
|
|
if (isset($parts[2])) {
|
|
|
|
return $parts[2];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2015-12-10 14:14:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return array
|
|
|
|
* @throws \OC\User\NoUserException
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function getUidAndFilename($filename) {
|
|
|
|
$info = $this->getFileInfo($filename);
|
|
|
|
if (!$info instanceof \OCP\Files\FileInfo) {
|
2015-12-14 10:24:38 +01:00
|
|
|
throw new NotFoundException($this->getAbsolutePath($filename) . ' not found');
|
2015-12-10 14:14:54 +01:00
|
|
|
}
|
|
|
|
$uid = $info->getOwner()->getUID();
|
2021-02-16 11:56:07 +01:00
|
|
|
if ($uid != \OC_User::getUser()) {
|
2015-12-10 14:14:54 +01:00
|
|
|
Filesystem::initMountPoints($uid);
|
|
|
|
$ownerView = new View('/' . $uid . '/files');
|
|
|
|
try {
|
|
|
|
$filename = $ownerView->getPath($info['fileid']);
|
|
|
|
} catch (NotFoundException $e) {
|
2015-12-14 10:24:38 +01:00
|
|
|
throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid);
|
2015-12-10 14:14:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [$uid, $filename];
|
|
|
|
}
|
2016-06-21 17:10:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates parent non-existing folders
|
|
|
|
*
|
|
|
|
* @param string $filePath
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function createParentDirectories($filePath) {
|
2016-12-01 16:52:12 +01:00
|
|
|
$directoryParts = explode('/', $filePath);
|
|
|
|
$directoryParts = array_filter($directoryParts);
|
2017-01-03 15:01:47 +01:00
|
|
|
foreach ($directoryParts as $key => $part) {
|
2016-12-01 16:52:12 +01:00
|
|
|
$currentPathElements = array_slice($directoryParts, 0, $key);
|
|
|
|
$currentPath = '/' . implode('/', $currentPathElements);
|
2017-01-03 15:01:47 +01:00
|
|
|
if ($this->is_file($currentPath)) {
|
2016-06-21 17:10:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-03 15:01:47 +01:00
|
|
|
if (!$this->file_exists($currentPath)) {
|
2016-12-01 16:52:12 +01:00
|
|
|
$this->mkdir($currentPath);
|
|
|
|
}
|
2016-06-21 17:10:52 +02:00
|
|
|
}
|
2016-12-01 16:52:12 +01:00
|
|
|
|
2016-06-21 17:10:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-20 00:40:52 +01:00
|
|
|
}
|