/** * @param string $path * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @throws \OCP\Lock\LockedException */ public function acquireLock($path, $type) { if ($this->connection->inTransaction()) { $this->logger->warning("Trying to acquire a lock for '{$path}' while inside a transition"); } $this->connection->beginTransaction(); $this->initLockField($path); if ($type === self::LOCK_SHARED) { $result = $this->connection->executeUpdate('UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1 WHERE `key` = ? AND `lock` >= 0', [$path]); } else { $result = $this->connection->executeUpdate('UPDATE `*PREFIX*file_locks` SET `lock` = -1 WHERE `key` = ? AND `lock` = 0', [$path]); } $this->connection->commit(); if ($result !== 1) { throw new LockedException($path); } $this->markAcquire($path, $type); }
/** * Check if a transaction is active * * @return bool */ public function inTransaction() { return $this->connection->inTransaction(); }