|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | + |
| 10 | +import '../utils.dart'; |
| 11 | + |
| 12 | +extension DirectoryRobustRecursiveListing on Directory { |
| 13 | + /// Lists the given directory recursively ignoring not-found or access errors. |
| 14 | + /// |
| 15 | + /// These can arise from concurrent file-system modification. |
| 16 | + /// |
| 17 | + /// See [listRecursively] for how symlinks are handled. |
| 18 | + Stream<FileSystemEntity> listRecursivelyIgnoringErrors() { |
| 19 | + return listRecursively() |
| 20 | + .ignoring<PathNotFoundException>() |
| 21 | + .ignoring<PathAccessException>(); |
| 22 | + } |
| 23 | + |
| 24 | + /// Lists the directory recursively. |
| 25 | + /// |
| 26 | + /// This is like `Directory.list(recursive: true)`, but handles symlinks like |
| 27 | + /// `find -L` to avoid a performance issue with symbolic link cycles. |
| 28 | + /// |
| 29 | + /// See: https://github.com/dart-lang/sdk/issues/61407. |
| 30 | + /// |
| 31 | + /// A link to a directory is only followed if the link target is not currently |
| 32 | + /// being traversed. For this check, directories are compared using their |
| 33 | + /// symlink-resolved paths. |
| 34 | + /// |
| 35 | + /// Skipped links to directories are not mentioned in the directory listing. |
| 36 | + Stream<FileSystemEntity> listRecursively() => |
| 37 | + _DirectoryTraversal(this).listRecursively(); |
| 38 | +} |
| 39 | + |
| 40 | +/// A recursive directory listing algorithm that follows symlinks carefully. |
| 41 | +class _DirectoryTraversal { |
| 42 | + final Directory root; |
| 43 | + final StreamController<FileSystemEntity> _result = StreamController(); |
| 44 | + |
| 45 | + /// The directories currently being traversed. |
| 46 | + /// |
| 47 | + /// These are canonical paths with symlinks resolved, for correct comparison. |
| 48 | + final Set<String> _traversing = {}; |
| 49 | + |
| 50 | + _DirectoryTraversal(this.root); |
| 51 | + |
| 52 | + Stream<FileSystemEntity> listRecursively() { |
| 53 | + unawaited(_listAndRecurse()); |
| 54 | + return _result.stream; |
| 55 | + } |
| 56 | + |
| 57 | + /// Lists [root] then closes [_result]. |
| 58 | + Future<void> _listAndRecurse() async { |
| 59 | + try { |
| 60 | + final resolvedRoot = _ResolvedDirectory.resolve(root); |
| 61 | + _traversing.add(resolvedRoot.canonicalPath); |
| 62 | + await _listAndRecurseOrThrow(resolvedRoot); |
| 63 | + } catch (e, s) { |
| 64 | + _result.addError(e, s); |
| 65 | + } finally { |
| 66 | + await _result.close(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Lists [directory] and its subdirectories. |
| 71 | + /// |
| 72 | + /// A subdirectory is only listed if its canonical path is not already in |
| 73 | + /// [_traversing]. |
| 74 | + Future<void> _listAndRecurseOrThrow(_ResolvedDirectory directory) async { |
| 75 | + final subdirectories = <_ResolvedDirectory>[]; |
| 76 | + |
| 77 | + await for (var entity |
| 78 | + in directory.directory.list(recursive: false, followLinks: false)) { |
| 79 | + // Handle links. |
| 80 | + if (entity is Link) { |
| 81 | + // Look up their target and target type. |
| 82 | + final target = entity.targetSync(); |
| 83 | + final targetType = FileSystemEntity.typeSync(target); |
| 84 | + |
| 85 | + if (targetType == FileSystemEntityType.directory) { |
| 86 | + // Add links to directories with their target to [subdirectories]. |
| 87 | + subdirectories.add(_ResolvedDirectory( |
| 88 | + directory: Directory(entity.path), canonicalPath: target)); |
| 89 | + } else if (targetType == FileSystemEntityType.file) { |
| 90 | + // Output files. |
| 91 | + _result.add(File(entity.path)); |
| 92 | + } else { |
| 93 | + // Anything else. Broken links get output with type `Link`. |
| 94 | + _result.add(entity); |
| 95 | + } |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + // Handle directories. |
| 100 | + if (entity is Directory) { |
| 101 | + // If [directory] is canonical, a subdirectory within it is already |
| 102 | + // canonical. |
| 103 | + // |
| 104 | + // If [directory] is _not_ canonical, construct the canonical path of |
| 105 | + // the subdirectory by joining its basename to |
| 106 | + // [directory.resolvedDirectory]. |
| 107 | + final resolvedDirectory = directory.isCanonical |
| 108 | + ? entity.path |
| 109 | + : p.join(directory.canonicalPath, p.basename(entity.path)); |
| 110 | + subdirectories.add(_ResolvedDirectory( |
| 111 | + directory: entity, canonicalPath: resolvedDirectory)); |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + // Files and anything else. |
| 116 | + _result.add(entity); |
| 117 | + } |
| 118 | + |
| 119 | + // Recurse into subdirectories that are not already being traversed. |
| 120 | + for (final directory in subdirectories) { |
| 121 | + if (_traversing.add(directory.canonicalPath)) { |
| 122 | + _result.add(directory.directory); |
| 123 | + await _listAndRecurseOrThrow(directory); |
| 124 | + _traversing.remove(directory.canonicalPath); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// A directory plus its canonical path. |
| 131 | +class _ResolvedDirectory { |
| 132 | + final Directory directory; |
| 133 | + final String canonicalPath; |
| 134 | + |
| 135 | + _ResolvedDirectory({required this.directory, required this.canonicalPath}); |
| 136 | + |
| 137 | + static _ResolvedDirectory resolve(Directory directory) { |
| 138 | + try { |
| 139 | + return _ResolvedDirectory( |
| 140 | + directory: directory, |
| 141 | + canonicalPath: directory.resolveSymbolicLinksSync()); |
| 142 | + } on FileSystemException catch (e, s) { |
| 143 | + // The first operation on a directory is to resolve symbolic links, which |
| 144 | + // fails with a general FileSystemException if the file is not found. |
| 145 | + // Convert that into a PathNotFoundException as that makes more sense |
| 146 | + // to the caller, who didn't ask for anything to do with symbolic links. |
| 147 | + if (e.message.contains('Cannot resolve symbolic links') && |
| 148 | + e.osError?.errorCode == 2) { |
| 149 | + throw Error.throwWithStackTrace( |
| 150 | + PathNotFoundException(directory.path, e.osError!), s); |
| 151 | + } |
| 152 | + rethrow; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + bool get isCanonical => canonicalPath == directory.path; |
| 157 | +} |
0 commit comments