mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-16 00:00:12 +02:00
fs: Copy file permissions (#2354)
Signed-off-by: Kevin Leimkuhler <[email protected]>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user