part of '../action.dart'; typedef BackupDelivery = Future Function(String archivePath); /// Returns the archive to restore, or null when the user backed out. A source /// that downloads writes to [downloadPath], which is removed afterwards. typedef BackupFetch = Future Function(String downloadPath); @Riverpod(keepAlive: true) class BackupAction extends _$BackupAction { @override void build() {} Future backup(BackupDelivery deliver) { return _withWorkDir((workDir) async { return deliver(await _createArchive(workDir)); }); } Future restore(RestoreOption option, BackupFetch fetch) { return _withWorkDir((workDir) async { final archivePath = await fetch(join(workDir, 'download.zip')); if (archivePath == null) { return false; } final stagingDirPath = join(workDir, 'staging'); final MigrationData data; try { data = await restoreTask(( archivePath: archivePath, stagingDirPath: stagingDirPath, )); } on BackupException catch (e) { throw MessageException(switch (e.failure) { BackupFailure.invalid => currentAppLocalizations.invalidBackupFile, BackupFailure.newerVersion => currentAppLocalizations.backupFromNewerVersion, }); } await applyRestore(data, option, stagingDirPath: stagingDirPath); return true; }); } Future _withWorkDir(Future Function(String workDir) run) async { final workDir = Directory(join(await appPath.tempPath, 'backup$uniqueId')); await workDir.create(recursive: true); try { return await run(workDir.path); } finally { await workDir.safeDelete(recursive: true); await _discardLegacyStaging(); } } /// Earlier versions staged restores in the home directory and never removed /// the downloaded archive. Future _discardLegacyStaging() async { final homeDirPath = await appPath.homeDirPath; await File(join(homeDirPath, 'backup.zip')).safeDelete(); await Directory(join(homeDirPath, 'restore')).safeDelete(recursive: true); } Future _createArchive(String workDir) async { final databaseSnapshotPath = join(workDir, backupDatabaseName); await database.customStatement('VACUUM INTO ?', [databaseSnapshotPath]); final (profileIds, scriptIds, providers, mitmModules) = await ( database.profilesDao.query().map((item) => item.id).get(), database.scriptsDao.query().map((item) => item.id).get(), database.clashProvidersDao.query().get(), database.mitmModulesDao.query().get(), ).wait; final configMap = ref.read(configProvider).toJson(); configMap['version'] = await preferences.getVersion(); final archivePath = join(workDir, 'backup.zip'); await backupTask(( archivePath: archivePath, configMap: configMap, databaseSnapshotPath: databaseSnapshotPath, homeDirPath: await appPath.homeDirPath, entries: BackupEntries.of( profileIds: profileIds, scriptIds: scriptIds, providers: providers, mitmModules: mitmModules, ), )); return archivePath; } @visibleForTesting Future applyRestore( MigrationData data, RestoreOption option, { String? stagingDirPath, }) async { final configMap = data.configMap; final config = option == RestoreOption.onlyProfiles || configMap == null ? null : Config.fromJson(configMap); final isOverride = ref.read(appSettingProvider).restoreStrategy == RestoreStrategy.override; final previousProviders = await database.clashProvidersDao.query().get(); final putBack = stagingDirPath == null ? null : await _copyStagedFiles(data, stagingDirPath); try { await database.restore( data.profiles, data.scripts, data.rules, data.disabledRules, data.proxyGroups, clashProviders: data.clashProviders, customProxies: data.customProxies, proxyDialers: data.proxyDialers, mitmModules: data.mitmModules, isOverride: isOverride, ); } catch (_) { await putBack?.call(); rethrow; } await _clearReplacedProviderCaches(previousProviders); await _clearOutdatedCompiledRuleSets(data.clashProviders); if (config == null) { await _restoreMitmModules(); return; } // A desktop backup carries no package lists; the apps picked here stay. final accessControlProps = config.vpnProps.accessControlProps.hasPackages ? config.vpnProps.accessControlProps : ref.read(vpnSettingProvider).accessControlProps; writeConfig( ref, config.copyWith( davProps: config.davProps ?? ref.read(davSettingProvider), vpnProps: config.vpnProps.copyWith( accessControlProps: accessControlProps, ), ), ); if (unusableMixedPort(configMap?['patchClashConfig']) case final value?) { dialogs.showMixedPortReset(value, config.patchClashConfig.mixedPort); } await _restoreMitmModules(); } Future _restoreMitmModules() async { final action = ref.read(mitmModulesActionProvider.notifier); await action.recheckConsent(); await action.clean(); if (ref.mounted) { unawaited(action.autoUpdate()); } } /// The files go in before the rows that name them, so the returned callback /// is how a restore whose rows fail takes them back out. Future Function()> _copyStagedFiles( MigrationData data, String stagingDirPath, ) async { final homeDirPath = await appPath.homeDirPath; final replacedDirPath = join(stagingDirPath, 'replaced'); final copied = {}; Future putBack() async { for (final MapEntry(key: entry, value: replaced) in copied.entries) { final target = BackupEntries.resolve(homeDirPath, entry); if (replaced) { await File( BackupEntries.resolve(replacedDirPath, entry), ).safeCopy(target); } else { await File(target).safeDelete(); } } } try { for (final entry in BackupEntries.ofData(data)) { final staged = File(BackupEntries.resolve(stagingDirPath, entry)); if (!await staged.exists()) { continue; } final target = File(BackupEntries.resolve(homeDirPath, entry)); final replaced = await target.exists(); if (replaced) { await target.safeCopy(BackupEntries.resolve(replacedDirPath, entry)); } copied[entry] = replaced; await staged.safeCopy(target.path); } } catch (_) { await putBack(); rethrow; } return putBack; } Future _clearReplacedProviderCaches( List previousProviders, ) async { final kept = (await database.clashProvidersDao.fileNames().get()).toSet(); for (final provider in previousProviders) { if (kept.contains(provider.fileName)) { continue; } await File(await provider.path).safeDelete(); await File(await provider.compiledPath).safeDelete(); } } /// A restored source outdates the mrs compiled from the one it replaced. Future _clearOutdatedCompiledRuleSets( List restored, ) async { for (final provider in restored) { if (!provider.isRemote) { await File(await provider.compiledPath).safeDelete(); } } } }