provisioning.php 6.5 KB

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