diff --git a/tokio/src/fs/copy.rs b/tokio/src/fs/copy.rs index e39924c56e8..d4d4d29c850 100644 --- a/tokio/src/fs/copy.rs +++ b/tokio/src/fs/copy.rs @@ -1,5 +1,4 @@ -use crate::fs::File; -use crate::io; +use crate::fs::asyncify; use std::path::Path; /// Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file. @@ -19,8 +18,7 @@ use std::path::Path; /// ``` pub async fn copy, Q: AsRef>(from: P, to: Q) -> Result { - let from = File::open(from).await?; - let to = File::create(to).await?; - let (mut from, mut to) = (io::BufReader::new(from), io::BufWriter::new(to)); - io::copy(&mut from, &mut to).await + let from = from.as_ref().to_owned(); + let to = to.as_ref().to_owned(); + asyncify(|| std::fs::copy(from, to)).await } diff --git a/tokio/tests/fs_copy.rs b/tokio/tests/fs_copy.rs index dc6c0d6f939..8d1632013ea 100644 --- a/tokio/tests/fs_copy.rs +++ b/tokio/tests/fs_copy.rs @@ -19,3 +19,21 @@ async fn copy() { assert_eq!(from, to); } + +#[tokio::test] +async fn copy_permissions() { + let dir = tempdir().unwrap(); + let from_path = dir.path().join("foo.txt"); + let to_path = dir.path().join("bar.txt"); + + let from = tokio::fs::File::create(&from_path).await.unwrap(); + let mut from_perms = from.metadata().await.unwrap().permissions(); + from_perms.set_readonly(true); + from.set_permissions(from_perms.clone()).await.unwrap(); + + tokio::fs::copy(from_path, &to_path).await.unwrap(); + + let to_perms = tokio::fs::metadata(to_path).await.unwrap().permissions(); + + assert_eq!(from_perms, to_perms); +}