diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/files/cache/cache.php | 18 | ||||
-rw-r--r-- | lib/private/files/cache/updater.php | 14 | ||||
-rw-r--r-- | lib/private/files/storage/wrapper/availability.php | 3 | ||||
-rw-r--r-- | lib/private/repair.php | 13 | ||||
-rw-r--r-- | lib/private/share/mailnotifications.php | 10 | ||||
-rw-r--r-- | lib/repair/oldgroupmembershipshares.php | 117 | ||||
-rw-r--r-- | lib/repair/repairinvalidshares.php | 32 |
7 files changed, 199 insertions, 8 deletions
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index f3e22701f40..71720ac58bf 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -313,6 +313,14 @@ class Cache { $fields = array( 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions'); + + $doNotCopyStorageMTime = false; + if (array_key_exists('mtime', $data) && $data['mtime'] === null) { + // this horrific magic tells it to not copy storage_mtime to mtime + unset($data['mtime']); + $doNotCopyStorageMTime = true; + } + $params = array(); $queryParts = array(); foreach ($data as $name => $value) { @@ -325,7 +333,7 @@ class Cache { $queryParts[] = '`mimepart`'; $value = $this->mimetypeLoader->getId($value); } elseif ($name === 'storage_mtime') { - if (!isset($data['mtime'])) { + if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { $params[] = $value; $queryParts[] = '`mtime`'; } @@ -477,6 +485,9 @@ class Cache { list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); + // sql for final update + $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; + if ($sourceData['mimetype'] === 'httpd/unix-directory') { //find all child entries $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; @@ -490,11 +501,12 @@ class Cache { $newTargetPath = $targetPath . substr($child['path'], $sourceLength); \OC_DB::executeAudited($query, [$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); } + \OC_DB::executeAudited($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); \OC_DB::commit(); + } else { + \OC_DB::executeAudited($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); } - $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; - \OC_DB::executeAudited($sql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); } /** diff --git a/lib/private/files/cache/updater.php b/lib/private/files/cache/updater.php index 4cc5f511565..85dcc8589de 100644 --- a/lib/private/files/cache/updater.php +++ b/lib/private/files/cache/updater.php @@ -194,12 +194,26 @@ class Updater { $targetCache->correctFolderSize($targetInternalPath); $this->correctParentStorageMtime($sourceStorage, $sourceInternalPath); $this->correctParentStorageMtime($targetStorage, $targetInternalPath); + $this->updateStorageMTimeOnly($targetStorage, $targetInternalPath); $this->propagator->addChange($source); $this->propagator->addChange($target); $this->propagator->propagateChanges(); } } + private function updateStorageMTimeOnly($storage, $internalPath) { + $cache = $storage->getCache(); + $fileId = $cache->getId($internalPath); + if ($fileId !== -1) { + $cache->update( + $fileId, [ + 'mtime' => null, // this magic tells it to not overwrite mtime + 'storage_mtime' => $storage->filemtime($internalPath) + ] + ); + } + } + /** * update the storage_mtime of the direct parent in the cache to the mtime from the storage * diff --git a/lib/private/files/storage/wrapper/availability.php b/lib/private/files/storage/wrapper/availability.php index 37319a8f7d1..d6ce78f6e44 100644 --- a/lib/private/files/storage/wrapper/availability.php +++ b/lib/private/files/storage/wrapper/availability.php @@ -220,6 +220,9 @@ class Availability extends Wrapper { /** {@inheritdoc} */ public function file_exists($path) { + if ($path === '') { + return true; + } $this->checkAvailability(); try { return parent::file_exists($path); diff --git a/lib/private/repair.php b/lib/private/repair.php index 20219e313fd..f6ac7ebe65b 100644 --- a/lib/private/repair.php +++ b/lib/private/repair.php @@ -34,6 +34,7 @@ use OC\Repair\AssetCache; use OC\Repair\CleanTags; use OC\Repair\Collation; use OC\Repair\DropOldJobs; +use OC\Repair\OldGroupMembershipShares; use OC\Repair\RemoveGetETagEntries; use OC\Repair\SqliteAutoincrement; use OC\Repair\DropOldTables; @@ -119,6 +120,18 @@ class Repair extends BasicEmitter { } /** + * Returns expensive repair steps to be run on the + * command line with a special option. + * + * @return array of RepairStep instances + */ + public static function getExpensiveRepairSteps() { + return [ + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), + ]; + } + + /** * Returns the repair steps to be run before an * upgrade. * diff --git a/lib/private/share/mailnotifications.php b/lib/private/share/mailnotifications.php index 8056260bf17..2797e5ed99b 100644 --- a/lib/private/share/mailnotifications.php +++ b/lib/private/share/mailnotifications.php @@ -136,7 +136,7 @@ class MailNotifications { $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); - list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal'); // send it out now try { @@ -210,20 +210,20 @@ class MailNotifications { * @param string $filename the shared file * @param string $link link to the shared file * @param int $expiration expiration date (timestamp) + * @param bool $prefix prefix of mail template files * @return array an array of the html mail body and the plain text mail body */ - private function createMailBody($filename, $link, $expiration) { - + private function createMailBody($filename, $link, $expiration, $prefix = '') { $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; - $html = new \OC_Template("core", "mail", ""); + $html = new \OC_Template('core', $prefix . 'mail', ''); $html->assign ('link', $link); $html->assign ('user_displayname', $this->senderDisplayName); $html->assign ('filename', $filename); $html->assign('expiration', $formattedDate); $htmlMail = $html->fetchPage(); - $plainText = new \OC_Template("core", "altmail", ""); + $plainText = new \OC_Template('core', $prefix . 'altmail', ''); $plainText->assign ('link', $link); $plainText->assign ('user_displayname', $this->senderDisplayName); $plainText->assign ('filename', $filename); diff --git a/lib/repair/oldgroupmembershipshares.php b/lib/repair/oldgroupmembershipshares.php new file mode 100644 index 00000000000..2d701ac9fb7 --- /dev/null +++ b/lib/repair/oldgroupmembershipshares.php @@ -0,0 +1,117 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, 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\Repair; + + +use OC\Hooks\BasicEmitter; +use OC\RepairStep; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\Share; + +class OldGroupMembershipShares extends BasicEmitter implements RepairStep { + + /** @var \OCP\IDBConnection */ + protected $connection; + + /** @var \OCP\IGroupManager */ + protected $groupManager; + + /** + * @var array [gid => [uid => (bool)]] + */ + protected $memberships; + + /** + * @param IDBConnection $connection + * @param IGroupManager $groupManager + */ + public function __construct(IDBConnection $connection, IGroupManager $groupManager) { + $this->connection = $connection; + $this->groupManager = $groupManager; + } + + /** + * Returns the step's name + * + * @return string + */ + public function getName() { + return 'Remove shares of old group memberships'; + } + + /** + * Run repair step. + * Must throw exception on error. + * + * @throws \Exception in case of failure + */ + public function run() { + $deletedEntries = 0; + + $query = $this->connection->getQueryBuilder(); + $query->select(['s1.id', $query->createFunction('s1.`share_with` AS `user`'), $query->createFunction('s2.`share_with` AS `group`')]) + ->from('share', 's1') + ->where($query->expr()->isNotNull('s1.parent')) + // \OC\Share\Constant::$shareTypeGroupUserUnique === 2 + ->andWhere($query->expr()->eq('s1.share_type', $query->expr()->literal(2))) + ->andWhere($query->expr()->isNotNull('s2.id')) + ->andWhere($query->expr()->eq('s2.share_type', $query->expr()->literal(Share::SHARE_TYPE_GROUP))) + ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')); + + $deleteQuery = $this->connection->getQueryBuilder(); + $deleteQuery->delete('share') + ->where($query->expr()->eq('id', $deleteQuery->createParameter('share'))); + + $result = $query->execute(); + while ($row = $result->fetch()) { + if (!$this->isMember($row['group'], $row['user'])) { + $deletedEntries += $deleteQuery->setParameter('share', (int) $row['id']) + ->execute(); + } + } + $result->closeCursor(); + + if ($deletedEntries) { + $this->emit('\OC\Repair', 'info', array('Removed ' . $deletedEntries . ' shares where user is not a member of the group anymore')); + } + } + + /** + * @param string $gid + * @param string $uid + * @return bool + */ + protected function isMember($gid, $uid) { + if (isset($this->memberships[$gid][$uid])) { + return $this->memberships[$gid][$uid]; + } + + $isMember = $this->groupManager->isInGroup($uid, $gid); + if (!isset($this->memberships[$gid])) { + $this->memberships[$gid] = []; + } + $this->memberships[$gid][$uid] = $isMember; + + return $isMember; + } +} diff --git a/lib/repair/repairinvalidshares.php b/lib/repair/repairinvalidshares.php index 4b0aeb70c12..5a4cb445ce9 100644 --- a/lib/repair/repairinvalidshares.php +++ b/lib/repair/repairinvalidshares.php @@ -70,11 +70,43 @@ class RepairInvalidShares extends BasicEmitter implements \OC\RepairStep { } } + /** + * Remove shares where the parent share does not exist anymore + */ + private function removeSharesNonExistingParent() { + $deletedEntries = 0; + + $query = $this->connection->getQueryBuilder(); + $query->select('s1.parent') + ->from('share', 's1') + ->where($query->expr()->isNotNull('s1.parent')) + ->andWhere($query->expr()->isNull('s2.id')) + ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')) + ->groupBy('s1.parent'); + + $deleteQuery = $this->connection->getQueryBuilder(); + $deleteQuery->delete('share') + ->where($query->expr()->eq('parent', $deleteQuery->createParameter('parent'))); + + $result = $query->execute(); + while ($row = $result->fetch()) { + $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent']) + ->execute(); + } + $result->closeCursor(); + + if ($deletedEntries) { + $this->emit('\OC\Repair', 'info', array('Removed ' . $deletedEntries . ' shares where the parent did not exist')); + } + } + public function run() { $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); if (version_compare($ocVersionFromBeforeUpdate, '8.2.0.7', '<')) { // this situation was only possible before 8.2 $this->removeExpirationDateFromNonLinkShares(); } + + $this->removeSharesNonExistingParent(); } } |