use more efficient method to find mountpoint for path

this changes the complexity from the number of mounts to the depth of
the path

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2018-08-16 18:53:32 +02:00
parent 95981810c0
commit f8116ad4cf
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB

View file

@ -80,32 +80,29 @@ class Manager implements IMountManager {
*/
public function find(string $path) {
\OC_Util::setupFS();
$path = $this->formatPath($path);
if (isset($this->mounts[$path])) {
return $this->mounts[$path];
}
$path = Filesystem::normalizePath($path);
if (isset($this->pathCache[$path])) {
return $this->pathCache[$path];
}
\OC_Hook::emit('OC_Filesystem', 'get_mountpoint', ['path' => $path]);
$foundMountPoint = '';
$mountPoints = array_keys($this->mounts);
$foundMountPointLength = 0;
foreach ($mountPoints as $mountpoint) {
if (\strlen($mountpoint) > $foundMountPointLength && strpos($path, $mountpoint) === 0) {
$foundMountPoint = $mountpoint;
$foundMountPointLength = \strlen($foundMountPoint);
$current = $path;
while(true) {
$mountPoint = $current . '/';
if (isset($this->mounts[$mountPoint])) {
$this->pathCache[$path] = $this->mounts[$mountPoint];
return $this->mounts[$mountPoint];
}
if ($current === '') {
return null;
}
$current = dirname($current);
if ($current === '.' || $current === '/') {
$current = '';
}
}
if (isset($this->mounts[$foundMountPoint])) {
$this->pathCache[$path] = $this->mounts[$foundMountPoint];
return $this->mounts[$foundMountPoint];
}
return null;
}
/**