scan method

Lists the directory at path and everything below it, without blocking the event loop for long.

Each directory is read with the asynchronous Directory.list, so other work runs between directory reads; prefer this in servers and interactive tools, and scanSync in short scripts. Both return the same entries for the same directory (see DirectoryScanner for their shape and order).

The returned future completes with a PathNotFoundException when nothing exists at path (including a broken link), with a FileSystemException when path exists but is not a directory (or a link to one), and with an ArgumentError when maxDepth is negative. Failures below the root go to onError instead.

The paths use host syntax; to build a tree from them, parse them with PathPolicy(backslashIsSeparator: Platform.isWindows, ...) (see DirectoryScanner), or call scanTree, which does.

Takes O(n log n) time for n reported entities, plus one directory listing per listed directory.

Implementation

Future<List<TreeEntry<FileSystemEntity>>> scan(String path) async {
  _checkMaxDepth();
  final root = _root(path);
  final walk = _Walk(
    this,
    root,
    rootResolved: followLinks ? await _resolveRootAsync(root) : null,
  );
  while (walk.isNotDone) {
    final visit = walk.next();
    if (visit == null) continue;
    final (children, failures) = await _listAsync(visit.directory);
    failures.forEach(_reportFailure);
    for (final child in children) {
      if (walk.isHidden(child)) continue;
      if (child is Link && followLinks) {
        walk.addLink(child, visit, await _followAsync(child));
      } else {
        walk.add(child, visit);
      }
    }
  }
  return walk.sortedEntries();
}