provisioning.php 6.5 KB

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