Skip to content

Commit

Permalink
fix: getNodeForPath caching (#1545)
Browse files Browse the repository at this point in the history
* fix getNodeForPath caching

* add test that cached parent nodes are used in Tree::getNodeForPath
  • Loading branch information
icewind1991 authored Sep 5, 2024
1 parent fd570dc commit 6e72bc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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;
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

0 comments on commit 6e72bc2

Please sign in to comment.