Browse Source

move the mount point up if a share is mounted to a subfolder and the subfolder no longer exists

tags/v7.0.0alpha2
Bjoern Schiessle 10 years ago
parent
commit
5f403f3e92

+ 3
- 0
apps/files_sharing/appinfo/app.php View File

@@ -9,6 +9,7 @@ OC::$CLASSPATH['OC\Files\Cache\Shared_Updater'] = 'files_sharing/lib/updater.php
OC::$CLASSPATH['OC\Files\Cache\Shared_Watcher'] = 'files_sharing/lib/watcher.php';
OC::$CLASSPATH['OCA\Files\Share\Api'] = 'files_sharing/lib/api.php';
OC::$CLASSPATH['OCA\Files\Share\Maintainer'] = 'files_sharing/lib/maintainer.php';
OC::$CLASSPATH['OCA\Files\Share\Proxy'] = 'files_sharing/lib/proxy.php';
OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
OCP\Share::registerBackend('file', 'OC_Share_Backend_File');
OCP\Share::registerBackend('folder', 'OC_Share_Backend_Folder', 'file');
@@ -18,3 +19,5 @@ OCP\Util::addScript('files_sharing', 'share');
\OC_Hook::connect('OC_Filesystem', 'delete', '\OC\Files\Cache\Shared_Updater', 'deleteHook');
\OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Shared_Updater', 'renameHook');
\OC_Hook::connect('OC_Appconfig', 'post_set_value', '\OCA\Files\Share\Maintainer', 'configChangeHook');

OC_FileProxy::register(new OCA\Files\Share\Proxy());

+ 22
- 0
apps/files_sharing/lib/helper.php View File

@@ -180,4 +180,26 @@ class Helper {

return $relPath;
}

/**
* check if file name already exists and generate unique target
*
* @param string $path
* @param array $excludeList
* @param \OC\Files\View $view
* @return string $path
*/
public static function generateUniqueTarget($path, $excludeList, $view) {
$pathinfo = pathinfo($path);
$ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
$name = $pathinfo['filename'];
$dir = $pathinfo['dirname'];
$i = 2;
while ($view->file_exists($path) || in_array($path, $excludeList)) {
$path = \OC\Files\Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
$i++;
}

return $path;
}
}

+ 69
- 0
apps/files_sharing/lib/proxy.php View File

@@ -0,0 +1,69 @@
<?php
/**
* ownCloud
*
* @author Bjoern Schiessle
* @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Files\Share;

class Proxy extends \OC_FileProxy {

/**
* check if the deleted folder contains share mount points and move them
* up to the parent
*
* @param string $path
*/
public function preUnlink($path) {
$this->moveMountPointsUp($path);
}

/**
* check if the deleted folder contains share mount points and move them
* up to the parent
*
* @param string $path
*/
public function preRmdir($path) {
$this->moveMountPointsUp($path);
}

/**
* move share mount points up to the parent
*
* @param string $path
*/
private function moveMountPointsUp($path) {
$view = new \OC\Files\View('/');

// find share mount points within $path and move them up to the parent folder
// before we delete $path
$mountManager = \OC\Files\Filesystem::getMountManager();
$mountedShares = $mountManager->findIn($path);
foreach ($mountedShares as $mount) {
if ($mount->getStorage() instanceof \OC\Files\Storage\Shared) {
$mountPoint = $mount->getMountPoint();
$mountPointName = $mount->getMountPointName();
$target = \OCA\Files_Sharing\Helper::generateUniqueTarget(dirname($path) . '/' . $mountPointName, array(), $view);
$view->rename($mountPoint, $target);
}
}
}

}

+ 1
- 10
apps/files_sharing/lib/share/file.php View File

@@ -75,16 +75,7 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent {
$excludeList = array_merge($excludeList, $exclude);
}

$pathinfo = pathinfo($target);
$ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
$name = $pathinfo['filename'];
$i = 2;
while ($view->file_exists($target) || in_array($target, $excludeList)) {
$target = '/' . $name . ' ('.$i.')' . $ext;
$i++;
}

return $target;
return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view);
}

