Skip to content

Commit

Permalink
fix: use std copy function in a blocking thread
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Oct 21, 2024
1 parent 16881aa commit 041744a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 103 deletions.
159 changes: 78 additions & 81 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
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";
};
};
Expand Down
1 change: 1 addition & 0 deletions yazi-fm/src/lives/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Tab {

pub(super) fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<Self>(|reg| {
reg.add_field_method_get("idx", |_, me| Ok(me.idx + 1));
reg.add_method("name", |lua, me, ()| {
lua.create_string(me.current.url.name().as_encoded_bytes())
});
Expand Down
39 changes: 18 additions & 21 deletions yazi-shared/src/fs/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

let written;
#[cfg(any(target_os = "linux", target_os = "android"))]
{
use std::os::fd::AsRawFd;

let mut reader = fs::File::open(from).await?;
let mut writer = fs::OpenOptions::new()
.mode(cha.perm as u32)
.write(true)
.create(true)
.truncate(true)
.open(to)
.await?;

written = io::copy(&mut reader, &mut writer).await?;
let writer = writer.into_std().await;
use std::os::{fd::AsRawFd, unix::fs::OpenOptionsExt};

_ = tokio::task::spawn_blocking(move || {
tokio::task::spawn_blocking(move || {
let mut reader = std::fs::File::open(from)?;
let mut writer = std::fs::OpenOptions::new()
.mode(cha.perm as u32)
.write(true)
.create(true)
.truncate(true)
.open(to)?;

let written = std::io::copy(&mut reader, &mut writer)?;
unsafe { libc::fchmod(writer.as_raw_fd(), cha.perm) };
writer.set_times(ft).ok();

Ok(written)
})
.await;
.await?
}

#[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();
Ok(written)
})
.await;
.await?
}

Ok(written)
}

pub async fn remove_dir_clean(dir: &Path) {
Expand Down

0 comments on commit 041744a

Please sign in to comment.