visitDirectory function

  1. @Deprecated('Will be removed in 3.0.0')
Future visitDirectory (
  1. Directory dir,
  2. Future<bool> visit(
    1. FileSystemEntity f
    )
)

Lists the sub-directories and files of this Directory, optionally recursing into sub-directories based on the return value of visit.

visit is called with a File, Directory or Link to a directory, never a Symlink to a File. If visit returns true, then its argument is listed recursively.

Implementation

@Deprecated('Will be removed in 3.0.0')
Future visitDirectory(Directory dir, Future<bool> visit(FileSystemEntity f)) {
  final completer = Completer();
  final directories = <String, bool>{dir.path: false};

  void _list(Directory dir) {
    directories.putIfAbsent(dir.path, () => false);
    dir.list(followLinks: false).listen((FileSystemEntity entity) {
      var future = visit(entity);
      // TODO(cbracken): Remove this post-NNBD.
      if (future == null) return;
      future.then((bool recurse) {
        // recurse on directories, but not cyclic symlinks
        if (entity is! File && recurse == true) {
          if (entity is Link) {
            if (FileSystemEntity.typeSync(entity.path, followLinks: true) ==
                FileSystemEntityType.directory) {
              var fullPath = getFullPath(entity.path).toString();
              var dirFullPath = getFullPath(dir.path).toString();
              if (!dirFullPath.startsWith(fullPath)) {
                _list(Directory(entity.path));
              }
            }
          } else if (entity is Directory) {
            _list(entity);
          }
        }
      });
    }, onDone: () {
      directories[dir.path] = true;
      if (directories.values.every((v) => v)) {
        completer.complete();
      }
    }, onError: (e) {
      completer.completeError(e);
    }, cancelOnError: true);
  }

  _list(dir);

  return completer.future;
}