Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take OwnedFd instead of RawFd as argument to receive_to_fd #408

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `xkeysym::Keysym` is used as a keyboard key representation instead of `u32`
- `wayland-rs` dependencies are updated to 0.31
- `calloop` dependency updated to 0.12.1
- Take `OwnedFd` instead of `RawFd` as argument to `receive_to_fd` functions.

#### Fixed

Expand Down
10 changes: 4 additions & 6 deletions src/data_device_manager/data_offer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
ops::{Deref, DerefMut},
os::unix::prelude::{BorrowedFd, FromRawFd, RawFd},
os::unix::prelude::{AsFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd},
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -427,16 +427,14 @@ pub fn receive(offer: &WlDataOffer, mime_type: String) -> std::io::Result<ReadPi
/// At least make sure you flush your events to the server before
/// doing so.
///
/// # Safety
///
/// The provided file destructor must be a valid FD for writing, and will be closed
/// once the contents are written.
pub unsafe fn receive_to_fd(offer: &WlDataOffer, mime_type: String, writefd: RawFd) {
pub fn receive_to_fd(offer: &WlDataOffer, mime_type: String, writefd: OwnedFd) {
use nix::unistd::close;

offer.receive(mime_type, unsafe { BorrowedFd::borrow_raw(writefd) });
offer.receive(mime_type, writefd.as_fd());

if let Err(err) = close(writefd) {
if let Err(err) = close(writefd.into_raw_fd()) {
log::warn!("Failed to close write pipe: {}", err);
}
}
10 changes: 5 additions & 5 deletions src/primary_selection/offer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
os::unix::io::{BorrowedFd, FromRawFd, RawFd},
os::unix::io::{AsFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd},
sync::Mutex,
};

Expand Down Expand Up @@ -50,16 +50,16 @@ impl PrimarySelectionOffer {
Ok(unsafe { FromRawFd::from_raw_fd(readfd) })
}

/// # Safety
/// Request to receive the data of a given mime type, writen to `writefd`.
///
/// The provided file destructor must be a valid FD for writing, and will be closed
/// once the contents are written.
pub unsafe fn receive_to_fd(&self, mime_type: String, writefd: RawFd) {
pub fn receive_to_fd(&self, mime_type: String, writefd: OwnedFd) {
use nix::unistd::close;

self.offer.receive(mime_type, unsafe { BorrowedFd::borrow_raw(writefd) });
self.offer.receive(mime_type, writefd.as_fd());

if let Err(err) = close(writefd) {
if let Err(err) = close(writefd.into_raw_fd()) {
log::warn!("Failed to close write pipe: {}", err);
}
}
Expand Down
Loading