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 <install>.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".
This commit is contained in:
2026-11-09 19:40:52 +00:00
parent 15939fcf09
commit 277c691b2c
7 changed files with 320 additions and 34 deletions
@@ -155,7 +155,9 @@ class _SettingsUpdatesSectionState extends State<SettingsUpdatesSection> {
if (_available != null)
button(
const Key('settings-install-update'),
'Download update',
_available!.platform == 'linux'
? 'Install and restart'
: 'Download update',
_busy ? null : _install,
true,
),
@@ -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<void> 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());
@@ -22,7 +22,11 @@ Future<bool?> 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<bool?> 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<bool?> 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<bool?> showLaunchUpdateDialog(
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: Text(web ? 'Refresh' : 'Download'),
child: Text(
web
? 'Refresh'
: nativePlatform == 'linux'
? 'Install and restart'
: 'Download',
),
),
],
),
@@ -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
/// `<install>.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<ProcessResult> Function(String, List<String>)? run,
}) : _run = run ?? ((exe, args) => Process.run(exe, args));
/// The folder holding the running executable.
final Directory installDir;
final String executableName;
final Future<ProcessResult> Function(String, List<String>) _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<String?> 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<String> 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<Directory> _bundleIn(Directory staging) async {
final candidates = [
staging,
...await staging
.list()
.where((e) => e is Directory)
.cast<Directory>()
.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<Never> relaunch(String executable) async {
await Process.start(
executable,
const [],
mode: ProcessStartMode.detached,
workingDirectory: File(executable).parent.path,
);
exit(0);
}
}
@@ -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<WebLaunchUpdate>(),
);
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);
@@ -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<File> _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<FileSystemException>()),
);
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<FileSystemException>().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'));
});
}
@@ -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);
});
}
}