os.dart 14.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:archive/archive.dart';
6 7
import 'package:file/file.dart';
import 'package:meta/meta.dart';
8
import 'package:process/process.dart';
9

10
import 'common.dart';
11
import 'file_system.dart';
12
import 'io.dart';
13
import 'logger.dart';
14
import 'platform.dart';
15
import 'process.dart';
16

17
abstract class OperatingSystemUtils {
18
  factory OperatingSystemUtils({
19 20 21 22
    required FileSystem fileSystem,
    required Logger logger,
    required Platform platform,
    required ProcessManager processManager,
23 24 25 26 27 28 29 30
  }) {
    if (platform.isWindows) {
      return _WindowsUtils(
        fileSystem: fileSystem,
        logger: logger,
        platform: platform,
        processManager: processManager,
      );
31 32 33 34 35 36 37
    } else if (platform.isMacOS) {
      return _MacOSUtils(
        fileSystem: fileSystem,
        logger: logger,
        platform: platform,
        processManager: processManager,
      );
38
    } else {
39 40 41 42 43 44
      return _PosixUtils(
        fileSystem: fileSystem,
        logger: logger,
        platform: platform,
        processManager: processManager,
      );
45 46 47
    }
  }

48
  OperatingSystemUtils._private({
49 50 51 52
    required FileSystem fileSystem,
    required Logger logger,
    required Platform platform,
    required ProcessManager processManager,
53 54 55 56 57 58 59 60 61
  }) : _fileSystem = fileSystem,
       _logger = logger,
       _platform = platform,
       _processManager = processManager,
       _processUtils = ProcessUtils(
        logger: logger,
        processManager: processManager,
      );

62 63 64
  @visibleForTesting
  static final GZipCodec gzipLevel1 = GZipCodec(level: 1);

65 66 67 68 69
  final FileSystem _fileSystem;
  final Logger _logger;
  final Platform _platform;
  final ProcessManager _processManager;
  final ProcessUtils _processUtils;
70

71
  /// Make the given file executable. This may be a no-op on some platforms.
72 73 74 75 76 77 78 79 80
  void makeExecutable(File file);

  /// Updates the specified file system [entity] to have the file mode
  /// bits set to the value defined by [mode], which can be specified in octal
  /// (e.g. `644`) or symbolically (e.g. `u+x`).
  ///
  /// On operating systems that do not support file mode bits, this will be a
  /// no-op.
  void chmod(FileSystemEntity entity, String mode);
81

82
  /// Return the path (with symlinks resolved) to the given executable, or null
83
  /// if `which` was not able to locate the binary.
84
  File? which(String execName) {
85
    final List<File> result = _which(execName);
86
    if (result == null || result.isEmpty) {
87
      return null;
88
    }
89 90
    return result.first;
  }
91

92 93
  /// Return a list of all paths to `execName` found on the system. Uses the
  /// PATH environment variable.
94
  List<File> whichAll(String execName) => _which(execName, all: true);
95

96 97 98
  /// Return the File representing a new pipe.
  File makePipe(String path);

99
  void unzip(File file, Directory targetDirectory);
100

101 102
  void unpack(File gzippedTarFile, Directory targetDirectory);

103 104 105 106 107
  /// Compresses a stream using gzip level 1 (faster but larger).
  Stream<List<int>> gzipLevel1Stream(Stream<List<int>> stream) {
    return stream.cast<List<int>>().transform<List<int>>(gzipLevel1.encoder);
  }

108 109 110 111
  /// Returns a pretty name string for the current operating system.
  ///
  /// If available, the detailed version of the OS is included.
  String get name {
112
    const Map<String, String> osNames = <String, String>{
113 114
      'macos': 'Mac OS',
      'linux': 'Linux',
115
      'windows': 'Windows',
116
    };
117
    final String osName = _platform.operatingSystem;
118
    return osNames[osName] ?? osName;
119 120
  }

121 122
  HostPlatform get hostPlatform;

123
  List<File> _which(String execName, { bool all = false });
124 125 126

  /// Returns the separator between items in the PATH environment variable.
  String get pathVarSeparator;
127 128 129 130 131 132 133 134 135

