Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: Copy file permissions #2354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions tokio/src/fs/copy.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,8 +18,7 @@ use std::path::Path;
/// ```

pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64, std::io::Error> {
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
}
18 changes: 18 additions & 0 deletions tokio/tests/fs_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}