Welcome to mirror list, hosted at ThFree Co, Russian Federation.

repairlegacystorages.php « repair « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab123afeca6f0591736cdb770c166335dfbc5cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
 * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\Repair;

use OC\Hooks\BasicEmitter;

class RepairLegacyStorages extends BasicEmitter {
	/**
	 * @var \OCP\IConfig
	 */
	protected $config;

	/**
	 * @var \OC\DB\Connection
	 */
	protected $connection;

	protected $findStorageInCacheStatement;
	protected $renameStorageStatement;

	/**
	 * @param \OCP\IConfig $config
	 * @param \OC\DB\Connection $connection
	 */
	public function __construct($config, $connection) {
		$this->connection = $connection;
		$this->config = $config;

		$this->findStorageInCacheStatement = $this->connection->prepare(
			'SELECT DISTINCT `storage` FROM `*PREFIX*filecache`'
			. ' WHERE `storage` in (?, ?)'
		);
		$this->renameStorageStatement = $this->connection->prepare(
			'UPDATE `*PREFIX*storages`'
			. ' SET `id` = ?'
			. ' WHERE `id` = ?'
		);
	}

	public function getName() {
		return 'Repair legacy storages';
	}

	/**
	 * Extracts the user id	from a legacy storage id
	 *
	 * @param string $storageId legacy storage id in the
	 * format "local::/path/to/datadir/userid"
	 * @return string user id extracted from the storage id
	 */
	private function extractUserId($storageId) {
		$storageId = rtrim($storageId, '/');
		$pos = strrpos($storageId, '/');
		return substr($storageId, $pos + 1);
	}

	/**
	 * Fix the given legacy storage by renaming the old id
	 * to the new id. If the new id already exists, whichever
	 * storage that has data in the file cache will be used.
	 * If both have data, nothing will be done and false is
	 * returned.
	 *
	 * @param string $oldId old storage id
	 * @param int $oldNumericId old storage numeric id
	 *
	 * @return bool true if fixed, false otherwise
	 */
	private function fixLegacyStorage($oldId, $oldNumericId, $userId = null) {
		// check whether the new storage already exists
		if (is_null($userId)) {
			$userId = $this->extractUserId($oldId);
		}
		$newId = 'home::' . $userId;

		// check if target id already exists
		$newNumericId = \OC\Files\Cache\Storage::getNumericStorageId($newId);
		if (!is_null($newNumericId)) {
			$newNumericId = (int)$newNumericId;
			// try and resolve the conflict
			// check which one of "local::" or "home::" needs to be kept
			$result = $this->findStorageInCacheStatement->execute(array($oldNumericId, $newNumericId));
			$row1 = $this->findStorageInCacheStatement->fetch();
			$row2 = $this->findStorageInCacheStatement->fetch();
			$this->findStorageInCacheStatement->closeCursor();
			if ($row2 !== false) {
				// two results means both storages have data, not auto-fixable
				throw new \OC\RepairException(
					'Could not automatically fix legacy storage '
					. '"' . $oldId . '" => "' . $newId . '"'
					. ' because they both have data.'
				);
			}
			if ($row1 === false || (int)$row1['storage'] === $oldNumericId) {
				// old storage has data, then delete the empty new id
				$toDelete = $newId;
			} else if ((int)$row1['storage'] === $newNumericId) {
				// new storage has data, then delete the empty old id
				$toDelete = $oldId;
			} else {
				// unknown case, do not continue
				return false;
			}

			// delete storage including file cache
			\OC\Files\Cache\Storage::remove($toDelete);

			// if we deleted the old id, the new id will be used
			// automatically
			if ($toDelete === $oldId) {
				// nothing more to do
				return true;
			}
		}

		// rename old id to new id
		$newId = \OC\Files\Cache\Storage::adjustStorageId($newId);
		$oldId = \OC\Files\Cache\Storage::adjustStorageId($oldId);
		$rowCount = $this->renameStorageStatement->execute(array($newId, $oldId));
		$this->renameStorageStatement->closeCursor();
		return ($rowCount === 1);
	}

	/**
	 * Converts legacy home storage ids in the format
	 * "local::/data/dir/path/userid/" to the new format "home::userid"
	 */
	public function run() {
		// only run once
		if ($this->config->getAppValue('core', 'repairlegacystoragesdone') === 'yes') {
			return;
		}

		$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
		$dataDir = rtrim($dataDir, '/') . '/';
		$dataDirId = 'local::' . $dataDir;

		$count = 0;

		$this->connection->beginTransaction();

		try {
			// note: not doing a direct UPDATE with the REPLACE function
			// because regexp search/extract is needed and it is not guaranteed
			// to work on all database types
			$sql = 'SELECT `id`, `numeric_id` FROM `*PREFIX*storages`'
				. ' WHERE `id` LIKE ?'
				. ' ORDER BY `id`';
			$result = $this->connection->executeQuery($sql, array($dataDirId . '%'));
			while ($row = $result->fetch()) {
				$currentId = $row['id'];
				// one entry is the datadir itself
				if ($currentId === $dataDirId) {
					continue;
				}

				if ($this->fixLegacyStorage($currentId, (int)$row['numeric_id'])) {
					$count++;
				}
			}

			// check for md5 ids, not in the format "prefix::"
			$sql = 'SELECT COUNT(*) AS "c" FROM `*PREFIX*storages`'
				. ' WHERE `id` NOT LIKE \'%::%\'';
			$result = $this->connection->executeQuery($sql);
			$row = $result->fetch();
			// find at least one to make sure it's worth
			// querying the user list
			if ((int)$row['c'] > 0) {
				$userManager = \OC_User::getManager();

				// use chunks to avoid caching too many users in memory
				$limit = 30;
				$offset = 0;

				do {
					// query the next page of users
					$results = $userManager->search('', $limit, $offset);
					$storageIds = array();
					$userIds = array();
					foreach ($results as $uid => $userObject) {
						$storageId = $dataDirId . $uid . '/';
						if (strlen($storageId) <= 64) {
							// skip short storage ids as they were handled in the previous section
							continue;
						}
						$storageIds[$uid] = $storageId;
					}

					if (count($storageIds) > 0) {
						// update the storages of these users
						foreach ($storageIds as $uid => $storageId) {
							$numericId = \OC\Files\Cache\Storage::getNumericStorageId($storageId);
							if (!is_null($numericId) && $this->fixLegacyStorage($storageId, (int)$numericId)) {
								$count++;
							}
						}
					}
					$offset += $limit;
				} while (count($results) >= $limit);
			}

			$this->emit('\OC\Repair', 'info', array('Updated ' . $count . ' legacy home storage ids'));

			$this->connection->commit();
		}
		catch (\OC\RepairException $e) {
			$this->connection->rollback();
			throw $e;
		}

		$this->config->setAppValue('core', 'repairlegacystoragesdone', 'yes');
	}
}