LockingController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Testing\Controller;
  24. use OC\Lock\DBLockingProvider;
  25. use OC\User\NoUserException;
  26. use OCA\Testing\Locking\FakeDBLockingProvider;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCS\OCSException;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\IConfig;
  34. use OCP\IDBConnection;
  35. use OCP\IRequest;
  36. use OCP\Lock\ILockingProvider;
  37. use OCP\Lock\LockedException;
  38. class LockingController extends OCSController {
  39. /** @var ILockingProvider */
  40. protected $lockingProvider;
  41. /** @var FakeDBLockingProvider */
  42. protected $fakeDBLockingProvider;
  43. /** @var IDBConnection */
  44. protected $connection;
  45. /** @var IConfig */
  46. protected $config;
  47. /** @var IRootFolder */
  48. protected $rootFolder;
  49. /**
  50. * @param string $appName
  51. * @param IRequest $request
  52. * @param ILockingProvider $lockingProvider
  53. * @param FakeDBLockingProvider $fakeDBLockingProvider
  54. * @param IDBConnection $connection
  55. * @param IConfig $config
  56. * @param IRootFolder $rootFolder
  57. */
  58. public function __construct($appName,
  59. IRequest $request,
  60. ILockingProvider $lockingProvider,
  61. FakeDBLockingProvider $fakeDBLockingProvider,
  62. IDBConnection $connection,
  63. IConfig $config,
  64. IRootFolder $rootFolder) {
  65. parent::__construct($appName, $request);
  66. $this->lockingProvider = $lockingProvider;
  67. $this->fakeDBLockingProvider = $fakeDBLockingProvider;
  68. $this->connection = $connection;
  69. $this->config = $config;
  70. $this->rootFolder = $rootFolder;
  71. }
  72. /**
  73. * @throws \RuntimeException
  74. */
  75. protected function getLockingProvider(): ILockingProvider {
  76. if ($this->lockingProvider instanceof DBLockingProvider) {
  77. return $this->fakeDBLockingProvider;
  78. }
  79. throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider');
  80. }
  81. /**
  82. * @throws NotFoundException
  83. */
  84. protected function getPath(string $user, string $path): string {
  85. $node = $this->rootFolder->getUserFolder($user)->get($path);
  86. return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/'));
  87. }
  88. /**
  89. * @throws OCSException
  90. */
  91. public function isLockingEnabled(): DataResponse {
  92. try {
  93. $this->getLockingProvider();
  94. return new DataResponse();
  95. } catch (\RuntimeException $e) {
  96. throw new OCSException($e->getMessage(), Http::STATUS_NOT_IMPLEMENTED, $e);
  97. }
  98. }
  99. /**
  100. * @throws OCSException
  101. */
  102. public function acquireLock(int $type, string $user, string $path): DataResponse {
  103. try {
  104. $path = $this->getPath($user, $path);
  105. } catch (NoUserException $e) {
  106. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  107. } catch (NotFoundException $e) {
  108. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  109. }
  110. $lockingProvider = $this->getLockingProvider();
  111. try {
  112. $lockingProvider->acquireLock($path, $type);
  113. $this->config->setAppValue('testing', 'locking_' . $path, (string)$type);
  114. return new DataResponse();
  115. } catch (LockedException $e) {
  116. throw new OCSException('', Http::STATUS_LOCKED, $e);
  117. }
  118. }
  119. /**
  120. * @throws OCSException
  121. */
  122. public function changeLock(int $type, string $user, string $path): DataResponse {
  123. try {
  124. $path = $this->getPath($user, $path);
  125. } catch (NoUserException $e) {
  126. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  127. } catch (NotFoundException $e) {
  128. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  129. }
  130. $lockingProvider = $this->getLockingProvider();
  131. try {
  132. $lockingProvider->changeLock($path, $type);
  133. $this->config->setAppValue('testing', 'locking_' . $path, (string)$type);
  134. return new DataResponse();
  135. } catch (LockedException $e) {
  136. throw new OCSException('', Http::STATUS_LOCKED, $e);
  137. }
  138. }
  139. /**
  140. * @throws OCSException
  141. */
  142. public function releaseLock(int $type, string $user, string $path): DataResponse {
  143. try {
  144. $path = $this->getPath($user, $path);
  145. } catch (NoUserException $e) {
  146. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  147. } catch (NotFoundException $e) {
  148. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  149. }
  150. $lockingProvider = $this->getLockingProvider();
  151. try {
  152. $lockingProvider->releaseLock($path, $type);
  153. $this->config->deleteAppValue('testing', 'locking_' . $path);
  154. return new DataResponse();
  155. } catch (LockedException $e) {
  156. throw new OCSException('', Http::STATUS_LOCKED, $e);
  157. }
  158. }
  159. /**
  160. * @throws OCSException
  161. */
  162. public function isLocked(int $type, string $user, string $path): DataResponse {
  163. try {
  164. $path = $this->getPath($user, $path);
  165. } catch (NoUserException $e) {
  166. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  167. } catch (NotFoundException $e) {
  168. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  169. }
  170. $lockingProvider = $this->getLockingProvider();
  171. if ($lockingProvider->isLocked($path, $type)) {
  172. return new DataResponse();
  173. }
  174. throw new OCSException('', Http::STATUS_LOCKED);
  175. }
  176. public function releaseAll(int $type = null): DataResponse {
  177. $lockingProvider = $this->getLockingProvider();
  178. foreach ($this->config->getAppKeys('testing') as $lock) {
  179. if (strpos($lock, 'locking_') === 0) {
  180. $path = substr($lock, strlen('locking_'));
  181. if ($type === ILockingProvider::LOCK_EXCLUSIVE && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_EXCLUSIVE) {
  182. $lockingProvider->releaseLock($path, (int)$this->config->getAppValue('testing', $lock));
  183. } elseif ($type === ILockingProvider::LOCK_SHARED && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_SHARED) {
  184. $lockingProvider->releaseLock($path, (int)$this->config->getAppValue('testing', $lock));
  185. } else {
  186. $lockingProvider->releaseLock($path, (int)$this->config->getAppValue('testing', $lock));
  187. }
  188. }
  189. }
  190. return new DataResponse();
  191. }
  192. }