fix: use std copy function in a blocking thread (#1817)

This commit is contained in:
三咲雅 · Misaki Masa 2024-10-21 09:16:54 +08:00 committed by GitHub
parent 16881aab2f
commit 0e118b5b3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 97 additions and 102 deletions

23
flake.lock generated
View file

@ -5,11 +5,11 @@
"systems": "systems" "systems": "systems"
}, },
"locked": { "locked": {
"lastModified": 1710146030, "lastModified": 1726560853,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -20,11 +20,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1722415718, "lastModified": 1729265718,
"narHash": "sha256-5US0/pgxbMksF92k1+eOa8arJTJiPvsdZj9Dl+vJkM4=", "narHash": "sha256-4HQI+6LsO3kpWTYuVGIzhJs1cetFcwT7quWCk/6rqeo=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "c3392ad349a5227f4a3464dce87bcc5046692fce", "rev": "ccc0c2126893dd20963580b6478d1a10a4512185",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -43,22 +43,19 @@
}, },
"rust-overlay": { "rust-overlay": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": ["nixpkgs"]
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1721441897, "lastModified": 1729391507,
"narHash": "sha256-gYGX9/22tPNeF7dR6bWN5rsrpU4d06GnQNNgZ6ZiXz0=", "narHash": "sha256-as0I9xieJUHf7kiK2a9znDsVZQTFWhM1pLivII43Gi0=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "b7996075da11a2d441cfbf4e77c2939ce51506fd", "rev": "784981a9feeba406de38c1c9a3decf966d853cca",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "b7996075da11a2d441cfbf4e77c2939ce51506fd",
"type": "github" "type": "github"
} }
}, },

View file

@ -3,7 +3,7 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
rust-overlay = { rust-overlay = {
url = "github:oxalica/rust-overlay/b7996075da11a2d441cfbf4e77c2939ce51506fd"; # FIX: pin to a specific commit until cargo-c is updated url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
}; };

View file

@ -23,6 +23,7 @@ impl Tab {
pub(super) fn register(lua: &Lua) -> mlua::Result<()> { pub(super) fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Self>(|reg| { lua.register_userdata_type::<Self>(|reg| {
reg.add_field_method_get("idx", |_, me| Ok(me.idx + 1));
reg.add_method("name", |lua, me, ()| { reg.add_method("name", |lua, me, ()| {
lua.create_string(me.current.url.name().as_encoded_bytes()) lua.create_string(me.current.url.name().as_encoded_bytes())
}); });

View file

@ -251,40 +251,37 @@ async fn _copy_with_progress(from: PathBuf, to: PathBuf, cha: Cha) -> io::Result
cha.btime.map(|t| ft = ft.set_created(t)); cha.btime.map(|t| ft = ft.set_created(t));
} }
let written;
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
{ {
use std::os::fd::AsRawFd; use std::os::{fd::AsRawFd, unix::fs::OpenOptionsExt};
let mut reader = fs::File::open(from).await?; tokio::task::spawn_blocking(move || {
let mut writer = fs::OpenOptions::new() let mut reader = std::fs::File::open(from)?;
let mut writer = std::fs::OpenOptions::new()
.mode(cha.perm as u32) .mode(cha.perm as u32)
.write(true) .write(true)
.create(true) .create(true)
.truncate(true) .truncate(true)
.open(to) .open(to)?;
.await?;
written = io::copy(&mut reader, &mut writer).await?; let written = std::io::copy(&mut reader, &mut writer)?;
let writer = writer.into_std().await;
_ = tokio::task::spawn_blocking(move || {
unsafe { libc::fchmod(writer.as_raw_fd(), cha.perm) }; unsafe { libc::fchmod(writer.as_raw_fd(), cha.perm) };
writer.set_times(ft).ok(); writer.set_times(ft).ok();
Ok(written)
}) })
.await; .await?
} }
#[cfg(not(any(target_os = "linux", target_os = "android")))] #[cfg(not(any(target_os = "linux", target_os = "android")))]
{ {
written = fs::copy(from, &to).await?; tokio::task::spawn_blocking(move || {
_ = tokio::task::spawn_blocking(move || { let written = std::fs::copy(from, &to)?;
std::fs::File::options().write(true).open(to).and_then(|f| f.set_times(ft)).ok(); std::fs::File::options().write(true).open(to).and_then(|f| f.set_times(ft)).ok();
})
.await;
}
Ok(written) Ok(written)
})
.await?
}
} }
pub async fn remove_dir_clean(dir: &Path) { pub async fn remove_dir_clean(dir: &Path) {