copyRecursively method

Future<void> copyRecursively(
  1. Directory target
)

Copies all of the files in this directory to target.

This is similar to cp -R <from> <to>:

  • Symlinks are supported.
  • Existing files are over-written, if any.
  • If target is within this, throws ArgumentError.
  • If this and target are canonically the same, no operation occurs.

Returns a future that completes when complete.

Implementation

Future<void> copyRecursively(Directory target) async {
  if (path_helper.canonicalize(path) ==
      path_helper.canonicalize(target.path)) {
    return;
  }
  if (path_helper.isWithin(path, target.path)) {
    throw ArgumentError('Cannot copy $path to ${target.path}');
  }
  await target.create(recursive: true);
  await for (final file in list(recursive: true)) {
    final copyTo = path_helper.join(
      target.path,
      path_helper.relative(file.path, from: path),
    );
    if (file is Directory) {
      await Directory(copyTo).create(recursive: true);
    } else if (file is File) {
      await File(file.path).copy(copyTo);
    } else if (file is Link) {
      await Link(copyTo).create(await file.target(), recursive: true);
    }
  }
}