@@ -45,212 +45,212 @@ |
||
| 45 | 45 | * @package OC\Files\Cache |
| 46 | 46 | */ |
| 47 | 47 | class Storage { |
| 48 | - /** @var StorageGlobal|null */ |
|
| 49 | - private static $globalCache = null; |
|
| 50 | - private $storageId; |
|
| 51 | - private $numericId; |
|
| 48 | + /** @var StorageGlobal|null */ |
|
| 49 | + private static $globalCache = null; |
|
| 50 | + private $storageId; |
|
| 51 | + private $numericId; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return StorageGlobal |
|
| 55 | - */ |
|
| 56 | - public static function getGlobalCache() { |
|
| 57 | - if (is_null(self::$globalCache)) { |
|
| 58 | - self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection()); |
|
| 59 | - } |
|
| 60 | - return self::$globalCache; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * @return StorageGlobal |
|
| 55 | + */ |
|
| 56 | + public static function getGlobalCache() { |
|
| 57 | + if (is_null(self::$globalCache)) { |
|
| 58 | + self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection()); |
|
| 59 | + } |
|
| 60 | + return self::$globalCache; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @param \OC\Files\Storage\Storage|string $storage |
|
| 65 | - * @param bool $isAvailable |
|
| 66 | - * @throws \RuntimeException |
|
| 67 | - */ |
|
| 68 | - public function __construct($storage, $isAvailable = true) { |
|
| 69 | - if ($storage instanceof IStorage) { |
|
| 70 | - $this->storageId = $storage->getId(); |
|
| 71 | - } else { |
|
| 72 | - $this->storageId = $storage; |
|
| 73 | - } |
|
| 74 | - $this->storageId = self::adjustStorageId($this->storageId); |
|
| 63 | + /** |
|
| 64 | + * @param \OC\Files\Storage\Storage|string $storage |
|
| 65 | + * @param bool $isAvailable |
|
| 66 | + * @throws \RuntimeException |
|
| 67 | + */ |
|
| 68 | + public function __construct($storage, $isAvailable = true) { |
|
| 69 | + if ($storage instanceof IStorage) { |
|
| 70 | + $this->storageId = $storage->getId(); |
|
| 71 | + } else { |
|
| 72 | + $this->storageId = $storage; |
|
| 73 | + } |
|
| 74 | + $this->storageId = self::adjustStorageId($this->storageId); |
|
| 75 | 75 | |
| 76 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 77 | - $this->numericId = (int)$row['numeric_id']; |
|
| 78 | - } else { |
|
| 79 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 80 | - $available = $isAvailable ? 1 : 0; |
|
| 81 | - if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) { |
|
| 82 | - $this->numericId = $connection->lastInsertId('*PREFIX*storages'); |
|
| 83 | - } else { |
|
| 84 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 85 | - $this->numericId = (int)$row['numeric_id']; |
|
| 86 | - } else { |
|
| 87 | - throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 76 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 77 | + $this->numericId = (int)$row['numeric_id']; |
|
| 78 | + } else { |
|
| 79 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 80 | + $available = $isAvailable ? 1 : 0; |
|
| 81 | + if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) { |
|
| 82 | + $this->numericId = $connection->lastInsertId('*PREFIX*storages'); |
|
| 83 | + } else { |
|
| 84 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 85 | + $this->numericId = (int)$row['numeric_id']; |
|
| 86 | + } else { |
|
| 87 | + throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * @param string $storageId |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public static function getStorageById($storageId) { |
|
| 98 | - return self::getGlobalCache()->getStorageInfo($storageId); |
|
| 99 | - } |
|
| 93 | + /** |
|
| 94 | + * @param string $storageId |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public static function getStorageById($storageId) { |
|
| 98 | + return self::getGlobalCache()->getStorageInfo($storageId); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Adjusts the storage id to use md5 if too long |
|
| 103 | - * @param string $storageId storage id |
|
| 104 | - * @return string unchanged $storageId if its length is less than 64 characters, |
|
| 105 | - * else returns the md5 of $storageId |
|
| 106 | - */ |
|
| 107 | - public static function adjustStorageId($storageId) { |
|
| 108 | - if (strlen($storageId) > 64) { |
|
| 109 | - return md5($storageId); |
|
| 110 | - } |
|
| 111 | - return $storageId; |
|
| 112 | - } |
|
| 101 | + /** |
|
| 102 | + * Adjusts the storage id to use md5 if too long |
|
| 103 | + * @param string $storageId storage id |
|
| 104 | + * @return string unchanged $storageId if its length is less than 64 characters, |
|
| 105 | + * else returns the md5 of $storageId |
|
| 106 | + */ |
|
| 107 | + public static function adjustStorageId($storageId) { |
|
| 108 | + if (strlen($storageId) > 64) { |
|
| 109 | + return md5($storageId); |
|
| 110 | + } |
|
| 111 | + return $storageId; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Get the numeric id for the storage |
|
| 116 | - * |
|
| 117 | - * @return int |
|
| 118 | - */ |
|
| 119 | - public function getNumericId() { |
|
| 120 | - return $this->numericId; |
|
| 121 | - } |
|
| 114 | + /** |
|
| 115 | + * Get the numeric id for the storage |
|
| 116 | + * |
|
| 117 | + * @return int |
|
| 118 | + */ |
|
| 119 | + public function getNumericId() { |
|
| 120 | + return $this->numericId; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Get the string id for the storage |
|
| 125 | - * |
|
| 126 | - * @param int $numericId |
|
| 127 | - * @return string|null either the storage id string or null if the numeric id is not known |
|
| 128 | - */ |
|
| 129 | - public static function getStorageId(int $numericId): ?string { |
|
| 130 | - $storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId); |
|
| 131 | - return $storage['id'] ?? null; |
|
| 132 | - } |
|
| 123 | + /** |
|
| 124 | + * Get the string id for the storage |
|
| 125 | + * |
|
| 126 | + * @param int $numericId |
|
| 127 | + * @return string|null either the storage id string or null if the numeric id is not known |
|
| 128 | + */ |
|
| 129 | + public static function getStorageId(int $numericId): ?string { |
|
| 130 | + $storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId); |
|
| 131 | + return $storage['id'] ?? null; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the numeric of the storage with the provided string id |
|
| 136 | - * |
|
| 137 | - * @param $storageId |
|
| 138 | - * @return int|null either the numeric storage id or null if the storage id is not knwon |
|
| 139 | - */ |
|
| 140 | - public static function getNumericStorageId($storageId) { |
|
| 141 | - $storageId = self::adjustStorageId($storageId); |
|
| 134 | + /** |
|
| 135 | + * Get the numeric of the storage with the provided string id |
|
| 136 | + * |
|
| 137 | + * @param $storageId |
|
| 138 | + * @return int|null either the numeric storage id or null if the storage id is not knwon |
|
| 139 | + */ |
|
| 140 | + public static function getNumericStorageId($storageId) { |
|
| 141 | + $storageId = self::adjustStorageId($storageId); |
|
| 142 | 142 | |
| 143 | - if ($row = self::getStorageById($storageId)) { |
|
| 144 | - return (int)$row['numeric_id']; |
|
| 145 | - } else { |
|
| 146 | - return null; |
|
| 147 | - } |
|
| 148 | - } |
|
| 143 | + if ($row = self::getStorageById($storageId)) { |
|
| 144 | + return (int)$row['numeric_id']; |
|
| 145 | + } else { |
|
| 146 | + return null; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @return array [ available, last_checked ] |
|
| 152 | - */ |
|
| 153 | - public function getAvailability() { |
|
| 154 | - if ($row = self::getStorageById($this->storageId)) { |
|
| 155 | - return [ |
|
| 156 | - 'available' => (int)$row['available'] === 1, |
|
| 157 | - 'last_checked' => $row['last_checked'] |
|
| 158 | - ]; |
|
| 159 | - } else { |
|
| 160 | - return [ |
|
| 161 | - 'available' => true, |
|
| 162 | - 'last_checked' => time(), |
|
| 163 | - ]; |
|
| 164 | - } |
|
| 165 | - } |
|
| 150 | + /** |
|
| 151 | + * @return array [ available, last_checked ] |
|
| 152 | + */ |
|
| 153 | + public function getAvailability() { |
|
| 154 | + if ($row = self::getStorageById($this->storageId)) { |
|
| 155 | + return [ |
|
| 156 | + 'available' => (int)$row['available'] === 1, |
|
| 157 | + 'last_checked' => $row['last_checked'] |
|
| 158 | + ]; |
|
| 159 | + } else { |
|
| 160 | + return [ |
|
| 161 | + 'available' => true, |
|
| 162 | + 'last_checked' => time(), |
|
| 163 | + ]; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param bool $isAvailable |
|
| 169 | - * @param int $delay amount of seconds to delay reconsidering that storage further |
|
| 170 | - */ |
|
| 171 | - public function setAvailability($isAvailable, int $delay = 0) { |
|
| 172 | - $available = $isAvailable ? 1 : 0; |
|
| 173 | - if (!$isAvailable) { |
|
| 174 | - \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']); |
|
| 175 | - } |
|
| 167 | + /** |
|
| 168 | + * @param bool $isAvailable |
|
| 169 | + * @param int $delay amount of seconds to delay reconsidering that storage further |
|
| 170 | + */ |
|
| 171 | + public function setAvailability($isAvailable, int $delay = 0) { |
|
| 172 | + $available = $isAvailable ? 1 : 0; |
|
| 173 | + if (!$isAvailable) { |
|
| 174 | + \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 178 | - $query->update('storages') |
|
| 179 | - ->set('available', $query->createNamedParameter($available)) |
|
| 180 | - ->set('last_checked', $query->createNamedParameter(time() + $delay)) |
|
| 181 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId))); |
|
| 182 | - $query->execute(); |
|
| 183 | - } |
|
| 177 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 178 | + $query->update('storages') |
|
| 179 | + ->set('available', $query->createNamedParameter($available)) |
|
| 180 | + ->set('last_checked', $query->createNamedParameter(time() + $delay)) |
|
| 181 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId))); |
|
| 182 | + $query->execute(); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Check if a string storage id is known |
|
| 187 | - * |
|
| 188 | - * @param string $storageId |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public static function exists($storageId) { |
|
| 192 | - return !is_null(self::getNumericStorageId($storageId)); |
|
| 193 | - } |
|
| 185 | + /** |
|
| 186 | + * Check if a string storage id is known |
|
| 187 | + * |
|
| 188 | + * @param string $storageId |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public static function exists($storageId) { |
|
| 192 | + return !is_null(self::getNumericStorageId($storageId)); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * remove the entry for the storage |
|
| 197 | - * |
|
| 198 | - * @param string $storageId |
|
| 199 | - */ |
|
| 200 | - public static function remove($storageId) { |
|
| 201 | - $storageId = self::adjustStorageId($storageId); |
|
| 202 | - $numericId = self::getNumericStorageId($storageId); |
|
| 195 | + /** |
|
| 196 | + * remove the entry for the storage |
|
| 197 | + * |
|
| 198 | + * @param string $storageId |
|
| 199 | + */ |
|
| 200 | + public static function remove($storageId) { |
|
| 201 | + $storageId = self::adjustStorageId($storageId); |
|
| 202 | + $numericId = self::getNumericStorageId($storageId); |
|
| 203 | 203 | |
| 204 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 205 | - $query->delete('storages') |
|
| 206 | - ->where($query->expr()->eq('id', $query->createNamedParameter($storageId))); |
|
| 207 | - $query->execute(); |
|
| 204 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 205 | + $query->delete('storages') |
|
| 206 | + ->where($query->expr()->eq('id', $query->createNamedParameter($storageId))); |
|
| 207 | + $query->execute(); |
|
| 208 | 208 | |
| 209 | - if (!is_null($numericId)) { |
|
| 210 | - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 211 | - $query->delete('filecache') |
|
| 212 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId))); |
|
| 213 | - $query->execute(); |
|
| 214 | - } |
|
| 215 | - } |
|
| 209 | + if (!is_null($numericId)) { |
|
| 210 | + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 211 | + $query->delete('filecache') |
|
| 212 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId))); |
|
| 213 | + $query->execute(); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * remove the entry for the storage by the mount id |
|
| 219 | - * |
|
| 220 | - * @param int $mountId |
|
| 221 | - */ |
|
| 222 | - public static function cleanByMountId(int $mountId) { |
|
| 223 | - $db = \OC::$server->getDatabaseConnection(); |
|
| 217 | + /** |
|
| 218 | + * remove the entry for the storage by the mount id |
|
| 219 | + * |
|
| 220 | + * @param int $mountId |
|
| 221 | + */ |
|
| 222 | + public static function cleanByMountId(int $mountId) { |
|
| 223 | + $db = \OC::$server->getDatabaseConnection(); |
|
| 224 | 224 | |
| 225 | - try { |
|
| 226 | - $db->beginTransaction(); |
|
| 225 | + try { |
|
| 226 | + $db->beginTransaction(); |
|
| 227 | 227 | |
| 228 | - $query = $db->getQueryBuilder(); |
|
| 229 | - $query->select('storage_id') |
|
| 230 | - ->from('mounts') |
|
| 231 | - ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 232 | - $storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 233 | - $storageIds = array_unique($storageIds); |
|
| 228 | + $query = $db->getQueryBuilder(); |
|
| 229 | + $query->select('storage_id') |
|
| 230 | + ->from('mounts') |
|
| 231 | + ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 232 | + $storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 233 | + $storageIds = array_unique($storageIds); |
|
| 234 | 234 | |
| 235 | - $query = $db->getQueryBuilder(); |
|
| 236 | - $query->delete('filecache') |
|
| 237 | - ->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 238 | - $query->executeStatement(); |
|
| 235 | + $query = $db->getQueryBuilder(); |
|
| 236 | + $query->delete('filecache') |
|
| 237 | + ->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 238 | + $query->executeStatement(); |
|
| 239 | 239 | |
| 240 | - $query = $db->getQueryBuilder(); |
|
| 241 | - $query->delete('storages') |
|
| 242 | - ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 243 | - $query->executeStatement(); |
|
| 240 | + $query = $db->getQueryBuilder(); |
|
| 241 | + $query->delete('storages') |
|
| 242 | + ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 243 | + $query->executeStatement(); |
|
| 244 | 244 | |
| 245 | - $query = $db->getQueryBuilder(); |
|
| 246 | - $query->delete('mounts') |
|
| 247 | - ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 248 | - $query->executeStatement(); |
|
| 245 | + $query = $db->getQueryBuilder(); |
|
| 246 | + $query->delete('mounts') |
|
| 247 | + ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 248 | + $query->executeStatement(); |
|
| 249 | 249 | |
| 250 | - $db->commit(); |
|
| 251 | - } catch (\Exception $e) { |
|
| 252 | - $db->rollBack(); |
|
| 253 | - throw $e; |
|
| 254 | - } |
|
| 255 | - } |
|
| 250 | + $db->commit(); |
|
| 251 | + } catch (\Exception $e) { |
|
| 252 | + $db->rollBack(); |
|
| 253 | + throw $e; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | 256 | } |