2013-06-30 12:02:34 -04:00
|
|
|
<?php
|
2024-05-27 11:39:07 -04:00
|
|
|
|
2013-06-30 12:02:34 -04:00
|
|
|
/**
|
2024-05-27 11:39:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-06-30 12:02:34 -04:00
|
|
|
*/
|
2015-08-30 13:13:01 -04:00
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
2013-06-30 12:02:34 -04:00
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Files\FileInfo;
|
2017-11-30 15:29:06 -05:00
|
|
|
use OC\Files\Storage\FailedStorage;
|
2024-10-18 06:04:22 -04:00
|
|
|
use OC\Files\Storage\Storage;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OC\Files\View;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
|
2015-11-13 08:13:16 -05:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
|
2015-08-30 13:13:01 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
|
2015-11-13 08:13:16 -05:00
|
|
|
use OCP\Files\ForbiddenException;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\Files\InvalidPathException;
|
|
|
|
|
use OCP\Files\Mount\IMountManager;
|
2014-07-01 09:03:29 -04:00
|
|
|
use OCP\Files\StorageInvalidException;
|
2014-06-30 10:36:11 -04:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2015-05-29 03:59:20 -04:00
|
|
|
use OCP\Lock\LockedException;
|
2013-06-30 12:02:34 -04:00
|
|
|
|
2017-06-14 12:11:55 -04:00
|
|
|
class ObjectTree extends CachingTree {
|
2013-09-26 04:50:15 -04:00
|
|
|
|
|
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @var View
|
2013-09-26 04:50:15 -04:00
|
|
|
*/
|
2014-02-25 10:23:09 -05:00
|
|
|
protected $fileView;
|
|
|
|
|
|
2014-06-17 08:10:11 -04:00
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @var IMountManager
|
2014-06-17 08:10:11 -04:00
|
|
|
*/
|
|
|
|
|
protected $mountManager;
|
|
|
|
|
|
2014-02-25 10:23:09 -05:00
|
|
|
/**
|
|
|
|
|
* Creates the object
|
2014-03-04 07:28:48 -05:00
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-27 03:22:52 -05:00
|
|
|
* @param \Sabre\DAV\INode $rootNode
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param View $view
|
|
|
|
|
* @param IMountManager $mountManager
|
2014-02-25 10:23:09 -05:00
|
|
|
*/
|
2024-10-10 06:40:31 -04:00
|
|
|
public function init(\Sabre\DAV\INode $rootNode, View $view, IMountManager $mountManager) {
|
2014-03-04 07:28:48 -05:00
|
|
|
$this->rootNode = $rootNode;
|
2014-02-25 10:23:09 -05:00
|
|
|
$this->fileView = $view;
|
2014-06-17 08:10:11 -04:00
|
|
|
$this->mountManager = $mountManager;
|
2014-02-25 10:23:09 -05:00
|
|
|
}
|
2013-09-26 04:50:15 -04:00
|
|
|
|
2013-06-30 12:02:34 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the INode object for the requested path
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
2014-01-09 08:25:48 -05:00
|
|
|
* @return \Sabre\DAV\INode
|
2016-03-24 09:29:55 -04:00
|
|
|
* @throws InvalidPath
|
|
|
|
|
* @throws \Sabre\DAV\Exception\Locked
|
|
|
|
|
* @throws \Sabre\DAV\Exception\NotFound
|
|
|
|
|
* @throws \Sabre\DAV\Exception\ServiceUnavailable
|
2013-06-30 12:02:34 -04:00
|
|
|
*/
|
|
|
|
|
public function getNodeForPath($path) {
|
2014-03-04 07:28:48 -05:00
|
|
|
if (!$this->fileView) {
|
2014-01-09 08:25:48 -05:00
|
|
|
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
|
2014-03-04 07:28:48 -05:00
|
|
|
}
|
2013-06-30 12:02:34 -04:00
|
|
|
|
|
|
|
|
$path = trim($path, '/');
|
2015-12-30 08:10:03 -05:00
|
|
|
|
|
|
|
|
if (isset($this->cache[$path])) {
|
|
|
|
|
return $this->cache[$path];
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 11:10:11 -04:00
|
|
|
if ($path) {
|
2015-06-15 11:49:11 -04:00
|
|
|
try {
|
|
|
|
|
$this->fileView->verifyPath($path, basename($path));
|
2024-10-10 06:40:31 -04:00
|
|
|
} catch (InvalidPathException $ex) {
|
2015-06-15 11:49:11 -04:00
|
|
|
throw new InvalidPath($ex->getMessage());
|
|
|
|
|
}
|
2015-06-11 11:10:11 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 12:02:34 -04:00
|
|
|
// Is it the root node?
|
|
|
|
|
if (!strlen($path)) {
|
|
|
|
|
return $this->rootNode;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 06:46:12 -05:00
|
|
|
if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
|
|
|
|
|
// read from storage
|
2014-02-25 10:23:09 -05:00
|
|
|
$absPath = $this->fileView->getAbsolutePath($path);
|
2014-12-16 08:24:48 -05:00
|
|
|
$mount = $this->fileView->getMount($path);
|
|
|
|
|
$storage = $mount->getStorage();
|
|
|
|
|
$internalPath = $mount->getInternalPath($absPath);
|
2016-02-11 11:22:40 -05:00
|
|
|
if ($storage && $storage->file_exists($internalPath)) {
|
2014-02-25 10:23:09 -05:00
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @var Storage $storage
|
2014-02-25 10:23:09 -05:00
|
|
|
*/
|
2013-11-27 06:46:12 -05:00
|
|
|
// get data directly
|
2015-12-02 09:05:29 -05:00
|
|
|
$data = $storage->getMetaData($internalPath);
|
2014-12-16 08:24:48 -05:00
|
|
|
$info = new FileInfo($absPath, $storage, $internalPath, $data, $mount);
|
2014-02-25 10:23:09 -05:00
|
|
|
} else {
|
|
|
|
|
$info = null;
|
2013-11-27 06:46:12 -05:00
|
|
|
}
|
2014-02-25 10:23:09 -05:00
|
|
|
} else {
|
2013-11-27 06:46:12 -05:00
|
|
|
// read from cache
|
2014-06-30 10:36:11 -04:00
|
|
|
try {
|
|
|
|
|
$info = $this->fileView->getFileInfo($path);
|
2017-11-30 15:29:06 -05:00
|
|
|
|
2017-12-01 06:34:37 -05:00
|
|
|
if ($info instanceof \OCP\Files\FileInfo && $info->getStorage()->instanceOfStorage(FailedStorage::class)) {
|
2017-11-30 15:29:06 -05:00
|
|
|
throw new StorageNotAvailableException();
|
|
|
|
|
}
|
2014-06-30 10:36:11 -04:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
2018-07-03 08:56:37 -04:00
|
|
|
throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage is temporarily not available', 0, $e);
|
2023-02-21 01:36:43 -05:00
|
|
|
} catch (StorageInvalidException $e) {
|
|
|
|
|
throw new \Sabre\DAV\Exception\NotFound('Storage ' . $path . ' is invalid');
|
|
|
|
|
} catch (LockedException $e) {
|
|
|
|
|
throw new \Sabre\DAV\Exception\Locked();
|
|
|
|
|
} catch (ForbiddenException $e) {
|
|
|
|
|
throw new \Sabre\DAV\Exception\Forbidden();
|
2014-06-30 10:36:11 -04:00
|
|
|
}
|
2013-11-27 06:46:12 -05:00
|
|
|
}
|
2013-06-30 12:02:34 -04:00
|
|
|
|
|
|
|
|
if (!$info) {
|
2014-01-09 08:25:48 -05:00
|
|
|
throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
|
2013-06-30 12:02:34 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-25 10:23:09 -05:00
|
|
|
if ($info->getType() === 'dir') {
|
2024-10-10 06:40:31 -04:00
|
|
|
$node = new Directory($this->fileView, $info, $this);
|
2013-06-30 12:02:34 -04:00
|
|
|
} else {
|
2024-10-10 06:40:31 -04:00
|
|
|
$node = new File($this->fileView, $info);
|
2013-06-30 12:02:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->cache[$path] = $node;
|
|
|
|
|
return $node;
|
|
|
|
|
}
|
2013-06-30 12:27:55 -04:00
|
|
|
|
2013-06-30 13:41:38 -04:00
|
|
|
/**
|
|
|
|
|
* Copies a file or directory.
|
|
|
|
|
*
|
|
|
|
|
* This method must work recursively and delete the destination
|
|
|
|
|
* if it exists
|
|
|
|
|
*
|
2020-08-19 11:54:00 -04:00
|
|
|
* @param string $sourcePath
|
|
|
|
|
* @param string $destinationPath
|
2016-06-30 07:19:50 -04:00
|
|
|
* @throws FileLocked
|
|
|
|
|
* @throws Forbidden
|
|
|
|
|
* @throws InvalidPath
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
* @throws \Sabre\DAV\Exception\Forbidden
|
|
|
|
|
* @throws \Sabre\DAV\Exception\Locked
|
|
|
|
|
* @throws \Sabre\DAV\Exception\NotFound
|
2014-01-09 08:25:48 -05:00
|
|
|
* @throws \Sabre\DAV\Exception\ServiceUnavailable
|
2013-06-30 13:41:38 -04:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2020-08-19 11:54:00 -04:00
|
|
|
public function copy($sourcePath, $destinationPath) {
|
2014-03-04 07:28:48 -05:00
|
|
|
if (!$this->fileView) {
|
2014-01-09 08:25:48 -05:00
|
|
|
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
|
2014-03-04 07:28:48 -05:00
|
|
|
}
|
2013-06-30 13:41:38 -04:00
|
|
|
|
2016-06-30 05:09:20 -04:00
|
|
|
|
2020-08-19 11:54:00 -04:00
|
|
|
$info = $this->fileView->getFileInfo(dirname($destinationPath));
|
|
|
|
|
if ($this->fileView->file_exists($destinationPath)) {
|
2016-09-07 05:10:48 -04:00
|
|
|
$destinationPermission = $info && $info->isUpdateable();
|
|
|
|
|
} else {
|
|
|
|
|
$destinationPermission = $info && $info->isCreatable();
|
|
|
|
|
}
|
|
|
|
|
if (!$destinationPermission) {
|
2016-06-30 07:50:31 -04:00
|
|
|
throw new Forbidden('No permissions to copy object.');
|
2016-06-30 05:09:20 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-25 10:35:13 -05:00
|
|
|
// this will trigger existence check
|
2020-08-19 11:54:00 -04:00
|
|
|
$this->getNodeForPath($sourcePath);
|
2013-06-30 13:41:38 -04:00
|
|
|
|
2021-01-12 04:15:48 -05:00
|
|
|
[$destinationDir, $destinationName] = \Sabre\Uri\split($destinationPath);
|
2015-05-29 13:14:38 -04:00
|
|
|
try {
|
|
|
|
|
$this->fileView->verifyPath($destinationDir, $destinationName);
|
2024-10-10 06:40:31 -04:00
|
|
|
} catch (InvalidPathException $ex) {
|
2015-05-29 13:14:38 -04:00
|
|
|
throw new InvalidPath($ex->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 10:35:13 -05:00
|
|
|
try {
|
2020-08-19 11:54:00 -04:00
|
|
|
$this->fileView->copy($sourcePath, $destinationPath);
|
2015-05-29 03:59:20 -04:00
|
|
|
} catch (StorageNotAvailableException $e) {
|
2014-11-06 04:59:36 -05:00
|
|
|
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
|
2015-11-13 08:13:16 -05:00
|
|
|
} catch (ForbiddenException $ex) {
|
|
|
|
|
throw new Forbidden($ex->getMessage(), $ex->getRetry());
|
2015-05-29 04:55:25 -04:00
|
|
|
} catch (LockedException $e) {
|
|
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
|
2013-06-30 13:41:38 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-12 04:15:48 -05:00
|
|
|
[$destinationDir,] = \Sabre\Uri\split($destinationPath);
|
2013-06-30 13:41:38 -04:00
|
|
|
$this->markDirty($destinationDir);
|
|
|
|
|
}
|
2013-06-30 12:02:34 -04:00
|
|
|
}
|