Skip to content

Commit

Permalink
Replace nix with rustix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigas002 committed May 30, 2024
1 parent b8774ae commit 6a3d908
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 47 deletions.
17 changes: 3 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion libwayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
44 changes: 16 additions & 28 deletions libwayshot/src/screencopy.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -117,58 +116,47 @@ pub fn create_shm_fd() -> std::io::Result<OwnedFd> {
#[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)),
}
}

// 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.
// O_CLOEXEC = Close on succesful execution.
// 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)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion wayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(ouputs: &[T]) -> Option<usize>
where
Expand Down Expand Up @@ -158,10 +158,10 @@ fn clipboard_daemonize(buffer: Cursor<Vec<u8>>) -> 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()),
Expand Down

0 comments on commit 6a3d908

Please sign in to comment.