Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-11-12 15:43:46 +0300
committerMorris Jobke <hey@morrisjobke.de>2018-11-13 16:05:13 +0300
commit0737a6fbe4548e512f9fe182c10d5a97390bd16a (patch)
treefac759a9f8027a48752fc272a57e500ec6a10d3b /lib
parent413121dea1378777b1a717a268cea906010a7c81 (diff)
Fix UniqueConstraintViolationException while insert into oc_filecache
* fixes #6160 by not being prone to the race condition in insertIfNotExists * fixes #12228 by not using a query that can result in a deadlock * replaces the insertIfNotExists call with an insert which is wrapped into a try-catch block Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Files/Cache/Cache.php25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php
index cf017c73960..947dd11b4f4 100644
--- a/lib/private/Files/Cache/Cache.php
+++ b/lib/private/Files/Cache/Cache.php
@@ -37,6 +37,7 @@
namespace OC\Files\Cache;
+use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache;
@@ -239,6 +240,8 @@ class Cache implements ICache {
*
* @return int file id
* @throws \RuntimeException
+ *
+ * @suppress SqlInjectionChecker
*/
public function insert($file, array $data) {
// normalize file
@@ -269,12 +272,20 @@ class Cache implements ICache {
return trim($item, "`");
}, $queryParts);
$values = array_combine($queryParts, $params);
- if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
- 'storage',
- 'path_hash',
- ])
- ) {
- return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+
+ try {
+ $builder = $this->connection->getQueryBuilder();
+ $builder->insert('filecache');
+
+ foreach ($values as $column => $value) {
+ $builder->setValue($column, $builder->createNamedParameter($value));
+ }
+
+ if ($builder->execute()) {
+ return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+ }
+ } catch(UniqueConstraintViolationException $e) {
+ // entry exists already
}
// The file was created in the mean time
@@ -282,7 +293,7 @@ class Cache implements ICache {
$this->update($id, $data);
return $id;
} else {
- throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
+ throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}