From 6a3d9084ac68a0f033d9a1a7881cad4c2f4441e7 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 30 May 2024 23:43:09 +0900 Subject: [PATCH] Replace nix with rustix --- Cargo.lock | 17 +++----------- libwayshot/Cargo.toml | 2 +- libwayshot/src/screencopy.rs | 44 +++++++++++++----------------------- wayshot/Cargo.toml | 2 +- wayshot/src/wayshot.rs | 6 ++--- 5 files changed, 24 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04fcbd3d..2c9f32ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -475,7 +475,7 @@ version = "0.3.2-dev" dependencies = [ "image", "memmap2", - "nix 0.27.1", + "rustix", "thiserror", "tracing", "wayland-client", @@ -536,17 +536,6 @@ dependencies = [ "simd-adler32", ] -[[package]] -name = "nix" -version = "0.27.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" -dependencies = [ - "bitflags 2.5.0", - "cfg-if", - "libc", -] - [[package]] name = "nix" version = "0.28.0" @@ -1019,7 +1008,7 @@ dependencies = [ "flate2", "image", "libwayshot", - "nix 0.28.0", + "rustix", "tracing", "tracing-subscriber", "wl-clipboard-rs", @@ -1140,7 +1129,7 @@ dependencies = [ "derive-new", "libc", "log", - "nix 0.28.0", + "nix", "os_pipe", "tempfile", "thiserror", diff --git a/libwayshot/Cargo.toml b/libwayshot/Cargo.toml index 8a9719b0..7ff54d5e 100644 --- a/libwayshot/Cargo.toml +++ b/libwayshot/Cargo.toml @@ -12,7 +12,7 @@ edition.workspace = true tracing.workspace = true image = { version = "0.24", default-features = false } memmap2 = "0.9.0" -nix = { version = "0.27.1", features = ["fs", "mman"] } +rustix = { version = "0.38", features = ["fs", "shm"] } thiserror = "1" wayland-client = "0.31.1" diff --git a/libwayshot/src/screencopy.rs b/libwayshot/src/screencopy.rs index 8b01c7c6..ee7ae337 100644 --- a/libwayshot/src/screencopy.rs +++ b/libwayshot/src/screencopy.rs @@ -1,15 +1,14 @@ use std::{ ffi::CString, - os::fd::{AsRawFd, IntoRawFd, OwnedFd}, + os::fd::OwnedFd, time::{SystemTime, UNIX_EPOCH}, }; use image::{ColorType, DynamicImage, ImageBuffer, Pixel}; use memmap2::MmapMut; -use nix::{ - fcntl, - sys::{memfd, mman, stat}, - unistd, +use rustix::{ + fs::{self, SealFlags}, + io, shm, }; use wayland_client::protocol::{ wl_buffer::WlBuffer, wl_output, wl_shm::Format, wl_shm_pool::WlShmPool, @@ -117,24 +116,19 @@ pub fn create_shm_fd() -> std::io::Result { #[cfg(any(target_os = "linux", target_os = "freebsd"))] loop { // Create a file that closes on succesful execution and seal it's operations. - match memfd::memfd_create( + match fs::memfd_create( CString::new("libwayshot")?.as_c_str(), - memfd::MemFdCreateFlag::MFD_CLOEXEC | memfd::MemFdCreateFlag::MFD_ALLOW_SEALING, + fs::MemfdFlags::CLOEXEC | fs::MemfdFlags::ALLOW_SEALING, ) { Ok(fd) => { // This is only an optimization, so ignore errors. // F_SEAL_SRHINK = File cannot be reduced in size. // F_SEAL_SEAL = Prevent further calls to fcntl(). - let _ = fcntl::fcntl( - fd.as_raw_fd(), - fcntl::F_ADD_SEALS( - fcntl::SealFlag::F_SEAL_SHRINK | fcntl::SealFlag::F_SEAL_SEAL, - ), - ); + let _ = fs::fcntl_add_seals(&fd, fs::SealFlags::SHRINK | SealFlags::SEAL); return Ok(fd); } - Err(nix::errno::Errno::EINTR) => continue, - Err(nix::errno::Errno::ENOSYS) => break, + Err(io::Errno::INTR) => continue, + Err(io::Errno::NOSYS) => break, Err(errno) => return Err(std::io::Error::from(errno)), } } @@ -142,7 +136,7 @@ pub fn create_shm_fd() -> std::io::Result { // Fallback to using shm_open. let mut mem_file_handle = get_mem_file_handle(); loop { - match mman::shm_open( + match shm::shm_open( // O_CREAT = Create file if does not exist. // O_EXCL = Error if create and file exists. // O_RDWR = Open for reading and writing. @@ -150,25 +144,19 @@ pub fn create_shm_fd() -> std::io::Result { // S_IRUSR = Set user read permission bit . // S_IWUSR = Set user write permission bit. mem_file_handle.as_str(), - fcntl::OFlag::O_CREAT - | fcntl::OFlag::O_EXCL - | fcntl::OFlag::O_RDWR - | fcntl::OFlag::O_CLOEXEC, - stat::Mode::S_IRUSR | stat::Mode::S_IWUSR, + shm::ShmOFlags::CREATE | shm::ShmOFlags::EXCL | shm::ShmOFlags::RDWR, + fs::Mode::RUSR | fs::Mode::WUSR, ) { - Ok(fd) => match mman::shm_unlink(mem_file_handle.as_str()) { + Ok(fd) => match shm::shm_unlink(mem_file_handle.as_str()) { Ok(_) => return Ok(fd), - Err(errno) => match unistd::close(fd.into_raw_fd()) { - Ok(_) => return Err(std::io::Error::from(errno)), - Err(errno) => return Err(std::io::Error::from(errno)), - }, + Err(errno) => return Err(std::io::Error::from(errno)), }, - Err(nix::errno::Errno::EEXIST) => { + Err(io::Errno::EXIST) => { // If a file with that handle exists then change the handle mem_file_handle = get_mem_file_handle(); continue; } - Err(nix::errno::Errno::EINTR) => continue, + Err(io::Errno::INTR) => continue, Err(errno) => return Err(std::io::Error::from(errno)), } } diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 2835b246..44bc8652 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -36,7 +36,7 @@ eyre = "0.6.8" chrono = "0.4.35" wl-clipboard-rs = "0.8.0" -nix = { version = "0.28.0", features = ["process"] } +rustix = { version = "0.38", features = ["process", "runtime"] } [[bin]] name = "wayshot" diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 2d25e098..0930b845 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -15,7 +15,7 @@ use utils::EncodingFormat; use wl_clipboard_rs::copy::{MimeType, Options, Source}; -use nix::unistd::{fork, ForkResult}; +use rustix::runtime::{fork, Fork}; fn select_ouput(ouputs: &[T]) -> Option where @@ -158,10 +158,10 @@ fn clipboard_daemonize(buffer: Cursor>) -> Result<()> { match unsafe { fork() } { // Having the image persistently available on the clipboard requires a wayshot process to be alive. // Fork the process with a child detached from the main process and have the parent exit - Ok(ForkResult::Parent { .. }) => { + Ok(Fork::Parent(_)) => { return Ok(()); } - Ok(ForkResult::Child) => { + Ok(Fork::Child(_)) => { opts.foreground(true); // Offer the image till something else is available on the clipboard opts.copy( Source::Bytes(buffer.into_inner().into()),