public function formatItems($items, $format, $parameters = null) {

+ 68
- 45
apps/files_sharing/lib/sharedstorage.php View File

@@ -305,27 +305,12 @@ class Shared extends \OC\Files\Storage\Common {

$relTargetPath = $this->stripUserFilesPath($targetPath);

// if the user renames a mount point from a group share we need to create a new db entry
// for the unique name
if ($this->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && $this->uniqueNameSet() === false) {
$query = \OC_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($this->share['item_type'], $this->share['item_source'], $this->share['item_target'],
2, \OCP\User::getUser(), $this->share['uid_owner'], $this->share['permissions'], $this->share['stime'], $this->share['file_source'],
$relTargetPath, $this->share['token'], $this->share['id']);

} else {
// rename mount point
$query = \OC_DB::prepare(
'Update `*PREFIX*share`
SET `file_target` = ?
WHERE `id` = ?'
);
$arguments = array($relTargetPath, $this->getShareId());
if ($relTargetPath === false) {
\OCP\Util::writeLog('file sharing', 'Wrong target path given: ' . $targetPath, \OCP\Util::ERROR);
return false;
}

$result = $query->execute($arguments);
$result = self::updateFileTarget($relTargetPath, $this->share);

if ($result) {
// update the mount manager with the new paths
@@ -343,9 +328,39 @@ class Shared extends \OC\Files\Storage\Common {
\OCP\Util::ERROR);
}

return $result;
return (bool)$result;
}

/**
* @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 type
*/
private static 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 &&
(isset($share['unique_name']) && $share['unique_name'])) {
$query = \OC_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, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
$newPath, $share['token'], $share['id']);

} else {
// rename mount point
$query = \OC_DB::prepare(
'Update `*PREFIX*share`
SET `file_target` = ?
WHERE `id` = ?'
);
$arguments = array($newPath, $share['id']);
}

return $query->execute($arguments);
}

