Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions lib/src/cross/package_stager.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';

import 'package:emb_cli/src/cross/process_runner.dart';
Expand Down Expand Up @@ -47,22 +48,68 @@ abstract class PackageStager {
final root = Directory(p.join(outDir.path, '$packageName.stage'));
if (root.existsSync()) root.deleteSync(recursive: true);

String stagedPath(String target) => p.join(root.path, target.substring(1));

Future<void> chmod(String mode, List<String> paths) async {
final result = await _run('chmod', ['--', mode, ...paths]);
if (result.exitCode != 0) {
fail('chmod failed (exit ${result.exitCode}): ${result.stderr.trim()}');
}
}

// Install the binary at the requested path inside the staging root.
final dest = File(p.join(root.path, installPath.substring(1)))
final dest = File(stagedPath(installPath))
..parent.createSync(recursive: true);
binary.copySync(dest.path);
await _run('chmod', ['0755', dest.path]);
await chmod('0755', [dest.path]);

// Stage any extra files at their absolute target paths inside the root,
// applying an explicit mode when given.
// Repeated destinations must retain their original copy/chmod order.
final seenDestinations = <String>{};
final repeatedDestinations = <String>{};
for (final target in extraFiles.values) {
final staged = stagedPath(target);
if (!seenDestinations.add(staged)) repeatedDestinations.add(staged);
}

// Stage extra files, collecting unique destinations by requested mode.
final pathsByMode = <String, List<String>>{};
for (final entry in extraFiles.entries) {
final src = File(entry.key);
if (!src.existsSync()) fail('extra file not found: ${entry.key}');
final to = File(p.join(root.path, entry.value.substring(1)))
final to = File(stagedPath(entry.value))
..parent.createSync(recursive: true);
src.copySync(to.path);
final mode = fileModes[entry.key];
if (mode != null) await _run('chmod', [mode, to.path]);
if (mode != null) {
if (repeatedDestinations.contains(to.path)) {
await chmod(mode, [to.path]);
} else {
pathsByMode.putIfAbsent(mode, () => []).add(to.path);
}
}
}
// Bound both the path count and argv bytes; hundreds of asset files should
// not cause hundreds of processes or exceed a platform's argument limit.
const maxPathsPerCall = 1024;
const maxArgumentBytes = 96 * 1024;
for (final entry in pathsByMode.entries) {
final mode = entry.key;
final initialArgumentBytes = 3 + utf8.encode(mode).length + 1;
var paths = <String>[];
var argumentBytes = initialArgumentBytes;
for (final path in entry.value) {
final pathBytes = utf8.encode(path).length + 1;
if (paths.isNotEmpty &&
(paths.length >= maxPathsPerCall ||
argumentBytes + pathBytes > maxArgumentBytes)) {
await chmod(mode, paths);
paths = <String>[];
argumentBytes = initialArgumentBytes;
}
paths.add(path);
argumentBytes += pathBytes;
}
if (paths.isNotEmpty) await chmod(mode, paths);
}
return root;
}
Expand Down
2 changes: 1 addition & 1 deletion test/src/cross/deb_packager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ lrwxrwxrwx root/root 0 2024-01-01 ./usr/lib/aarch64-linux-gnu/libgbm.so.1 -> lib
// The staged extra file was chmod 0755'd (separate from the binary's).
expect(
chmodArgs.any(
(a) => a.startsWith('0755 ') && a.endsWith('usr/bin/helper'),
(a) => a.startsWith('-- 0755 ') && a.endsWith('usr/bin/helper'),
),
isTrue,
);
Expand Down
227 changes: 227 additions & 0 deletions test/src/cross/package_stager_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import 'dart:convert';
import 'dart:io';

import 'package:emb_cli/src/cross/package_stager.dart';
import 'package:emb_cli/src/cross/process_runner.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

class _TestStager extends PackageStager {
_TestStager(super.run);

@override
Never fail(String message) => throw StateError(message);
}

