You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LockingController.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * @return ILockingProvider
  74. * @throws \RuntimeException
  75. */
  76. protected function getLockingProvider() {
  77. if ($this->lockingProvider instanceof DBLockingProvider) {
  78. return $this->fakeDBLockingProvider;
  79. }
  80. throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider');
  81. }
  82. /**
  83. * @param string $user
  84. * @param string $path
  85. * @return string
  86. * @throws NotFoundException
  87. */
  88. protected function getPath($user, $path) {
  89. $node = $this->rootFolder->getUserFolder($user)->get($path);
  90. return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/'));
  91. }
  92. /**
  93. * @return DataResponse
  94. * @throws OCSException
  95. */
  96. public function isLockingEnabled() {
  97. try {
  98. $this->getLockingProvider();
  99. return new DataResponse();
  100. } catch (\RuntimeException $e) {
  101. throw new OCSException($e->getMessage(), Http::STATUS_NOT_IMPLEMENTED, $e);
  102. }
  103. }
  104. /**
  105. * @param int $type
  106. * @param string $user
  107. * @param string $path
  108. * @return DataResponse
  109. * @throws OCSException
  110. */
  111. public function acquireLock($type, $user, $path) {
  112. try {
  113. $path = $this->getPath($user, $path);
  114. } catch (NoUserException $e) {
  115. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  116. } catch (NotFoundException $e) {
  117. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  118. }
  119. $lockingProvider = $this->getLockingProvider();
  120. try {
  121. $lockingProvider->acquireLock($path, $type);
  122. $this->config->setAppValue('testing', 'locking_' . $path, $type);
  123. return new DataResponse();
  124. } catch (LockedException $e) {
  125. throw new OCSException('', Http::STATUS_LOCKED, $e);
  126. }
  127. }
  128. /**
  129. * @param int $type
  130. * @param string $user
  131. * @param string $path
  132. * @return DataResponse
  133. * @throws OCSException
  134. */
  135. public function changeLock($type, $user, $path) {
  136. try {
  137. $path = $this->getPath($user, $path);
  138. } catch (NoUserException $e) {
  139. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  140. } catch (NotFoundException $e) {
  141. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  142. }
  143. $lockingProvider = $this->getLockingProvider();
  144. try {
  145. $lockingProvider->changeLock($path, $type);
  146. $this->config->setAppValue('testing', 'locking_' . $path, $type);
  147. return new DataResponse();
  148. } catch (LockedException $e) {
  149. throw new OCSException('', Http::STATUS_LOCKED, $e);
  150. }
  151. }
  152. /**
  153. * @param int $type
  154. * @param string $user
  155. * @param string $path
  156. * @return DataResponse
  157. * @throws OCSException
  158. */
  159. public function releaseLock($type, $user, $path) {
  160. try {
  161. $path = $this->getPath($user, $path);
  162. } catch (NoUserException $e) {
  163. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  164. } catch (NotFoundException $e) {
  165. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  166. }
  167. $lockingProvider = $this->getLockingProvider();
  168. try {
  169. $lockingProvider->releaseLock($path, $type);
  170. $this->config->deleteAppValue('testing', 'locking_' . $path);
  171. return new DataResponse();
  172. } catch (LockedException $e) {
  173. throw new OCSException('', Http::STATUS_LOCKED, $e);
  174. }
  175. }
  176. /**
  177. * @param int $type
  178. * @param string $user
  179. * @param string $path
  180. * @return DataResponse
  181. * @throws OCSException
  182. */
  183. public function isLocked($type, $user, $path) {
  184. try {
  185. $path = $this->getPath($user, $path);
  186. } catch (NoUserException $e) {
  187. throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
  188. } catch (NotFoundException $e) {
  189. throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
  190. }
  191. $lockingProvider = $this->getLockingProvider();
  192. if ($lockingProvider->isLocked($path, $type)) {
  193. return new DataResponse();
  194. }
  195. throw new OCSException('', Http::STATUS_LOCKED);
  196. }
  197. /**
  198. * @param int $type
  199. * @return DataResponse
  200. */
  201. public function releaseAll($type = null) {
  202. $lockingProvider = $this->getLockingProvider();
  203. foreach ($this->config->getAppKeys('testing') as $lock) {
  204. if (strpos($lock, 'locking_') === 0) {
  205. $path = substr($lock, strlen('locking_'));
  206. if ($type === ILockingProvider::LOCK_EXCLUSIVE && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_EXCLUSIVE) {
  207. $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
  208. } elseif ($type === ILockingProvider::LOCK_SHARED && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_SHARED) {
  209. $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
  210. } else {
  211. $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
  212. }
  213. }
  214. }
  215. return new DataResponse();
  216. }
  217. }