Repository: denoland/deno
File: ext/fs/std_fs.rs (Rust)

Code before:
    #[cfg(unix)]
    {
      use std::os::unix::fs::MetadataExt;
      source_meta.ino() == dest_meta.ino()
    }
    #[cfg(windows)]
    {
      use std::os::windows::fs::MetadataExt;
      // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
      //
      // The identifier (low and high parts) and the volume serial number uniquely identify a file on a single computer.
      // To determine whether two open handles represent the same file, combine the identifier and the volume serial
      // number for each file and compare them.
      //
      // Use this code once file_index() and volume_serial_number() is stabalized
      // See: https://github.com/rust-lang/rust/issues/63010
      //
      // source_meta.file_index() == dest_meta.file_index()
      //   && source_meta.volume_serial_number()
      //     == dest_meta.volume_serial_number()
      source_meta.last_write_time() == dest_meta.last_write_time()
        && source_meta.creation_time() == dest_meta.creation_time()
    }
  }

  match (fs::metadata(to), fs::symlink_metadata(to)) {
    (Ok(m), _) if m.is_dir() => cp_(
      source_meta,
      from,
      &to.join(from.file_name().ok_or_else(|| {
        io::Error::new(
          io::ErrorKind::InvalidInput,
          "the source path is not a valid file",
        )
      })?),
    )?,
    (_, Ok(m)) if is_identical(&source_meta, &m) => {
      return Err(
        io::Error::new(
          io::ErrorKind::InvalidInput,
          "the source and destination are the same file",
        )
        .into(),
      )
    }
    _ => cp_(source_meta, from, to)?,
  }

  Ok(())
}


Review comment (suggestion):
It's faster because we avoid the second symlink_metadata lookup when the first metadata call is a directory.

Code after:
    #[cfg(unix)]
    {
      use std::os::unix::fs::MetadataExt;
      source_meta.ino() == dest_meta.ino()
    }
    #[cfg(windows)]
    {
      use std::os::windows::fs::MetadataExt;
      // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
      //
      // The identifier (low and high parts) and the volume serial number uniquely identify a file on a single computer.
      // To determine whether two open handles represent the same file, combine the identifier and the volume serial
      // number for each file and compare them.
      //
      // Use this code once file_index() and volume_serial_number() is stabalized
      // See: https://github.com/rust-lang/rust/issues/63010
      //
      // source_meta.file_index() == dest_meta.file_index()
      //   && source_meta.volume_serial_number()
      //     == dest_meta.volume_serial_number()
      source_meta.last_write_time() == dest_meta.last_write_time()
        && source_meta.creation_time() == dest_meta.creation_time()
    }
  }

  if let Ok(m) = fs::metadata(to) {
    if m.is_dir() {
      return cp_(
        source_meta,
        from,
        &to.join(from.file_name().ok_or_else(|| {
          io::Error::new(
            io::ErrorKind::InvalidInput,
            "the source path is not a valid file",
          )
        })?),
      );
    }
  }

  if let Ok(m) = fs::symlink_metadata(to) {
    if is_identical(&source_meta, &m) {
      return Err(
        io::Error::new(
          io::ErrorKind::InvalidInput,
          "the source and destination are the same file",
        )
        .into(),
      );
    }
  }