Replace FileDesc with File
We are only using FileDesc to get exclusive ownership of the V4L2 device
fd, but that can be done just as well by the standard File combined with
FromRawFd. Also Files can be passed across threads, contrary to
FileDesc.
diff --git a/Cargo.lock b/Cargo.lock
index aefe698..54b9846 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -64,15 +64,6 @@
]
[[package]]
-name = "fd"
-version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "275ab28c5c4076abec490e95cd4d69eaad40b6c16a2e692debd0e53453d006d0"
-dependencies = [
- "libc",
-]
-
-[[package]]
name = "hermit-abi"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -128,7 +119,6 @@
"bitflags",
"clap",
"ctrlc",
- "fd",
"nix",
]
diff --git a/Cargo.toml b/Cargo.toml
index dd889d1..cffba37 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,7 +8,6 @@
[dependencies]
nix = "0.17.0"
bitflags = "1.2.1"
-fd = "0.2.3"
# For example programs
[dev-dependencies]
diff --git a/examples/vicodec_test/ioctl_api.rs b/examples/vicodec_test/ioctl_api.rs
index c5c07ae..055533e 100644
--- a/examples/vicodec_test/ioctl_api.rs
+++ b/examples/vicodec_test/ioctl_api.rs
@@ -1,9 +1,10 @@
use super::framegen;
-use fd::FileDesc;
use nix::fcntl::{open, OFlag};
use nix::sys::stat::Mode;
use std::collections::BTreeMap;
+use std::fs::File;
use std::io::{self, Write};
+use std::os::unix::io::FromRawFd;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@@ -14,11 +15,12 @@
/// Run a sample encoder on device `device_path`, which must be a `vicodec`
/// encoder instance. `lets_quit` will turn to true when Ctrl+C is pressed.
pub fn run(device_path: &Path, lets_quit: Arc<AtomicBool>) {
- let mut fd = FileDesc::new(
- open(device_path, OFlag::O_RDWR | OFlag::O_CLOEXEC, Mode::empty())
- .expect(&format!("Cannot open {}", device_path.display())),
- true,
- );
+ let mut fd = unsafe {
+ File::from_raw_fd(
+ open(device_path, OFlag::O_RDWR | OFlag::O_CLOEXEC, Mode::empty())
+ .expect(&format!("Cannot open {}", device_path.display())),
+ )
+ };
// Check that we are dealing with vicodec.
let caps: Capability = querycap(&fd).expect("Failed to get device capacities");
@@ -192,5 +194,5 @@
reqbufs::<(), _>(&mut fd, output_queue, MemoryType::UserPtr, 0)
.expect("Failed to release output buffers");
- // The fd will be closed as the FileDesc instance gets out of scope.
+ // The fd will be closed as the File instance gets out of scope.
}
diff --git a/src/device.rs b/src/device.rs
index abbff31..1a12b1d 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -21,10 +21,10 @@
use super::ioctl::Capability;
use super::QueueType;
use super::Result;
-use fd::FileDesc;
use std::cell::RefCell;
use std::collections::BTreeSet;
-use std::os::unix::io::{AsRawFd, RawFd};
+use std::fs::File;
+use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::path::Path;
pub mod queue;
@@ -51,12 +51,12 @@
/// An opened V4L2 device. `Queue` objects can be instantiated from it.
pub struct Device {
pub capability: Capability,
- fd: RefCell<FileDesc>,
+ fd: RefCell<File>,
used_queues: RefCell<BTreeSet<QueueType>>,
}
impl Device {
- fn new(fd: FileDesc) -> Result<Self> {
+ fn new(fd: File) -> Result<Self> {
Ok(Device {
capability: ioctl::querycap(&fd)?,
fd: RefCell::new(fd),
@@ -75,7 +75,10 @@
false => OFlag::empty(),
};
- Device::new(FileDesc::new(open(path, flags, Mode::empty())?, true))
+ let fd = open(path, flags, Mode::empty())?;
+
+ // Safe because we are constructing a file from Fd we just opened.
+ Device::new(unsafe { File::from_raw_fd(fd) })
}
}
diff --git a/src/memory/dmabuf.rs b/src/memory/dmabuf.rs
index b5f73c8..1af1de2 100644
--- a/src/memory/dmabuf.rs
+++ b/src/memory/dmabuf.rs
@@ -1,8 +1,8 @@
//! Operations specific to DMABuf-type buffers.
use super::*;
use crate::bindings;
-use fd::FileDesc;
use std::default::Default;
+use std::fs::File;
use std::os::unix::io::{AsRawFd, RawFd};
/// Handle for a DMABUF buffer. These buffers are backed by DMABuf-shared
@@ -34,14 +34,14 @@
pub struct DMABuf;
-/// DMABUF buffers support for queues. This takes a `FileDesc` containing the
+/// DMABUF buffers support for queues. This takes a `File` containing the
/// DMABUF file description as `qbuf` input, and gives it back as output so it
/// can be reused.
///
/// TODO Reusing the same DMABUF on the save V4L2 buffer saves some processing
/// in the kernel, so maybe some binding or other affinity should be done?
impl Memory for DMABuf {
- type QBufType = FileDesc;
+ type QBufType = File;
type DQBufType = Self::QBufType;
type HandleType = DMABufHandle;