public function rename($path1, $path2) {

@@ -471,6 +486,7 @@ class Shared extends \OC\Files\Storage\Common {
|| $shares
) {
foreach ($shares as $share) {
self::verifyMountPoint($share);
\OC\Files\Filesystem::mount('\OC\Files\Storage\Shared',
array(
'share' => $share,
@@ -481,50 +497,57 @@ class Shared extends \OC\Files\Storage\Common {
}

/**
* return mount point of share, relative to data/user/files
* @return string
* check if the parent folder exists otherwise move the mount point up
*
* @param array $share reference to the share we want to check
*/
public function getMountPoint() {
return $this->share['file_target'];
private static function verifyMountPoint(&$share) {
$mountPoint = basename($share['file_target']);
$parent = dirname($share['file_target']);

while (!\OC\Files\Filesystem::is_dir($parent)) {
$parent = dirname($parent);
}

$newMountPoint = \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint);

if($newMountPoint !== $share['file_target']) {
$newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
$newMountPoint,
array(),
new \OC\Files\View('/' . \OCP\User::getUser() . '/files')
);
self::updateFileTarget($newMountPoint, $share);
$share['file_target'] = $newMountPoint;

}
}

/**
* get share type
* @return integer can be single user share (0) group share (1), unique group share name (2)
* return mount point of share, relative to data/user/files
*
* @return string
*/
private function getShareType() {
return $this->share['share_type'];
public function getMountPoint() {
return $this->share['file_target'];
}

private function setMountPoint($path) {
$this->share['file_target'] = $path;
}

/**
* does the group share already has a user specific unique name
* @return bool
*/
private function uniqueNameSet() {
return (isset($this->share['unique_name']) && $this->share['unique_name']);
}

/**
* the share now uses a unique name of this user
*
* @brief the share now uses a unique name of this user
*/
private function setUniqueName() {
$this->share['unique_name'] = true;
}

/**
* get share ID
* @return integer unique share ID
*/
private function getShareId() {
return $this->share['id'];
}

/**
* get the user who shared the file
* @brief get the user who shared the file
*
* @return string
*/
public function getSharedFrom() {

+ 6
- 3
apps/files_sharing/lib/updater.php View File

@@ -115,11 +115,14 @@ class Shared_Updater {
* @param array $params
*/
static public function deleteHook($params) {
self::correctFolders($params['path']);
$fileInfo = \OC\Files\Filesystem::getFileInfo($params['path']);
$path = $params['path'];
self::correctFolders($path);

$fileInfo = \OC\Files\Filesystem::getFileInfo($path);

// mark file as deleted so that we can clean up the share table if
// the file was deleted successfully
self::$toRemove[$params['path']] = $fileInfo['fileid'];
self::$toRemove[$path] = $fileInfo['fileid'];
}

/**

+ 98
- 0
apps/files_sharing/tests/proxy.php View File

@@ -0,0 +1,98 @@
<?php
/**
* ownCloud
*
* @author Bjoern Schiessle
* @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

require_once __DIR__ . '/base.php';

use OCA\Files\Share;

/**
* Class Test_Files_Sharing_Api
*/
class Test_Files_Sharing_Api extends Test_Files_Sharing_Base {

const TEST_FOLDER_NAME = '/folder_share_api_test';

private static $tempStorage;

function setUp() {
parent::setUp();

// load proxies
OC::$CLASSPATH['OCA\Files\Share\Proxy'] = 'files_sharing/lib/proxy.php';
OC_FileProxy::register(new OCA\Files\Share\Proxy());

$this->folder = self::TEST_FOLDER_NAME;
$this->subfolder = '/subfolder_share_api_test';
$this->subsubfolder = '/subsubfolder_share_api_test';

$this->filename = '/share-api-test';

// save file with content
$this->view->file_put_contents($this->filename, $this->data);
$this->view->mkdir($this->folder);
$this->view->mkdir($this->folder . $this->subfolder);
$this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
$this->view->file_put_contents($this->folder.$this->filename, $this->data);
$this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
}

function tearDown() {
$this->view->unlink($this->filename);
$this->view->deleteAll($this->folder);

self::$tempStorage = null;

parent::tearDown();
}

/**
* @medium
*/
function testpreUnlink() {

$fileInfo1 = \OC\Files\Filesystem::getFileInfo($this->filename);
$fileInfo2 = \OC\Files\Filesystem::getFileInfo($this->folder);

$result = \OCP\Share::shareItem('file', $fileInfo1->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);
$this->assertTrue($result);

$result = \OCP\Share::shareItem('folder', $fileInfo2->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);
$this->assertTrue($result);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

// move shared folder to 'localDir' and rename it, so that it uses the same
// name as the shared file
\OC\Files\Filesystem::mkdir('localDir');
$result = \OC\Files\Filesystem::rename($this->folder, '/localDir/' . $this->filename);
$this->assertTrue($result);

\OC\Files\Filesystem::unlink('localDir');

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

// after we deleted 'localDir' the share should be moved up to the root and be
// renamed to "filename (2)"
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
$this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . ' (2)' ));
}
}

+ 46
- 0
apps/files_sharing/tests/sharedstorage.php View File

@@ -46,6 +46,48 @@ class Test_Files_Sharing_Storage extends Test_Files_Sharing_Base {
parent::tearDown();
}

/**
* @medium
*/
function testDeleteParentOfMountPoint() {

// share to user
$fileinfo = $this->view->getFileInfo($this->folder);
$result = \OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
self::TEST_FILES_SHARING_API_USER2, 31);

$this->assertTrue($result);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
$this->assertTrue($user2View->file_exists($this->folder));

// create a local folder
$result = $user2View->mkdir('localfolder');
$this->assertTrue($result);

// move mount point to local folder
$result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
$this->assertTrue($result);

// mount point in the root folder should no longer exist
$this->assertFalse($user2View->is_dir($this->folder));

// delete the local folder
$result = $user2View->unlink('/localfolder');
$this->assertTrue($result);

//enforce reload of the mount points
self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

//mount point should be back at the root
$this->assertTrue($user2View->is_dir($this->folder));

//cleanup
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$this->view->unlink($this->folder);
}

/**
* @medium
*/
@@ -79,5 +121,9 @@ class Test_Files_Sharing_Storage extends Test_Files_Sharing_Base {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$this->assertTrue($this->view->file_exists( $this->folder. '/foo.txt'));

//cleanup
$this->view->unlink($this->folder);
}

}

+ 108
- 20
apps/files_sharing/tests/updater.php View File

@@ -21,47 +21,103 @@
*/

require_once __DIR__ . '/../appinfo/update.php';
require_once __DIR__ . '/base.php';

/**
* Class Test_Files_Sharing_Updater
*/
class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase {
class Test_Files_Sharing_Updater extends Test_Files_Sharing_Base {

const TEST_FOLDER_NAME = '/folder_share_api_test';

function setUp() {
// some previous tests didn't clean up and therefore this has to be done here
// FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there
$this->tearDown();
parent::setUp();

// add items except one - because this is the test case for the broken share table
$addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' .
'`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' .
'VALUES (1, ?, 1, 1, 1, 1, 1, 1)');
$items = array(1, 3);
$fileIds = array();
foreach($items as $item) {
// the number is used as path_hash
$addItems->execute(array($item));
$fileIds[] = \OC_DB::insertId('*PREFIX*filecache');
}
$this->folder = self::TEST_FOLDER_NAME;

$addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)');
// the number is used as item_source
$addShares->execute(array($fileIds[0]));
$addShares->execute(array(200)); // id of "deleted" file
$addShares->execute(array($fileIds[1]));
$this->filename = '/share-api-test.txt';

// save file with content
$this->view->file_put_contents($this->filename, $this->data);
$this->view->mkdir($this->folder);
$this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data);
}

function tearDown() {
$this->view->unlink($this->filename);
$this->view->deleteAll($this->folder);

$removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`');
$removeShares->execute();
$removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`');
$removeItems->execute();

parent::tearDown();
}

/**
* test deletion of a folder which contains share mount points. Share mount
* points should move up to the parent before the folder gets deleted so
* that the mount point doesn't end up at the trash bin
*/
function testDeleteParentFolder() {
$status = \OC_App::isEnabled('files_trashbin');
\OC_App::enable('files_trashbin');

\OCA\Files_Trashbin\Trashbin::registerHooks();
OC_FileProxy::register(new OCA\Files\Share\Proxy());

$fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
$this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);

\OCP\Share::shareItem('folder', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);

$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
$view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');

// check if user2 can see the shared folder
$this->assertTrue($view->file_exists($this->folder));

$view->mkdir("localFolder");
$view->file_put_contents("localFolder/localFile.txt", "local file");

$view->rename($this->folder, 'localFolder/' . $this->folder);

// share mount point should now be moved to the subfolder
$this->assertFalse($view->file_exists($this->folder));
$this->assertTrue($view->file_exists('localFolder/' .$this->folder));

$view->unlink('localFolder');

$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);

// mount point should move up again
$this->assertTrue($view->file_exists($this->folder));

// trashbin should contain the local file but not the mount point
$rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
$dirContent = $rootView->getDirectoryContent('files_trashbin/files');
$this->assertSame(1, count($dirContent));
$firstElement = reset($dirContent);
$ext = pathinfo($firstElement['path'], PATHINFO_EXTENSION);
$this->assertTrue($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/localFile.txt'));
$this->assertFalse($rootView->file_exists('files_trashbin/files/localFolder.' . $ext . '/' . $this->folder));

//cleanup
$rootView->deleteAll('files_trashin');

if ($status === false) {
\OC_App::disable('files_trashbin');
}
}

/**
* @medium
*/
function testRemoveBrokenShares() {

$this->prepareFileCache();

// check if there are just 3 shares (see setUp - precondition: empty table)
$countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`');
$result = $countShares->execute()->fetchOne();
@@ -114,6 +170,7 @@ class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase {
}
}

// cleanup
$this->cleanupSharedTable();

}
@@ -123,6 +180,9 @@ class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase {
$query->execute();
}

/**
* prepare sharing table for testRemoveSharedFolder()
*/
private function prepareDB() {
$this->cleanupSharedTable();
// add items except one - because this is the test case for the broken share table
@@ -143,4 +203,32 @@ class Test_Files_Sharing_Updater extends \PHPUnit_Framework_TestCase {
$addItems->execute($item);
}
}

/**
* prepare file cache for testRemoveBrokenShares()
*/
private function prepareFileCache() {
// some previous tests didn't clean up and therefore this has to be done here
// FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there
$this->tearDown();

// add items except one - because this is the test case for the broken share table
$addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' .
'`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' .
'VALUES (1, ?, 1, 1, 1, 1, 1, 1)');
$items = array(1, 3);
$fileIds = array();
foreach($items as $item) {
// the number is used as path_hash
$addItems->execute(array($item));
$fileIds[] = \OC_DB::insertId('*PREFIX*filecache');
}

$addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)');
// the number is used as item_source
$addShares->execute(array($fileIds[0]));
$addShares->execute(array(200)); // id of "deleted" file
$addShares->execute(array($fileIds[1]));
}

}

+ 11
- 0
lib/private/files/mount/mount.php View File

@@ -59,12 +59,23 @@ class Mount {
}

/**
* get complete path to the mount point, relative to data/
*
* @return string
*/
public function getMountPoint() {
return $this->mountPoint;
}

/**
* get name of the mount point
*
* @return string
*/
public function getMountPointName() {
return basename(rtrim($this->mountPoint, '/'));
}

/**
* @param string $mountPoint new mount point
*/

Loading…
Cancel
Save