From f0a3cbace3920c1ab29ea0ee900ca89c89baf726 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 25 Aug 2020 10:58:21 -0700 Subject: [PATCH] [flutter_tools] handle FileSystemException thrown when attempting to decode stamp file (#64559) Fixes #64246 If decoding the string contents of a stamp file fails, treat it as if the stamp file is missing. This is always safe to do, since at worst it downloads more artifacts. The files could end up corrupted if they were edited by another program, for example --- packages/flutter_tools/lib/src/cache.dart | 12 ++++- .../test/general.shard/cache_test.dart | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index a9ac3513c5..5f6cc4f3ca 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -399,9 +399,19 @@ class Cache { } } + /// Read the stamp for [artifactName]. + /// + /// If the file is missing or cannot be parsed, returns `null`. String getStampFor(String artifactName) { final File stampFile = getStampFileFor(artifactName); - return stampFile.existsSync() ? stampFile.readAsStringSync().trim() : null; + if (!stampFile.existsSync()) { + return null; + } + try { + return stampFile.readAsStringSync().trim(); + } on FileSystemException { + return null; + } } void setStampFor(String artifactName, String version) { diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 1ab020853a..111bdc4cb2 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -634,6 +634,32 @@ void main() { expect(logger.errorText, contains('Failed to delete some stamp files')); }); + + testWithoutContext('Cache handles exception thrown if stamp file cannot be parsed', () { + final FileSystem fileSystem = MemoryFileSystem.test(); + final Logger logger = BufferLogger.test(); + final FakeCache cache = FakeCache( + fileSystem: fileSystem, + logger: logger, + platform: FakePlatform(), + osUtils: MockOperatingSystemUtils() + ); + final MockFile file = MockFile(); + cache.stampFile = file; + when(file.existsSync()).thenReturn(false); + + expect(cache.getStampFor('foo'), null); + + when(file.existsSync()).thenReturn(true); + when(file.readAsStringSync()).thenThrow(const FileSystemException()); + + expect(cache.getStampFor('foo'), null); + + when(file.existsSync()).thenReturn(true); + when(file.readAsStringSync()).thenReturn('ABC '); + + expect(cache.getStampFor('foo'), 'ABC'); + }); } class FakeCachedArtifact extends EngineCachedArtifact { @@ -698,3 +724,24 @@ class MockInternetAddress extends Mock implements InternetAddress {} class MockCache extends Mock implements Cache {} class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} class MockVersionedPackageResolver extends Mock implements VersionedPackageResolver {} +class FakeCache extends Cache { + FakeCache({ + @required Logger logger, + @required FileSystem fileSystem, + @required Platform platform, + @required OperatingSystemUtils osUtils, + }) : super( + logger: logger, + fileSystem: fileSystem, + platform: platform, + osUtils: osUtils, + artifacts: [], + ); + + File stampFile; + + @override + File getStampFileFor(String artifactName) { + return stampFile; + } +}