Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getNodeForPath caching #1545

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/DAV/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,21 @@ public function getNodeForPath($path)
return $this->rootNode;
}

$parts = explode('/', $path);
$node = $this->rootNode;

// look for any cached parent and collect the parts below the parent
$parts = [];
$remainingPath = $path;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we know that $path is not the empty string, because of the check a few lines above.
So the call to Uri\split() below will always pass a non-empty string.
So we don't have to worry about the fact that Uri\split returns [null, null] when passed and empty string.

Just noting this, because I saw that Uri\split can return [null, null] and I was concerned about that - it would cause it to be called again with null and explode.

do {
list($remainingPath, $baseName) = Uri\split($remainingPath);
array_unshift($parts, $baseName);

if (isset($this->cache[$remainingPath])) {
$node = $this->cache[$remainingPath];
break;
}
} while ('' !== $remainingPath);

while (count($parts)) {
if (!($node instanceof ICollection)) {
throw new Exception\NotFound('Could not find node at path: '.$path);
Expand Down
17 changes: 17 additions & 0 deletions tests/Sabre/DAV/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ public function testGetSubTreeNode()
$this->assertInstanceOf(INode::class, $tree->getNodeForPath('subtree/sub/1'));
$this->assertInstanceOf(INode::class, $tree->getNodeForPath('subtree/2/3'));
}

public function testGetNodeCacheParent()
{
$tree = new TreeMock();

/** @var TreeDirectoryTester $root */
$root = $tree->getNodeForPath('');
$root->createDirectory('new');
$parent = $tree->getNodeForPath('new');
$parent->createDirectory('child');

// make it so we can't create the 'new' folder again
unset($root->newDirectories['new']);

// we should still be able to query child items from the 'new' folder because it is cached in the tree
$this->assertInstanceOf(INode::class, $tree->getNodeForPath('new/child'));
}
}

class TreeMock extends Tree
Expand Down