Skip to content

Commit

Permalink
Merge pull request #12433 from nextcloud/backport/12411-12413/unique-…
Browse files Browse the repository at this point in the history
…constraint-fix

[stable14] Unique contraint and deadlock fixes for filecache and file_locks
  • Loading branch information
MorrisJobke authored Nov 13, 2018
2 parents 655b7f0 + ec5c11d commit 40f5f12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
25 changes: 18 additions & 7 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -238,6 +239,8 @@ public function put($file, array $data) {
*
* @return int file id
* @throws \RuntimeException
*
* @suppress SqlInjectionChecker
*/
public function insert($file, array $data) {
// normalize file
Expand Down Expand Up @@ -268,20 +271,28 @@ public function insert($file, array $data) {
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
if (($id = $this->getId($file)) > -1) {
$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.');
}
}

Expand Down
13 changes: 12 additions & 1 deletion lib/private/Lock/DBLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OC\Lock;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\DB\QueryBuilder\Literal;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand Down Expand Up @@ -133,7 +134,17 @@ public function __construct(

protected function initLockField(string $path, int $lock = 0): int {
$expire = $this->getExpireTime();
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);

try {
$builder = $this->connection->getQueryBuilder();
return $builder->insert('file_locks')
->setValue('key', $builder->createNamedParameter($path))
->setValue('lock', $builder->createNamedParameter($lock))
->setValue('ttl', $builder->createNamedParameter($expire))
->execute();
} catch(UniqueConstraintViolationException $e) {
return 0;
}
}

/**
Expand Down

0 comments on commit 40f5f12

Please sign in to comment.