Skip to content

Commit

Permalink
Take OwnedFd instead of RawFd as argument to receive_to_fd
Browse files Browse the repository at this point in the history
This appears to be the only use of `RawFd` in a public API. With this
change, these functions should be safe to call.

I'm not sure if it would make more sense for these functions to not
close the file descriptor. In which case they could take `BorrowedFd`.
But this matches the current implementation, but with an IO safe type.
  • Loading branch information
ids1024 authored and wash2 committed Sep 22, 2023
1 parent a77fef4 commit 08f6081
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
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

0 comments on commit 08f6081

Please sign in to comment.