From 5132ae5e69fa0d02c05f789fcda4609f291b638f Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 27 May 2014 20:48:41 +0200 Subject: [PATCH] fix mount point move up if the parent no longer exists --- apps/files_sharing/lib/sharedmount.php | 82 ++++++++++++++++++------ apps/files_sharing/lib/sharedstorage.php | 59 ----------------- 2 files changed, 61 insertions(+), 80 deletions(-) diff --git a/apps/files_sharing/lib/sharedmount.php b/apps/files_sharing/lib/sharedmount.php index f1704504f6..873740f4a2 100644 --- a/apps/files_sharing/lib/sharedmount.php +++ b/apps/files_sharing/lib/sharedmount.php @@ -22,6 +22,66 @@ class SharedMount extends Mount implements MoveableMount { */ protected $storage = null; + public function __construct($storage, $mountpoint, $arguments = null, $loader = null) { + parent::__construct($storage, $mountpoint, $arguments, $loader); + + self::verifyMountPoint($arguments['share']); + } + + /** + * check if the parent folder exists otherwise move the mount point up + */ + 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 = \OCA\Files_Sharing\Helper::generateUniqueTarget( + \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), + array(), + new \OC\Files\View('/' . \OCP\User::getUser() . '/files') + ); + + if($newMountPoint !== $share['file_target']) { + self::updateFileTarget($newMountPoint, $share); + $share['file_target'] = $newMountPoint; + $share['unique_name'] = true; + } + } + + /** + * 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 && $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($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); + } + /** * Format a path to be relative to the /user/files/ directory * @@ -66,27 +126,7 @@ class SharedMount extends Mount implements MoveableMount { $relTargetPath = $this->stripUserFilesPath($target); $share = $this->storage->getShare(); - // 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->storage->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && $this->storage->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($share['item_type'], $share['item_source'], $share['item_target'], - 2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'], - $relTargetPath, $share['token'], $share['id']); - - } else { - // rename mount point - $query = \OC_DB::prepare( - 'UPDATE `*PREFIX*share` - SET `file_target` = ? - WHERE `id` = ?' - ); - $arguments = array($relTargetPath, $this->storage->getShareId()); - } - - $result = $query->execute($arguments); + $result = $this->updateFileTarget($relTargetPath, $share); if ($result) { $this->setMountPoint($target); diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index f8c241425d..1456ad1888 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -401,7 +401,6 @@ class Shared extends \OC\Files\Storage\Common { || $shares ) { foreach ($shares as $share) { - self::verifyMountPoint($share); $mount = new SharedMount( '\OC\Files\Storage\Shared', $options['user_dir'] . '/' . $share['file_target'], @@ -415,64 +414,6 @@ class Shared extends \OC\Files\Storage\Common { } } - /** - * check if the parent folder exists otherwise move the mount point up - * - * @param array $share reference to the share we want to check - */ - 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 = \OCA\Files_Sharing\Helper::generateUniqueTarget( - \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), - array(), - new \OC\Files\View('/' . \OCP\User::getUser() . '/files') - ); - - if($newMountPoint !== $share['file_target']) { - - self::updateFileTarget($newMountPoint, $share); - $share['file_target'] = $newMountPoint; - - } - } - - /** - * 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 && $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($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']); - - $this->setUniqueName(); - } 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); - } - /** * return mount point of share, relative to data/user/files *