fs: Copy file permissions (#2354)

Signed-off-by: Kevin Leimkuhler <[email protected]>
This commit is contained in:
Kevin Leimkuhler
2020-04-02 17:10:44 -04:00
committed by GitHub
parent cf4cbc142b
commit 3eaa1885c3
2 changed files with 22 additions and 6 deletions
+4 -6
View File
@@ -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<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
View File
@@ -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);
}