From 277c691b2c9945ca7efb5a97d559b58b49e6b6fd Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 9 Nov 2026 19:40:52 +0000 Subject: [PATCH] Install Linux updates in place and restart The Linux app now downloads the release archive to a temporary folder, verifies its checksum, extracts it beside the install folder, swaps the bundle in with two renames (keeping .previous for rollback) and relaunches. A read-only install location is reported with how to fix it. The prompt and Settings button say "Install and restart". --- .../settings/settings_updates_section.dart | 4 +- app/lib/features/updates/app_updates_io.dart | 22 ++++ .../updates/launch_update_dialog.dart | 28 ++++- .../features/updates/linux_self_update.dart | 116 ++++++++++++++++++ .../features/updates/launch_update_test.dart | 18 ++- .../updates/linux_self_update_test.dart | 115 +++++++++++++++++ .../updates/native_update_dialog_test.dart | 51 ++++---- 7 files changed, 320 insertions(+), 34 deletions(-) create mode 100644 app/lib/features/updates/linux_self_update.dart create mode 100644 app/test/features/updates/linux_self_update_test.dart diff --git a/app/lib/features/settings/settings_updates_section.dart b/app/lib/features/settings/settings_updates_section.dart index 31d9f3b6..60e95376 100644 --- a/app/lib/features/settings/settings_updates_section.dart +++ b/app/lib/features/settings/settings_updates_section.dart @@ -155,7 +155,9 @@ class _SettingsUpdatesSectionState extends State { if (_available != null) button( const Key('settings-install-update'), - 'Download update', + _available!.platform == 'linux' + ? 'Install and restart' + : 'Download update', _busy ? null : _install, true, ), diff --git a/app/lib/features/updates/app_updates_io.dart b/app/lib/features/updates/app_updates_io.dart index 6e2fe953..bfb1136e 100644 --- a/app/lib/features/updates/app_updates_io.dart +++ b/app/lib/features/updates/app_updates_io.dart @@ -10,6 +10,7 @@ import 'package:path_provider/path_provider.dart'; import '../../core/session/server_session.dart'; import 'app_updates.dart'; +import 'linux_self_update.dart'; bool get supported => Platform.isAndroid || Platform.isLinux || Platform.isMacOS; @@ -150,6 +151,27 @@ Future downloadAndInstall( AppUpdate update, { void Function(double progress)? onProgress, }) async { + if (Platform.isLinux) { + // Linux installs in place: download, verify, swap the bundle, relaunch. + final installer = LinuxSelfUpdate.current(); + final problem = await installer.problem(); + if (problem != null) throw FileSystemException(problem); + final file = await downloadUpdate( + update, + await getTemporaryDirectory(), + onProgress: onProgress, + ); + final String executable; + try { + executable = await installer.install( + file, + versionCode: update.versionCode, + ); + } finally { + if (await file.exists()) await file.delete(); + } + await LinuxSelfUpdate.relaunch(executable); + } final dir = Platform.isAndroid ? await getTemporaryDirectory() : (await getDownloadsDirectory() ?? await getTemporaryDirectory()); diff --git a/app/lib/features/updates/launch_update_dialog.dart b/app/lib/features/updates/launch_update_dialog.dart index deb95d9b..23eddfd9 100644 --- a/app/lib/features/updates/launch_update_dialog.dart +++ b/app/lib/features/updates/launch_update_dialog.dart @@ -22,7 +22,11 @@ Future showLaunchUpdateDialog( ? 'macOS' : nativePlatform == 'linux' ? 'Linux' - : 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}', + : 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${switch (nativePlatform) { + 'linux' => ' Pipelines installs it and restarts.', + 'macos' => ' Download and extract the archive to update the app.', + _ => '', + }}', ), actions: [ CupertinoDialogAction( @@ -32,7 +36,13 @@ Future showLaunchUpdateDialog( CupertinoDialogAction( isDefaultAction: true, onPressed: () => Navigator.pop(context, true), - child: Text(web ? 'Refresh' : 'Download'), + child: Text( + web + ? 'Refresh' + : nativePlatform == 'linux' + ? 'Install and restart' + : 'Download', + ), ), ], ), @@ -48,7 +58,11 @@ Future showLaunchUpdateDialog( ? 'macOS' : nativePlatform == 'linux' ? 'Linux' - : 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}', + : 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${switch (nativePlatform) { + 'linux' => ' Pipelines installs it and restarts.', + 'macos' => ' Download and extract the archive to update the app.', + _ => '', + }}', ), actions: [ TextButton( @@ -57,7 +71,13 @@ Future showLaunchUpdateDialog( ), FilledButton( onPressed: () => Navigator.pop(context, true), - child: Text(web ? 'Refresh' : 'Download'), + child: Text( + web + ? 'Refresh' + : nativePlatform == 'linux' + ? 'Install and restart' + : 'Download', + ), ), ], ), diff --git a/app/lib/features/updates/linux_self_update.dart b/app/lib/features/updates/linux_self_update.dart new file mode 100644 index 00000000..3f246572 --- /dev/null +++ b/app/lib/features/updates/linux_self_update.dart @@ -0,0 +1,116 @@ +import 'dart:io'; + +/// Installs a downloaded Linux release archive over the running app. +/// +/// The archive holds one `pipelines-linux-x64/` bundle (executable `pipelines-app`, +/// `data/`, `lib/`). It is extracted beside the install folder, checked, and +/// swapped in with two renames; the previous bundle is kept as +/// `.previous` until the next update so a failed swap rolls back. +/// The running process keeps its already-open files, so it can relaunch the +/// new executable and exit. +class LinuxSelfUpdate { + LinuxSelfUpdate({ + required this.installDir, + this.executableName = 'pipelines-app', + Future Function(String, List)? run, + }) : _run = run ?? ((exe, args) => Process.run(exe, args)); + + /// The folder holding the running executable. + final Directory installDir; + final String executableName; + final Future Function(String, List) _run; + + /// The install folder of this process. + static LinuxSelfUpdate current() => + LinuxSelfUpdate(installDir: File(Platform.resolvedExecutable).parent); + + /// Why this install cannot update itself, or null when it can. + Future problem() async { + final parent = installDir.parent; + try { + final probe = File( + '${parent.path}/.pipelines-update-probe-${DateTime.now().microsecondsSinceEpoch}', + ); + await probe.writeAsString(''); + await probe.delete(); + } on FileSystemException { + return 'Pipelines is installed in ${installDir.path}, which this user ' + 'cannot change. Reinstall it in your home folder to update it from ' + 'the app.'; + } + return null; + } + + /// Extracts [archive] and swaps it in; returns the new executable path. + Future install(File archive, {required int versionCode}) async { + final reason = await problem(); + if (reason != null) throw FileSystemException(reason, installDir.path); + final parent = installDir.parent.path; + final name = installDir.uri.pathSegments.where((s) => s.isNotEmpty).last; + final staging = Directory('$parent/.$name-update-$versionCode'); + if (await staging.exists()) await staging.delete(recursive: true); + await staging.create(); + try { + final result = await _run('tar', [ + '-xzf', + archive.path, + '-C', + staging.path, + ]); + if (result.exitCode != 0) { + throw FileSystemException( + 'The update archive could not be extracted: ${'${result.stderr}'.trim()}', + archive.path, + ); + } + final bundle = await _bundleIn(staging); + final previous = Directory('$parent/$name.previous'); + if (await previous.exists()) await previous.delete(recursive: true); + await installDir.rename(previous.path); + try { + await bundle.rename(installDir.path); + } catch (_) { + await previous.rename(installDir.path); + rethrow; + } + return '${installDir.path}/$executableName'; + } finally { + if (await staging.exists()) await staging.delete(recursive: true); + } + } + + /// The extracted bundle: the folder holding the executable and its data. + Future _bundleIn(Directory staging) async { + final candidates = [ + staging, + ...await staging + .list() + .where((e) => e is Directory) + .cast() + .toList(), + ]; + for (final dir in candidates) { + final exe = File('${dir.path}/$executableName'); + if (await exe.exists() && + await Directory('${dir.path}/data/flutter_assets').exists() && + await Directory('${dir.path}/lib').exists()) { + return dir; + } + } + throw FileSystemException( + 'The update archive has no complete $executableName bundle.', + staging.path, + ); + } + + /// Starts [executable] on its own and ends this process. + static Future relaunch(String executable) async { + await Process.start( + executable, + const [], + mode: ProcessStartMode.detached, + workingDirectory: File(executable).parent.path, + ); + exit(0); + } +} diff --git a/app/test/features/updates/launch_update_test.dart b/app/test/features/updates/launch_update_test.dart index fa2af45a..d49c8751 100644 --- a/app/test/features/updates/launch_update_test.dart +++ b/app/test/features/updates/launch_update_test.dart @@ -5,18 +5,24 @@ import 'package:pipelines_app/features/updates/launch_update.dart'; void main() { test('web compares release identity and ignores Android metadata', () { expect( - webUpdateFor('web-current', - '{"schema":1,"platform":"web","buildId":"web-current"}'), + webUpdateFor( + 'web-current', + '{"schema":1,"platform":"web","buildId":"web-current"}', + ), isNull, ); expect( - webUpdateFor('web-current', - '{"schema":1,"platform":"web","buildId":"web-new"}'), + webUpdateFor( + 'web-current', + '{"schema":1,"platform":"web","buildId":"web-new"}', + ), isA(), ); expect( - webUpdateFor('web-current', - '{"schema":1,"platform":"android","buildId":"web-new"}'), + webUpdateFor( + 'web-current', + '{"schema":1,"platform":"android","buildId":"web-new"}', + ), isNull, ); expect(webUpdateFor('', '{"buildId":"web-new"}'), isNull); diff --git a/app/test/features/updates/linux_self_update_test.dart b/app/test/features/updates/linux_self_update_test.dart new file mode 100644 index 00000000..2fbef23f --- /dev/null +++ b/app/test/features/updates/linux_self_update_test.dart @@ -0,0 +1,115 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/updates/linux_self_update.dart'; + +/// Builds a release-shaped archive: pipelines-linux-x64/{pipelines-app,data/flutter_assets,lib}. +Future _archive( + Directory root, + String marker, { + bool complete = true, +}) async { + final src = Directory('${root.path}/src-$marker/pipelines-linux-x64'); + await Directory('${src.path}/data/flutter_assets').create(recursive: true); + if (complete) await Directory('${src.path}/lib').create(); + await File('${src.path}/pipelines-app').writeAsString(marker); + final out = File('${root.path}/$marker.tar.gz'); + final result = await Process.run('tar', [ + '-czf', + out.path, + '-C', + src.parent.path, + 'pipelines-linux-x64', + ]); + expect(result.exitCode, 0, reason: '${result.stderr}'); + return out; +} + +void main() { + late Directory root; + late Directory install; + + setUp(() async { + root = await Directory.systemTemp.createTemp('linux-self-update'); + install = Directory('${root.path}/apps/pipelines'); + await Directory( + '${install.path}/data/flutter_assets', + ).create(recursive: true); + await Directory('${install.path}/lib').create(); + await File('${install.path}/pipelines-app').writeAsString('old'); + }); + tearDown(() => root.delete(recursive: true)); + + test('swaps in the new bundle and keeps the previous one', () async { + final updater = LinuxSelfUpdate(installDir: install); + expect(await updater.problem(), isNull); + final exe = await updater.install( + await _archive(root, 'new'), + versionCode: 7, + ); + expect(exe, '${install.path}/pipelines-app'); + expect(await File(exe).readAsString(), 'new'); + expect( + await File( + '${root.path}/apps/pipelines.previous/pipelines-app', + ).readAsString(), + 'old', + ); + // No staging folder is left behind. + expect( + Directory('${root.path}/apps').listSync().map( + (e) => e.uri.pathSegments.where((s) => s.isNotEmpty).last, + ), + unorderedEquals(['pipelines', 'pipelines.previous']), + ); + // A second update replaces the kept previous bundle. + await updater.install(await _archive(root, 'newer'), versionCode: 8); + expect(await File(exe).readAsString(), 'newer'); + expect( + await File( + '${root.path}/apps/pipelines.previous/pipelines-app', + ).readAsString(), + 'new', + ); + }); + + test('an incomplete archive leaves the installed app untouched', () async { + final updater = LinuxSelfUpdate(installDir: install); + await expectLater( + updater.install( + await _archive(root, 'broken', complete: false), + versionCode: 9, + ), + throwsA(isA()), + ); + expect(await File('${install.path}/pipelines-app').readAsString(), 'old'); + expect( + Directory('${root.path}/apps/pipelines.previous').existsSync(), + isFalse, + ); + }); + + test('a corrupt archive is reported, not installed', () async { + final updater = LinuxSelfUpdate(installDir: install); + final junk = File('${root.path}/junk.tar.gz')..writeAsStringSync('nope'); + await expectLater( + updater.install(junk, versionCode: 10), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('could not be extracted'), + ), + ), + ); + expect(await File('${install.path}/pipelines-app').readAsString(), 'old'); + }); + + test('a read-only install location explains how to fix it', () async { + final apps = Directory('${root.path}/apps'); + await Process.run('chmod', ['555', apps.path]); + addTearDown(() => Process.run('chmod', ['755', apps.path])); + final reason = await LinuxSelfUpdate(installDir: install).problem(); + expect(reason, contains('Reinstall it in your home folder')); + }); +} diff --git a/app/test/features/updates/native_update_dialog_test.dart b/app/test/features/updates/native_update_dialog_test.dart index 68790c99..7216640f 100644 --- a/app/test/features/updates/native_update_dialog_test.dart +++ b/app/test/features/updates/native_update_dialog_test.dart @@ -3,30 +3,35 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/updates/launch_update_dialog.dart'; void main() { - testWidgets('desktop prompt identifies source and archive action', ( - tester, - ) async { - await tester.pumpWidget( - MaterialApp( - home: Builder( - builder: (context) => TextButton( - onPressed: () => showLaunchUpdateDialog( - context, - web: false, - cupertino: false, - nativePlatform: 'linux', - sourceName: 'Home Linux', + for (final (platform, sentence, action) in [ + ('linux', 'installs it and restarts', 'Install and restart'), + ('macos', 'extract the archive', 'Download'), + ]) { + testWidgets('$platform prompt identifies source and its action', ( + tester, + ) async { + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (context) => TextButton( + onPressed: () => showLaunchUpdateDialog( + context, + web: false, + cupertino: false, + nativePlatform: platform, + sourceName: 'Home Linux', + ), + child: const Text('Check'), ), - child: const Text('Check'), ), ), - ), - ); - await tester.tap(find.text('Check')); - await tester.pumpAndSettle(); - expect(find.textContaining('from Home Linux'), findsOneWidget); - expect(find.textContaining('extract the archive'), findsOneWidget); - expect(find.text('Download'), findsOneWidget); - expect(find.textContaining('Android'), findsNothing); - }); + ); + await tester.tap(find.text('Check')); + await tester.pumpAndSettle(); + expect(find.textContaining('from Home Linux'), findsOneWidget); + expect(find.textContaining(sentence), findsOneWidget); + expect(find.text(action), findsOneWidget); + expect(find.textContaining('Android'), findsNothing); + }); + } }