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

[stable9] Backport lazy init of shared storage/mount #26912

Merged
merged 4 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions apps/files_sharing/lib/sharedmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,28 @@ class SharedMount extends MountPoint implements MoveableMount {
*/
private $user;

/** @var array */
private $share;

/**
* @param string $storage
* @param SharedMount[] $mountpoints
* @param array|null $arguments
* @param \OCP\Files\Storage\IStorageFactory $loader
*/
public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
if (!isset($arguments['user'])) {
throw new \InvalidArgumentException('Missing "user" key in arguments');
}
if (!isset($arguments['share'])) {
throw new \InvalidArgumentException('Missing "share" key in arguments');
}
$this->user = $arguments['user'];
$this->recipientView = new View('/' . $this->user . '/files');
$newMountPoint = $this->verifyMountPoint($arguments['share'], $mountpoints);
$this->share = $arguments['share'];
Copy link
Member

Choose a reason for hiding this comment

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

We should check if the "share" key is present and throw an error otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

$newMountPoint = $this->verifyMountPoint($this->share, $mountpoints);
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
$arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files');
$arguments['ownerView'] = new View('/' . $this->share['uid_owner'] . '/files');
parent::__construct($storage, $absMountPoint, $arguments, $loader);
}

Expand Down Expand Up @@ -238,8 +248,24 @@ public function removeMount() {
* @return array
*/
public function getShare() {
/** @var $storage \OC\Files\Storage\Shared */
$storage = $this->getStorage();
return $storage->getShare();
return $this->share;
}

/**
* Get the file id of the root of the storage
*
* @return int
*/
public function getStorageRootId() {
return $this->share['file_source'];
}

public function getStorageNumericId() {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->select('storage')
->from('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($this->getStorageRootId())));

return $query->execute()->fetchColumn();
}
}
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/sharedstorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function init() {
$this->initialized = true;
try {
Filesystem::initMountPoints($this->share['uid_owner']);
$sourcePath = $this->ownerView->getPath($this->share['file_source']);
$sourcePath = $this->ownerView->getPath($this->share['file_source'], false);
list($this->sourceStorage, $sourceInternalPath) = $this->ownerView->resolvePath($sourcePath);
$this->sourceRootInfo = $this->sourceStorage->getCache()->get($sourceInternalPath);
} catch (\Exception $e) {
Expand Down
14 changes: 7 additions & 7 deletions lib/private/files/config/cachedmountinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ class CachedMountInfo implements ICachedMountInfo {
/**
* @var IUser
*/
private $user;
protected $user;

/**
* @var int
*/
private $storageId;
protected $storageId;

/**
* @var int
*/
private $rootId;
protected $rootId;

/**
* @var string
*/
private $mountPoint;
protected $mountPoint;

/**
* CachedMountInfo constructor.
Expand Down Expand Up @@ -88,9 +88,9 @@ public function getRootId() {
*/
public function getMountPointNode() {
// TODO injection etc
Filesystem::initMountPoints($this->user->getUID());
$userNode = \OC::$server->getUserFolder($this->user->getUID());
$nodes = $userNode->getById($this->rootId);
Filesystem::initMountPoints($this->getUser()->getUID());
$userNode = \OC::$server->getUserFolder($this->getUser()->getUID());
$nodes = $userNode->getById($this->getRootId());
if (count($nodes) > 0) {
return $nodes[0];
} else {
Expand Down
82 changes: 82 additions & 0 deletions lib/private/files/config/lazystoragemountinfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @author Robin Appelman <icewind@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Config;

use OC\Files\Filesystem;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\IUser;

class LazyStorageMountInfo extends CachedMountInfo {
/** @var IMountPoint */
private $mount;

/**
* CachedMountInfo constructor.
*
* @param IUser $user
* @param IMountPoint $mount
*/
public function __construct(IUser $user, IMountPoint $mount) {
$this->user = $user;
$this->mount = $mount;
Copy link
Member

Choose a reason for hiding this comment

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

I think we have to call the parent constructor. The parent might not be properly initialized otherwise and could cause issues.
If this is intended, write a comment explaining it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was backported from 9.1 which didn't call the parent either (not my code), I hope it had a good reason

}

/**
* @return int the numeric storage id of the mount
*/
public function getStorageId() {
if (!$this->storageId) {
if (method_exists($this->mount, 'getStorageNumericId')) {
$this->storageId = $this->mount->getStorageNumericId();
} else {
$storage = $this->mount->getStorage();
if (!$storage) {
return -1;
}
$this->storageId = $storage->getStorageCache()->getNumericId();
}
}
return parent::getStorageId();
}

/**
* @return int the fileid of the root of the mount
*/
public function getRootId() {
if (!$this->rootId) {
$this->rootId = $this->mount->getStorageRootId();
}
return parent::getRootId();
}

/**
* @return string the mount point of the mount for the user
*/
public function getMountPoint() {
if (!$this->mountPoint) {
$this->mountPoint = $this->mount->getMountPoint();
}
return parent::getMountPoint();
}
}
10 changes: 5 additions & 5 deletions lib/private/files/config/usermountcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public function registerMounts(IUser $user, array $mounts) {
});
/** @var ICachedMountInfo[] $newMounts */
$newMounts = array_map(function (IMountPoint $mount) use ($user) {
$storage = $mount->getStorage();
if ($storage->instanceOfStorage('\OC\Files\Storage\Shared')) {
$rootId = (int)$storage->getShare()['file_source'];
// note: SharedMount goes there
if (method_exists($mount, 'getStorageRootId')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary? IMountPoint has this method, if it does not exist you would either get an error for something not implementing IMountPoint correctly, or not injecting an object of type IMountPoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMountPoint only has this method on stable9.1 or master and we don't want to introduce new methods as it qualifies as API change. Other implementers of IMountPoint (ex: external storage) will likely not implement this one in this version. So this hack is there to accomodate for that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, i didn't think about that! Makes sense then!

$rootId = $mount->getStorageRootId();
} else {
$storage = $mount->getStorage();
$rootId = (int)$storage->getCache()->getId('');
}
$storageId = (int)$storage->getStorageCache()->getNumericId();
// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
if ($rootId === -1) {
return null;
} else {
return new CachedMountInfo($user, $storageId, $rootId, $mount->getMountPoint());
return new LazyStorageMountInfo($user, $mount);
}
}, $mounts);
$newMounts = array_values(array_filter($newMounts));
Expand Down
9 changes: 9 additions & 0 deletions lib/private/files/mount/mountpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,13 @@ public function getOption($name, $default) {
public function getOptions() {
return $this->mountOptions;
}

/**
* Get the file id of the root of the storage
*
* @return int
*/
public function getStorageRootId() {
return (int)$this->getStorage()->getCache()->getId('');
}
}
9 changes: 8 additions & 1 deletion lib/private/files/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use OCP\IUser;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCA\Files_Sharing\SharedMount;

/**
* Class to provide access to ownCloud filesystem via a "view", and methods for
Expand Down Expand Up @@ -1648,10 +1649,11 @@ public function getETag($path) {
* Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file
*
* @param int $id
* @param bool $includeShares whether to recurse into shared mounts
* @throws NotFoundException
* @return string
*/
public function getPath($id) {
public function getPath($id, $includeShares = true) {
$id = (int)$id;
$manager = Filesystem::getMountManager();
$mounts = $manager->findIn($this->fakeRoot);
Expand All @@ -1663,6 +1665,11 @@ public function getPath($id) {
/**
* @var \OC\Files\Mount\MountPoint $mount
*/
if (!$includeShares && $mount instanceof SharedMount) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this will be enough to prevent the infinite loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was enough on 9.1, at least for known cases

// prevent potential infinite loop when instantiating shared storages
// recursively
continue;
}
if ($mount->getStorage()) {
$cache = $mount->getStorage()->getCache();
$internalPath = $cache->getPathById($id);
Expand Down