1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:collection';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:meta/meta.dart';
import 'package:pool/pool.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/utils.dart';
import '../convert.dart';
import 'build_system.dart';
/// The default threshold for file chunking is 250 KB, or about the size of `framework.dart`.
const int kDefaultFileChunkThresholdBytes = 250000;
/// An encoded representation of all file hashes.
class FileStorage {
FileStorage(this.version, this.files);
factory FileStorage.fromBuffer(Uint8List buffer) {
final Map<String, dynamic> json = castStringKeyedMap(jsonDecode(utf8.decode(buffer)));
final int version = json['version'] as int;
final List<Map<String, Object>> rawCachedFiles = (json['files'] as List<dynamic>).cast<Map<String, Object>>();
final List<FileHash> cachedFiles = <FileHash>[
for (final Map<String, Object> rawFile in rawCachedFiles) FileHash.fromJson(rawFile),
];
return FileStorage(version, cachedFiles);
}
final int version;
final List<FileHash> files;
List<int> toBuffer() {
final Map<String, Object> json = <String, Object>{
'version': version,
'files': <Object>[
for (final FileHash file in files) file.toJson(),
],
};
return utf8.encode(jsonEncode(json));
}
}
/// A stored file hash and path.
class FileHash {
FileHash(this.path, this.hash);
factory FileHash.fromJson(Map<String, Object> json) {
return FileHash(json['path'] as String, json['hash'] as String);
}
final String path;
final String hash;
Object toJson() {
return <String, Object>{
'path': path,
'hash': hash,
};
}
}
/// The strategy used by [FileStore] to determine if a file has been
/// invalidated.
enum FileStoreStrategy {
/// The [FileStore] will compute an md5 hash of the file contents.
hash,
/// The [FileStore] will check for differences in the file's last modified
/// timestamp.
timestamp,
}
/// A globally accessible cache of files.
///
/// In cases where multiple targets read the same source files as inputs, we
/// avoid recomputing or storing multiple copies of hashes by delegating
/// through this class.
///
/// This class uses either timestamps or file hashes depending on the
/// provided [FileStoreStrategy]. All information is held in memory during
/// a build operation, and may be persisted to cache in the root build
/// directory.
///
/// The format of the file store is subject to change and not part of its API.
class FileStore {
FileStore({
@required File cacheFile,
@required Logger logger,
FileStoreStrategy strategy = FileStoreStrategy.hash,
int fileChunkThreshold = kDefaultFileChunkThresholdBytes,
}) : _logger = logger,
_strategy = strategy,
_cacheFile = cacheFile,
_fileChunkThreshold = fileChunkThreshold;
final File _cacheFile;
final Logger _logger;
final FileStoreStrategy _strategy;
final int _fileChunkThreshold;
final HashMap<String, String> previousAssetKeys = HashMap<String, String>();
final HashMap<String, String> currentAssetKeys = HashMap<String, String>();
// The name of the file which stores the file hashes.
static const String kFileCache = '.filecache';
// The current version of the file cache storage format.
static const int _kVersion = 2;
/// Read file hashes from disk.
void initialize() {
_logger.printTrace('Initializing file store');
if (!_cacheFile.existsSync()) {
return;
}
Uint8List data;
try {
data = _cacheFile.readAsBytesSync();
} on FileSystemException catch (err) {
_logger.printError(
'Failed to read file store at ${_cacheFile.path} due to $err.\n'
'Build artifacts will not be cached. Try clearing the cache directories '
'with "flutter clean"',
);
return;
}
FileStorage fileStorage;
try {
fileStorage = FileStorage.fromBuffer(data);
} on Exception catch (err) {
_logger.printTrace('Filestorage format changed: $err');
_cacheFile.deleteSync();
return;
}
if (fileStorage.version != _kVersion) {
_logger.printTrace('file cache format updating, clearing old hashes.');
_cacheFile.deleteSync();
return;
}
for (final FileHash fileHash in fileStorage.files) {
previousAssetKeys[fileHash.path] = fileHash.hash;
}
_logger.printTrace('Done initializing file store');
}
/// Persist file marks to disk for a non-incremental build.
void persist() {
_logger.printTrace('Persisting file store');
if (!_cacheFile.existsSync()) {
_cacheFile.createSync(recursive: true);
}
final List<FileHash> fileHashes = <FileHash>[];
for (final MapEntry<String, String> entry in currentAssetKeys.entries) {
fileHashes.add(FileHash(entry.key, entry.value));
}
final FileStorage fileStorage = FileStorage(
_kVersion,
fileHashes,
);
final List<int> buffer = fileStorage.toBuffer();
try {
_cacheFile.writeAsBytesSync(buffer);
} on FileSystemException catch (err) {
_logger.printError(
'Failed to persist file store at ${_cacheFile.path} due to $err.\n'
'Build artifacts will not be cached. Try clearing the cache directories '
'with "flutter clean"',
);
}
_logger.printTrace('Done persisting file store');
}
/// Reset `previousMarks` for an incremental build.
void persistIncremental() {
previousAssetKeys.clear();
previousAssetKeys.addAll(currentAssetKeys);
currentAssetKeys.clear();
}
/// Computes a diff of the provided files and returns a list of files
/// that were dirty.
Future<List<File>> diffFileList(List<File> files) async {
final List<File> dirty = <File>[];
switch (_strategy) {
case FileStoreStrategy.hash:
final Pool openFiles = Pool(kMaxOpenFiles);
await Future.wait(<Future<void>>[
for (final File file in files) _hashFile(file, dirty, openFiles)
]);
break;
case FileStoreStrategy.timestamp:
for (final File file in files) {
_checkModification(file, dirty);
}
break;
}
return dirty;
}
void _checkModification(File file, List<File> dirty) {
final String absolutePath = file.path;
final String previousTime = previousAssetKeys[absolutePath];
// If the file is missing it is assumed to be dirty.
if (!file.existsSync()) {
currentAssetKeys.remove(absolutePath);
previousAssetKeys.remove(absolutePath);
dirty.add(file);
return;
}
final String modifiedTime = file.lastModifiedSync().toString();
if (modifiedTime != previousTime) {
dirty.add(file);
}
currentAssetKeys[absolutePath] = modifiedTime;
}
Future<void> _hashFile(File file, List<File> dirty, Pool pool) async {
final PoolResource resource = await pool.request();
try {
final String absolutePath = file.path;
final String previousHash = previousAssetKeys[absolutePath];
// If the file is missing it is assumed to be dirty.
if (!file.existsSync()) {
currentAssetKeys.remove(absolutePath);
previousAssetKeys.remove(absolutePath);
dirty.add(file);
return;
}
Digest digest;
final int fileBytes = file.lengthSync();
// For files larger than a given threshold, chunk the conversion.
if (fileBytes > _fileChunkThreshold) {
final StreamController<Digest> digests = StreamController<Digest>();
final ByteConversionSink inputSink = md5.startChunkedConversion(digests);
await file.openRead().forEach(inputSink.add);
inputSink.close();
digest = await digests.stream.last;
} else {
digest = md5.convert(await file.readAsBytes());
}
final String currentHash = digest.toString();
if (currentHash != previousHash) {
dirty.add(file);
}
currentAssetKeys[absolutePath] = currentHash;
} finally {
resource.release();
}
}
}