From 3eaa1885c314345f3fa0022e8a04af4f159f2365 Mon Sep 17 00:00:00 2001 From: Kevin Leimkuhler Date: Thu, 2 Apr 2020 14:10:44 -0700 Subject: [PATCH] fs: Copy file permissions (#2354) Signed-off-by: Kevin Leimkuhler --- tokio/src/fs/copy.rs | 10 ++++------ tokio/tests/fs_copy.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tokio/src/fs/copy.rs b/tokio/src/fs/copy.rs index e39924c56..d4d4d29c8 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 dc6c0d6f9..8d1632013 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); +}