Replace rsync when unzipping artifacts on a Mac (#126703)

Instead of using rsync, which has caused errors in the past (https://github.com/flutter/flutter/issues/99785), delete the file/directory/link prior to moving it.

Hopefully should let us stop double zipping the FlutterMacOS.framework in the engine: https://github.com/flutter/engine/pull/41306/files

Part of https://github.com/flutter/flutter/issues/126016.
This commit is contained in:
Victoria Ashworth
2023-05-16 11:58:06 -05:00
committed by GitHub
parent a3b38aa8f3
commit 2b3cd7f4d9
2 changed files with 121 additions and 65 deletions

View File

@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'common.dart';
import 'error_handling_io.dart';
import 'file_system.dart';
import 'io.dart';
import 'logger.dart';
@@ -423,7 +424,11 @@ class _MacOSUtils extends _PosixUtils {
return _hostPlatform!;
}
// unzip, then rsync
/// Unzip into a temporary directory.
///
/// For every file/directory/link in the unzipped file, delete the
/// corresponding entity in the [targetDirectory] before moving from the
/// temporary directory to the [targetDirectory].
@override
void unzip(File file, Directory targetDirectory) {
if (!_processManager.canRun('unzip')) {
@@ -431,35 +436,35 @@ class _MacOSUtils extends _PosixUtils {
// error in bin/internal/update_dart_sdk.sh
throwToolExit('Missing "unzip" tool. Unable to extract ${file.path}.\nConsider running "brew install unzip".');
}
if (_processManager.canRun('rsync')) {
final Directory tempDirectory = _fileSystem.systemTempDirectory.createTempSync('flutter_${file.basename}.');
try {
// Unzip to a temporary directory.
_processUtils.runSync(
<String>['unzip', '-o', '-q', file.path, '-d', tempDirectory.path],
throwOnError: true,
verboseExceptions: true,
);
for (final FileSystemEntity unzippedFile in tempDirectory.listSync(followLinks: false)) {
// rsync --delete the unzipped files so files removed from the archive are also removed from the target.
// Add the '-8' parameter to avoid mangling filenames with encodings that do not match the current locale.
_processUtils.runSync(
<String>['rsync', '-8', '-av', '--delete', unzippedFile.path, targetDirectory.path],
throwOnError: true,
verboseExceptions: true,
);
}
} finally {
tempDirectory.deleteSync(recursive: true);
}
} else {
// Fall back to just unzipping.
_logger.printTrace('Unable to find rsync, falling back to direct unzipping.');
final Directory tempDirectory = _fileSystem.systemTempDirectory.createTempSync('flutter_${file.basename}.');
try {
// Unzip to a temporary directory.
_processUtils.runSync(
<String>['unzip', '-o', '-q', file.path, '-d', targetDirectory.path],
<String>['unzip', '-o', '-q', file.path, '-d', tempDirectory.path],
throwOnError: true,
verboseExceptions: true,
);
for (final FileSystemEntity unzippedFile in tempDirectory.listSync(followLinks: false)) {
final FileSystemEntityType fileType = targetDirectory.fileSystem.typeSync(
targetDirectory.fileSystem.path.join(targetDirectory.path, unzippedFile.basename),
followLinks: false,
);
final FileSystemEntity fileToReplace;
if (fileType == FileSystemEntityType.directory) {
fileToReplace = targetDirectory.childDirectory(unzippedFile.basename);
} else if (fileType == FileSystemEntityType.link) {
fileToReplace = targetDirectory.childLink(unzippedFile.basename);
} else {
fileToReplace = targetDirectory.childFile(unzippedFile.basename);
}
// Delete existing version before moving.
ErrorHandlingFileSystem.deleteIfExists(fileToReplace, recursive: true);
unzippedFile.renameSync(fileToReplace.path);
}
} on FileSystemException catch (e) {
_logger.printTrace('${e.message}: ${e.osError}');
} finally {
tempDirectory.deleteSync(recursive: true);
}
}
}