fileMirror function

File fileMirror(
  1. File srcFile,
  2. File dstFile
)

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) {
  final newPath = dstFile.path;
  if (!dstFile.existsSync()) {
    dstFile.createSync(recursive: true);
    return srcFile.copySync(newPath);
  }

  if ((srcFile.lengthSync() != dstFile.lengthSync()) ||
      (srcFile.lastModifiedSync() != dstFile.lastModifiedSync())) {
    return srcFile.copySync(newPath);
  }

  final srcCrc64 = getCrc64(srcFile.readAsBytesSync());
  final dstCrc64 = getCrc64(dstFile.readAsBytesSync());
  if (srcCrc64 != dstCrc64) return srcFile.copySync(newPath);

  return dstFile;
}