  /// Returns an unused network port.
  ///
  /// Returns 0 if an unused port cannot be found.
  ///
  /// The port returned by this function may become used before it is bound by
  /// its intended user.
  Future<int> findFreePort({bool ipv6 = false}) async {
    int port = 0;
136
    ServerSocket? serverSocket;
137 138 139 140 141 142 143 144 145 146
    final InternetAddress loopback =
        ipv6 ? InternetAddress.loopbackIPv6 : InternetAddress.loopbackIPv4;
    try {
      serverSocket = await ServerSocket.bind(loopback, 0);
      port = serverSocket.port;
    } on SocketException catch (e) {
      // If ipv4 loopback bind fails, try ipv6.
      if (!ipv6) {
        return findFreePort(ipv6: true);
      }
147
      _logger.printTrace('findFreePort failed: $e');
148
    } on Exception catch (e) {
149
      // Failures are signaled by a return value of 0 from this function.
150
      _logger.printTrace('findFreePort failed: $e');
151 152 153 154 155 156 157
    } finally {
      if (serverSocket != null) {
        await serverSocket.close();
      }
    }
    return port;
  }
158 159
}

160
class _PosixUtils extends OperatingSystemUtils {
161
  _PosixUtils({
162 163 164 165
    required FileSystem fileSystem,
    required Logger logger,
    required Platform platform,
    required ProcessManager processManager,
166 167 168 169 170 171
  }) : super._private(
    fileSystem: fileSystem,
    logger: logger,
    platform: platform,
    processManager: processManager,
  );
172

173
  @override
174 175 176 177 178 179 180
  void makeExecutable(File file) {
    chmod(file, 'a+x');
  }

  @override
  void chmod(FileSystemEntity entity, String mode) {
    try {
181 182 183
      final ProcessResult result = _processManager.runSync(
        <String>['chmod', mode, entity.path],
      );
184
      if (result.exitCode != 0) {
185
        _logger.printTrace(
186 187 188 189 190 191
          'Error trying to run chmod on ${entity.absolute.path}'
          '\nstdout: ${result.stdout}'
          '\nstderr: ${result.stderr}',
        );
      }
    } on ProcessException catch (error) {
192 193 194
      _logger.printTrace(
        'Error trying to run chmod on ${entity.absolute.path}: $error',
      );
195
    }
196
  }
197

198
  @override
199
  List<File> _which(String execName, { bool all = false }) {
200 201 202 203 204
    final List<String> command = <String>[
      'which',
      if (all) '-a',
      execName,
    ];
205
    final ProcessResult result = _processManager.runSync(command);
206
    if (result.exitCode != 0) {
207
      return const <File>[];
208
    }
209
    final String stdout = result.stdout as String;
210 211 212
    return stdout.trim().split('\n').map<File>(
      (String path) => _fileSystem.file(path.trim()),
    ).toList();
213
  }
214 215 216 217

  // unzip -o -q zipfile -d dest
  @override
  void unzip(File file, Directory targetDirectory) {
218 219 220 221 222 223
    try {
      _processUtils.runSync(
        <String>['unzip', '-o', '-q', file.path, '-d', targetDirectory.path],
        throwOnError: true,
        verboseExceptions: true,
      );
224
    } on ArgumentError {
225 226 227 228 229 230 231 232 233 234 235 236
      // unzip is not available. this error message is modeled after the download
      // error in bin/internal/update_dart_sdk.sh
      String message = 'Please install unzip.';
      if (_platform.isMacOS) {
        message = 'Consider running "brew install unzip".';
      } else if (_platform.isLinux) {
        message = 'Consider running "sudo apt-get install unzip".';
      }
      throwToolExit(
        'Missing "unzip" tool. Unable to extract ${file.path}.\n$message'
      );
    }
237
  }
238

239 240 241
  // tar -xzf tarball -C dest
  @override
  void unpack(File gzippedTarFile, Directory targetDirectory) {
242
    _processUtils.runSync(
243 244 245
      <String>['tar', '-xzf', gzippedTarFile.path, '-C', targetDirectory.path],
      throwOnError: true,
    );
246 247
  }

248 249
  @override
  File makePipe(String path) {
250
    _processUtils.runSync(
251 252 253
      <String>['mkfifo', path],
      throwOnError: true,
    );
254
    return _fileSystem.file(path);
255
  }
256

257 258 259
  @override
  String get pathVarSeparator => ':';

260
  HostPlatform? _hostPlatform;
261

262
  @override
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  HostPlatform get hostPlatform {
    if (_hostPlatform == null) {
      final RunResult hostPlatformCheck =
          _processUtils.runSync(<String>['uname', '-m']);
      // On x64 stdout is "uname -m: x86_64"
      // On arm64 stdout is "uname -m: aarch64, arm64_v8a"
      if (hostPlatformCheck.exitCode != 0) {
        _logger.printError(
          'Error trying to run uname -m'
          '\nstdout: ${hostPlatformCheck.stdout}'
          '\nstderr: ${hostPlatformCheck.stderr}',
        );
        _hostPlatform = HostPlatform.linux_x64;
      } else if (hostPlatformCheck.stdout.trim().endsWith('x86_64')) {
        _hostPlatform = HostPlatform.linux_x64;
      } else {
        _hostPlatform = HostPlatform.linux_arm64;
      }
    }
282
    return _hostPlatform!;
283
  }
284 285 286 287
}

