Skip to content

Commit

Permalink
make LazyFolder::get not load the real folder if we know the path
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Aug 15, 2023
1 parent d729daa commit e67f7f6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
50 changes: 36 additions & 14 deletions lib/private/Files/Node/LazyFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
use OC\Files\Utils\PathHelper;
use OCP\Files\Folder;
use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotPermittedException;

/**
* Class LazyFolder
Expand All @@ -41,23 +43,33 @@
*/
class LazyFolder implements Folder {
/** @var \Closure(): Folder */
private $folderClosure;

/** @var LazyFolder | null */
protected $folder = null;

private \Closure $folderClosure;
protected ?Folder $folder = null;
protected IRootFolder $rootFolder;
protected array $data;

/**
* LazyFolder constructor.
*
* @param IRootFolder $rootFolder
* @param \Closure(): Folder $folderClosure
* @param array $data
*/
public function __construct(\Closure $folderClosure, array $data = []) {
public function __construct(IRootFolder $rootFolder, \Closure $folderClosure, array $data = []) {
$this->rootFolder = $rootFolder;
$this->folderClosure = $folderClosure;
$this->data = $data;
}

protected function getRootFolder(): IRootFolder {
return $this->rootFolder;
}

protected function getRealFolder(): Folder {
if ($this->folder === null) {
$this->folder = call_user_func($this->folderClosure);
}
return $this->folder;
}

/**
* Magic method to first get the real rootFolder and then
* call $method with $args on it
Expand All @@ -67,11 +79,7 @@ public function __construct(\Closure $folderClosure, array $data = []) {
* @return mixed
*/
public function __call($method, $args) {
if ($this->folder === null) {
$this->folder = call_user_func($this->folderClosure);
}

return call_user_func_array([$this->folder, $method], $args);
return call_user_func_array([$this->getRealFolder(), $method], $args);
}

/**
Expand Down Expand Up @@ -148,7 +156,7 @@ public function unMount($mount) {
* @inheritDoc
*/
public function get($path) {
return $this->__call(__FUNCTION__, func_get_args());
return $this->getRootFolder()->get($this->getFullPath($path));
}

/**
Expand Down Expand Up @@ -408,9 +416,23 @@ public function getExtension(): string {
* @inheritDoc
*/
public function getFullPath($path) {
if (isset($this->data['path'])) {
$path = PathHelper::normalizePath($path);
if (!$this->isValidPath($path)) {
throw new NotPermittedException('Invalid path "' . $path . '"');
}
return $this->data['path'] . $path;
}
return $this->__call(__FUNCTION__, func_get_args());
}

public function isValidPath($path) {
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
return !(str_contains($path, '/../') || strrchr($path, '/') === '/..');
}

/**
* @inheritDoc
*/
Expand Down
15 changes: 12 additions & 3 deletions lib/private/Files/Node/LazyRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@
* @package OC\Files\Node
*/
class LazyRoot extends LazyFolder implements IRootFolder {
/**
* @inheritDoc
*/
public function __construct(\Closure $folderClosure, array $data = []) {
parent::__construct($this, $folderClosure, $data);
}

protected function getRootFolder(): IRootFolder {
$folder = $this->getRealFolder();
if (!$folder instanceof IRootFolder) {
throw new \Exception('Lazy root folder closure didn\'t return a root folder');
}
return $folder;
}

public function getUserFolder($userId) {
return $this->__call(__FUNCTION__, func_get_args());
}
Expand Down
16 changes: 7 additions & 9 deletions lib/private/Files/Node/LazyUserFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@
use Psr\Log\LoggerInterface;

class LazyUserFolder extends LazyFolder {
private IRootFolder $root;
private IUser $user;
private string $path;
private IMountManager $mountManager;

public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
$this->root = $rootFolder;
$this->user = $user;
$this->mountManager = $mountManager;
$this->path = '/' . $user->getUID() . '/files';
parent::__construct(function () use ($user): Folder {
parent::__construct($rootFolder, function () use ($user): Folder {
try {
$node = $this->root->get($this->path);
$node = $this->getRootFolder()->get($this->path);
if ($node instanceof File) {
$e = new \RuntimeException();
\OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [
Expand All @@ -56,10 +54,10 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
}
return $node;
} catch (NotFoundException $e) {
if (!$this->root->nodeExists('/' . $user->getUID())) {
$this->root->newFolder('/' . $user->getUID());
if (!$this->getRootFolder()->nodeExists('/' . $user->getUID())) {
$this->getRootFolder()->newFolder('/' . $user->getUID());
}
return $this->root->newFolder($this->path);
return $this->getRootFolder()->newFolder($this->path);
}
}, [
'path' => $this->path,
Expand All @@ -70,15 +68,15 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
}

public function get($path) {
return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
return $this->getRootFolder()->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
}

/**
* @param int $id
* @return \OCP\Files\Node[]
*/
public function getById($id) {
return $this->root->getByIdInPath((int)$id, $this->getPath());
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
}

public function getMountPoint() {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function getParent(): INode|IRootFolder {
];

// and create lazy folder with it instead of always querying
$this->parent = new LazyFolder(function () use ($newPath) {
$this->parent = new LazyFolder($this->root, function () use ($newPath) {
return $this->root->get($newPath);
}, $parentData);
}
Expand Down

0 comments on commit e67f7f6

Please sign in to comment.