Storage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Cache;
  29. /**
  30. * Handle the mapping between the string and numeric storage ids
  31. *
  32. * Each storage has 2 different ids
  33. * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
  34. * and a numeric storage id which is referenced in the file cache
  35. *
  36. * A mapping between the two storage ids is stored in the database and accessible trough this class
  37. *
  38. * @package OC\Files\Cache
  39. */
  40. class Storage {
  41. /** @var StorageGlobal|null */
  42. private static $globalCache = null;
  43. private $storageId;
  44. private $numericId;
  45. /**
  46. * @return StorageGlobal
  47. */
  48. public static function getGlobalCache() {
  49. if (is_null(self::$globalCache)) {
  50. self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
  51. }
  52. return self::$globalCache;
  53. }
  54. /**
  55. * @param \OC\Files\Storage\Storage|string $storage
  56. * @param bool $isAvailable
  57. * @throws \RuntimeException
  58. */
  59. public function __construct($storage, $isAvailable = true) {
  60. if ($storage instanceof \OC\Files\Storage\Storage) {
  61. $this->storageId = $storage->getId();
  62. } else {
  63. $this->storageId = $storage;
  64. }
  65. $this->storageId = self::adjustStorageId($this->storageId);
  66. if ($row = self::getStorageById($this->storageId)) {
  67. $this->numericId = (int)$row['numeric_id'];
  68. } else {
  69. $connection = \OC::$server->getDatabaseConnection();
  70. $available = $isAvailable ? 1 : 0;
  71. if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
  72. $this->numericId = (int)$connection->lastInsertId('*PREFIX*storages');
  73. } else {
  74. if ($row = self::getStorageById($this->storageId)) {
  75. $this->numericId = (int)$row['numeric_id'];
  76. } else {
  77. throw new \RuntimeException('Storage could neither be inserted nor be selected from the database');
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * @param string $storageId
  84. * @return array
  85. */
  86. public static function getStorageById($storageId) {
  87. return self::getGlobalCache()->getStorageInfo($storageId);
  88. }
  89. /**
  90. * Adjusts the storage id to use md5 if too long
  91. * @param string $storageId storage id
  92. * @return string unchanged $storageId if its length is less than 64 characters,
  93. * else returns the md5 of $storageId
  94. */
  95. public static function adjustStorageId($storageId) {
  96. if (strlen($storageId) > 64) {
  97. return md5($storageId);
  98. }
  99. return $storageId;
  100. }
  101. /**
  102. * Get the numeric id for the storage
  103. *
  104. * @return int
  105. */
  106. public function getNumericId() {
  107. return $this->numericId;
  108. }
  109. /**
  110. * Get the string id for the storage
  111. *
  112. * @param int $numericId
  113. * @return string|null either the storage id string or null if the numeric id is not known
  114. */
  115. public static function getStorageId($numericId) {
  116. $sql = 'SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?';
  117. $result = \OC_DB::executeAudited($sql, array($numericId));
  118. if ($row = $result->fetchRow()) {
  119. return $row['id'];
  120. } else {
  121. return null;
  122. }
  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 knwon
  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|null [ 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 null;
  149. }
  150. }
  151. /**
  152. * @param bool $isAvailable
  153. */
  154. public function setAvailability($isAvailable) {
  155. $sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
  156. $available = $isAvailable ? 1 : 0;
  157. \OC_DB::executeAudited($sql, array($available, time(), $this->storageId));
  158. }
  159. /**
  160. * Check if a string storage id is known
  161. *
  162. * @param string $storageId
  163. * @return bool
  164. */
  165. public static function exists($storageId) {
  166. return !is_null(self::getNumericStorageId($storageId));
  167. }
  168. /**
  169. * remove the entry for the storage
  170. *
  171. * @param string $storageId
  172. */
  173. public static function remove($storageId) {
  174. $storageId = self::adjustStorageId($storageId);
  175. $numericId = self::getNumericStorageId($storageId);
  176. $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
  177. \OC_DB::executeAudited($sql, array($storageId));
  178. if (!is_null($numericId)) {
  179. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
  180. \OC_DB::executeAudited($sql, array($numericId));
  181. }
  182. }
  183. }