Skip to content

Commit 204466c

Browse files
authored
Merge pull request #12434 from nextcloud/backport/12411-12413/unique-constraint-fix-13
[stable13] Unique contraint and deadlock fixes for filecache and file_locks
2 parents 413121d + 243516d commit 204466c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/private/Files/Cache/Cache.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
namespace OC\Files\Cache;
3939

40+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
4041
use OCP\DB\QueryBuilder\IQueryBuilder;
4142
use Doctrine\DBAL\Driver\Statement;
4243
use OCP\Files\Cache\ICache;
@@ -239,6 +240,8 @@ public function put($file, array $data) {
239240
*
240241
* @return int file id
241242
* @throws \RuntimeException
243+
*
244+
* @suppress SqlInjectionChecker
242245
*/
243246
public function insert($file, array $data) {
244247
// normalize file
@@ -269,20 +272,28 @@ public function insert($file, array $data) {
269272
return trim($item, "`");
270273
}, $queryParts);
271274
$values = array_combine($queryParts, $params);
272-
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
273-
'storage',
274-
'path_hash',
275-
])
276-
) {
277-
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
275+
276+
try {
277+
$builder = $this->connection->getQueryBuilder();
278+
$builder->insert('filecache');
279+
280+
foreach ($values as $column => $value) {
281+
$builder->setValue($column, $builder->createNamedParameter($value));
282+
}
283+
284+
if ($builder->execute()) {
285+
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
286+
}
287+
} catch(UniqueConstraintViolationException $e) {
288+
// entry exists already
278289
}
279290

280291
// The file was created in the mean time
281292
if (($id = $this->getId($file)) > -1) {
282293
$this->update($id, $data);
283294
return $id;
284295
} else {
285-
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.');
296+
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.');
286297
}
287298
}
288299

lib/private/Lock/DBLockingProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace OC\Lock;
2828

29+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
2930
use OC\DB\QueryBuilder\Literal;
3031
use OCP\AppFramework\Utility\ITimeFactory;
3132
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -116,7 +117,17 @@ public function __construct(IDBConnection $connection, ILogger $logger, ITimeFac
116117

117118
protected function initLockField($path, $lock = 0) {
118119
$expire = $this->getExpireTime();
119-
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
120+
121+
try {
122+
$builder = $this->connection->getQueryBuilder();
123+
return $builder->insert('file_locks')
124+
->setValue('key', $builder->createNamedParameter($path))
125+
->setValue('lock', $builder->createNamedParameter($lock))
126+
->setValue('ttl', $builder->createNamedParameter($expire))
127+
->execute();
128+
} catch(UniqueConstraintViolationException $e) {
129+
return 0;
130+
}
120131
}
121132

122133
/**

0 commit comments

Comments
 (0)