execute method

Future<int> execute(
  1. CopyNodeOperation operation
)

Implementation

Future<int> execute(CopyNodeOperation operation) async {
  if (operation.isDirectory!) {
    final dstDirectory = operation.dst!.asDirectory();
    if (!await dstDirectory.exists()) {
      await dstDirectory.create(recursive: true);
      return 1;
    }
    return 0;
  } else {
    final srcFile = src!.asFile();
    final dstFile = dst!.asFile();

    // Try to link first
    // allow link if asked and on the same file system
    if (options!.tryToLinkFile &&
        (src!.fs == dst!.fs) &&
        src!.fs.supportsFileLink) {
      final srcTarget = srcFile.absolute.path;
      // Check if dst is link
      final type = await dst!.type(followLinks: false);

      var deleteDst = false;
      if (type != FileSystemEntityType.notFound) {
        if (type == FileSystemEntityType.link) {
          // check target
          if (await dst!.asLink().target() != srcTarget) {
            deleteDst = true;
          } else {
            // nothing to do
            return 0;
          }
        } else {
          deleteDst = true;
        }
      }

      if (deleteDst) {
        //devPrint('Deleting $dstFile');
        await dstFile.delete(recursive: true);
        //devPrint('Deleted $dstFile');
      }

      await dst!.asLink().create(srcTarget, recursive: true);
      return 1;
    }
    // Handle modified date
    if (options!.checkSizeAndModifiedDate == true) {
      final srcStat = await srcFile.stat();
      final dstStat = await dstFile.stat();
      if ((dstStat.type != FileSystemEntityType.notFound) &&
          (srcStat.size == dstStat.size) &&
          (srcStat.modified.compareTo(dstStat.modified) <= 0)) {
        // should be same...
        return 0;
      }
    }

    var count = await copyFileContent(srcFile, dstFile);
    await copyFileMeta(srcFile, dstFile);
    return count;
  }
}