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

159
flake.lock generated
View file

@ -1,83 +1,80 @@
{ {
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"inputs": { "inputs": {
"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": {
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"type": "github" "type": "github"
} }
}, },
"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": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixpkgs-unstable", "ref": "nixpkgs-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay" "rust-overlay": "rust-overlay"
} }
}, },
"rust-overlay": { "rust-overlay": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": ["nixpkgs"]
"nixpkgs" },
] "locked": {
}, "lastModified": 1729391507,
"locked": { "narHash": "sha256-as0I9xieJUHf7kiK2a9znDsVZQTFWhM1pLivII43Gi0=",
"lastModified": 1721441897, "owner": "oxalica",
"narHash": "sha256-gYGX9/22tPNeF7dR6bWN5rsrpU4d06GnQNNgZ6ZiXz0=", "repo": "rust-overlay",
"owner": "oxalica", "rev": "784981a9feeba406de38c1c9a3decf966d853cca",
"repo": "rust-overlay", "type": "github"
"rev": "b7996075da11a2d441cfbf4e77c2939ce51506fd", },
"type": "github" "original": {
}, "owner": "oxalica",
"original": { "repo": "rust-overlay",
"owner": "oxalica", "type": "github"
"repo": "rust-overlay", }
"rev": "b7996075da11a2d441cfbf4e77c2939ce51506fd", },
"type": "github" "systems": {
} "locked": {
}, "lastModified": 1681028828,
"systems": { "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"locked": { "owner": "nix-systems",
"lastModified": 1681028828, "repo": "default",
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"owner": "nix-systems", "type": "github"
"repo": "default", },
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "original": {
"type": "github" "owner": "nix-systems",
}, "repo": "default",
"original": { "type": "github"
"owner": "nix-systems", }
"repo": "default", }
"type": "github" },
} "root": "root",
} "version": 7
},
"root": "root",
"version": 7
} }

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)?;
.mode(cha.perm as u32) let mut writer = std::fs::OpenOptions::new()
.write(true) .mode(cha.perm as u32)
.create(true) .write(true)
.truncate(true) .create(true)
.open(to) .truncate(true)
.await?; .open(to)?;
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();
Ok(written)
}) })
.await; .await?
} }
Ok(written)
} }
pub async fn remove_dir_clean(dir: &Path) { pub async fn remove_dir_clean(dir: &Path) {