void main() {
late Directory tmp;
setUp(() => tmp = Directory.systemTemp.createTempSync('emb_stage_'));
tearDown(() => tmp.deleteSync(recursive: true));

test(
'stages many extra files with bounded chmod calls and correct modes',
() async {
final chmodCalls = <List<String>>[];
Future<RunResult> fakeRun(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessOutputMode output = ProcessOutputMode.capture,
String? label,
}) async {
if (executable == 'chmod') chmodCalls.add(List.of(arguments));
return const RunResult(0, '', '');
}

final binary = File(p.join(tmp.path, 'app'))..writeAsStringSync('app');
final extras = <String, String>{};
final modes = <String, String>{};
final contents = <String, String>{};
final deepDir = 'x'.padRight(160, 'x');
for (var i = 0; i < 960; i++) {
final source = File(p.join(tmp.path, 'source', 'file $i.txt'))
..createSync(recursive: true)
..writeAsStringSync('$i');
extras[source.path] = '/usr/share/emb/$deepDir/file $i.txt';
modes[source.path] = i.isEven ? '0644' : '0755';
contents[source.path] = '$i';
}
final unmoded = File(p.join(tmp.path, 'source', 'unmoded.txt'))
..writeAsStringSync('keep');
extras[unmoded.path] = '/usr/share/emb/unmoded.txt';
contents[unmoded.path] = 'keep';

final root = await _TestStager(fakeRun).stagePayload(
binary: binary,
installPath: '/usr/bin/app',
packageName: 'example',
outDir: Directory(p.join(tmp.path, 'out')),
extraFiles: extras,
fileModes: modes,
);

expect(chmodCalls.first, [
'--',
'0755',
p.join(root.path, 'usr/bin/app'),
]);
final extraCalls = chmodCalls.skip(1).toList();
expect(extraCalls.length, lessThan(12));

final expected = <String, Set<String>>{'0644': {}, '0755': {}};
for (final entry in extras.entries) {
final staged = p.join(root.path, entry.value.substring(1));
expect(File(staged).readAsStringSync(), contents[entry.key]);
final mode = modes[entry.key];
if (mode != null) expected[mode]!.add(staged);
}

final actual = <String, Set<String>>{'0644': {}, '0755': {}};
var pathCount = 0;
for (final args in extraCalls) {
expect(args.length, greaterThan(2));
expect(
utf8.encode(args.join('\u0000')).length + 1,
lessThanOrEqualTo(100 * 1024),
);
expect(args.first, '--');
final mode = args[1];
expect(actual, contains(mode));
actual[mode]!.addAll(args.skip(2));
pathCount += args.length - 2;
}
expect(pathCount, 960);
expect(actual, expected);
},
);

test('keeps a 2000-file single-mode package to a few chmod calls', () async {
final chmodCalls = <List<String>>[];
Future<RunResult> fakeRun(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessOutputMode output = ProcessOutputMode.capture,
String? label,
}) async {
if (executable == 'chmod') chmodCalls.add(List.of(arguments));
return const RunResult(0, '', '');
}

final binary = File(p.join(tmp.path, 'app'))..writeAsStringSync('app');
final extras = <String, String>{};
final modes = <String, String>{};
for (var i = 0; i < 2000; i++) {
final source = File(p.join(tmp.path, 'source', 'f$i'))
..createSync(recursive: true)
..writeAsStringSync('$i');
extras[source.path] = '/assets/f$i';
modes[source.path] = '0644';
}

final root = await _TestStager(fakeRun).stagePayload(
binary: binary,
installPath: '/usr/bin/app',
packageName: 'example',
outDir: Directory(p.join(tmp.path, 'out')),
extraFiles: extras,
fileModes: modes,
);
final extraCalls = chmodCalls.skip(1).toList();
expect(extraCalls.length, lessThanOrEqualTo(3));
for (final args in extraCalls) {
expect(args.take(2).toList(), ['--', '0644']);
}
expect(extraCalls.expand((args) => args.skip(2)).toSet(), {
for (final dest in extras.values) p.join(root.path, dest.substring(1)),
});
});

test('preserves chmod order when sources share a target path', () async {
final calls = <List<String>>[];
Future<RunResult> fakeRun(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessOutputMode output = ProcessOutputMode.capture,
String? label,
}) async {
if (executable == 'chmod') calls.add(List.of(arguments));
return const RunResult(0, '', '');
}

final binary = File(p.join(tmp.path, 'app'))..writeAsStringSync('app');
final first = File(p.join(tmp.path, 'first'))..writeAsStringSync('a');
final second = File(p.join(tmp.path, 'second'))..writeAsStringSync('b');
final third = File(p.join(tmp.path, 'third'))..writeAsStringSync('c');
final root = await _TestStager(fakeRun).stagePayload(
binary: binary,
installPath: '/usr/bin/app',
packageName: 'example',
outDir: Directory(p.join(tmp.path, 'out')),
extraFiles: {
first.path: '/share/repeated',
second.path: '/share/repeated',
third.path: '/share/repeated',
},
fileModes: {first.path: '0600', second.path: '0644'},
);
final target = p.join(root.path, 'share/repeated');
expect(File(target).readAsStringSync(), 'c');
expect(calls, [
['--', '0755', p.join(root.path, 'usr/bin/app')],
['--', '0600', target],
['--', '0644', target],
]);
});

test('passes -- before a mode and reports a batched chmod failure', () async {
final calls = <List<String>>[];
Future<RunResult> fakeRun(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessOutputMode output = ProcessOutputMode.capture,
String? label,
}) async {
if (executable == 'chmod') calls.add(List.of(arguments));
if (arguments.length > 1 && arguments[1].startsWith('--reference=')) {
return const RunResult(1, '', 'invalid mode');
}
return const RunResult(0, '', '');
}

final binary = File(p.join(tmp.path, 'app'))..writeAsStringSync('app');
final extra = File(p.join(tmp.path, 'extra'))..writeAsStringSync('extra');
await expectLater(
_TestStager(fakeRun).stagePayload(
binary: binary,
installPath: '/usr/bin/app',
packageName: 'example',
outDir: Directory(p.join(tmp.path, 'out')),
extraFiles: {extra.path: '/share/extra'},
fileModes: {extra.path: '--reference=unexpected'},
),
throwsA(
isA<StateError>().having(
(e) => e.toString(),
'message',
contains('chmod failed (exit 1): invalid mode'),
),
),
);
expect(calls.last.take(2).toList(), ['--', '--reference=unexpected']);
});
}
Loading