class _MacOSUtils extends _PosixUtils {
  _MacOSUtils({
288 289 290 291
    required FileSystem fileSystem,
    required Logger logger,
    required Platform platform,
    required ProcessManager processManager,
292 293 294 295 296 297 298
  }) : super(
          fileSystem: fileSystem,
          logger: logger,
          platform: platform,
          processManager: processManager,
        );

299
  String? _name;
300 301 302

  @override
  String get name {
303
    if (_name == null) {
304 305 306 307 308 309 310 311
      final List<RunResult> results = <RunResult>[
        _processUtils.runSync(<String>['sw_vers', '-productName']),
        _processUtils.runSync(<String>['sw_vers', '-productVersion']),
        _processUtils.runSync(<String>['sw_vers', '-buildVersion']),
      ];
      if (results.every((RunResult result) => result.exitCode == 0)) {
        _name =
            '${results[0].stdout.trim()} ${results[1].stdout.trim()} ${results[2].stdout.trim()} ${getNameForHostPlatform(hostPlatform)}';
312 313
      }
      _name ??= super.name;
314
    }
315
    return _name!;
316
  }
317

318
  // On ARM returns arm64, even when this process is running in Rosetta.
319
  @override
320 321
  HostPlatform get hostPlatform {
    if (_hostPlatform == null) {
322
      String? sysctlPath;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
      if (which('sysctl') == null) {
        // Fallback to known install locations.
        for (final String path in <String>[
          '/usr/sbin/sysctl',
          '/sbin/sysctl',
        ]) {
          if (_fileSystem.isFileSync(path)) {
            sysctlPath = path;
          }
        }
      } else {
        sysctlPath = 'sysctl';
      }

      if (sysctlPath == null) {
        throwToolExit('sysctl not found. Try adding it to your PATH environment variable.');
      }
340
      final RunResult arm64Check =
341
          _processUtils.runSync(<String>[sysctlPath, 'hw.optional.arm64']);
342
      // On arm64 stdout is "sysctl hw.optional.arm64: 1"
343
      // On x86 hw.optional.arm64 is unavailable and exits with 1.
344 345 346 347 348 349
      if (arm64Check.exitCode == 0 && arm64Check.stdout.trim().endsWith('1')) {
        _hostPlatform = HostPlatform.darwin_arm;
      } else {
        _hostPlatform = HostPlatform.darwin_x64;
      }
    }
350
    return _hostPlatform!;
351
  }
352 353
}

354
class _WindowsUtils extends OperatingSystemUtils {
355
  _WindowsUtils({
356 357 358 359
    required FileSystem fileSystem,
    required Logger logger,
    required Platform platform,
    required ProcessManager processManager,
360 361 362 363 364
  }) : super._private(
    fileSystem: fileSystem,
    logger: logger,
    platform: platform,
    processManager: processManager,
365
  );
366

367 368 369
  @override
  HostPlatform hostPlatform = HostPlatform.windows_x64;

370
  @override
371 372 373 374
  void makeExecutable(File file) {}

