copyRecursively method
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 withinthis
, throws ArgumentError. - If
this
andtarget
are canonically the same, no operation occurs.
For the sync version see copyRecursivelySync
Implementation
Future<void> copyRecursively(
Directory target, {
bool followLinks = true,
LinkFactory linkFactory = Link.new,
FileFactory fileFactory = File.new,
DirectoryFactory dirFactory = Directory.new,
lib_path.Context? pathContext,
}) async {
pathContext ??= lib_path.context;
if (pathContext.canonicalize(path) ==
pathContext.canonicalize(target.path)) {
return;
}
if (pathContext.isWithin(path, target.path)) {
throw ArgumentError("Cannot copy $path to ${target.path}");
}
await target.create(recursive: true);
await for (final entity
in list(recursive: true, followLinks: followLinks)) {
final copyTo = pathContext.join(
target.path,
pathContext.relative(entity.path, from: path),
);
if (entity is Directory) {
await dirFactory(copyTo).create(recursive: true);
} else if (entity is File) {
await fileFactory(entity.path).copy(copyTo);
} else if (entity is Link) {
final linkTarget = await entity.target();
await linkFactory(copyTo).create(linkTarget, recursive: true);
}
}
}