]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix: Use proper path when creating node instances
authorJulius Härtl <jus@bitgrid.net>
Sat, 18 Feb 2023 14:43:39 +0000 (15:43 +0100)
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>
Mon, 8 May 2023 15:21:52 +0000 (15:21 +0000)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
apps/dav/lib/Connector/Sabre/Node.php
apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php
lib/private/Files/Node/Node.php
tests/lib/Files/Node/FolderTest.php

index 2c8d313eefda3b8b1a9183a28f6e57b8ab02b78a..c9407b127860d7fde7b5200faa1c636b22d5a835 100644 (file)
@@ -91,11 +91,13 @@ abstract class Node implements \Sabre\DAV\INode {
                if ($info instanceof Folder || $info instanceof File) {
                        $this->node = $info;
                } else {
+                       // The Node API assumes that the view passed doesn't have a fake root
+                       $rootView = \OC::$server->get(View::class);
                        $root = \OC::$server->get(IRootFolder::class);
                        if ($info->getType() === FileInfo::TYPE_FOLDER) {
-                               $this->node = new Folder($root, $view, $this->path, $info);
+                               $this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
                        } else {
-                               $this->node = new File($root, $view, $this->path, $info);
+                               $this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
                        }
                }
        }
@@ -107,10 +109,11 @@ abstract class Node implements \Sabre\DAV\INode {
                }
                $this->info = $info;
                $root = \OC::$server->get(IRootFolder::class);
+               $rootView = \OC::$server->get(View::class);
                if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
-                       $this->node = new Folder($root, $this->fileView, $this->path, $this->info);
+                       $this->node = new Folder($root, $rootView, $this->path, $this->info);
                } else {
-                       $this->node = new File($root, $this->fileView, $this->path, $this->info);
+                       $this->node = new File($root, $rootView, $this->path, $this->info);
                }
        }
 
index a74cb139966071a5574e1a4b28b6d4ffeb5362d4..65ae08d67d3f1e88aebfd2aaebd92d307549be77 100644 (file)
 namespace OCA\DAV\Tests\Unit\Connector\Sabre;
 
 use OC\Files\FileInfo;
+use OC\Files\Filesystem;
 use OC\Files\Node\Node;
 use OC\Files\Storage\Wrapper\Quota;
+use OC\Files\View;
 use OCA\DAV\Connector\Sabre\Directory;
+use OCP\Constants;
 use OCP\Files\ForbiddenException;
 use OCP\Files\Mount\IMountPoint;
 use Test\Traits\UserTrait;
@@ -92,6 +95,10 @@ class DirectoryTest extends \Test\TestCase {
                        ->willReturn(Node::TYPE_FOLDER);
                $this->info->method('getName')
                        ->willReturn("folder");
+               $this->info->method('getPath')
+                       ->willReturn("/admin/files/folder");
+               $this->info->method('getPermissions')
+                       ->willReturn(Constants::PERMISSION_READ);
        }
 
        private function getDir($path = '/') {
@@ -208,12 +215,21 @@ class DirectoryTest extends \Test\TestCase {
 
                $this->view->expects($this->once())
                        ->method('getDirectoryContent')
-                       ->with('')
                        ->willReturn([$info1, $info2]);
 
                $this->view->expects($this->any())
                        ->method('getRelativePath')
-                       ->willReturn('');
+                       ->willReturnCallback(function($path) {
+                               return str_replace('/admin/files/', '', $path);
+                       });
+
+               $this->view->expects($this->any())
+                       ->method('getAbsolutePath')
+                       ->willReturnCallback(function($path) {
+                               return Filesystem::normalizePath('/admin/files' . $path);
+                       });
+
+               $this->overwriteService(View::class, $this->view);
 
                $dir = new Directory($this->view, $this->info);
                $nodes = $dir->getChildren();
index 2f88cc3a15acd3faa85aad8222000fa1f7aa14c3..0eef27161410a2018bde5efcd61557e8ca7426e5 100644 (file)
@@ -37,6 +37,7 @@ use OCP\Files\InvalidPathException;
 use OCP\Files\NotFoundException;
 use OCP\Files\NotPermittedException;
 use OCP\Lock\LockedException;
+use OCP\PreConditionNotMetException;
 use Symfony\Component\EventDispatcher\GenericEvent;
 
 // FIXME: this class really should be abstract
@@ -52,7 +53,7 @@ class Node implements \OCP\Files\Node {
        protected $root;
 
        /**
-        * @var string $path
+        * @var string $path Absolute path to the node (e.g. /admin/files/folder/file)
         */
        protected $path;
 
@@ -72,6 +73,9 @@ class Node implements \OCP\Files\Node {
         * @param FileInfo $fileInfo
         */
        public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null, bool $infoHasSubMountsIncluded = true) {
+               if (Filesystem::normalizePath($view->getRoot()) !== '/') {
+                       throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
+               }
                $this->view = $view;
                $this->root = $root;
                $this->path = $path;
index 8a604af38469570b06f70faae377bc742693a2f0..f8573f0214df574db1fe97754d97aaad873d7bb0 100644 (file)
@@ -81,6 +81,8 @@ class FolderTest extends NodeTest {
                        ]);
                $view->method('getFileInfo')
                        ->willReturn($this->createMock(FileInfo::class));
+               $view->method('getRelativePath')
+                       ->willReturn('/bar/foo');
 
                $node = new Folder($root, $view, '/bar/foo');
                $children = $node->getDirectoryListing();