  @override
  void chmod(FileSystemEntity entity, String mode) {}
375

376
  @override
377
  List<File> _which(String execName, { bool all = false }) {
378
    // `where` always returns all matches, not just the first one.
379
    ProcessResult result;
380 381
    try {
      result = _processManager.runSync(<String>['where', execName]);
382
    } on ArgumentError {
383 384 385
      // `where` could be missing if system32 is not on the PATH.
      throwToolExit(
        'Cannot find the executable for `where`. This can happen if the System32 '
386
        r'folder (e.g. C:\Windows\System32 ) is removed from the PATH environment '
387 388 389 390
        'variable. Ensure that this is present and then try again after restarting '
        'the terminal and/or IDE.'
      );
    }
391
    if (result.exitCode != 0) {
392
      return const <File>[];
393
    }
394
    final List<String> lines = (result.stdout as String).trim().split('\n');
395
    if (all) {
396
      return lines.map<File>((String path) => _fileSystem.file(path.trim())).toList();
397
    }
398
    return <File>[_fileSystem.file(lines.first.trim())];
399 400 401 402
  }

  @override
  void unzip(File file, Directory targetDirectory) {
403 404
    final Archive archive = ZipDecoder().decodeBytes(file.readAsBytesSync());
    _unpackArchive(archive, targetDirectory);
405 406 407 408
  }

  @override
  void unpack(File gzippedTarFile, Directory targetDirectory) {
409 410
    final Archive archive = TarDecoder().decodeBytes(
      GZipDecoder().decodeBytes(gzippedTarFile.readAsBytesSync()),
411 412 413
    );
    _unpackArchive(archive, targetDirectory);
  }
414

415
  void _unpackArchive(Archive archive, Directory targetDirectory) {
416
    for (final ArchiveFile archiveFile in archive.files) {
417
      // The archive package doesn't correctly set isFile.
418
      if (!archiveFile.isFile || archiveFile.name.endsWith('/')) {
419
        continue;
420
      }
421

422 423 424 425
      final File destFile = _fileSystem.file(_fileSystem.path.join(
        targetDirectory.path,
        archiveFile.name,
      ));
426
      if (!destFile.parent.existsSync()) {
427
        destFile.parent.createSync(recursive: true);
428
      }
429
      destFile.writeAsBytesSync(archiveFile.content as List<int>);
430
    }
431
  }
432 433 434

  @override
  File makePipe(String path) {
435
    throw UnsupportedError('makePipe is not implemented on Windows.');
436
  }
437

438
  String? _name;
439 440 441 442

  @override
  String get name {
    if (_name == null) {
443
      final ProcessResult result = _processManager.runSync(
444
          <String>['ver'], runInShell: true);
445
      if (result.exitCode == 0) {
446
        _name = (result.stdout as String).trim();
447
      } else {
448
        _name = super.name;
449
      }
450
    }
451
    return _name!;
452
  }
453 454 455

  @override
  String get pathVarSeparator => ';';
456
}
457

458 459
/// Find and return the project root directory relative to the specified
/// directory or the current working directory if none specified.
460
/// Return null if the project root could not be found
461
/// or if the project root is the flutter repository root.
462
String? findProjectRoot(FileSystem fileSystem, [ String? directory ]) {
463
  const String kProjectRootSentinel = 'pubspec.yaml';
464
  directory ??= fileSystem.currentDirectory.path;
465
  while (true) {
466
    if (fileSystem.isFileSync(fileSystem.path.join(directory!, kProjectRootSentinel))) {
467
      return directory;
468
    }
469
    final String parent = fileSystem.path.dirname(directory);
470
    if (directory == parent) {
471
      return null;
472
    }
473 474 475
    directory = parent;
  }
}
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

enum HostPlatform {
  darwin_x64,
  darwin_arm,
  linux_x64,
  linux_arm64,
  windows_x64,
}

String getNameForHostPlatform(HostPlatform platform) {
  switch (platform) {
    case HostPlatform.darwin_x64:
      return 'darwin-x64';
    case HostPlatform.darwin_arm:
      return 'darwin-arm';
    case HostPlatform.linux_x64:
      return 'linux-x64';
    case HostPlatform.linux_arm64:
      return 'linux-arm64';
    case HostPlatform.windows_x64:
      return 'windows-x64';
  }
}