* @brief Copy-with-marker migration from {workspace}/JSON Projects to {workspace}/Projects. */
| 164 | * @brief Copy-with-marker migration from {workspace}/JSON Projects to {workspace}/Projects. |
| 165 | */ |
| 166 | void Misc::WorkspaceManager::migrateLegacyProjectsFolder() |
| 167 | { |
| 168 | static const QLatin1StringView kMigratedKey("Workspace/LegacyProjectsFolderMigrated"); |
| 169 | static const QLatin1StringView kMarkerFileName(".migrated-from-json-projects"); |
| 170 | |
| 171 | if (m_settings.value(kMigratedKey, false).toBool()) |
| 172 | return; |
| 173 | |
| 174 | const QString legacyDir = QStringLiteral("%1/JSON Projects").arg(m_path); |
| 175 | const QString newDir = QStringLiteral("%1/Projects").arg(m_path); |
| 176 | const QString marker = QStringLiteral("%1/%2").arg(newDir, kMarkerFileName); |
| 177 | |
| 178 | QDir legacy(legacyDir); |
| 179 | if (!legacy.exists()) { |
| 180 | m_settings.setValue(kMigratedKey, true); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (QFileInfo::exists(marker)) { |
| 185 | m_settings.setValue(kMigratedKey, true); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | QDir target(newDir); |
| 190 | if (!target.exists() && !target.mkpath(QStringLiteral("."))) { |
| 191 | qWarning() << "Failed to create" << newDir << "for legacy projects migration"; |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | const auto entries = |
| 196 | legacy.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden); |
| 197 | |
| 198 | bool allCopied = true; |
| 199 | for (const auto& entry : entries) { |
| 200 | const QString src = entry.absoluteFilePath(); |
| 201 | QString dst = QStringLiteral("%1/%2").arg(newDir, entry.fileName()); |
| 202 | |
| 203 | if (entry.isFile() && QFileInfo::exists(dst)) { |
| 204 | QFile a(src); |
| 205 | QFile b(dst); |
| 206 | if (a.size() == b.size() && a.open(QIODevice::ReadOnly) && b.open(QIODevice::ReadOnly) |
| 207 | && a.readAll() == b.readAll()) |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if (QFileInfo::exists(dst)) { |
| 212 | const QString base = entry.completeBaseName(); |
| 213 | const QString ext = entry.suffix(); |
| 214 | int n = 1; |
| 215 | do { |
| 216 | const QString suffix = |
| 217 | n == 1 ? QStringLiteral(".legacy") : QStringLiteral(".legacy.%1").arg(n); |
| 218 | dst = ext.isEmpty() ? QStringLiteral("%1/%2%3").arg(newDir, base, suffix) |
| 219 | : QStringLiteral("%1/%2%3.%4").arg(newDir, base, suffix, ext); |
| 220 | ++n; |
| 221 | } while (QFileInfo::exists(dst) && n < 1000); |
| 222 | } |
| 223 |