Skip to content

Commit 5ca63a2

Browse files
authored
Add timeout to tar command and exit if it doesn't succeed. (#156)
* Add timeout to tar command and exit if it doesn't succeed. Also remove the verbose option of the tar file to improve speed. Bug: flutter/flutter#57176 * Add tests and reduce timeout to 5 mins.
1 parent c41258e commit 5ca63a2

4 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/fuchsia_ctl/bin/main.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,12 @@ Future<OperationResult> pushPackages(
262262

263263
stdout.writeln('Untaring $repoArchive to ${repo.path}');
264264
repo.createSync(recursive: true);
265-
await tar.untar(repoArchive, repo.path);
265+
final OperationResult result = await tar.untar(repoArchive, repo.path);
266+
if (!result.success) {
267+
stdout.writeln(
268+
'Error untarring $repoArchive \nstdout: ${result.info} \nstderr: ${result.error}');
269+
exit(-1);
270+
}
266271

267272
final String repositoryBase = path.join(repo.path, 'amber-files');
268273
stdout.writeln('Serving $repositoryBase to $targetIp');

‎packages/fuchsia_ctl/lib/src/tar.dart‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ abstract class Tar {
1717
const Tar();
1818

1919
/// Untars a tar file.
20-
Future<OperationResult> untar(String src, String destination);
20+
Future<OperationResult> untar(
21+
String src,
22+
String destination, {
23+
Duration timeoutMs,
24+
});
2125
}
2226

2327
/// The archive package is very slow and memory intensive. Use
@@ -35,16 +39,21 @@ class SystemTar implements Tar {
3539
/// program.
3640
final ProcessManager processManager;
3741

42+
/// The default timeout for untar operations as [Duration] in milliseconds.
43+
static const Duration defaultTarTimeoutMs =
44+
Duration(milliseconds: 5 * 60 * 1000);
45+
3846
@override
3947
Future<OperationResult> untar(
4048
String src,
41-
String destination,
42-
) async {
49+
String destination, {
50+
Duration timeoutMs = defaultTarTimeoutMs,
51+
}) async {
4352
final ProcessResult result = await processManager.run(<String>[
4453
'tar',
45-
'-xvf', src, //
54+
'-xf', src, //
4655
'-C', destination,
47-
]);
56+
]).timeout(timeoutMs);
4857

4958
return OperationResult.fromProcessResult(result);
5059
}

‎packages/fuchsia_ctl/test/image_paver_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ class FakeTar implements Tar {
158158
final MemoryFileSystem fs;
159159

160160
@override
161-
Future<OperationResult> untar(String src, String destination) async {
161+
Future<OperationResult> untar(String src, String destination,
162+
{Duration timeoutMs}) async {
162163
if (passes) {
163164
final Directory dir = fs.directory(destination)
164165
..createSync(recursive: true);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2020 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
8+
import 'package:process/process.dart';
9+
import 'package:test/test.dart';
10+
import 'package:fuchsia_ctl/src/tar.dart';
11+
import 'package:mockito/mockito.dart';
12+
13+
void main() {
14+
group('Tar', () {
15+
test('Untar file times out', () async {
16+
final MockProcessManager processManager = MockProcessManager();
17+
18+
when(processManager.run(any)).thenAnswer((_) async {
19+
await Future<void>.delayed(const Duration(milliseconds: 3));
20+
return ProcessResult(0, 0, 'Good job', '');
21+
});
22+
23+
final Tar tar = SystemTar(processManager: processManager);
24+
expect(
25+
tar.untar(
26+
'source.tar',
27+
'/destination',
28+
timeoutMs: const Duration(milliseconds: 1),
29+
),
30+
throwsA(const TypeMatcher<TimeoutException>()));
31+
});
32+
});
33+
}
34+
35+
class MockProcessManager extends Mock implements ProcessManager {}

0 commit comments

Comments
 (0)