Skip to content

Commit

Permalink
Move SharedStorage et al over to the shareManager and jail
Browse files Browse the repository at this point in the history
  • Loading branch information
rullzer committed Apr 19, 2016
1 parent 3f95d44 commit 6123bad
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 456 deletions.
3 changes: 2 additions & 1 deletion apps/files_sharing/appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public function __construct(array $urlParams = array()) {
/** @var \OCP\IServerContainer $server */
$server = $c->query('ServerContainer');
return new MountProvider(
$server->getConfig()
$server->getConfig(),
$server->getShareManager()
);
});

Expand Down
4 changes: 4 additions & 0 deletions apps/files_sharing/lib/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,8 @@ protected function formatCacheEntry($entry) {
public function clear() {
// Not a valid action for Shared Cache
}

public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
parent::moveFromCache($sourceCache, $sourcePath, $this->storage->getSourcePath($targetPath));
}
}
25 changes: 17 additions & 8 deletions apps/files_sharing/lib/mountprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@

namespace OCA\Files_Sharing;

use OC\Files\Filesystem;
use OC\User\NoUserException;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\Share\IManager;

class MountProvider implements IMountProvider {
/**
* @var \OCP\IConfig
*/
protected $config;

/**
* @var IManager
*/
protected $shareManager;

/**
* @param \OCP\IConfig $config
* @param IManager $shareManager
*/
public function __construct(IConfig $config) {
public function __construct(IConfig $config, IManager $shareManager) {
$this->config = $config;
$this->shareManager = $shareManager;
}


Expand All @@ -51,18 +57,21 @@ public function __construct(IConfig $config) {
* @return \OCP\Files\Mount\IMountPoint[]
*/
public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
$shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID());
$shares = array_filter($shares, function ($share) {
return $share['permissions'] > 0;
$shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1);
$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1));
$shares = array_filter($shares, function (\OCP\Share\IShare $share) {
return $share->getPermissions() > 0;
});

$mounts = [];
foreach ($shares as $share) {

$mounts[] = new SharedMount(
'\OC\Files\Storage\Shared',
$mounts,
[
'share' => $share,
'user' => $user->getUID()
'user' => $user->getUID(),
'newShare' => $share,
],
$storageFactory
);
Expand Down
5 changes: 4 additions & 1 deletion apps/files_sharing/lib/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class SharedScanner extends Scanner {
*/
public function getData($path) {
$data = parent::getData($path);
$sourcePath = $this->storage->getSourcePath($path);
if ($data === null) {
return null;
}
$sourcePath = '/' . $this->storage->getOwner($path) . '/' . $this->storage->getSourcePath($path);
list($sourceStorage, $internalPath) = \OC\Files\Filesystem::resolvePath($sourcePath);
$data['permissions'] = $sourceStorage->getPermissions($internalPath);
return $data;
Expand Down
82 changes: 27 additions & 55 deletions apps/files_sharing/lib/sharedmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,25 @@ class SharedMount extends MountPoint implements MoveableMount {
public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
$this->user = $arguments['user'];
$this->recipientView = new View('/' . $this->user . '/files');
$newMountPoint = $this->verifyMountPoint($arguments['share'], $mountpoints);
/** @var \OCP\Share\IShare $share */
$share = $arguments['newShare'];
$newMountPoint = $this->verifyMountPoint($share, $mountpoints);
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
$arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files');
$arguments['ownerView'] = new View('/' . $share->getShareOwner() . '/files');
parent::__construct($storage, $absMountPoint, $arguments, $loader);
}

/**
* check if the parent folder exists otherwise move the mount point up
*
* @param array $share
* @param \OCP\Share\IShare $share
* @param SharedMount[] $mountpoints
* @return string
*/
private function verifyMountPoint(&$share, array $mountpoints) {
private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints) {

$mountPoint = basename($share['file_target']);
$parent = dirname($share['file_target']);
$mountPoint = basename($share->getTarget());
$parent = dirname($share->getTarget());

if (!$this->recipientView->is_dir($parent)) {
$parent = Helper::getShareFolder();
Expand All @@ -86,15 +88,26 @@ private function verifyMountPoint(&$share, array $mountpoints) {
$mountpoints
);

if ($newMountPoint !== $share['file_target']) {
if ($newMountPoint !== $share->getTarget()) {
$this->updateFileTarget($newMountPoint, $share);
$share['file_target'] = $newMountPoint;
$share['unique_name'] = true;
}

return $newMountPoint;
}

/**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath
* @param \OCP\Share\IShare $share
* @return bool
*/
private function updateFileTarget($newPath, &$share) {
$share->setTarget($newPath);
\OC::$server->getShareManager()->moveShare($share, $this->user);
}


/**
* @param string $path
* @param View $view
Expand All @@ -110,7 +123,7 @@ private function generateUniqueTarget($path, $view, array $mountpoints) {
// Helper function to find existing mount points
$mountpointExists = function($path) use ($mountpoints) {
foreach ($mountpoints as $mountpoint) {
if ($mountpoint->getShare()['file_target'] === $path) {
if ($mountpoint->getShare()->getTarget() === $path) {
return true;
}
}
Expand All @@ -126,38 +139,6 @@ private function generateUniqueTarget($path, $view, array $mountpoints) {
return $path;
}

/**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath
* @param array $share reference to the share which should be modified
* @return bool
*/
private function updateFileTarget($newPath, &$share) {
// if the user renames a mount point from a group share we need to create a new db entry
// for the unique name
if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
.' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
.' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
$arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
2, $this->user, $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
$newPath, $share['token'], $share['id']);
} else {
// rename mount point
$query = \OCP\DB::prepare(
'Update `*PREFIX*share`
SET `file_target` = ?
WHERE `id` = ?'
);
$arguments = array($newPath, $share['id']);
}

$result = $query->execute($arguments);

return $result === 1 ? true : false;
}

/**
* Format a path to be relative to the /user/files/ directory
*
Expand Down Expand Up @@ -197,20 +178,11 @@ public function moveMount($target) {

$result = true;

if (!empty($share['grouped'])) {
foreach ($share['grouped'] as $s) {
$result = $this->updateFileTarget($relTargetPath, $s) && $result;
}
} else {
$result = $this->updateFileTarget($relTargetPath, $share) && $result;
}

if ($result) {
try {
$this->updateFileTarget($relTargetPath, $share);
$this->setMountPoint($target);
$this->storage->setUniqueName();
$this->storage->setMountPoint($relTargetPath);

} else {
} catch (\Exception $e) {
\OCP\Util::writeLog('file sharing',
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
\OCP\Util::ERROR);
Expand All @@ -235,7 +207,7 @@ public function removeMount() {
}

/**
* @return array
* @return \OCP\Share\IShare
*/
public function getShare() {
/** @var $storage \OC\Files\Storage\Shared */
Expand Down
3 changes: 1 addition & 2 deletions apps/files_sharing/lib/sharedpropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class SharedPropagator extends Propagator {
* @return \array[] all propagated entries
*/
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
$source = $this->storage->getSourcePath($internalPath);
/** @var \OC\Files\Storage\Storage $storage */
list($storage, $sourceInternalPath) = \OC\Files\Filesystem::resolvePath($source);
list($storage, $sourceInternalPath) = $this->storage->resolvePath($internalPath);
return $storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
}
}
Loading

0 comments on commit 6123bad

Please sign in to comment.