fileMirror function
Efficiently mirrors srcFile content to dstFile, copying only if necessary.
Compares size, modification time, and CRC64 checksums.
Creates dstFile (and its parent directories) if it doesn't exist.
Implementation
File fileMirror(File srcFile, File dstFile, [bool keepMtime = false]) {
final srcModified = srcFile.lastModifiedSync();
final newPath = dstFile.path;
if (!dstFile.existsSync()) {
dstFile.createSync(recursive: true);
final copied = srcFile.copySync(newPath);
if (keepMtime) copied.setLastModifiedSync(srcModified);
return copied;
}
if ((srcFile.lengthSync() != dstFile.lengthSync()) ||
(srcModified != dstFile.lastModifiedSync())) {
final copied = srcFile.copySync(newPath);
if (keepMtime) copied.setLastModifiedSync(srcModified);
return copied;
}
final srcCrc64 = getCrc64(srcFile.readAsBytesSync());
final dstCrc64 = getCrc64(dstFile.readAsBytesSync());
if (srcCrc64 != dstCrc64) {
final copied = srcFile.copySync(newPath);
if (keepMtime) copied.setLastModifiedSync(srcModified);
return copied;
}
return dstFile;
}