Storage.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Files\Cache;
  31. use OCP\DB\QueryBuilder\IQueryBuilder;
  32. use OCP\Files\Storage\IStorage;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * Handle the mapping between the string and numeric storage ids
  36. *
  37. * Each storage has 2 different ids
  38. * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
  39. * and a numeric storage id which is referenced in the file cache
  40. *
  41. * A mapping between the two storage ids is stored in the database and accessible through this class
  42. *
  43. * @package OC\Files\Cache
  44. */
  45. class Storage {
  46. /** @var StorageGlobal|null */
  47. private static $globalCache = null;
  48. private $storageId;
  49. private $numericId;
  50. /**
  51. * @return StorageGlobal
  52. */
  53. public static function getGlobalCache() {
  54. if (is_null(self::$globalCache)) {
  55. self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
  56. }
  57. return self::$globalCache;
  58. }
  59. /**
  60. * @param \OC\Files\Storage\Storage|string $storage
  61. * @param bool $isAvailable
  62. * @throws \RuntimeException
  63. */
  64. public function __construct($storage, $isAvailable = true) {
  65. if ($storage instanceof IStorage) {
  66. $this->storageId = $storage->getId();
  67. } else {
  68. $this->storageId = $storage;
  69. }
  70. $this->storageId = self::adjustStorageId($this->storageId);
  71. if ($row = self::getStorageById($this->storageId)) {
  72. $this->numericId = (int)$row['numeric_id'];
  73. } else {
  74. $connection = \OC::$server->getDatabaseConnection();
  75. $available = $isAvailable ? 1 : 0;
  76. if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
  77. $this->numericId = $connection->lastInsertId('*PREFIX*storages');
  78. } else {
  79. if ($row = self::getStorageById($this->storageId)) {
  80. $this->numericId = (int)$row['numeric_id'];
  81. } else {
  82. throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * @param string $storageId
  89. * @return array
  90. */
  91. public static function getStorageById($storageId) {
  92. return self::getGlobalCache()->getStorageInfo($storageId);
  93. }
  94. /**
  95. * Adjusts the storage id to use md5 if too long
  96. * @param string $storageId storage id
  97. * @return string unchanged $storageId if its length is less than 64 characters,
  98. * else returns the md5 of $storageId
  99. */
  100. public static function adjustStorageId($storageId) {
  101. if (strlen($storageId) > 64) {
  102. return md5($storageId);
  103. }
  104. return $storageId;
  105. }
  106. /**
  107. * Get the numeric id for the storage
  108. *
  109. * @return int
  110. */
  111. public function getNumericId() {
  112. return $this->numericId;
  113. }
  114. /**
  115. * Get the string id for the storage
  116. *
  117. * @param int $numericId
  118. * @return string|null either the storage id string or null if the numeric id is not known
  119. */
  120. public static function getStorageId(int $numericId): ?string {
  121. $storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId);
  122. return $storage['id'] ?? null;
  123. }
  124. /**
  125. * Get the numeric of the storage with the provided string id
  126. *
  127. * @param $storageId
  128. * @return int|null either the numeric storage id or null if the storage id is not known
  129. */
  130. public static function getNumericStorageId($storageId) {
  131. $storageId = self::adjustStorageId($storageId);
  132. if ($row = self::getStorageById($storageId)) {
  133. return (int)$row['numeric_id'];
  134. } else {
  135. return null;
  136. }
  137. }
  138. /**
  139. * @return array [ available, last_checked ]
  140. */
  141. public function getAvailability() {
  142. if ($row = self::getStorageById($this->storageId)) {
  143. return [
  144. 'available' => (int)$row['available'] === 1,
  145. 'last_checked' => $row['last_checked']
  146. ];
  147. } else {
  148. return [
  149. 'available' => true,
  150. 'last_checked' => time(),
  151. ];
  152. }
  153. }
  154. /**
  155. * @param bool $isAvailable
  156. * @param int $delay amount of seconds to delay reconsidering that storage further
  157. */
  158. public function setAvailability($isAvailable, int $delay = 0) {
  159. $available = $isAvailable ? 1 : 0;
  160. if (!$isAvailable) {
  161. \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
  162. }
  163. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  164. $query->update('storages')
  165. ->set('available', $query->createNamedParameter($available))
  166. ->set('last_checked', $query->createNamedParameter(time() + $delay))
  167. ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
  168. $query->execute();
  169. }
  170. /**
  171. * Check if a string storage id is known
  172. *
  173. * @param string $storageId
  174. * @return bool
  175. */
  176. public static function exists($storageId) {
  177. return !is_null(self::getNumericStorageId($storageId));
  178. }
  179. /**
  180. * remove the entry for the storage
  181. *
  182. * @param string $storageId
  183. */
  184. public static function remove($storageId) {
  185. $storageId = self::adjustStorageId($storageId);
  186. $numericId = self::getNumericStorageId($storageId);
  187. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  188. $query->delete('storages')
  189. ->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
  190. $query->execute();
  191. if (!is_null($numericId)) {
  192. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  193. $query->delete('filecache')
  194. ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
  195. $query->execute();
  196. }
  197. }
  198. /**
  199. * remove the entry for the storage by the mount id
  200. *
  201. * @param int $mountId
  202. */
  203. public static function cleanByMountId(int $mountId) {
  204. $db = \OC::$server->getDatabaseConnection();
  205. try {
  206. $db->beginTransaction();
  207. $query = $db->getQueryBuilder();
  208. $query->select('storage_id')
  209. ->from('mounts')
  210. ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  211. $storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  212. $storageIds = array_unique($storageIds);
  213. $query = $db->getQueryBuilder();
  214. $query->delete('filecache')
  215. ->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
  216. $query->executeStatement();
  217. $query = $db->getQueryBuilder();
  218. $query->delete('storages')
  219. ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
  220. $query->executeStatement();
  221. $query = $db->getQueryBuilder();
  222. $query->delete('mounts')
  223. ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
  224. $query->executeStatement();
  225. $db->commit();
  226. } catch (\Exception $e) {
  227. $db->rollBack();
  228. throw $e;
  229. }
  230. }
  231. }